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.
|
|
|
|
*
|
|
|
|
* 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> (Original Author)
|
|
|
|
* Ben Goodger <ben@netscape.com> (Original Author)
|
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>
|
2008-01-26 21:11:31 +00:00
|
|
|
* Graeme McCutcheon <graememcc_firefox@graeme-online.co.uk>
|
2003-11-27 00:54:33 +00: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
|
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
|
|
|
#include "nsDownloadManager.h"
|
|
|
|
#include "nsIWebProgress.h"
|
2007-05-22 00:03:33 +00:00
|
|
|
#include "nsIRDFService.h"
|
|
|
|
#include "nsIRDFContainer.h"
|
2003-11-27 00:54:33 +00:00
|
|
|
#include "nsIRDFLiteral.h"
|
|
|
|
#include "rdf.h"
|
|
|
|
#include "nsNetUtil.h"
|
2006-05-12 04:16:28 +00:00
|
|
|
#include "nsIURL.h"
|
2004-02-11 03:48:16 +00:00
|
|
|
#include "nsIDOMChromeWindow.h"
|
2003-11-27 00:54:33 +00:00
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDOMWindowInternal.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMEventTarget.h"
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2007-08-21 17:22:02 +00:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2003-11-27 00:54:33 +00:00
|
|
|
#include "nsIWindowWatcher.h"
|
|
|
|
#include "nsIWindowMediator.h"
|
|
|
|
#include "nsIPromptService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsVoidArray.h"
|
2003-12-10 21:59:41 +00:00
|
|
|
#include "nsEnumeratorUtils.h"
|
2004-04-17 00:58:23 +00:00
|
|
|
#include "nsIFileURL.h"
|
2005-03-07 16:07:11 +00:00
|
|
|
#include "nsEmbedCID.h"
|
2007-05-22 00:03:33 +00:00
|
|
|
#include "mozStorageCID.h"
|
|
|
|
#include "mozIStorageService.h"
|
2007-06-18 20:49:05 +00:00
|
|
|
#include "mozStorageHelper.h"
|
2007-05-22 00:03:33 +00:00
|
|
|
#include "nsIMutableArray.h"
|
|
|
|
#include "nsIAlertsService.h"
|
2007-08-22 20:20:32 +00:00
|
|
|
#include "nsIPropertyBag2.h"
|
2007-08-17 23:05:26 +00:00
|
|
|
#include "nsIHttpChannel.h"
|
2007-08-28 00:10:25 +00:00
|
|
|
#include "nsIDownloadManagerUI.h"
|
2007-09-06 04:59:34 +00:00
|
|
|
#include "nsIResumableChannel.h"
|
2007-10-24 22:19:04 +00:00
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsIExternalHelperAppService.h"
|
|
|
|
#include "nsIMIMEService.h"
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-11-23 21:29:22 +00:00
|
|
|
#include "nsIDownloadHistory.h"
|
|
|
|
#include "nsDocShellCID.h"
|
|
|
|
|
2008-02-20 22:42:38 +00:00
|
|
|
#if defined(XP_WIN) && !defined(WINCE)
|
2006-07-02 20:47:53 +00:00
|
|
|
#include <shlobj.h>
|
2008-01-09 12:20:18 +00:00
|
|
|
#ifndef __MINGW32__
|
2007-08-22 21:55:57 +00:00
|
|
|
#include "nsDownloadScanner.h"
|
2006-07-02 20:47:53 +00:00
|
|
|
#endif
|
2008-01-09 12:20:18 +00:00
|
|
|
#endif
|
2006-07-02 20:47:53 +00:00
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
#define DOWNLOAD_MANAGER_BUNDLE "chrome://mozapps/locale/downloads/downloads.properties"
|
|
|
|
#define DOWNLOAD_MANAGER_ALERT_ICON "chrome://mozapps/skin/downloads/downloadIcon.png"
|
2003-12-01 03:49:01 +00:00
|
|
|
#define PREF_BDM_SHOWALERTONCOMPLETE "browser.download.manager.showAlertOnComplete"
|
|
|
|
#define PREF_BDM_SHOWALERTINTERVAL "browser.download.manager.showAlertInterval"
|
|
|
|
#define PREF_BDM_RETENTION "browser.download.manager.retention"
|
2008-01-26 21:11:31 +00:00
|
|
|
#define PREF_BDM_QUITBEHAVIOR "browser.download.manager.quitBehavior"
|
2006-07-02 20:47:53 +00:00
|
|
|
#define PREF_BDM_ADDTORECENTDOCS "browser.download.manager.addToRecentDocs"
|
2008-01-30 05:07:44 +00:00
|
|
|
#define PREF_BDM_SCANWHENDONE "browser.download.manager.scanWhenDone"
|
2007-10-24 22:19:04 +00:00
|
|
|
#define PREF_BH_DELETETEMPFILEONEXIT "browser.helperApps.deleteTempFileOnExit"
|
2006-02-06 00:34:30 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
static const PRInt64 gUpdateInterval = 400 * PR_USEC_PER_MSEC;
|
2004-04-17 00:58:23 +00:00
|
|
|
|
2007-10-24 22:23:13 +00:00
|
|
|
#define DM_SCHEMA_VERSION 8
|
2007-07-03 21:04:54 +00:00
|
|
|
#define DM_DB_NAME NS_LITERAL_STRING("downloads.sqlite")
|
|
|
|
#define DM_DB_CORRUPT_FILENAME NS_LITERAL_STRING("downloads.sqlite.corrupt")
|
|
|
|
|
2007-10-02 15:03:16 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsDownloadManager
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-06-22 23:20:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(nsDownloadManager, nsIDownloadManager, nsIObserver)
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-07-12 20:01:57 +00:00
|
|
|
nsDownloadManager *nsDownloadManager::gDownloadManagerService = nsnull;
|
|
|
|
|
|
|
|
nsDownloadManager *
|
|
|
|
nsDownloadManager::GetSingleton()
|
|
|
|
{
|
|
|
|
if (gDownloadManagerService) {
|
|
|
|
NS_ADDREF(gDownloadManagerService);
|
|
|
|
return gDownloadManagerService;
|
|
|
|
}
|
|
|
|
|
|
|
|
gDownloadManagerService = new nsDownloadManager();
|
|
|
|
if (gDownloadManagerService) {
|
|
|
|
NS_ADDREF(gDownloadManagerService);
|
|
|
|
if (NS_FAILED(gDownloadManagerService->Init()))
|
|
|
|
NS_RELEASE(gDownloadManagerService);
|
|
|
|
}
|
|
|
|
|
|
|
|
return gDownloadManagerService;
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
nsDownloadManager::~nsDownloadManager()
|
|
|
|
{
|
2008-01-10 09:10:21 +00:00
|
|
|
#if defined(XP_WIN) && !defined(__MINGW32__)
|
2007-08-22 21:55:57 +00:00
|
|
|
delete mScanner;
|
|
|
|
#endif
|
2007-07-12 20:01:57 +00:00
|
|
|
gDownloadManagerService = nsnull;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-10-24 22:34:02 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::ResumeRetry(nsDownload *aDl)
|
|
|
|
{
|
|
|
|
// Keep a reference in case we need to cancel the download
|
|
|
|
nsRefPtr<nsDownload> dl = aDl;
|
|
|
|
|
|
|
|
// Try to resume the active download
|
|
|
|
nsresult rv = dl->Resume();
|
|
|
|
|
|
|
|
// If not, try to retry the download
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// First cancel the download so it's no longer active
|
|
|
|
rv = CancelDownload(dl->mID);
|
|
|
|
|
|
|
|
// Then retry it
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = RetryDownload(dl->mID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-10-24 22:31:49 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::PauseAllDownloads(PRBool aSetResume)
|
|
|
|
{
|
|
|
|
nsresult retVal = NS_OK;
|
|
|
|
for (PRInt32 i = mCurrentDownloads.Count() - 1; i >= 0; --i) {
|
|
|
|
nsRefPtr<nsDownload> dl = mCurrentDownloads[i];
|
|
|
|
|
|
|
|
// Only pause things that need to be paused
|
|
|
|
if (!dl->IsPaused()) {
|
|
|
|
// Set auto-resume before pausing so that it gets into the DB
|
|
|
|
dl->mAutoResume = aSetResume ? nsDownload::AUTO_RESUME :
|
|
|
|
nsDownload::DONT_RESUME;
|
|
|
|
|
|
|
|
// Try to pause the download but don't bail now if we fail
|
|
|
|
nsresult rv = dl->Pause();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
retVal = rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2007-10-24 22:34:02 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::ResumeAllDownloads(PRBool aResumeAll)
|
|
|
|
{
|
|
|
|
nsresult retVal = NS_OK;
|
|
|
|
for (PRInt32 i = mCurrentDownloads.Count() - 1; i >= 0; --i) {
|
|
|
|
nsRefPtr<nsDownload> dl = mCurrentDownloads[i];
|
|
|
|
|
|
|
|
// If aResumeAll is true, then resume everything; otherwise, check if the
|
|
|
|
// download should auto-resume
|
|
|
|
if (aResumeAll || dl->ShouldAutoResume()) {
|
|
|
|
// Reset auto-resume before retrying so that it gets into the DB through
|
|
|
|
// ResumeRetry's eventual call to SetState. We clear the value now so we
|
|
|
|
// don't accidentally query completed downloads that were previously
|
|
|
|
// auto-resumed (and try to resume them).
|
|
|
|
dl->mAutoResume = nsDownload::DONT_RESUME;
|
|
|
|
|
|
|
|
// Try to resume/retry the download but don't bail now if we fail
|
|
|
|
nsresult rv = ResumeRetry(dl);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
retVal = rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult
|
2007-09-26 07:26:06 +00:00
|
|
|
nsDownloadManager::RemoveAllDownloads()
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
for (PRInt32 i = mCurrentDownloads.Count() - 1; i >= 0; --i) {
|
|
|
|
nsRefPtr<nsDownload> dl = mCurrentDownloads[0];
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-09-26 07:26:06 +00:00
|
|
|
nsresult result;
|
2008-01-26 21:11:31 +00:00
|
|
|
if (dl->IsPaused() && GetQuitBehavior() != QUIT_AND_CANCEL)
|
2007-09-26 07:26:06 +00:00
|
|
|
result = mCurrentDownloads.RemoveObject(dl);
|
|
|
|
else
|
|
|
|
result = CancelDownload(dl->mID);
|
|
|
|
|
|
|
|
// Track the failure, but don't miss out on other downloads
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
rv = result;
|
2007-05-22 00:03:33 +00:00
|
|
|
}
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
return rv;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::InitDB(PRBool *aDoImport)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2007-05-22 00:03:33 +00:00
|
|
|
*aDoImport = PR_FALSE;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<mozIStorageService> storage =
|
|
|
|
do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIFile> dbFile;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
|
|
|
|
getter_AddRefs(dbFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-07-03 21:04:54 +00:00
|
|
|
rv = dbFile->Append(DM_DB_NAME);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
rv = storage->OpenDatabase(dbFile, getter_AddRefs(mDBConn));
|
2008-02-09 19:10:06 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRBool ready;
|
|
|
|
(void)mDBConn->GetConnectionReady(&ready);
|
|
|
|
if (!ready) {
|
|
|
|
// delete and try again, since we don't care so much about losing a users
|
|
|
|
// download history
|
|
|
|
rv = dbFile->Remove(PR_FALSE);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = storage->OpenDatabase(dbFile, getter_AddRefs(mDBConn));
|
2008-02-09 19:10:06 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
(void)mDBConn->GetConnectionReady(&ready);
|
|
|
|
if (!ready)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2006-03-10 16:57:31 +00:00
|
|
|
}
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
PRBool tableExists;
|
|
|
|
rv = mDBConn->TableExists(NS_LITERAL_CSTRING("moz_downloads"), &tableExists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!tableExists) {
|
|
|
|
*aDoImport = PR_TRUE;
|
|
|
|
rv = CreateTable();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-08-10 23:27:18 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checking the database schema now
|
|
|
|
PRInt32 schemaVersion;
|
|
|
|
rv = mDBConn->GetSchemaVersion(&schemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-07-03 21:04:54 +00:00
|
|
|
|
2007-08-10 23:27:18 +00:00
|
|
|
// Changing the database? Be sure to do these two things!
|
|
|
|
// 1) Increment DM_SCHEMA_VERSION
|
|
|
|
// 2) Implement the proper downgrade/upgrade code for the current version
|
|
|
|
|
|
|
|
switch (schemaVersion) {
|
|
|
|
// Upgrading
|
|
|
|
// Every time you increment the database schema, you need to implement
|
|
|
|
// the upgrading code from the previous version to the new one.
|
|
|
|
// Also, don't forget to make a unit test to test your upgrading code!
|
|
|
|
case 1: // Drop a column (iconURL) from the database (bug 385875)
|
|
|
|
{
|
|
|
|
// Safely wrap this in a transaction so we don't hose the whole DB
|
|
|
|
mozStorageTransaction safeTransaction(mDBConn, PR_TRUE);
|
|
|
|
|
|
|
|
// Create a temporary table that will store the existing records
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TEMPORARY TABLE moz_downloads_backup ("
|
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"name TEXT, "
|
|
|
|
"source TEXT, "
|
|
|
|
"target TEXT, "
|
|
|
|
"startTime INTEGER, "
|
|
|
|
"endTime INTEGER, "
|
|
|
|
"state INTEGER"
|
|
|
|
")"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Insert into a temporary table
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO moz_downloads_backup "
|
|
|
|
"SELECT id, name, source, target, startTime, endTime, state "
|
|
|
|
"FROM moz_downloads"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Drop the old table
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE moz_downloads"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Now recreate it with this schema version
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
2007-09-17 20:49:24 +00:00
|
|
|
"CREATE TABLE moz_downloads ("
|
2007-08-10 23:27:18 +00:00
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"name TEXT, "
|
|
|
|
"source TEXT, "
|
|
|
|
"target TEXT, "
|
|
|
|
"startTime INTEGER, "
|
|
|
|
"endTime INTEGER, "
|
|
|
|
"state INTEGER"
|
|
|
|
")"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Insert the data back into it
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO moz_downloads "
|
|
|
|
"SELECT id, name, source, target, startTime, endTime, state "
|
|
|
|
"FROM moz_downloads_backup"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// And drop our temporary table
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE moz_downloads_backup"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Finally, update the schemaVersion variable and the database schema
|
|
|
|
schemaVersion = 2;
|
|
|
|
rv = mDBConn->SetSchemaVersion(schemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// Fallthrough to the next upgrade
|
|
|
|
|
2007-08-17 23:05:26 +00:00
|
|
|
case 2: // Add referrer column to the database
|
|
|
|
{
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_downloads "
|
|
|
|
"ADD COLUMN referrer TEXT"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Finally, update the schemaVersion variable and the database schema
|
|
|
|
schemaVersion = 3;
|
|
|
|
rv = mDBConn->SetSchemaVersion(schemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// Fallthrough to the next upgrade
|
|
|
|
|
2007-09-06 04:59:34 +00:00
|
|
|
case 3: // This version adds a column to the database (entityID)
|
|
|
|
{
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_downloads "
|
|
|
|
"ADD COLUMN entityID TEXT"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Finally, update the schemaVersion variable and the database schema
|
|
|
|
schemaVersion = 4;
|
|
|
|
rv = mDBConn->SetSchemaVersion(schemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// Fallthrough to the next upgrade
|
|
|
|
|
2007-09-26 07:20:16 +00:00
|
|
|
case 4: // This version adds a column to the database (tempPath)
|
|
|
|
{
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_downloads "
|
|
|
|
"ADD COLUMN tempPath TEXT"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Finally, update the schemaVersion variable and the database schema
|
|
|
|
schemaVersion = 5;
|
|
|
|
rv = mDBConn->SetSchemaVersion(schemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// Fallthrough to the next upgrade
|
|
|
|
|
2007-10-24 22:23:13 +00:00
|
|
|
case 5: // This version adds two columns for tracking transfer progress
|
2007-10-02 00:16:12 +00:00
|
|
|
{
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_downloads "
|
|
|
|
"ADD COLUMN currBytes INTEGER NOT NULL DEFAULT 0"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_downloads "
|
|
|
|
"ADD COLUMN maxBytes INTEGER NOT NULL DEFAULT -1"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Finally, update the schemaVersion variable and the database schema
|
|
|
|
schemaVersion = 6;
|
|
|
|
rv = mDBConn->SetSchemaVersion(schemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// Fallthrough to the next upgrade
|
|
|
|
|
2007-10-24 22:19:04 +00:00
|
|
|
case 6: // This version adds three columns to DB (MIME type related info)
|
|
|
|
{
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_downloads "
|
|
|
|
"ADD COLUMN mimeType TEXT"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_downloads "
|
|
|
|
"ADD COLUMN preferredApplication TEXT"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_downloads "
|
|
|
|
"ADD COLUMN preferredAction INTEGER NOT NULL DEFAULT 0"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Finally, update the schemaVersion variable and the database schema
|
|
|
|
schemaVersion = 7;
|
|
|
|
rv = mDBConn->SetSchemaVersion(schemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// Fallthrough to next upgrade
|
|
|
|
|
2007-10-24 22:23:13 +00:00
|
|
|
case 7: // This version adds a column to remember to auto-resume downloads
|
|
|
|
{
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_downloads "
|
|
|
|
"ADD COLUMN autoResume INTEGER NOT NULL DEFAULT 0"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Finally, update the schemaVersion variable and the database schema
|
|
|
|
schemaVersion = 8;
|
|
|
|
rv = mDBConn->SetSchemaVersion(schemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// Fallthrough to the next upgrade
|
|
|
|
|
2007-09-17 22:51:33 +00:00
|
|
|
// Extra sanity checking for developers
|
|
|
|
#ifndef DEBUG
|
2007-08-10 23:27:18 +00:00
|
|
|
case DM_SCHEMA_VERSION:
|
2007-09-17 22:51:33 +00:00
|
|
|
#endif
|
2007-08-10 23:27:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
NS_WARNING("Could not get download database's schema version!");
|
2007-07-03 21:04:54 +00:00
|
|
|
|
|
|
|
// The table may still be usable - someone may have just messed with the
|
|
|
|
// schema version, so let's just treat this like a downgrade and verify
|
|
|
|
// that the needed columns are there. If they aren't there, we'll drop
|
|
|
|
// the table anyway.
|
|
|
|
rv = mDBConn->SetSchemaVersion(DM_SCHEMA_VERSION);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2007-08-10 23:27:18 +00:00
|
|
|
// Fallthrough to downgrade check
|
|
|
|
|
|
|
|
// Downgrading
|
|
|
|
// If columns have been added to the table, we can still use the ones we
|
|
|
|
// understand safely. If columns have been deleted or alterd, we just
|
|
|
|
// drop the table and start from scratch. If you change how a column
|
|
|
|
// should be interpreted, make sure you also change its name so this
|
|
|
|
// check will catch it.
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
2007-09-26 07:20:16 +00:00
|
|
|
"SELECT id, name, source, target, tempPath, startTime, endTime, state, "
|
2007-10-24 22:19:04 +00:00
|
|
|
"referrer, entityID, currBytes, maxBytes, mimeType, "
|
2007-10-24 22:23:13 +00:00
|
|
|
"preferredApplication, preferredAction, autoResume "
|
2007-08-10 23:27:18 +00:00
|
|
|
"FROM moz_downloads"), getter_AddRefs(stmt));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// if the statement fails, that means all the columns were not there.
|
|
|
|
// First we backup the database
|
|
|
|
nsCOMPtr<nsIFile> backup;
|
|
|
|
rv = mDBConn->BackupDB(DM_DB_CORRUPT_FILENAME, nsnull,
|
|
|
|
getter_AddRefs(backup));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-07-03 21:04:54 +00:00
|
|
|
|
2007-08-10 23:27:18 +00:00
|
|
|
// Then we dump it
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DROP TABLE moz_downloads"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = CreateTable();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-07-03 21:04:54 +00:00
|
|
|
}
|
2007-08-10 23:27:18 +00:00
|
|
|
break;
|
2007-05-22 00:03:33 +00:00
|
|
|
}
|
2004-04-27 04:13:20 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::CreateTable()
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-07-03 21:04:54 +00:00
|
|
|
nsresult rv = mDBConn->SetSchemaVersion(DM_SCHEMA_VERSION);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
return mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE moz_downloads ("
|
2007-07-22 17:24:03 +00:00
|
|
|
"id INTEGER PRIMARY KEY, "
|
|
|
|
"name TEXT, "
|
|
|
|
"source TEXT, "
|
|
|
|
"target TEXT, "
|
2007-09-26 07:20:16 +00:00
|
|
|
"tempPath TEXT, "
|
2007-07-22 17:24:03 +00:00
|
|
|
"startTime INTEGER, "
|
|
|
|
"endTime INTEGER, "
|
2007-08-17 23:05:26 +00:00
|
|
|
"state INTEGER, "
|
2007-09-06 04:59:34 +00:00
|
|
|
"referrer TEXT, "
|
2007-10-02 00:16:12 +00:00
|
|
|
"entityID TEXT, "
|
|
|
|
"currBytes INTEGER NOT NULL DEFAULT 0, "
|
2007-10-24 22:19:04 +00:00
|
|
|
"maxBytes INTEGER NOT NULL DEFAULT -1, "
|
|
|
|
"mimeType TEXT, "
|
|
|
|
"preferredApplication TEXT, "
|
2007-10-24 22:23:13 +00:00
|
|
|
"preferredAction INTEGER NOT NULL DEFAULT 0, "
|
|
|
|
"autoResume INTEGER NOT NULL DEFAULT 0"
|
2007-07-22 17:24:03 +00:00
|
|
|
")"));
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::ImportDownloadHistory()
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIFile> dlFile;
|
|
|
|
nsresult rv = NS_GetSpecialDirectory(NS_APP_DOWNLOADS_50_FILE,
|
|
|
|
getter_AddRefs(dlFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRBool check;
|
|
|
|
rv = dlFile->Exists(&check);
|
|
|
|
if (NS_FAILED(rv) || !check)
|
|
|
|
return rv;
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
rv = dlFile->IsFile(&check);
|
|
|
|
if (NS_FAILED(rv) || !check)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCAutoString dlSrc;
|
|
|
|
rv = NS_GetURLSpecFromFile(dlFile, dlSrc);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIRDFService> rdfs =
|
|
|
|
do_GetService("@mozilla.org/rdf/rdf-service;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIRDFDataSource> ds;
|
|
|
|
rv = rdfs->GetDataSourceBlocking(dlSrc.get(), getter_AddRefs(ds));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// OK, we now have our datasouce, so lets get our resources
|
|
|
|
nsCOMPtr<nsIRDFResource> NC_DownloadsRoot;
|
|
|
|
rv = rdfs->GetResource(NS_LITERAL_CSTRING("NC:DownloadsRoot"),
|
|
|
|
getter_AddRefs(NC_DownloadsRoot));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIRDFResource> NC_Name;
|
|
|
|
rv = rdfs->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "Name"),
|
|
|
|
getter_AddRefs(NC_Name));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIRDFResource> NC_URL;
|
|
|
|
rv = rdfs->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "URL"),
|
|
|
|
getter_AddRefs(NC_URL));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIRDFResource> NC_File;
|
|
|
|
rv = rdfs->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "File"),
|
|
|
|
getter_AddRefs(NC_File));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIRDFResource> NC_DateStarted;
|
|
|
|
rv = rdfs->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "DateStarted"),
|
|
|
|
getter_AddRefs(NC_DateStarted));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIRDFResource> NC_DateEnded;
|
|
|
|
rv = rdfs->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "DateEnded"),
|
|
|
|
getter_AddRefs(NC_DateEnded));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIRDFResource> NC_DownloadState;
|
|
|
|
rv = rdfs->GetResource(NS_LITERAL_CSTRING(NC_NAMESPACE_URI "DownloadState"),
|
|
|
|
getter_AddRefs(NC_DownloadState));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-06-18 20:49:05 +00:00
|
|
|
mozStorageTransaction transaction(mDBConn, PR_TRUE);
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// OK, now we can actually start to read and process our data
|
|
|
|
nsCOMPtr<nsIRDFContainer> container =
|
|
|
|
do_CreateInstance(NS_RDF_CONTRACTID "/container;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = container->Init(ds, NC_DownloadsRoot);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> dls;
|
|
|
|
rv = container->GetElements(getter_AddRefs(dls));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
PRBool hasMore;
|
|
|
|
while (NS_SUCCEEDED(dls->HasMoreElements(&hasMore)) && hasMore) {
|
|
|
|
nsCOMPtr<nsISupports> itm;
|
|
|
|
rv = dls->GetNext(getter_AddRefs(itm));
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFResource> dl = do_QueryInterface(itm, &rv);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFNode> node;
|
|
|
|
// Getting the data
|
|
|
|
nsString name;
|
|
|
|
nsCString source, target;
|
|
|
|
PRInt64 startTime, endTime;
|
|
|
|
PRInt32 state;
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
rv = ds->GetTarget(dl, NC_Name, PR_TRUE, getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
nsCOMPtr<nsIRDFLiteral> rdfLit = do_QueryInterface(node, &rv);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
rv = rdfLit->GetValue(getter_Copies(name));
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
|
|
|
|
rv = ds->GetTarget(dl, NC_URL, PR_TRUE, getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
nsCOMPtr<nsIRDFResource> rdfRes = do_QueryInterface(node, &rv);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
rv = rdfRes->GetValueUTF8(source);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
|
|
|
|
rv = ds->GetTarget(dl, NC_File, PR_TRUE, getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
rdfRes = do_QueryInterface(node, &rv);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
rv = rdfRes->GetValueUTF8(target);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
|
|
|
|
rv = ds->GetTarget(dl, NC_DateStarted, PR_TRUE, getter_AddRefs(node));
|
2007-05-25 17:25:51 +00:00
|
|
|
if (NS_FAILED(rv) || !node) {
|
|
|
|
rv = ds->GetTarget(dl, NC_DateEnded, PR_TRUE, getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
}
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIRDFDate> rdfDate = do_QueryInterface(node, &rv);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
rv = rdfDate->GetValue(&startTime);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
rv = ds->GetTarget(dl, NC_DateEnded, PR_TRUE, getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
rdfDate = do_QueryInterface(node, &rv);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
rv = rdfDate->GetValue(&endTime);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
|
|
|
|
rv = ds->GetTarget(dl, NC_DownloadState, PR_TRUE, getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
nsCOMPtr<nsIRDFInt> rdfInt = do_QueryInterface(node, &rv);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
|
|
|
rv = rdfInt->GetValue(&state);
|
|
|
|
if (NS_FAILED(rv)) continue;
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-09-26 07:20:16 +00:00
|
|
|
(void)AddDownloadToDB(name, source, target, EmptyString(), startTime,
|
2007-10-24 22:19:04 +00:00
|
|
|
endTime, state, EmptyCString(), EmptyCString(),
|
|
|
|
nsIMIMEInfo::saveToDisk);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-08-30 19:01:39 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::RestoreDatabaseState()
|
|
|
|
{
|
2007-12-27 01:42:05 +00:00
|
|
|
// Restore downloads that were in a scanning state. We can assume that they
|
|
|
|
// have been dealt with by the virus scanner
|
2007-08-30 19:01:39 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
2007-12-27 01:42:05 +00:00
|
|
|
"UPDATE moz_downloads "
|
|
|
|
"SET state = ?1 "
|
|
|
|
"WHERE state = ?2"), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRInt32 i = 0;
|
|
|
|
rv = stmt->BindInt32Parameter(i++, nsIDownloadManager::DOWNLOAD_FINISHED);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = stmt->BindInt32Parameter(i++, nsIDownloadManager::DOWNLOAD_SCANNING);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Convert supposedly-active downloads into downloads that should auto-resume
|
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
2007-08-30 19:01:39 +00:00
|
|
|
"UPDATE moz_downloads "
|
2007-10-24 22:39:05 +00:00
|
|
|
"SET autoResume = ?1 "
|
2007-08-30 19:01:39 +00:00
|
|
|
"WHERE state = ?2 "
|
2007-09-17 20:49:24 +00:00
|
|
|
"OR state = ?3 "
|
|
|
|
"OR state = ?4"), getter_AddRefs(stmt));
|
2007-08-30 19:01:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-12-27 01:42:05 +00:00
|
|
|
i = 0;
|
2007-10-24 22:39:05 +00:00
|
|
|
rv = stmt->BindInt32Parameter(i++, nsDownload::AUTO_RESUME);
|
2007-08-30 19:01:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindInt32Parameter(i++, nsIDownloadManager::DOWNLOAD_NOTSTARTED);
|
2007-08-30 19:01:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindInt32Parameter(i++, nsIDownloadManager::DOWNLOAD_QUEUED);
|
2007-08-30 19:01:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindInt32Parameter(i++, nsIDownloadManager::DOWNLOAD_DOWNLOADING);
|
2007-08-30 19:01:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-10-07 16:15:32 +00:00
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-02-22 03:50:01 +00:00
|
|
|
// Switch any download that is supposed to automatically resume and is in a
|
|
|
|
// finished state to *not* automatically resume. See Bug 409179 for details.
|
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE moz_downloads "
|
|
|
|
"SET autoResume = ?1 "
|
|
|
|
"WHERE state = ?2 "
|
|
|
|
"AND autoResume = ?3"),
|
|
|
|
getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
rv = stmt->BindInt32Parameter(i++, nsDownload::DONT_RESUME);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = stmt->BindInt32Parameter(i++, nsIDownloadManager::DOWNLOAD_FINISHED);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = stmt->BindInt32Parameter(i++, nsDownload::AUTO_RESUME);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-08-30 19:01:39 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-09-26 07:26:06 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::RestoreActiveDownloads()
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SELECT id "
|
|
|
|
"FROM moz_downloads "
|
2007-10-24 22:34:02 +00:00
|
|
|
"WHERE (state = ?1 AND LENGTH(entityID) > 0) "
|
|
|
|
"OR autoResume != ?2"), getter_AddRefs(stmt));
|
2007-09-26 07:26:06 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->BindInt32Parameter(0, nsIDownloadManager::DOWNLOAD_PAUSED);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-10-24 22:34:02 +00:00
|
|
|
rv = stmt->BindInt32Parameter(1, nsDownload::DONT_RESUME);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-26 07:26:06 +00:00
|
|
|
|
2007-10-24 22:34:02 +00:00
|
|
|
nsresult retVal = NS_OK;
|
2007-09-26 07:26:06 +00:00
|
|
|
PRBool hasResults;
|
|
|
|
while (NS_SUCCEEDED(stmt->ExecuteStep(&hasResults)) && hasResults) {
|
|
|
|
nsRefPtr<nsDownload> dl;
|
|
|
|
// Keep trying to add even if we fail one, but make sure to return failure.
|
|
|
|
// Additionally, be careful to not call anything that tries to change the
|
|
|
|
// database because we're iterating over a live statement.
|
|
|
|
if (NS_FAILED(GetDownloadFromDB(stmt->AsInt32(0), getter_AddRefs(dl))) ||
|
|
|
|
NS_FAILED(AddToCurrentDownloads(dl)))
|
2007-10-24 22:34:02 +00:00
|
|
|
retVal = NS_ERROR_FAILURE;
|
2007-09-26 07:26:06 +00:00
|
|
|
}
|
2007-10-24 22:34:02 +00:00
|
|
|
|
|
|
|
// Try to resume only the downloads that should auto-resume
|
|
|
|
rv = ResumeAllDownloads(PR_FALSE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return retVal;
|
2007-09-26 07:26:06 +00:00
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
PRInt64
|
|
|
|
nsDownloadManager::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,
|
2007-10-24 22:19:04 +00:00
|
|
|
PRInt32 aState,
|
|
|
|
const nsACString &aMimeType,
|
|
|
|
const nsACString &aPreferredApp,
|
|
|
|
nsHandlerInfoAction aPreferredAction)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO moz_downloads "
|
2007-10-24 22:19:04 +00:00
|
|
|
"(name, source, target, tempPath, startTime, endTime, state, "
|
|
|
|
"mimeType, preferredApplication, preferredAction) "
|
|
|
|
"VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)"), getter_AddRefs(stmt));
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-09-17 21:10:29 +00:00
|
|
|
PRInt32 i = 0;
|
2007-05-22 00:03:33 +00:00
|
|
|
// name
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindStringParameter(i++, aName);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// source
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindUTF8StringParameter(i++, aSource);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// target
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindUTF8StringParameter(i++, aTarget);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-09-26 07:20:16 +00:00
|
|
|
// tempPath
|
|
|
|
rv = stmt->BindStringParameter(i++, aTempPath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// startTime
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindInt64Parameter(i++, aStartTime);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// endTime
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindInt64Parameter(i++, aEndTime);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// state
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindInt32Parameter(i++, aState);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-10-24 22:19:04 +00:00
|
|
|
// mimeType
|
|
|
|
rv = stmt->BindUTF8StringParameter(i++, aMimeType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
|
|
|
|
|
|
|
// preferredApplication
|
|
|
|
rv = stmt->BindUTF8StringParameter(i++, aPreferredApp);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
|
|
|
|
|
|
|
// preferredAction
|
|
|
|
rv = stmt->BindInt32Parameter(i++, aPreferredAction);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
PRBool hasMore;
|
|
|
|
rv = stmt->ExecuteStep(&hasMore); // we want to keep our lock
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
PRInt64 id = 0;
|
|
|
|
rv = mDBConn->GetLastInsertRowID(&id);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// lock on DB from statement will be released once we return
|
|
|
|
return id;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::Init()
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2007-05-22 00:03:33 +00:00
|
|
|
mObserverService = do_GetService("@mozilla.org/observer-service;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
PRBool doImport;
|
|
|
|
rv = InitDB(&doImport);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
if (doImport)
|
|
|
|
ImportDownloadHistory();
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
rv = bundleService->CreateBundle(DOWNLOAD_MANAGER_BUNDLE,
|
|
|
|
getter_AddRefs(mBundle));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2008-02-20 22:42:38 +00:00
|
|
|
#if defined(XP_WIN) && !defined(__MINGW32__) && !defined(WINCE)
|
2007-08-22 21:55:57 +00:00
|
|
|
mScanner = new nsDownloadScanner();
|
|
|
|
if (!mScanner)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
rv = mScanner->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete mScanner;
|
|
|
|
mScanner = nsnull;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-08-17 23:05:26 +00:00
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE moz_downloads "
|
2007-10-14 00:57:34 +00:00
|
|
|
"SET tempPath = ?1, startTime = ?2, endTime = ?3, state = ?4, "
|
2007-10-24 22:23:13 +00:00
|
|
|
"referrer = ?5, entityID = ?6, currBytes = ?7, maxBytes = ?8, "
|
|
|
|
"autoResume = ?9 "
|
|
|
|
"WHERE id = ?10"), getter_AddRefs(mUpdateDownloadStatement));
|
2007-08-17 23:05:26 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-10-07 16:15:32 +00:00
|
|
|
// Do things *after* initializing various download manager properties such as
|
|
|
|
// restoring downloads to a consistent state
|
|
|
|
rv = RestoreDatabaseState();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = RestoreActiveDownloads();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// The following three AddObserver calls must be the last lines in this function,
|
|
|
|
// because otherwise, this function may fail (and thus, this object would be not
|
|
|
|
// completely initialized), but the observerservice would still keep a reference
|
|
|
|
// to us and notify us about shutdown, which may cause crashes.
|
|
|
|
// failure to add an observer is not critical
|
|
|
|
//
|
|
|
|
// These observers will be cleaned up automatically at app shutdown. We do
|
|
|
|
// not bother explicitly breaking the observers because we are a singleton
|
|
|
|
// that lives for the duration of the app.
|
|
|
|
mObserverService->AddObserver(this, "quit-application", PR_FALSE);
|
|
|
|
mObserverService->AddObserver(this, "quit-application-requested", PR_FALSE);
|
|
|
|
mObserverService->AddObserver(this, "offline-requested", PR_FALSE);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-09-17 20:49:24 +00:00
|
|
|
PRInt32
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::GetRetentionBehavior()
|
|
|
|
{
|
|
|
|
// We use 0 as the default, which is "remove when done"
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPrefBranch> pref = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
PRInt32 val;
|
|
|
|
rv = pref->GetIntPref(PREF_BDM_RETENTION, &val);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
return val;
|
|
|
|
}
|
2005-12-19 15:25:22 +00:00
|
|
|
|
2008-01-26 21:11:31 +00:00
|
|
|
enum nsDownloadManager::QuitBehavior
|
|
|
|
nsDownloadManager::GetQuitBehavior()
|
|
|
|
{
|
|
|
|
// We use 0 as the default, which is "remember and resume the download"
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPrefBranch> pref = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, QUIT_AND_RESUME);
|
|
|
|
|
|
|
|
PRInt32 val;
|
|
|
|
rv = pref->GetIntPref(PREF_BDM_QUITBEHAVIOR, &val);
|
|
|
|
NS_ENSURE_SUCCESS(rv, QUIT_AND_RESUME);
|
|
|
|
|
|
|
|
switch (val) {
|
|
|
|
case 1:
|
|
|
|
return QUIT_AND_PAUSE;
|
|
|
|
case 2:
|
|
|
|
return QUIT_AND_CANCEL;
|
|
|
|
default:
|
|
|
|
return QUIT_AND_RESUME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-05 00:03:35 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::GetDownloadFromDB(PRUint32 aID, nsDownload **retVal)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!FindDownload(aID),
|
|
|
|
"If it is a current download, you should not call this method!");
|
|
|
|
|
|
|
|
// First, let's query the database and see if it even exists
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
2007-09-26 07:20:16 +00:00
|
|
|
"SELECT id, state, startTime, source, target, tempPath, name, referrer, "
|
2007-10-24 22:19:04 +00:00
|
|
|
"entityID, currBytes, maxBytes, mimeType, preferredAction, "
|
2007-10-24 22:23:13 +00:00
|
|
|
"preferredApplication, autoResume "
|
2007-06-05 00:03:35 +00:00
|
|
|
"FROM moz_downloads "
|
|
|
|
"WHERE id = ?1"), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmt->BindInt64Parameter(0, aID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRBool hasResults = PR_FALSE;
|
|
|
|
rv = stmt->ExecuteStep(&hasResults);
|
|
|
|
if (NS_FAILED(rv) || !hasResults)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
// We have a download, so lets create it
|
|
|
|
nsRefPtr<nsDownload> dl = new nsDownload();
|
|
|
|
if (!dl)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2007-09-17 21:10:29 +00:00
|
|
|
PRInt32 i = 0;
|
2007-06-05 00:03:35 +00:00
|
|
|
// Setting all properties of the download now
|
|
|
|
dl->mCancelable = nsnull;
|
2007-09-17 21:10:29 +00:00
|
|
|
dl->mID = stmt->AsInt64(i++);
|
|
|
|
dl->mDownloadState = stmt->AsInt32(i++);
|
|
|
|
dl->mStartTime = stmt->AsInt64(i++);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-06-05 00:03:35 +00:00
|
|
|
nsCString source;
|
2007-09-17 21:10:29 +00:00
|
|
|
stmt->GetUTF8String(i++, source);
|
2007-06-05 00:03:35 +00:00
|
|
|
rv = NS_NewURI(getter_AddRefs(dl->mSource), source);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCString target;
|
2007-09-17 21:10:29 +00:00
|
|
|
stmt->GetUTF8String(i++, target);
|
2007-06-05 00:03:35 +00:00
|
|
|
rv = NS_NewURI(getter_AddRefs(dl->mTarget), target);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-09-26 07:20:16 +00:00
|
|
|
nsString tempPath;
|
|
|
|
stmt->GetString(i++, tempPath);
|
|
|
|
if (!tempPath.IsEmpty()) {
|
|
|
|
rv = NS_NewLocalFile(tempPath, PR_TRUE, getter_AddRefs(dl->mTempFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2007-09-17 21:10:29 +00:00
|
|
|
stmt->GetString(i++, dl->mDisplayName);
|
2007-06-05 00:03:35 +00:00
|
|
|
|
2007-08-17 23:05:26 +00:00
|
|
|
nsCString referrer;
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->GetUTF8String(i++, referrer);
|
2007-08-17 23:05:26 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && !referrer.IsEmpty()) {
|
|
|
|
rv = NS_NewURI(getter_AddRefs(dl->mReferrer), referrer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->GetUTF8String(i++, dl->mEntityID);
|
2007-09-06 04:59:34 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-10-02 15:08:12 +00:00
|
|
|
PRInt64 currBytes = stmt->AsInt64(i++);
|
|
|
|
PRInt64 maxBytes = stmt->AsInt64(i++);
|
|
|
|
dl->SetProgressBytes(currBytes, maxBytes);
|
2007-10-02 00:16:12 +00:00
|
|
|
|
2007-10-24 22:19:04 +00:00
|
|
|
// Build mMIMEInfo only if the mimeType in DB is not empty
|
|
|
|
nsCAutoString mimeType;
|
|
|
|
rv = stmt->GetUTF8String(i++, mimeType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mimeType.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeService =
|
|
|
|
do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mimeService->GetFromTypeAndExtension(mimeType, EmptyCString(),
|
|
|
|
getter_AddRefs(dl->mMIMEInfo));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsHandlerInfoAction action = stmt->AsInt32(i++);
|
|
|
|
rv = dl->mMIMEInfo->SetPreferredAction(action);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCAutoString persistentDescriptor;
|
|
|
|
rv = stmt->GetUTF8String(i++, persistentDescriptor);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!persistentDescriptor.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsILocalHandlerApp> handler =
|
|
|
|
do_CreateInstance(NS_LOCALHANDLERAPP_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFile> localExecutable;
|
|
|
|
rv = NS_NewNativeLocalFile(EmptyCString(), PR_FALSE,
|
|
|
|
getter_AddRefs(localExecutable));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = localExecutable->SetPersistentDescriptor(persistentDescriptor);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = handler->SetExecutable(localExecutable);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = dl->mMIMEInfo->SetPreferredApplicationHandler(handler);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Compensate for the i++s skipped in the true block
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
|
2007-10-24 22:23:13 +00:00
|
|
|
dl->mAutoResume =
|
|
|
|
static_cast<enum nsDownload::AutoResume>(stmt->AsInt32(i++));
|
|
|
|
|
2007-06-05 00:03:35 +00:00
|
|
|
// Addrefing and returning
|
|
|
|
NS_ADDREF(*retVal = dl);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-09-17 21:02:39 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::AddToCurrentDownloads(nsDownload *aDl)
|
|
|
|
{
|
|
|
|
if (!mCurrentDownloads.AppendObject(aDl))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
aDl->mDownloadManager = this;
|
2007-09-17 21:02:39 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-08-22 21:55:57 +00:00
|
|
|
void
|
|
|
|
nsDownloadManager::SendEvent(nsDownload *aDownload, const char *aTopic)
|
|
|
|
{
|
|
|
|
(void)mObserverService->NotifyObservers(aDownload, aTopic, nsnull);
|
|
|
|
}
|
|
|
|
|
2007-10-02 15:03:16 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2007-06-05 00:03:35 +00:00
|
|
|
//// nsIDownloadManager
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloadManager::GetActiveDownloadCount(PRInt32 *aResult)
|
|
|
|
{
|
|
|
|
*aResult = mCurrentDownloads.Count();
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloadManager::GetActiveDownloads(nsISimpleEnumerator **aResult)
|
|
|
|
{
|
|
|
|
return NS_NewArrayEnumerator(aResult, mCurrentDownloads);
|
|
|
|
}
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-08-21 17:22:02 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloadManager::GetDefaultDownloadsDirectory(nsILocalFile **aResult)
|
|
|
|
{
|
2007-08-22 20:20:32 +00:00
|
|
|
nsCOMPtr<nsILocalFile> downloadDir;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIProperties> dirService =
|
|
|
|
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// OSX:
|
|
|
|
// Safari download folder or Desktop/Downloads
|
|
|
|
// Vista:
|
|
|
|
// Downloads
|
|
|
|
// XP/2K:
|
|
|
|
// Desktop/Downloads
|
|
|
|
// Linux:
|
|
|
|
// Home/Downloads
|
2007-08-21 17:22:02 +00:00
|
|
|
|
|
|
|
nsXPIDLString folderName;
|
|
|
|
mBundle->GetStringFromName(NS_LITERAL_STRING("downloadsFolder").get(),
|
|
|
|
getter_Copies(folderName));
|
|
|
|
|
2007-08-22 20:20:32 +00:00
|
|
|
#if defined (XP_MACOSX)
|
|
|
|
nsCOMPtr<nsILocalFile> desktopDir;
|
|
|
|
rv = dirService->Get(NS_OSX_DEFAULT_DOWNLOAD_DIR,
|
|
|
|
NS_GET_IID(nsILocalFile),
|
|
|
|
getter_AddRefs(downloadDir));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = dirService->Get(NS_OSX_USER_DESKTOP_DIR,
|
|
|
|
NS_GET_IID(nsILocalFile),
|
|
|
|
getter_AddRefs(desktopDir));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Check to see if we have the desktop or the Safari downloads folder
|
|
|
|
PRBool equals;
|
|
|
|
rv = downloadDir->Equals(desktopDir, &equals);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (equals) {
|
|
|
|
rv = downloadDir->Append(folderName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2008-02-20 22:42:38 +00:00
|
|
|
#elif defined(XP_WIN) && !defined(WINCE)
|
2007-08-22 20:20:32 +00:00
|
|
|
rv = dirService->Get(NS_WIN_DEFAULT_DOWNLOAD_DIR,
|
|
|
|
NS_GET_IID(nsILocalFile),
|
|
|
|
getter_AddRefs(downloadDir));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Check the os version
|
|
|
|
#define NS_SYSTEMINFO_CONTRACTID "@mozilla.org/system-info;1"
|
|
|
|
nsCOMPtr<nsIPropertyBag2> infoService =
|
|
|
|
do_GetService(NS_SYSTEMINFO_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRInt32 version;
|
|
|
|
NS_NAMED_LITERAL_STRING(osVersion, "version");
|
|
|
|
rv = infoService->GetPropertyAsInt32(osVersion, &version);
|
|
|
|
if (version < 6) { // XP/2K
|
|
|
|
rv = downloadDir->Append(folderName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
rv = dirService->Get(NS_OS_HOME_DIR,
|
|
|
|
NS_GET_IID(nsILocalFile),
|
|
|
|
getter_AddRefs(downloadDir));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = downloadDir->Append(folderName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-08-21 17:22:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_ADDREF(*aResult = downloadDir);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-08-22 20:20:32 +00:00
|
|
|
#define NS_BRANCH_DOWNLOAD "browser.download."
|
|
|
|
#define NS_PREF_FOLDERLIST "folderList"
|
|
|
|
#define NS_PREF_DIR "dir"
|
2007-08-21 17:22:02 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloadManager::GetUserDownloadsDirectory(nsILocalFile **aResult)
|
|
|
|
{
|
2007-08-22 20:20:32 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIProperties> dirService =
|
|
|
|
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefService> prefService =
|
|
|
|
do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch;
|
2007-09-17 20:49:24 +00:00
|
|
|
rv = prefService->GetBranch(NS_BRANCH_DOWNLOAD,
|
2007-08-22 20:20:32 +00:00
|
|
|
getter_AddRefs(prefBranch));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRInt32 val;
|
|
|
|
rv = prefBranch->GetIntPref(NS_PREF_FOLDERLIST,
|
|
|
|
&val);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRBool bRes = PR_FALSE;
|
|
|
|
|
|
|
|
switch(val) {
|
|
|
|
case 0: // Desktop
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILocalFile> downloadDir;
|
|
|
|
nsCOMPtr<nsIProperties> dirService =
|
|
|
|
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = dirService->Get(NS_OS_DESKTOP_DIR,
|
|
|
|
NS_GET_IID(nsILocalFile),
|
|
|
|
getter_AddRefs(downloadDir));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ADDREF(*aResult = downloadDir);
|
2007-08-21 17:22:02 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-08-22 20:20:32 +00:00
|
|
|
break;
|
|
|
|
case 1: // Downloads
|
|
|
|
{
|
|
|
|
rv = GetDefaultDownloadsDirectory(aResult); // refup
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
(*aResult)->Exists(&bRes);
|
|
|
|
if (!bRes) {
|
2007-10-11 21:45:12 +00:00
|
|
|
rv = (*aResult)->Create(nsIFile::DIRECTORY_TYPE, 0755);
|
2007-08-22 20:20:32 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // Custom
|
|
|
|
{
|
2007-08-28 18:59:49 +00:00
|
|
|
nsCOMPtr<nsILocalFile> customDirectory;
|
2007-09-17 20:49:24 +00:00
|
|
|
prefBranch->GetComplexValue(NS_PREF_DIR,
|
2007-08-28 18:59:49 +00:00
|
|
|
NS_GET_IID(nsILocalFile),
|
2007-08-22 20:20:32 +00:00
|
|
|
getter_AddRefs(customDirectory));
|
|
|
|
if (customDirectory) {
|
2007-08-28 18:59:49 +00:00
|
|
|
customDirectory->Exists(&bRes);
|
2007-08-22 20:20:32 +00:00
|
|
|
if (bRes) {
|
2007-08-28 18:59:49 +00:00
|
|
|
NS_ADDREF(*aResult = customDirectory);
|
2007-08-21 17:22:02 +00:00
|
|
|
return NS_OK;
|
2007-08-22 20:20:32 +00:00
|
|
|
}
|
2007-10-11 21:45:12 +00:00
|
|
|
rv = customDirectory->Create(nsIFile::DIRECTORY_TYPE, 0755);
|
2007-08-22 20:20:32 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-08-28 18:59:49 +00:00
|
|
|
NS_ADDREF(*aResult = customDirectory);
|
|
|
|
return NS_OK;
|
2007-08-22 20:20:32 +00:00
|
|
|
}
|
|
|
|
rv = GetDefaultDownloadsDirectory(aResult); // refup
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
(*aResult)->Exists(&bRes);
|
|
|
|
if (!bRes) {
|
2007-10-11 21:45:12 +00:00
|
|
|
rv = (*aResult)->Create(nsIFile::DIRECTORY_TYPE, 0755);
|
2007-08-22 20:20:32 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Update dir pref
|
|
|
|
prefBranch->SetComplexValue(NS_PREF_DIR,
|
|
|
|
NS_GET_IID(nsILocalFile),
|
|
|
|
*aResult);
|
|
|
|
}
|
2007-08-21 17:22:02 +00:00
|
|
|
return NS_OK;
|
2007-08-22 20:20:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-08-21 17:22:02 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownloadManager::AddDownload(DownloadType aDownloadType,
|
|
|
|
nsIURI *aSource,
|
|
|
|
nsIURI *aTarget,
|
2005-04-24 21:16:28 +00:00
|
|
|
const nsAString& aDisplayName,
|
2003-11-27 00:54:33 +00:00
|
|
|
nsIMIMEInfo *aMIMEInfo,
|
2005-04-24 21:16:28 +00:00
|
|
|
PRTime aStartTime,
|
2007-09-17 20:49:24 +00:00
|
|
|
nsILocalFile *aTempFile,
|
|
|
|
nsICancelable *aCancelable,
|
|
|
|
nsIDownload **aDownload)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aSource);
|
|
|
|
NS_ENSURE_ARG_POINTER(aTarget);
|
|
|
|
NS_ENSURE_ARG_POINTER(aDownload);
|
|
|
|
|
2004-04-17 00:58:23 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// target must be on the local filesystem
|
|
|
|
nsCOMPtr<nsIFileURL> targetFileURL = do_QueryInterface(aTarget, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> targetFile;
|
|
|
|
rv = targetFileURL->GetFile(getter_AddRefs(targetFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsRefPtr<nsDownload> dl = new nsDownload();
|
|
|
|
if (!dl)
|
2003-11-27 00:54:33 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// give our new nsIDownload some info so it's ready to go off into the world
|
2007-05-22 00:03:33 +00:00
|
|
|
dl->mTarget = aTarget;
|
|
|
|
dl->mSource = aSource;
|
|
|
|
dl->mTempFile = aTempFile;
|
|
|
|
|
|
|
|
dl->mDisplayName = aDisplayName;
|
|
|
|
if (dl->mDisplayName.IsEmpty())
|
|
|
|
targetFile->GetLeafName(dl->mDisplayName);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
dl->mMIMEInfo = aMIMEInfo;
|
2007-10-01 22:39:06 +00:00
|
|
|
dl->SetStartTime(aStartTime == 0 ? PR_Now() : aStartTime);
|
2003-12-10 21:59:41 +00:00
|
|
|
|
2007-06-17 01:12:27 +00:00
|
|
|
// Creates a cycle that will be broken when the download finishes
|
2007-05-22 00:03:33 +00:00
|
|
|
dl->mCancelable = aCancelable;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// Adding to the DB
|
|
|
|
nsCAutoString source, target;
|
|
|
|
aSource->GetSpec(source);
|
|
|
|
aTarget->GetSpec(target);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-09-26 07:20:16 +00:00
|
|
|
// Track the temp file for exthandler downloads
|
|
|
|
nsAutoString tempPath;
|
|
|
|
if (aTempFile)
|
|
|
|
aTempFile->GetPath(tempPath);
|
|
|
|
|
2007-10-24 22:19:04 +00:00
|
|
|
// Break down MIMEInfo but don't panic if we can't get all the pieces - we
|
|
|
|
// can still download the file
|
|
|
|
nsCAutoString persistentDescriptor, mimeType;
|
|
|
|
nsHandlerInfoAction action = nsIMIMEInfo::saveToDisk;
|
|
|
|
if (aMIMEInfo) {
|
|
|
|
(void)aMIMEInfo->GetType(mimeType);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHandlerApp> handlerApp;
|
|
|
|
(void)aMIMEInfo->GetPreferredApplicationHandler(getter_AddRefs(handlerApp));
|
|
|
|
nsCOMPtr<nsILocalHandlerApp> locHandlerApp = do_QueryInterface(handlerApp);
|
|
|
|
|
|
|
|
if (locHandlerApp) {
|
|
|
|
nsCOMPtr<nsIFile> executable;
|
|
|
|
(void)locHandlerApp->GetExecutable(getter_AddRefs(executable));
|
|
|
|
nsCOMPtr<nsILocalFile> locExecutable = do_QueryInterface(executable);
|
|
|
|
|
|
|
|
if (locExecutable)
|
|
|
|
(void)locExecutable->GetPersistentDescriptor(persistentDescriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)aMIMEInfo->GetPreferredAction(&action);
|
|
|
|
}
|
|
|
|
|
2007-09-26 07:20:16 +00:00
|
|
|
PRInt64 id = AddDownloadToDB(dl->mDisplayName, source, target, tempPath,
|
2007-10-01 22:39:06 +00:00
|
|
|
dl->mStartTime, dl->mLastUpdate,
|
2007-10-24 22:19:04 +00:00
|
|
|
nsIDownloadManager::DOWNLOAD_NOTSTARTED,
|
|
|
|
mimeType, persistentDescriptor, action);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_TRUE(id, NS_ERROR_FAILURE);
|
|
|
|
dl->mID = id;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-06-05 00:03:35 +00:00
|
|
|
rv = AddToCurrentDownloads(dl);
|
2007-08-15 17:56:50 +00:00
|
|
|
(void)dl->SetState(nsIDownloadManager::DOWNLOAD_QUEUED);
|
2007-06-05 00:03:35 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-12-10 21:59:41 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ADDREF(*aDownload = dl);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
return NS_OK;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::GetDownload(PRUint32 aID, nsIDownload **aDownloadItem)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownload *itm = FindDownload(aID);
|
|
|
|
|
2007-06-12 23:39:15 +00:00
|
|
|
nsRefPtr<nsDownload> dl;
|
2007-05-22 00:03:33 +00:00
|
|
|
if (!itm) {
|
2007-06-12 23:39:15 +00:00
|
|
|
nsresult rv = GetDownloadFromDB(aID, getter_AddRefs(dl));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-22 00:03:33 +00:00
|
|
|
|
2007-06-12 23:39:15 +00:00
|
|
|
itm = dl.get();
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ADDREF(*aDownloadItem = itm);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownload *
|
|
|
|
nsDownloadManager::FindDownload(PRUint32 aID)
|
|
|
|
{
|
|
|
|
// we shouldn't ever have many downloads, so we can loop over them
|
|
|
|
for (PRInt32 i = mCurrentDownloads.Count() - 1; i >= 0; --i) {
|
|
|
|
nsDownload *dl = mCurrentDownloads[i];
|
|
|
|
|
|
|
|
if (dl->mID == aID)
|
|
|
|
return dl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
NS_IMETHODIMP
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::CancelDownload(PRUint32 aID)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
// We AddRef here so we don't lose access to member variables when we remove
|
|
|
|
nsRefPtr<nsDownload> dl = FindDownload(aID);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// if it's null, someone passed us a bad id.
|
|
|
|
if (!dl)
|
2003-11-27 00:54:33 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Don't cancel if download is already finished
|
2007-09-17 21:02:39 +00:00
|
|
|
if (dl->IsFinished())
|
2003-11-27 00:54:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
// if the download is fake-paused, we have to resume it so we can cancel it
|
|
|
|
if (dl->IsPaused() && !dl->IsResumable())
|
|
|
|
(void)dl->Resume();
|
2007-08-12 22:15:15 +00:00
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
// Have the download cancel its connection
|
|
|
|
(void)dl->Cancel();
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-10-24 22:12:54 +00:00
|
|
|
// Dump the temp file because we know we don't need the file anymore. The
|
|
|
|
// underlying transfer creating the file doesn't delete the file because it
|
|
|
|
// can't distinguish between a pause that cancels the transfer or a real
|
|
|
|
// cancel.
|
2007-05-22 00:03:33 +00:00
|
|
|
if (dl->mTempFile) {
|
2005-07-08 19:11:56 +00:00
|
|
|
PRBool exists;
|
2007-05-22 00:03:33 +00:00
|
|
|
dl->mTempFile->Exists(&exists);
|
2005-07-08 19:11:56 +00:00
|
|
|
if (exists)
|
2007-05-22 00:03:33 +00:00
|
|
|
dl->mTempFile->Remove(PR_FALSE);
|
2005-07-08 19:11:56 +00:00
|
|
|
}
|
2005-07-07 17:45:47 +00:00
|
|
|
|
2007-08-22 21:55:57 +00:00
|
|
|
nsresult rv = dl->SetState(nsIDownloadManager::DOWNLOAD_CANCELED);
|
2007-05-23 23:12:03 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-23 23:12:03 +00:00
|
|
|
return NS_OK;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-06-05 00:03:35 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloadManager::RetryDownload(PRUint32 aID)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsDownload> dl;
|
|
|
|
nsresult rv = GetDownloadFromDB(aID, getter_AddRefs(dl));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// if our download is not canceled or failed, we should fail
|
|
|
|
if (dl->mDownloadState != nsIDownloadManager::DOWNLOAD_FAILED &&
|
2007-08-21 16:37:39 +00:00
|
|
|
dl->mDownloadState != nsIDownloadManager::DOWNLOAD_BLOCKED &&
|
2007-12-01 06:49:07 +00:00
|
|
|
dl->mDownloadState != nsIDownloadManager::DOWNLOAD_DIRTY &&
|
2007-06-05 00:03:35 +00:00
|
|
|
dl->mDownloadState != nsIDownloadManager::DOWNLOAD_CANCELED)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-10-02 00:16:12 +00:00
|
|
|
// reset time and download progress
|
2007-06-05 00:03:35 +00:00
|
|
|
dl->SetStartTime(PR_Now());
|
2007-10-02 00:16:12 +00:00
|
|
|
dl->SetProgressBytes(0, -1);
|
2007-06-05 00:03:35 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIWebBrowserPersist> wbp =
|
|
|
|
do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = wbp->SetPersistFlags(nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES |
|
|
|
|
nsIWebBrowserPersist::PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION);
|
2007-09-17 22:41:15 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-05 00:03:35 +00:00
|
|
|
|
|
|
|
rv = AddToCurrentDownloads(dl);
|
2007-09-17 22:41:15 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = dl->SetState(nsIDownloadManager::DOWNLOAD_QUEUED);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Creates a cycle that will be broken when the download finishes
|
|
|
|
dl->mCancelable = wbp;
|
|
|
|
(void)wbp->SetProgressListener(dl);
|
2007-06-05 00:03:35 +00:00
|
|
|
|
2007-08-15 17:56:50 +00:00
|
|
|
rv = wbp->SaveURI(dl->mSource, nsnull, nsnull, nsnull, nsnull, dl->mTarget);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
dl->mCancelable = nsnull;
|
|
|
|
(void)wbp->SetProgressListener(nsnull);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-06-05 00:03:35 +00:00
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
NS_IMETHODIMP
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::RemoveDownload(PRUint32 aID)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownload *dl = FindDownload(aID);
|
|
|
|
NS_ASSERTION(!dl, "Can't call RemoveDownload on a download in progress!");
|
|
|
|
if (dl)
|
2003-11-27 00:54:33 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"DELETE FROM moz_downloads "
|
|
|
|
"WHERE id = ?1"), getter_AddRefs(stmt));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-10-07 17:21:57 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
rv = stmt->BindInt64Parameter(0, aID); // unsigned; 64-bit to prevent overflow
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-10-07 17:21:57 +00:00
|
|
|
|
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsPRUint32> id =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_PRUINT32_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = id->SetData(aID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Notify the UI with the topic and download id
|
|
|
|
return mObserverService->NotifyObservers(id,
|
|
|
|
"download-manager-remove-download",
|
|
|
|
nsnull);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloadManager::CleanUp()
|
|
|
|
{
|
2003-12-09 05:55:09 +00:00
|
|
|
DownloadState states[] = { nsIDownloadManager::DOWNLOAD_FINISHED,
|
|
|
|
nsIDownloadManager::DOWNLOAD_FAILED,
|
2007-08-21 16:37:39 +00:00
|
|
|
nsIDownloadManager::DOWNLOAD_CANCELED,
|
2007-12-01 06:49:07 +00:00
|
|
|
nsIDownloadManager::DOWNLOAD_BLOCKED,
|
|
|
|
nsIDownloadManager::DOWNLOAD_DIRTY };
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"DELETE FROM moz_downloads "
|
|
|
|
"WHERE state = ?1 "
|
2007-09-17 20:49:24 +00:00
|
|
|
"OR state = ?2 "
|
|
|
|
"OR state = ?3 "
|
2007-12-01 06:49:07 +00:00
|
|
|
"OR state = ?4 "
|
|
|
|
"OR state = ?5"), getter_AddRefs(stmt));
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-12-01 06:49:07 +00:00
|
|
|
for (PRUint32 i = 0; i < NS_ARRAY_LENGTH(states); ++i) {
|
2007-05-22 00:03:33 +00:00
|
|
|
rv = stmt->BindInt32Parameter(i, states[i]);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-10-01 23:24:08 +00:00
|
|
|
rv = stmt->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-10-07 17:21:57 +00:00
|
|
|
// Notify the UI with the topic and null subject to indicate "remove all"
|
2007-10-01 23:24:08 +00:00
|
|
|
return mObserverService->NotifyObservers(nsnull,
|
2007-10-07 17:21:57 +00:00
|
|
|
"download-manager-remove-download",
|
2007-10-01 23:24:08 +00:00
|
|
|
nsnull);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::GetCanCleanUp(PRBool *aResult)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
|
2003-12-09 05:55:09 +00:00
|
|
|
DownloadState states[] = { nsIDownloadManager::DOWNLOAD_FINISHED,
|
|
|
|
nsIDownloadManager::DOWNLOAD_FAILED,
|
2007-08-21 16:37:39 +00:00
|
|
|
nsIDownloadManager::DOWNLOAD_CANCELED,
|
2007-12-01 06:49:07 +00:00
|
|
|
nsIDownloadManager::DOWNLOAD_BLOCKED,
|
|
|
|
nsIDownloadManager::DOWNLOAD_DIRTY };
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"SELECT COUNT(*) "
|
|
|
|
"FROM moz_downloads "
|
|
|
|
"WHERE state = ?1 "
|
2007-09-17 20:49:24 +00:00
|
|
|
"OR state = ?2 "
|
|
|
|
"OR state = ?3 "
|
2007-12-01 06:49:07 +00:00
|
|
|
"OR state = ?4 "
|
|
|
|
"OR state = ?5"), getter_AddRefs(stmt));
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-12-01 06:49:07 +00:00
|
|
|
for (PRUint32 i = 0; i < NS_ARRAY_LENGTH(states); ++i) {
|
2007-05-22 00:03:33 +00:00
|
|
|
rv = stmt->BindInt32Parameter(i, states[i]);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
PRBool moreResults; // We don't really care...
|
|
|
|
rv = stmt->ExecuteStep(&moreResults);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
|
|
|
PRInt32 count;
|
2007-05-22 00:03:33 +00:00
|
|
|
rv = stmt->GetInt32(0, &count);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
if (count > 0)
|
|
|
|
*aResult = PR_TRUE;
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
return rv;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::PauseDownload(PRUint32 aID)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-09-17 22:41:15 +00:00
|
|
|
nsDownload *dl = FindDownload(aID);
|
|
|
|
if (!dl)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return dl->Pause();
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::ResumeDownload(PRUint32 aID)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownload *dl = FindDownload(aID);
|
|
|
|
if (!dl)
|
2003-11-27 00:54:33 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
return dl->Resume();
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloadManager::GetDBConnection(mozIStorageConnection **aDBConn)
|
|
|
|
{
|
|
|
|
NS_ADDREF(*aDBConn = mDBConn);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-25 23:47:53 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloadManager::AddListener(nsIDownloadProgressListener *aListener)
|
|
|
|
{
|
|
|
|
mListeners.AppendObject(aListener);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-25 23:47:53 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloadManager::RemoveListener(nsIDownloadProgressListener *aListener)
|
|
|
|
{
|
|
|
|
mListeners.RemoveObject(aListener);
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-25 23:47:53 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDownloadManager::NotifyListenersOnDownloadStateChange(PRInt16 aOldState,
|
|
|
|
nsIDownload *aDownload)
|
|
|
|
{
|
|
|
|
for (PRInt32 i = mListeners.Count() - 1; i >= 0; --i)
|
|
|
|
mListeners[i]->OnDownloadStateChange(aOldState, aDownload);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDownloadManager::NotifyListenersOnProgressChange(nsIWebProgress *aProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRInt64 aCurSelfProgress,
|
|
|
|
PRInt64 aMaxSelfProgress,
|
|
|
|
PRInt64 aCurTotalProgress,
|
|
|
|
PRInt64 aMaxTotalProgress,
|
|
|
|
nsIDownload *aDownload)
|
|
|
|
{
|
|
|
|
for (PRInt32 i = mListeners.Count() - 1; i >= 0; --i)
|
|
|
|
mListeners[i]->OnProgressChange(aProgress, aRequest, aCurSelfProgress,
|
|
|
|
aMaxSelfProgress, aCurTotalProgress,
|
|
|
|
aMaxTotalProgress, aDownload);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDownloadManager::NotifyListenersOnStateChange(nsIWebProgress *aProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRUint32 aStateFlags,
|
|
|
|
nsresult aStatus,
|
|
|
|
nsIDownload *aDownload)
|
|
|
|
{
|
|
|
|
for (PRInt32 i = mListeners.Count() - 1; i >= 0; --i)
|
|
|
|
mListeners[i]->OnStateChange(aProgress, aRequest, aStateFlags, aStatus,
|
|
|
|
aDownload);
|
|
|
|
}
|
|
|
|
|
2007-10-02 15:03:16 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIObserver
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
2007-09-17 20:49:24 +00:00
|
|
|
const PRUnichar *aData)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2008-01-22 03:43:02 +00:00
|
|
|
PRInt32 currDownloadCount = mCurrentDownloads.Count();
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2008-01-29 04:58:31 +00:00
|
|
|
// If we don't need to cancel all the downloads on quit, only count the ones
|
|
|
|
// that aren't resumable.
|
|
|
|
if (GetQuitBehavior() != QUIT_AND_CANCEL)
|
|
|
|
for (PRInt32 i = currDownloadCount - 1; i >= 0; --i)
|
|
|
|
if (mCurrentDownloads[i]->IsResumable())
|
|
|
|
currDownloadCount--;
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult rv;
|
|
|
|
if (strcmp(aTopic, "oncancel") == 0) {
|
|
|
|
nsCOMPtr<nsIDownload> dl = do_QueryInterface(aSubject, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRUint32 id;
|
|
|
|
dl->GetId(&id);
|
|
|
|
nsDownload *dl2 = FindDownload(id);
|
2007-08-17 23:05:26 +00:00
|
|
|
if (dl2)
|
2007-09-17 20:49:24 +00:00
|
|
|
return CancelDownload(id);
|
2007-05-22 00:03:33 +00:00
|
|
|
} else if (strcmp(aTopic, "quit-application") == 0) {
|
2008-01-26 21:11:31 +00:00
|
|
|
// Try to pause all downloads and, if appropriate, mark them as auto-resume
|
|
|
|
// unless user has specified that downloads should be canceled
|
|
|
|
enum QuitBehavior behavior = GetQuitBehavior();
|
|
|
|
if (behavior != QUIT_AND_CANCEL)
|
|
|
|
(void)PauseAllDownloads(PRBool(behavior != QUIT_AND_PAUSE));
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-10-24 22:31:49 +00:00
|
|
|
// Remove downloads to break cycles and cancel downloads
|
|
|
|
(void)RemoveAllDownloads();
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2008-01-26 21:11:31 +00:00
|
|
|
// Now that active downloads have been canceled, remove all completed or
|
|
|
|
// aborted downloads if the user's retention policy specifies it.
|
2007-05-22 00:03:33 +00:00
|
|
|
if (GetRetentionBehavior() == 1)
|
|
|
|
CleanUp();
|
|
|
|
} else if (strcmp(aTopic, "quit-application-requested") == 0 &&
|
|
|
|
currDownloadCount) {
|
|
|
|
nsCOMPtr<nsISupportsPRBool> cancelDownloads =
|
|
|
|
do_QueryInterface(aSubject, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
#ifndef XP_MACOSX
|
2003-12-02 00:59:58 +00:00
|
|
|
ConfirmCancelDownloads(currDownloadCount, cancelDownloads,
|
|
|
|
NS_LITERAL_STRING("quitCancelDownloadsAlertTitle").get(),
|
|
|
|
NS_LITERAL_STRING("quitCancelDownloadsAlertMsgMultiple").get(),
|
2003-12-03 06:26:07 +00:00
|
|
|
NS_LITERAL_STRING("quitCancelDownloadsAlertMsg").get(),
|
|
|
|
NS_LITERAL_STRING("dontQuitButtonWin").get());
|
2003-11-27 00:54:33 +00:00
|
|
|
#else
|
2003-12-02 00:59:58 +00:00
|
|
|
ConfirmCancelDownloads(currDownloadCount, cancelDownloads,
|
|
|
|
NS_LITERAL_STRING("quitCancelDownloadsAlertTitle").get(),
|
|
|
|
NS_LITERAL_STRING("quitCancelDownloadsAlertMsgMacMultiple").get(),
|
2003-12-03 06:26:07 +00:00
|
|
|
NS_LITERAL_STRING("quitCancelDownloadsAlertMsgMac").get(),
|
|
|
|
NS_LITERAL_STRING("dontQuitButtonMac").get());
|
2003-11-27 00:54:33 +00:00
|
|
|
#endif
|
2007-05-22 00:03:33 +00:00
|
|
|
} else if (strcmp(aTopic, "offline-requested") == 0 && currDownloadCount) {
|
|
|
|
nsCOMPtr<nsISupportsPRBool> cancelDownloads =
|
|
|
|
do_QueryInterface(aSubject, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-12-02 00:59:58 +00:00
|
|
|
ConfirmCancelDownloads(currDownloadCount, cancelDownloads,
|
|
|
|
NS_LITERAL_STRING("offlineCancelDownloadsAlertTitle").get(),
|
|
|
|
NS_LITERAL_STRING("offlineCancelDownloadsAlertMsgMultiple").get(),
|
2003-12-03 06:26:07 +00:00
|
|
|
NS_LITERAL_STRING("offlineCancelDownloadsAlertMsg").get(),
|
|
|
|
NS_LITERAL_STRING("dontGoOfflineButton").get());
|
2007-05-22 00:03:33 +00:00
|
|
|
} else if (strcmp(aTopic, "alertclickcallback") == 0) {
|
2007-08-28 00:10:25 +00:00
|
|
|
nsCOMPtr<nsIDownloadManagerUI> dmui =
|
|
|
|
do_GetService("@mozilla.org/download-manager-ui;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return dmui->Show(nsnull, 0);
|
2004-12-12 16:46:33 +00:00
|
|
|
}
|
2007-05-22 00:03:33 +00:00
|
|
|
|
2003-12-02 00:59:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2003-12-02 00:59:58 +00:00
|
|
|
void
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::ConfirmCancelDownloads(PRInt32 aCount,
|
2007-09-17 20:49:24 +00:00
|
|
|
nsISupportsPRBool *aCancelDownloads,
|
|
|
|
const PRUnichar *aTitle,
|
|
|
|
const PRUnichar *aCancelMessageMultiple,
|
|
|
|
const PRUnichar *aCancelMessageSingle,
|
|
|
|
const PRUnichar *aDontCancelButton)
|
2003-12-02 00:59:58 +00:00
|
|
|
{
|
2008-01-29 21:16:02 +00:00
|
|
|
// If user has already dismissed quit request, then do nothing
|
|
|
|
PRBool quitRequestCancelled = PR_FALSE;
|
|
|
|
aCancelDownloads->GetData(&quitRequestCancelled);
|
|
|
|
if (quitRequestCancelled)
|
|
|
|
return;
|
|
|
|
|
2003-12-03 06:26:07 +00:00
|
|
|
nsXPIDLString title, message, quitButton, dontQuitButton;
|
2007-09-17 20:49:24 +00:00
|
|
|
|
|
|
|
mBundle->GetStringFromName(aTitle, getter_Copies(title));
|
2007-05-22 00:03:33 +00:00
|
|
|
|
|
|
|
nsAutoString countString;
|
|
|
|
countString.AppendInt(aCount);
|
2007-09-17 20:49:24 +00:00
|
|
|
const PRUnichar *strings[1] = { countString.get() };
|
2007-05-22 00:03:33 +00:00
|
|
|
if (aCount > 1) {
|
|
|
|
mBundle->FormatStringFromName(aCancelMessageMultiple, strings, 1,
|
|
|
|
getter_Copies(message));
|
|
|
|
mBundle->FormatStringFromName(NS_LITERAL_STRING("cancelDownloadsOKTextMultiple").get(),
|
|
|
|
strings, 1, getter_Copies(quitButton));
|
|
|
|
} else {
|
|
|
|
mBundle->GetStringFromName(aCancelMessageSingle, getter_Copies(message));
|
|
|
|
mBundle->GetStringFromName(NS_LITERAL_STRING("cancelDownloadsOKText").get(),
|
|
|
|
getter_Copies(quitButton));
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
2003-12-02 00:59:58 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
mBundle->GetStringFromName(aDontCancelButton, getter_Copies(dontQuitButton));
|
|
|
|
|
2003-12-02 00:59:58 +00:00
|
|
|
// Get Download Manager window, to be parent of alert.
|
|
|
|
nsCOMPtr<nsIWindowMediator> wm = do_GetService(NS_WINDOWMEDIATOR_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIDOMWindowInternal> dmWindow;
|
2007-05-22 00:03:33 +00:00
|
|
|
if (wm) {
|
|
|
|
wm->GetMostRecentWindow(NS_LITERAL_STRING("Download:Manager").get(),
|
|
|
|
getter_AddRefs(dmWindow));
|
|
|
|
}
|
2003-12-02 00:59:58 +00:00
|
|
|
|
|
|
|
// Show alert.
|
2005-03-07 16:07:11 +00:00
|
|
|
nsCOMPtr<nsIPromptService> prompter(do_GetService(NS_PROMPTSERVICE_CONTRACTID));
|
2003-12-02 00:59:58 +00:00
|
|
|
if (prompter) {
|
2003-12-03 06:26:07 +00:00
|
|
|
PRInt32 flags = (nsIPromptService::BUTTON_TITLE_IS_STRING * nsIPromptService::BUTTON_POS_0) + (nsIPromptService::BUTTON_TITLE_IS_STRING * nsIPromptService::BUTTON_POS_1);
|
2003-12-02 00:59:58 +00:00
|
|
|
PRBool nothing = PR_FALSE;
|
|
|
|
PRInt32 button;
|
2003-12-03 06:26:07 +00:00
|
|
|
prompter->ConfirmEx(dmWindow, title, message, flags, quitButton.get(), dontQuitButton.get(), nsnull, nsnull, ¬hing, &button);
|
2003-12-02 00:59:58 +00:00
|
|
|
|
|
|
|
aCancelDownloads->SetData(button == 1);
|
|
|
|
}
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-10-02 15:03:16 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsDownload
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2005-04-05 23:24:23 +00:00
|
|
|
NS_IMPL_ISUPPORTS4(nsDownload, nsIDownload, nsITransfer, nsIWebProgressListener,
|
|
|
|
nsIWebProgressListener2)
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownload::nsDownload() : mDownloadState(nsIDownloadManager::DOWNLOAD_NOTSTARTED),
|
|
|
|
mID(0),
|
|
|
|
mPercentComplete(0),
|
2007-06-18 20:11:29 +00:00
|
|
|
mCurrBytes(0),
|
2007-10-02 00:16:12 +00:00
|
|
|
mMaxBytes(-1),
|
2007-06-18 20:11:29 +00:00
|
|
|
mStartTime(0),
|
2007-05-22 00:03:33 +00:00
|
|
|
mLastUpdate(PR_Now() - (PRUint32)gUpdateInterval),
|
2007-10-01 23:57:48 +00:00
|
|
|
mResumedAt(-1),
|
2007-10-24 22:23:13 +00:00
|
|
|
mSpeed(0),
|
2008-01-27 22:38:37 +00:00
|
|
|
mHasMultipleFiles(PR_FALSE),
|
2007-10-24 22:23:13 +00:00
|
|
|
mAutoResume(DONT_RESUME)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDownload::~nsDownload()
|
2007-09-17 20:49:24 +00:00
|
|
|
{
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-05-23 23:12:03 +00:00
|
|
|
nsresult
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownload::SetState(DownloadState aState)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-24 21:42:22 +00:00
|
|
|
NS_ASSERTION(mDownloadState != aState,
|
|
|
|
"Trying to set the download state to what it already is set to!");
|
2007-05-22 00:03:33 +00:00
|
|
|
|
|
|
|
PRInt16 oldState = mDownloadState;
|
2003-11-27 00:54:33 +00:00
|
|
|
mDownloadState = aState;
|
2007-05-22 00:03:33 +00:00
|
|
|
|
2007-08-22 21:55:57 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> pref = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
|
|
|
|
// We don't want to lose access to our member variables
|
|
|
|
nsRefPtr<nsDownload> kungFuDeathGrip = this;
|
|
|
|
|
|
|
|
// When the state changed listener is dispatched, queries to the database and
|
|
|
|
// the download manager api should reflect what the nsIDownload object would
|
|
|
|
// return. So, if a download is done (finished, canceled, etc.), it should
|
|
|
|
// first be removed from the current downloads. We will also have to update
|
|
|
|
// the database *before* notifying listeners. At this point, you can safely
|
|
|
|
// dispatch to the observers as well.
|
|
|
|
switch (aState) {
|
|
|
|
case nsIDownloadManager::DOWNLOAD_BLOCKED:
|
2007-12-01 06:49:07 +00:00
|
|
|
case nsIDownloadManager::DOWNLOAD_DIRTY:
|
2007-08-22 21:55:57 +00:00
|
|
|
case nsIDownloadManager::DOWNLOAD_CANCELED:
|
2007-08-25 00:22:06 +00:00
|
|
|
case nsIDownloadManager::DOWNLOAD_FAILED:
|
2007-10-02 15:03:16 +00:00
|
|
|
// Transfers are finished, so break the reference cycle
|
|
|
|
Finalize();
|
2007-08-22 21:55:57 +00:00
|
|
|
break;
|
2008-02-20 22:42:38 +00:00
|
|
|
#if defined(XP_WIN) && !defined(__MINGW32__) && !defined(WINCE)
|
2007-08-22 21:55:57 +00:00
|
|
|
case nsIDownloadManager::DOWNLOAD_SCANNING:
|
|
|
|
{
|
|
|
|
nsresult rv = mDownloadManager->mScanner ? mDownloadManager->mScanner->ScanDownload(this) : NS_ERROR_NOT_INITIALIZED;
|
|
|
|
// If we failed, then fall through to 'download finished'
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
break;
|
|
|
|
mDownloadState = aState = nsIDownloadManager::DOWNLOAD_FINISHED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
case nsIDownloadManager::DOWNLOAD_FINISHED:
|
|
|
|
{
|
2007-10-02 15:03:16 +00:00
|
|
|
// Do what exthandler would have done if necessary
|
|
|
|
(void)ExecuteDesiredAction();
|
|
|
|
|
|
|
|
// Now that we're done with handling the download, clean it up
|
|
|
|
Finalize();
|
2007-08-22 21:55:57 +00:00
|
|
|
|
2007-09-17 20:49:24 +00:00
|
|
|
// Master pref to control this function.
|
2007-08-22 21:55:57 +00:00
|
|
|
PRBool showTaskbarAlert = PR_TRUE;
|
|
|
|
if (pref)
|
|
|
|
pref->GetBoolPref(PREF_BDM_SHOWALERTONCOMPLETE, &showTaskbarAlert);
|
|
|
|
|
|
|
|
if (showTaskbarAlert) {
|
|
|
|
PRInt32 alertInterval = 2000;
|
|
|
|
if (pref)
|
|
|
|
pref->GetIntPref(PREF_BDM_SHOWALERTINTERVAL, &alertInterval);
|
|
|
|
|
|
|
|
PRInt64 alertIntervalUSec = alertInterval * PR_USEC_PER_MSEC;
|
|
|
|
PRInt64 goat = PR_Now() - mStartTime;
|
|
|
|
showTaskbarAlert = goat > alertIntervalUSec;
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-08-22 21:55:57 +00:00
|
|
|
PRInt32 size = mDownloadManager->mCurrentDownloads.Count();
|
|
|
|
if (showTaskbarAlert && size == 0) {
|
|
|
|
nsCOMPtr<nsIAlertsService> alerts =
|
|
|
|
do_GetService("@mozilla.org/alerts-service;1");
|
|
|
|
if (alerts) {
|
|
|
|
nsXPIDLString title, message;
|
|
|
|
|
|
|
|
mDownloadManager->mBundle->GetStringFromName(
|
|
|
|
NS_LITERAL_STRING("downloadsCompleteTitle").get(),
|
|
|
|
getter_Copies(title));
|
|
|
|
mDownloadManager->mBundle->GetStringFromName(
|
|
|
|
NS_LITERAL_STRING("downloadsCompleteMsg").get(),
|
|
|
|
getter_Copies(message));
|
|
|
|
|
|
|
|
PRBool removeWhenDone =
|
|
|
|
mDownloadManager->GetRetentionBehavior() == 0;
|
|
|
|
|
|
|
|
// If downloads are automatically removed per the user's
|
|
|
|
// retention policy, there's no reason to make the text clickable
|
|
|
|
// because if it is, they'll click open the download manager and
|
2007-09-17 20:49:24 +00:00
|
|
|
// the items they downloaded will have been removed.
|
2007-08-22 21:55:57 +00:00
|
|
|
alerts->ShowAlertNotification(
|
|
|
|
NS_LITERAL_STRING(DOWNLOAD_MANAGER_ALERT_ICON), title,
|
2007-09-22 02:08:28 +00:00
|
|
|
message, !removeWhenDone, EmptyString(), mDownloadManager,
|
|
|
|
EmptyString());
|
2007-08-22 21:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-20 22:42:38 +00:00
|
|
|
#if defined(XP_WIN) && !defined(WINCE)
|
2007-10-03 16:00:48 +00:00
|
|
|
// Default is to add the download to the system's "recent documents"
|
|
|
|
// list, with a pref to disable.
|
2007-08-22 21:55:57 +00:00
|
|
|
PRBool addToRecentDocs = PR_TRUE;
|
|
|
|
if (pref)
|
|
|
|
pref->GetBoolPref(PREF_BDM_ADDTORECENTDOCS, &addToRecentDocs);
|
|
|
|
|
|
|
|
if (addToRecentDocs) {
|
|
|
|
LPSHELLFOLDER lpShellFolder = NULL;
|
|
|
|
|
|
|
|
if (SUCCEEDED(::SHGetDesktopFolder(&lpShellFolder))) {
|
2007-10-01 19:56:24 +00:00
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(mTarget);
|
2007-08-22 21:55:57 +00:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsAutoString path;
|
2007-10-30 18:31:50 +00:00
|
|
|
if (fileURL &&
|
|
|
|
NS_SUCCEEDED(fileURL->GetFile(getter_AddRefs(file))) &&
|
|
|
|
file &&
|
2007-10-01 19:56:24 +00:00
|
|
|
NS_SUCCEEDED(file->GetPath(path))) {
|
|
|
|
PRUnichar *filePath = ToNewUnicode(path);
|
|
|
|
LPITEMIDLIST lpItemIDList = NULL;
|
|
|
|
if (SUCCEEDED(lpShellFolder->ParseDisplayName(NULL, NULL, filePath,
|
|
|
|
NULL, &lpItemIDList,
|
|
|
|
NULL))) {
|
|
|
|
::SHAddToRecentDocs(SHARD_PIDL, lpItemIDList);
|
|
|
|
::CoTaskMemFree(lpItemIDList);
|
|
|
|
}
|
|
|
|
nsMemory::Free(filePath);
|
2007-08-22 21:55:57 +00:00
|
|
|
}
|
|
|
|
lpShellFolder->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// Now remove the download if the user's retention policy is "Remove when Done"
|
|
|
|
if (mDownloadManager->GetRetentionBehavior() == 0)
|
|
|
|
mDownloadManager->RemoveDownload(mID);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2007-05-23 23:12:03 +00:00
|
|
|
// Before notifying the listener, we must update the database so that calls
|
|
|
|
// to it work out properly.
|
2007-08-22 21:55:57 +00:00
|
|
|
rv = UpdateDB();
|
2007-05-23 23:12:03 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-08-22 21:55:57 +00:00
|
|
|
|
2007-05-25 23:47:53 +00:00
|
|
|
mDownloadManager->NotifyListenersOnDownloadStateChange(oldState, this);
|
2007-05-23 23:12:03 +00:00
|
|
|
|
2007-08-22 21:55:57 +00:00
|
|
|
switch (mDownloadState) {
|
|
|
|
case nsIDownloadManager::DOWNLOAD_DOWNLOADING:
|
2007-11-13 02:50:13 +00:00
|
|
|
// Only send the dl-start event to downloads that are actually starting.
|
|
|
|
if (oldState == nsIDownloadManager::DOWNLOAD_QUEUED)
|
|
|
|
mDownloadManager->SendEvent(this, "dl-start");
|
2007-08-22 21:55:57 +00:00
|
|
|
break;
|
|
|
|
case nsIDownloadManager::DOWNLOAD_FAILED:
|
|
|
|
mDownloadManager->SendEvent(this, "dl-failed");
|
|
|
|
break;
|
|
|
|
case nsIDownloadManager::DOWNLOAD_SCANNING:
|
|
|
|
mDownloadManager->SendEvent(this, "dl-scanning");
|
|
|
|
break;
|
|
|
|
case nsIDownloadManager::DOWNLOAD_FINISHED:
|
|
|
|
mDownloadManager->SendEvent(this, "dl-done");
|
|
|
|
break;
|
|
|
|
case nsIDownloadManager::DOWNLOAD_BLOCKED:
|
|
|
|
mDownloadManager->SendEvent(this, "dl-blocked");
|
|
|
|
break;
|
2007-12-01 06:49:07 +00:00
|
|
|
case nsIDownloadManager::DOWNLOAD_DIRTY:
|
|
|
|
mDownloadManager->SendEvent(this, "dl-dirty");
|
2007-08-22 21:55:57 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-05-23 23:12:03 +00:00
|
|
|
return NS_OK;
|
2003-12-10 21:59:41 +00:00
|
|
|
}
|
|
|
|
|
2007-10-02 15:03:16 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIWebProgressListener2
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2005-04-05 23:24:23 +00:00
|
|
|
nsDownload::OnProgressChange64(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRInt64 aCurSelfProgress,
|
|
|
|
PRInt64 aMaxSelfProgress,
|
|
|
|
PRInt64 aCurTotalProgress,
|
|
|
|
PRInt64 aMaxTotalProgress)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
if (!mRequest)
|
|
|
|
mRequest = aRequest; // used for pause/resume
|
|
|
|
|
2007-08-15 17:56:50 +00:00
|
|
|
if (mDownloadState == nsIDownloadManager::DOWNLOAD_QUEUED) {
|
2007-08-17 23:05:26 +00:00
|
|
|
// Obtain the referrer
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
|
2007-11-23 21:29:22 +00:00
|
|
|
if (channel)
|
|
|
|
(void)NS_GetReferrerFromChannel(channel, getter_AddRefs(mReferrer));
|
|
|
|
|
|
|
|
// If we have a MIME info, we know that exthandler has already added this to
|
|
|
|
// the history, but if we do not, we'll have to add it ourselves.
|
|
|
|
if (!mMIMEInfo) {
|
|
|
|
nsCOMPtr<nsIDownloadHistory> dh =
|
|
|
|
do_GetService(NS_DOWNLOADHISTORY_CONTRACTID);
|
|
|
|
if (dh)
|
|
|
|
(void)dh->AddDownload(mSource, mReferrer, mStartTime);
|
2007-08-17 23:05:26 +00:00
|
|
|
}
|
|
|
|
|
2007-10-01 19:12:26 +00:00
|
|
|
// Fetch the entityID, but if we can't get it, don't panic (non-resumable)
|
2007-09-06 04:59:34 +00:00
|
|
|
nsCOMPtr<nsIResumableChannel> resumableChannel(do_QueryInterface(aRequest));
|
2007-10-01 19:12:26 +00:00
|
|
|
if (resumableChannel)
|
|
|
|
(void)resumableChannel->GetEntityID(mEntityID);
|
2007-09-06 04:59:34 +00:00
|
|
|
|
2007-08-17 23:05:26 +00:00
|
|
|
// Update the state and the database
|
|
|
|
rv = SetState(nsIDownloadManager::DOWNLOAD_DOWNLOADING);
|
2007-06-05 16:54:40 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
// filter notifications since they come in so frequently
|
|
|
|
PRTime now = PR_Now();
|
2007-05-22 00:03:33 +00:00
|
|
|
PRIntervalTime delta = now - mLastUpdate;
|
|
|
|
if (delta < gUpdateInterval)
|
2003-11-27 00:54:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
mLastUpdate = now;
|
|
|
|
|
2006-02-06 00:34:30 +00:00
|
|
|
// Calculate the speed using the elapsed delta time and bytes downloaded
|
|
|
|
// during that time for more accuracy.
|
|
|
|
double elapsedSecs = double(delta) / PR_USEC_PER_SEC;
|
|
|
|
if (elapsedSecs > 0) {
|
2007-09-17 20:40:47 +00:00
|
|
|
double speed = double(aCurTotalProgress - mCurrBytes) / elapsedSecs;
|
2007-05-22 00:03:33 +00:00
|
|
|
if (mCurrBytes == 0) {
|
2006-02-06 00:34:30 +00:00
|
|
|
mSpeed = speed;
|
2007-05-22 00:03:33 +00:00
|
|
|
} else {
|
2006-02-06 00:34:30 +00:00
|
|
|
// Calculate 'smoothed average' of 10 readings.
|
|
|
|
mSpeed = mSpeed * 0.9 + speed * 0.1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-02 00:16:12 +00:00
|
|
|
SetProgressBytes(aCurTotalProgress, aMaxTotalProgress);
|
2007-09-17 20:40:47 +00:00
|
|
|
|
2007-10-02 00:16:12 +00:00
|
|
|
// Report to the listener our real sizes
|
|
|
|
PRInt64 currBytes, maxBytes;
|
2007-09-17 20:40:47 +00:00
|
|
|
(void)GetAmountTransferred(&currBytes);
|
|
|
|
(void)GetSize(&maxBytes);
|
2007-05-25 23:47:53 +00:00
|
|
|
mDownloadManager->NotifyListenersOnProgressChange(
|
2007-09-17 20:40:47 +00:00
|
|
|
aWebProgress, aRequest, currBytes, maxBytes, currBytes, maxBytes, this);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2008-01-27 22:38:37 +00:00
|
|
|
// If the maximums are different, then there must be more than one file
|
|
|
|
if (aMaxSelfProgress != aMaxTotalProgress)
|
|
|
|
mHasMultipleFiles = PR_TRUE;
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
return NS_OK;
|
2005-04-05 23:24:23 +00:00
|
|
|
}
|
|
|
|
|
2007-02-08 13:15:50 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::OnRefreshAttempted(nsIWebProgress *aWebProgress,
|
|
|
|
nsIURI *aUri,
|
|
|
|
PRInt32 aDelay,
|
|
|
|
PRBool aSameUri,
|
|
|
|
PRBool *allowRefresh)
|
|
|
|
{
|
|
|
|
*allowRefresh = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-10-02 15:03:16 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIWebProgressListener
|
2005-04-05 23:24:23 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::OnProgressChange(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRInt32 aCurSelfProgress,
|
|
|
|
PRInt32 aMaxSelfProgress,
|
|
|
|
PRInt32 aCurTotalProgress,
|
|
|
|
PRInt32 aMaxTotalProgress)
|
|
|
|
{
|
|
|
|
return OnProgressChange64(aWebProgress, aRequest,
|
|
|
|
aCurSelfProgress, aMaxSelfProgress,
|
|
|
|
aCurTotalProgress, aMaxTotalProgress);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::OnLocationChange(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest, nsIURI *aLocation)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::OnStatusChange(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest, nsresult aStatus,
|
|
|
|
const PRUnichar *aMessage)
|
2007-09-17 20:49:24 +00:00
|
|
|
{
|
2007-10-02 00:39:50 +00:00
|
|
|
if (NS_FAILED(aStatus))
|
|
|
|
return FailDownload(aStatus, aMessage);
|
2003-11-27 00:54:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownload::OnStateChange(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest, PRUint32 aStateFlags,
|
2003-11-27 00:54:33 +00:00
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
// We don't want to lose access to our member variables
|
|
|
|
nsRefPtr<nsDownload> kungFuDeathGrip = this;
|
2007-09-17 20:49:24 +00:00
|
|
|
|
2008-01-27 22:38:37 +00:00
|
|
|
// Check if we're starting a request; the NETWORK flag is necessary to not
|
|
|
|
// pick up the START of *each* file but only for the whole request
|
|
|
|
if ((aStateFlags & STATE_START) && (aStateFlags & STATE_IS_NETWORK)) {
|
2007-08-21 16:37:39 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIHttpChannel> channel = do_QueryInterface(aRequest, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRUint32 status;
|
|
|
|
rv = channel->GetResponseStatus(&status);
|
|
|
|
// HTTP 450 - Blocked by parental control proxies
|
|
|
|
if (NS_SUCCEEDED(rv) && status == 450) {
|
|
|
|
// Cancel using the provided object
|
2007-09-17 22:41:15 +00:00
|
|
|
(void)Cancel();
|
2007-08-21 16:37:39 +00:00
|
|
|
|
|
|
|
// Fail the download - DOWNLOAD_BLOCKED
|
2007-08-22 21:55:57 +00:00
|
|
|
(void)SetState(nsIDownloadManager::DOWNLOAD_BLOCKED);
|
2007-08-21 16:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-27 22:38:37 +00:00
|
|
|
} else if ((aStateFlags & STATE_STOP) && (aStateFlags & STATE_IS_NETWORK) &&
|
|
|
|
IsFinishable()) {
|
|
|
|
// We got both STOP and NETWORK so that means the whole request is done
|
|
|
|
// (and not just a single file if there are multiple files)
|
2007-10-02 00:39:50 +00:00
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
2007-10-02 00:16:12 +00:00
|
|
|
// We can't completely trust the bytes we've added up because we might be
|
|
|
|
// missing on some/all of the progress updates (especially from cache).
|
2008-01-27 22:38:37 +00:00
|
|
|
// Our best bet is the file itself, but if for some reason it's gone or
|
|
|
|
// if we have multiple files, the next best is what we've calculated.
|
2007-10-02 00:16:12 +00:00
|
|
|
PRInt64 fileSize;
|
|
|
|
nsCOMPtr<nsILocalFile> file;
|
|
|
|
// We need a nsIFile clone to deal with file size caching issues. :(
|
|
|
|
nsCOMPtr<nsIFile> clone;
|
2008-01-27 22:38:37 +00:00
|
|
|
if (!mHasMultipleFiles &&
|
|
|
|
NS_SUCCEEDED(GetTargetFile(getter_AddRefs(file))) &&
|
2007-10-02 00:16:12 +00:00
|
|
|
NS_SUCCEEDED(file->Clone(getter_AddRefs(clone))) &&
|
|
|
|
NS_SUCCEEDED(clone->GetFileSize(&fileSize)) && fileSize > 0) {
|
|
|
|
mCurrBytes = mMaxBytes = fileSize;
|
|
|
|
|
|
|
|
// If we resumed, keep the fact that we did and fix size calculations
|
|
|
|
if (WasResumed())
|
|
|
|
mResumedAt = 0;
|
|
|
|
} else if (mMaxBytes == -1) {
|
2005-12-19 15:25:22 +00:00
|
|
|
mMaxBytes = mCurrBytes;
|
2007-10-02 00:16:12 +00:00
|
|
|
} else {
|
|
|
|
mCurrBytes = mMaxBytes;
|
|
|
|
}
|
2005-12-19 15:25:22 +00:00
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
mPercentComplete = 100;
|
2007-10-02 00:16:12 +00:00
|
|
|
mLastUpdate = PR_Now();
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2008-02-20 22:42:38 +00:00
|
|
|
#if defined(XP_WIN) && !defined(__MINGW32__) && !defined(WINCE)
|
2008-01-30 05:07:44 +00:00
|
|
|
PRBool scan = PR_TRUE;
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
|
|
|
if (prefs)
|
|
|
|
(void)prefs->GetBoolPref(PREF_BDM_SCANWHENDONE, &scan);
|
|
|
|
|
|
|
|
if (scan)
|
|
|
|
(void)SetState(nsIDownloadManager::DOWNLOAD_SCANNING);
|
|
|
|
else
|
|
|
|
(void)SetState(nsIDownloadManager::DOWNLOAD_FINISHED);
|
2007-08-22 21:55:57 +00:00
|
|
|
#else
|
|
|
|
(void)SetState(nsIDownloadManager::DOWNLOAD_FINISHED);
|
2006-07-02 20:47:53 +00:00
|
|
|
#endif
|
2007-10-02 00:39:50 +00:00
|
|
|
} else {
|
|
|
|
// We failed for some unknown reason -- fail with a generic message
|
|
|
|
(void)FailDownload(aStatus, nsnull);
|
2007-08-21 16:37:39 +00:00
|
|
|
}
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-05-25 23:47:53 +00:00
|
|
|
mDownloadManager->NotifyListenersOnStateChange(aWebProgress, aRequest,
|
|
|
|
aStateFlags, aStatus, this);
|
2007-08-22 21:55:57 +00:00
|
|
|
return NS_OK;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::OnSecurityChange(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest, PRUint32 aState)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-10-02 15:03:16 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIDownload
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownload::Init(nsIURI *aSource,
|
|
|
|
nsIURI *aTarget,
|
2005-04-24 21:16:28 +00:00
|
|
|
const nsAString& aDisplayName,
|
2003-11-27 00:54:33 +00:00
|
|
|
nsIMIMEInfo *aMIMEInfo,
|
2005-04-24 21:16:28 +00:00
|
|
|
PRTime aStartTime,
|
2007-09-17 20:49:24 +00:00
|
|
|
nsILocalFile *aTempFile,
|
|
|
|
nsICancelable *aCancelable)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
NS_WARNING("Huh...how did we get here?!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownload::GetState(PRInt16 *aState)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
*aState = mDownloadState;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::GetDisplayName(nsAString &aDisplayName)
|
|
|
|
{
|
|
|
|
aDisplayName = mDisplayName;
|
2003-11-27 00:54:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-04-24 21:16:28 +00:00
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownload::GetCancelable(nsICancelable **aCancelable)
|
2005-04-24 21:16:28 +00:00
|
|
|
{
|
|
|
|
*aCancelable = mCancelable;
|
|
|
|
NS_IF_ADDREF(*aCancelable);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownload::GetTarget(nsIURI **aTarget)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
*aTarget = mTarget;
|
|
|
|
NS_IF_ADDREF(*aTarget);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownload::GetSource(nsIURI **aSource)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
*aSource = mSource;
|
|
|
|
NS_IF_ADDREF(*aSource);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownload::GetStartTime(PRInt64 *aStartTime)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
*aStartTime = mStartTime;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownload::GetPercentComplete(PRInt32 *aPercentComplete)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
*aPercentComplete = mPercentComplete;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-12-18 04:06:51 +00:00
|
|
|
NS_IMETHODIMP
|
2007-10-02 00:16:12 +00:00
|
|
|
nsDownload::GetAmountTransferred(PRInt64 *aAmountTransferred)
|
2004-12-18 04:06:51 +00:00
|
|
|
{
|
2007-10-01 23:57:48 +00:00
|
|
|
*aAmountTransferred = mCurrBytes + (WasResumed() ? mResumedAt : 0);
|
2004-12-18 04:06:51 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-10-02 00:16:12 +00:00
|
|
|
nsDownload::GetSize(PRInt64 *aSize)
|
2004-12-18 04:06:51 +00:00
|
|
|
{
|
2007-10-02 00:16:12 +00:00
|
|
|
*aSize = mMaxBytes + (WasResumed() && mMaxBytes != -1 ? mResumedAt : 0);
|
2004-12-18 04:06:51 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownload::GetMIMEInfo(nsIMIMEInfo **aMIMEInfo)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
*aMIMEInfo = mMIMEInfo;
|
|
|
|
NS_IF_ADDREF(*aMIMEInfo);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-04-17 00:58:23 +00:00
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownload::GetTargetFile(nsILocalFile **aTargetFile)
|
2004-04-17 00:58:23 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(mTarget, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = fileURL->GetFile(getter_AddRefs(file));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = CallQueryInterface(file, aTargetFile);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2006-02-06 00:34:30 +00:00
|
|
|
NS_IMETHODIMP
|
2007-09-17 20:49:24 +00:00
|
|
|
nsDownload::GetSpeed(double *aSpeed)
|
2006-02-06 00:34:30 +00:00
|
|
|
{
|
|
|
|
*aSpeed = mSpeed;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::GetId(PRUint32 *aId)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
*aId = mID;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-09-17 20:49:24 +00:00
|
|
|
NS_IMETHODIMP
|
2007-08-17 23:05:26 +00:00
|
|
|
nsDownload::GetReferrer(nsIURI **referrer)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*referrer = mReferrer);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-22 03:43:02 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::GetResumable(PRBool *resumable)
|
|
|
|
{
|
|
|
|
*resumable = IsResumable();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-10-02 15:03:16 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsDownload Helper Functions
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDownload::Finalize()
|
|
|
|
{
|
|
|
|
// We're stopping, so break the cycle we created at download start
|
|
|
|
mCancelable = nsnull;
|
|
|
|
|
|
|
|
// Reset values that aren't needed anymore, so the DB can be updated as well
|
|
|
|
mEntityID.Truncate();
|
2007-10-14 00:57:34 +00:00
|
|
|
mTempFile = nsnull;
|
2007-10-02 15:03:16 +00:00
|
|
|
|
|
|
|
// Remove ourself from the active downloads
|
|
|
|
(void)mDownloadManager->mCurrentDownloads.RemoveObject(this);
|
2008-02-22 03:50:01 +00:00
|
|
|
|
|
|
|
// Make sure we do not automatically resume
|
|
|
|
mAutoResume = DONT_RESUME;
|
2007-10-02 15:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDownload::ExecuteDesiredAction()
|
|
|
|
{
|
|
|
|
// If we have a temp file and we have resumed, we have to do what the
|
|
|
|
// external helper app service would have done.
|
|
|
|
if (!mTempFile || !WasResumed())
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// We need to bail if for some reason the temp file got removed
|
|
|
|
PRBool fileExists;
|
|
|
|
if (NS_FAILED(mTempFile->Exists(&fileExists)) || !fileExists)
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
|
|
|
|
// Assume an unknown action is save to disk
|
|
|
|
nsHandlerInfoAction action = nsIMIMEInfo::saveToDisk;
|
|
|
|
if (mMIMEInfo) {
|
|
|
|
nsresult rv = mMIMEInfo->GetPreferredAction(&action);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2007-10-24 22:19:04 +00:00
|
|
|
nsresult retVal = NS_OK;
|
2007-10-02 15:03:16 +00:00
|
|
|
switch (action) {
|
|
|
|
case nsIMIMEInfo::saveToDisk:
|
|
|
|
// Move the file to the proper location
|
2007-10-24 22:19:04 +00:00
|
|
|
retVal = MoveTempToTarget();
|
|
|
|
break;
|
|
|
|
case nsIMIMEInfo::useHelperApp:
|
|
|
|
case nsIMIMEInfo::useSystemDefault:
|
|
|
|
// For these cases we have to move the file to the target location and
|
|
|
|
// open with the appropriate application
|
|
|
|
retVal = OpenWithApplication();
|
2007-10-02 15:03:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-10-24 22:19:04 +00:00
|
|
|
return retVal;
|
2007-10-02 15:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDownload::MoveTempToTarget()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILocalFile> target;
|
|
|
|
nsresult rv = GetTargetFile(getter_AddRefs(target));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// MoveTo will fail if the file already exists, but we've already obtained
|
|
|
|
// confirmation from the user that this is OK, so remove it if it exists.
|
|
|
|
PRBool fileExists;
|
|
|
|
if (NS_SUCCEEDED(target->Exists(&fileExists)) && fileExists) {
|
|
|
|
rv = target->Remove(PR_FALSE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the new leaf name from the file location
|
|
|
|
nsAutoString fileName;
|
|
|
|
rv = target->GetLeafName(fileName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
rv = target->GetParent(getter_AddRefs(dir));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mTempFile->MoveTo(dir, fileName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-10-24 22:19:04 +00:00
|
|
|
nsresult
|
|
|
|
nsDownload::OpenWithApplication()
|
|
|
|
{
|
|
|
|
// First move the temporary file to the target location
|
|
|
|
nsCOMPtr<nsILocalFile> target;
|
|
|
|
nsresult rv = GetTargetFile(getter_AddRefs(target));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Make sure the suggested name is unique since in this case we don't
|
|
|
|
// have a file name that was guaranteed to be unique by going through
|
|
|
|
// the File Save dialog
|
|
|
|
rv = target->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Move the temporary file to the target location
|
|
|
|
rv = MoveTempToTarget();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// We do not verify the return value here because, irrespective of success
|
|
|
|
// or failure of the method, the deletion of temp file has to take place, as
|
|
|
|
// per the corresponding preference. But we store this separately as this is
|
|
|
|
// what we ultimately return from this function.
|
|
|
|
nsresult retVal = mMIMEInfo->LaunchWithFile(target);
|
|
|
|
|
|
|
|
PRBool deleteTempFileOnExit;
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
|
|
|
if (!prefs || NS_FAILED(prefs->GetBoolPref(PREF_BH_DELETETEMPFILEONEXIT,
|
|
|
|
&deleteTempFileOnExit))) {
|
|
|
|
// No prefservice or no pref set; use default value
|
|
|
|
#if !defined(XP_MACOSX)
|
|
|
|
// Mac users have been very verbal about temp files being deleted on
|
|
|
|
// app exit - they don't like it - but we'll continue to do this on
|
|
|
|
// other platforms for now.
|
|
|
|
deleteTempFileOnExit = PR_TRUE;
|
|
|
|
#else
|
|
|
|
deleteTempFileOnExit = PR_FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deleteTempFileOnExit) {
|
|
|
|
// Use the ExternalHelperAppService to push the temporary file to the list
|
|
|
|
// of files to be deleted on exit.
|
|
|
|
nsCOMPtr<nsPIExternalAppLauncher> appLauncher(do_GetService
|
|
|
|
(NS_EXTERNALHELPERAPPSERVICE_CONTRACTID));
|
|
|
|
|
|
|
|
// Even if we are unable to get this service we return the result
|
|
|
|
// of LaunchWithFile() which makes more sense.
|
|
|
|
if (appLauncher)
|
|
|
|
(void)appLauncher->DeleteTemporaryFileOnExit(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2007-10-02 15:03:16 +00:00
|
|
|
void
|
|
|
|
nsDownload::SetStartTime(PRInt64 aStartTime)
|
|
|
|
{
|
|
|
|
mStartTime = aStartTime;
|
|
|
|
mLastUpdate = aStartTime;
|
|
|
|
}
|
|
|
|
|
2007-10-02 00:16:12 +00:00
|
|
|
void
|
|
|
|
nsDownload::SetProgressBytes(PRInt64 aCurrBytes, PRInt64 aMaxBytes)
|
|
|
|
{
|
|
|
|
mCurrBytes = aCurrBytes;
|
|
|
|
mMaxBytes = aMaxBytes;
|
|
|
|
|
|
|
|
// Get the real bytes that include resume position
|
|
|
|
PRInt64 currBytes, maxBytes;
|
|
|
|
(void)GetAmountTransferred(&currBytes);
|
|
|
|
(void)GetSize(&maxBytes);
|
|
|
|
|
|
|
|
if (currBytes == maxBytes)
|
|
|
|
mPercentComplete = 100;
|
|
|
|
else if (maxBytes <= 0)
|
|
|
|
mPercentComplete = -1;
|
|
|
|
else
|
|
|
|
mPercentComplete = (PRInt32)((PRFloat64)currBytes / maxBytes * 100 + .5);
|
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult
|
2007-09-17 22:41:15 +00:00
|
|
|
nsDownload::Pause()
|
2007-05-22 00:03:33 +00:00
|
|
|
{
|
2008-01-22 03:43:02 +00:00
|
|
|
if (!IsResumable())
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
nsresult rv = Cancel();
|
2007-09-17 22:41:15 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-23 23:12:03 +00:00
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
return SetState(nsIDownloadManager::DOWNLOAD_PAUSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDownload::Cancel()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mCancelable) {
|
|
|
|
rv = mCancelable->Cancel(NS_BINDING_ABORTED);
|
|
|
|
// we're done with this, so break the cycle
|
|
|
|
mCancelable = nsnull;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
2007-05-22 00:03:33 +00:00
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2007-09-06 04:59:34 +00:00
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
nsresult
|
|
|
|
nsDownload::Resume()
|
|
|
|
{
|
2008-01-22 03:43:02 +00:00
|
|
|
if (!IsPaused() || !IsResumable())
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-09-06 04:59:34 +00:00
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIWebBrowserPersist> wbp =
|
|
|
|
do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = wbp->SetPersistFlags(nsIWebBrowserPersist::PERSIST_FLAGS_APPEND_TO_FILE |
|
|
|
|
nsIWebBrowserPersist::PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Create a new channel for the source URI
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> ir(do_QueryInterface(wbp));
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel), mSource, nsnull, nsnull, ir);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-06 04:59:34 +00:00
|
|
|
|
2007-10-01 23:57:48 +00:00
|
|
|
// Make sure we can get a file, either the temporary or the real target, for
|
|
|
|
// both purposes of file size and a target to write to
|
2007-09-17 22:41:15 +00:00
|
|
|
nsCOMPtr<nsILocalFile> targetLocalFile(mTempFile);
|
|
|
|
if (!targetLocalFile) {
|
|
|
|
rv = GetTargetFile(getter_AddRefs(targetLocalFile));
|
2007-09-06 04:59:34 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 22:41:15 +00:00
|
|
|
}
|
2007-09-06 04:59:34 +00:00
|
|
|
|
2007-10-01 23:57:48 +00:00
|
|
|
// Get the file size to be used as an offset, but if anything goes wrong
|
|
|
|
// along the way, we'll silently restart at 0.
|
|
|
|
PRInt64 fileSize;
|
|
|
|
// We need a nsIFile clone to deal with file size caching issues. :(
|
2007-09-17 22:41:15 +00:00
|
|
|
nsCOMPtr<nsIFile> clone;
|
2007-10-01 23:57:48 +00:00
|
|
|
if (NS_FAILED(targetLocalFile->Clone(getter_AddRefs(clone))) ||
|
|
|
|
NS_FAILED(clone->GetFileSize(&fileSize)))
|
|
|
|
fileSize = 0;
|
2007-09-17 20:40:47 +00:00
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
// Set the channel to resume at the right position along with the entityID
|
|
|
|
nsCOMPtr<nsIResumableChannel> resumableChannel(do_QueryInterface(channel));
|
|
|
|
if (!resumableChannel)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
rv = resumableChannel->ResumeAt(fileSize, mEntityID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-06 04:59:34 +00:00
|
|
|
|
2007-10-02 15:08:12 +00:00
|
|
|
// If we know the max size, we know what it should be when resuming
|
|
|
|
PRInt64 maxBytes;
|
|
|
|
GetSize(&maxBytes);
|
|
|
|
SetProgressBytes(0, maxBytes != -1 ? maxBytes - fileSize : -1);
|
2007-09-17 22:41:15 +00:00
|
|
|
// Track where we resumed because progress notifications restart at 0
|
|
|
|
mResumedAt = fileSize;
|
2007-09-06 04:59:34 +00:00
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
// Set the referrer
|
|
|
|
if (mReferrer) {
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
|
|
|
|
if (httpChannel) {
|
|
|
|
rv = httpChannel->SetReferrer(mReferrer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-06 04:59:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-17 22:41:15 +00:00
|
|
|
// Creates a cycle that will be broken when the download finishes
|
|
|
|
mCancelable = wbp;
|
|
|
|
(void)wbp->SetProgressListener(this);
|
|
|
|
|
|
|
|
// Save the channel using nsIWBP
|
|
|
|
rv = wbp->SaveChannel(channel, targetLocalFile);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCancelable = nsnull;
|
|
|
|
(void)wbp->SetProgressListener(nsnull);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-01-22 03:43:02 +00:00
|
|
|
return SetState(nsIDownloadManager::DOWNLOAD_DOWNLOADING);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-09-17 21:16:16 +00:00
|
|
|
PRBool
|
|
|
|
nsDownload::IsPaused()
|
|
|
|
{
|
|
|
|
return mDownloadState == nsIDownloadManager::DOWNLOAD_PAUSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsDownload::IsResumable()
|
|
|
|
{
|
2007-10-24 22:19:04 +00:00
|
|
|
return !mEntityID.IsEmpty();
|
2007-09-17 21:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsDownload::WasResumed()
|
|
|
|
{
|
2007-10-01 23:57:48 +00:00
|
|
|
return mResumedAt != -1;
|
2007-09-17 21:16:16 +00:00
|
|
|
}
|
|
|
|
|
2007-10-24 22:23:13 +00:00
|
|
|
PRBool
|
|
|
|
nsDownload::ShouldAutoResume()
|
|
|
|
{
|
|
|
|
return mAutoResume == AUTO_RESUME;
|
|
|
|
}
|
|
|
|
|
2007-09-17 21:02:39 +00:00
|
|
|
PRBool
|
|
|
|
nsDownload::IsFinishable()
|
|
|
|
{
|
|
|
|
return mDownloadState == nsIDownloadManager::DOWNLOAD_NOTSTARTED ||
|
|
|
|
mDownloadState == nsIDownloadManager::DOWNLOAD_QUEUED ||
|
|
|
|
mDownloadState == nsIDownloadManager::DOWNLOAD_DOWNLOADING;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsDownload::IsFinished()
|
|
|
|
{
|
|
|
|
return mDownloadState == nsIDownloadManager::DOWNLOAD_FINISHED;
|
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult
|
|
|
|
nsDownload::UpdateDB()
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ASSERTION(mID, "Download ID is stored as zero. This is bad!");
|
2007-06-05 00:03:35 +00:00
|
|
|
NS_ASSERTION(mDownloadManager, "Egads! We have no download manager!");
|
2007-05-22 00:03:33 +00:00
|
|
|
|
2007-07-02 17:29:59 +00:00
|
|
|
mozIStorageStatement *stmt = mDownloadManager->mUpdateDownloadStatement;
|
|
|
|
|
2007-09-17 21:10:29 +00:00
|
|
|
PRInt32 i = 0;
|
2007-10-14 00:57:34 +00:00
|
|
|
// tempPath
|
|
|
|
nsAutoString tempPath;
|
|
|
|
if (mTempFile)
|
|
|
|
(void)mTempFile->GetPath(tempPath);
|
|
|
|
nsresult rv = stmt->BindStringParameter(i++, tempPath);
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// startTime
|
2007-10-14 00:57:34 +00:00
|
|
|
rv = stmt->BindInt64Parameter(i++, mStartTime);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// endTime
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindInt64Parameter(i++, mLastUpdate);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// state
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindInt32Parameter(i++, mDownloadState);
|
2007-08-17 23:05:26 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// referrer
|
|
|
|
if (mReferrer) {
|
|
|
|
nsCAutoString referrer;
|
|
|
|
rv = mReferrer->GetSpec(referrer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindUTF8StringParameter(i++, referrer);
|
2007-08-17 23:05:26 +00:00
|
|
|
} else {
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindNullParameter(i++);
|
2007-08-17 23:05:26 +00:00
|
|
|
}
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-09-17 20:49:24 +00:00
|
|
|
// entityID
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindUTF8StringParameter(i++, mEntityID);
|
2007-09-06 04:59:34 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-10-02 00:16:12 +00:00
|
|
|
// currBytes
|
|
|
|
PRInt64 currBytes;
|
|
|
|
(void)GetAmountTransferred(&currBytes);
|
|
|
|
rv = stmt->BindInt64Parameter(i++, currBytes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// maxBytes
|
|
|
|
PRInt64 maxBytes;
|
|
|
|
(void)GetSize(&maxBytes);
|
|
|
|
rv = stmt->BindInt64Parameter(i++, maxBytes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-10-24 22:23:13 +00:00
|
|
|
// autoResume
|
|
|
|
rv = stmt->BindInt32Parameter(i++, mAutoResume);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// id
|
2007-09-17 21:10:29 +00:00
|
|
|
rv = stmt->BindInt64Parameter(i++, mID);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return stmt->Execute();
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
2007-10-02 00:39:50 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDownload::FailDownload(nsresult aStatus, const PRUnichar *aMessage)
|
|
|
|
{
|
|
|
|
// Grab the bundle before potentially losing our member variables
|
|
|
|
nsCOMPtr<nsIStringBundle> bundle = mDownloadManager->mBundle;
|
|
|
|
|
|
|
|
(void)SetState(nsIDownloadManager::DOWNLOAD_FAILED);
|
|
|
|
|
|
|
|
// Get title for alert.
|
|
|
|
nsXPIDLString title;
|
|
|
|
nsresult rv = bundle->GetStringFromName(
|
|
|
|
NS_LITERAL_STRING("downloadErrorAlertTitle").get(), getter_Copies(title));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Get a generic message if we weren't supplied one
|
|
|
|
nsXPIDLString message;
|
|
|
|
message = aMessage;
|
|
|
|
if (message.IsEmpty()) {
|
|
|
|
rv = bundle->GetStringFromName(
|
|
|
|
NS_LITERAL_STRING("downloadErrorGeneric").get(), getter_Copies(message));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Download Manager window to be parent of alert
|
|
|
|
nsCOMPtr<nsIWindowMediator> wm =
|
|
|
|
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIDOMWindowInternal> dmWindow;
|
|
|
|
rv = wm->GetMostRecentWindow(NS_LITERAL_STRING("Download:Manager").get(),
|
|
|
|
getter_AddRefs(dmWindow));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Show alert
|
|
|
|
nsCOMPtr<nsIPromptService> prompter =
|
|
|
|
do_GetService("@mozilla.org/embedcomp/prompt-service;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return prompter->Alert(dmWindow, title, message);
|
|
|
|
}
|