// Copyright 2011 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Wrappers for the HTML5 IndexedDB. The wrappers export nearly16* the same interface as the standard API, but return goog.async.Deferred17* objects instead of request objects and use Closure events. The wrapper works18* and has been tested on Chrome version 22+. It may work on older Chrome19* versions, but they aren't explicitly supported.20*21* Example usage:22*23* <code>24* goog.db.openDatabase('mydb', 1, function(ev, db, tx) {25* db.createObjectStore('mystore');26* }).addCallback(function(db) {27* var putTx = db.createTransaction(28* [],29* goog.db.Transaction.TransactionMode.READ_WRITE);30* var store = putTx.objectStore('mystore');31* store.put('value', 'key');32* goog.listen(putTx, goog.db.Transaction.EventTypes.COMPLETE, function() {33* var getTx = db.createTransaction([]);34* var request = getTx.objectStore('mystore').get('key');35* request.addCallback(function(result) {36* ...37* });38* });39* </code>40*41*/424344goog.provide('goog.db');45goog.provide('goog.db.BlockedCallback');46goog.provide('goog.db.UpgradeNeededCallback');4748goog.require('goog.asserts');49goog.require('goog.async.Deferred');50goog.require('goog.db.Error');51goog.require('goog.db.IndexedDb');52goog.require('goog.db.Transaction');535455/**56* The IndexedDB factory object.57*58* @type {!IDBFactory|undefined}59* @private60*/61goog.db.indexedDb_ = goog.global.indexedDB || goog.global.mozIndexedDB ||62goog.global.webkitIndexedDB || goog.global.moz_indexedDB;636465/**66* A callback that's called if a blocked event is received. When a database is67* supposed to be deleted or upgraded (i.e. versionchange), and there are open68* connections to this database, a block event will be fired to prevent the69* operations from going through until all such open connections are closed.70* This callback can be used to notify users that they should close other tabs71* that have open connections, or to close the connections manually. Databases72* can also listen for the {@link goog.db.IndexedDb.EventType.VERSION_CHANGE}73* event to automatically close themselves when they're blocking such74* operations.75*76* This is passed a VersionChangeEvent that has the version of the database77* before it was deleted, and "null" as the new version.78*79* @typedef {function(!goog.db.IndexedDb.VersionChangeEvent)}80*/81goog.db.BlockedCallback;828384/**85* A callback that's called when opening a database whose internal version is86* lower than the version passed to {@link goog.db.openDatabase}.87*88* This callback is passed three arguments: a VersionChangeEvent with both the89* old version and the new version of the database; the database that's being90* opened, for which you can create and delete object stores; and the version91* change transaction, with which you can abort the version change.92*93* Note that the transaction is not active, which means that it can't be used to94* make changes to the database. However, since there is a transaction running,95* you can't create another one via {@link goog.db.IndexedDb.createTransaction}.96* This means that it's not possible to manipulate the database other than97* creating or removing object stores in this callback.98*99* @typedef {function(!goog.db.IndexedDb.VersionChangeEvent,100* !goog.db.IndexedDb,101* !goog.db.Transaction)}102*/103goog.db.UpgradeNeededCallback;104105106/**107* Opens a database connection and wraps it.108*109* @param {string} name The name of the database to open.110* @param {number=} opt_version The expected version of the database. If this is111* larger than the actual version, opt_onUpgradeNeeded will be called112* (possibly after opt_onBlocked; see {@link goog.db.BlockedCallback}). If113* this is passed, opt_onUpgradeNeeded must be passed as well.114* @param {goog.db.UpgradeNeededCallback=} opt_onUpgradeNeeded Called if115* opt_version is greater than the old version of the database. If116* opt_version is passed, this must be passed as well.117* @param {goog.db.BlockedCallback=} opt_onBlocked Called if there are active118* connections to the database.119* @return {!goog.async.Deferred} The deferred database object.120*/121goog.db.openDatabase = function(122name, opt_version, opt_onUpgradeNeeded, opt_onBlocked) {123goog.asserts.assert(124goog.isDef(opt_version) == goog.isDef(opt_onUpgradeNeeded),125'opt_version must be passed to goog.db.openDatabase if and only if ' +126'opt_onUpgradeNeeded is also passed');127128var d = new goog.async.Deferred();129var openRequest = opt_version ? goog.db.indexedDb_.open(name, opt_version) :130goog.db.indexedDb_.open(name);131openRequest.onsuccess = function(ev) {132var db = new goog.db.IndexedDb(ev.target.result);133d.callback(db);134};135openRequest.onerror = function(ev) {136var msg = 'opening database ' + name;137d.errback(goog.db.Error.fromRequest(ev.target, msg));138};139openRequest.onupgradeneeded = function(ev) {140if (!opt_onUpgradeNeeded) return;141var db = new goog.db.IndexedDb(ev.target.result);142opt_onUpgradeNeeded(143new goog.db.IndexedDb.VersionChangeEvent(ev.oldVersion, ev.newVersion),144db, new goog.db.Transaction(ev.target.transaction, db));145};146openRequest.onblocked = function(ev) {147if (opt_onBlocked) {148opt_onBlocked(149new goog.db.IndexedDb.VersionChangeEvent(150ev.oldVersion, ev.newVersion));151}152};153return d;154};155156157/**158* Deletes a database once all open connections have been closed.159*160* @param {string} name The name of the database to delete.161* @param {goog.db.BlockedCallback=} opt_onBlocked Called if there are active162* connections to the database.163* @return {!goog.async.Deferred} A deferred object that will fire once the164* database is deleted.165*/166goog.db.deleteDatabase = function(name, opt_onBlocked) {167var d = new goog.async.Deferred();168var deleteRequest = goog.db.indexedDb_.deleteDatabase(name);169deleteRequest.onsuccess = function(ev) { d.callback(); };170deleteRequest.onerror = function(ev) {171var msg = 'deleting database ' + name;172d.errback(goog.db.Error.fromRequest(ev.target, msg));173};174deleteRequest.onblocked = function(ev) {175if (opt_onBlocked) {176opt_onBlocked(177new goog.db.IndexedDb.VersionChangeEvent(178ev.oldVersion, ev.newVersion));179}180};181return d;182};183184185