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.
6 * The model class for audio player.
9 function AudioPlayerModel() {
13 * List of values to be stored into the model.
14 * @type {!Object<string, *>}
17 var VALUES = Object.freeze(
19 * They will be used as properties of AudioPlayerModel.
20 * @lends {AudioPlayerModel.prototype}
30 * Prefix of the stored values in the storage.
33 var STORAGE_PREFIX = 'audioplayer-';
36 * Save the values in the model into the storage.
37 * @param {AudioPlayerModel} model The model.
39 function saveModel(model) {
40 var objectToBeSaved = {};
41 for (var key in VALUES) {
42 objectToBeSaved[STORAGE_PREFIX + key] = model[key];
45 chrome.storage.local.set(objectToBeSaved);
49 * Load the values in the model from the storage.
50 * @param {AudioPlayerModel} model The model.
51 * @param {function()} callback Called when the load is completed.
53 function loadModel(model, callback) {
54 // Restores the values from the storage
55 var objectsToBeRead = Object.keys(VALUES).
57 return STORAGE_PREFIX + a;
60 chrome.storage.local.get(objectsToBeRead, function(result) {
61 for (var key in result) {
63 model[key.substr(STORAGE_PREFIX.length)] = result[key];
69 // Initializes values.
70 for (var key in VALUES) {
71 this[key] = VALUES[key];
75 // Restores the values from the storage
77 loadModel(target, function() {
78 // Installs observer to watch changes of the values.
79 Object.observe(target, function(changes) {