2003-11-27 00:54:33 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2005-02-01 17:07:35 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2003-11-27 00:54:33 +00:00
|
|
|
*
|
2005-02-01 17:07:35 +00:00
|
|
|
* 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/
|
2003-11-27 00:54:33 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2005-02-01 17:07:35 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2003-11-27 00:54:33 +00:00
|
|
|
* 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-22 00:03:33 +00:00
|
|
|
* Shawn Wilsher <me@shawnwilsher.com>
|
2007-09-06 04:59:34 +00:00
|
|
|
* Srirang G Doddihal <brahmana@doddihal.com>
|
2007-09-26 07:26:06 +00:00
|
|
|
* Edward Lee <edward.lee@engineering.uiuc.edu>
|
2003-11-27 00:54:33 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2005-02-01 17:07:35 +00:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2003-11-27 00:54:33 +00:00
|
|
|
* 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
|
2005-02-01 17:07:35 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2003-11-27 00:54:33 +00:00
|
|
|
* 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
|
2005-02-01 17:07:35 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2003-11-27 00:54:33 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2003-11-27 00:54:33 +00: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"
|
2005-04-05 23:24:23 +00:00
|
|
|
#include "nsIWebProgressListener2.h"
|
2003-11-27 00:54:33 +00:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIWebBrowserPersist.h"
|
|
|
|
#include "nsILocalFile.h"
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsIObserver.h"
|
2007-05-22 00:03:33 +00:00
|
|
|
#include "nsString.h"
|
2003-11-27 00:54:33 +00:00
|
|
|
#include "nsIStringBundle.h"
|
2003-12-02 00:59:58 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2003-11-27 00:54:33 +00:00
|
|
|
#include "nsIMIMEInfo.h"
|
2007-05-22 00:03:33 +00:00
|
|
|
#include "mozIStorageConnection.h"
|
2007-07-02 17:29:59 +00:00
|
|
|
#include "mozIStorageStatement.h"
|
2007-05-22 00:03:33 +00:00
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsArrayEnumerator.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsIObserverService.h"
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2003-12-09 05:55:09 +00:00
|
|
|
typedef PRInt16 DownloadState;
|
2003-12-10 21:59:41 +00:00
|
|
|
typedef PRInt16 DownloadType;
|
2003-12-09 05:55:09 +00:00
|
|
|
|
|
|
|
class nsDownload;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-08-22 21:55:57 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
class nsDownloadScanner;
|
|
|
|
#endif
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
class nsDownloadManager : public nsIDownloadManager,
|
|
|
|
public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDOWNLOADMANAGER
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
2007-07-12 20:01:57 +00:00
|
|
|
static nsDownloadManager *GetSingleton();
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
virtual ~nsDownloadManager();
|
2007-08-22 21:55:57 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
nsDownloadManager() : mScanner(nsnull) { };
|
|
|
|
private:
|
|
|
|
nsDownloadScanner *mScanner;
|
|
|
|
#endif
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
protected:
|
|
|
|
nsresult InitDB(PRBool *aDoImport);
|
|
|
|
nsresult CreateTable();
|
|
|
|
nsresult ImportDownloadHistory();
|
2007-08-30 19:01:39 +00:00
|
|
|
nsresult RestoreDatabaseState();
|
2007-09-26 07:26:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Paused downloads that survive across sessions are considered active, so
|
|
|
|
* rebuild the list of these downloads.
|
|
|
|
*/
|
|
|
|
nsresult RestoreActiveDownloads();
|
|
|
|
|
2007-06-05 00:03:35 +00:00
|
|
|
nsresult GetDownloadFromDB(PRUint32 aID, nsDownload **retVal);
|
2007-07-12 20:01:57 +00:00
|
|
|
|
2007-09-17 21:02:39 +00: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 23:20:00 +00:00
|
|
|
|
2007-08-22 21:55:57 +00:00
|
|
|
void SendEvent(nsDownload *aDownload, const char *aTopic);
|
|
|
|
|
2007-05-22 00:03:33 +00: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 07:20:16 +00:00
|
|
|
const nsAString &aTempPath,
|
2007-05-22 00:03:33 +00:00
|
|
|
PRInt64 aStartTime,
|
|
|
|
PRInt64 aEndTime,
|
|
|
|
PRInt32 aState);
|
|
|
|
|
2007-05-25 23:47:53 +00: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-22 00:03:33 +00:00
|
|
|
nsDownload *FindDownload(PRUint32 aID);
|
2007-09-26 07:26:06 +00: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();
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-06-16 22:03:02 +00:00
|
|
|
/**
|
2007-09-17 20:49:24 +00:00
|
|
|
* Removes download from "current downloads".
|
2007-06-17 01:12:27 +00:00
|
|
|
*
|
2007-09-17 20:49:24 +00:00
|
|
|
* This method removes the cycle created when starting the download, so
|
2007-06-17 01:12:27 +00:00
|
|
|
* make sure to use kungFuDeathGrip if you want to access member variables
|
2007-06-16 22:03:02 +00:00
|
|
|
*/
|
2007-08-22 21:55:57 +00:00
|
|
|
void CompleteDownload(nsDownload *aDownload);
|
2003-12-09 05:55:09 +00:00
|
|
|
|
2007-09-17 20:49:24 +00:00
|
|
|
void ConfirmCancelDownloads(PRInt32 aCount,
|
|
|
|
nsISupportsPRBool *aCancelDownloads,
|
|
|
|
const PRUnichar *aTitle,
|
|
|
|
const PRUnichar *aCancelMessageMultiple,
|
|
|
|
const PRUnichar *aCancelMessageSingle,
|
|
|
|
const PRUnichar *aDontCancelButton);
|
2003-12-02 00:59:58 +00:00
|
|
|
|
2007-09-17 20:49:24 +00:00
|
|
|
PRInt32 GetRetentionBehavior();
|
2007-09-06 04:59:34 +00:00
|
|
|
nsresult ExecuteDesiredAction(nsDownload *aDownload);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
private:
|
2007-05-25 23:47:53 +00:00
|
|
|
nsCOMArray<nsIDownloadProgressListener> mListeners;
|
2003-11-27 00:54:33 +00:00
|
|
|
nsCOMPtr<nsIStringBundle> mBundle;
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<mozIStorageConnection> mDBConn;
|
|
|
|
nsCOMArray<nsDownload> mCurrentDownloads;
|
|
|
|
nsCOMPtr<nsIObserverService> mObserverService;
|
2007-07-02 17:29:59 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> mUpdateDownloadStatement;
|
2007-05-22 00:03:33 +00:00
|
|
|
|
2007-07-12 20:01:57 +00:00
|
|
|
static nsDownloadManager *gDownloadManagerService;
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
friend class nsDownload;
|
|
|
|
};
|
|
|
|
|
2005-01-04 13:40:51 +00:00
|
|
|
class nsDownload : public nsIDownload
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIWEBPROGRESSLISTENER
|
2005-04-05 23:24:23 +00:00
|
|
|
NS_DECL_NSIWEBPROGRESSLISTENER2
|
2004-04-17 00:58:23 +00:00
|
|
|
NS_DECL_NSITRANSFER
|
2003-11-27 00:54:33 +00:00
|
|
|
NS_DECL_NSIDOWNLOAD
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsDownload();
|
|
|
|
virtual ~nsDownload();
|
|
|
|
|
2003-12-09 05:55:09 +00:00
|
|
|
public:
|
2007-05-22 00:03:33 +00:00
|
|
|
/**
|
|
|
|
* This method MUST be called when changing states on a download. It will
|
2007-05-23 23:12:03 +00:00
|
|
|
* notify the download listener when a change happens. This also updates the
|
|
|
|
* database, by calling UpdateDB().
|
2007-05-22 00:03:33 +00:00
|
|
|
*/
|
2007-05-23 23:12:03 +00:00
|
|
|
nsresult SetState(DownloadState aState);
|
2007-05-22 00:03:33 +00:00
|
|
|
|
2003-12-10 21:59:41 +00:00
|
|
|
DownloadType GetDownloadType();
|
|
|
|
void SetDownloadType(DownloadType aType);
|
2003-12-09 05:55:09 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult UpdateDB();
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
protected:
|
2007-05-22 00:03:33 +00:00
|
|
|
void SetStartTime(PRInt64 aStartTime);
|
|
|
|
|
2007-09-17 22:41:15 +00: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();
|
2005-12-19 15:25:22 +00:00
|
|
|
|
2007-09-17 21:16:16 +00: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-09-17 21:02:39 +00:00
|
|
|
/**
|
|
|
|
* Download is in a state to stop and complete the download?
|
|
|
|
*/
|
|
|
|
PRBool IsFinishable();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download is totally done transferring and all?
|
|
|
|
*/
|
|
|
|
PRBool IsFinished();
|
|
|
|
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownloadManager *mDownloadManager;
|
2004-04-17 00:58:23 +00:00
|
|
|
nsCOMPtr<nsIURI> mTarget;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsString mDisplayName;
|
2007-09-06 04:59:34 +00:00
|
|
|
nsCString mEntityID;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> mSource;
|
2007-08-17 23:05:26 +00:00
|
|
|
nsCOMPtr<nsIURI> mReferrer;
|
2005-04-24 21:16:28 +00:00
|
|
|
nsCOMPtr<nsICancelable> mCancelable;
|
2003-11-27 00:54:33 +00:00
|
|
|
nsCOMPtr<nsIRequest> mRequest;
|
2005-07-07 17:45:47 +00:00
|
|
|
nsCOMPtr<nsILocalFile> mTempFile;
|
2003-11-27 00:54:33 +00:00
|
|
|
nsCOMPtr<nsIMIMEInfo> mMIMEInfo;
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
DownloadState mDownloadState;
|
2007-09-17 20:49:24 +00:00
|
|
|
DownloadType mDownloadType;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
PRUint32 mID;
|
2003-11-27 00:54:33 +00:00
|
|
|
PRInt32 mPercentComplete;
|
2007-09-17 20:40:47 +00: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.
|
|
|
|
*/
|
2004-12-18 04:06:51 +00:00
|
|
|
PRUint64 mCurrBytes;
|
|
|
|
PRUint64 mMaxBytes;
|
2007-09-17 20:40:47 +00:00
|
|
|
|
2006-02-06 00:34:30 +00:00
|
|
|
PRTime mStartTime;
|
2003-11-27 00:54:33 +00:00
|
|
|
PRTime mLastUpdate;
|
2007-10-01 23:57:48 +00:00
|
|
|
PRInt64 mResumedAt;
|
2006-02-06 00:34:30 +00:00
|
|
|
double mSpeed;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
friend class nsDownloadManager;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|