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 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 }, | 201 }, |
202 | 202 |
203 /** | 203 /** |
204 * Forwards changes to this.prefs to settings-prefs-singleton. | 204 * Forwards changes to this.prefs to settings-prefs-singleton. |
205 * @private | 205 * @private |
206 */ | 206 */ |
207 prefsChanged_: function(info) { | 207 prefsChanged_: function(info) { |
208 // Ignore changes that came from singleton_ so we don't re-process | 208 // Ignore changes that came from singleton_ so we don't re-process |
209 // changes made in other instances of this element. | 209 // changes made in other instances of this element. |
210 if (!this.ignoreChanges_) | 210 if (!this.ignoreChanges_) |
211 this.singleton_.fire('prefs-changed', info, {bubbles: false}); | 211 this.singleton_.fire('shared-prefs-changed', info, {bubbles: false}); |
212 }, | 212 }, |
213 | 213 |
214 /** | 214 /** |
215 * Sets ignoreChanged_ before calling the function to suppress change | 215 * Sets ignoreChanged_ before calling the function to suppress change |
216 * events that are manually handled. | 216 * events that are manually handled. |
217 * @param {!function()} fn | 217 * @param {!function()} fn |
218 * @private | 218 * @private |
219 */ | 219 */ |
220 runWhileIgnoringChanges_: function(fn) { | 220 runWhileIgnoringChanges_: function(fn) { |
221 assert(!this.ignoreChanges_, | 221 assert(!this.ignoreChanges_, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 * pref store as of the last update from the API. | 272 * pref store as of the last update from the API. |
273 * @type {Object<*>} | 273 * @type {Object<*>} |
274 * @private | 274 * @private |
275 */ | 275 */ |
276 lastPrefValues_: { | 276 lastPrefValues_: { |
277 type: Object, | 277 type: Object, |
278 value: function() { return {}; }, | 278 value: function() { return {}; }, |
279 }, | 279 }, |
280 }, | 280 }, |
281 | 281 |
282 // Listen for the manually fired prefs-changed event. | 282 // Listen for the manually fired shared-prefs-changed event, fired when |
| 283 // a shared-prefs instance is changed by another element. |
283 listeners: { | 284 listeners: { |
284 'prefs-changed': 'prefsChanged_', | 285 'shared-prefs-changed': 'sharedPrefsChanged_', |
285 }, | 286 }, |
286 | 287 |
287 /** @type {SettingsPrivate} */ | 288 /** @type {SettingsPrivate} */ |
288 settingsApi_: /** @type {SettingsPrivate} */(chrome.settingsPrivate), | 289 settingsApi_: /** @type {SettingsPrivate} */(chrome.settingsPrivate), |
289 | 290 |
290 /** | 291 /** |
291 * @param {SettingsPrivate=} opt_settingsApi SettingsPrivate implementation | 292 * @param {SettingsPrivate=} opt_settingsApi SettingsPrivate implementation |
292 * to use (chrome.settingsPrivate by default). | 293 * to use (chrome.settingsPrivate by default). |
293 */ | 294 */ |
294 initialize: function(opt_settingsApi) { | 295 initialize: function(opt_settingsApi) { |
295 // Only initialize once (or after resetForTesting() is called). | 296 // Only initialize once (or after resetForTesting() is called). |
296 if (this.initialized_) | 297 if (this.initialized_) |
297 return; | 298 return; |
298 this.initialized_ = true; | 299 this.initialized_ = true; |
299 | 300 |
300 if (opt_settingsApi) | 301 if (opt_settingsApi) |
301 this.settingsApi_ = opt_settingsApi; | 302 this.settingsApi_ = opt_settingsApi; |
302 | 303 |
303 /** @private {function(!Array<!chrome.settingsPrivate.PrefObject>)} */ | 304 /** @private {function(!Array<!chrome.settingsPrivate.PrefObject>)} */ |
304 this.boundPrefsChanged_ = this.onSettingsPrivatePrefsChanged_.bind(this); | 305 this.boundPrefsChanged_ = this.onSettingsPrivatePrefsChanged_.bind(this); |
305 this.settingsApi_.onPrefsChanged.addListener(this.boundPrefsChanged_); | 306 this.settingsApi_.onPrefsChanged.addListener(this.boundPrefsChanged_); |
306 this.settingsApi_.getAllPrefs( | 307 this.settingsApi_.getAllPrefs( |
307 this.onSettingsPrivatePrefsFetched_.bind(this)); | 308 this.onSettingsPrivatePrefsFetched_.bind(this)); |
308 }, | 309 }, |
309 | 310 |
310 /** | 311 /** |
311 * Polymer callback for changes to this.prefs. | 312 * Polymer callback for changes to prefs.* from a shared-prefs element. |
312 * @param {!CustomEvent} e | 313 * @param {!CustomEvent} e |
313 * @param {!{path: string}} change | 314 * @param {!{path: string}} change |
314 * @private | 315 * @private |
315 */ | 316 */ |
316 prefsChanged_: function(e, change) { | 317 sharedPrefsChanged_: function(e, change) { |
317 if (!CrSettingsPrefs.isInitialized) | 318 if (!CrSettingsPrefs.isInitialized) |
318 return; | 319 return; |
319 | 320 |
320 var key = this.getPrefKeyFromPath_(change.path); | 321 var key = this.getPrefKeyFromPath_(change.path); |
321 var prefStoreValue = this.lastPrefValues_[key]; | 322 var prefStoreValue = this.lastPrefValues_[key]; |
322 | 323 |
323 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( | 324 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( |
324 this.get(key, this.prefs)); | 325 this.get(key, this.prefs)); |
325 | 326 |
326 // If settingsPrivate already has this value, do nothing. (Otherwise, | 327 // If settingsPrivate already has this value, ignore it. (Otherwise, |
327 // a change event from settingsPrivate could make us call | 328 // a change event from settingsPrivate could make us call |
328 // settingsPrivate.setPref and potentially trigger an IPC loop.) | 329 // settingsPrivate.setPref and potentially trigger an IPC loop.) |
329 if (deepEqual(prefStoreValue, prefObj.value)) | 330 if (!deepEqual(prefStoreValue, prefObj.value)) { |
330 return; | 331 this.settingsApi_.setPref( |
| 332 key, |
| 333 prefObj.value, |
| 334 /* pageId */ '', |
| 335 /* callback */ this.setPrefCallback_.bind(this, key)); |
| 336 } |
331 | 337 |
332 this.settingsApi_.setPref( | 338 // Package the event as a prefs-changed event for other elements. |
333 key, | 339 this.fire('prefs-changed', change); |
334 prefObj.value, | |
335 /* pageId */ '', | |
336 /* callback */ this.setPrefCallback_.bind(this, key)); | |
337 }, | 340 }, |
338 | 341 |
339 /** | 342 /** |
340 * Called when prefs in the underlying Chrome pref store are changed. | 343 * Called when prefs in the underlying Chrome pref store are changed. |
341 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 344 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
342 * The prefs that changed. | 345 * The prefs that changed. |
343 * @private | 346 * @private |
344 */ | 347 */ |
345 onSettingsPrivatePrefsChanged_: function(prefs) { | 348 onSettingsPrivatePrefsChanged_: function(prefs) { |
346 if (CrSettingsPrefs.isInitialized) | 349 if (CrSettingsPrefs.isInitialized) |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 this.prefs = undefined; | 434 this.prefs = undefined; |
432 this.lastPrefValues_ = {}; | 435 this.lastPrefValues_ = {}; |
433 this.initialized_ = false; | 436 this.initialized_ = false; |
434 // Remove the listener added in initialize(). | 437 // Remove the listener added in initialize(). |
435 this.settingsApi_.onPrefsChanged.removeListener(this.boundPrefsChanged_); | 438 this.settingsApi_.onPrefsChanged.removeListener(this.boundPrefsChanged_); |
436 this.settingsApi_ = | 439 this.settingsApi_ = |
437 /** @type {SettingsPrivate} */(chrome.settingsPrivate); | 440 /** @type {SettingsPrivate} */(chrome.settingsPrivate); |
438 }, | 441 }, |
439 }); | 442 }); |
440 })(); | 443 })(); |
OLD | NEW |