1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/uriloader/exthandler/nsMIMEInfoImpl.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,195 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim:set ts=4 sw=4 sts=4 et: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 +#ifndef __nsmimeinfoimpl_h___ 1.10 +#define __nsmimeinfoimpl_h___ 1.11 + 1.12 +#include "nsIMIMEInfo.h" 1.13 +#include "nsIAtom.h" 1.14 +#include "nsString.h" 1.15 +#include "nsTArray.h" 1.16 +#include "nsIMutableArray.h" 1.17 +#include "nsIFile.h" 1.18 +#include "nsCOMPtr.h" 1.19 +#include "nsIURI.h" 1.20 +#include "nsIProcess.h" 1.21 + 1.22 +/** 1.23 + * UTF8 moz-icon URI string for the default handler application's icon, if 1.24 + * available. 1.25 + */ 1.26 +#define PROPERTY_DEFAULT_APP_ICON_URL "defaultApplicationIconURL" 1.27 +/** 1.28 + * UTF8 moz-icon URI string for the user's preferred handler application's 1.29 + * icon, if available. 1.30 + */ 1.31 +#define PROPERTY_CUSTOM_APP_ICON_URL "customApplicationIconURL" 1.32 + 1.33 +/** 1.34 + * Basic implementation of nsIMIMEInfo. Incomplete - it is meant to be 1.35 + * subclassed, and GetHasDefaultHandler as well as LaunchDefaultWithFile need to 1.36 + * be implemented. 1.37 + */ 1.38 +class nsMIMEInfoBase : public nsIMIMEInfo { 1.39 + public: 1.40 + NS_DECL_THREADSAFE_ISUPPORTS 1.41 + 1.42 + // I'd use NS_DECL_NSIMIMEINFO, but I don't want GetHasDefaultHandler 1.43 + NS_IMETHOD GetFileExtensions(nsIUTF8StringEnumerator **_retval); 1.44 + NS_IMETHOD SetFileExtensions(const nsACString & aExtensions); 1.45 + NS_IMETHOD ExtensionExists(const nsACString & aExtension, bool *_retval); 1.46 + NS_IMETHOD AppendExtension(const nsACString & aExtension); 1.47 + NS_IMETHOD GetPrimaryExtension(nsACString & aPrimaryExtension); 1.48 + NS_IMETHOD SetPrimaryExtension(const nsACString & aPrimaryExtension); 1.49 + NS_IMETHOD GetType(nsACString & aType); 1.50 + NS_IMETHOD GetMIMEType(nsACString & aMIMEType); 1.51 + NS_IMETHOD GetDescription(nsAString & aDescription); 1.52 + NS_IMETHOD SetDescription(const nsAString & aDescription); 1.53 + NS_IMETHOD Equals(nsIMIMEInfo *aMIMEInfo, bool *_retval); 1.54 + NS_IMETHOD GetPreferredApplicationHandler(nsIHandlerApp * *aPreferredAppHandler); 1.55 + NS_IMETHOD SetPreferredApplicationHandler(nsIHandlerApp * aPreferredAppHandler); 1.56 + NS_IMETHOD GetPossibleApplicationHandlers(nsIMutableArray * *aPossibleAppHandlers); 1.57 + NS_IMETHOD GetDefaultDescription(nsAString & aDefaultDescription); 1.58 + NS_IMETHOD LaunchWithFile(nsIFile *aFile); 1.59 + NS_IMETHOD LaunchWithURI(nsIURI *aURI, 1.60 + nsIInterfaceRequestor *aWindowContext); 1.61 + NS_IMETHOD GetPreferredAction(nsHandlerInfoAction *aPreferredAction); 1.62 + NS_IMETHOD SetPreferredAction(nsHandlerInfoAction aPreferredAction); 1.63 + NS_IMETHOD GetAlwaysAskBeforeHandling(bool *aAlwaysAskBeforeHandling); 1.64 + NS_IMETHOD SetAlwaysAskBeforeHandling(bool aAlwaysAskBeforeHandling); 1.65 + NS_IMETHOD GetPossibleLocalHandlers(nsIArray **_retval); 1.66 + 1.67 + enum HandlerClass { 1.68 + eMIMEInfo, 1.69 + eProtocolInfo 1.70 + }; 1.71 + 1.72 + // nsMIMEInfoBase methods 1.73 + nsMIMEInfoBase(const char *aMIMEType = "") NS_HIDDEN; 1.74 + nsMIMEInfoBase(const nsACString& aMIMEType) NS_HIDDEN; 1.75 + nsMIMEInfoBase(const nsACString& aType, HandlerClass aClass) NS_HIDDEN; 1.76 + virtual ~nsMIMEInfoBase(); // must be virtual, as the the base class's Release should call the subclass's destructor 1.77 + 1.78 + void SetMIMEType(const nsACString & aMIMEType) { mSchemeOrType = aMIMEType; } 1.79 + 1.80 + void SetDefaultDescription(const nsString& aDesc) { mDefaultAppDescription = aDesc; } 1.81 + 1.82 + /** 1.83 + * Copies basic data of this MIME Info Implementation to the given other 1.84 + * MIME Info. The data consists of the MIME Type, the (default) description, 1.85 + * the MacOS type and creator, and the extension list (this object's 1.86 + * extension list will replace aOther's list, not append to it). This 1.87 + * function also ensures that aOther's primary extension will be the same as 1.88 + * the one of this object. 1.89 + */ 1.90 + void CopyBasicDataTo(nsMIMEInfoBase* aOther); 1.91 + 1.92 + /** 1.93 + * Return whether this MIMEInfo has any extensions 1.94 + */ 1.95 + bool HasExtensions() const { return mExtensions.Length() != 0; } 1.96 + 1.97 + protected: 1.98 + /** 1.99 + * Launch the default application for the given file. 1.100 + * For even more control over the launching, override launchWithFile. 1.101 + * Also see the comment about nsIMIMEInfo in general, above. 1.102 + * 1.103 + * @param aFile The file that should be opened 1.104 + */ 1.105 + virtual NS_HIDDEN_(nsresult) LaunchDefaultWithFile(nsIFile* aFile) = 0; 1.106 + 1.107 + /** 1.108 + * Loads the URI with the OS default app. 1.109 + * 1.110 + * @param aURI The URI to pass off to the OS. 1.111 + */ 1.112 + virtual NS_HIDDEN_(nsresult) LoadUriInternal(nsIURI *aURI) = 0; 1.113 + 1.114 + static already_AddRefed<nsIProcess> InitProcess(nsIFile* aApp, 1.115 + nsresult* aResult); 1.116 + 1.117 + /** 1.118 + * This method can be used to launch the file or URI with a single 1.119 + * argument (typically either a file path or a URI spec). This is 1.120 + * meant as a helper method for implementations of 1.121 + * LaunchWithURI/LaunchDefaultWithFile. 1.122 + * 1.123 + * @param aApp The application to launch (may not be null) 1.124 + * @param aArg The argument to pass on the command line 1.125 + */ 1.126 + static NS_HIDDEN_(nsresult) LaunchWithIProcess(nsIFile* aApp, 1.127 + const nsCString &aArg); 1.128 + static NS_HIDDEN_(nsresult) LaunchWithIProcess(nsIFile* aApp, 1.129 + const nsString &aArg); 1.130 + 1.131 + /** 1.132 + * Given a file: nsIURI, return the associated nsIFile 1.133 + * 1.134 + * @param aURI the file: URI in question 1.135 + * @param aFile the associated nsIFile (out param) 1.136 + */ 1.137 + static NS_HIDDEN_(nsresult) GetLocalFileFromURI(nsIURI *aURI, 1.138 + nsIFile **aFile); 1.139 + 1.140 + // member variables 1.141 + nsTArray<nsCString> mExtensions; ///< array of file extensions associated w/ this MIME obj 1.142 + nsString mDescription; ///< human readable description 1.143 + nsCString mSchemeOrType; 1.144 + HandlerClass mClass; 1.145 + nsCOMPtr<nsIHandlerApp> mPreferredApplication; 1.146 + nsCOMPtr<nsIMutableArray> mPossibleApplications; 1.147 + nsHandlerInfoAction mPreferredAction; ///< preferred action to associate with this type 1.148 + nsString mPreferredAppDescription; 1.149 + nsString mDefaultAppDescription; 1.150 + bool mAlwaysAskBeforeHandling; 1.151 +}; 1.152 + 1.153 + 1.154 +/** 1.155 + * This is a complete implementation of nsIMIMEInfo, and contains all necessary 1.156 + * methods. However, depending on your platform you may want to use a different 1.157 + * way of launching applications. This class stores the default application in a 1.158 + * member variable and provides a function for setting it. For local 1.159 + * applications, launching is done using nsIProcess, native path of the file to 1.160 + * open as first argument. 1.161 + */ 1.162 +class nsMIMEInfoImpl : public nsMIMEInfoBase { 1.163 + public: 1.164 + nsMIMEInfoImpl(const char *aMIMEType = "") : nsMIMEInfoBase(aMIMEType) {} 1.165 + nsMIMEInfoImpl(const nsACString& aMIMEType) : nsMIMEInfoBase(aMIMEType) {} 1.166 + nsMIMEInfoImpl(const nsACString& aType, HandlerClass aClass) : 1.167 + nsMIMEInfoBase(aType, aClass) {} 1.168 + virtual ~nsMIMEInfoImpl() {} 1.169 + 1.170 + // nsIMIMEInfo methods 1.171 + NS_IMETHOD GetHasDefaultHandler(bool *_retval); 1.172 + NS_IMETHOD GetDefaultDescription(nsAString& aDefaultDescription); 1.173 + 1.174 + // additional methods 1.175 + /** 1.176 + * Sets the default application. Supposed to be only called by the OS Helper 1.177 + * App Services; the default application is immutable after it is first set. 1.178 + */ 1.179 + void SetDefaultApplication(nsIFile* aApp) { if (!mDefaultApplication) mDefaultApplication = aApp; } 1.180 + 1.181 + protected: 1.182 + // nsMIMEInfoBase methods 1.183 + /** 1.184 + * The base class implementation is to use LaunchWithIProcess in combination 1.185 + * with mDefaultApplication. Subclasses can override that behaviour. 1.186 + */ 1.187 + virtual NS_HIDDEN_(nsresult) LaunchDefaultWithFile(nsIFile* aFile); 1.188 + 1.189 + /** 1.190 + * Loads the URI with the OS default app. This should be overridden by each 1.191 + * OS's implementation. 1.192 + */ 1.193 + virtual NS_HIDDEN_(nsresult) LoadUriInternal(nsIURI *aURI) = 0; 1.194 + 1.195 + nsCOMPtr<nsIFile> mDefaultApplication; ///< default application associated with this type. 1.196 +}; 1.197 + 1.198 +#endif //__nsmimeinfoimpl_h___