Bug 463882 - nsDownloadManager::Init is not safe for multiple calls; r=sdwilsh a191=beltzner

This commit is contained in:
Ehsan Akhgari 2008-11-28 12:53:57 -08:00
Родитель 20237b4a4a
Коммит 3d2d31045a
2 изменённых файлов: 43 добавлений и 20 удалений

Просмотреть файл

@ -940,13 +940,11 @@ nsDownloadManager::AddDownloadToDB(const nsAString &aName,
}
nsresult
nsDownloadManager::Init()
nsDownloadManager::InitDB()
{
nsresult rv;
mObserverService = do_GetService("@mozilla.org/observer-service;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsresult rv = NS_OK;
PRBool doImport = PR_FALSE;
switch (mDBType) {
case DATABASE_MEMORY:
rv = InitMemoryDB();
@ -965,10 +963,37 @@ nsDownloadManager::Init()
if (doImport)
ImportDownloadHistory();
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
"UPDATE moz_downloads "
"SET tempPath = ?1, startTime = ?2, endTime = ?3, state = ?4, "
"referrer = ?5, entityID = ?6, currBytes = ?7, maxBytes = ?8, "
"autoResume = ?9 "
"WHERE id = ?10"), getter_AddRefs(mUpdateDownloadStatement));
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
"SELECT id "
"FROM moz_downloads "
"WHERE source = ?1"), getter_AddRefs(mGetIdsForURIStatement));
NS_ENSURE_SUCCESS(rv, rv);
return rv;
}
nsresult
nsDownloadManager::Init()
{
nsresult rv;
mObserverService = do_GetService("@mozilla.org/observer-service;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIStringBundleService> bundleService =
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = InitDB();
NS_ENSURE_SUCCESS(rv, rv);
rv = bundleService->CreateBundle(DOWNLOAD_MANAGER_BUNDLE,
getter_AddRefs(mBundle));
NS_ENSURE_SUCCESS(rv, rv);
@ -982,20 +1007,6 @@ nsDownloadManager::Init()
mScanner = nsnull;
#endif
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
"UPDATE moz_downloads "
"SET tempPath = ?1, startTime = ?2, endTime = ?3, state = ?4, "
"referrer = ?5, entityID = ?6, currBytes = ?7, maxBytes = ?8, "
"autoResume = ?9 "
"WHERE id = ?10"), getter_AddRefs(mUpdateDownloadStatement));
NS_ENSURE_SUCCESS(rv, rv);
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
"SELECT id "
"FROM moz_downloads "
"WHERE source = ?1"), getter_AddRefs(mGetIdsForURIStatement));
NS_ENSURE_SUCCESS(rv, rv);
// Do things *after* initializing various download manager properties such as
// restoring downloads to a consistent state
rv = RestoreDatabaseState();
@ -1903,7 +1914,18 @@ nsDownloadManager::SwitchDatabaseTypeTo(enum nsDownloadManager::DatabaseType aTy
(void)PauseAllDownloads(PR_TRUE);
(void)RemoveAllDownloads();
return Init();
nsresult rv = InitDB();
NS_ENSURE_SUCCESS(rv, rv);
// 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_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Failed to restore all active downloads");
return rv;
}
////////////////////////////////////////////////////////////////////////////////

Просмотреть файл

@ -107,6 +107,7 @@ protected:
DATABASE_MEMORY
};
nsresult InitDB();
nsresult InitFileDB(PRBool *aDoImport);
nsresult InitMemoryDB();
already_AddRefed<mozIStorageConnection> GetFileDBConnection(nsIFile *dbFile) const;