LCOV - code coverage report
Current view: directory - uriloader/exthandler - nsMIMEInfoImpl.h (source / functions) Found Hit Coverage
Test: app.info Lines: 8 6 75.0 %
Date: 2012-06-02 Functions: 8 5 62.5 %

       1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 : /* vim:set ts=4 sw=4 sts=4 et: */
       3                 : /* ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is mozilla.org code.
      17                 :  *
      18                 :  * The Initial Developer of the Original Code is
      19                 :  * Netscape Communications Corporation.
      20                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      21                 :  * the Initial Developer. All Rights Reserved.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *   Dan Mosedale <dmose@mozilla.org>
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      28                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : #ifndef __nsmimeinfoimpl_h___
      40                 : #define __nsmimeinfoimpl_h___
      41                 : 
      42                 : #include "nsIMIMEInfo.h"
      43                 : #include "nsIAtom.h"
      44                 : #include "nsString.h"
      45                 : #include "nsTArray.h"
      46                 : #include "nsIMutableArray.h"
      47                 : #include "nsIFile.h"
      48                 : #include "nsCOMPtr.h"
      49                 : #include "nsIURI.h"
      50                 : #include "nsIProcess.h"
      51                 : 
      52                 : /** 
      53                 :  * UTF8 moz-icon URI string for the default handler application's icon, if 
      54                 :  * available.
      55                 :  */
      56                 : #define PROPERTY_DEFAULT_APP_ICON_URL "defaultApplicationIconURL"
      57                 : /** 
      58                 :  * UTF8 moz-icon URI string for the user's preferred handler application's 
      59                 :  * icon, if available.
      60                 :  */
      61                 : #define PROPERTY_CUSTOM_APP_ICON_URL "customApplicationIconURL"
      62                 : 
      63                 : /**
      64                 :  * Basic implementation of nsIMIMEInfo. Incomplete - it is meant to be
      65                 :  * subclassed, and GetHasDefaultHandler as well as LaunchDefaultWithFile need to
      66                 :  * be implemented.
      67                 :  */
      68                 : class nsMIMEInfoBase : public nsIMIMEInfo {
      69                 :   public:
      70                 :     NS_DECL_ISUPPORTS
      71                 : 
      72                 :     // I'd use NS_DECL_NSIMIMEINFO, but I don't want GetHasDefaultHandler
      73                 :     NS_IMETHOD GetFileExtensions(nsIUTF8StringEnumerator **_retval);
      74                 :     NS_IMETHOD SetFileExtensions(const nsACString & aExtensions);
      75                 :     NS_IMETHOD ExtensionExists(const nsACString & aExtension, bool *_retval);
      76                 :     NS_IMETHOD AppendExtension(const nsACString & aExtension);
      77                 :     NS_IMETHOD GetPrimaryExtension(nsACString & aPrimaryExtension);
      78                 :     NS_IMETHOD SetPrimaryExtension(const nsACString & aPrimaryExtension);
      79                 :     NS_IMETHOD GetType(nsACString & aType);
      80                 :     NS_IMETHOD GetMIMEType(nsACString & aMIMEType);
      81                 :     NS_IMETHOD GetDescription(nsAString & aDescription);
      82                 :     NS_IMETHOD SetDescription(const nsAString & aDescription);
      83                 :     NS_IMETHOD Equals(nsIMIMEInfo *aMIMEInfo, bool *_retval);
      84                 :     NS_IMETHOD GetPreferredApplicationHandler(nsIHandlerApp * *aPreferredAppHandler);
      85                 :     NS_IMETHOD SetPreferredApplicationHandler(nsIHandlerApp * aPreferredAppHandler);
      86                 :     NS_IMETHOD GetPossibleApplicationHandlers(nsIMutableArray * *aPossibleAppHandlers);
      87                 :     NS_IMETHOD GetDefaultDescription(nsAString & aDefaultDescription);
      88                 :     NS_IMETHOD LaunchWithFile(nsIFile *aFile);
      89                 :     NS_IMETHOD LaunchWithURI(nsIURI *aURI,
      90                 :                              nsIInterfaceRequestor *aWindowContext);
      91                 :     NS_IMETHOD GetPreferredAction(nsHandlerInfoAction *aPreferredAction);
      92                 :     NS_IMETHOD SetPreferredAction(nsHandlerInfoAction aPreferredAction);
      93                 :     NS_IMETHOD GetAlwaysAskBeforeHandling(bool *aAlwaysAskBeforeHandling);
      94                 :     NS_IMETHOD SetAlwaysAskBeforeHandling(bool aAlwaysAskBeforeHandling); 
      95                 :     NS_IMETHOD GetPossibleLocalHandlers(nsIArray **_retval); 
      96                 : 
      97                 :     enum HandlerClass {
      98                 :       eMIMEInfo,
      99                 :       eProtocolInfo
     100                 :     };
     101                 : 
     102                 :     // nsMIMEInfoBase methods
     103                 :     nsMIMEInfoBase(const char *aMIMEType = "") NS_HIDDEN;
     104                 :     nsMIMEInfoBase(const nsACString& aMIMEType) NS_HIDDEN;
     105                 :     nsMIMEInfoBase(const nsACString& aType, HandlerClass aClass) NS_HIDDEN;
     106                 :     virtual ~nsMIMEInfoBase();        // must be virtual, as the the base class's Release should call the subclass's destructor
     107                 : 
     108               0 :     void SetMIMEType(const nsACString & aMIMEType) { mSchemeOrType = aMIMEType; }
     109                 : 
     110              51 :     void SetDefaultDescription(const nsString& aDesc) { mDefaultAppDescription = aDesc; }
     111                 : 
     112                 :     /**
     113                 :      * Copies basic data of this MIME Info Implementation to the given other
     114                 :      * MIME Info. The data consists of the MIME Type, the (default) description,
     115                 :      * the MacOS type and creator, and the extension list (this object's
     116                 :      * extension list will replace aOther's list, not append to it). This
     117                 :      * function also ensures that aOther's primary extension will be the same as
     118                 :      * the one of this object.
     119                 :      */
     120                 :     void CopyBasicDataTo(nsMIMEInfoBase* aOther);
     121                 : 
     122                 :     /**
     123                 :      * Return whether this MIMEInfo has any extensions
     124                 :      */
     125              24 :     bool HasExtensions() const { return mExtensions.Length() != 0; }
     126                 : 
     127                 :   protected:
     128                 :     /**
     129                 :      * Launch the default application for the given file.
     130                 :      * For even more control over the launching, override launchWithFile.
     131                 :      * Also see the comment about nsIMIMEInfo in general, above.
     132                 :      *
     133                 :      * @param aFile The file that should be opened
     134                 :      */
     135                 :     virtual NS_HIDDEN_(nsresult) LaunchDefaultWithFile(nsIFile* aFile) = 0;
     136                 : 
     137                 :     /**
     138                 :      * Loads the URI with the OS default app.
     139                 :      *
     140                 :      * @param aURI The URI to pass off to the OS.
     141                 :      */
     142                 :     virtual NS_HIDDEN_(nsresult) LoadUriInternal(nsIURI *aURI) = 0;
     143                 : 
     144                 :     static already_AddRefed<nsIProcess> InitProcess(nsIFile* aApp,
     145                 :                                                     nsresult* aResult);
     146                 : 
     147                 :     /**
     148                 :      * This method can be used to launch the file or URI with a single 
     149                 :      * argument (typically either a file path or a URI spec).  This is 
     150                 :      * meant as a helper method for implementations of
     151                 :      * LaunchWithURI/LaunchDefaultWithFile.
     152                 :      *
     153                 :      * @param aApp The application to launch (may not be null)
     154                 :      * @param aArg The argument to pass on the command line
     155                 :      */
     156                 :     static NS_HIDDEN_(nsresult) LaunchWithIProcess(nsIFile* aApp,
     157                 :                                                    const nsCString &aArg);
     158                 :     static NS_HIDDEN_(nsresult) LaunchWithIProcess(nsIFile* aApp,
     159                 :                                                    const nsString &aArg);
     160                 : 
     161                 :     /**
     162                 :      * Given a file: nsIURI, return the associated nsILocalFile
     163                 :      *
     164                 :      * @param  aURI      the file: URI in question
     165                 :      * @param  aFile     the associated nsILocalFile (out param)
     166                 :      */
     167                 :     static NS_HIDDEN_(nsresult) GetLocalFileFromURI(nsIURI *aURI,
     168                 :                                                     nsILocalFile **aFile);
     169                 : 
     170                 :     // member variables
     171                 :     nsTArray<nsCString>    mExtensions; ///< array of file extensions associated w/ this MIME obj
     172                 :     nsString               mDescription; ///< human readable description
     173                 :     nsCString              mSchemeOrType;
     174                 :     HandlerClass           mClass;
     175                 :     nsCOMPtr<nsIHandlerApp> mPreferredApplication;
     176                 :     nsCOMPtr<nsIMutableArray> mPossibleApplications;
     177                 :     nsHandlerInfoAction    mPreferredAction; ///< preferred action to associate with this type
     178                 :     nsString               mPreferredAppDescription;
     179                 :     nsString               mDefaultAppDescription;
     180                 :     bool                   mAlwaysAskBeforeHandling;
     181                 : };
     182                 : 
     183                 : 
     184                 : /**
     185                 :  * This is a complete implementation of nsIMIMEInfo, and contains all necessary
     186                 :  * methods. However, depending on your platform you may want to use a different
     187                 :  * way of launching applications. This class stores the default application in a
     188                 :  * member variable and provides a function for setting it. For local
     189                 :  * applications, launching is done using nsIProcess, native path of the file to
     190                 :  * open as first argument.
     191                 :  */
     192                 : class nsMIMEInfoImpl : public nsMIMEInfoBase {
     193                 :   public:
     194                 :     nsMIMEInfoImpl(const char *aMIMEType = "") : nsMIMEInfoBase(aMIMEType) {}
     195             120 :     nsMIMEInfoImpl(const nsACString& aMIMEType) : nsMIMEInfoBase(aMIMEType) {}
     196              25 :     nsMIMEInfoImpl(const nsACString& aType, HandlerClass aClass) :
     197              25 :       nsMIMEInfoBase(aType, aClass) {}
     198             290 :     virtual ~nsMIMEInfoImpl() {}
     199                 : 
     200                 :     // nsIMIMEInfo methods
     201                 :     NS_IMETHOD GetHasDefaultHandler(bool *_retval);
     202                 :     NS_IMETHOD GetDefaultDescription(nsAString& aDefaultDescription);
     203                 : 
     204                 :     // additional methods
     205                 :     /**
     206                 :      * Sets the default application. Supposed to be only called by the OS Helper
     207                 :      * App Services; the default application is immutable after it is first set.
     208                 :      */
     209               0 :     void SetDefaultApplication(nsIFile* aApp) { if (!mDefaultApplication) mDefaultApplication = aApp; }
     210                 : 
     211                 :   protected:
     212                 :     // nsMIMEInfoBase methods
     213                 :     /**
     214                 :      * The base class implementation is to use LaunchWithIProcess in combination
     215                 :      * with mDefaultApplication. Subclasses can override that behaviour.
     216                 :      */
     217                 :     virtual NS_HIDDEN_(nsresult) LaunchDefaultWithFile(nsIFile* aFile);
     218                 : 
     219                 :     /**
     220                 :      * Loads the URI with the OS default app.  This should be overridden by each
     221                 :      * OS's implementation.
     222                 :      */
     223                 :     virtual NS_HIDDEN_(nsresult) LoadUriInternal(nsIURI *aURI) = 0;
     224                 : 
     225                 :     nsCOMPtr<nsIFile>      mDefaultApplication; ///< default application associated with this type.
     226                 : };
     227                 : 
     228                 : #endif //__nsmimeinfoimpl_h___

Generated by: LCOV version 1.7