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 * '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 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 type: Object, | 260 type: Object, |
| 261 value: function() { return {}; }, | 261 value: function() { return {}; }, |
| 262 }, | 262 }, |
| 263 }, | 263 }, |
| 264 | 264 |
| 265 // Listen for the manually fired prefs-changed event. | 265 // Listen for the manually fired prefs-changed event. |
| 266 listeners: { | 266 listeners: { |
| 267 'prefs-changed': 'prefsChanged_', | 267 'prefs-changed': 'prefsChanged_', |
| 268 }, | 268 }, |
| 269 | 269 |
| 270 settingsApi_: chrome.settingsPrivate, | |
| 271 | |
| 272 initialize: function() { | 270 initialize: function() { |
| 273 // Only initialize once (or after resetForTesting() is called). | 271 // Only initialize once (or after resetForTesting() is called). |
| 274 if (this.initialized_) | 272 if (this.initialized_) |
| 275 return; | 273 return; |
| 276 this.initialized_ = true; | 274 this.initialized_ = true; |
| 277 | 275 |
| 278 // Set window.mockApi to pass a custom settings API, i.e. for tests. | 276 chrome.settingsPrivate.onPrefsChanged.addListener( |
|
Dan Beam
2015/11/17 23:01:15
why are you [able to] undo this?
michaelpg
2015/11/17 23:51:51
I don't like this because:
* checking for a glob
| |
| 279 // TODO(michaelpg): don't use a global. | |
| 280 if (window.mockApi) | |
| 281 this.settingsApi_ = window.mockApi; | |
| 282 | |
| 283 this.settingsApi_.onPrefsChanged.addListener( | |
| 284 this.onSettingsPrivatePrefsChanged_.bind(this)); | 277 this.onSettingsPrivatePrefsChanged_.bind(this)); |
| 285 this.settingsApi_.getAllPrefs( | 278 chrome.settingsPrivate.getAllPrefs( |
| 286 this.onSettingsPrivatePrefsFetched_.bind(this)); | 279 this.onSettingsPrivatePrefsFetched_.bind(this)); |
| 287 }, | 280 }, |
| 288 | 281 |
| 289 /** | 282 /** |
| 290 * Polymer callback for changes to this.prefs. | 283 * Polymer callback for changes to this.prefs. |
| 291 * @param {!CustomEvent} e | 284 * @param {!CustomEvent} e |
| 292 * @param {!{path: string}} change | 285 * @param {!{path: string}} change |
| 293 * @private | 286 * @private |
| 294 */ | 287 */ |
| 295 prefsChanged_: function(e, change) { | 288 prefsChanged_: function(e, change) { |
| 296 if (!CrSettingsPrefs.isInitialized) | 289 if (!CrSettingsPrefs.isInitialized) |
| 297 return; | 290 return; |
| 298 | 291 |
| 299 var key = this.getPrefKeyFromPath_(change.path); | 292 var key = this.getPrefKeyFromPath_(change.path); |
| 300 var prefStoreValue = this.lastPrefValues_[key]; | 293 var prefStoreValue = this.lastPrefValues_[key]; |
| 301 | 294 |
| 302 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( | 295 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( |
| 303 this.get(key, this.prefs)); | 296 this.get(key, this.prefs)); |
| 304 | 297 |
| 305 // If settingsPrivate already has this value, do nothing. (Otherwise, | 298 // If settingsPrivate already has this value, do nothing. (Otherwise, |
| 306 // a change event from settingsPrivate could make us call | 299 // a change event from settingsPrivate could make us call |
| 307 // settingsPrivate.setPref and potentially trigger an IPC loop.) | 300 // settingsPrivate.setPref and potentially trigger an IPC loop.) |
| 308 if (deepEqual(prefStoreValue, prefObj.value)) | 301 if (deepEqual(prefStoreValue, prefObj.value)) |
| 309 return; | 302 return; |
| 310 | 303 |
| 311 this.settingsApi_.setPref( | 304 chrome.settingsPrivate.setPref( |
| 312 key, | 305 key, |
| 313 prefObj.value, | 306 prefObj.value, |
| 314 /* pageId */ '', | 307 /* pageId */ '', |
| 315 /* callback */ this.setPrefCallback_.bind(this, key)); | 308 /* callback */ this.setPrefCallback_.bind(this, key)); |
| 316 }, | 309 }, |
| 317 | 310 |
| 318 /** | 311 /** |
| 319 * Called when prefs in the underlying Chrome pref store are changed. | 312 * Called when prefs in the underlying Chrome pref store are changed. |
| 320 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 313 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
| 321 * The prefs that changed. | 314 * The prefs that changed. |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 341 * @param {string} key The key used in the call to setPref. | 334 * @param {string} key The key used in the call to setPref. |
| 342 * @param {boolean} success True if setting the pref succeeded. | 335 * @param {boolean} success True if setting the pref succeeded. |
| 343 * @private | 336 * @private |
| 344 */ | 337 */ |
| 345 setPrefCallback_: function(key, success) { | 338 setPrefCallback_: function(key, success) { |
| 346 if (success) | 339 if (success) |
| 347 return; | 340 return; |
| 348 | 341 |
| 349 // Get the current pref value from chrome.settingsPrivate to ensure the | 342 // Get the current pref value from chrome.settingsPrivate to ensure the |
| 350 // UI stays up to date. | 343 // UI stays up to date. |
| 351 this.settingsApi_.getPref(key, function(pref) { | 344 chrome.settingsPrivate.getPref(key, function(pref) { |
| 352 this.updatePrefs_([pref]); | 345 this.updatePrefs_([pref]); |
| 353 }.bind(this)); | 346 }.bind(this)); |
| 354 }, | 347 }, |
| 355 | 348 |
| 356 /** | 349 /** |
| 357 * Updates the prefs model with the given prefs. | 350 * Updates the prefs model with the given prefs. |
| 358 * @param {!Array<!chrome.settingsPrivate.PrefObject>} newPrefs | 351 * @param {!Array<!chrome.settingsPrivate.PrefObject>} newPrefs |
| 359 * @private | 352 * @private |
| 360 */ | 353 */ |
| 361 updatePrefs_: function(newPrefs) { | 354 updatePrefs_: function(newPrefs) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 404 /** | 397 /** |
| 405 * Resets the element so it can be re-initialized with a new prefs state. | 398 * Resets the element so it can be re-initialized with a new prefs state. |
| 406 */ | 399 */ |
| 407 resetForTesting: function() { | 400 resetForTesting: function() { |
| 408 this.prefs = undefined; | 401 this.prefs = undefined; |
| 409 this.lastPrefValues_ = {}; | 402 this.lastPrefValues_ = {}; |
| 410 this.initialized_ = false; | 403 this.initialized_ = false; |
| 411 }, | 404 }, |
| 412 }); | 405 }); |
| 413 })(); | 406 })(); |
| OLD | NEW |