storage/test/unit/vacuumParticipant.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 3 */
michael@0 4
michael@0 5 // This testing component is used in test_vacuum* tests.
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9
michael@0 10 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 11 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 12
michael@0 13 /**
michael@0 14 * Returns a new nsIFile reference for a profile database.
michael@0 15 * @param filename for the database, excluded the .sqlite extension.
michael@0 16 */
michael@0 17 function new_db_file(name)
michael@0 18 {
michael@0 19 let file = Services.dirsvc.get("ProfD", Ci.nsIFile);
michael@0 20 file.append(name + ".sqlite");
michael@0 21 return file;
michael@0 22 }
michael@0 23
michael@0 24 /**
michael@0 25 * Opens and returns a connection to the provided database file.
michael@0 26 * @param nsIFile interface to the database file.
michael@0 27 */
michael@0 28 function getDatabase(aFile)
michael@0 29 {
michael@0 30 return Cc["@mozilla.org/storage/service;1"].getService(Ci.mozIStorageService)
michael@0 31 .openDatabase(aFile);
michael@0 32 }
michael@0 33
michael@0 34 function vacuumParticipant()
michael@0 35 {
michael@0 36 this._dbConn = getDatabase(new_db_file("testVacuum"));
michael@0 37 Services.obs.addObserver(this, "test-options", false);
michael@0 38 }
michael@0 39
michael@0 40 vacuumParticipant.prototype =
michael@0 41 {
michael@0 42 classDescription: "vacuumParticipant",
michael@0 43 classID: Components.ID("{52aa0b22-b82f-4e38-992a-c3675a3355d2}"),
michael@0 44 contractID: "@unit.test.com/test-vacuum-participant;1",
michael@0 45
michael@0 46 get expectedDatabasePageSize() this._dbConn.defaultPageSize,
michael@0 47 get databaseConnection() this._dbConn,
michael@0 48
michael@0 49 _grant: true,
michael@0 50 onBeginVacuum: function TVP_onBeginVacuum()
michael@0 51 {
michael@0 52 if (!this._grant) {
michael@0 53 this._grant = true;
michael@0 54 return false;
michael@0 55 }
michael@0 56 Services.obs.notifyObservers(null, "test-begin-vacuum", null);
michael@0 57 return true;
michael@0 58 },
michael@0 59 onEndVacuum: function TVP_EndVacuum(aSucceeded)
michael@0 60 {
michael@0 61 if (this._stmt) {
michael@0 62 this._stmt.finalize();
michael@0 63 }
michael@0 64 Services.obs.notifyObservers(null, "test-end-vacuum", aSucceeded);
michael@0 65 },
michael@0 66
michael@0 67 observe: function TVP_observe(aSubject, aTopic, aData)
michael@0 68 {
michael@0 69 if (aData == "opt-out") {
michael@0 70 this._grant = false;
michael@0 71 }
michael@0 72 else if (aData == "wal") {
michael@0 73 try {
michael@0 74 this._dbConn.close();
michael@0 75 }
michael@0 76 catch(e) {}
michael@0 77 this._dbConn = getDatabase(new_db_file("testVacuum2"));
michael@0 78 }
michael@0 79 else if (aData == "wal-fail") {
michael@0 80 try {
michael@0 81 this._dbConn.close();
michael@0 82 }
michael@0 83 catch(e) {}
michael@0 84 this._dbConn = getDatabase(new_db_file("testVacuum3"));
michael@0 85 // Use WAL journal mode.
michael@0 86 this._dbConn.executeSimpleSQL("PRAGMA journal_mode = WAL");
michael@0 87 // Create a not finalized statement.
michael@0 88 this._stmt = this._dbConn.createStatement("SELECT :test");
michael@0 89 this._stmt.params.test = 1;
michael@0 90 this._stmt.executeStep();
michael@0 91 }
michael@0 92 else if (aData == "memory") {
michael@0 93 try {
michael@0 94 this._dbConn.asyncClose();
michael@0 95 }
michael@0 96 catch(e) {}
michael@0 97 this._dbConn = Cc["@mozilla.org/storage/service;1"].
michael@0 98 getService(Ci.mozIStorageService).
michael@0 99 openSpecialDatabase("memory");
michael@0 100 }
michael@0 101 else if (aData == "dispose") {
michael@0 102 Services.obs.removeObserver(this, "test-options");
michael@0 103 try {
michael@0 104 this._dbConn.asyncClose();
michael@0 105 }
michael@0 106 catch(e) {}
michael@0 107 }
michael@0 108 },
michael@0 109
michael@0 110 QueryInterface: XPCOMUtils.generateQI([
michael@0 111 Ci.mozIStorageVacuumParticipant
michael@0 112 , Ci.nsIObserver
michael@0 113 ])
michael@0 114 };
michael@0 115
michael@0 116 let gComponentsArray = [vacuumParticipant];
michael@0 117 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray);

mercurial