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-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 ***** */
|
|
|
|
|
|
|
|
#ifndef downloadmanager___h___
|
|
|
|
#define downloadmanager___h___
|
|
|
|
|
|
|
|
#include "nsIDownloadManager.h"
|
|
|
|
#include "nsIXPInstallManagerUI.h"
|
|
|
|
#include "nsIDownloadProgressListener.h"
|
|
|
|
#include "nsIDownload.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIWebProgressListener.h"
|
|
|
|
#include "nsIWebProgressListener2.h"
|
|
|
|
#include "nsIXPIProgressDialog.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 "nsIProgressDialog.h"
|
|
|
|
#include "nsIMIMEInfo.h"
|
|
|
|
#include "nsITimer.h"
|
2007-05-21 17:03:33 -07:00
|
|
|
#include "mozIStorageConnection.h"
|
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
#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 nsXPIProgressListener;
|
|
|
|
class nsDownload;
|
|
|
|
|
|
|
|
class nsDownloadManager : public nsIDownloadManager,
|
|
|
|
public nsIXPInstallManagerUI,
|
|
|
|
public nsIObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIDOWNLOADMANAGER
|
|
|
|
NS_DECL_NSIXPINSTALLMANAGERUI
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
virtual ~nsDownloadManager();
|
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
protected:
|
|
|
|
struct TimerParams {
|
|
|
|
nsRefPtr<nsDownload> download;
|
|
|
|
nsCOMPtr<nsIDOMWindow> parent;
|
|
|
|
};
|
|
|
|
nsresult InitDB(PRBool *aDoImport);
|
|
|
|
nsresult CreateTable();
|
|
|
|
nsresult ImportDownloadHistory();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
const nsAString &aIconURL,
|
|
|
|
PRInt64 aStartTime,
|
|
|
|
PRInt64 aEndTime,
|
|
|
|
PRInt32 aState);
|
|
|
|
|
|
|
|
nsDownload *FindDownload(PRUint32 aID);
|
|
|
|
nsresult PauseResumeDownload(PRUint32 aID, PRBool aPause);
|
|
|
|
nsresult CancelAllDownloads();
|
|
|
|
inline PRBool RemoveDownloadFromCurrent(nsDownload *aDownload)
|
|
|
|
{
|
|
|
|
return mCurrentDownloads.RemoveObject(aDownload);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
void ConfirmCancelDownloads(PRInt32 aCount,
|
|
|
|
nsISupportsPRBool* aCancelDownloads,
|
2007-03-22 10:30:00 -07:00
|
|
|
const PRUnichar* aTitle,
|
|
|
|
const PRUnichar* aCancelMessageMultiple,
|
|
|
|
const PRUnichar* aCancelMessageSingle,
|
|
|
|
const PRUnichar* aDontCancelButton);
|
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
static void OpenTimerCallback(nsITimer* aTimer, void* aClosure);
|
|
|
|
static nsresult OpenDownloadManager(PRBool aShouldFocus, PRInt32 aFlashCount,
|
|
|
|
nsIDownload* aDownload,
|
|
|
|
nsIDOMWindow* aParent);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRBool NeedsUIUpdate() { return mListener != nsnull; }
|
|
|
|
PRInt32 GetRetentionBehavior();
|
|
|
|
|
|
|
|
static PRBool IsInFinalStage(DownloadState aState)
|
|
|
|
{
|
|
|
|
return aState == nsIDownloadManager::DOWNLOAD_NOTSTARTED ||
|
|
|
|
aState == nsIDownloadManager::DOWNLOAD_DOWNLOADING ||
|
|
|
|
aState == nsIXPInstallManagerUI::INSTALL_INSTALLING;
|
2007-04-23 07:21:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static PRBool IsInProgress(DownloadState aState)
|
|
|
|
{
|
|
|
|
return aState == nsIDownloadManager::DOWNLOAD_NOTSTARTED ||
|
|
|
|
aState == nsIDownloadManager::DOWNLOAD_DOWNLOADING ||
|
|
|
|
aState == nsIDownloadManager::DOWNLOAD_PAUSED ||
|
|
|
|
aState == nsIXPInstallManagerUI::INSTALL_DOWNLOADING ||
|
|
|
|
aState == nsIXPInstallManagerUI::INSTALL_INSTALLING;
|
2007-04-23 07:21:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static PRBool CompletedSuccessfully(DownloadState aState)
|
|
|
|
{
|
|
|
|
return aState == nsIDownloadManager::DOWNLOAD_FINISHED ||
|
|
|
|
aState == nsIXPInstallManagerUI::INSTALL_FINISHED;
|
2007-04-23 07:21:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIDownloadProgressListener> mListener;
|
|
|
|
nsCOMPtr<nsIXPIProgressDialog> mXPIProgress;
|
|
|
|
nsCOMPtr<nsIStringBundle> mBundle;
|
|
|
|
nsCOMPtr<nsITimer> mDMOpenTimer;
|
2007-05-21 17:03:33 -07:00
|
|
|
nsCOMPtr<mozIStorageConnection> mDBConn;
|
|
|
|
nsCOMArray<nsDownload> mCurrentDownloads;
|
|
|
|
nsCOMPtr<nsIObserverService> mObserverService;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
friend class nsDownload;
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsXPIProgressListener : public nsIXPIProgressDialog
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIXPIPROGRESSDIALOG
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2007-04-23 07:21:53 -07:00
|
|
|
nsXPIProgressListener() { }
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXPIProgressListener(nsDownloadManager* aManager);
|
|
|
|
virtual ~nsXPIProgressListener();
|
|
|
|
|
|
|
|
void AddDownload(nsIDownload* aDownload);
|
|
|
|
|
|
|
|
PRBool HasActiveXPIOperations();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void RemoveDownloadAtIndex(PRUint32 aIndex);
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsDownloadManager* mDownloadManager;
|
|
|
|
nsCOMPtr<nsISupportsArray> mDownloads;
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsDownload : public nsIDownload
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_NSIWEBPROGRESSLISTENER
|
|
|
|
NS_DECL_NSIWEBPROGRESSLISTENER2
|
|
|
|
NS_DECL_NSITRANSFER
|
|
|
|
NS_DECL_NSIDOWNLOAD
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsDownload();
|
|
|
|
virtual ~nsDownload();
|
|
|
|
|
|
|
|
public:
|
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-03-22 10:30:00 -07:00
|
|
|
DownloadType GetDownloadType();
|
|
|
|
void SetDownloadType(DownloadType aType);
|
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
nsresult UpdateDB();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
2007-05-21 17:03:33 -07:00
|
|
|
void SetStartTime(PRInt64 aStartTime);
|
|
|
|
|
|
|
|
nsresult PauseResume(PRBool aPause);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsDownloadManager* mDownloadManager;
|
|
|
|
nsCOMPtr<nsIURI> mTarget;
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsString mDisplayName;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> mSource;
|
|
|
|
nsCOMPtr<nsICancelable> mCancelable;
|
|
|
|
nsCOMPtr<nsIRequest> mRequest;
|
|
|
|
nsCOMPtr<nsIProgressDialog> mDialog;
|
|
|
|
nsCOMPtr<nsILocalFile> mTempFile;
|
|
|
|
nsCOMPtr<nsIMIMEInfo> mMIMEInfo;
|
|
|
|
|
|
|
|
DownloadState mDownloadState;
|
|
|
|
DownloadType mDownloadType;
|
|
|
|
|
2007-05-21 17:03:33 -07:00
|
|
|
PRUint32 mID;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 mPercentComplete;
|
|
|
|
PRUint64 mCurrBytes;
|
|
|
|
PRUint64 mMaxBytes;
|
|
|
|
PRTime mStartTime;
|
|
|
|
PRTime mLastUpdate;
|
2007-05-21 17:03:33 -07:00
|
|
|
PRBool mPaused;
|
2007-03-22 10:30:00 -07:00
|
|
|
double mSpeed;
|
|
|
|
|
|
|
|
friend class nsDownloadManager;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|