Bug 1760193 [wpt PR 33188] - Update wpt metadata, a=testonly
[gecko.git] / uriloader / exthandler / nsMIMEInfoImpl.h
blob9e7c4a47991536100b22d80d3bb96450b64ed4ca
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"
10 #include "nsAtom.h"
11 #include "nsString.h"
12 #include "nsTArray.h"
13 #include "nsIMutableArray.h"
14 #include "nsIFile.h"
15 #include "nsCOMPtr.h"
16 #include "nsIURI.h"
17 #include "nsIProcess.h"
18 #include "mozilla/dom/BrowsingContext.h"
20 /**
21 * UTF8 moz-icon URI string for the default handler application's icon, if
22 * available.
24 #define PROPERTY_DEFAULT_APP_ICON_URL "defaultApplicationIconURL"
25 /**
26 * UTF8 moz-icon URI string for the user's preferred handler application's
27 * icon, if available.
29 #define PROPERTY_CUSTOM_APP_ICON_URL "customApplicationIconURL"
31 /**
32 * Basic implementation of nsIMIMEInfo. Incomplete - it is meant to be
33 * subclassed, and GetHasDefaultHandler as well as LaunchDefaultWithFile need to
34 * be implemented.
36 class nsMIMEInfoBase : public nsIMIMEInfo {
37 public:
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;
83 /**
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);
93 /**
94 * Return whether this MIMEInfo has any extensions
96 bool HasExtensions() const { return mExtensions.Length() != 0; }
98 static already_AddRefed<nsIFile> GetCanonicalExecutable(nsIFile* aFile);
100 protected:
101 virtual ~nsMIMEInfoBase(); // must be virtual, as the the base class's
102 // Release should call the subclass's destructor
105 * Launch the default application for the given file.
106 * For even more control over the launching, override launchWithFile.
107 * Also see the comment about nsIMIMEInfo in general, above.
109 * @param aFile The file that should be opened
111 virtual nsresult LaunchDefaultWithFile(nsIFile* aFile) = 0;
114 * Loads the URI with the OS default app.
116 * @param aURI The URI to pass off to the OS.
118 virtual nsresult LoadUriInternal(nsIURI* aURI) = 0;
120 bool AutomationOnlyCheckIfLaunchStubbed(nsIFile* aFile);
122 static already_AddRefed<nsIProcess> InitProcess(nsIFile* aApp,
123 nsresult* aResult);
126 * This method can be used to launch the file or URI with a single
127 * argument (typically either a file path or a URI spec). This is
128 * meant as a helper method for implementations of
129 * LaunchWithURI/LaunchDefaultWithFile.
131 * @param aApp The application to launch (may not be null)
132 * @param aArg The argument to pass on the command line
134 static nsresult LaunchWithIProcess(nsIFile* aApp, const nsCString& aArg);
135 static nsresult LaunchWithIProcess(nsIFile* aApp, const nsString& aArg);
136 static nsresult LaunchWithIProcess(nsIFile* aApp, const int aArgc,
137 const char16_t** aArgv);
140 * Given a file: nsIURI, return the associated nsIFile
142 * @param aURI the file: URI in question
143 * @param aFile the associated nsIFile (out param)
145 static nsresult GetLocalFileFromURI(nsIURI* aURI, nsIFile** aFile);
148 * Internal helper to avoid adding duplicates.
150 void AddUniqueExtension(const nsACString& aExtension);
152 // member variables
153 nsTArray<nsCString>
154 mExtensions; ///< array of file extensions associated w/ this MIME obj
155 nsString mDescription; ///< human readable description
156 nsCString mSchemeOrType;
157 HandlerClass mClass;
158 nsCOMPtr<nsIHandlerApp> mPreferredApplication;
159 nsCOMPtr<nsIMutableArray> mPossibleApplications;
160 nsHandlerInfoAction
161 mPreferredAction; ///< preferred action to associate with this type
162 nsString mPreferredAppDescription;
163 nsString mDefaultAppDescription;
164 bool mAlwaysAskBeforeHandling;
168 * This is a complete implementation of nsIMIMEInfo, and contains all necessary
169 * methods. However, depending on your platform you may want to use a different
170 * way of launching applications. This class stores the default application in a
171 * member variable and provides a function for setting it. For local
172 * applications, launching is done using nsIProcess, native path of the file to
173 * open as first argument.
175 class nsMIMEInfoImpl : public nsMIMEInfoBase {
176 public:
177 explicit nsMIMEInfoImpl(const char* aMIMEType = "")
178 : nsMIMEInfoBase(aMIMEType) {}
179 explicit nsMIMEInfoImpl(const nsACString& aMIMEType)
180 : nsMIMEInfoBase(aMIMEType) {}
181 nsMIMEInfoImpl(const nsACString& aType, HandlerClass aClass)
182 : nsMIMEInfoBase(aType, aClass) {}
183 virtual ~nsMIMEInfoImpl() {}
185 // nsIMIMEInfo methods
186 NS_IMETHOD GetHasDefaultHandler(bool* _retval) override;
187 NS_IMETHOD GetDefaultDescription(nsAString& aDefaultDescription) override;
188 NS_IMETHOD IsCurrentAppOSDefault(bool* _retval) override;
190 // additional methods
192 * Sets the default application. Supposed to be only called by the OS Helper
193 * App Services; the default application is immutable after it is first set.
195 void SetDefaultApplication(nsIFile* aApp) {
196 if (!mDefaultApplication) mDefaultApplication = aApp;
199 protected:
200 // nsMIMEInfoBase methods
202 * The base class implementation is to use LaunchWithIProcess in combination
203 * with mDefaultApplication. Subclasses can override that behaviour.
205 virtual nsresult LaunchDefaultWithFile(nsIFile* aFile) override;
208 * Loads the URI with the OS default app. This should be overridden by each
209 * OS's implementation.
211 virtual nsresult LoadUriInternal(nsIURI* aURI) override = 0;
213 nsCOMPtr<nsIFile>
214 mDefaultApplication; ///< default application associated with this type.
217 #endif //__nsmimeinfoimpl_h___