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