uriloader/exthandler/nsExternalHelperAppService.h

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

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

mercurial