Chromium Code Reviews| 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 * 'cr-settings-prefs' is an element which serves as a model for | 7 * 'cr-settings-prefs' models Chrome settings and preferences, listening for |
| 8 * interaction with settings which are stored in Chrome's | 8 * changes to Chrome prefs whitelisted in chrome.settingsPrivate. |
| 9 * Preferences. | 9 * When changing prefs in this element's 'prefs' property via the UI, this |
| 10 * element tries to set those preferences in Chrome. Whether or not the calls to | |
| 11 * settingsPrivate.setPref succeed, 'prefs' is eventually consistent with the | |
| 12 * Chrome pref store. | |
| 10 * | 13 * |
| 11 * Example: | 14 * Example: |
| 12 * | 15 * |
| 13 * <cr-settings-prefs id="prefs"></cr-settings-prefs> | 16 * <cr-settings-prefs prefs="{{prefs}}"></cr-settings-prefs> |
| 14 * <cr-settings-a11y-page prefs="{{this.$.prefs}}"></cr-settings-a11y-page> | 17 * <cr-settings-a11y-page prefs="{{prefs}}"></cr-settings-a11y-page> |
| 15 * | 18 * |
| 16 * @group Chrome Settings Elements | 19 * @group Chrome Settings Elements |
| 17 * @element cr-settings-a11y-page | 20 * @element cr-settings-prefs |
| 18 */ | 21 */ |
| 19 (function() { | 22 (function() { |
| 20 'use strict'; | 23 'use strict'; |
| 21 | 24 |
| 25 /** | |
| 26 * Pref state object. Copies values of PrefObjects received from | |
| 27 * settingsPrivate to determine when pref values have changed. | |
| 28 * This prototype works for primitive types, but more complex types should | |
| 29 * override these functions. | |
| 30 * @constructor | |
| 31 * @param {!PrefObject} prefObj | |
| 32 */ | |
| 33 function PrefWrapper(prefObj) { | |
| 34 this.key_ = prefObj.key_; | |
| 35 this.value_ = prefObj.value; | |
| 36 } | |
| 37 | |
| 38 /** | |
| 39 * Checks if other's value equals this.value_. Both prefs wrapped by the | |
| 40 * PrefWrappers should have the same keys. | |
| 41 * @param {PrefWrapper} other | |
| 42 * @return {boolean} | |
| 43 */ | |
| 44 PrefWrapper.prototype.equals = function(other) { | |
| 45 assert(this.key_ == other.key_); | |
| 46 return this.value_ == other.value_; | |
| 47 }; | |
| 48 | |
| 49 /** | |
| 50 * @constructor | |
| 51 * @extends {PrefWrapper} | |
| 52 * @param {!PrefObject} prefObj | |
| 53 */ | |
| 54 function ListPrefWrapper(prefObj) { | |
| 55 // Copy the array so changes to prefObj aren't reflected in this.value_. | |
| 56 // TODO(michaelpg): Do a deep copy to support nested lists and objects. | |
| 57 this.value_ = prefObj.value.slice(); | |
| 58 } | |
| 59 | |
| 60 ListPrefWrapper.prototype = { | |
| 61 __proto__: PrefWrapper.prototype, | |
| 62 | |
| 63 /** | |
| 64 * @override | |
| 65 * @param {ListPrefWrapper} other | |
| 66 * @return {boolean} | |
| 67 */ | |
| 68 equals: function(other) { | |
| 69 assert(this.key_ == other.key_); | |
| 70 // Two arrays might have the same values, so don't just use "==". | |
|
stevenjb
2015/08/28 17:35:20
This comment doesn't make much sense to me. IIUC,
michaelpg
2015/08/28 18:51:45
Done.
| |
| 71 return this.arraysEqual_(this.value_, other.value_); | |
| 72 }, | |
| 73 | |
| 74 /** | |
| 75 * Tests whether two arrays contain the same primitives. | |
| 76 * @param {Array} arr1 | |
| 77 * @param {Array} arr2 | |
| 78 * @return {boolean} True if the arrays contain equal values. | |
| 79 * @private | |
| 80 */ | |
| 81 arraysEqual_: function(arr1, arr2) { | |
| 82 if (arr1.length != arr2.length) | |
| 83 return false; | |
| 84 for (let i = 0; i < arr1.length; i++) { | |
| 85 if (arr1[i] != arr2[i]) | |
| 86 return false; | |
| 87 } | |
| 88 return true; | |
| 89 }, | |
| 90 }; | |
| 91 | |
| 22 Polymer({ | 92 Polymer({ |
| 23 is: 'cr-settings-prefs', | 93 is: 'cr-settings-prefs', |
| 24 | 94 |
| 25 properties: { | 95 properties: { |
| 26 /** | 96 /** |
| 27 * Object containing all preferences. | 97 * Object containing all preferences, for use by Polymer controls. |
| 28 */ | 98 */ |
| 29 prefStore: { | 99 prefs: { |
| 30 type: Object, | 100 type: Object, |
| 31 value: function() { return {}; }, | 101 value: function() { return {}; }, |
| 32 notify: true, | 102 notify: true, |
| 33 }, | 103 }, |
| 104 | |
| 105 /** | |
| 106 * Map of pref keys to PrefWrapper objects representing the state of the | |
| 107 * Chrome pref store. | |
| 108 * @type {Object<PrefWrapper>} | |
| 109 * @private | |
| 110 */ | |
| 111 settingsPrivateWrappers_: { | |
|
Dan Beam
2015/08/28 02:05:36
nit: wrappers_ or prefWrappers_
stevenjb
2015/08/28 17:35:21
FWIW I personally prefer the more explicit name to
michaelpg
2015/08/28 18:51:45
Done.
michaelpg
2015/08/28 18:51:45
Acknowledged.
| |
| 112 type: Object, | |
| 113 value: function() { return {}; }, | |
| 114 }, | |
| 34 }, | 115 }, |
| 35 | 116 |
| 117 observers: [ | |
| 118 'prefsChanged_(prefs.*)', | |
| 119 ], | |
| 120 | |
| 36 /** @override */ | 121 /** @override */ |
| 37 created: function() { | 122 created: function() { |
| 38 CrSettingsPrefs.isInitialized = false; | 123 CrSettingsPrefs.isInitialized = false; |
| 39 | 124 |
| 40 chrome.settingsPrivate.onPrefsChanged.addListener( | 125 chrome.settingsPrivate.onPrefsChanged.addListener( |
| 41 this.onPrefsChanged_.bind(this)); | 126 this.onSettingsPrivatePrefsChanged_.bind(this)); |
| 42 chrome.settingsPrivate.getAllPrefs(this.onPrefsFetched_.bind(this)); | 127 chrome.settingsPrivate.getAllPrefs( |
| 128 this.onSettingsPrivatePrefsFetched_.bind(this)); | |
| 43 }, | 129 }, |
| 44 | 130 |
| 45 /** | 131 /** |
| 132 * Polymer callback for changes to this.prefs. | |
| 133 * @param {!{path: string, value: *}} change | |
| 134 * @private | |
| 135 */ | |
| 136 prefsChanged_: function(change) { | |
| 137 if (!CrSettingsPrefs.isInitialized) | |
| 138 return; | |
| 139 | |
| 140 var key = this.getPrefKeyFromPath_(change.path); | |
| 141 var prefWrapper = this.settingsPrivateWrappers_[key]; | |
|
Dan Beam
2015/08/28 02:05:36
evidence that settingsPrivateWrappers_ should be n
michaelpg
2015/08/28 18:51:45
Done.
| |
| 142 if (!prefWrapper) | |
|
Dan Beam
2015/08/28 02:05:36
out of curiosity, when should this happen? never?
stevenjb
2015/08/28 17:35:21
IIUC, this would happen if UI referenced a pref th
michaelpg
2015/08/28 18:51:46
<cr-settings-pref-tracker> detects this case more
| |
| 143 return; | |
| 144 | |
| 145 var prefObj = this.get(key, this.prefs); | |
| 146 | |
| 147 // If settingsPrivate already has this value, do nothing. (Otherwise, | |
| 148 // a change event from settingsPrivate could make us call | |
| 149 // settingsPrivate.setPref and potentially trigger an IPC loop.) | |
| 150 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) | |
| 151 return; | |
| 152 | |
| 153 chrome.settingsPrivate.setPref( | |
| 154 key, | |
| 155 prefObj.value, | |
| 156 /* pageId */ '', | |
| 157 /* callback */ this.setPrefCallback_.bind(this, key)); | |
| 158 }, | |
| 159 | |
| 160 /** | |
| 46 * Called when prefs in the underlying Chrome pref store are changed. | 161 * Called when prefs in the underlying Chrome pref store are changed. |
| 47 * @param {!Array<!PrefObject>} prefs The prefs that changed. | 162 * @param {!Array<!PrefObject>} prefs The prefs that changed. |
| 48 * @private | 163 * @private |
| 49 */ | 164 */ |
| 50 onPrefsChanged_: function(prefs) { | 165 onSettingsPrivatePrefsChanged_: function(prefs) { |
| 51 this.updatePrefs_(prefs, false); | 166 if (CrSettingsPrefs.isInitialized) |
| 167 this.updatePrefs_(prefs); | |
| 52 }, | 168 }, |
| 53 | 169 |
| 54 /** | 170 /** |
| 55 * Called when prefs are fetched from settingsPrivate. | 171 * Called when prefs are fetched from settingsPrivate. |
| 56 * @param {!Array<!PrefObject>} prefs | 172 * @param {!Array<!PrefObject>} prefs |
| 57 * @private | 173 * @private |
| 58 */ | 174 */ |
| 59 onPrefsFetched_: function(prefs) { | 175 onSettingsPrivatePrefsFetched_: function(prefs) { |
| 60 this.updatePrefs_(prefs, true); | 176 this.updatePrefs_(prefs); |
| 61 | 177 |
| 62 CrSettingsPrefs.isInitialized = true; | 178 CrSettingsPrefs.isInitialized = true; |
| 63 document.dispatchEvent(new Event(CrSettingsPrefs.INITIALIZED)); | 179 document.dispatchEvent(new Event(CrSettingsPrefs.INITIALIZED)); |
| 64 }, | 180 }, |
| 65 | 181 |
| 182 /** | |
| 183 * Checks the result of calling settingsPrivate.setPref. | |
| 184 * @param {string} key The key used in the call to setPref. | |
| 185 * @param {boolean} success True if setting the pref succeeded. | |
| 186 * @private | |
| 187 */ | |
| 188 setPrefCallback_: function(key, success) { | |
| 189 if (success) | |
| 190 return; | |
| 191 | |
| 192 // Get the current pref value from chrome.settingsPrivate to ensure the | |
| 193 // UI stays up to date. | |
| 194 chrome.settingsPrivate.getPref(key, function(pref) { | |
| 195 this.updatePrefs_([pref]); | |
| 196 }.bind(this)); | |
| 197 }, | |
| 66 | 198 |
| 67 /** | 199 /** |
| 68 * Updates the settings model with the given prefs. | 200 * Updates the prefs model with the given prefs. |
| 69 * @param {!Array<!PrefObject>} prefs | 201 * @param {!Array<!PrefObject>} prefs |
| 70 * @param {boolean} shouldObserve Whether each of the prefs should be | |
| 71 * observed. | |
| 72 * @private | 202 * @private |
| 73 */ | 203 */ |
| 74 updatePrefs_: function(prefs, shouldObserve) { | 204 updatePrefs_: function(prefs) { |
| 75 prefs.forEach(function(prefObj) { | 205 prefs.forEach(function(newPrefObj) { |
| 76 let root = this.prefStore; | 206 // Use the PrefObject from settingsPrivate to create a PrefWrapper in |
| 77 let tokens = prefObj.key.split('.'); | 207 // settingsPrivateWrappers_ at the pref's key. |
| 208 this.settingsPrivateWrappers_[newPrefObj.key] = | |
| 209 this.createPrefWrapper_(newPrefObj); | |
| 78 | 210 |
| 79 assert(tokens.length > 0); | 211 // Set or update the pref in |prefs|. This triggers observers in the UI, |
| 80 | 212 // which update controls associated with the pref. |
| 81 for (let i = 0; i < tokens.length; i++) { | 213 this.setPref_(newPrefObj); |
| 82 let token = tokens[i]; | |
| 83 | |
| 84 if (!root.hasOwnProperty(token)) { | |
| 85 let path = 'prefStore.' + tokens.slice(0, i + 1).join('.'); | |
| 86 this.set(path, {}); | |
| 87 } | |
| 88 root = root[token]; | |
| 89 } | |
| 90 | |
| 91 // NOTE: Do this copy rather than just a re-assignment, so that the | |
| 92 // observer fires. | |
| 93 for (let objKey in prefObj) { | |
| 94 let path = 'prefStore.' + prefObj.key + '.' + objKey; | |
| 95 | |
| 96 // Handle lists specially. We don't want to call this.set() | |
| 97 // unconditionally upon updating a list value, since even its contents | |
| 98 // are the same as the old list, doing this set() may cause an | |
| 99 // infinite update cycle (http://crbug.com/498586). | |
| 100 if (objKey == 'value' && | |
| 101 prefObj.type == chrome.settingsPrivate.PrefType.LIST && | |
| 102 !this.shouldUpdateListPrefValue_(root, prefObj['value'])) { | |
| 103 continue; | |
| 104 } | |
| 105 | |
| 106 this.set(path, prefObj[objKey]); | |
| 107 } | |
| 108 | |
| 109 if (shouldObserve) { | |
| 110 Object.observe(root, this.propertyChangeCallback_, ['update']); | |
| 111 } | |
| 112 }, this); | 214 }, this); |
| 113 }, | 215 }, |
| 114 | 216 |
| 217 /** | |
| 218 * Given a 'property-changed' path, returns the key of the preference the | |
| 219 * path refers to. E.g., if the path of the changed property is | |
| 220 * 'prefs.search.suggest_enabled.value', the key of the pref that changed is | |
|
Dan Beam
2015/08/28 02:05:36
why can this just be
return path.split('.').sli
stevenjb
2015/08/28 17:35:20
Until / unless we support dictionary objects, I th
Dan Beam
2015/08/28 17:42:05
can't**
michaelpg
2015/08/28 18:51:45
There is always a "prefs." and a ".value", but aft
| |
| 221 * 'search.suggest_enabled'. | |
| 222 * @param {string} path | |
| 223 * @return {string} | |
| 224 * @private | |
| 225 */ | |
| 226 getPrefKeyFromPath_: function(path) { | |
| 227 // Skip the first token, which refers to the member variable (this.prefs). | |
| 228 var tokens = path.split('.').slice(1); | |
| 115 | 229 |
| 116 /** | 230 for (let i = 1; i <= tokens.length; i++) { |
| 117 * @param {Object} root The root object for a pref that contains a list | 231 let key = tokens.slice(0, i).join('.'); |
| 118 * value. | 232 // The settingsPrivateWrappers_ keys match the pref keys. |
| 119 * @param {!Array} newValue The new list value. | 233 if (this.settingsPrivateWrappers_[key] != undefined) |
| 120 * @return {boolean} Whether the new value is different from the one in | 234 return key; |
| 121 * root, thus necessitating a pref update. | |
| 122 */ | |
| 123 shouldUpdateListPrefValue_: function(root, newValue) { | |
| 124 if (root.value == null || | |
| 125 root.value.length != newValue.length) { | |
| 126 return true; | |
| 127 } | 235 } |
| 128 | 236 return ''; |
| 129 for (let i = 0; i < newValue.length; i++) { | |
| 130 if (root.value != null && root.value[i] != newValue[i]) { | |
| 131 return true; | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 return false; | |
| 136 }, | 237 }, |
| 137 | 238 |
| 138 /** | 239 /** |
| 139 * Called when a property of a pref changes. | 240 * Sets or updates the pref denoted by newPrefObj.key in the publicy exposed |
| 140 * @param {!Array<!Object>} changes An array of objects describing changes. | 241 * |prefs| property. |
| 141 * @see http://www.html5rocks.com/en/tutorials/es7/observe/ | 242 * @param {PrefObject} newPrefObj The pref object to update the pref with. |
| 142 * @private | 243 * @private |
| 143 */ | 244 */ |
| 144 propertyChangeCallback_: function(changes) { | 245 setPref_: function(newPrefObj) { |
| 145 changes.forEach(function(change) { | 246 // Check if the pref exists already in the Polymer |prefs| object. |
| 146 // UI should only be able to change the value of a setting for now, not | 247 if (this.get(newPrefObj.key, this.prefs)) { |
| 147 // disabled, etc. | 248 // Update just the value, notifying listeners of the change. |
| 148 assert(change.name == 'value'); | 249 this.set('prefs.' + newPrefObj.key + '.value', newPrefObj.value); |
|
Dan Beam
2015/08/28 02:05:36
it's unfortunate that this auto expanding object s
michaelpg
2015/08/28 18:51:46
yup. in reality we should be transforming dots to
| |
| 250 } else { | |
| 251 // Add the pref to |prefs|. cr.exportPath doesn't use Polymer.Base.set, | |
| 252 // which is OK because the nested property update events aren't useful. | |
| 253 cr.exportPath(newPrefObj.key, newPrefObj, this.prefs); | |
| 254 // Notify listeners of the change at the preference key. | |
| 255 this.notifyPath('prefs.' + newPrefObj.key, newPrefObj); | |
| 256 } | |
| 257 }, | |
| 149 | 258 |
| 150 let newValue = change.object[change.name]; | 259 /** |
| 151 assert(newValue !== undefined); | 260 * Creates a PrefWrapper object from a chrome.settingsPrivate pref. |
| 152 | 261 * @param {!PrefObject} PrefObject received from chrome.settingsPrivate. |
| 153 chrome.settingsPrivate.setPref( | 262 * @return {PrefWrapper} An object containing a copy of the PrefObject's |
| 154 change.object['key'], | 263 * value. |
| 155 newValue, | 264 * @private |
| 156 /* pageId */ '', | 265 */ |
| 157 /* callback */ function() {}); | 266 createPrefWrapper_: function(prefObj) { |
| 158 }); | 267 return (prefObj.type == chrome.settingsPrivate.PrefType.LIST) ? |
|
Dan Beam
2015/08/28 02:05:36
nit: remove extra ()
michaelpg
2015/08/28 18:51:45
Done.
| |
| 268 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); | |
| 159 }, | 269 }, |
| 160 }); | 270 }); |
| 161 })(); | 271 })(); |
| OLD | NEW |