dom/base/IndexedDBHelper.jsm

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 let DEBUG = 0;
michael@0 8 let debug;
michael@0 9 if (DEBUG) {
michael@0 10 debug = function (s) { dump("-*- IndexedDBHelper: " + s + "\n"); }
michael@0 11 } else {
michael@0 12 debug = function (s) {}
michael@0 13 }
michael@0 14
michael@0 15 const Cu = Components.utils;
michael@0 16 const Cc = Components.classes;
michael@0 17 const Ci = Components.interfaces;
michael@0 18
michael@0 19 this.EXPORTED_SYMBOLS = ["IndexedDBHelper"];
michael@0 20
michael@0 21 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 22 Cu.import("resource://gre/modules/Services.jsm");
michael@0 23 Cu.importGlobalProperties(["indexedDB"]);
michael@0 24
michael@0 25 this.IndexedDBHelper = function IndexedDBHelper() {}
michael@0 26
michael@0 27 IndexedDBHelper.prototype = {
michael@0 28
michael@0 29 // Cache the database
michael@0 30 _db: null,
michael@0 31
michael@0 32 // Close the database
michael@0 33 close: function close() {
michael@0 34 if (this._db) {
michael@0 35 this._db.close();
michael@0 36 this._db = null;
michael@0 37 }
michael@0 38 },
michael@0 39
michael@0 40 /**
michael@0 41 * Open a new database.
michael@0 42 * User has to provide upgradeSchema.
michael@0 43 *
michael@0 44 * @param successCb
michael@0 45 * Success callback to call once database is open.
michael@0 46 * @param failureCb
michael@0 47 * Error callback to call when an error is encountered.
michael@0 48 */
michael@0 49 open: function open(aSuccessCb, aFailureCb) {
michael@0 50 let self = this;
michael@0 51 if (DEBUG) debug("Try to open database:" + self.dbName + " " + self.dbVersion);
michael@0 52 let req = indexedDB.open(this.dbName, this.dbVersion);
michael@0 53 req.onsuccess = function (event) {
michael@0 54 if (DEBUG) debug("Opened database:" + self.dbName + " " + self.dbVersion);
michael@0 55 self._db = event.target.result;
michael@0 56 self._db.onversionchange = function(event) {
michael@0 57 if (DEBUG) debug("WARNING: DB modified from a different window.");
michael@0 58 }
michael@0 59 aSuccessCb && aSuccessCb();
michael@0 60 };
michael@0 61
michael@0 62 req.onupgradeneeded = function (aEvent) {
michael@0 63 if (DEBUG) {
michael@0 64 debug("Database needs upgrade:" + self.dbName + aEvent.oldVersion + aEvent.newVersion);
michael@0 65 debug("Correct new database version:" + (aEvent.newVersion == this.dbVersion));
michael@0 66 }
michael@0 67
michael@0 68 let _db = aEvent.target.result;
michael@0 69 self.upgradeSchema(req.transaction, _db, aEvent.oldVersion, aEvent.newVersion);
michael@0 70 };
michael@0 71 req.onerror = function (aEvent) {
michael@0 72 if (DEBUG) debug("Failed to open database: " + self.dbName);
michael@0 73 aFailureCb && aFailureCb(aEvent.target.error.name);
michael@0 74 };
michael@0 75 req.onblocked = function (aEvent) {
michael@0 76 if (DEBUG) debug("Opening database request is blocked.");
michael@0 77 };
michael@0 78 },
michael@0 79
michael@0 80 /**
michael@0 81 * Use the cached DB or open a new one.
michael@0 82 *
michael@0 83 * @param successCb
michael@0 84 * Success callback to call.
michael@0 85 * @param failureCb
michael@0 86 * Error callback to call when an error is encountered.
michael@0 87 */
michael@0 88 ensureDB: function ensureDB(aSuccessCb, aFailureCb) {
michael@0 89 if (this._db) {
michael@0 90 if (DEBUG) debug("ensureDB: already have a database, returning early.");
michael@0 91 aSuccessCb && aSuccessCb();
michael@0 92 return;
michael@0 93 }
michael@0 94 this.open(aSuccessCb, aFailureCb);
michael@0 95 },
michael@0 96
michael@0 97 /**
michael@0 98 * Start a new transaction.
michael@0 99 *
michael@0 100 * @param txn_type
michael@0 101 * Type of transaction (e.g. "readwrite")
michael@0 102 * @param store_name
michael@0 103 * The object store you want to be passed to the callback
michael@0 104 * @param callback
michael@0 105 * Function to call when the transaction is available. It will
michael@0 106 * be invoked with the transaction and the `store' object store.
michael@0 107 * @param successCb
michael@0 108 * Success callback to call on a successful transaction commit.
michael@0 109 * The result is stored in txn.result.
michael@0 110 * @param failureCb
michael@0 111 * Error callback to call when an error is encountered.
michael@0 112 */
michael@0 113 newTxn: function newTxn(txn_type, store_name, callback, successCb, failureCb) {
michael@0 114 this.ensureDB(function () {
michael@0 115 if (DEBUG) debug("Starting new transaction" + txn_type);
michael@0 116 let txn = this._db.transaction(Array.isArray(store_name) ? store_name : this.dbStoreNames, txn_type);
michael@0 117 if (DEBUG) debug("Retrieving object store", this.dbName);
michael@0 118 let stores;
michael@0 119 if (Array.isArray(store_name)) {
michael@0 120 stores = [];
michael@0 121 for (let i = 0; i < store_name.length; ++i) {
michael@0 122 stores.push(txn.objectStore(store_name[i]));
michael@0 123 }
michael@0 124 } else {
michael@0 125 stores = txn.objectStore(store_name);
michael@0 126 }
michael@0 127
michael@0 128 txn.oncomplete = function (event) {
michael@0 129 if (DEBUG) debug("Transaction complete. Returning to callback.");
michael@0 130 if (successCb) {
michael@0 131 successCb(txn.result);
michael@0 132 }
michael@0 133 };
michael@0 134
michael@0 135 txn.onabort = function (event) {
michael@0 136 if (DEBUG) debug("Caught error on transaction");
michael@0 137 /*
michael@0 138 * event.target.error may be null
michael@0 139 * if txn was aborted by calling txn.abort()
michael@0 140 */
michael@0 141 if (failureCb) {
michael@0 142 if (event.target.error) {
michael@0 143 failureCb(event.target.error.name);
michael@0 144 } else {
michael@0 145 failureCb("UnknownError");
michael@0 146 }
michael@0 147 }
michael@0 148 };
michael@0 149 callback(txn, stores);
michael@0 150 }.bind(this), failureCb);
michael@0 151 },
michael@0 152
michael@0 153 /**
michael@0 154 * Initialize the DB. Does not call open.
michael@0 155 *
michael@0 156 * @param aDBName
michael@0 157 * DB name for the open call.
michael@0 158 * @param aDBVersion
michael@0 159 * Current DB version. User has to implement upgradeSchema.
michael@0 160 * @param aDBStoreName
michael@0 161 * ObjectStore that is used.
michael@0 162 */
michael@0 163 initDBHelper: function initDBHelper(aDBName, aDBVersion, aDBStoreNames) {
michael@0 164 this.dbName = aDBName;
michael@0 165 this.dbVersion = aDBVersion;
michael@0 166 this.dbStoreNames = aDBStoreNames;
michael@0 167 }
michael@0 168 }

mercurial