toolkit/components/downloads/nsDownloadManager.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 4; 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 downloadmanager___h___
michael@0 7 #define downloadmanager___h___
michael@0 8
michael@0 9 #if defined(XP_WIN)
michael@0 10 #define DOWNLOAD_SCANNER
michael@0 11 #endif
michael@0 12
michael@0 13 #include "nsIDownload.h"
michael@0 14 #include "nsIDownloadManager.h"
michael@0 15 #include "nsIDownloadProgressListener.h"
michael@0 16 #include "nsIFile.h"
michael@0 17 #include "nsIMIMEInfo.h"
michael@0 18 #include "nsINavHistoryService.h"
michael@0 19 #include "nsIObserver.h"
michael@0 20 #include "nsIObserverService.h"
michael@0 21 #include "nsIStringBundle.h"
michael@0 22 #include "nsISupportsPrimitives.h"
michael@0 23 #include "nsWeakReference.h"
michael@0 24 #include "nsITimer.h"
michael@0 25 #include "nsString.h"
michael@0 26
michael@0 27 #include "mozStorageHelper.h"
michael@0 28 #include "nsAutoPtr.h"
michael@0 29 #include "nsCOMArray.h"
michael@0 30
michael@0 31 typedef int16_t DownloadState;
michael@0 32 typedef int16_t DownloadType;
michael@0 33
michael@0 34 class nsIArray;
michael@0 35 class nsDownload;
michael@0 36
michael@0 37 #ifdef DOWNLOAD_SCANNER
michael@0 38 #include "nsDownloadScanner.h"
michael@0 39 #endif
michael@0 40
michael@0 41 class nsDownloadManager : public nsIDownloadManager,
michael@0 42 public nsINavHistoryObserver,
michael@0 43 public nsIObserver,
michael@0 44 public nsSupportsWeakReference
michael@0 45 {
michael@0 46 public:
michael@0 47 NS_DECL_ISUPPORTS
michael@0 48 NS_DECL_NSIDOWNLOADMANAGER
michael@0 49 NS_DECL_NSINAVHISTORYOBSERVER
michael@0 50 NS_DECL_NSIOBSERVER
michael@0 51
michael@0 52 nsresult Init();
michael@0 53
michael@0 54 static nsDownloadManager *GetSingleton();
michael@0 55
michael@0 56 virtual ~nsDownloadManager();
michael@0 57 nsDownloadManager()
michael@0 58 #ifdef DOWNLOAD_SCANNER
michael@0 59 : mScanner(nullptr)
michael@0 60 #endif
michael@0 61 {
michael@0 62 }
michael@0 63
michael@0 64 protected:
michael@0 65 nsresult InitDB();
michael@0 66 nsresult InitFileDB();
michael@0 67 void CloseAllDBs();
michael@0 68 void CloseDB(mozIStorageConnection* aDBConn,
michael@0 69 mozIStorageStatement* aUpdateStmt,
michael@0 70 mozIStorageStatement* aGetIdsStmt);
michael@0 71 nsresult InitPrivateDB();
michael@0 72 already_AddRefed<mozIStorageConnection> GetFileDBConnection(nsIFile *dbFile) const;
michael@0 73 already_AddRefed<mozIStorageConnection> GetPrivateDBConnection() const;
michael@0 74 nsresult CreateTable(mozIStorageConnection* aDBConn);
michael@0 75
michael@0 76 /**
michael@0 77 * Fix up the database after a crash such as dealing with previously-active
michael@0 78 * downloads. Call this before RestoreActiveDownloads to get the downloads
michael@0 79 * fixed here to be auto-resumed.
michael@0 80 */
michael@0 81 nsresult RestoreDatabaseState();
michael@0 82
michael@0 83 /**
michael@0 84 * Paused downloads that survive across sessions are considered active, so
michael@0 85 * rebuild the list of these downloads.
michael@0 86 */
michael@0 87 nsresult RestoreActiveDownloads();
michael@0 88
michael@0 89 nsresult GetDownloadFromDB(const nsACString& aGUID, nsDownload **retVal);
michael@0 90 nsresult GetDownloadFromDB(uint32_t aID, nsDownload **retVal);
michael@0 91 nsresult GetDownloadFromDB(mozIStorageConnection* aDBConn,
michael@0 92 mozIStorageStatement* stmt,
michael@0 93 nsDownload **retVal);
michael@0 94
michael@0 95 /**
michael@0 96 * Specially track the active downloads so that we don't need to check
michael@0 97 * every download to see if they're in progress.
michael@0 98 */
michael@0 99 nsresult AddToCurrentDownloads(nsDownload *aDl);
michael@0 100
michael@0 101 void SendEvent(nsDownload *aDownload, const char *aTopic);
michael@0 102
michael@0 103 /**
michael@0 104 * Adds a download with the specified information to the DB.
michael@0 105 *
michael@0 106 * @return The id of the download, or 0 if there was an error.
michael@0 107 */
michael@0 108 int64_t AddDownloadToDB(const nsAString &aName,
michael@0 109 const nsACString &aSource,
michael@0 110 const nsACString &aTarget,
michael@0 111 const nsAString &aTempPath,
michael@0 112 int64_t aStartTime,
michael@0 113 int64_t aEndTime,
michael@0 114 const nsACString &aMimeType,
michael@0 115 const nsACString &aPreferredApp,
michael@0 116 nsHandlerInfoAction aPreferredAction,
michael@0 117 bool aPrivate,
michael@0 118 nsACString &aNewGUID);
michael@0 119
michael@0 120 void NotifyListenersOnDownloadStateChange(int16_t aOldState,
michael@0 121 nsDownload *aDownload);
michael@0 122 void NotifyListenersOnProgressChange(nsIWebProgress *aProgress,
michael@0 123 nsIRequest *aRequest,
michael@0 124 int64_t aCurSelfProgress,
michael@0 125 int64_t aMaxSelfProgress,
michael@0 126 int64_t aCurTotalProgress,
michael@0 127 int64_t aMaxTotalProgress,
michael@0 128 nsDownload *aDownload);
michael@0 129 void NotifyListenersOnStateChange(nsIWebProgress *aProgress,
michael@0 130 nsIRequest *aRequest,
michael@0 131 uint32_t aStateFlags,
michael@0 132 nsresult aStatus,
michael@0 133 nsDownload *aDownload);
michael@0 134
michael@0 135 nsDownload *FindDownload(const nsACString& aGUID);
michael@0 136 nsDownload *FindDownload(uint32_t aID);
michael@0 137
michael@0 138 /**
michael@0 139 * First try to resume the download, and if that fails, retry it.
michael@0 140 *
michael@0 141 * @param aDl The download to resume and/or retry.
michael@0 142 */
michael@0 143 nsresult ResumeRetry(nsDownload *aDl);
michael@0 144
michael@0 145 /**
michael@0 146 * Pause all active downloads and remember if they should try to auto-resume
michael@0 147 * when the download manager starts again.
michael@0 148 *
michael@0 149 * @param aSetResume Indicate if the downloads that get paused should be set
michael@0 150 * as auto-resume.
michael@0 151 */
michael@0 152 nsresult PauseAllDownloads(bool aSetResume);
michael@0 153
michael@0 154 /**
michael@0 155 * Resume all paused downloads unless we're only supposed to do the automatic
michael@0 156 * ones; in that case, try to retry them as well if resuming doesn't work.
michael@0 157 *
michael@0 158 * @param aResumeAll If true, all downloads will be resumed; otherwise, only
michael@0 159 * those that are marked as auto-resume will resume.
michael@0 160 */
michael@0 161 nsresult ResumeAllDownloads(bool aResumeAll);
michael@0 162
michael@0 163 /**
michael@0 164 * Stop tracking the active downloads. Only use this when we're about to quit
michael@0 165 * the download manager because we destroy our list of active downloads to
michael@0 166 * break the dlmgr<->dl cycle. Active downloads that aren't real-paused will
michael@0 167 * be canceled.
michael@0 168 */
michael@0 169 nsresult RemoveAllDownloads();
michael@0 170
michael@0 171 /**
michael@0 172 * Find all downloads from a source URI and delete them.
michael@0 173 *
michael@0 174 * @param aURI
michael@0 175 * The source URI to remove downloads
michael@0 176 */
michael@0 177 nsresult RemoveDownloadsForURI(nsIURI *aURI);
michael@0 178
michael@0 179 /**
michael@0 180 * Callback used for resuming downloads after getting a wake notification.
michael@0 181 *
michael@0 182 * @param aTimer
michael@0 183 * Timer object fired after some delay after a wake notification
michael@0 184 * @param aClosure
michael@0 185 * nsDownloadManager object used to resume downloads
michael@0 186 */
michael@0 187 static void ResumeOnWakeCallback(nsITimer *aTimer, void *aClosure);
michael@0 188 nsCOMPtr<nsITimer> mResumeOnWakeTimer;
michael@0 189
michael@0 190 void ConfirmCancelDownloads(int32_t aCount,
michael@0 191 nsISupportsPRBool *aCancelDownloads,
michael@0 192 const char16_t *aTitle,
michael@0 193 const char16_t *aCancelMessageMultiple,
michael@0 194 const char16_t *aCancelMessageSingle,
michael@0 195 const char16_t *aDontCancelButton);
michael@0 196
michael@0 197 int32_t GetRetentionBehavior();
michael@0 198
michael@0 199 /**
michael@0 200 * Type to indicate possible behaviors for active downloads across sessions.
michael@0 201 *
michael@0 202 * Possible values are:
michael@0 203 * QUIT_AND_RESUME - downloads should be auto-resumed
michael@0 204 * QUIT_AND_PAUSE - downloads should be paused
michael@0 205 * QUIT_AND_CANCEL - downloads should be cancelled
michael@0 206 */
michael@0 207 enum QuitBehavior {
michael@0 208 QUIT_AND_RESUME = 0,
michael@0 209 QUIT_AND_PAUSE = 1,
michael@0 210 QUIT_AND_CANCEL = 2
michael@0 211 };
michael@0 212
michael@0 213 /**
michael@0 214 * Indicates user-set behavior for active downloads across sessions,
michael@0 215 *
michael@0 216 * @return value of user-set pref for active download behavior
michael@0 217 */
michael@0 218 enum QuitBehavior GetQuitBehavior();
michael@0 219
michael@0 220 void OnEnterPrivateBrowsingMode();
michael@0 221 void OnLeavePrivateBrowsingMode();
michael@0 222
michael@0 223 nsresult RetryDownload(const nsACString& aGUID);
michael@0 224 nsresult RetryDownload(nsDownload* dl);
michael@0 225
michael@0 226 nsresult RemoveDownload(const nsACString& aGUID);
michael@0 227
michael@0 228 nsresult NotifyDownloadRemoval(nsDownload* aRemoved);
michael@0 229
michael@0 230 // Virus scanner for windows
michael@0 231 #ifdef DOWNLOAD_SCANNER
michael@0 232 private:
michael@0 233 nsDownloadScanner* mScanner;
michael@0 234 #endif
michael@0 235
michael@0 236 private:
michael@0 237 nsresult CleanUp(mozIStorageConnection* aDBConn);
michael@0 238 nsresult InitStatements(mozIStorageConnection* aDBConn,
michael@0 239 mozIStorageStatement** aUpdateStatement,
michael@0 240 mozIStorageStatement** aGetIdsStatement);
michael@0 241 nsresult RemoveAllDownloads(nsCOMArray<nsDownload>& aDownloads);
michael@0 242 nsresult PauseAllDownloads(nsCOMArray<nsDownload>& aDownloads, bool aSetResume);
michael@0 243 nsresult ResumeAllDownloads(nsCOMArray<nsDownload>& aDownloads, bool aResumeAll);
michael@0 244 nsresult RemoveDownloadsForURI(mozIStorageStatement* aStatement, nsIURI *aURI);
michael@0 245
michael@0 246 bool mUseJSTransfer;
michael@0 247 nsCOMArray<nsIDownloadProgressListener> mListeners;
michael@0 248 nsCOMArray<nsIDownloadProgressListener> mPrivacyAwareListeners;
michael@0 249 nsCOMPtr<nsIStringBundle> mBundle;
michael@0 250 nsCOMPtr<mozIStorageConnection> mDBConn;
michael@0 251 nsCOMPtr<mozIStorageConnection> mPrivateDBConn;
michael@0 252 nsCOMArray<nsDownload> mCurrentDownloads;
michael@0 253 nsCOMArray<nsDownload> mCurrentPrivateDownloads;
michael@0 254 nsCOMPtr<nsIObserverService> mObserverService;
michael@0 255 nsCOMPtr<mozIStorageStatement> mUpdateDownloadStatement;
michael@0 256 nsCOMPtr<mozIStorageStatement> mUpdatePrivateDownloadStatement;
michael@0 257 nsCOMPtr<mozIStorageStatement> mGetIdsForURIStatement;
michael@0 258 nsCOMPtr<mozIStorageStatement> mGetPrivateIdsForURIStatement;
michael@0 259 nsAutoPtr<mozStorageTransaction> mHistoryTransaction;
michael@0 260
michael@0 261 static nsDownloadManager *gDownloadManagerService;
michael@0 262
michael@0 263 friend class nsDownload;
michael@0 264 };
michael@0 265
michael@0 266 class nsDownload : public nsIDownload
michael@0 267 {
michael@0 268 public:
michael@0 269 NS_DECL_NSIWEBPROGRESSLISTENER
michael@0 270 NS_DECL_NSIWEBPROGRESSLISTENER2
michael@0 271 NS_DECL_NSITRANSFER
michael@0 272 NS_DECL_NSIDOWNLOAD
michael@0 273 NS_DECL_ISUPPORTS
michael@0 274
michael@0 275 nsDownload();
michael@0 276 virtual ~nsDownload();
michael@0 277
michael@0 278 /**
michael@0 279 * This method MUST be called when changing states on a download. It will
michael@0 280 * notify the download listener when a change happens. This also updates the
michael@0 281 * database, by calling UpdateDB().
michael@0 282 */
michael@0 283 nsresult SetState(DownloadState aState);
michael@0 284
michael@0 285 protected:
michael@0 286 /**
michael@0 287 * Finish up the download by breaking reference cycles and clearing unneeded
michael@0 288 * data. Additionally, the download removes itself from the download
michael@0 289 * manager's list of current downloads.
michael@0 290 *
michael@0 291 * NOTE: This method removes the cycle created when starting the download, so
michael@0 292 * make sure to use kungFuDeathGrip if you want to access member variables.
michael@0 293 */
michael@0 294 void Finalize();
michael@0 295
michael@0 296 /**
michael@0 297 * For finished resumed downloads that came in from exthandler, perform the
michael@0 298 * action that would have been done if the download wasn't resumed.
michael@0 299 */
michael@0 300 nsresult ExecuteDesiredAction();
michael@0 301
michael@0 302 /**
michael@0 303 * Move the temporary file to the final destination by removing the existing
michael@0 304 * dummy target and renaming the temporary.
michael@0 305 */
michael@0 306 nsresult MoveTempToTarget();
michael@0 307
michael@0 308 /**
michael@0 309 * Update the start time which also implies the last update time is the same.
michael@0 310 */
michael@0 311 void SetStartTime(int64_t aStartTime);
michael@0 312
michael@0 313 /**
michael@0 314 * Update the amount of bytes transferred and max bytes; and recalculate the
michael@0 315 * download percent.
michael@0 316 */
michael@0 317 void SetProgressBytes(int64_t aCurrBytes, int64_t aMaxBytes);
michael@0 318
michael@0 319 /**
michael@0 320 * All this does is cancel the connection that the download is using. It does
michael@0 321 * not remove it from the download manager.
michael@0 322 */
michael@0 323 nsresult CancelTransfer();
michael@0 324
michael@0 325 /**
michael@0 326 * Download is not transferring?
michael@0 327 */
michael@0 328 bool IsPaused();
michael@0 329
michael@0 330 /**
michael@0 331 * Download can continue from the middle of a transfer?
michael@0 332 */
michael@0 333 bool IsResumable();
michael@0 334
michael@0 335 /**
michael@0 336 * Download was resumed?
michael@0 337 */
michael@0 338 bool WasResumed();
michael@0 339
michael@0 340 /**
michael@0 341 * Indicates if the download should try to automatically resume or not.
michael@0 342 */
michael@0 343 bool ShouldAutoResume();
michael@0 344
michael@0 345 /**
michael@0 346 * Download is in a state to stop and complete the download?
michael@0 347 */
michael@0 348 bool IsFinishable();
michael@0 349
michael@0 350 /**
michael@0 351 * Download is totally done transferring and all?
michael@0 352 */
michael@0 353 bool IsFinished();
michael@0 354
michael@0 355 /**
michael@0 356 * Update the DB with the current state of the download including time,
michael@0 357 * download state and other values not known when first creating the
michael@0 358 * download DB entry.
michael@0 359 */
michael@0 360 nsresult UpdateDB();
michael@0 361
michael@0 362 /**
michael@0 363 * Fail a download because of a failure status and prompt the provided
michael@0 364 * message or use a generic download failure message if nullptr.
michael@0 365 */
michael@0 366 nsresult FailDownload(nsresult aStatus, const char16_t *aMessage);
michael@0 367
michael@0 368 /**
michael@0 369 * Opens the downloaded file with the appropriate application, which is
michael@0 370 * either the OS default, MIME type default, or the one selected by the user.
michael@0 371 *
michael@0 372 * This also adds the temporary file to the "To be deleted on Exit" list, if
michael@0 373 * the corresponding user preference is set (except on OS X).
michael@0 374 *
michael@0 375 * This function was adopted from nsExternalAppHandler::OpenWithApplication
michael@0 376 * (uriloader/exthandler/nsExternalHelperAppService.cpp).
michael@0 377 */
michael@0 378 nsresult OpenWithApplication();
michael@0 379
michael@0 380 nsDownloadManager *mDownloadManager;
michael@0 381 nsCOMPtr<nsIURI> mTarget;
michael@0 382
michael@0 383 private:
michael@0 384 nsString mDisplayName;
michael@0 385 nsCString mEntityID;
michael@0 386 nsCString mGUID;
michael@0 387
michael@0 388 nsCOMPtr<nsIURI> mSource;
michael@0 389 nsCOMPtr<nsIURI> mReferrer;
michael@0 390 nsCOMPtr<nsICancelable> mCancelable;
michael@0 391 nsCOMPtr<nsIRequest> mRequest;
michael@0 392 nsCOMPtr<nsIFile> mTempFile;
michael@0 393 nsCOMPtr<nsIMIMEInfo> mMIMEInfo;
michael@0 394
michael@0 395 DownloadState mDownloadState;
michael@0 396
michael@0 397 uint32_t mID;
michael@0 398 int32_t mPercentComplete;
michael@0 399
michael@0 400 /**
michael@0 401 * These bytes are based on the position of where the request started, so 0
michael@0 402 * doesn't necessarily mean we have nothing. Use GetAmountTransferred and
michael@0 403 * GetSize for the real transferred amount and size.
michael@0 404 */
michael@0 405 int64_t mCurrBytes;
michael@0 406 int64_t mMaxBytes;
michael@0 407
michael@0 408 PRTime mStartTime;
michael@0 409 PRTime mLastUpdate;
michael@0 410 int64_t mResumedAt;
michael@0 411 double mSpeed;
michael@0 412
michael@0 413 bool mHasMultipleFiles;
michael@0 414 bool mPrivate;
michael@0 415
michael@0 416 /**
michael@0 417 * Track various states of the download trying to auto-resume when starting
michael@0 418 * the download manager or restoring from a crash.
michael@0 419 *
michael@0 420 * DONT_RESUME: Don't automatically resume the download
michael@0 421 * AUTO_RESUME: Automaically resume the download
michael@0 422 */
michael@0 423 enum AutoResume { DONT_RESUME, AUTO_RESUME };
michael@0 424 AutoResume mAutoResume;
michael@0 425
michael@0 426 /**
michael@0 427 * Stores the SHA-256 hash associated with the downloaded file.
michael@0 428 */
michael@0 429 nsAutoCString mHash;
michael@0 430
michael@0 431 /**
michael@0 432 * Stores the certificate chains in an nsIArray of nsIX509CertList of
michael@0 433 * nsIX509Cert, if this binary is signed.
michael@0 434 */
michael@0 435 nsCOMPtr<nsIArray> mSignatureInfo;
michael@0 436
michael@0 437 friend class nsDownloadManager;
michael@0 438 };
michael@0 439
michael@0 440 #endif

mercurial