Back to index

lightning-sunbird  0.9+nobinonly
nsDownloadManager.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Blake Ross <blaker@netscape.com>
00024  *   Ben Goodger <ben@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039  
00040 #ifndef downloadmanager___h___
00041 #define downloadmanager___h___
00042 
00043 #include "nsIDownloadManager.h"
00044 #include "nsIXPInstallManagerUI.h"
00045 #include "nsIDownloadProgressListener.h"
00046 #include "nsIDownload.h"
00047 #include "nsIRDFDataSource.h"
00048 #include "nsIRDFRemoteDataSource.h"
00049 #include "nsIRDFService.h"
00050 #include "nsIDOMDocument.h"
00051 #include "nsIDOMEventListener.h"
00052 #include "nsIRDFContainerUtils.h"
00053 #include "nsIWebProgressListener.h"
00054 #include "nsIWebProgressListener2.h"
00055 #include "nsIXPIProgressDialog.h"
00056 #include "nsIURI.h"
00057 #include "nsIWebBrowserPersist.h"
00058 #include "nsILocalFile.h"
00059 #include "nsHashtable.h"
00060 #include "nsIRequest.h"
00061 #include "nsIObserver.h"
00062 #include "nsIStringBundle.h"
00063 #include "nsISupportsPrimitives.h"
00064 #include "nsIProgressDialog.h"
00065 #include "nsIMIMEInfo.h"
00066 #include "nsITimer.h"
00067 #include "nsIAlertsService.h"
00068 #include "nsWeakReference.h"
00069 
00070 typedef PRInt16 DownloadState;
00071 typedef PRInt16 DownloadType;
00072 
00073 class nsXPIProgressListener;
00074 class nsDownload;
00075 
00076 class nsDownloadManager : public nsIDownloadManager,
00077                           public nsIXPInstallManagerUI,
00078                           public nsIObserver,
00079                           public nsSupportsWeakReference
00080 {
00081 public:
00082   NS_DECL_ISUPPORTS
00083   NS_DECL_NSIDOWNLOADMANAGER
00084   NS_DECL_NSIXPINSTALLMANAGERUI
00085   NS_DECL_NSIOBSERVER
00086 
00087   nsresult Init();
00088 
00089   nsDownloadManager();
00090   virtual ~nsDownloadManager();
00091 
00092   static PRInt32 PR_CALLBACK CancelAllDownloads(nsHashKey* aKey, void* aData, void* aClosure);
00093   static PRInt32 PR_CALLBACK BuildActiveDownloadsList(nsHashKey* aKey, void* aData, void* aClosure);
00094   nsresult DownloadEnded(const PRUnichar* aPersistentDescriptor, const PRUnichar* aMessage); 
00095 
00096 public:
00097   nsresult AssertProgressInfoFor(const PRUnichar* aPersistentDescriptor);
00098 
00099 protected:
00100   nsresult GetDownloadsContainer(nsIRDFContainer** aResult);
00101   nsresult GetProfileDownloadsFileURL(nsCString& aDownloadsFileURL);
00102   nsresult GetDataSource(nsIRDFDataSource** aDataSource);
00103   nsresult DownloadStarted(const PRUnichar* aPersistentDescriptor);
00104   nsresult GetInternalListener(nsIDownloadProgressListener** aListener);
00105   nsresult PauseResumeDownload(const PRUnichar* aPath, PRBool aPause);
00106   nsresult RemoveDownload(nsIRDFResource* aDownload);
00107   nsresult ValidateDownloadsContainer();
00108 
00109   void     ConfirmCancelDownloads(PRInt32 aCount, nsISupportsPRBool* aCancelDownloads,
00110                                   const PRUnichar* aTitle, 
00111                                   const PRUnichar* aCancelMessageMultiple, 
00112                                   const PRUnichar* aCancelMessageSingle,
00113                                   const PRUnichar* aDontCancelButton);
00114 
00115   static void     OpenTimerCallback(nsITimer* aTimer, void* aClosure);
00116   static nsresult OpenDownloadManager(PRBool aShouldFocus, PRInt32 aFlashCount, nsIDownload* aDownload, nsIDOMWindow* aParent);
00117 
00118   PRBool   NeedsUIUpdate() { return mListener != nsnull; }
00119   PRInt32  GetRetentionBehavior();
00120 
00121   static PRBool IsInFinalStage(DownloadState aState)
00122   {
00123     return aState == nsIDownloadManager::DOWNLOAD_NOTSTARTED ||
00124            aState == nsIDownloadManager::DOWNLOAD_DOWNLOADING ||
00125            aState == nsIXPInstallManagerUI::INSTALL_INSTALLING;
00126   };
00127 
00128   static PRBool IsInProgress(DownloadState aState) 
00129   {
00130     return aState == nsIDownloadManager::DOWNLOAD_NOTSTARTED || 
00131            aState == nsIDownloadManager::DOWNLOAD_DOWNLOADING || 
00132            aState == nsIDownloadManager::DOWNLOAD_PAUSED || 
00133            aState == nsIXPInstallManagerUI::INSTALL_DOWNLOADING ||
00134            aState == nsIXPInstallManagerUI::INSTALL_INSTALLING;
00135   };
00136 
00137   static PRBool CompletedSuccessfully(DownloadState aState)
00138   {
00139     return aState == nsIDownloadManager::DOWNLOAD_FINISHED || 
00140            aState == nsIXPInstallManagerUI::INSTALL_FINISHED;
00141   };
00142 
00143 private:
00144   nsCOMPtr<nsIDownloadProgressListener> mListener;
00145   nsCOMPtr<nsIRDFDataSource> mDataSource;
00146   nsCOMPtr<nsIXPIProgressDialog> mXPIProgress;
00147   nsCOMPtr<nsIRDFContainer> mDownloadsContainer;
00148   nsCOMPtr<nsIRDFContainerUtils> mRDFContainerUtils;
00149   nsCOMPtr<nsIStringBundle> mBundle;
00150   nsCOMPtr<nsITimer> mDMOpenTimer;
00151   PRInt32 mBatches;
00152   nsHashtable mCurrDownloads;
00153   
00154   friend class nsDownload;
00155 };
00156 
00157 class nsXPIProgressListener : public nsIXPIProgressDialog
00158 {
00159 public:
00160   NS_DECL_NSIXPIPROGRESSDIALOG
00161   NS_DECL_ISUPPORTS
00162 
00163   nsXPIProgressListener() { };
00164   nsXPIProgressListener(nsDownloadManager* aManager);
00165   virtual ~nsXPIProgressListener();
00166 
00167   void AddDownload(nsIDownload* aDownload);
00168 
00169   PRBool HasActiveXPIOperations();
00170 
00171 protected:
00172   void RemoveDownloadAtIndex(PRUint32 aIndex);
00173 
00174   inline void AssertProgressInfoForDownload(nsDownload* aDownload);
00175 
00176 private:
00177   nsDownloadManager* mDownloadManager;
00178   nsCOMPtr<nsISupportsArray> mDownloads;
00179 };
00180 
00181 class nsDownloadsDataSource : public nsIRDFDataSource, 
00182                               public nsIRDFRemoteDataSource
00183 {
00184 public:
00185   NS_DECL_NSIRDFDATASOURCE
00186   NS_DECL_NSIRDFREMOTEDATASOURCE
00187   NS_DECL_ISUPPORTS
00188 
00189   nsDownloadsDataSource() { };
00190   virtual ~nsDownloadsDataSource() { };
00191 
00192   nsresult LoadDataSource();
00193 
00194 private:
00195   nsCOMPtr<nsIRDFDataSource> mInner;
00196 };
00197 
00198 class nsDownload : public nsIDownload_MOZILLA_1_8_BRANCH
00199 {
00200 public:
00201   NS_DECL_NSIWEBPROGRESSLISTENER
00202   NS_DECL_NSIWEBPROGRESSLISTENER2
00203   NS_DECL_NSITRANSFER
00204   NS_DECL_NSIDOWNLOAD
00205   NS_DECL_NSIDOWNLOAD_MOZILLA_1_8_BRANCH
00206   NS_DECL_ISUPPORTS
00207 
00208   nsDownload();
00209   virtual ~nsDownload();
00210 
00211 public:
00212   DownloadState GetDownloadState();
00213   void SetDownloadState(DownloadState aState);
00214   DownloadType GetDownloadType();
00215   void SetDownloadType(DownloadType aType);
00216 
00217 protected:
00218   nsresult SetDownloadManager(nsDownloadManager* aDownloadManager);
00219   nsresult SetDialogListener(nsIWebProgressListener* aInternalListener);
00220   nsresult GetDialogListener(nsIWebProgressListener** aInternalListener);
00221   nsresult SetDialog(nsIProgressDialog* aDialog);
00222   nsresult GetDialog(nsIProgressDialog** aDialog);
00223   nsresult SetTempFile(nsILocalFile* aTempFile);
00224   nsresult GetTempFile(nsILocalFile** aTempFile);
00225   nsresult SetCancelable(nsICancelable* aCancelable);
00226   nsresult SetTarget(nsIURI* aTarget);
00227   nsresult SetDisplayName(const PRUnichar* aDisplayName);
00228   nsresult SetSource(nsIURI* aSource);
00229   nsresult SetMIMEInfo(nsIMIMEInfo* aMIMEInfo);
00230   nsresult SetStartTime(PRInt64 aStartTime);
00231 
00232   void Pause(PRBool aPaused);
00233   PRBool IsPaused();
00234 
00235   struct TransferInformation {
00236     PRInt64 mCurrBytes, mMaxBytes;
00237     TransferInformation(PRInt64 aCurr, PRInt64 aMax) :
00238       mCurrBytes(aCurr),
00239       mMaxBytes(aMax)
00240       {}
00241   };
00242   TransferInformation GetTransferInformation();
00243 
00244   nsDownloadManager* mDownloadManager;
00245   nsCOMPtr<nsIURI> mTarget;
00246 
00247 private:
00248   nsString mDisplayName;
00249 
00250   nsCOMPtr<nsIURI> mSource;
00251   nsCOMPtr<nsIWebProgressListener> mDialogListener;
00252   nsCOMPtr<nsICancelable> mCancelable;
00253   nsCOMPtr<nsIRequest> mRequest;
00254   nsCOMPtr<nsIProgressDialog> mDialog;
00255   nsCOMPtr<nsILocalFile> mTempFile;
00256   nsCOMPtr<nsIMIMEInfo> mMIMEInfo;
00257   
00258   DownloadState mDownloadState;
00259   DownloadType  mDownloadType;
00260 
00261   PRBool  mPaused;
00262   PRInt32 mPercentComplete;
00263   PRUint64 mCurrBytes;
00264   PRUint64 mMaxBytes;
00265   PRTime mStartTime;
00266   PRTime mLastUpdate;
00267   double mSpeed;
00268 
00269   friend class nsDownloadManager;
00270 };
00271 
00272 #endif