| 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 value: document.createElement('settings-prefs-singleton'), | 145 value: document.createElement('settings-prefs-singleton'), |
| 146 }, | 146 }, |
| 147 }, | 147 }, |
| 148 | 148 |
| 149 observers: [ | 149 observers: [ |
| 150 'prefsChanged_(prefs.*)', | 150 'prefsChanged_(prefs.*)', |
| 151 ], | 151 ], |
| 152 | 152 |
| 153 /** @override */ | 153 /** @override */ |
| 154 ready: function() { | 154 ready: function() { |
| 155 this.singleton_.initialize(); | 155 // Register a callback on CrSettingsPrefs.initialized immediately so prefs |
| 156 // is set as soon as the settings API returns. This enables other elements |
| 157 // dependent on |prefs| to add their own callbacks to |
| 158 // CrSettingsPrefs.initialized. |
| 156 this.startListening_(); | 159 this.startListening_(); |
| 160 if (!CrSettingsPrefs.deferInitialization) |
| 161 this.initialize(); |
| 157 }, | 162 }, |
| 158 | 163 |
| 159 /** | 164 /** |
| 160 * Binds this.prefs to the settings-prefs-singleton's shared prefs once | 165 * Binds this.prefs to the settings-prefs-singleton's shared prefs once |
| 161 * preferences are initialized. | 166 * preferences are initialized. |
| 162 * @private | 167 * @private |
| 163 */ | 168 */ |
| 164 startListening_: function() { | 169 startListening_: function() { |
| 165 CrSettingsPrefs.initialized.then(function() { | 170 CrSettingsPrefs.initialized.then(function() { |
| 166 // Ignore changes to prevent prefsChanged_ from notifying singleton_. | 171 // Ignore changes to prevent prefsChanged_ from notifying singleton_. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 runWhileIgnoringChanges_: function(fn) { | 220 runWhileIgnoringChanges_: function(fn) { |
| 216 assert(!this.ignoreChanges_, | 221 assert(!this.ignoreChanges_, |
| 217 'Nested calls to runWhileIgnoringChanges_ are not supported'); | 222 'Nested calls to runWhileIgnoringChanges_ are not supported'); |
| 218 this.ignoreChanges_ = true; | 223 this.ignoreChanges_ = true; |
| 219 fn.call(this); | 224 fn.call(this); |
| 220 // We can unset ignoreChanges_ now because change notifications | 225 // We can unset ignoreChanges_ now because change notifications |
| 221 // are synchronous. | 226 // are synchronous. |
| 222 this.ignoreChanges_ = false; | 227 this.ignoreChanges_ = false; |
| 223 }, | 228 }, |
| 224 | 229 |
| 230 /** Initializes the singleton, which will fetch the prefs. */ |
| 231 initialize: function() { |
| 232 this.singleton_.initialize(); |
| 233 }, |
| 234 |
| 235 /** |
| 236 * Used to initialize the singleton with a fake SettingsPrivate. |
| 237 * @param {SettingsPrivate} settingsApi Fake implementation to use. |
| 238 */ |
| 239 initializeForTesting: function(settingsApi) { |
| 240 this.singleton_.initialize(settingsApi); |
| 241 }, |
| 242 |
| 225 /** | 243 /** |
| 226 * Uninitializes this element to remove it from tests. Also resets | 244 * Uninitializes this element to remove it from tests. Also resets |
| 227 * settings-prefs-singleton, allowing newly created elements to | 245 * settings-prefs-singleton, allowing newly created elements to |
| 228 * re-initialize it. | 246 * re-initialize it. |
| 229 */ | 247 */ |
| 230 resetForTesting: function() { | 248 resetForTesting: function() { |
| 231 this.stopListening_(); | |
| 232 this.singleton_.resetForTesting(); | 249 this.singleton_.resetForTesting(); |
| 233 }, | 250 }, |
| 234 }); | 251 }); |
| 235 | 252 |
| 236 /** | 253 /** |
| 237 * Privately used element that contains, listens to and updates the shared | 254 * Privately used element that contains, listens to and updates the shared |
| 238 * prefs state. | 255 * prefs state. |
| 239 */ | 256 */ |
| 240 Polymer({ | 257 Polymer({ |
| 241 is: 'settings-prefs-singleton', | 258 is: 'settings-prefs-singleton', |
| (...skipping 18 matching lines...) Expand all Loading... |
| 260 type: Object, | 277 type: Object, |
| 261 value: function() { return {}; }, | 278 value: function() { return {}; }, |
| 262 }, | 279 }, |
| 263 }, | 280 }, |
| 264 | 281 |
| 265 // Listen for the manually fired prefs-changed event. | 282 // Listen for the manually fired prefs-changed event. |
| 266 listeners: { | 283 listeners: { |
| 267 'prefs-changed': 'prefsChanged_', | 284 'prefs-changed': 'prefsChanged_', |
| 268 }, | 285 }, |
| 269 | 286 |
| 270 settingsApi_: chrome.settingsPrivate, | 287 /** @type {SettingsPrivate} */ |
| 288 settingsApi_: /** @type {SettingsPrivate} */(chrome.settingsPrivate), |
| 271 | 289 |
| 272 initialize: function() { | 290 /** |
| 291 * @param {SettingsPrivate=} opt_settingsApi SettingsPrivate implementation |
| 292 * to use (chrome.settingsPrivate by default). |
| 293 */ |
| 294 initialize: function(opt_settingsApi) { |
| 273 // Only initialize once (or after resetForTesting() is called). | 295 // Only initialize once (or after resetForTesting() is called). |
| 274 if (this.initialized_) | 296 if (this.initialized_) |
| 275 return; | 297 return; |
| 276 this.initialized_ = true; | 298 this.initialized_ = true; |
| 277 | 299 |
| 278 // Set window.mockApi to pass a custom settings API, i.e. for tests. | 300 if (opt_settingsApi) |
| 279 // TODO(michaelpg): don't use a global. | 301 this.settingsApi_ = opt_settingsApi; |
| 280 if (window.mockApi) | |
| 281 this.settingsApi_ = window.mockApi; | |
| 282 | 302 |
| 283 this.settingsApi_.onPrefsChanged.addListener( | 303 this.settingsApi_.onPrefsChanged.addListener( |
| 284 this.onSettingsPrivatePrefsChanged_.bind(this)); | 304 this.onSettingsPrivatePrefsChanged_.bind(this)); |
| 285 this.settingsApi_.getAllPrefs( | 305 this.settingsApi_.getAllPrefs( |
| 286 this.onSettingsPrivatePrefsFetched_.bind(this)); | 306 this.onSettingsPrivatePrefsFetched_.bind(this)); |
| 287 }, | 307 }, |
| 288 | 308 |
| 289 /** | 309 /** |
| 290 * Polymer callback for changes to this.prefs. | 310 * Polymer callback for changes to this.prefs. |
| 291 * @param {!CustomEvent} e | 311 * @param {!CustomEvent} e |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 if (this.lastPrefValues_.hasOwnProperty(key)) | 418 if (this.lastPrefValues_.hasOwnProperty(key)) |
| 399 return key; | 419 return key; |
| 400 } | 420 } |
| 401 return ''; | 421 return ''; |
| 402 }, | 422 }, |
| 403 | 423 |
| 404 /** | 424 /** |
| 405 * Resets the element so it can be re-initialized with a new prefs state. | 425 * Resets the element so it can be re-initialized with a new prefs state. |
| 406 */ | 426 */ |
| 407 resetForTesting: function() { | 427 resetForTesting: function() { |
| 428 if (!this.initialized_) |
| 429 return; |
| 408 this.prefs = undefined; | 430 this.prefs = undefined; |
| 409 this.lastPrefValues_ = {}; | 431 this.lastPrefValues_ = {}; |
| 410 this.initialized_ = false; | 432 this.initialized_ = false; |
| 433 this.settingsApi_ = |
| 434 /** @type {SettingsPrivate} */(chrome.settingsPrivate); |
| 411 }, | 435 }, |
| 412 }); | 436 }); |
| 413 })(); | 437 })(); |
| OLD | NEW |