Bug 690903 - Lazily initializes nsFormHistory's SQLite DB. f=mfinkle r=dolske

This commit is contained in:
Mounir Lamouri 2011-11-01 20:23:43 +01:00
parent 3a49db6e96
commit 2a6d02c93c
3 changed files with 49 additions and 22 deletions

View File

@ -89,7 +89,6 @@ FormHistory.prototype = {
},
}
},
dbConnection : null, // The database connection
dbStmts : null, // Database statements for memoization
dbFile : null,
@ -141,26 +140,8 @@ FormHistory.prototype = {
// Add observers
Services.obs.addObserver(function() { self.expireOldEntries() }, "idle-daily", false);
Services.obs.addObserver(function() { self.expireOldEntries() }, "formhistory-expire-now", false);
try {
this.dbFile = Services.dirsvc.get("ProfD", Ci.nsIFile).clone();
this.dbFile.append("formhistory.sqlite");
this.log("Opening database at " + this.dbFile.path);
this.dbInit();
} catch (e) {
this.log("Initialization failed: " + e);
// If dbInit fails...
if (e.result == Cr.NS_ERROR_FILE_CORRUPTED) {
this.dbCleanup(true);
this.dbInit();
} else {
throw "Initialization failed";
}
}
},
/* ---- message listener ---- */
@ -377,6 +358,33 @@ FormHistory.prototype = {
},
get dbConnection() {
let connection;
// Make sure dbConnection can't be called from now to prevent infinite loops.
delete FormHistory.prototype.dbConnection;
try {
this.dbFile = Services.dirsvc.get("ProfD", Ci.nsIFile).clone();
this.dbFile.append("formhistory.sqlite");
this.log("Opening database at " + this.dbFile.path);
FormHistory.prototype.dbConnection = this.dbOpen();
this.dbInit();
} catch (e) {
this.log("Initialization failed: " + e);
// If dbInit fails...
if (e.result == Cr.NS_ERROR_FILE_CORRUPTED) {
this.dbCleanup(true);
FormHistory.prototype.dbConnection = this.dbOpen();
this.dbInit();
} else {
throw "Initialization failed";
}
}
return FormHistory.prototype.dbConnection;
},
get DBConnection() {
return this.dbConnection;
@ -591,6 +599,20 @@ FormHistory.prototype = {
return stmt;
},
/*
* dbOpen
*
* Open a connection with the database and returns it.
*
* @returns a db connection object.
*/
dbOpen : function () {
this.log("Open Database");
let storage = Cc["@mozilla.org/storage/service;1"].
getService(Ci.mozIStorageService);
return storage.openDatabase(this.dbFile);
},
/*
* dbInit
@ -601,9 +623,6 @@ FormHistory.prototype = {
dbInit : function () {
this.log("Initializing Database");
let storage = Cc["@mozilla.org/storage/service;1"].
getService(Ci.mozIStorageService);
this.dbConnection = storage.openDatabase(this.dbFile);
let version = this.dbConnection.schemaVersion;
// Note: Firefox 3 didn't set a schema value, so it started from 0.

View File

@ -63,6 +63,10 @@ function run_test()
do_check_false(bakFile.exists());
var fh = Cc["@mozilla.org/satchel/form-history;1"].
getService(Ci.nsIFormHistory2);
// DB init is done lazily so the DB shouldn't be created yet.
do_check_false(bakFile.exists());
// Doing any request to the DB should create it.
fh.DBConnection;
do_check_true(bakFile.exists());
bakFile.remove(false);

View File

@ -73,6 +73,10 @@ function run_test()
do_check_false(bakFile.exists());
var fh = Cc["@mozilla.org/satchel/form-history;1"].
getService(Ci.nsIFormHistory2);
// DB init is done lazily so the DB shouldn't be created yet.
do_check_false(bakFile.exists());
// Doing any request to the DB should create it.
fh.DBConnection;
do_check_true(bakFile.exists());
bakFile.remove(false);