db.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Wrappers for the HTML5 IndexedDB. The wrappers export nearly
  16. * the same interface as the standard API, but return goog.async.Deferred
  17. * objects instead of request objects and use Closure events. The wrapper works
  18. * and has been tested on Chrome version 22+. It may work on older Chrome
  19. * 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. */
  42. goog.provide('goog.db');
  43. goog.provide('goog.db.BlockedCallback');
  44. goog.provide('goog.db.UpgradeNeededCallback');
  45. goog.require('goog.asserts');
  46. goog.require('goog.async.Deferred');
  47. goog.require('goog.db.Error');
  48. goog.require('goog.db.IndexedDb');
  49. goog.require('goog.db.Transaction');
  50. /**
  51. * The IndexedDB factory object.
  52. *
  53. * @type {!IDBFactory|undefined}
  54. * @private
  55. */
  56. goog.db.indexedDb_ = goog.global.indexedDB || goog.global.mozIndexedDB ||
  57. goog.global.webkitIndexedDB || goog.global.moz_indexedDB;
  58. /**
  59. * A callback that's called if a blocked event is received. When a database is
  60. * supposed to be deleted or upgraded (i.e. versionchange), and there are open
  61. * connections to this database, a block event will be fired to prevent the
  62. * operations from going through until all such open connections are closed.
  63. * This callback can be used to notify users that they should close other tabs
  64. * that have open connections, or to close the connections manually. Databases
  65. * can also listen for the {@link goog.db.IndexedDb.EventType.VERSION_CHANGE}
  66. * event to automatically close themselves when they're blocking such
  67. * operations.
  68. *
  69. * This is passed a VersionChangeEvent that has the version of the database
  70. * before it was deleted, and "null" as the new version.
  71. *
  72. * @typedef {function(!goog.db.IndexedDb.VersionChangeEvent)}
  73. */
  74. goog.db.BlockedCallback;
  75. /**
  76. * A callback that's called when opening a database whose internal version is
  77. * lower than the version passed to {@link goog.db.openDatabase}.
  78. *
  79. * This callback is passed three arguments: a VersionChangeEvent with both the
  80. * old version and the new version of the database; the database that's being
  81. * opened, for which you can create and delete object stores; and the version
  82. * change transaction, with which you can abort the version change.
  83. *
  84. * Note that the transaction is not active, which means that it can't be used to
  85. * make changes to the database. However, since there is a transaction running,
  86. * you can't create another one via {@link goog.db.IndexedDb.createTransaction}.
  87. * This means that it's not possible to manipulate the database other than
  88. * creating or removing object stores in this callback.
  89. *
  90. * @typedef {function(!goog.db.IndexedDb.VersionChangeEvent,
  91. * !goog.db.IndexedDb,
  92. * !goog.db.Transaction)}
  93. */
  94. goog.db.UpgradeNeededCallback;
  95. /**
  96. * Opens a database connection and wraps it.
  97. *
  98. * @param {string} name The name of the database to open.
  99. * @param {number=} opt_version The expected version of the database. If this is
  100. * larger than the actual version, opt_onUpgradeNeeded will be called
  101. * (possibly after opt_onBlocked; see {@link goog.db.BlockedCallback}). If
  102. * this is passed, opt_onUpgradeNeeded must be passed as well.
  103. * @param {goog.db.UpgradeNeededCallback=} opt_onUpgradeNeeded Called if
  104. * opt_version is greater than the old version of the database. If
  105. * opt_version is passed, this must be passed as well.
  106. * @param {goog.db.BlockedCallback=} opt_onBlocked Called if there are active
  107. * connections to the database.
  108. * @return {!goog.async.Deferred} The deferred database object.
  109. */
  110. goog.db.openDatabase = function(
  111. name, opt_version, opt_onUpgradeNeeded, opt_onBlocked) {
  112. goog.asserts.assert(
  113. goog.isDef(opt_version) == goog.isDef(opt_onUpgradeNeeded),
  114. 'opt_version must be passed to goog.db.openDatabase if and only if ' +
  115. 'opt_onUpgradeNeeded is also passed');
  116. var d = new goog.async.Deferred();
  117. var openRequest = opt_version ? goog.db.indexedDb_.open(name, opt_version) :
  118. goog.db.indexedDb_.open(name);
  119. openRequest.onsuccess = function(ev) {
  120. var db = new goog.db.IndexedDb(ev.target.result);
  121. d.callback(db);
  122. };
  123. openRequest.onerror = function(ev) {
  124. var msg = 'opening database ' + name;
  125. d.errback(goog.db.Error.fromRequest(ev.target, msg));
  126. };
  127. openRequest.onupgradeneeded = function(ev) {
  128. if (!opt_onUpgradeNeeded) return;
  129. var db = new goog.db.IndexedDb(ev.target.result);
  130. opt_onUpgradeNeeded(
  131. new goog.db.IndexedDb.VersionChangeEvent(ev.oldVersion, ev.newVersion),
  132. db, new goog.db.Transaction(ev.target.transaction, db));
  133. };
  134. openRequest.onblocked = function(ev) {
  135. if (opt_onBlocked) {
  136. opt_onBlocked(
  137. new goog.db.IndexedDb.VersionChangeEvent(
  138. ev.oldVersion, ev.newVersion));
  139. }
  140. };
  141. return d;
  142. };
  143. /**
  144. * Deletes a database once all open connections have been closed.
  145. *
  146. * @param {string} name The name of the database to delete.
  147. * @param {goog.db.BlockedCallback=} opt_onBlocked Called if there are active
  148. * connections to the database.
  149. * @return {!goog.async.Deferred} A deferred object that will fire once the
  150. * database is deleted.
  151. */
  152. goog.db.deleteDatabase = function(name, opt_onBlocked) {
  153. var d = new goog.async.Deferred();
  154. var deleteRequest = goog.db.indexedDb_.deleteDatabase(name);
  155. deleteRequest.onsuccess = function(ev) { d.callback(); };
  156. deleteRequest.onerror = function(ev) {
  157. var msg = 'deleting database ' + name;
  158. d.errback(goog.db.Error.fromRequest(ev.target, msg));
  159. };
  160. deleteRequest.onblocked = function(ev) {
  161. if (opt_onBlocked) {
  162. opt_onBlocked(
  163. new goog.db.IndexedDb.VersionChangeEvent(
  164. ev.oldVersion, ev.newVersion));
  165. }
  166. };
  167. return d;
  168. };