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