Chromium Code Reviews| Index: chrome/browser/resources/chromeos/switch_access/prefs.js |
| diff --git a/chrome/browser/resources/chromeos/switch_access/prefs.js b/chrome/browser/resources/chromeos/switch_access/prefs.js |
| index bbe21bd3be69cdb67d01d059da4f2b509639beac..597093e7e2fcb0dccdf29883c0ab9ac2a89e1e69 100644 |
| --- a/chrome/browser/resources/chromeos/switch_access/prefs.js |
| +++ b/chrome/browser/resources/chromeos/switch_access/prefs.js |
| @@ -14,6 +14,7 @@ let SwitchAccessPrefs = function() { |
| * @private |
| */ |
| this.prefs_ = Object.assign({}, this.DEFAULT_PREFS); |
| + |
| this.loadPrefs_(); |
| chrome.storage.onChanged.addListener(this.handleStorageChange_.bind(this)); |
| }; |
| @@ -21,45 +22,54 @@ let SwitchAccessPrefs = function() { |
| SwitchAccessPrefs.prototype = { |
| /** |
| * Asynchronously load the current preferences from chrome.storage.sync and |
| - * store them in this.prefs_. |
| + * store them in this.prefs_, if this.prefs_ is not already set to that value. |
| + * If this.prefs_ changes, fire a prefsUpdate event. |
| * |
| * @private |
| */ |
| loadPrefs_: function() { |
| let defaultKeys = Object.keys(this.DEFAULT_PREFS); |
| chrome.storage.sync.get(defaultKeys, function(loadedPrefs) { |
| - let loadedKeys = Object.keys(loadedPrefs); |
| - if (loadedKeys.length > 0) { |
| - for (let key of loadedKeys) { |
| + let updatedPrefs = {}; |
| + for (let key of Object.keys(loadedPrefs)) { |
| + if (this.prefs_[key] !== loadedPrefs[key]) { |
| this.prefs_[key] = loadedPrefs[key]; |
| + updatedPrefs[key] = loadedPrefs[key]; |
| } |
| - let event = new CustomEvent('prefsUpdate', {'detail': loadedPrefs}); |
| + } |
| + if (Object.keys(updatedPrefs).length > 0) { |
| + let event = new CustomEvent('prefsUpdate', {'detail': updatedPrefs}); |
| document.dispatchEvent(event); |
| } |
| }.bind(this)); |
| }, |
| /** |
| - * Store any changes to chrome.storage.sync in this.prefs_. |
| + * Store any changes from chrome.storage.sync to this.prefs_, if this.prefs_ |
| + * is not already set to that value. If this.prefs_ changes, fire a |
| + * prefsUpdate event. |
| * |
| - * @param {!Object} changes |
| + * @param {!Object} storageChanges |
| * @param {string} areaName |
| * @private |
| */ |
| - handleStorageChange_: function(changes, areaName) { |
| - for (let key of Object.keys(changes)) { |
| - // If pref change happened on same device, prefs will already be updated, |
| - // so this will have no effect. |
| - this.prefs_[key] = changes[key].newValue; |
| - changes[key] = changes[key].newValue; |
| + handleStorageChange_: function(storageChanges, areaName) { |
| + let updatedPrefs = {}; |
| + for (let key of Object.keys(storageChanges)) { |
| + if (this.prefs_[key] !== storageChanges[key].newValue) { |
| + this.prefs_[key] = storageChanges[key].newValue; |
| + updatedPrefs[key] = storageChanges[key].newValue; |
| + } |
| + } |
| + if (Object.keys(updatedPrefs).length > 0) { |
| + 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 '
|
| + document.dispatchEvent(event); |
| } |
| - |
| - let event = new CustomEvent('prefsUpdate', {'detail': changes}); |
| - document.dispatchEvent(event); |
| }, |
| /** |
| - * Set the value of the preference |key| to |value|. |
| + * Set the value of the preference |key| to |value| in chrome.storage.sync. |
| + * this.prefs_ is not set until handleStorageChange_. |
| * |
| * @param {string} key |
| * @param {boolean|string|number} value |
| @@ -68,14 +78,16 @@ SwitchAccessPrefs.prototype = { |
| let pref = {}; |
| pref[key] = value; |
| chrome.storage.sync.set(pref); |
| - this.prefs_[key] = value; |
| }, |
| /** |
| - * Get all user preferences. |
| + * Get the value of the preference |key|. |
| + * |
| + * @param {string} key |
| + * @return {boolean|string|number} |
| */ |
| - getPrefs: function() { |
| - return Object.assign({}, this.prefs_); |
| + getPref: function(key) { |
| + return this.prefs_[key]; |
| }, |
| /** |
| @@ -86,6 +98,6 @@ SwitchAccessPrefs.prototype = { |
| */ |
| DEFAULT_PREFS: { |
| 'enableAutoScan': false, |
| - 'autoScanTime': .75 |
| + 'autoScanTime': .8 |
| } |
| }; |