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' models Chrome settings and preferences, listening for | 7 * 'cr-settings-prefs' models Chrome settings and preferences, listening for |
8 * changes to Chrome prefs whitelisted in chrome.settingsPrivate. | 8 * changes to Chrome prefs whitelisted in chrome.settingsPrivate. |
9 * When changing prefs in this element's 'prefs' property via the UI, this | 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 | 10 * element tries to set those preferences in Chrome. Whether or not the calls to |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 80 |
81 Polymer({ | 81 Polymer({ |
82 is: 'cr-settings-prefs', | 82 is: 'cr-settings-prefs', |
83 | 83 |
84 properties: { | 84 properties: { |
85 /** | 85 /** |
86 * Object containing all preferences, for use by Polymer controls. | 86 * Object containing all preferences, for use by Polymer controls. |
87 */ | 87 */ |
88 prefs: { | 88 prefs: { |
89 type: Object, | 89 type: Object, |
90 value: function() { return {}; }, | |
91 notify: true, | 90 notify: true, |
92 }, | 91 }, |
93 | 92 |
94 /** | 93 /** |
95 * Map of pref keys to PrefWrapper objects representing the state of the | 94 * Map of pref keys to PrefWrapper objects representing the state of the |
96 * Chrome pref store. | 95 * Chrome pref store. |
97 * @type {Object<PrefWrapper>} | 96 * @type {Object<PrefWrapper>} |
98 * @private | 97 * @private |
99 */ | 98 */ |
100 prefWrappers_: { | 99 prefWrappers_: { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 181 |
183 // Get the current pref value from chrome.settingsPrivate to ensure the | 182 // Get the current pref value from chrome.settingsPrivate to ensure the |
184 // UI stays up to date. | 183 // UI stays up to date. |
185 chrome.settingsPrivate.getPref(key, function(pref) { | 184 chrome.settingsPrivate.getPref(key, function(pref) { |
186 this.updatePrefs_([pref]); | 185 this.updatePrefs_([pref]); |
187 }.bind(this)); | 186 }.bind(this)); |
188 }, | 187 }, |
189 | 188 |
190 /** | 189 /** |
191 * Updates the prefs model with the given prefs. | 190 * Updates the prefs model with the given prefs. |
192 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 191 * @param {!Array<!chrome.settingsPrivate.PrefObject>} newPrefs |
193 * @private | 192 * @private |
194 */ | 193 */ |
195 updatePrefs_: function(prefs) { | 194 updatePrefs_: function(newPrefs) { |
196 prefs.forEach(function(newPrefObj) { | 195 // Use the existing prefs object or create it. |
| 196 var prefs = this.prefs || {}; |
| 197 newPrefs.forEach(function(newPrefObj) { |
197 // Use the PrefObject from settingsPrivate to create a PrefWrapper in | 198 // Use the PrefObject from settingsPrivate to create a PrefWrapper in |
198 // prefWrappers_ at the pref's key. | 199 // prefWrappers_ at the pref's key. |
199 this.prefWrappers_[newPrefObj.key] = | 200 this.prefWrappers_[newPrefObj.key] = |
200 this.createPrefWrapper_(newPrefObj); | 201 this.createPrefWrapper_(newPrefObj); |
201 | 202 |
202 // Set or update the pref in |prefs|. This triggers observers in the UI, | 203 // Add the pref to |prefs|. |
203 // which update controls associated with the pref. | 204 cr.exportPath(newPrefObj.key, newPrefObj, prefs); |
204 this.setPref_(newPrefObj); | 205 // If this.prefs already exists, notify listeners of the change. |
| 206 if (prefs == this.prefs) |
| 207 this.notifyPath('prefs.' + newPrefObj.key, newPrefObj); |
205 }, this); | 208 }, this); |
| 209 if (!this.prefs) |
| 210 this.prefs = prefs; |
206 }, | 211 }, |
207 | 212 |
208 /** | 213 /** |
209 * Given a 'property-changed' path, returns the key of the preference the | 214 * Given a 'property-changed' path, returns the key of the preference the |
210 * path refers to. E.g., if the path of the changed property is | 215 * path refers to. E.g., if the path of the changed property is |
211 * 'prefs.search.suggest_enabled.value', the key of the pref that changed is | 216 * 'prefs.search.suggest_enabled.value', the key of the pref that changed is |
212 * 'search.suggest_enabled'. | 217 * 'search.suggest_enabled'. |
213 * @param {string} path | 218 * @param {string} path |
214 * @return {string} | 219 * @return {string} |
215 * @private | 220 * @private |
216 */ | 221 */ |
217 getPrefKeyFromPath_: function(path) { | 222 getPrefKeyFromPath_: function(path) { |
218 // Skip the first token, which refers to the member variable (this.prefs). | 223 // Skip the first token, which refers to the member variable (this.prefs). |
219 var parts = path.split('.'); | 224 var parts = path.split('.'); |
220 assert(parts.shift() == 'prefs'); | 225 assert(parts.shift() == 'prefs'); |
221 | 226 |
222 for (let i = 1; i <= parts.length; i++) { | 227 for (let i = 1; i <= parts.length; i++) { |
223 let key = parts.slice(0, i).join('.'); | 228 let key = parts.slice(0, i).join('.'); |
224 // The prefWrappers_ keys match the pref keys. | 229 // The prefWrappers_ keys match the pref keys. |
225 if (this.prefWrappers_[key] != undefined) | 230 if (this.prefWrappers_[key] != undefined) |
226 return key; | 231 return key; |
227 } | 232 } |
228 return ''; | 233 return ''; |
229 }, | 234 }, |
230 | 235 |
231 /** | 236 /** |
232 * Sets or updates the pref denoted by newPrefObj.key in the publicy exposed | |
233 * |prefs| property. | |
234 * @param {chrome.settingsPrivate.PrefObject} newPrefObj The pref object to | |
235 * update the pref with. | |
236 * @private | |
237 */ | |
238 setPref_: function(newPrefObj) { | |
239 // Check if the pref exists already in the Polymer |prefs| object. | |
240 if (this.get(newPrefObj.key, this.prefs)) { | |
241 // Update just the value, notifying listeners of the change. | |
242 this.set('prefs.' + newPrefObj.key + '.value', newPrefObj.value); | |
243 } else { | |
244 // Add the pref to |prefs|. cr.exportPath doesn't use Polymer.Base.set, | |
245 // which is OK because the nested property update events aren't useful. | |
246 cr.exportPath(newPrefObj.key, newPrefObj, this.prefs); | |
247 // Notify listeners of the change at the preference key. | |
248 this.notifyPath('prefs.' + newPrefObj.key, newPrefObj); | |
249 } | |
250 }, | |
251 | |
252 /** | |
253 * Creates a PrefWrapper object from a chrome.settingsPrivate pref. | 237 * Creates a PrefWrapper object from a chrome.settingsPrivate pref. |
254 * @param {!chrome.settingsPrivate.PrefObject} prefObj | 238 * @param {!chrome.settingsPrivate.PrefObject} prefObj |
255 * PrefObject received from chrome.settingsPrivate. | 239 * PrefObject received from chrome.settingsPrivate. |
256 * @return {PrefWrapper} An object containing a copy of the PrefObject's | 240 * @return {PrefWrapper} An object containing a copy of the PrefObject's |
257 * value. | 241 * value. |
258 * @private | 242 * @private |
259 */ | 243 */ |
260 createPrefWrapper_: function(prefObj) { | 244 createPrefWrapper_: function(prefObj) { |
261 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? | 245 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? |
262 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); | 246 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); |
263 }, | 247 }, |
264 }); | 248 }); |
265 })(); | 249 })(); |
OLD | NEW |