| 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 prefWrappers_: { | 99 prefWrappers_: { |
| 100 type: Object, | 100 type: Object, |
| 101 value: function() { return {}; }, | 101 value: function() { return {}; }, |
| 102 }, | 102 }, |
| 103 }, | 103 }, |
| 104 | 104 |
| 105 observers: [ | 105 observers: [ |
| 106 'prefsChanged_(prefs.*)', | 106 'prefsChanged_(prefs.*)', |
| 107 ], | 107 ], |
| 108 | 108 |
| 109 settingsApi_: chrome.settingsPrivate, |
| 110 |
| 109 /** @override */ | 111 /** @override */ |
| 110 created: function() { | 112 ready: function() { |
| 113 // Set window.mockApi to pass a custom settings API, i.e. for tests. |
| 114 // TODO(michaelpg): don't use a global. |
| 115 if (window.mockApi) |
| 116 this.settingsApi_ = window.mockApi; |
| 111 CrSettingsPrefs.isInitialized = false; | 117 CrSettingsPrefs.isInitialized = false; |
| 112 | 118 |
| 113 chrome.settingsPrivate.onPrefsChanged.addListener( | 119 this.settingsApi_.onPrefsChanged.addListener( |
| 114 this.onSettingsPrivatePrefsChanged_.bind(this)); | 120 this.onSettingsPrivatePrefsChanged_.bind(this)); |
| 115 chrome.settingsPrivate.getAllPrefs( | 121 this.settingsApi_.getAllPrefs( |
| 116 this.onSettingsPrivatePrefsFetched_.bind(this)); | 122 this.onSettingsPrivatePrefsFetched_.bind(this)); |
| 117 }, | 123 }, |
| 118 | 124 |
| 119 /** | 125 /** |
| 120 * Polymer callback for changes to this.prefs. | 126 * Polymer callback for changes to this.prefs. |
| 121 * @param {!{path: string, value: *}} change | 127 * @param {!{path: string, value: *}} change |
| 122 * @private | 128 * @private |
| 123 */ | 129 */ |
| 124 prefsChanged_: function(change) { | 130 prefsChanged_: function(change) { |
| 125 if (!CrSettingsPrefs.isInitialized) | 131 if (!CrSettingsPrefs.isInitialized) |
| 126 return; | 132 return; |
| 127 | 133 |
| 128 var key = this.getPrefKeyFromPath_(change.path); | 134 var key = this.getPrefKeyFromPath_(change.path); |
| 129 var prefWrapper = this.prefWrappers_[key]; | 135 var prefWrapper = this.prefWrappers_[key]; |
| 130 if (!prefWrapper) | 136 if (!prefWrapper) |
| 131 return; | 137 return; |
| 132 | 138 |
| 133 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( | 139 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( |
| 134 this.get(key, this.prefs)); | 140 this.get(key, this.prefs)); |
| 135 | 141 |
| 136 // If settingsPrivate already has this value, do nothing. (Otherwise, | 142 // If settingsPrivate already has this value, do nothing. (Otherwise, |
| 137 // a change event from settingsPrivate could make us call | 143 // a change event from settingsPrivate could make us call |
| 138 // settingsPrivate.setPref and potentially trigger an IPC loop.) | 144 // settingsPrivate.setPref and potentially trigger an IPC loop.) |
| 139 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) | 145 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) |
| 140 return; | 146 return; |
| 141 | 147 |
| 142 chrome.settingsPrivate.setPref( | 148 this.settingsApi_.setPref( |
| 143 key, | 149 key, |
| 144 prefObj.value, | 150 prefObj.value, |
| 145 /* pageId */ '', | 151 /* pageId */ '', |
| 146 /* callback */ this.setPrefCallback_.bind(this, key)); | 152 /* callback */ this.setPrefCallback_.bind(this, key)); |
| 147 }, | 153 }, |
| 148 | 154 |
| 149 /** | 155 /** |
| 150 * Called when prefs in the underlying Chrome pref store are changed. | 156 * Called when prefs in the underlying Chrome pref store are changed. |
| 151 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 157 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
| 152 * The prefs that changed. | 158 * The prefs that changed. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 174 * @param {string} key The key used in the call to setPref. | 180 * @param {string} key The key used in the call to setPref. |
| 175 * @param {boolean} success True if setting the pref succeeded. | 181 * @param {boolean} success True if setting the pref succeeded. |
| 176 * @private | 182 * @private |
| 177 */ | 183 */ |
| 178 setPrefCallback_: function(key, success) { | 184 setPrefCallback_: function(key, success) { |
| 179 if (success) | 185 if (success) |
| 180 return; | 186 return; |
| 181 | 187 |
| 182 // Get the current pref value from chrome.settingsPrivate to ensure the | 188 // Get the current pref value from chrome.settingsPrivate to ensure the |
| 183 // UI stays up to date. | 189 // UI stays up to date. |
| 184 chrome.settingsPrivate.getPref(key, function(pref) { | 190 this.settingsApi_.getPref(key, function(pref) { |
| 185 this.updatePrefs_([pref]); | 191 this.updatePrefs_([pref]); |
| 186 }.bind(this)); | 192 }.bind(this)); |
| 187 }, | 193 }, |
| 188 | 194 |
| 189 /** | 195 /** |
| 190 * Updates the prefs model with the given prefs. | 196 * Updates the prefs model with the given prefs. |
| 191 * @param {!Array<!chrome.settingsPrivate.PrefObject>} newPrefs | 197 * @param {!Array<!chrome.settingsPrivate.PrefObject>} newPrefs |
| 192 * @private | 198 * @private |
| 193 */ | 199 */ |
| 194 updatePrefs_: function(newPrefs) { | 200 updatePrefs_: function(newPrefs) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 * @return {PrefWrapper} An object containing a copy of the PrefObject's | 246 * @return {PrefWrapper} An object containing a copy of the PrefObject's |
| 241 * value. | 247 * value. |
| 242 * @private | 248 * @private |
| 243 */ | 249 */ |
| 244 createPrefWrapper_: function(prefObj) { | 250 createPrefWrapper_: function(prefObj) { |
| 245 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? | 251 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? |
| 246 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); | 252 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); |
| 247 }, | 253 }, |
| 248 }); | 254 }); |
| 249 })(); | 255 })(); |
| OLD | NEW |