2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Blake Ross <blaker@netscape.com>
|
|
|
|
* Ben Goodger <ben@netscape.com>
|
2007-05-21 17:03:33 -07:00
|
|
|
* Shawn Wilsher <me@shawnwilsher.com>
|
2007-09-05 21:59:34 -07:00
|
|
|
* Srirang G Doddihal <brahmana@doddihal.com>
|
2007-09-26 00:26:06 -07:00
|
|
|
* Edward Lee <edward.lee@engineering.uiuc.edu>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2007-09-17 13:49:24 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifndef downloadmanager___h___
|
|
|
|
#define downloadmanager___h___
|
|
|
|
|
|
|
|
#include "nsIDownloadManager.h"
|
|
|
|
#include "nsIDownloadProgressListener.h"
|
|
|
|
#include "nsIDownload.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIWebProgressListener.h"
|
|
|
|
#include "nsIWebProgressListener2.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIWebBrowserPersist.h"
|
|
|
|
#include "nsILocalFile.h"
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsIObserver.h"
|
2007-05-21 17:03:33 -07:00
|
|
|
#include "nsString.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsIMIMEInfo.h"
|
2007-05-21 17:03:33 -07:00
|
|
|
#include "mozIStorageConnection.h"
|
2007-07-02 10:29:59 -07:00
|
|
|
#include "mozIStorageStatement.h"
|
2007-05-21 17:03:33 -07:00
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsArrayEnumerator.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsIObserverService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
typedef PRInt16 DownloadState;
|
|
|
|
typedef PRInt16 DownloadType;
|
|
|
|
|
|
|
|
class nsDownload;
|
|
|
|
|
2008-01-10 01:10:21 -08:00
|
|
|
#if defined(XP_WIN) && !defined(__MINGW32__)
|
2007-08-22 14:55:57 -07:00
|
|
|
class nsDownloadScanner;
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
class nsDownloadManager : public nsIDownloadManager,
|
|
|
|
public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDOWNLOADMANAGER
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
2007-07-12 13:01:57 -07:00
|
|
|
static nsDownloadManager *GetSingleton();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual ~nsDownloadManager();
|
2008-01-10 01:10:21 -08:00
|
|
|
#if defined(XP_WIN) && !defined(__MINGW32__)
|
2007-08-22 14:55:57 -07:00
|
|
|
nsDownloadManager() : mScanner(nsnull) { };
|
|
|
|
private:
|
|
|
|
nsDownloadScanner *mScanner;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
protected:
|
|
|
|
nsresult InitDB(PRBool *aDoImport);
|
|
|
|
nsresult CreateTable();
|
|
|
|
nsresult ImportDownloadHistory();
|
2007-10-07 09:15:32 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fix up the database after a crash such as dealing with previously-active
|
2007-10-24 15:39:05 -07:00
|
|
|
* downloads. Call this before RestoreActiveDownloads to get the downloads
|
|
|
|
* fixed here to be auto-resumed.
|
2007-10-07 09:15:32 -07:00
|
|
|
*/
|
2007-08-30 12:01:39 -07:00
|
|
|
nsresult RestoreDatabaseState();
|
2007-09-26 00:26:06 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Paused downloads that survive across sessions are considered active, so
|
|
|
|
* rebuild the list of these downloads.
|
|
|
|
*/
|
|
|
|
nsresult RestoreActiveDownloads();
|
|
|
|
|
2007-06-04 17:03:35 -07:00
|
|
|
nsresult GetDownloadFromDB(PRUint32 aID, nsDownload **retVal);
|
2007-07-12 13:01:57 -07:00
|
|
|
|
2007-09-17 14:02:39 -07:00
|
|
|
/**
|
|
|
|
* Specially track the active downloads so that we don't need to check
|
|
|
|
* every download to see if they're in progress.
|
|
|
|
*/
|
|
|
|
nsresult AddToCurrentDownloads(nsDownload *aDl);
|
2007-06-22 16:20:00 -07:00
|
|
|
|
2007-08-22 14:55:57 -07:00
|
|
|
void SendEvent(nsDownload *aDownload, const char *aTopic);
|
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
/**
|
|
|
|
* Adds a download with the specified information to the DB.
|
|
|
|
*
|
|
|
|
* @return The id of the download, or 0 if there was an error.
|
|
|
|
*/
|
|
|
|
PRInt64 AddDownloadToDB(const nsAString &aName,
|
|
|
|
const nsACString &aSource,
|
|
|
|
const nsACString &aTarget,
|
2007-09-26 00:20:16 -07:00
|
|
|
const nsAString &aTempPath,
|
2007-05-21 17:03:33 -07:00
|
|
|
PRInt64 aStartTime,
|
|
|
|
PRInt64 aEndTime,
|
2007-10-24 15:19:04 -07:00
|
|
|
PRInt32 aState,
|
|
|
|
const nsACString &aMimeType,
|
|
|
|
const nsACString &aPreferredApp,
|
|
|
|
nsHandlerInfoAction aPreferredAction);
|
2007-05-21 17:03:33 -07:00
|
|
|
|
2007-05-25 16:47:53 -07:00
|
|
|
void NotifyListenersOnDownloadStateChange(PRInt16 aOldState,
|
|
|
|
nsIDownload *aDownload);
|
|
|
|
void NotifyListenersOnProgressChange(nsIWebProgress *aProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRInt64 aCurSelfProgress,
|
|
|
|
PRInt64 aMaxSelfProgress,
|
|
|
|
PRInt64 aCurTotalProgress,
|
|
|
|
PRInt64 aMaxTotalProgress,
|
|
|
|
nsIDownload *aDownload);
|
|
|
|
void NotifyListenersOnStateChange(nsIWebProgress *aProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRUint32 aStateFlags,
|
|
|
|
nsresult aStatus,
|
|
|
|
nsIDownload *aDownload);
|
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
nsDownload *FindDownload(PRUint32 aID);
|
2007-09-26 00:26:06 -07:00
|
|
|
|
2007-10-24 15:34:02 -07:00
|
|
|
/**
|
|
|
|
* First try to resume the download, and if that fails, retry it.
|
|
|
|
*
|
|
|
|
* @param aDl The download to resume and/or retry.
|
|
|
|
*/
|
|
|
|
nsresult ResumeRetry(nsDownload *aDl);
|
|
|
|
|
2007-10-24 15:31:49 -07:00
|
|
|
/**
|
|
|
|
* Pause all active downloads and remember if they should try to auto-resume
|
|
|
|
* when the download manager starts again.
|
|
|
|
*
|
|
|
|
* @param aSetResume Indicate if the downloads that get paused should be set
|
|
|
|
* as auto-resume.
|
|
|
|
*/
|
|
|
|
nsresult PauseAllDownloads(PRBool aSetResume);
|
|
|
|
|
2007-10-24 15:34:02 -07:00
|
|
|
/**
|
|
|
|
* Resume all paused downloads unless we're only supposed to do the automatic
|
|
|
|
* ones; in that case, try to retry them as well if resuming doesn't work.
|
|
|
|
*
|
|
|
|
* @param aResumeAll If true, all downloads will be resumed; otherwise, only
|
|
|
|
* those that are marked as auto-resume will resume.
|
|
|
|
*/
|
|
|
|
nsresult ResumeAllDownloads(PRBool aResumeAll);
|
|
|
|
|
2007-09-26 00:26:06 -07:00
|
|
|
/**
|
|
|
|
* Stop tracking the active downloads. Only use this when we're about to quit
|
|
|
|
* the download manager because we destroy our list of active downloads to
|
|
|
|
* break the dlmgr<->dl cycle. Active downloads that aren't real-paused will
|
|
|
|
* be canceled.
|
|
|
|
*/
|
|
|
|
nsresult RemoveAllDownloads();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-17 13:49:24 -07:00
|
|
|
void ConfirmCancelDownloads(PRInt32 aCount,
|
|
|
|
nsISupportsPRBool *aCancelDownloads,
|
|
|
|
const PRUnichar *aTitle,
|
|
|
|
const PRUnichar *aCancelMessageMultiple,
|
|
|
|
const PRUnichar *aCancelMessageSingle,
|
|
|
|
const PRUnichar *aDontCancelButton);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-17 13:49:24 -07:00
|
|
|
PRInt32 GetRetentionBehavior();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2007-05-25 16:47:53 -07:00
|
|
|
nsCOMArray<nsIDownloadProgressListener> mListeners;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIStringBundle> mBundle;
|
2007-05-21 17:03:33 -07:00
|
|
|
nsCOMPtr<mozIStorageConnection> mDBConn;
|
|
|
|
nsCOMArray<nsDownload> mCurrentDownloads;
|
|
|
|
nsCOMPtr<nsIObserverService> mObserverService;
|
2007-07-02 10:29:59 -07:00
|
|
|
nsCOMPtr<mozIStorageStatement> mUpdateDownloadStatement;
|
2007-05-21 17:03:33 -07:00
|
|
|
|
2007-07-12 13:01:57 -07:00
|
|
|
static nsDownloadManager *gDownloadManagerService;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
friend class nsDownload;
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsDownload : public nsIDownload
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIWEBPROGRESSLISTENER
|
|
|
|
NS_DECL_NSIWEBPROGRESSLISTENER2
|
|
|
|
NS_DECL_NSITRANSFER
|
|
|
|
NS_DECL_NSIDOWNLOAD
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsDownload();
|
|
|
|
virtual ~nsDownload();
|
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
/**
|
|
|
|
* This method MUST be called when changing states on a download. It will
|
2007-05-23 16:12:03 -07:00
|
|
|
* notify the download listener when a change happens. This also updates the
|
|
|
|
* database, by calling UpdateDB().
|
2007-05-21 17:03:33 -07:00
|
|
|
*/
|
2007-05-23 16:12:03 -07:00
|
|
|
nsresult SetState(DownloadState aState);
|
2007-05-21 17:03:33 -07:00
|
|
|
|
2007-10-02 08:03:16 -07:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Finish up the download by breaking reference cycles and clearing unneeded
|
|
|
|
* data. Additionally, the download removes itself from the download
|
|
|
|
* manager's list of current downloads.
|
|
|
|
*
|
|
|
|
* NOTE: This method removes the cycle created when starting the download, so
|
|
|
|
* make sure to use kungFuDeathGrip if you want to access member variables.
|
|
|
|
*/
|
|
|
|
void Finalize();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-10-02 08:03:16 -07:00
|
|
|
/**
|
|
|
|
* For finished resumed downloads that came in from exthandler, perform the
|
|
|
|
* action that would have been done if the download wasn't resumed.
|
|
|
|
*/
|
|
|
|
nsresult ExecuteDesiredAction();
|
2007-05-21 17:03:33 -07:00
|
|
|
|
2007-10-02 08:03:16 -07:00
|
|
|
/**
|
|
|
|
* Move the temporary file to the final destination by removing the existing
|
|
|
|
* dummy target and renaming the temporary.
|
|
|
|
*/
|
|
|
|
nsresult MoveTempToTarget();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the start time which also implies the last update time is the same.
|
|
|
|
*/
|
2007-05-21 17:03:33 -07:00
|
|
|
void SetStartTime(PRInt64 aStartTime);
|
|
|
|
|
2007-10-01 17:16:12 -07:00
|
|
|
/**
|
|
|
|
* Update the amount of bytes transferred and max bytes; and recalculate the
|
|
|
|
* download percent.
|
|
|
|
*/
|
|
|
|
void SetProgressBytes(PRInt64 aCurrBytes, PRInt64 aMaxBytes);
|
|
|
|
|
2007-09-17 15:41:15 -07:00
|
|
|
/**
|
|
|
|
* Pause the download, but in certain cases it might get fake-paused instead
|
|
|
|
* of real-paused.
|
|
|
|
*/
|
|
|
|
nsresult Pause();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All this does is cancel the connection that the download is using. It does
|
|
|
|
* not remove it from the download manager.
|
|
|
|
*/
|
|
|
|
nsresult Cancel();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resume the download. Works for both real-paused and fake-paused.
|
|
|
|
*/
|
|
|
|
nsresult Resume();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resume the real-paused download. Let Resume decide if this should get used.
|
|
|
|
*/
|
|
|
|
nsresult RealResume();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-17 14:16:16 -07:00
|
|
|
/**
|
|
|
|
* Download is not transferring?
|
|
|
|
*/
|
|
|
|
PRBool IsPaused();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download can continue from the middle of a transfer?
|
|
|
|
*/
|
|
|
|
PRBool IsResumable();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download was resumed?
|
|
|
|
*/
|
|
|
|
PRBool WasResumed();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download is real-paused? (not fake-paused by stalling the channel)
|
|
|
|
*/
|
|
|
|
PRBool IsRealPaused();
|
|
|
|
|
2007-10-24 15:23:13 -07:00
|
|
|
/**
|
|
|
|
* Indicates if the download should try to automatically resume or not.
|
|
|
|
*/
|
|
|
|
PRBool ShouldAutoResume();
|
|
|
|
|
2007-09-17 14:02:39 -07:00
|
|
|
/**
|
|
|
|
* Download is in a state to stop and complete the download?
|
|
|
|
*/
|
|
|
|
PRBool IsFinishable();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download is totally done transferring and all?
|
|
|
|
*/
|
|
|
|
PRBool IsFinished();
|
|
|
|
|
2007-10-02 08:03:16 -07:00
|
|
|
/**
|
|
|
|
* Update the DB with the current state of the download including time,
|
|
|
|
* download state and other values not known when first creating the
|
|
|
|
* download DB entry.
|
|
|
|
*/
|
|
|
|
nsresult UpdateDB();
|
|
|
|
|
2007-10-01 17:39:50 -07:00
|
|
|
/**
|
|
|
|
* Fail a download because of a failure status and prompt the provided
|
|
|
|
* message or use a generic download failure message if nsnull.
|
|
|
|
*/
|
|
|
|
nsresult FailDownload(nsresult aStatus, const PRUnichar *aMessage);
|
|
|
|
|
2007-10-24 15:19:04 -07:00
|
|
|
/**
|
|
|
|
* Opens the downloaded file with the appropriate application, which is
|
|
|
|
* either the OS default, MIME type default, or the one selected by the user.
|
|
|
|
*
|
|
|
|
* This also adds the temporary file to the "To be deleted on Exit" list, if
|
|
|
|
* the corresponding user preference is set (except on OS X).
|
|
|
|
*
|
|
|
|
* This function was adopted from nsExternalAppHandler::OpenWithApplication
|
|
|
|
* (uriloader/exthandler/nsExternalHelperAppService.cpp).
|
|
|
|
*/
|
|
|
|
nsresult OpenWithApplication();
|
|
|
|
|
2007-09-17 13:49:24 -07:00
|
|
|
nsDownloadManager *mDownloadManager;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIURI> mTarget;
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsString mDisplayName;
|
2007-09-05 21:59:34 -07:00
|
|
|
nsCString mEntityID;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> mSource;
|
2007-08-17 16:05:26 -07:00
|
|
|
nsCOMPtr<nsIURI> mReferrer;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsICancelable> mCancelable;
|
|
|
|
nsCOMPtr<nsIRequest> mRequest;
|
|
|
|
nsCOMPtr<nsILocalFile> mTempFile;
|
|
|
|
nsCOMPtr<nsIMIMEInfo> mMIMEInfo;
|
2007-09-17 13:49:24 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
DownloadState mDownloadState;
|
2007-09-17 13:49:24 -07:00
|
|
|
DownloadType mDownloadType;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
PRUint32 mID;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 mPercentComplete;
|
2007-09-17 13:40:47 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* These bytes are based on the position of where the request started, so 0
|
|
|
|
* doesn't necessarily mean we have nothing. Use GetAmountTransferred and
|
|
|
|
* GetSize for the real transferred amount and size.
|
|
|
|
*/
|
2007-10-01 17:16:12 -07:00
|
|
|
PRInt64 mCurrBytes;
|
|
|
|
PRInt64 mMaxBytes;
|
2007-09-17 13:40:47 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PRTime mStartTime;
|
|
|
|
PRTime mLastUpdate;
|
2007-10-01 16:57:48 -07:00
|
|
|
PRInt64 mResumedAt;
|
2007-03-22 10:30:00 -07:00
|
|
|
double mSpeed;
|
|
|
|
|
2007-10-24 15:23:13 -07:00
|
|
|
/**
|
|
|
|
* Track various states of the download trying to auto-resume when starting
|
|
|
|
* the download manager or restoring from a crash.
|
|
|
|
*
|
|
|
|
* DONT_RESUME: Don't automatically resume the download
|
|
|
|
* AUTO_RESUME: Automaically resume the download
|
|
|
|
*/
|
|
|
|
enum AutoResume { DONT_RESUME, AUTO_RESUME };
|
|
|
|
AutoResume mAutoResume;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
friend class nsDownloadManager;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|