From 03fc0bca99ed44a63d2ade8014565cd024b58fb1 Mon Sep 17 00:00:00 2001 From: Aaron Vaage Date: Wed, 9 May 2018 13:13:23 -0700 Subject: [PATCH] Add Back Progress on Remove Tracking progress when removing an asset was missing from the new storage system. This CL adds that support back. Issue #1248 Change-Id: Iab275cd75af817cfc34ee0888ddeea257b1ead56 --- externs/shaka/offline.js | 11 +++++++++-- lib/offline/indexeddb/v1_storage_cell.js | 13 +++++++++---- lib/offline/indexeddb/v2_storage_cell.js | 13 +++++++++---- lib/offline/storage.js | 16 ++++++++++++++-- test/offline/indexeddb_storage_unit.js | 6 ++++-- test/offline/offline_manifest_parser_unit.js | 6 ++++-- test/offline/storage_unit.js | 4 +++- test/offline/v1_indexeddb_storage_unit.js | 5 +++-- 8 files changed, 55 insertions(+), 19 deletions(-) diff --git a/externs/shaka/offline.js b/externs/shaka/offline.js index 75f22be1f..ded16431d 100644 --- a/externs/shaka/offline.js +++ b/externs/shaka/offline.js @@ -289,9 +289,12 @@ shaka.extern.StorageCell.prototype.addSegments = function(segments) {}; * is not found, then that removal should be considered successful. * * @param {!Array.} keys + * @param {function(number)} onRemove A callback for when a segment is removed + * from the cell. The key of the segment + * will be passed to the callback. * @return {!Promise} */ -shaka.extern.StorageCell.prototype.removeSegments = function(keys) {}; +shaka.extern.StorageCell.prototype.removeSegments = function(keys, onRemove) {}; /** @@ -333,9 +336,13 @@ shaka.extern.StorageCell.prototype.updateManifestExpiration = * is not found, then that removal should be considered successful. * * @param {!Array.} keys + * @param {function(number)} onRemove A callback for when a manifest is removed + * from the cell. The key of the manifest + * will be passed to the callback. * @return {!Promise} */ -shaka.extern.StorageCell.prototype.removeManifests = function(keys) {}; +shaka.extern.StorageCell.prototype.removeManifests = + function(keys, onRemove) {}; /** diff --git a/lib/offline/indexeddb/v1_storage_cell.js b/lib/offline/indexeddb/v1_storage_cell.js index b6bf158af..1a3c0aa83 100644 --- a/lib/offline/indexeddb/v1_storage_cell.js +++ b/lib/offline/indexeddb/v1_storage_cell.js @@ -67,7 +67,9 @@ shaka.offline.indexeddb.V1StorageCell = class { /** * @override */ - removeSegments(keys) { return this.remove_(this.segmentStore_, keys); } + removeSegments(keys, onRemove) { + return this.remove_(this.segmentStore_, keys, onRemove); + } /** * @override @@ -131,7 +133,9 @@ shaka.offline.indexeddb.V1StorageCell = class { /** * @override */ - removeManifests(keys) { return this.remove_(this.manifestStore_, keys); } + removeManifests(keys, onRemove) { + return this.remove_(this.manifestStore_, keys, onRemove); + } /** * @override @@ -195,15 +199,16 @@ shaka.offline.indexeddb.V1StorageCell = class { /** * @param {string} storeName * @param {!Array.} keys + * @param {function(number)} onRemove * @return {!Promise} * @private */ - remove_(storeName, keys) { + remove_(storeName, keys, onRemove) { let op = this.connection_.startReadWriteOperation(storeName); let store = op.store(); keys.forEach((key) => { - store.delete(key); + store.delete(key).onsuccess = () => onRemove(key); }); return op.promise(); diff --git a/lib/offline/indexeddb/v2_storage_cell.js b/lib/offline/indexeddb/v2_storage_cell.js index 84a2f51f7..c1cf10b52 100644 --- a/lib/offline/indexeddb/v2_storage_cell.js +++ b/lib/offline/indexeddb/v2_storage_cell.js @@ -69,7 +69,9 @@ shaka.offline.indexeddb.V2StorageCell = class { /** * @override */ - removeSegments(keys) { return this.remove_(this.segmentStore_, keys); } + removeSegments(keys, onRemove) { + return this.remove_(this.segmentStore_, keys, onRemove); + } /** * @override @@ -102,7 +104,9 @@ shaka.offline.indexeddb.V2StorageCell = class { /** * @override */ - removeManifests(keys) { return this.remove_(this.manifestStore_, keys); } + removeManifests(keys, onRemove) { + return this.remove_(this.manifestStore_, keys, onRemove); + } /** * @override @@ -183,15 +187,16 @@ shaka.offline.indexeddb.V2StorageCell = class { /** * @param {string} storeName * @param {!Array.} keys + * @param {function(number)} onRemove * @return {!Promise} * @private */ - remove_(storeName, keys) { + remove_(storeName, keys, onRemove) { let op = this.connection_.startReadWriteOperation(storeName); let store = op.store(); keys.forEach((key) => { - store.delete(key); + store.delete(key).onsuccess = () => onRemove(key); }); return op.promise(); diff --git a/lib/offline/storage.js b/lib/offline/storage.js index 38b5380ac..cbc6fe119 100644 --- a/lib/offline/storage.js +++ b/lib/offline/storage.js @@ -373,9 +373,21 @@ shaka.offline.Storage.prototype.removeFromStorage_ = function( /** @type {!Array.} */ let segmentIds = shaka.offline.Storage.getAllSegmentIds_(manifest); + // Count(segments) + Count(manifests) + let toRemove = segmentIds.length + 1; + let removed = 0; + + let pendingContent = shaka.offline.StoredContentUtils.fromManifestDB( + uri, manifest); + + let onRemove = (key) => { + removed += 1; + this.config_.progressCallback(pendingContent, removed / toRemove); + }; + return Promise.all([ - storage.removeSegments(segmentIds), - storage.removeManifests([uri.key()]) + storage.removeSegments(segmentIds, onRemove), + storage.removeManifests([uri.key()], onRemove) ]); }; diff --git a/test/offline/indexeddb_storage_unit.js b/test/offline/indexeddb_storage_unit.js index b1502dc5b..5fdea0de2 100644 --- a/test/offline/indexeddb_storage_unit.js +++ b/test/offline/indexeddb_storage_unit.js @@ -25,6 +25,8 @@ describe('IndexeddbStorageCell', function() { const segmentStore = 'segment-store'; const manifestStore = 'manifest-store'; + const noop = () => {}; + /** @type {!Array.} */ let cells = []; @@ -78,7 +80,7 @@ describe('IndexeddbStorageCell', function() { OfflineUtils.expectSegmentToEqual(found[1], segments[1]); OfflineUtils.expectSegmentToEqual(found[2], segments[2]); - return cell.removeSegments(keys); + return cell.removeSegments(keys, noop); }).then(() => { // The get should fail as there should be no entries under the keys // anymore. @@ -121,7 +123,7 @@ describe('IndexeddbStorageCell', function() { expect(found[1]).toEqual(manifests[1]); expect(found[2]).toEqual(manifests[2]); - return cell.removeManifests(keys); + return cell.removeManifests(keys, noop); }).then(() => { // The get should fail as there should be no entries under the keys // anymore. diff --git a/test/offline/offline_manifest_parser_unit.js b/test/offline/offline_manifest_parser_unit.js index ad14a2c73..3e84d09e3 100644 --- a/test/offline/offline_manifest_parser_unit.js +++ b/test/offline/offline_manifest_parser_unit.js @@ -106,7 +106,8 @@ describe('OfflineManifestParser', function() { handle.path.mechanism, handle.path.cell, keys[0]); // Remove the manifest so that the uri will point to nothing. - await handle.cell.removeManifests(keys); + const noop = () => {}; + await handle.cell.removeManifests(keys, noop); }); try { @@ -149,7 +150,8 @@ describe('OfflineManifestParser', function() { // Remove the manifest after we have parsed it so that the // update won't find it. Oh, we are sneaky. - await handle.cell.removeManifests(keys); + const noop = () => {}; + await handle.cell.removeManifests(keys, noop); await parser.onExpirationUpdated(sessionId, newExpiration); }); })); diff --git a/test/offline/storage_unit.js b/test/offline/storage_unit.js index 99e49b2be..949fd30c8 100644 --- a/test/offline/storage_unit.js +++ b/test/offline/storage_unit.js @@ -753,7 +753,9 @@ describe('Storage', function() { // There should be way more than one segment. let keys = stream.segments.map((segment) => segment.dataKey); expect(keys.length).toBeGreaterThan(0); - await cell.removeSegments(keys); + + const noop = () => {}; + await cell.removeSegments(keys, noop); }); await storage.remove(uri.toString()); diff --git a/test/offline/v1_indexeddb_storage_unit.js b/test/offline/v1_indexeddb_storage_unit.js index 3c03007cf..3ada53213 100644 --- a/test/offline/v1_indexeddb_storage_unit.js +++ b/test/offline/v1_indexeddb_storage_unit.js @@ -170,8 +170,9 @@ describe('V1IndexeddbStorageCell', function() { expect(segmentKeys.length).toBe(6); // Remove all the segments. - await cell.removeManifests(manifestKeys); - await cell.removeSegments(segmentKeys); + const noop = () => {}; + await cell.removeManifests(manifestKeys, noop); + await cell.removeSegments(segmentKeys, noop); let checkMissingSegment = async (key) => { try {