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.
9 * List of values to be stored into the model.
10 * @type {Object<string, *>}
13 var VALUES = Object.freeze({
21 * Prefix of the stored values in the storage.
24 var STORAGE_PREFIX = 'audioplayer-';
27 * Save the values in the model into the storage.
28 * @param {AudioPlayerModel} model The model.
30 function saveModel(model) {
31 var objectToBeSaved = {};
32 for (var key in VALUES) {
33 objectToBeSaved[STORAGE_PREFIX + key] = model[key];
36 chrome.storage.local.set(objectToBeSaved);
40 * Load the values in the model from the storage.
41 * @param {AudioPlayerModel} model The model.
42 * @param {function()} callback Called when the load is completed.
44 function loadModel(model, callback) {
45 // Restores the values from the storage
46 var objectsToBeRead = Object.keys(VALUES).
48 return STORAGE_PREFIX + a;
51 chrome.storage.local.get(objectsToBeRead, function(result) {
52 for (var key in result) {
54 model[key.substr(STORAGE_PREFIX.length)] = result[key];
61 * The model class for audio player.
64 function AudioPlayerModel() {
65 // Initializes values.
66 for (var key in VALUES) {
67 this[key] = VALUES[key];
71 // Restores the values from the storage
72 loadModel(this, function() {
73 // Installs observer to watch changes of the values.
74 var observer = new ObjectObserver(this);
75 observer.open(function(added, removed, changed, getOldValueFn) {
81 // Exports AudioPlayerModel class to the global.
82 global.AudioPlayerModel = AudioPlayerModel;