| 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 function SwitchAccessPrefs() { |
| 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}); |
| 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 type 'boolean' of the preference |key|. Will throw a type |
| 85 * error if the value of |key| is not 'boolean'. |
| 86 * |
| 87 * @param {string} key |
| 88 * @return {boolean} |
| 76 */ | 89 */ |
| 77 getPrefs: function() { | 90 getBooleanPref: function(key) { |
| 78 return Object.assign({}, this.prefs_); | 91 let value = this.prefs_[key]; |
| 92 if (typeof value === 'boolean') |
| 93 return value; |
| 94 else |
| 95 throw new TypeError('No value of boolean type for key \'' + key + '\''); |
| 79 }, | 96 }, |
| 80 | 97 |
| 81 /** | 98 /** |
| 99 * Get the value of type 'number' of the preference |key|. Will throw a type |
| 100 * error if the value of |key| is not 'number'. |
| 101 * |
| 102 * @param {string} key |
| 103 * @return {number} |
| 104 */ |
| 105 getNumberPref: function(key) { |
| 106 let value = this.prefs_[key]; |
| 107 if (typeof value === 'number') |
| 108 return value; |
| 109 else |
| 110 throw new TypeError('No value of number type for key \'' + key + '\''); |
| 111 }, |
| 112 |
| 113 /** |
| 114 * Get the value of type 'string' of the preference |key|. Will throw a type |
| 115 * error if the value of |key| is not 'string'. |
| 116 * |
| 117 * @param {string} key |
| 118 * @return {string} |
| 119 */ |
| 120 getStringPref: function(key) { |
| 121 let value = this.prefs_[key]; |
| 122 if (typeof value === 'string') |
| 123 return value; |
| 124 else |
| 125 throw new TypeError('No value of string type for key \'' + key + '\''); |
| 126 }, |
| 127 |
| 128 /** |
| 82 * The default value of all preferences. All preferences should be primitives | 129 * The default value of all preferences. All preferences should be primitives |
| 83 * to prevent changes to default values. | 130 * to prevent changes to default values. |
| 84 * | 131 * |
| 85 * @const | 132 * @const |
| 86 */ | 133 */ |
| 87 DEFAULT_PREFS: { | 134 DEFAULT_PREFS: { |
| 88 'enableAutoScan': false, | 135 'enableAutoScan': false, |
| 89 'autoScanTime': .75 | 136 'autoScanTime': 800 |
| 90 } | 137 } |
| 91 }; | 138 }; |
| OLD | NEW |