Bug 385875 - Remove image url from being stored in the DM. r=mano

This commit is contained in:
sdwilsh@shawnwilsher.com 2007-07-22 10:24:03 -07:00
parent 0547b616b0
commit 9f74d0d3bc
10 changed files with 263 additions and 34 deletions

View File

@ -51,7 +51,7 @@ interface nsIDownloadProgressListener;
interface nsISimpleEnumerator;
interface mozIStorageConnection;
[scriptable, uuid(bbf6561b-d7d7-48fa-967c-9cc46cc372b4)]
[scriptable, uuid(0e8ded0d-c6e4-4b67-9d1e-42fe3d405491)]
interface nsIDownloadManager : nsISupports {
// Download States
const short DOWNLOAD_NOTSTARTED = -1;
@ -99,7 +99,6 @@ interface nsIDownloadManager : nsISupports {
in nsIURI aSource,
in nsIURI aTarget,
in AString aDisplayName,
in AString aIconURL,
in nsIMIMEInfo aMIMEInfo,
in PRTime aStartTime,
in nsILocalFile aTempFile,

View File

@ -88,7 +88,7 @@ static PRBool gStoppingDownloads = PR_FALSE;
static const PRInt64 gUpdateInterval = 400 * PR_USEC_PER_MSEC;
#define DM_SCHEMA_VERSION 1
#define DM_SCHEMA_VERSION 2
#define DM_DB_NAME NS_LITERAL_STRING("downloads.sqlite")
#define DM_DB_CORRUPT_FILENAME NS_LITERAL_STRING("downloads.sqlite.corrupt")
@ -256,7 +256,68 @@ nsDownloadManager::InitDB(PRBool *aDoImport)
// 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 upgradeing code!
}
if (schemaVersion < 2) {
// This version simple drops a column from the database (iconURL)
// Safely wrap this in a transaction so we don't hose the whole DB
mozStorageTransaction safeTransaction(mDBConn, PR_TRUE);
// Create a temporary table that we'll 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);
} // End of schema #2 upgrade code
} // End of upgrading
}
}
@ -271,8 +332,14 @@ nsDownloadManager::CreateTable()
return mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
"CREATE TABLE moz_downloads ("
"id INTEGER PRIMARY KEY, name TEXT, source TEXT, target TEXT,"
"iconURL TEXT, startTime INTEGER, endTime INTEGER, state INTEGER)"));
"id INTEGER PRIMARY KEY, "
"name TEXT, "
"source TEXT, "
"target TEXT, "
"startTime INTEGER, "
"endTime INTEGER, "
"state INTEGER"
")"));
}
nsresult
@ -407,8 +474,7 @@ nsDownloadManager::ImportDownloadHistory()
rv = rdfInt->GetValue(&state);
if (NS_FAILED(rv)) continue;
(void)AddDownloadToDB(name, source, target, EmptyString(), startTime,
endTime, state);
(void)AddDownloadToDB(name, source, target, startTime, endTime, state);
}
return NS_OK;
@ -418,7 +484,6 @@ PRInt64
nsDownloadManager::AddDownloadToDB(const nsAString &aName,
const nsACString &aSource,
const nsACString &aTarget,
const nsAString &aIconURL,
PRInt64 aStartTime,
PRInt64 aEndTime,
PRInt32 aState)
@ -426,8 +491,8 @@ nsDownloadManager::AddDownloadToDB(const nsAString &aName,
nsCOMPtr<mozIStorageStatement> stmt;
nsresult rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
"INSERT INTO moz_downloads "
"(name, source, target, iconURL, startTime, endTime, state) "
"VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)"), getter_AddRefs(stmt));
"(name, source, target, startTime, endTime, state) "
"VALUES (?1, ?2, ?3, ?4, ?5, ?6)"), getter_AddRefs(stmt));
NS_ENSURE_SUCCESS(rv, 0);
// name
@ -442,20 +507,16 @@ nsDownloadManager::AddDownloadToDB(const nsAString &aName,
rv = stmt->BindUTF8StringParameter(2, aTarget);
NS_ENSURE_SUCCESS(rv, 0);
// iconURL
rv = stmt->BindStringParameter(3, aIconURL);
NS_ENSURE_SUCCESS(rv, 0);
// startTime
rv = stmt->BindInt64Parameter(4, aStartTime);
rv = stmt->BindInt64Parameter(3, aStartTime);
NS_ENSURE_SUCCESS(rv, 0);
// endTime
rv = stmt->BindInt64Parameter(5, aEndTime);
rv = stmt->BindInt64Parameter(4, aEndTime);
NS_ENSURE_SUCCESS(rv, 0);
// state
rv = stmt->BindInt32Parameter(6, aState);
rv = stmt->BindInt32Parameter(5, aState);
NS_ENSURE_SUCCESS(rv, 0);
PRBool hasMore;
@ -626,7 +687,6 @@ nsDownloadManager::AddDownload(DownloadType aDownloadType,
nsIURI* aSource,
nsIURI* aTarget,
const nsAString& aDisplayName,
const nsAString& aIconURL,
nsIMIMEInfo *aMIMEInfo,
PRTime aStartTime,
nsILocalFile* aTempFile,
@ -672,8 +732,7 @@ nsDownloadManager::AddDownload(DownloadType aDownloadType,
aSource->GetSpec(source);
aTarget->GetSpec(target);
PRInt64 id = AddDownloadToDB(dl->mDisplayName, source, target, aIconURL,
aStartTime, 0,
PRInt64 id = AddDownloadToDB(dl->mDisplayName, source, target, aStartTime, 0,
nsIDownloadManager::DOWNLOAD_NOTSTARTED);
NS_ENSURE_TRUE(id, NS_ERROR_FAILURE);
dl->mID = id;

View File

@ -114,7 +114,6 @@ protected:
PRInt64 AddDownloadToDB(const nsAString &aName,
const nsACString &aSource,
const nsACString &aTarget,
const nsAString &aIconURL,
PRInt64 aStartTime,
PRInt64 aEndTime,
PRInt32 aState);

View File

@ -69,9 +69,8 @@ public:
NS_ENSURE_SUCCESS(rv, rv);
rv = dm->AddDownload(nsIDownloadManager::DOWNLOAD_TYPE_DOWNLOAD, aSource,
aTarget, aDisplayName, EmptyString(), aMIMEInfo,
aStartTime, aTempFile, aCancelable,
getter_AddRefs(mInner));
aTarget, aDisplayName, aMIMEInfo, aStartTime,
aTempFile, aCancelable, getter_AddRefs(mInner));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIPrefService> prefs = do_GetService("@mozilla.org/preferences-service;1", &rv);

View File

@ -45,7 +45,10 @@ include $(DEPTH)/config/autoconf.mk
MODULE = test_dm
XPCSHELL_TESTS = unit
XPCSHELL_TESTS = \
unit \
schema_migration \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,91 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Download Manager Test Code.
*
* The Initial Developer of the Original Code is
* Shawn Wilsher <me@shawnwilsher.com>.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by devaring the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not devare
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
var profileDir = null;
try {
profileDir = dirSvc.get("ProfD", Ci.nsIFile);
} catch (e) { }
if (!profileDir) {
// Register our own provider for the profile directory.
// It will simply return the current directory.
var provider = {
getFile: function(prop, persistent) {
persistent.value = true;
if (prop == "ProfD") {
return dirSvc.get("CurProcD", Ci.nsILocalFile);
} else if (prop == "DLoads") {
var file = dirSvc.get("CurProcD", Ci.nsILocalFile);
file.append("downloads.rdf");
return file;
}
print("*** Throwing trying to get " + prop);
throw Cr.NS_ERROR_FAILURE;
},
QueryInterface: function(iid) {
if (iid.equals(Ci.nsIDirectoryProvider) ||
iid.equals(Ci.nsISupports)) {
return this;
}
throw Cr.NS_ERROR_NO_INTERFACE;
}
};
dirSvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider);
}
function importDatabaseFile(aFName)
{
var file = do_get_file("toolkit/components/downloads/test/schema_migration/" + aFName);
var newFile = dirSvc.get("ProfD", Ci.nsIFile);
file.copyTo(newFile, "downloads.sqlite");
}
function cleanup()
{
// removing database
var dbFile = dirSvc.get("ProfD", Ci.nsIFile);
dbFile.append("downloads.sqlite");
if (dbFile.exists())
try { dbFile.remove(true); } catch(e) { /* stupid windows box */ }
}
cleanup();

View File

@ -0,0 +1,81 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Download Manager Test Code.
*
* The Initial Developer of the Original Code is
* Shawn Wilsher <me@shawnwilsher.com>.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by devaring the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not devare
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// This file tests migration from v1 to v2
function run_test()
{
// First import the downloads.sqlite file
importDatabaseFile("v1.sqlite");
// ok, now it is OK to init the download manager - this will perform the
// migration!
var dm = Cc["@mozilla.org/download-manager;1"].
getService(Ci.nsIDownloadManager);
var dbConn = dm.DBConnection;
var stmt = null;
// check schema version
do_check_eq(2, dbConn.schemaVersion);
// Check that the column no longer exists
try {
// throws when it doesn't exist
stmt = dbConn.createStatement("SELECT iconURL FROM moz_downloads");
do_throw("should not get here");
} catch (e) {
do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
}
// now we check the entries
stmt = dbConn.createStatement(
"SELECT name, source, target, startTime, endTime, state " +
"FROM moz_downloads " +
"WHERE id = 2");
stmt.executeStep();
do_check_eq("381603.patch", stmt.getString(0));
do_check_eq("https://bugzilla.mozilla.org/attachment.cgi?id=266520",
stmt.getUTF8String(1));
do_check_eq("file:///Users/sdwilsh/Desktop/381603.patch",
stmt.getUTF8String(2));
do_check_eq(1180493839859230, stmt.getInt64(3));
do_check_eq(1180493839859230, stmt.getInt64(4));
do_check_eq(1, stmt.getInt32(5));
stmt.reset();
cleanup();
}

View File

@ -122,7 +122,7 @@ function addDownload()
var dl = dm.addDownload(nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
createURI("http://localhost:4444/LICENSE"),
createURI(destFile), null, null, null,
createURI(destFile), null, null,
Math.round(Date.now() * 1000), null, persist);
// This will throw if it isn't found, and that would mean test failure, so no

View File

@ -73,7 +73,7 @@ function test_count_entries()
function test_download_data()
{
var stmt = dm.DBConnection.createStatement("SELECT COUNT(*), source, target," +
"iconURL, state, startTime," +
"state, startTime," +
"endTime " +
"FROM moz_downloads " +
"WHERE name = ?1");
@ -83,11 +83,10 @@ function test_download_data()
do_check_eq(1, stmt.getInt32(0));
do_check_eq("http://www.google.com/", stmt.getUTF8String(1));
do_check_eq("file:///Users/jruderman/Desktop/download/Google.htm", stmt.getUTF8String(2));
do_check_eq("", stmt.getString(3));
do_check_eq(1, stmt.getInt32(4));
do_check_eq(1, stmt.getInt32(3));
// Actual check of dates being the same
do_check_eq(stmt.getInt64(5), stmt.getInt64(6));
do_check_eq(stmt.getInt64(4), stmt.getInt64(5));
stmt.reset();
}

View File

@ -58,7 +58,7 @@ function test_count_entries()
function test_random_download()
{
var stmt = dm.DBConnection.createStatement("SELECT COUNT(*), source, target," +
" iconURL, state " +
"state " +
"FROM moz_downloads " +
"WHERE name = ?1");
stmt.bindStringParameter(0, "Firefox 2.0.0.3.dmg");
@ -67,8 +67,7 @@ function test_random_download()
do_check_eq(1, stmt.getInt32(0));
do_check_eq("http://ftp-mozilla.netscape.com/pub/mozilla.org/firefox/releases/2.0.0.3/mac/en-US/Firefox%202.0.0.3.dmg", stmt.getUTF8String(1));
do_check_eq("file:///Users/sdwilsh/Desktop/Firefox 2.0.0.3.dmg", stmt.getUTF8String(2));
do_check_eq("", stmt.getString(3));
do_check_eq(1, stmt.getInt32(4));
do_check_eq(1, stmt.getInt32(3));
stmt.reset();
}