| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * The model class for audio player. | |
| 7 * @constructor | |
| 8 */ | |
| 9 function AudioPlayerModel() {} | |
| 10 | |
| 11 AudioPlayerModel.prototype.initialize = function(callback) { | |
| 12 'use strict'; | |
| 13 | |
| 14 /** | |
| 15 * List of values to be stored into the model. | |
| 16 * @type {!Object<*>} | |
| 17 * @const | |
| 18 */ | |
| 19 var VALUES = | |
| 20 /** | |
| 21 * They will be used as properties of AudioPlayerModel. | |
| 22 * @lends {AudioPlayerModel.prototype} | |
| 23 */ | |
| 24 { | |
| 25 shuffle: false, | |
| 26 repeat: false, | |
| 27 volume: 100, | |
| 28 expanded: false, | |
| 29 }; | |
| 30 | |
| 31 /** | |
| 32 * Prefix of the stored values in the storage. | |
| 33 * @type {string} | |
| 34 */ | |
| 35 var STORAGE_PREFIX = 'audioplayer-'; | |
| 36 | |
| 37 /** | |
| 38 * Save the values in the model into the storage. | |
| 39 * @param {AudioPlayerModel} model The model. | |
| 40 */ | |
| 41 function saveModel(model) { | |
| 42 var objectToBeSaved = {}; | |
| 43 for (var key in VALUES) { | |
| 44 objectToBeSaved[STORAGE_PREFIX + key] = model[key]; | |
| 45 } | |
| 46 | |
| 47 chrome.storage.local.set(objectToBeSaved); | |
| 48 }; | |
| 49 | |
| 50 /** | |
| 51 * Load the values in the model from the storage. | |
| 52 * @param {AudioPlayerModel} model The model. | |
| 53 * @param {function()} inCallback Called when the load is completed. | |
| 54 */ | |
| 55 function loadModel(model, inCallback) { | |
| 56 // Restores the values from the storage | |
| 57 var objectsToBeRead = Object.keys(VALUES). | |
| 58 map(function(a) { | |
| 59 return STORAGE_PREFIX + a; | |
| 60 }); | |
| 61 | |
| 62 chrome.storage.local.get(objectsToBeRead, function(result) { | |
| 63 for (var key in result) { | |
| 64 // Strips the prefix. | |
| 65 model[key.substr(STORAGE_PREFIX.length)] = result[key]; | |
| 66 } | |
| 67 inCallback(); | |
| 68 }); | |
| 69 }; | |
| 70 | |
| 71 // Initializes values. | |
| 72 for (var key in VALUES) { | |
| 73 this[key] = VALUES[key]; | |
| 74 } | |
| 75 Object.seal(this); | |
| 76 | |
| 77 // Restores the values from the storage | |
| 78 var target = this; | |
| 79 loadModel(target, function() { | |
| 80 // Installs observer to watch changes of the values. | |
| 81 Object.observe(target, function(changes) { | |
| 82 saveModel(target); | |
| 83 }); | |
| 84 callback(); | |
| 85 }); | |
| 86 } | |
| OLD | NEW |