Bug 1890277: part 1) Add CSP parser tests for `require-trusted-types-for`. r=tschuster
[gecko.git] / uriloader / exthandler / nsMIMEInfoImpl.h
blobd04566b92c58200d69c769f0df753061b5ecd0ae
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 GetDefaultExecutable(nsIFile** aExecutable) override;
61 NS_IMETHOD LaunchWithFile(nsIFile* aFile) override;
62 NS_IMETHOD LaunchWithURI(
63 nsIURI* aURI, mozilla::dom::BrowsingContext* aBrowsingContext) override;
64 NS_IMETHOD GetPreferredAction(nsHandlerInfoAction* aPreferredAction) override;
65 NS_IMETHOD SetPreferredAction(nsHandlerInfoAction aPreferredAction) override;
66 NS_IMETHOD GetAlwaysAskBeforeHandling(
67 bool* aAlwaysAskBeforeHandling) override;
68 NS_IMETHOD SetAlwaysAskBeforeHandling(bool aAlwaysAskBeforeHandling) override;
69 NS_IMETHOD GetPossibleLocalHandlers(nsIArray** _retval) override;
71 enum HandlerClass { eMIMEInfo, eProtocolInfo };
73 // nsMIMEInfoBase methods
74 explicit nsMIMEInfoBase(const char* aMIMEType = "");
75 explicit nsMIMEInfoBase(const nsACString& aMIMEType);
76 nsMIMEInfoBase(const nsACString& aType, HandlerClass aClass);
78 void SetMIMEType(const nsACString& aMIMEType) { mSchemeOrType = aMIMEType; }
80 void SetDefaultDescription(const nsString& aDesc) {
81 mDefaultAppDescription = aDesc;
84 /**
85 * Copies basic data of this MIME Info Implementation to the given other
86 * MIME Info. The data consists of the MIME Type, the (default) description,
87 * the MacOS type and creator, and the extension list (this object's
88 * extension list will replace aOther's list, not append to it). This
89 * function also ensures that aOther's primary extension will be the same as
90 * the one of this object.
92 void CopyBasicDataTo(nsMIMEInfoBase* aOther);
94 /**
95 * Return whether this MIMEInfo has any extensions
97 bool HasExtensions() const { return mExtensions.Length() != 0; }
99 static already_AddRefed<nsIFile> GetCanonicalExecutable(nsIFile* aFile);
101 protected:
102 virtual ~nsMIMEInfoBase(); // must be virtual, as the the base class's
103 // Release should call the subclass's destructor
106 * Launch the default application for the given file.
107 * For even more control over the launching, override launchWithFile.
108 * Also see the comment about nsIMIMEInfo in general, above.
110 * @param aFile The file that should be opened
112 virtual nsresult LaunchDefaultWithFile(nsIFile* aFile) = 0;
115 * Loads the URI with the OS default app.
117 * @param aURI The URI to pass off to the OS.
119 virtual nsresult LoadUriInternal(nsIURI* aURI) = 0;
121 bool AutomationOnlyCheckIfLaunchStubbed(nsIFile* aFile);
123 static already_AddRefed<nsIProcess> InitProcess(nsIFile* aApp,
124 nsresult* aResult);
127 * This method can be used to launch the file or URI with a single
128 * argument (typically either a file path or a URI spec). This is
129 * meant as a helper method for implementations of
130 * LaunchWithURI/LaunchDefaultWithFile.
132 * @param aApp The application to launch (may not be null)
133 * @param aArg The argument to pass on the command line
135 static nsresult LaunchWithIProcess(nsIFile* aApp, const nsCString& aArg);
136 static nsresult LaunchWithIProcess(nsIFile* aApp, const nsString& aArg);
137 static nsresult LaunchWithIProcess(nsIFile* aApp, const int aArgc,
138 const char16_t** aArgv);
141 * Given a file: nsIURI, return the associated nsIFile
143 * @param aURI the file: URI in question
144 * @param aFile the associated nsIFile (out param)
146 static nsresult GetLocalFileFromURI(nsIURI* aURI, nsIFile** aFile);
149 * Internal helper to avoid adding duplicates.
151 void AddUniqueExtension(const nsACString& aExtension);
153 // member variables
154 nsTArray<nsCString>
155 mExtensions; ///< array of file extensions associated w/ this MIME obj
156 nsString mDescription; ///< human readable description
157 nsCString mSchemeOrType;
158 HandlerClass mClass;
159 nsCOMPtr<nsIHandlerApp> mPreferredApplication;
160 nsCOMPtr<nsIMutableArray> mPossibleApplications;
161 nsHandlerInfoAction mPreferredAction =
162 nsIMIMEInfo::saveToDisk; ///< preferred action to associate with this
163 ///< type
164 nsString mPreferredAppDescription;
165 nsString mDefaultAppDescription;
166 bool mAlwaysAskBeforeHandling;
167 bool mIsDefaultAppInfoFresh = false;
171 * This is a complete implementation of nsIMIMEInfo, and contains all necessary
172 * methods. However, depending on your platform you may want to use a different
173 * way of launching applications. This class stores the default application in a
174 * member variable and provides a function for setting it. For local
175 * applications, launching is done using nsIProcess, native path of the file to
176 * open as first argument.
178 class nsMIMEInfoImpl : public nsMIMEInfoBase {
179 public:
180 explicit nsMIMEInfoImpl(const char* aMIMEType = "")
181 : nsMIMEInfoBase(aMIMEType) {}
182 explicit nsMIMEInfoImpl(const nsACString& aMIMEType)
183 : nsMIMEInfoBase(aMIMEType) {}
184 nsMIMEInfoImpl(const nsACString& aType, HandlerClass aClass)
185 : nsMIMEInfoBase(aType, aClass) {}
186 virtual ~nsMIMEInfoImpl() {}
188 // nsIMIMEInfo methods
189 NS_IMETHOD GetHasDefaultHandler(bool* _retval) override;
190 NS_IMETHOD GetDefaultDescription(nsAString& aDefaultDescription) override;
191 NS_IMETHOD GetDefaultExecutable(nsIFile** aExecutable) override;
192 NS_IMETHOD IsCurrentAppOSDefault(bool* _retval) override;
194 // additional methods
196 * Sets the default application. Supposed to be only called by the OS Helper
197 * App Services.
199 void SetDefaultApplication(nsIFile* aApp) {
200 mDefaultApplication = aApp;
201 mIsDefaultAppInfoFresh = true;
204 protected:
205 // nsMIMEInfoBase methods
207 * The base class implementation is to use LaunchWithIProcess in combination
208 * with mDefaultApplication. Subclasses can override that behaviour.
210 virtual nsresult LaunchDefaultWithFile(nsIFile* aFile) override;
213 * Loads the URI with the OS default app. This should be overridden by each
214 * OS's implementation.
216 virtual nsresult LoadUriInternal(nsIURI* aURI) override = 0;
218 // Accessor for default application for subclasses.
219 nsIFile* GetDefaultApplication() { return mDefaultApplication; }
221 private:
222 nsCOMPtr<nsIFile>
223 mDefaultApplication; ///< default application associated with this type.
226 #endif //__nsmimeinfoimpl_h___