| OLD | NEW |
| 1 /* Copyright 2015 The Chromium Authors. All rights reserved. | 1 /* Copyright 2015 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 * @fileoverview | 6 * @fileoverview |
| 7 * 'settings-prefs' exposes a singleton model of Chrome settings and | 7 * 'settings-prefs' exposes a singleton model of Chrome settings and |
| 8 * preferences, which listens to changes to Chrome prefs whitelisted in | 8 * preferences, which listens to changes to Chrome prefs whitelisted in |
| 9 * chrome.settingsPrivate. When changing prefs in this element's 'prefs' | 9 * chrome.settingsPrivate. When changing prefs in this element's 'prefs' |
| 10 * property via the UI, the singleton model tries to set those preferences in | 10 * property via the UI, the singleton model tries to set those preferences in |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 * @param {*} val2 Value to compare with val1. | 22 * @param {*} val2 Value to compare with val1. |
| 23 * @return {boolean} True if the values are recursively equal. | 23 * @return {boolean} True if the values are recursively equal. |
| 24 */ | 24 */ |
| 25 function deepEqual(val1, val2) { | 25 function deepEqual(val1, val2) { |
| 26 if (val1 === val2) | 26 if (val1 === val2) |
| 27 return true; | 27 return true; |
| 28 | 28 |
| 29 if (Array.isArray(val1) || Array.isArray(val2)) { | 29 if (Array.isArray(val1) || Array.isArray(val2)) { |
| 30 if (!Array.isArray(val1) || !Array.isArray(val2)) | 30 if (!Array.isArray(val1) || !Array.isArray(val2)) |
| 31 return false; | 31 return false; |
| 32 return arraysEqual(/** @type {!Array} */(val1), | 32 return arraysEqual( |
| 33 /** @type {!Array} */(val2)); | 33 /** @type {!Array} */ (val1), |
| 34 /** @type {!Array} */ (val2)); |
| 34 } | 35 } |
| 35 | 36 |
| 36 if (val1 instanceof Object && val2 instanceof Object) | 37 if (val1 instanceof Object && val2 instanceof Object) |
| 37 return objectsEqual(val1, val2); | 38 return objectsEqual(val1, val2); |
| 38 | 39 |
| 39 return false; | 40 return false; |
| 40 } | 41 } |
| 41 | 42 |
| 42 /** | 43 /** |
| 43 * @param {!Array} arr1 | 44 * @param {!Array} arr1 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 /** | 80 /** |
| 80 * Returns a recursive copy of the value. | 81 * Returns a recursive copy of the value. |
| 81 * @param {*} val Value to copy. Should be a primitive or only contain | 82 * @param {*} val Value to copy. Should be a primitive or only contain |
| 82 * serializable data (primitives, serializable arrays and | 83 * serializable data (primitives, serializable arrays and |
| 83 * serializable objects). | 84 * serializable objects). |
| 84 * @return {*} A deep copy of the value. | 85 * @return {*} A deep copy of the value. |
| 85 */ | 86 */ |
| 86 function deepCopy(val) { | 87 function deepCopy(val) { |
| 87 if (!(val instanceof Object)) | 88 if (!(val instanceof Object)) |
| 88 return val; | 89 return val; |
| 89 return Array.isArray(val) ? deepCopyArray(/** @type {!Array} */(val)) : | 90 return Array.isArray(val) ? deepCopyArray(/** @type {!Array} */ (val)) : |
| 90 deepCopyObject(val); | 91 deepCopyObject(val); |
| 91 }; | 92 }; |
| 92 | 93 |
| 93 /** | 94 /** |
| 94 * @param {!Array} arr | 95 * @param {!Array} arr |
| 95 * @return {!Array} Deep copy of the array. | 96 * @return {!Array} Deep copy of the array. |
| 96 */ | 97 */ |
| 97 function deepCopyArray(arr) { | 98 function deepCopyArray(arr) { |
| 98 var copy = []; | 99 var copy = []; |
| 99 for (var i = 0; i < arr.length; i++) | 100 for (var i = 0; i < arr.length; i++) |
| 100 copy.push(deepCopy(arr[i])); | 101 copy.push(deepCopy(arr[i])); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 129 }, | 130 }, |
| 130 | 131 |
| 131 /** | 132 /** |
| 132 * Map of pref keys to values representing the state of the Chrome | 133 * Map of pref keys to values representing the state of the Chrome |
| 133 * pref store as of the last update from the API. | 134 * pref store as of the last update from the API. |
| 134 * @type {Object<*>} | 135 * @type {Object<*>} |
| 135 * @private | 136 * @private |
| 136 */ | 137 */ |
| 137 lastPrefValues_: { | 138 lastPrefValues_: { |
| 138 type: Object, | 139 type: Object, |
| 139 value: function() { return {}; }, | 140 value: function() { |
| 141 return {}; |
| 142 }, |
| 140 }, | 143 }, |
| 141 }, | 144 }, |
| 142 | 145 |
| 143 observers: [ | 146 observers: [ |
| 144 'prefsChanged_(prefs.*)', | 147 'prefsChanged_(prefs.*)', |
| 145 ], | 148 ], |
| 146 | 149 |
| 147 /** @type {SettingsPrivate} */ | 150 /** @type {SettingsPrivate} */ |
| 148 settingsApi_: /** @type {SettingsPrivate} */(chrome.settingsPrivate), | 151 settingsApi_: /** @type {SettingsPrivate} */ (chrome.settingsPrivate), |
| 149 | 152 |
| 150 created: function() { | 153 created: function() { |
| 151 if (!CrSettingsPrefs.deferInitialization) | 154 if (!CrSettingsPrefs.deferInitialization) |
| 152 this.initialize(); | 155 this.initialize(); |
| 153 }, | 156 }, |
| 154 | 157 |
| 155 /** | 158 /** |
| 156 * @param {SettingsPrivate=} opt_settingsApi SettingsPrivate implementation | 159 * @param {SettingsPrivate=} opt_settingsApi SettingsPrivate implementation |
| 157 * to use (chrome.settingsPrivate by default). | 160 * to use (chrome.settingsPrivate by default). |
| 158 */ | 161 */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 177 * @private | 180 * @private |
| 178 */ | 181 */ |
| 179 prefsChanged_: function(e) { | 182 prefsChanged_: function(e) { |
| 180 // |prefs| can be directly set or unset in tests. | 183 // |prefs| can be directly set or unset in tests. |
| 181 if (!CrSettingsPrefs.isInitialized || e.path == 'prefs') | 184 if (!CrSettingsPrefs.isInitialized || e.path == 'prefs') |
| 182 return; | 185 return; |
| 183 | 186 |
| 184 var key = this.getPrefKeyFromPath_(e.path); | 187 var key = this.getPrefKeyFromPath_(e.path); |
| 185 var prefStoreValue = this.lastPrefValues_[key]; | 188 var prefStoreValue = this.lastPrefValues_[key]; |
| 186 | 189 |
| 187 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( | 190 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */ ( |
| 188 this.get(key, this.prefs)); | 191 this.get(key, this.prefs)); |
| 189 | 192 |
| 190 // If settingsPrivate already has this value, ignore it. (Otherwise, | 193 // If settingsPrivate already has this value, ignore it. (Otherwise, |
| 191 // a change event from settingsPrivate could make us call | 194 // a change event from settingsPrivate could make us call |
| 192 // settingsPrivate.setPref and potentially trigger an IPC loop.) | 195 // settingsPrivate.setPref and potentially trigger an IPC loop.) |
| 193 if (!deepEqual(prefStoreValue, prefObj.value)) { | 196 if (!deepEqual(prefStoreValue, prefObj.value)) { |
| 194 this.settingsApi_.setPref( | 197 this.settingsApi_.setPref( |
| 195 key, | 198 key, prefObj.value, |
| 196 prefObj.value, | |
| 197 /* pageId */ '', | 199 /* pageId */ '', |
| 198 /* callback */ this.setPrefCallback_.bind(this, key)); | 200 /* callback */ this.setPrefCallback_.bind(this, key)); |
| 199 } | 201 } |
| 200 }, | 202 }, |
| 201 | 203 |
| 202 /** | 204 /** |
| 203 * Called when prefs in the underlying Chrome pref store are changed. | 205 * Called when prefs in the underlying Chrome pref store are changed. |
| 204 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 206 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
| 205 * The prefs that changed. | 207 * The prefs that changed. |
| 206 * @private | 208 * @private |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 */ | 297 */ |
| 296 resetForTesting: function() { | 298 resetForTesting: function() { |
| 297 if (!this.initialized_) | 299 if (!this.initialized_) |
| 298 return; | 300 return; |
| 299 this.prefs = undefined; | 301 this.prefs = undefined; |
| 300 this.lastPrefValues_ = {}; | 302 this.lastPrefValues_ = {}; |
| 301 this.initialized_ = false; | 303 this.initialized_ = false; |
| 302 // Remove the listener added in initialize(). | 304 // Remove the listener added in initialize(). |
| 303 this.settingsApi_.onPrefsChanged.removeListener(this.boundPrefsChanged_); | 305 this.settingsApi_.onPrefsChanged.removeListener(this.boundPrefsChanged_); |
| 304 this.settingsApi_ = | 306 this.settingsApi_ = |
| 305 /** @type {SettingsPrivate} */(chrome.settingsPrivate); | 307 /** @type {SettingsPrivate} */ (chrome.settingsPrivate); |
| 306 }, | 308 }, |
| 307 }); | 309 }); |
| 308 })(); | 310 })(); |
| OLD | NEW |