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