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.

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

mercurial