gecko/dom/base/IndexedDBHelper.jsm
Ryan VanderMeulen 1f6d0efee0 Backed out 10 changesets (bug 914944, bug 871445) for mochitest failures on a CLOSED TREE.
Backed out changeset a840638b5b18 (bug 914944)
Backed out changeset f40c7d5790bd (bug 914944)
Backed out changeset e1b288e29663 (bug 871445)
Backed out changeset 6242ddf7b6c7 (bug 871445)
Backed out changeset 7679185a8cf3 (bug 871445)
Backed out changeset 639ec7a627f8 (bug 871445)
Backed out changeset 93b050a79db4 (bug 871445)
Backed out changeset ec3382ceef99 (bug 871445)
Backed out changeset 51c0d5230306 (bug 871445)
Backed out changeset 76c9069bdb56 (bug 871445)
2013-09-11 11:45:51 -04:00

163 lines
4.9 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
let DEBUG = 0;
let debug;
if (DEBUG) {
debug = function (s) { dump("-*- IndexedDBHelper: " + s + "\n"); }
} else {
debug = function (s) {}
}
const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
this.EXPORTED_SYMBOLS = ["IndexedDBHelper"];
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
this.IndexedDBHelper = function IndexedDBHelper() {}
IndexedDBHelper.prototype = {
// Cache the database
_db: null,
// Close the database
close: function close() {
if (this._db) {
this._db.close();
this._db = null;
}
},
/**
* Open a new database.
* User has to provide upgradeSchema.
*
* @param successCb
* Success callback to call once database is open.
* @param failureCb
* Error callback to call when an error is encountered.
*/
open: function open(aSuccessCb, aFailureCb) {
let self = this;
if (DEBUG) debug("Try to open database:" + self.dbName + " " + self.dbVersion);
let req = this.dbGlobal.indexedDB.open(this.dbName, this.dbVersion);
req.onsuccess = function (event) {
if (DEBUG) debug("Opened database:" + self.dbName + " " + self.dbVersion);
self._db = event.target.result;
self._db.onversionchange = function(event) {
if (DEBUG) debug("WARNING: DB modified from a different window.");
}
aSuccessCb();
};
req.onupgradeneeded = function (aEvent) {
if (DEBUG) {
debug("Database needs upgrade:" + self.dbName + aEvent.oldVersion + aEvent.newVersion);
debug("Correct new database version:" + (aEvent.newVersion == this.dbVersion));
}
let _db = aEvent.target.result;
self.upgradeSchema(req.transaction, _db, aEvent.oldVersion, aEvent.newVersion);
};
req.onerror = function (aEvent) {
if (DEBUG) debug("Failed to open database: " + self.dbName);
aFailureCb(aEvent.target.error.name);
};
req.onblocked = function (aEvent) {
if (DEBUG) debug("Opening database request is blocked.");
};
},
/**
* Use the cached DB or open a new one.
*
* @param successCb
* Success callback to call.
* @param failureCb
* Error callback to call when an error is encountered.
*/
ensureDB: function ensureDB(aSuccessCb, aFailureCb) {
if (this._db) {
if (DEBUG) debug("ensureDB: already have a database, returning early.");
aSuccessCb();
return;
}
this.open(aSuccessCb, aFailureCb);
},
/**
* Start a new transaction.
*
* @param txn_type
* Type of transaction (e.g. "readwrite")
* @param store_name
* The object store you want to be passed to the callback
* @param callback
* Function to call when the transaction is available. It will
* be invoked with the transaction and the `store' object store.
* @param successCb
* Success callback to call on a successful transaction commit.
* The result is stored in txn.result.
* @param failureCb
* Error callback to call when an error is encountered.
*/
newTxn: function newTxn(txn_type, store_name, callback, successCb, failureCb) {
this.ensureDB(function () {
if (DEBUG) debug("Starting new transaction" + txn_type);
let txn = this._db.transaction(this.dbStoreNames, txn_type);
if (DEBUG) debug("Retrieving object store", this.dbName);
let store = txn.objectStore(store_name);
txn.oncomplete = function (event) {
if (DEBUG) debug("Transaction complete. Returning to callback.");
if (successCb) {
successCb(txn.result);
}
};
txn.onabort = function (event) {
if (DEBUG) debug("Caught error on transaction");
/*
* event.target.error may be null
* if txn was aborted by calling txn.abort()
*/
if (failureCb) {
if (event.target.error) {
failureCb(event.target.error.name);
} else {
failureCb("UnknownError");
}
}
};
callback(txn, store);
}.bind(this), failureCb);
},
/**
* Initialize the DB. Does not call open.
*
* @param aDBName
* DB name for the open call.
* @param aDBVersion
* Current DB version. User has to implement upgradeSchema.
* @param aDBStoreName
* ObjectStore that is used.
* @param aGlobal
* Global object that has indexedDB property.
*/
initDBHelper: function initDBHelper(aDBName, aDBVersion, aDBStoreNames, aGlobal) {
this.dbName = aDBName;
this.dbVersion = aDBVersion;
this.dbStoreNames = aDBStoreNames;
this.dbGlobal = aGlobal;
}
}