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
{
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 nsMIMEInfoBase(const char *aMIMEType
= "") NS_HIDDEN
;
71 nsMIMEInfoBase(const nsACString
& aMIMEType
) NS_HIDDEN
;
72 nsMIMEInfoBase(const nsACString
& aType
, HandlerClass aClass
) NS_HIDDEN
;
73 virtual ~nsMIMEInfoBase(); // must be virtual, as the the base class's Release should call the subclass's destructor
75 void SetMIMEType(const nsACString
& aMIMEType
) { mSchemeOrType
= aMIMEType
; }
77 void SetDefaultDescription(const nsString
& aDesc
) { mDefaultAppDescription
= aDesc
; }
80 * Copies basic data of this MIME Info Implementation to the given other
81 * MIME Info. The data consists of the MIME Type, the (default) description,
82 * the MacOS type and creator, and the extension list (this object's
83 * extension list will replace aOther's list, not append to it). This
84 * function also ensures that aOther's primary extension will be the same as
85 * the one of this object.
87 void CopyBasicDataTo(nsMIMEInfoBase
* aOther
);
90 * Return whether this MIMEInfo has any extensions
92 bool HasExtensions() const { return mExtensions
.Length() != 0; }
96 * Launch the default application for the given file.
97 * For even more control over the launching, override launchWithFile.
98 * Also see the comment about nsIMIMEInfo in general, above.
100 * @param aFile The file that should be opened
102 virtual NS_HIDDEN_(nsresult
) LaunchDefaultWithFile(nsIFile
* aFile
) = 0;
105 * Loads the URI with the OS default app.
107 * @param aURI The URI to pass off to the OS.
109 virtual NS_HIDDEN_(nsresult
) LoadUriInternal(nsIURI
*aURI
) = 0;
111 static already_AddRefed
<nsIProcess
> InitProcess(nsIFile
* aApp
,
115 * This method can be used to launch the file or URI with a single
116 * argument (typically either a file path or a URI spec). This is
117 * meant as a helper method for implementations of
118 * LaunchWithURI/LaunchDefaultWithFile.
120 * @param aApp The application to launch (may not be null)
121 * @param aArg The argument to pass on the command line
123 static NS_HIDDEN_(nsresult
) LaunchWithIProcess(nsIFile
* aApp
,
124 const nsCString
&aArg
);
125 static NS_HIDDEN_(nsresult
) LaunchWithIProcess(nsIFile
* aApp
,
126 const nsString
&aArg
);
129 * Given a file: nsIURI, return the associated nsIFile
131 * @param aURI the file: URI in question
132 * @param aFile the associated nsIFile (out param)
134 static NS_HIDDEN_(nsresult
) GetLocalFileFromURI(nsIURI
*aURI
,
138 nsTArray
<nsCString
> mExtensions
; ///< array of file extensions associated w/ this MIME obj
139 nsString mDescription
; ///< human readable description
140 nsCString mSchemeOrType
;
142 nsCOMPtr
<nsIHandlerApp
> mPreferredApplication
;
143 nsCOMPtr
<nsIMutableArray
> mPossibleApplications
;
144 nsHandlerInfoAction mPreferredAction
; ///< preferred action to associate with this type
145 nsString mPreferredAppDescription
;
146 nsString mDefaultAppDescription
;
147 bool mAlwaysAskBeforeHandling
;
152 * This is a complete implementation of nsIMIMEInfo, and contains all necessary
153 * methods. However, depending on your platform you may want to use a different
154 * way of launching applications. This class stores the default application in a
155 * member variable and provides a function for setting it. For local
156 * applications, launching is done using nsIProcess, native path of the file to
157 * open as first argument.
159 class nsMIMEInfoImpl
: public nsMIMEInfoBase
{
161 nsMIMEInfoImpl(const char *aMIMEType
= "") : nsMIMEInfoBase(aMIMEType
) {}
162 nsMIMEInfoImpl(const nsACString
& aMIMEType
) : nsMIMEInfoBase(aMIMEType
) {}
163 nsMIMEInfoImpl(const nsACString
& aType
, HandlerClass aClass
) :
164 nsMIMEInfoBase(aType
, aClass
) {}
165 virtual ~nsMIMEInfoImpl() {}
167 // nsIMIMEInfo methods
168 NS_IMETHOD
GetHasDefaultHandler(bool *_retval
);
169 NS_IMETHOD
GetDefaultDescription(nsAString
& aDefaultDescription
);
171 // additional methods
173 * Sets the default application. Supposed to be only called by the OS Helper
174 * App Services; the default application is immutable after it is first set.
176 void SetDefaultApplication(nsIFile
* aApp
) { if (!mDefaultApplication
) mDefaultApplication
= aApp
; }
179 // nsMIMEInfoBase methods
181 * The base class implementation is to use LaunchWithIProcess in combination
182 * with mDefaultApplication. Subclasses can override that behaviour.
184 virtual NS_HIDDEN_(nsresult
) LaunchDefaultWithFile(nsIFile
* aFile
);
187 * Loads the URI with the OS default app. This should be overridden by each
188 * OS's implementation.
190 virtual NS_HIDDEN_(nsresult
) LoadUriInternal(nsIURI
*aURI
) = 0;
192 nsCOMPtr
<nsIFile
> mDefaultApplication
; ///< default application associated with this type.
195 #endif //__nsmimeinfoimpl_h___