Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 * Class to manage user preferences. | 6 * Class to manage user preferences. |
| 7 * | 7 * |
| 8 * @constructor | 8 * @constructor |
| 9 */ | 9 */ |
| 10 let SwitchAccessPrefs = function() { | 10 let SwitchAccessPrefs = function() { |
| 11 /** | 11 /** |
| 12 * User preferences, initially set to the default preference values. | 12 * User preferences, initially set to the default preference values. |
| 13 * | 13 * |
| 14 * @private | 14 * @private |
| 15 */ | 15 */ |
| 16 this.prefs_ = Object.assign({}, this.DEFAULT_PREFS); | 16 this.prefs_ = Object.assign({}, this.DEFAULT_PREFS); |
| 17 | |
| 17 this.loadPrefs_(); | 18 this.loadPrefs_(); |
| 18 chrome.storage.onChanged.addListener(this.handleStorageChange_.bind(this)); | 19 chrome.storage.onChanged.addListener(this.handleStorageChange_.bind(this)); |
| 19 }; | 20 }; |
| 20 | 21 |
| 21 SwitchAccessPrefs.prototype = { | 22 SwitchAccessPrefs.prototype = { |
| 22 /** | 23 /** |
| 23 * Asynchronously load the current preferences from chrome.storage.sync and | 24 * Asynchronously load the current preferences from chrome.storage.sync and |
| 24 * store them in this.prefs_. | 25 * store them in this.prefs_, if this.prefs_ is not already set to that value. |
| 26 * If this.prefs_ changes, fire a prefsUpdate event. | |
| 25 * | 27 * |
| 26 * @private | 28 * @private |
| 27 */ | 29 */ |
| 28 loadPrefs_: function() { | 30 loadPrefs_: function() { |
| 29 let defaultKeys = Object.keys(this.DEFAULT_PREFS); | 31 let defaultKeys = Object.keys(this.DEFAULT_PREFS); |
| 30 chrome.storage.sync.get(defaultKeys, function(loadedPrefs) { | 32 chrome.storage.sync.get(defaultKeys, function(loadedPrefs) { |
| 31 let loadedKeys = Object.keys(loadedPrefs); | 33 let updatedPrefs = {}; |
| 32 if (loadedKeys.length > 0) { | 34 for (let key of Object.keys(loadedPrefs)) { |
| 33 for (let key of loadedKeys) { | 35 if (this.prefs_[key] !== loadedPrefs[key]) { |
| 34 this.prefs_[key] = loadedPrefs[key]; | 36 this.prefs_[key] = loadedPrefs[key]; |
| 37 updatedPrefs[key] = loadedPrefs[key]; | |
| 35 } | 38 } |
| 36 let event = new CustomEvent('prefsUpdate', {'detail': loadedPrefs}); | 39 } |
| 40 if (Object.keys(updatedPrefs).length > 0) { | |
| 41 let event = new CustomEvent('prefsUpdate', {'detail': updatedPrefs}); | |
| 37 document.dispatchEvent(event); | 42 document.dispatchEvent(event); |
| 38 } | 43 } |
| 39 }.bind(this)); | 44 }.bind(this)); |
| 40 }, | 45 }, |
| 41 | 46 |
| 42 /** | 47 /** |
| 43 * Store any changes to chrome.storage.sync in this.prefs_. | 48 * Store any changes from chrome.storage.sync to this.prefs_, if this.prefs_ |
| 49 * is not already set to that value. If this.prefs_ changes, fire a | |
| 50 * prefsUpdate event. | |
| 44 * | 51 * |
| 45 * @param {!Object} changes | 52 * @param {!Object} storageChanges |
| 46 * @param {string} areaName | 53 * @param {string} areaName |
| 47 * @private | 54 * @private |
| 48 */ | 55 */ |
| 49 handleStorageChange_: function(changes, areaName) { | 56 handleStorageChange_: function(storageChanges, areaName) { |
| 50 for (let key of Object.keys(changes)) { | 57 let updatedPrefs = {}; |
| 51 // If pref change happened on same device, prefs will already be updated, | 58 for (let key of Object.keys(storageChanges)) { |
| 52 // so this will have no effect. | 59 if (this.prefs_[key] !== storageChanges[key].newValue) { |
| 53 this.prefs_[key] = changes[key].newValue; | 60 this.prefs_[key] = storageChanges[key].newValue; |
| 54 changes[key] = changes[key].newValue; | 61 updatedPrefs[key] = storageChanges[key].newValue; |
| 62 } | |
| 55 } | 63 } |
| 56 | 64 if (Object.keys(updatedPrefs).length > 0) { |
| 57 let event = new CustomEvent('prefsUpdate', {'detail': changes}); | 65 let event = new CustomEvent('prefsUpdate', {'detail': updatedPrefs}); |
|
dmazzoni
2017/03/29 05:52:20
Now that you have a nice SwitchAccessInterface cla
elichtenberg
2017/03/29 19:15:11
Currently, the options page(s) also listen(s) to '
| |
| 58 document.dispatchEvent(event); | 66 document.dispatchEvent(event); |
| 67 } | |
| 59 }, | 68 }, |
| 60 | 69 |
| 61 /** | 70 /** |
| 62 * Set the value of the preference |key| to |value|. | 71 * Set the value of the preference |key| to |value| in chrome.storage.sync. |
| 72 * this.prefs_ is not set until handleStorageChange_. | |
| 63 * | 73 * |
| 64 * @param {string} key | 74 * @param {string} key |
| 65 * @param {boolean|string|number} value | 75 * @param {boolean|string|number} value |
| 66 */ | 76 */ |
| 67 setPref: function(key, value) { | 77 setPref: function(key, value) { |
| 68 let pref = {}; | 78 let pref = {}; |
| 69 pref[key] = value; | 79 pref[key] = value; |
| 70 chrome.storage.sync.set(pref); | 80 chrome.storage.sync.set(pref); |
| 71 this.prefs_[key] = value; | |
| 72 }, | 81 }, |
| 73 | 82 |
| 74 /** | 83 /** |
| 75 * Get all user preferences. | 84 * Get the value of the preference |key|. |
| 85 * | |
| 86 * @param {string} key | |
| 87 * @return {boolean|string|number} | |
| 76 */ | 88 */ |
| 77 getPrefs: function() { | 89 getPref: function(key) { |
| 78 return Object.assign({}, this.prefs_); | 90 return this.prefs_[key]; |
| 79 }, | 91 }, |
| 80 | 92 |
| 81 /** | 93 /** |
| 82 * The default value of all preferences. All preferences should be primitives | 94 * The default value of all preferences. All preferences should be primitives |
| 83 * to prevent changes to default values. | 95 * to prevent changes to default values. |
| 84 * | 96 * |
| 85 * @const | 97 * @const |
| 86 */ | 98 */ |
| 87 DEFAULT_PREFS: { | 99 DEFAULT_PREFS: { |
| 88 'enableAutoScan': false, | 100 'enableAutoScan': false, |
| 89 'autoScanTime': .75 | 101 'autoScanTime': .8 |
| 90 } | 102 } |
| 91 }; | 103 }; |
| OLD | NEW |