Bug 777574 - Skip quickCheckAPI-B2.html on Linux. r=bjacob, a=test-only
[gecko.git] / uriloader / exthandler / nsExternalHelperAppService.h
blob9a22ed85fe689c48729a10c5bfed8dbd4d7a3e28
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsExternalHelperAppService_h__
7 #define nsExternalHelperAppService_h__
9 #ifdef MOZ_LOGGING
10 #define FORCE_PR_LOG
11 #endif
12 #include "prlog.h"
13 #include "prtime.h"
15 #include "nsIExternalHelperAppService.h"
16 #include "nsIExternalProtocolService.h"
17 #include "nsIWebProgressListener2.h"
18 #include "nsIHelperAppLauncherDialog.h"
20 #include "nsIMIMEInfo.h"
21 #include "nsIMIMEService.h"
22 #include "nsIStreamListener.h"
23 #include "nsIFile.h"
24 #include "nsIFileStreams.h"
25 #include "nsIOutputStream.h"
26 #include "nsString.h"
27 #include "nsIInterfaceRequestor.h"
28 #include "nsIInterfaceRequestorUtils.h"
29 #include "nsIChannel.h"
30 #include "nsITimer.h"
31 #include "nsIBackgroundFileSaver.h"
33 #include "nsIHandlerService.h"
34 #include "nsCOMPtr.h"
35 #include "nsIObserver.h"
36 #include "nsCOMArray.h"
37 #include "nsWeakReference.h"
38 #include "nsIPrompt.h"
39 #include "nsAutoPtr.h"
40 #include "mozilla/Attributes.h"
41 #include "necko-config.h"
43 class nsExternalAppHandler;
44 class nsIMIMEInfo;
45 class nsITransfer;
46 class nsIDOMWindow;
48 /**
49 * The helper app service. Responsible for handling content that Mozilla
50 * itself can not handle
52 class nsExternalHelperAppService
53 : public nsIExternalHelperAppService,
54 public nsPIExternalAppLauncher,
55 public nsIExternalProtocolService,
56 public nsIMIMEService,
57 public nsIObserver,
58 public nsSupportsWeakReference
60 public:
61 NS_DECL_ISUPPORTS
62 NS_DECL_NSIEXTERNALHELPERAPPSERVICE
63 NS_DECL_NSPIEXTERNALAPPLAUNCHER
64 NS_DECL_NSIEXTERNALPROTOCOLSERVICE
65 NS_DECL_NSIMIMESERVICE
66 NS_DECL_NSIOBSERVER
68 nsExternalHelperAppService();
69 virtual ~nsExternalHelperAppService();
71 /**
72 * Initializes internal state. Will be called automatically when
73 * this service is first instantiated.
75 NS_HIDDEN_(nsresult) Init();
77 /**
78 * Given a mimetype and an extension, looks up a mime info from the OS.
79 * The mime type is given preference. This function follows the same rules
80 * as nsIMIMEService::GetFromTypeAndExtension.
81 * This is supposed to be overridden by the platform-specific
82 * nsOSHelperAppService!
83 * @param aFileExt The file extension; may be empty. UTF-8 encoded.
84 * @param [out] aFound
85 * Should be set to true if the os has a mapping, to
86 * false otherwise. Must not be null.
87 * @return A MIMEInfo. This function must return a MIMEInfo object if it
88 * can allocate one. The only justifiable reason for not
89 * returning one is an out-of-memory error.
90 * If null, the value of aFound is unspecified.
92 virtual already_AddRefed<nsIMIMEInfo> GetMIMEInfoFromOS(const nsACString& aMIMEType,
93 const nsACString& aFileExt,
94 bool * aFound) = 0;
96 /**
97 * Given a string identifying an application, create an nsIFile representing
98 * it. This function should look in $PATH for the application.
99 * The base class implementation will first try to interpret platformAppPath
100 * as an absolute path, and if that fails it will look for a file next to the
101 * mozilla executable. Subclasses can override this method if they want a
102 * different behaviour.
103 * @param platformAppPath A platform specific path to an application that we
104 * got out of the rdf data source. This can be a mac
105 * file spec, a unix path or a windows path depending
106 * on the platform
107 * @param aFile [out] An nsIFile representation of that platform
108 * application path.
110 virtual nsresult GetFileTokenForPath(const PRUnichar * platformAppPath,
111 nsIFile ** aFile);
113 virtual NS_HIDDEN_(nsresult) OSProtocolHandlerExists(const char *aScheme,
114 bool *aExists) = 0;
116 protected:
118 * Searches the "extra" array of MIMEInfo objects for an object
119 * with a specific type. If found, it will modify the passed-in
120 * MIMEInfo. Otherwise, it will return an error and the MIMEInfo
121 * will be untouched.
122 * @param aContentType The type to search for.
123 * @param aMIMEInfo [inout] The mime info, if found
125 NS_HIDDEN_(nsresult) FillMIMEInfoForMimeTypeFromExtras(
126 const nsACString& aContentType, nsIMIMEInfo * aMIMEInfo);
128 * Searches the "extra" array of MIMEInfo objects for an object
129 * with a specific extension.
131 * Does not change the MIME Type of the MIME Info.
133 * @see FillMIMEInfoForMimeTypeFromExtras
135 NS_HIDDEN_(nsresult) FillMIMEInfoForExtensionFromExtras(
136 const nsACString& aExtension, nsIMIMEInfo * aMIMEInfo);
139 * Searches the "extra" array for a MIME type, and gets its extension.
140 * @param aExtension The extension to search for
141 * @param aMIMEType [out] The found MIME type.
142 * @return true if the extension was found, false otherwise.
144 NS_HIDDEN_(bool) GetTypeFromExtras(const nsACString& aExtension,
145 nsACString& aMIMEType);
147 #ifdef PR_LOGGING
149 * NSPR Logging Module. Usage: set NSPR_LOG_MODULES=HelperAppService:level,
150 * where level should be 2 for errors, 3 for debug messages from the cross-
151 * platform nsExternalHelperAppService, and 4 for os-specific debug messages.
153 static PRLogModuleInfo* mLog;
155 #endif
156 // friend, so that it can access the nspr log module.
157 friend class nsExternalAppHandler;
160 * Helper function for ExpungeTemporaryFiles and ExpungeTemporaryPrivateFiles
162 static void ExpungeTemporaryFilesHelper(nsCOMArray<nsIFile> &fileList);
164 * Helper function for DeleteTemporaryFileOnExit and DeleteTemporaryPrivateFileWhenPossible
166 static nsresult DeleteTemporaryFileHelper(nsIFile* aTemporaryFile,
167 nsCOMArray<nsIFile> &aFileList);
169 * Functions related to the tempory file cleanup service provided by
170 * nsExternalHelperAppService
172 void ExpungeTemporaryFiles();
174 * Functions related to the tempory file cleanup service provided by
175 * nsExternalHelperAppService (for the temporary files added during
176 * the private browsing mode)
178 void ExpungeTemporaryPrivateFiles();
180 #ifdef NECKO_PROTOCOL_rtsp
182 * Launch video app for rtsp protocol. This function is supported only on Gonk
183 * for now.
185 static void LaunchVideoAppForRtsp(nsIURI* aURI);
186 #endif
189 * Array for the files that should be deleted
191 nsCOMArray<nsIFile> mTemporaryFilesList;
193 * Array for the files that should be deleted (for the temporary files
194 * added during the private browsing mode)
196 nsCOMArray<nsIFile> mTemporaryPrivateFilesList;
200 * An external app handler is just a small little class that presents itself as
201 * a nsIStreamListener. It saves the incoming data into a temp file. The handler
202 * is bound to an application when it is created. When it receives an
203 * OnStopRequest it launches the application using the temp file it has
204 * stored the data into. We create a handler every time we have to process
205 * data using a helper app.
207 class nsExternalAppHandler MOZ_FINAL : public nsIStreamListener,
208 public nsIHelperAppLauncher,
209 public nsITimerCallback,
210 public nsIBackgroundFileSaverObserver
212 public:
213 NS_DECL_THREADSAFE_ISUPPORTS
214 NS_DECL_NSISTREAMLISTENER
215 NS_DECL_NSIREQUESTOBSERVER
216 NS_DECL_NSIHELPERAPPLAUNCHER
217 NS_DECL_NSICANCELABLE
218 NS_DECL_NSITIMERCALLBACK
219 NS_DECL_NSIBACKGROUNDFILESAVEROBSERVER
222 * @param aMIMEInfo MIMEInfo object, representing the type of the
223 * content that should be handled
224 * @param aFileExtension The extension we need to append to our temp file,
225 * INCLUDING the ".". e.g. .mp3
226 * @param aWindowContext Window context, as passed to DoContent
227 * @param mExtProtSvc nsExternalHelperAppService on creation
228 * @param aFileName The filename to use
229 * @param aReason A constant from nsIHelperAppLauncherDialog indicating
230 * why the request is handled by a helper app.
232 nsExternalAppHandler(nsIMIMEInfo * aMIMEInfo, const nsCSubstring& aFileExtension,
233 nsIInterfaceRequestor * aWindowContext,
234 nsExternalHelperAppService * aExtProtSvc,
235 const nsAString& aFilename,
236 uint32_t aReason, bool aForceSave);
238 ~nsExternalAppHandler();
240 protected:
241 nsCOMPtr<nsIFile> mTempFile;
242 nsCOMPtr<nsIURI> mSourceUrl;
243 nsString mTempFileExtension;
244 nsString mTempLeafName;
247 * The MIME Info for this load. Will never be null.
249 nsCOMPtr<nsIMIMEInfo> mMimeInfo;
250 nsCOMPtr<nsIInterfaceRequestor> mWindowContext;
253 * Used to close the window on a timer, to avoid any exceptions that are
254 * thrown if we try to close the window before it's fully loaded.
256 nsCOMPtr<nsIDOMWindow> mWindowToClose;
257 nsCOMPtr<nsITimer> mTimer;
260 * The following field is set if we were processing an http channel that had
261 * a content disposition header which specified the SUGGESTED file name we
262 * should present to the user in the save to disk dialog.
264 nsString mSuggestedFileName;
267 * If set, this handler should forcibly save the file to disk regardless of
268 * MIME info settings or anything else, without ever popping up the
269 * unknown content type handling dialog.
271 bool mForceSave;
274 * The canceled flag is set if the user canceled the launching of this
275 * application before we finished saving the data to a temp file.
277 bool mCanceled;
280 * This is set based on whether the channel indicates that a new window
281 * was opened specifically for this download. If so, then we
282 * close it.
284 bool mShouldCloseWindow;
287 * True if a stop request has been issued.
289 bool mStopRequestIssued;
291 bool mIsFileChannel;
294 * One of the REASON_ constants from nsIHelperAppLauncherDialog. Indicates the
295 * reason the dialog was shown (unknown content type, server requested it,
296 * etc).
298 uint32_t mReason;
301 * Track the executable-ness of the temporary file.
303 bool mTempFileIsExecutable;
305 PRTime mTimeDownloadStarted;
306 int64_t mContentLength;
307 int64_t mProgress; /**< Number of bytes received (for sending progress notifications). */
310 * When we are told to save the temp file to disk (in a more permament
311 * location) before we are done writing the content to a temp file, then
312 * we need to remember the final destination until we are ready to use it.
314 nsCOMPtr<nsIFile> mFinalFileDestination;
316 uint32_t mBufferSize;
319 * This object handles saving the data received from the network to a
320 * temporary location first, and then move the file to its final location,
321 * doing all the input/output on a background thread.
323 nsCOMPtr<nsIBackgroundFileSaver> mSaver;
326 * Stores the SHA-256 hash associated with the file that we downloaded.
328 nsAutoCString mHash;
331 * Creates the temporary file for the download and an output stream for it.
332 * Upon successful return, both mTempFile and mSaver will be valid.
334 nsresult SetUpTempFile(nsIChannel * aChannel);
336 * When we download a helper app, we are going to retarget all load
337 * notifications into our own docloader and load group instead of
338 * using the window which initiated the load....RetargetLoadNotifications
339 * contains that information...
341 void RetargetLoadNotifications(nsIRequest *request);
343 * Once the user tells us how they want to dispose of the content
344 * create an nsITransfer so they know what's going on. If this fails, the
345 * caller MUST call Cancel.
347 nsresult CreateTransfer();
350 * The following two functions are part of the split of SaveToDisk
351 * to make it async, and works as following:
353 * SaveToDisk -------> RequestSaveDestination
357 * ContinueSave <------- SaveDestinationAvailable
361 * This is called by SaveToDisk to decide what's the final
362 * file destination chosen by the user or by auto-download settings.
364 void RequestSaveDestination(const nsAFlatString &aDefaultFile,
365 const nsAFlatString &aDefaultFileExt);
368 * When SaveToDisk is called, it possibly delegates to RequestSaveDestination
369 * to decide the file destination. ContinueSave must then be called when
370 * the final destination is finally known.
371 * @param aFile The file that was chosen as the final destination.
372 * Must not be null.
374 nsresult ContinueSave(nsIFile* aFile);
377 * After we're done prompting the user for any information, if the original
378 * channel had a refresh url associated with it (which might point to a
379 * "thank you for downloading" kind of page, then process that....It is safe
380 * to invoke this method multiple times. We'll clear mOriginalChannel after
381 * it's called and this ensures we won't call it again....
383 void ProcessAnyRefreshTags();
386 * Notify our nsITransfer object that we are done with the download. This is
387 * always called after the target file has been closed.
389 * @param aStatus
390 * NS_OK for success, or a failure code if the download failed.
391 * A partially downloaded file may still be available in this case.
393 void NotifyTransfer(nsresult aStatus);
396 * Helper routine that searches a pref string for a given mime type
398 bool GetNeverAskFlagFromPref(const char * prefName, const char * aContentType);
401 * Helper routine to ensure mSuggestedFileName is "correct";
402 * this ensures that mTempFileExtension only contains an extension when it
403 * is different from mSuggestedFileName's extension.
405 void EnsureSuggestedFileName();
407 typedef enum { kReadError, kWriteError, kLaunchError } ErrorType;
409 * Utility function to send proper error notification to web progress listener
411 void SendStatusChange(ErrorType type, nsresult aStatus, nsIRequest *aRequest, const nsAFlatString &path);
414 * Closes the window context if it does not have a refresh header
415 * and it never displayed content before the external helper app
416 * service was invoked.
418 nsresult MaybeCloseWindow();
421 * Set in nsHelperDlgApp.js. This is always null after the user has chosen an
422 * action.
424 nsCOMPtr<nsIWebProgressListener2> mDialogProgressListener;
426 * Set once the user has chosen an action. This is null after the download
427 * has been canceled or completes.
429 nsCOMPtr<nsITransfer> mTransfer;
431 nsCOMPtr<nsIChannel> mOriginalChannel; /**< in the case of a redirect, this will be the pre-redirect channel. */
432 nsCOMPtr<nsIHelperAppLauncherDialog> mDialog;
435 * Keep request alive in case when helper non-modal dialog shown.
436 * Thus in OnStopRequest the mRequest will not be set to null (it will be set to null further).
438 bool mKeepRequestAlive;
441 * The request that's being loaded. Initialized in OnStartRequest.
442 * Nulled out in OnStopRequest or once we know what we're doing
443 * with the data, whichever happens later.
445 nsCOMPtr<nsIRequest> mRequest;
447 nsRefPtr<nsExternalHelperAppService> mExtProtSvc;
450 #endif // nsExternalHelperAppService_h__