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>
|
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 ***** */
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#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"
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2006-07-02 20:47:53 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <shlobj.h>
|
|
|
|
#endif
|
|
|
|
|
2003-12-11 07:03:44 +00:00
|
|
|
static PRBool gStoppingDownloads = PR_FALSE;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
#define DOWNLOAD_MANAGER_FE_URL "chrome://mozapps/content/downloads/downloads.xul"
|
|
|
|
#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"
|
|
|
|
#define PREF_BDM_OPENDELAY "browser.download.manager.openDelay"
|
2004-02-11 03:48:16 +00:00
|
|
|
#define PREF_BDM_SHOWWHENSTARTING "browser.download.manager.showWhenStarting"
|
2003-12-01 03:49:01 +00:00
|
|
|
#define PREF_BDM_FOCUSWHENSTARTING "browser.download.manager.focusWhenStarting"
|
2005-08-16 12:06:07 +00:00
|
|
|
#define PREF_BDM_CLOSEWHENDONE "browser.download.manager.closeWhenDone"
|
2004-02-11 03:48:16 +00:00
|
|
|
#define PREF_BDM_FLASHCOUNT "browser.download.manager.flashCount"
|
2006-07-02 20:47:53 +00:00
|
|
|
#define PREF_BDM_ADDTORECENTDOCS "browser.download.manager.addToRecentDocs"
|
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-07-22 17:24:03 +00:00
|
|
|
#define DM_SCHEMA_VERSION 2
|
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")
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsDownloadManager
|
|
|
|
|
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()
|
|
|
|
{
|
2007-07-12 20:01:57 +00:00
|
|
|
gDownloadManagerService = nsnull;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::CancelAllDownloads()
|
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-05-22 00:03:33 +00:00
|
|
|
nsresult result = CancelDownload(dl->mID);
|
|
|
|
// We want to try the rest of them because they should be canceled if they
|
|
|
|
// can be canceled.
|
|
|
|
if (NS_FAILED(result)) rv = result;
|
|
|
|
}
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
return rv;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-06-16 22:03:02 +00:00
|
|
|
nsresult
|
|
|
|
nsDownloadManager::FinishDownload(nsDownload *aDownload, DownloadState aState,
|
|
|
|
const char *aTopic) {
|
2007-06-17 01:12:27 +00:00
|
|
|
// We don't want to lose access to the download's member variables
|
|
|
|
nsRefPtr<nsDownload> kungFuDeathGrip = aDownload;
|
|
|
|
|
|
|
|
// we've stopped, so break the cycle we created at download start
|
|
|
|
aDownload->mCancelable = nsnull;
|
|
|
|
|
2007-06-16 22:03:02 +00:00
|
|
|
// This has to be done in this exact order to not mess up our invariants
|
|
|
|
// 1) when the state changed listener is dispatched, it must no longer be
|
|
|
|
// an active download.
|
|
|
|
// 2) when the observer is dispatched, the same conditions for 1 must be
|
|
|
|
// true as well as the state being up to date.
|
|
|
|
(void)mCurrentDownloads.RemoveObject(aDownload);
|
|
|
|
|
|
|
|
nsresult rv = aDownload->SetState(aState);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
(void)mObserverService->NotifyObservers(aDownload, aTopic, nsnull);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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);
|
2003-11-27 00:54:33 +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));
|
|
|
|
if (rv == NS_ERROR_FILE_CORRUPTED) {
|
|
|
|
// delete and try again
|
|
|
|
rv = dbFile->Remove(PR_TRUE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = storage->OpenDatabase(dbFile, getter_AddRefs(mDBConn));
|
2006-03-10 16:57:31 +00:00
|
|
|
}
|
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
|
|
|
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(
|
|
|
|
"CREATE TABLE moz_downloads ("
|
|
|
|
"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
|
|
|
|
|
|
|
|
case DM_SCHEMA_VERSION:
|
|
|
|
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(
|
|
|
|
"SELECT id, name, source, target, startTime, endTime, state "
|
|
|
|
"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, "
|
|
|
|
"startTime INTEGER, "
|
|
|
|
"endTime INTEGER, "
|
|
|
|
"state INTEGER"
|
|
|
|
")"));
|
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;
|
2003-11-27 00:54:33 +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);
|
2003-11-27 00:54:33 +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);
|
|
|
|
|
|
|
|
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;
|
2003-11-27 00:54:33 +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;
|
|
|
|
|
|
|
|
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-07-22 17:24:03 +00:00
|
|
|
(void)AddDownloadToDB(name, source, target, startTime, endTime, state);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
PRInt64
|
|
|
|
nsDownloadManager::AddDownloadToDB(const nsAString &aName,
|
|
|
|
const nsACString &aSource,
|
|
|
|
const nsACString &aTarget,
|
|
|
|
PRInt64 aStartTime,
|
|
|
|
PRInt64 aEndTime,
|
|
|
|
PRInt32 aState)
|
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-07-22 17:24:03 +00:00
|
|
|
"(name, source, target, startTime, endTime, state) "
|
|
|
|
"VALUES (?1, ?2, ?3, ?4, ?5, ?6)"), 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-05-22 00:03:33 +00:00
|
|
|
// name
|
|
|
|
rv = stmt->BindStringParameter(0, aName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// source
|
|
|
|
rv = stmt->BindUTF8StringParameter(1, aSource);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// target
|
|
|
|
rv = stmt->BindUTF8StringParameter(2, aTarget);
|
|
|
|
NS_ENSURE_SUCCESS(rv, 0);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// startTime
|
2007-07-22 17:24:03 +00:00
|
|
|
rv = stmt->BindInt64Parameter(3, 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-07-22 17:24:03 +00:00
|
|
|
rv = stmt->BindInt64Parameter(4, 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-07-22 17:24:03 +00:00
|
|
|
rv = stmt->BindInt32Parameter(5, aState);
|
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
|
|
|
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);
|
2003-11-27 00:54:33 +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
|
|
|
|
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-07-02 17:29:59 +00:00
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE moz_downloads "
|
|
|
|
"SET name = ?1, source = ?2, target = ?3, startTime = ?4, endTime = ?5,"
|
|
|
|
"state = ?6 "
|
|
|
|
"WHERE id = ?7"), getter_AddRefs(mUpdateDownloadStatement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
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
|
|
|
PRInt32
|
|
|
|
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);
|
|
|
|
|
|
|
|
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
|
|
|
|
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(
|
|
|
|
"SELECT id, state, startTime, source, target, name "
|
|
|
|
"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;
|
|
|
|
|
|
|
|
// Setting all properties of the download now
|
|
|
|
dl->mCancelable = nsnull;
|
|
|
|
dl->mID = stmt->AsInt64(0);
|
|
|
|
dl->mDownloadState = stmt->AsInt32(1);
|
|
|
|
dl->mStartTime = stmt->AsInt64(2);
|
|
|
|
|
|
|
|
nsCString source;
|
|
|
|
stmt->GetUTF8String(3, source);
|
|
|
|
rv = NS_NewURI(getter_AddRefs(dl->mSource), source);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCString target;
|
|
|
|
stmt->GetUTF8String(4, target);
|
|
|
|
rv = NS_NewURI(getter_AddRefs(dl->mTarget), target);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
stmt->GetString(5, dl->mDisplayName);
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFile> file;
|
|
|
|
rv = dl->GetTargetFile(getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRBool fileExists;
|
|
|
|
if (NS_SUCCEEDED(file->Exists(&fileExists)) && fileExists) {
|
|
|
|
if (dl->mDownloadState == nsIDownloadManager::DOWNLOAD_FINISHED) {
|
|
|
|
dl->mPercentComplete = 100;
|
|
|
|
|
|
|
|
PRInt64 size;
|
|
|
|
rv = file->GetFileSize(&size);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
dl->mMaxBytes = dl->mCurrBytes = size;
|
|
|
|
} else {
|
|
|
|
dl->mPercentComplete = -1;
|
|
|
|
dl->mMaxBytes = LL_MAXUINT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dl->mPercentComplete = 0;
|
|
|
|
dl->mMaxBytes = LL_MAXUINT;
|
|
|
|
dl->mCurrBytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addrefing and returning
|
|
|
|
NS_ADDREF(*retVal = dl);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// 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
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-12-10 21:59:41 +00:00
|
|
|
nsDownloadManager::AddDownload(DownloadType aDownloadType,
|
|
|
|
nsIURI* aSource,
|
2004-04-17 00:58:23 +00:00
|
|
|
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,
|
2005-07-06 23:12:22 +00:00
|
|
|
nsILocalFile* aTempFile,
|
2005-07-06 23:22:27 +00:00
|
|
|
nsICancelable* aCancelable,
|
2003-11-27 00:54:33 +00:00
|
|
|
nsIDownload** aDownload)
|
|
|
|
{
|
|
|
|
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->mDownloadManager = this;
|
|
|
|
dl->mTarget = aTarget;
|
|
|
|
dl->mSource = aSource;
|
|
|
|
dl->mTempFile = aTempFile;
|
|
|
|
|
|
|
|
dl->mDisplayName = aDisplayName;
|
|
|
|
if (dl->mDisplayName.IsEmpty())
|
|
|
|
targetFile->GetLeafName(dl->mDisplayName);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
dl->mMIMEInfo = aMIMEInfo;
|
|
|
|
dl->SetStartTime(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);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-07-22 17:24:03 +00:00
|
|
|
PRInt64 id = AddDownloadToDB(dl->mDisplayName, source, target, aStartTime, 0,
|
2007-05-22 00:03:33 +00:00
|
|
|
nsIDownloadManager::DOWNLOAD_NOTSTARTED);
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
|
|
|
|
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-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);
|
2003-11-27 00:54:33 +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-05-22 00:03:33 +00:00
|
|
|
if (CompletedSuccessfully(dl->mDownloadState))
|
2003-11-27 00:54:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2007-08-12 22:15:15 +00:00
|
|
|
// if the download is paused, we have to resume it so we can cancel it
|
|
|
|
if (dl->mPaused)
|
|
|
|
(void)dl->PauseResume(PR_FALSE);
|
|
|
|
|
2005-04-24 21:16:28 +00:00
|
|
|
// Cancel using the provided object
|
2007-05-22 00:03:33 +00:00
|
|
|
if (dl->mCancelable)
|
|
|
|
dl->mCancelable->Cancel(NS_BINDING_ABORTED);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2006-08-07 23:03:48 +00:00
|
|
|
// Dump the temp file. This should really be done when the transfer
|
|
|
|
// is cancelled, but there are other cancellation causes that shouldn't
|
|
|
|
// remove this. We need to improve those bits.
|
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-06-16 22:03:02 +00:00
|
|
|
nsresult rv = FinishDownload(dl, nsIDownloadManager::DOWNLOAD_CANCELED,
|
|
|
|
"dl-cancel");
|
2007-05-23 23:12:03 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
// if there's a progress dialog open for the item,
|
|
|
|
// we have to notify it that we're cancelling
|
2007-05-22 00:03:33 +00:00
|
|
|
if (dl->mDialog) {
|
|
|
|
nsCOMPtr<nsIObserver> observer = do_QueryInterface(dl->mDialog);
|
|
|
|
observer->Observe(dl, "oncancel", nsnull);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
2005-06-22 17:12:14 +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 &&
|
|
|
|
dl->mDownloadState != nsIDownloadManager::DOWNLOAD_CANCELED)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// we are redownloading this, so we need to link the download manager to the
|
|
|
|
// download else we'll try to dereference null pointers - eww
|
|
|
|
dl->mDownloadManager = this;
|
|
|
|
|
|
|
|
dl->SetStartTime(PR_Now());
|
|
|
|
rv = dl->SetState(nsIDownloadManager::DOWNLOAD_NOTSTARTED);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIWebBrowserPersist> wbp =
|
|
|
|
do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-06-17 01:12:27 +00:00
|
|
|
// Creates a cycle that will be broken when the download finishes
|
2007-06-05 00:03:35 +00:00
|
|
|
dl->mCancelable = wbp;
|
|
|
|
wbp->SetProgressListener(dl);
|
|
|
|
|
|
|
|
rv = wbp->SetPersistFlags(nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES |
|
|
|
|
nsIWebBrowserPersist::PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = AddToCurrentDownloads(dl);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return wbp->SaveURI(dl->mSource, nsnull, nsnull, nsnull, nsnull, dl->mTarget);
|
|
|
|
}
|
|
|
|
|
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);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
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);
|
|
|
|
rv = stmt->BindInt64Parameter(0, aID); // unsigned; 64-bit to prevent overflow
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return stmt->Execute();
|
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-06-22 23:20:00 +00:00
|
|
|
nsIDownloadManager::DOWNLOAD_CANCELED };
|
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 "
|
|
|
|
"OR state = ?2 "
|
2007-06-22 23:20:00 +00:00
|
|
|
"OR state = ?3"), getter_AddRefs(stmt));
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-22 23:20:00 +00:00
|
|
|
for (PRUint32 i = 0; i < 3; ++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
|
|
|
return stmt->Execute();
|
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-06-22 23:20:00 +00:00
|
|
|
nsIDownloadManager::DOWNLOAD_CANCELED };
|
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 "
|
|
|
|
"OR state = ?2 "
|
2007-06-22 23:20:00 +00:00
|
|
|
"OR state = ?3"), getter_AddRefs(stmt));
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-22 23:20:00 +00:00
|
|
|
for (PRUint32 i = 0; i < 3; ++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);
|
|
|
|
|
|
|
|
PRInt32 count;
|
|
|
|
rv = stmt->GetInt32(0, &count);
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
*aResult = PR_TRUE;
|
|
|
|
|
|
|
|
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-05-22 00:03:33 +00:00
|
|
|
return PauseResumeDownload(aID, PR_TRUE);
|
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
|
|
|
return PauseResumeDownload(aID, PR_FALSE);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::PauseResumeDownload(PRUint32 aID, PRBool aPause)
|
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-05-22 00:03:33 +00:00
|
|
|
return dl->PauseResume(aPause);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::Open(nsIDOMWindow* aParent, PRUint32 aID)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-07-02 17:25:30 +00:00
|
|
|
// try to get an active download
|
|
|
|
nsRefPtr<nsDownload> dl = FindDownload(aID);
|
|
|
|
if (!dl) {
|
|
|
|
// try to get a finished download from the database
|
|
|
|
(void)GetDownloadFromDB(aID, getter_AddRefs(dl));
|
|
|
|
if (!dl) return NS_ERROR_FAILURE;
|
|
|
|
}
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
TimerParams* params = new TimerParams();
|
2007-07-02 17:25:30 +00:00
|
|
|
NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
params->parent = aParent;
|
|
|
|
params->download = dl;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2003-11-30 00:02:48 +00:00
|
|
|
PRInt32 delay = 0;
|
|
|
|
nsCOMPtr<nsIPrefBranch> pref(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
|
|
|
if (pref)
|
2003-12-01 03:49:01 +00:00
|
|
|
pref->GetIntPref(PREF_BDM_OPENDELAY, &delay);
|
2003-11-30 00:02:48 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// Look for an existing Download Manager window, if we find one we just
|
|
|
|
// tell it that a new download has begun (we don't focus, that's
|
|
|
|
// annoying), otherwise we need to open the window. We do this on a timer
|
|
|
|
// so that we can see if the download has already completed, if so, don't
|
|
|
|
// bother opening the window.
|
2003-11-27 00:54:33 +00:00
|
|
|
mDMOpenTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
return mDMOpenTimer->InitWithFuncCallback(OpenTimerCallback,
|
2003-11-30 00:02:48 +00:00
|
|
|
(void*)params, delay,
|
2003-11-27 00:54:33 +00:00
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDownloadManager::OpenTimerCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
2007-07-08 07:08:04 +00:00
|
|
|
TimerParams* params = static_cast<TimerParams*>(aClosure);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
PRInt32 complete;
|
2007-05-22 00:03:33 +00:00
|
|
|
params->download->GetPercentComplete(&complete);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2005-08-16 12:06:07 +00:00
|
|
|
PRBool closeDM = PR_FALSE;
|
|
|
|
nsCOMPtr<nsIPrefBranch> pref(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
|
|
|
if (pref)
|
|
|
|
pref->GetBoolPref(PREF_BDM_CLOSEWHENDONE, &closeDM);
|
|
|
|
|
|
|
|
// Check closeWhenDone pref before opening download manager
|
|
|
|
if (!closeDM || complete < 100) {
|
2003-11-27 00:54:33 +00:00
|
|
|
PRBool focusDM = PR_FALSE;
|
2004-02-11 03:48:16 +00:00
|
|
|
PRBool showDM = PR_TRUE;
|
|
|
|
PRInt32 flashCount = -1;
|
2005-08-16 12:06:07 +00:00
|
|
|
|
2004-02-11 03:48:16 +00:00
|
|
|
if (pref) {
|
2003-12-01 03:49:01 +00:00
|
|
|
pref->GetBoolPref(PREF_BDM_FOCUSWHENSTARTING, &focusDM);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2004-02-11 03:48:16 +00:00
|
|
|
// We only flash the download manager if the user has the download manager show
|
|
|
|
pref->GetBoolPref(PREF_BDM_SHOWWHENSTARTING, &showDM);
|
|
|
|
if (showDM)
|
|
|
|
pref->GetIntPref(PREF_BDM_FLASHCOUNT, &flashCount);
|
|
|
|
else
|
|
|
|
flashCount = 0;
|
|
|
|
}
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::OpenDownloadManager(focusDM, flashCount,
|
|
|
|
params->download, params->parent);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete params;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::OpenDownloadManager(PRBool aShouldFocus,
|
|
|
|
PRInt32 aFlashCount,
|
|
|
|
nsIDownload *aDownload,
|
|
|
|
nsIDOMWindow *aParent)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIWindowMediator> wm =
|
|
|
|
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindowInternal> recentWindow;
|
2007-05-22 00:03:33 +00:00
|
|
|
wm->GetMostRecentWindow(NS_LITERAL_STRING("Download:Manager").get(),
|
|
|
|
getter_AddRefs(recentWindow));
|
2003-11-27 00:54:33 +00:00
|
|
|
if (recentWindow) {
|
2007-05-22 00:03:33 +00:00
|
|
|
if (aShouldFocus) {
|
2003-11-27 00:54:33 +00:00
|
|
|
recentWindow->Focus();
|
2007-05-22 00:03:33 +00:00
|
|
|
} else {
|
2004-02-11 03:48:16 +00:00
|
|
|
nsCOMPtr<nsIDOMChromeWindow> chromeWindow(do_QueryInterface(recentWindow));
|
|
|
|
chromeWindow->GetAttentionWithCycleCount(aFlashCount);
|
|
|
|
}
|
2007-05-22 00:03:33 +00:00
|
|
|
} else {
|
|
|
|
// If we ever have the capability to display the UI of third party dl
|
|
|
|
// managers, we'll open their UI here instead.
|
|
|
|
nsCOMPtr<nsIWindowWatcher> ww =
|
|
|
|
do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
// pass the datasource to the window
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIMutableArray> params =
|
|
|
|
do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIDownloadManager> dlMgr =
|
|
|
|
do_GetService("@mozilla.org/download-manager;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<mozIStorageConnection> DBConn;
|
2007-06-21 23:56:17 +00:00
|
|
|
(void)dlMgr->GetDBConnection(getter_AddRefs(DBConn));
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
params->AppendElement(DBConn, PR_FALSE);
|
|
|
|
params->AppendElement(aDownload, PR_FALSE);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> newWindow;
|
|
|
|
rv = ww->OpenWindow(aParent,
|
|
|
|
DOWNLOAD_MANAGER_FE_URL,
|
|
|
|
"_blank",
|
2003-12-08 08:05:15 +00:00
|
|
|
"chrome,dialog=no,resizable",
|
2003-11-27 00:54:33 +00:00
|
|
|
params,
|
|
|
|
getter_AddRefs(newWindow));
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloadManager::RemoveListener(nsIDownloadProgressListener *aListener)
|
|
|
|
{
|
|
|
|
mListeners.RemoveObject(aListener);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIObserver
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-22 00:03:33 +00:00
|
|
|
nsDownloadManager::Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar* aData)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
2007-05-22 00:03:33 +00:00
|
|
|
PRInt32 currDownloadCount = mCurrentDownloads.Count();
|
2003-11-27 00:54:33 +00:00
|
|
|
|
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);
|
|
|
|
if (dl2) {
|
2003-11-27 00:54:33 +00:00
|
|
|
// unset dialog since it's closing
|
2007-05-22 00:03:33 +00:00
|
|
|
dl2->mDialog = nsnull;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
return CancelDownload(id);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
2007-05-22 00:03:33 +00:00
|
|
|
} else if (strcmp(aTopic, "quit-application") == 0) {
|
2003-12-11 07:03:44 +00:00
|
|
|
gStoppingDownloads = PR_TRUE;
|
2007-05-22 00:03:33 +00:00
|
|
|
|
2007-06-22 23:20:00 +00:00
|
|
|
if (currDownloadCount)
|
2007-05-22 00:03:33 +00:00
|
|
|
CancelAllDownloads();
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
// Now that active downloads have been canceled, remove all 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) {
|
2004-12-12 16:46:33 +00:00
|
|
|
// Attempt to locate a browser window to parent the download manager to
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIWindowMediator> wm = do_GetService(NS_WINDOWMEDIATOR_CONTRACTID);
|
2004-12-12 16:46:33 +00:00
|
|
|
nsCOMPtr<nsIDOMWindowInternal> browserWindow;
|
2007-05-22 00:03:33 +00:00
|
|
|
if (wm) {
|
|
|
|
wm->GetMostRecentWindow(NS_LITERAL_STRING("navigator:browser").get(),
|
|
|
|
getter_AddRefs(browserWindow));
|
|
|
|
}
|
2004-12-12 16:46:33 +00:00
|
|
|
|
|
|
|
return OpenDownloadManager(PR_TRUE, -1, nsnull, browserWindow);
|
|
|
|
}
|
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,
|
|
|
|
nsISupportsPRBool* aCancelDownloads,
|
2003-12-02 00:59:58 +00:00
|
|
|
const PRUnichar* aTitle,
|
|
|
|
const PRUnichar* aCancelMessageMultiple,
|
2003-12-03 06:26:07 +00:00
|
|
|
const PRUnichar* aCancelMessageSingle,
|
|
|
|
const PRUnichar* aDontCancelButton)
|
2003-12-02 00:59:58 +00:00
|
|
|
{
|
2003-12-03 06:26:07 +00:00
|
|
|
nsXPIDLString title, message, quitButton, dontQuitButton;
|
2003-12-02 00:59:58 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
mBundle->GetStringFromName(aTitle, getter_Copies(title));
|
|
|
|
|
|
|
|
nsAutoString countString;
|
|
|
|
countString.AppendInt(aCount);
|
|
|
|
const PRUnichar* strings[1] = { countString.get() };
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsDownload
|
|
|
|
|
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-06-05 00:03:35 +00:00
|
|
|
mMaxBytes(LL_MAXUINT),
|
2007-06-18 20:11:29 +00:00
|
|
|
mStartTime(0),
|
2007-05-22 00:03:33 +00:00
|
|
|
mLastUpdate(PR_Now() - (PRUint32)gUpdateInterval),
|
|
|
|
mPaused(PR_FALSE),
|
|
|
|
mSpeed(0)
|
2003-11-27 00:54:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDownload::~nsDownload()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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-05-23 23:12:03 +00:00
|
|
|
// Before notifying the listener, we must update the database so that calls
|
|
|
|
// to it work out properly.
|
|
|
|
nsresult rv = UpdateDB();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-25 23:47:53 +00:00
|
|
|
mDownloadManager->NotifyListenersOnDownloadStateChange(oldState, this);
|
2007-05-23 23:12:03 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2003-12-10 21:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DownloadType
|
|
|
|
nsDownload::GetDownloadType()
|
|
|
|
{
|
|
|
|
return mDownloadType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-11-27 00:54:33 +00:00
|
|
|
nsDownload::SetStartTime(PRInt64 aStartTime)
|
|
|
|
{
|
|
|
|
mStartTime = aStartTime;
|
2006-02-06 00:34:30 +00:00
|
|
|
mLastUpdate = aStartTime;
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2005-04-05 23:24:23 +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-06-05 16:54:40 +00:00
|
|
|
if (mDownloadState == nsIDownloadManager::DOWNLOAD_NOTSTARTED) {
|
|
|
|
nsresult rv = SetState(nsIDownloadManager::DOWNLOAD_DOWNLOADING);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mDownloadManager->mObserverService->NotifyObservers(this, "dl-start", nsnull);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
nsUint64 curTotalProgress = (PRUint64)aCurTotalProgress;
|
|
|
|
nsUint64 diffBytes = curTotalProgress - nsUint64(mCurrBytes);
|
|
|
|
double speed = double(diffBytes) / 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
if (aMaxTotalProgress > 0)
|
|
|
|
mPercentComplete = (PRInt32)((PRFloat64)aCurTotalProgress * 100 / aMaxTotalProgress + .5);
|
|
|
|
else
|
|
|
|
mPercentComplete = -1;
|
|
|
|
|
2005-12-19 15:25:22 +00:00
|
|
|
mCurrBytes = aCurTotalProgress;
|
|
|
|
mMaxBytes = aMaxTotalProgress;
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-25 23:47:53 +00:00
|
|
|
mDownloadManager->NotifyListenersOnProgressChange(
|
|
|
|
aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress,
|
|
|
|
aCurTotalProgress, aMaxTotalProgress, this);
|
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;
|
|
|
|
}
|
|
|
|
|
2005-04-05 23:24:23 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIWebProgressListener
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(aStatus)) {
|
2007-06-17 01:12:27 +00:00
|
|
|
// We don't want to lose access to our member variables
|
|
|
|
nsRefPtr<nsDownload> kungFuDeathGrip = this;
|
|
|
|
|
2007-06-16 22:03:02 +00:00
|
|
|
(void)mDownloadManager->FinishDownload(this,
|
|
|
|
nsIDownloadManager::DOWNLOAD_FAILED,
|
|
|
|
"dl-failed");
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
// Get title for alert.
|
|
|
|
nsXPIDLString title;
|
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIStringBundle> bundle = mDownloadManager->mBundle;
|
|
|
|
bundle->GetStringFromName(NS_LITERAL_STRING("downloadErrorAlertTitle").get(),
|
|
|
|
getter_Copies(title));
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
// Get Download Manager window, to be parent of alert.
|
2007-05-22 00:03:33 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIWindowMediator> wm =
|
|
|
|
do_GetService(NS_WINDOWMEDIATOR_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-27 00:54:33 +00:00
|
|
|
nsCOMPtr<nsIDOMWindowInternal> dmWindow;
|
2007-05-22 00:03:33 +00:00
|
|
|
wm->GetMostRecentWindow(NS_LITERAL_STRING("Download:Manager").get(),
|
|
|
|
getter_AddRefs(dmWindow));
|
2003-11-27 00:54:33 +00:00
|
|
|
|
|
|
|
// Show alert.
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIPromptService> prompter =
|
|
|
|
do_GetService("@mozilla.org/embedcomp/prompt-service;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
prompter->Alert(dmWindow, title, aMessage);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::OnStateChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest, PRUint32 aStateFlags,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2006-02-06 00:34:30 +00:00
|
|
|
// Record the start time only if it hasn't been set.
|
2007-06-18 20:11:29 +00:00
|
|
|
if (mStartTime == 0 && (aStateFlags & STATE_START))
|
2006-02-06 00:34:30 +00:00
|
|
|
SetStartTime(PR_Now());
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-22 00:03:33 +00:00
|
|
|
// We don't want to lose access to our member variables
|
|
|
|
nsRefPtr<nsDownload> kungFuDeathGrip = this;
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
// We need to update mDownloadState before updating the dialog, because
|
|
|
|
// that will close and call CancelDownload if it was the last open window.
|
2007-05-22 00:03:33 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch> pref = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
2006-07-02 20:47:53 +00:00
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
if (aStateFlags & STATE_STOP) {
|
2003-12-09 05:55:09 +00:00
|
|
|
if (nsDownloadManager::IsInFinalStage(mDownloadState)) {
|
2006-02-06 00:34:30 +00:00
|
|
|
// Set file size at the end of a tranfer (for unknown transfer amounts)
|
2007-05-22 00:03:33 +00:00
|
|
|
if (mMaxBytes == LL_MAXUINT)
|
2005-12-19 15:25:22 +00:00
|
|
|
mMaxBytes = mCurrBytes;
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
// Files less than 1Kb shouldn't show up as 0Kb.
|
2005-12-19 15:25:22 +00:00
|
|
|
if (mMaxBytes < 1024) {
|
|
|
|
mCurrBytes = 1024;
|
|
|
|
mMaxBytes = 1024;
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
mPercentComplete = 100;
|
|
|
|
|
2007-06-16 22:03:02 +00:00
|
|
|
(void)mDownloadManager->FinishDownload(this,
|
|
|
|
nsIDownloadManager::DOWNLOAD_FINISHED,
|
|
|
|
"dl-done");
|
2007-06-01 01:39:53 +00:00
|
|
|
|
2003-12-01 03:49:01 +00:00
|
|
|
// Master pref to control this function.
|
2007-06-01 01:39:53 +00:00
|
|
|
PRBool showTaskbarAlert = PR_TRUE;
|
2003-12-01 03:49:01 +00:00
|
|
|
if (pref)
|
|
|
|
pref->GetBoolPref(PREF_BDM_SHOWALERTONCOMPLETE, &showTaskbarAlert);
|
|
|
|
|
|
|
|
if (showTaskbarAlert) {
|
|
|
|
PRInt32 alertInterval = -1;
|
2007-05-22 00:03:33 +00:00
|
|
|
pref->GetIntPref(PREF_BDM_SHOWALERTINTERVAL, &alertInterval);
|
2003-12-01 03:49:01 +00:00
|
|
|
|
2007-06-18 20:11:29 +00:00
|
|
|
PRInt64 alertIntervalUSec = alertInterval * PR_USEC_PER_MSEC;
|
2003-12-01 03:49:01 +00:00
|
|
|
PRInt64 goat = PR_Now() - mStartTime;
|
|
|
|
showTaskbarAlert = goat > alertIntervalUSec;
|
2007-05-22 00:03:33 +00:00
|
|
|
|
|
|
|
PRInt32 size = mDownloadManager->mCurrentDownloads.Count();
|
|
|
|
if (showTaskbarAlert && size == 0) {
|
|
|
|
nsCOMPtr<nsIAlertsService> alerts =
|
|
|
|
do_GetService("@mozilla.org/alerts-service;1");
|
|
|
|
if (alerts) {
|
2003-12-01 03:49:01 +00:00
|
|
|
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 the items they downloaded will have
|
|
|
|
// been removed.
|
2005-03-15 19:41:41 +00:00
|
|
|
alerts->ShowAlertNotification(NS_LITERAL_STRING(DOWNLOAD_MANAGER_ALERT_ICON), title, message, !removeWhenDone,
|
2005-07-15 21:41:47 +00:00
|
|
|
EmptyString(), mDownloadManager);
|
2003-12-01 03:49:01 +00:00
|
|
|
}
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-22 00:03:33 +00:00
|
|
|
}
|
2006-07-02 20:47:53 +00:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
PRBool addToRecentDocs = PR_TRUE;
|
|
|
|
if (pref)
|
|
|
|
pref->GetBoolPref(PREF_BDM_ADDTORECENTDOCS, &addToRecentDocs);
|
|
|
|
|
|
|
|
if (addToRecentDocs) {
|
|
|
|
LPSHELLFOLDER lpShellFolder = NULL;
|
|
|
|
|
|
|
|
if (SUCCEEDED(::SHGetDesktopFolder(&lpShellFolder))) {
|
2007-05-22 00:41:11 +00:00
|
|
|
nsresult rv;
|
2007-05-22 00:34:53 +00:00
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(mTarget, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
2007-05-22 00:57:26 +00:00
|
|
|
rv = fileURL->GetFile(getter_AddRefs(file));
|
2007-05-22 00:34:53 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString path;
|
2007-05-22 00:59:50 +00:00
|
|
|
rv = file->GetPath(path);
|
2007-05-22 00:34:53 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-07-02 20:47:53 +00:00
|
|
|
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);
|
|
|
|
lpShellFolder->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
// Now remove the download if the user's retention policy is "Remove when Done"
|
2006-07-02 20:47:53 +00:00
|
|
|
if (mDownloadManager->GetRetentionBehavior() == 0)
|
2007-05-22 00:03:33 +00:00
|
|
|
mDownloadManager->RemoveDownload(mID);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
2007-05-25 23:47:53 +00:00
|
|
|
mDownloadManager->NotifyListenersOnStateChange(aWebProgress, aRequest,
|
|
|
|
aStateFlags, aStatus, this);
|
2003-11-27 00:54:33 +00:00
|
|
|
|
2007-05-23 23:12:03 +00:00
|
|
|
return UpdateDB();
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::OnSecurityChange(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest, PRUint32 aState)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIDownload
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::Init(nsIURI* aSource,
|
2004-04-17 00:58:23 +00:00
|
|
|
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,
|
2005-07-06 23:12:22 +00:00
|
|
|
nsILocalFile* aTempFile,
|
2005-04-24 21:16:28 +00:00
|
|
|
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
|
|
|
|
nsDownload::GetCancelable(nsICancelable** aCancelable)
|
|
|
|
{
|
|
|
|
*aCancelable = mCancelable;
|
|
|
|
NS_IF_ADDREF(*aCancelable);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
NS_IMETHODIMP
|
2004-04-17 00:58:23 +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
|
|
|
|
nsDownload::GetSource(nsIURI** aSource)
|
|
|
|
{
|
|
|
|
*aSource = mSource;
|
|
|
|
NS_IF_ADDREF(*aSource);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::GetStartTime(PRInt64* aStartTime)
|
|
|
|
{
|
|
|
|
*aStartTime = mStartTime;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::GetPercentComplete(PRInt32* aPercentComplete)
|
|
|
|
{
|
|
|
|
*aPercentComplete = mPercentComplete;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-12-18 04:06:51 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::GetAmountTransferred(PRUint64* aAmountTransferred)
|
|
|
|
{
|
|
|
|
*aAmountTransferred = mCurrBytes;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::GetSize(PRUint64* aSize)
|
|
|
|
{
|
|
|
|
*aSize = mMaxBytes;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-11-27 00:54:33 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::GetMIMEInfo(nsIMIMEInfo** aMIMEInfo)
|
|
|
|
{
|
|
|
|
*aMIMEInfo = mMIMEInfo;
|
|
|
|
NS_IF_ADDREF(*aMIMEInfo);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-04-17 00:58:23 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownload::GetTargetFile(nsILocalFile** aTargetFile)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
nsDownload::GetSpeed(double* aSpeed)
|
|
|
|
{
|
|
|
|
*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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDownload::PauseResume(PRBool aPause)
|
|
|
|
{
|
2007-05-23 23:12:03 +00:00
|
|
|
if (mPaused == aPause || !mRequest)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (aPause) {
|
|
|
|
nsresult rv = mRequest->Suspend();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mPaused = PR_TRUE;
|
|
|
|
return SetState(nsIDownloadManager::DOWNLOAD_PAUSED);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
2007-05-22 00:03:33 +00:00
|
|
|
|
2007-05-23 23:12:03 +00:00
|
|
|
nsresult rv = mRequest->Resume();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mPaused = PR_FALSE;
|
|
|
|
return SetState(nsIDownloadManager::DOWNLOAD_DOWNLOADING);
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
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-05-22 00:03:33 +00:00
|
|
|
// name
|
2007-07-02 17:29:59 +00:00
|
|
|
nsresult rv = stmt->BindStringParameter(0, mDisplayName);
|
2007-05-22 00:03:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// source
|
|
|
|
nsCAutoString src;
|
|
|
|
rv = mSource->GetSpec(src);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = stmt->BindUTF8StringParameter(1, src);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// target
|
|
|
|
nsCAutoString target;
|
|
|
|
rv = mTarget->GetSpec(target);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = stmt->BindUTF8StringParameter(2, target);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// startTime
|
|
|
|
rv = stmt->BindInt64Parameter(3, mStartTime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// endTime
|
|
|
|
rv = stmt->BindInt64Parameter(4, mLastUpdate);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// state
|
|
|
|
rv = stmt->BindInt32Parameter(5, mDownloadState);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// id
|
|
|
|
rv = stmt->BindInt64Parameter(6, mID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return stmt->Execute();
|
2003-11-27 00:54:33 +00:00
|
|
|
}
|