From a0aa5e51c644c6c6aaa33e30ba58c46c470eb25b Mon Sep 17 00:00:00 2001 From: Natalie Harris Date: Mon, 6 Apr 2015 15:17:04 -0700 Subject: [PATCH] Retrieve list of currently stored group ids. Issue #22 Change-Id: Ifeaa4085ec289679ccd9cb1fc22bd400c9cd8198 --- app.js | 56 +++++++++++++++++++++++++---------- lib/util/content_database.js | 25 ++++++++++++++++ spec/content_database_spec.js | 18 +++++++++++ 3 files changed, 83 insertions(+), 16 deletions(-) diff --git a/app.js b/app.js index 5121404f18..54fefed57b 100644 --- a/app.js +++ b/app.js @@ -136,12 +136,34 @@ app.init = function() { shaka.log.setLevel(shaka.log.Level.V1); } - // Retrieve list of offline streams - var groups = app.getOfflineGroups_(); - for (var key in groups) { - var value = groups[key]; - app.addOfflineStream_(key, value); - } + // Retrieve and verify list of offline streams + var storage = new shaka.util.ContentDatabase(null); + storage.setUpDatabase().then( + function() { + return storage.retrieveGroupIds(); + } + ).then( + function(groupIds) { + var groups = app.getOfflineGroups_(); + for (var i in groupIds) { + var id = groupIds[i]; + var value = groups[id]; + if (!value) { + value = 'Unknown Stream ID ' + id; + } + app.addOfflineStream_(value, id); + } + } + ).then( + function() { + storage.closeDatabaseConnection(); + } + ).catch( + function(e) { + storage.closeDatabaseConnection(); + console.error('Error retrieving stored data', e); + } + ); app.onMpdChange(); @@ -149,15 +171,12 @@ app.init = function() { if ('dash' in params) { document.getElementById('streamTypeList').value = 'dash'; - app.onStreamTypeChange(); app.loadStream(); } else if ('http' in params) { document.getElementById('streamTypeList').value = 'http'; - app.onStreamTypeChange(); app.loadStream(); - } else { - app.onStreamTypeChange(); } + app.onStreamTypeChange(); if ('cycleVideo' in params) { app.cycleVideo(); @@ -362,6 +381,11 @@ app.cycleTracks_ = * Deletes a group from storage. */ app.deleteStream = function() { + if (!app.player_) { + app.installPolyfills_(); + app.initPlayer_(); + } + var deleteButton = document.getElementById('deleteButton'); deleteButton.disabled = true; deleteButton.innerText = 'Deleting stream...'; @@ -379,7 +403,7 @@ app.deleteStream = function() { app.offlineStreams_.splice(deleted, 1); offlineList.removeChild(offlineList.childNodes[deleted]); var groups = app.getOfflineGroups_(); - delete groups[text]; + delete groups[groupId]; app.setOfflineGroups_(groups); deleteButton.innerText = 'Delete stream from storage'; app.onStreamTypeChange(); @@ -416,7 +440,7 @@ app.storeStream = function() { ).then( function(groupId) { var groups = app.getOfflineGroups_(); - groups[mediaUrl] = groupId; + groups[groupId] = mediaUrl; app.setOfflineGroups_(groups); app.addOfflineStream_(mediaUrl, groupId); app.updateStoreButton_(true, 'Stream already stored'); @@ -432,7 +456,7 @@ app.storeStream = function() { /** * Get a map of MPD URLs to group IDs for all streams stored offline. - * @return {!Object.} + * @return {!Object.} * @private */ app.getOfflineGroups_ = function() { @@ -440,7 +464,7 @@ app.getOfflineGroups_ = function() { var data = window.localStorage.getItem('offlineGroups') || '{}'; // JSON.parse can throw if the data stored isn't valid JSON. var groups = JSON.parse(data); - return /** @type {!Object.} */(groups); + return /** @type {!Object.} */(groups); } catch (exception) { console.debug('Disregarding stored offlineGroups.'); return {}; @@ -449,8 +473,8 @@ app.getOfflineGroups_ = function() { /** - * Store a map of MPD URLs to group IDs for all streams stored offline. - * @param {!Object.} groups + * Store a map of group IDs to MPD URLs for all streams stored offline. + * @param {!Object.} groups * @private */ app.setOfflineGroups_ = function(groups) { diff --git a/lib/util/content_database.js b/lib/util/content_database.js index e001b15f41..ecbb524e5f 100644 --- a/lib/util/content_database.js +++ b/lib/util/content_database.js @@ -538,6 +538,31 @@ shaka.util.ContentDatabase.prototype.retrieveGroup = function(groupId) { }; +/** + * Retrieves an array of all stored group IDs. + * @return {!Promise.>} The unique IDs of all of the + * stored groups. + */ +shaka.util.ContentDatabase.prototype.retrieveGroupIds = function() { + var p = new shaka.util.PublicPromise(); + var groupIds = []; + var request = this.getGroupStore_().openCursor(); + + request.onerror = function(e) { p.reject(request.error); }; + request.onsuccess = function(e) { + var cursor = e.target.result; + if (cursor) { + groupIds.push(cursor.key); + cursor.continue(); + } else { + p.resolve(groupIds); + } + }; + + return p; +}; + + /** * Retrieves an item from a store in the database. * @param {!IDBObjectStore|!IDBIndex} store The store to request an item from. diff --git a/spec/content_database_spec.js b/spec/content_database_spec.js index e6ca7ffdb3..4cbbdd2041 100644 --- a/spec/content_database_spec.js +++ b/spec/content_database_spec.js @@ -276,6 +276,24 @@ describe('ContentDatabase', function() { }); }); + it('retrieves a list of the stored group IDs', function(done) { + var initalGroupIdsLength = 0; + p.then(function() { + return db.retrieveGroupIds(); + }).then(function(groupIds) { + initalGroupIdsLength = groupIds.length; + return db.insertGroup([4, 5, 6], ['HIJK', 'LMNO']); + }).then(function() { + return db.retrieveGroupIds(); + }).then(function(groupIds) { + expect(groupIds.length - initalGroupIdsLength).toBe(1); + done(); + }).catch(function(err) { + fail(err); + done(); + }); + }); + it('deletes group information and throws error on retrieval', function(done) { var streamIds = []; var groupId;