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