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___
9 #include "nsIMIMEInfo.h"
13 #include "nsIMutableArray.h"
17 #include "nsIProcess.h"
20 * UTF8 moz-icon URI string for the default handler application's icon, if
23 #define PROPERTY_DEFAULT_APP_ICON_URL "defaultApplicationIconURL"
25 * UTF8 moz-icon URI string for the user's preferred handler application's
28 #define PROPERTY_CUSTOM_APP_ICON_URL "customApplicationIconURL"
31 * Basic implementation of nsIMIMEInfo. Incomplete - it is meant to be
32 * subclassed, and GetHasDefaultHandler as well as LaunchDefaultWithFile need to
35 class nsMIMEInfoBase
: public nsIMIMEInfo
{
37 NS_DECL_THREADSAFE_ISUPPORTS
39 // I'd use NS_DECL_NSIMIMEINFO, but I don't want GetHasDefaultHandler
40 NS_IMETHOD
GetFileExtensions(nsIUTF8StringEnumerator
**_retval
);
41 NS_IMETHOD
SetFileExtensions(const nsACString
& aExtensions
);
42 NS_IMETHOD
ExtensionExists(const nsACString
& aExtension
, bool *_retval
);
43 NS_IMETHOD
AppendExtension(const nsACString
& aExtension
);
44 NS_IMETHOD
GetPrimaryExtension(nsACString
& aPrimaryExtension
);
45 NS_IMETHOD
SetPrimaryExtension(const nsACString
& aPrimaryExtension
);
46 NS_IMETHOD
GetType(nsACString
& aType
);
47 NS_IMETHOD
GetMIMEType(nsACString
& aMIMEType
);
48 NS_IMETHOD
GetDescription(nsAString
& aDescription
);
49 NS_IMETHOD
SetDescription(const nsAString
& aDescription
);
50 NS_IMETHOD
Equals(nsIMIMEInfo
*aMIMEInfo
, bool *_retval
);
51 NS_IMETHOD
GetPreferredApplicationHandler(nsIHandlerApp
* *aPreferredAppHandler
);
52 NS_IMETHOD
SetPreferredApplicationHandler(nsIHandlerApp
* aPreferredAppHandler
);
53 NS_IMETHOD
GetPossibleApplicationHandlers(nsIMutableArray
* *aPossibleAppHandlers
);
54 NS_IMETHOD
GetDefaultDescription(nsAString
& aDefaultDescription
);
55 NS_IMETHOD
LaunchWithFile(nsIFile
*aFile
);
56 NS_IMETHOD
LaunchWithURI(nsIURI
*aURI
,
57 nsIInterfaceRequestor
*aWindowContext
);
58 NS_IMETHOD
GetPreferredAction(nsHandlerInfoAction
*aPreferredAction
);
59 NS_IMETHOD
SetPreferredAction(nsHandlerInfoAction aPreferredAction
);
60 NS_IMETHOD
GetAlwaysAskBeforeHandling(bool *aAlwaysAskBeforeHandling
);
61 NS_IMETHOD
SetAlwaysAskBeforeHandling(bool aAlwaysAskBeforeHandling
);
62 NS_IMETHOD
GetPossibleLocalHandlers(nsIArray
**_retval
);
69 // nsMIMEInfoBase methods
70 explicit nsMIMEInfoBase(const char *aMIMEType
= "");
71 explicit nsMIMEInfoBase(const nsACString
& aMIMEType
);
72 nsMIMEInfoBase(const nsACString
& aType
, HandlerClass aClass
);
74 void SetMIMEType(const nsACString
& aMIMEType
) { mSchemeOrType
= aMIMEType
; }
76 void SetDefaultDescription(const nsString
& aDesc
) { mDefaultAppDescription
= aDesc
; }
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.
86 void CopyBasicDataTo(nsMIMEInfoBase
* aOther
);
89 * Return whether this MIMEInfo has any extensions
91 bool HasExtensions() const { return mExtensions
.Length() != 0; }
94 virtual ~nsMIMEInfoBase(); // must be virtual, as the the base class's Release should call the subclass's destructor
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.
101 * @param aFile The file that should be opened
103 virtual nsresult
LaunchDefaultWithFile(nsIFile
* aFile
) = 0;
106 * Loads the URI with the OS default app.
108 * @param aURI The URI to pass off to the OS.
110 virtual nsresult
LoadUriInternal(nsIURI
*aURI
) = 0;
112 static already_AddRefed
<nsIProcess
> InitProcess(nsIFile
* aApp
,
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.
121 * @param aApp The application to launch (may not be null)
122 * @param aArg The argument to pass on the command line
124 static nsresult
LaunchWithIProcess(nsIFile
* aApp
,
125 const nsCString
&aArg
);
126 static nsresult
LaunchWithIProcess(nsIFile
* aApp
,
127 const nsString
&aArg
);
130 * Given a file: nsIURI, return the associated nsIFile
132 * @param aURI the file: URI in question
133 * @param aFile the associated nsIFile (out param)
135 static nsresult
GetLocalFileFromURI(nsIURI
*aURI
,
139 nsTArray
<nsCString
> mExtensions
; ///< array of file extensions associated w/ this MIME obj
140 nsString mDescription
; ///< human readable description
141 nsCString mSchemeOrType
;
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
;
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.
160 class nsMIMEInfoImpl
: public nsMIMEInfoBase
{
162 explicit nsMIMEInfoImpl(const char *aMIMEType
= "") : nsMIMEInfoBase(aMIMEType
) {}
163 explicit nsMIMEInfoImpl(const nsACString
& aMIMEType
) : nsMIMEInfoBase(aMIMEType
) {}
164 nsMIMEInfoImpl(const nsACString
& aType
, HandlerClass aClass
) :
165 nsMIMEInfoBase(aType
, aClass
) {}
166 virtual ~nsMIMEInfoImpl() {}
168 // nsIMIMEInfo methods
169 NS_IMETHOD
GetHasDefaultHandler(bool *_retval
);
170 NS_IMETHOD
GetDefaultDescription(nsAString
& aDefaultDescription
);
172 // additional methods
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.
177 void SetDefaultApplication(nsIFile
* aApp
) { if (!mDefaultApplication
) mDefaultApplication
= aApp
; }
180 // nsMIMEInfoBase methods
182 * The base class implementation is to use LaunchWithIProcess in combination
183 * with mDefaultApplication. Subclasses can override that behaviour.
185 virtual nsresult
LaunchDefaultWithFile(nsIFile
* aFile
);
188 * Loads the URI with the OS default app. This should be overridden by each
189 * OS's implementation.
191 virtual nsresult
LoadUriInternal(nsIURI
*aURI
) = 0;
193 nsCOMPtr
<nsIFile
> mDefaultApplication
; ///< default application associated with this type.
196 #endif //__nsmimeinfoimpl_h___