uriloader/exthandler/nsMIMEInfoImpl.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial