LCOV - app.info - toolkit/components/downloads/nsDownloadManager.h

LCOV - code coverage report
| Current view: | directory - toolkit/components/downloads - nsDownloadManager.h (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 3 | 3 | 100.0 %
| Date: | 2012-04-21 | Functions: | 1 | 1 | 100.0 %

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

Generated by: LCOV version 1.7