michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: // This testing component is used in test_vacuum* tests. michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: /** michael@0: * Returns a new nsIFile reference for a profile database. michael@0: * @param filename for the database, excluded the .sqlite extension. michael@0: */ michael@0: function new_db_file(name) michael@0: { michael@0: let file = Services.dirsvc.get("ProfD", Ci.nsIFile); michael@0: file.append(name + ".sqlite"); michael@0: return file; michael@0: } michael@0: michael@0: /** michael@0: * Opens and returns a connection to the provided database file. michael@0: * @param nsIFile interface to the database file. michael@0: */ michael@0: function getDatabase(aFile) michael@0: { michael@0: return Cc["@mozilla.org/storage/service;1"].getService(Ci.mozIStorageService) michael@0: .openDatabase(aFile); michael@0: } michael@0: michael@0: function vacuumParticipant() michael@0: { michael@0: this._dbConn = getDatabase(new_db_file("testVacuum")); michael@0: Services.obs.addObserver(this, "test-options", false); michael@0: } michael@0: michael@0: vacuumParticipant.prototype = michael@0: { michael@0: classDescription: "vacuumParticipant", michael@0: classID: Components.ID("{52aa0b22-b82f-4e38-992a-c3675a3355d2}"), michael@0: contractID: "@unit.test.com/test-vacuum-participant;1", michael@0: michael@0: get expectedDatabasePageSize() this._dbConn.defaultPageSize, michael@0: get databaseConnection() this._dbConn, michael@0: michael@0: _grant: true, michael@0: onBeginVacuum: function TVP_onBeginVacuum() michael@0: { michael@0: if (!this._grant) { michael@0: this._grant = true; michael@0: return false; michael@0: } michael@0: Services.obs.notifyObservers(null, "test-begin-vacuum", null); michael@0: return true; michael@0: }, michael@0: onEndVacuum: function TVP_EndVacuum(aSucceeded) michael@0: { michael@0: if (this._stmt) { michael@0: this._stmt.finalize(); michael@0: } michael@0: Services.obs.notifyObservers(null, "test-end-vacuum", aSucceeded); michael@0: }, michael@0: michael@0: observe: function TVP_observe(aSubject, aTopic, aData) michael@0: { michael@0: if (aData == "opt-out") { michael@0: this._grant = false; michael@0: } michael@0: else if (aData == "wal") { michael@0: try { michael@0: this._dbConn.close(); michael@0: } michael@0: catch(e) {} michael@0: this._dbConn = getDatabase(new_db_file("testVacuum2")); michael@0: } michael@0: else if (aData == "wal-fail") { michael@0: try { michael@0: this._dbConn.close(); michael@0: } michael@0: catch(e) {} michael@0: this._dbConn = getDatabase(new_db_file("testVacuum3")); michael@0: // Use WAL journal mode. michael@0: this._dbConn.executeSimpleSQL("PRAGMA journal_mode = WAL"); michael@0: // Create a not finalized statement. michael@0: this._stmt = this._dbConn.createStatement("SELECT :test"); michael@0: this._stmt.params.test = 1; michael@0: this._stmt.executeStep(); michael@0: } michael@0: else if (aData == "memory") { michael@0: try { michael@0: this._dbConn.asyncClose(); michael@0: } michael@0: catch(e) {} michael@0: this._dbConn = Cc["@mozilla.org/storage/service;1"]. michael@0: getService(Ci.mozIStorageService). michael@0: openSpecialDatabase("memory"); michael@0: } michael@0: else if (aData == "dispose") { michael@0: Services.obs.removeObserver(this, "test-options"); michael@0: try { michael@0: this._dbConn.asyncClose(); michael@0: } michael@0: catch(e) {} michael@0: } michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.mozIStorageVacuumParticipant michael@0: , Ci.nsIObserver michael@0: ]) michael@0: }; michael@0: michael@0: let gComponentsArray = [vacuumParticipant]; michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray);