Source: lib/offline/indexeddb/v2_storage_cell.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.offline.indexeddb.V2StorageCell');
  18. goog.require('shaka.offline.indexeddb.DBConnection');
  19. goog.require('shaka.util.Error');
  20. /**
  21. * The V2StorageCell is for all stores that follow the shakaExterns V2 offline
  22. * types. This storage cell will work for both IndexedDB version 2 and 3 as
  23. * both used the shakaExterns V2 offline types.
  24. *
  25. * @implements {shakaExtern.StorageCell}
  26. */
  27. shaka.offline.indexeddb.V2StorageCell = class {
  28. /**
  29. * @param {IDBDatabase} connection
  30. * @param {string} segmentStore
  31. * @param {string} manifestStore
  32. * @param {boolean} isFixedKey
  33. */
  34. constructor(connection,
  35. segmentStore,
  36. manifestStore,
  37. isFixedKey) {
  38. /** @private {!shaka.offline.indexeddb.DBConnection} */
  39. this.connection_ = new shaka.offline.indexeddb.DBConnection(connection);
  40. /** @private {string} */
  41. this.segmentStore_ = segmentStore;
  42. /** @private {string} */
  43. this.manifestStore_ = manifestStore;
  44. /** @private {boolean} */
  45. this.isFixedKey_ = isFixedKey;
  46. }
  47. /**
  48. * @override
  49. */
  50. destroy() { return this.connection_.destroy(); }
  51. /**
  52. * @override
  53. */
  54. hasFixedKeySpace() { return this.isFixedKey_; }
  55. /**
  56. * @override
  57. */
  58. addSegments(segments) { return this.add_(this.segmentStore_, segments); }
  59. /**
  60. * @override
  61. */
  62. removeSegments(keys, onRemove) {
  63. return this.remove_(this.segmentStore_, keys, onRemove);
  64. }
  65. /**
  66. * @override
  67. */
  68. getSegments(keys) { return this.get_(this.segmentStore_, keys); }
  69. /**
  70. * @override
  71. */
  72. addManifests(manifests) { return this.add_(this.manifestStore_, manifests); }
  73. /**
  74. * @override
  75. */
  76. updateManifestExpiration(key, newExpiration) {
  77. let op = this.connection_.startReadWriteOperation(this.manifestStore_);
  78. let store = op.store();
  79. store.get(key).onsuccess = (e) => {
  80. let found = e.target.result;
  81. // If we can't find the value, then there is nothing for us to update.
  82. if (found) {
  83. found.expiration = newExpiration;
  84. store.put(found, key);
  85. }
  86. };
  87. return op.promise();
  88. }
  89. /**
  90. * @override
  91. */
  92. removeManifests(keys, onRemove) {
  93. return this.remove_(this.manifestStore_, keys, onRemove);
  94. }
  95. /**
  96. * @override
  97. */
  98. getManifests(keys) { return this.get_(this.manifestStore_, keys); }
  99. /**
  100. * @override
  101. */
  102. getAllManifests() {
  103. let op = this.connection_.startReadOnlyOperation(this.manifestStore_);
  104. let store = op.store();
  105. let values = {};
  106. store.openCursor().onsuccess = (event) => {
  107. // When we reach the end of the data that the cursor is iterating
  108. // over, |event.target.result| will be null to signal the end of the
  109. // iteration.
  110. // https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor/continue
  111. let cursor = event.target.result;
  112. if (!cursor) {
  113. return;
  114. }
  115. values[cursor.key] = cursor.value;
  116. // Go to the next item in the store, which will cause |onsuccess| to be
  117. // called again.
  118. cursor.continue();
  119. };
  120. // Wait until the operation completes or else values may be missing from
  121. // |values|.
  122. return op.promise().then(() => values);
  123. }
  124. /**
  125. * @param {string} storeName
  126. * @param {!Array.<T>} values
  127. * @return {!Promise.<!Array.<number>>}
  128. * @template T
  129. * @private
  130. */
  131. add_(storeName, values) {
  132. // In the case that this storage cell does not support add-operations, just
  133. // reject the request immediately instead of allowing it to try.
  134. if (this.isFixedKey_) {
  135. return Promise.reject(new shaka.util.Error(
  136. shaka.util.Error.Severity.RECOVERABLE,
  137. shaka.util.Error.Category.STORAGE,
  138. shaka.util.Error.Code.NEW_KEY_OPERATION_NOT_SUPPORTED,
  139. 'Cannot add new value to ' + storeName));
  140. }
  141. let op = this.connection_.startReadWriteOperation(storeName);
  142. let store = op.store();
  143. /** @type {!Array.<number>} */
  144. let keys = [];
  145. // Write each segment out. When each request completes, the key will
  146. // be in |event.target.result| as can be seen in
  147. // https://w3c.github.io/IndexedDB/#key-generator-construct.
  148. values.forEach((value) => {
  149. let request = store.add(value);
  150. request.onsuccess = (event) => {
  151. let key = event.target.result;
  152. keys.push(key);
  153. };
  154. });
  155. // Wait until the operation completes or else |keys| will not be fully
  156. // populated.
  157. return op.promise().then(() => keys);
  158. }
  159. /**
  160. * @param {string} storeName
  161. * @param {!Array.<number>} keys
  162. * @param {function(number)} onRemove
  163. * @return {!Promise}
  164. * @private
  165. */
  166. remove_(storeName, keys, onRemove) {
  167. let op = this.connection_.startReadWriteOperation(storeName);
  168. let store = op.store();
  169. keys.forEach((key) => {
  170. store.delete(key).onsuccess = () => onRemove(key);
  171. });
  172. return op.promise();
  173. }
  174. /**
  175. * @param {string} storeName
  176. * @param {!Array.<number>} keys
  177. * @return {!Promise.<!Array.<T>>}
  178. * @template T
  179. * @private
  180. */
  181. get_(storeName, keys) {
  182. let op = this.connection_.startReadOnlyOperation(storeName);
  183. let store = op.store();
  184. let values = {};
  185. let missing = [];
  186. // Use a map to store the objects so that we can reorder the results to
  187. // match the order of |keys|.
  188. keys.forEach((key) => {
  189. let request = store.get(key);
  190. request.onsuccess = () => {
  191. // Make sure a defined value was found. Indexeddb treats no-value found
  192. // as a success with an undefined result.
  193. if (request.result == undefined) {
  194. missing.push(key);
  195. }
  196. values[key] = request.result;
  197. };
  198. });
  199. // Wait until the operation completes or else values may be missing from
  200. // |values|. Use the original key list to convert the map to a list so that
  201. // the order will match.
  202. return op.promise().then(() => {
  203. if (missing.length) {
  204. return Promise.reject(new shaka.util.Error(
  205. shaka.util.Error.Severity.RECOVERABLE,
  206. shaka.util.Error.Category.STORAGE,
  207. shaka.util.Error.Code.KEY_NOT_FOUND,
  208. 'Could not find values for ' + missing
  209. ));
  210. }
  211. return keys.map((key) => values[key]);
  212. });
  213. }
  214. };