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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 prefWrappers_: { | 100 prefWrappers_: { |
101 type: Object, | 101 type: Object, |
102 value: function() { return {}; }, | 102 value: function() { return {}; }, |
103 }, | 103 }, |
104 }, | 104 }, |
105 | 105 |
106 observers: [ | 106 observers: [ |
107 'prefsChanged_(prefs.*)', | 107 'prefsChanged_(prefs.*)', |
108 ], | 108 ], |
109 | 109 |
110 settingsApi_: chrome.settingsPrivate, | |
111 | |
110 /** @override */ | 112 /** @override */ |
111 created: function() { | 113 ready: function() { |
114 // Set window.mockApi to pass a custom settings API, i.e. for tests. | |
115 // TODO(michaelpg): don't use a global. | |
116 if (window.mockApi) | |
117 this.settingsApi_ = window.mockApi; | |
112 CrSettingsPrefs.isInitialized = false; | 118 CrSettingsPrefs.isInitialized = false; |
113 | 119 |
114 chrome.settingsPrivate.onPrefsChanged.addListener( | 120 this.settingsApi_.onPrefsChanged.addListener( |
115 this.onSettingsPrivatePrefsChanged_.bind(this)); | 121 this.onSettingsPrivatePrefsChanged_.bind(this)); |
116 chrome.settingsPrivate.getAllPrefs( | 122 this.settingsApi_.getAllPrefs( |
117 this.onSettingsPrivatePrefsFetched_.bind(this)); | 123 this.onSettingsPrivatePrefsFetched_.bind(this)); |
124 | |
125 this.ready_ = true; | |
118 }, | 126 }, |
119 | 127 |
120 /** | 128 /** |
121 * Polymer callback for changes to this.prefs. | 129 * Polymer callback for changes to this.prefs. |
122 * @param {!{path: string, value: *}} change | 130 * @param {!{path: string, value: *}} change |
123 * @private | 131 * @private |
124 */ | 132 */ |
125 prefsChanged_: function(change) { | 133 prefsChanged_: function(change) { |
134 if (!this.ready_) | |
135 return; | |
Dan Beam
2015/09/16 05:53:56
can't you just use:
if (!this.$)
return;
?
michaelpg
2015/09/16 18:27:17
removed w/ rebase on https://codereview.chromium.o
| |
136 | |
126 if (!CrSettingsPrefs.isInitialized) | 137 if (!CrSettingsPrefs.isInitialized) |
127 return; | 138 return; |
128 | 139 |
129 var key = this.getPrefKeyFromPath_(change.path); | 140 var key = this.getPrefKeyFromPath_(change.path); |
130 var prefWrapper = this.prefWrappers_[key]; | 141 var prefWrapper = this.prefWrappers_[key]; |
131 if (!prefWrapper) | 142 if (!prefWrapper) |
132 return; | 143 return; |
133 | 144 |
134 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( | 145 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( |
135 this.get(key, this.prefs)); | 146 this.get(key, this.prefs)); |
136 | 147 |
137 // If settingsPrivate already has this value, do nothing. (Otherwise, | 148 // If settingsPrivate already has this value, do nothing. (Otherwise, |
138 // a change event from settingsPrivate could make us call | 149 // a change event from settingsPrivate could make us call |
139 // settingsPrivate.setPref and potentially trigger an IPC loop.) | 150 // settingsPrivate.setPref and potentially trigger an IPC loop.) |
140 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) | 151 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) |
141 return; | 152 return; |
142 | 153 |
143 chrome.settingsPrivate.setPref( | 154 this.settingsApi_.setPref( |
144 key, | 155 key, |
145 prefObj.value, | 156 prefObj.value, |
146 /* pageId */ '', | 157 /* pageId */ '', |
147 /* callback */ this.setPrefCallback_.bind(this, key)); | 158 /* callback */ this.setPrefCallback_.bind(this, key)); |
148 }, | 159 }, |
149 | 160 |
150 /** | 161 /** |
151 * Called when prefs in the underlying Chrome pref store are changed. | 162 * Called when prefs in the underlying Chrome pref store are changed. |
152 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 163 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
153 * The prefs that changed. | 164 * The prefs that changed. |
(...skipping 21 matching lines...) Expand all Loading... | |
175 * @param {string} key The key used in the call to setPref. | 186 * @param {string} key The key used in the call to setPref. |
176 * @param {boolean} success True if setting the pref succeeded. | 187 * @param {boolean} success True if setting the pref succeeded. |
177 * @private | 188 * @private |
178 */ | 189 */ |
179 setPrefCallback_: function(key, success) { | 190 setPrefCallback_: function(key, success) { |
180 if (success) | 191 if (success) |
181 return; | 192 return; |
182 | 193 |
183 // Get the current pref value from chrome.settingsPrivate to ensure the | 194 // Get the current pref value from chrome.settingsPrivate to ensure the |
184 // UI stays up to date. | 195 // UI stays up to date. |
185 chrome.settingsPrivate.getPref(key, function(pref) { | 196 this.settingsApi_.getPref(key, function(pref) { |
186 this.updatePrefs_([pref]); | 197 this.updatePrefs_([pref]); |
187 }.bind(this)); | 198 }.bind(this)); |
188 }, | 199 }, |
189 | 200 |
190 /** | 201 /** |
191 * Updates the prefs model with the given prefs. | 202 * Updates the prefs model with the given prefs. |
192 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 203 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
193 * @private | 204 * @private |
194 */ | 205 */ |
195 updatePrefs_: function(prefs) { | 206 updatePrefs_: function(prefs) { |
(...skipping 26 matching lines...) Expand all Loading... | |
222 for (let i = 1; i <= parts.length; i++) { | 233 for (let i = 1; i <= parts.length; i++) { |
223 let key = parts.slice(0, i).join('.'); | 234 let key = parts.slice(0, i).join('.'); |
224 // The prefWrappers_ keys match the pref keys. | 235 // The prefWrappers_ keys match the pref keys. |
225 if (this.prefWrappers_[key] != undefined) | 236 if (this.prefWrappers_[key] != undefined) |
226 return key; | 237 return key; |
227 } | 238 } |
228 return ''; | 239 return ''; |
229 }, | 240 }, |
230 | 241 |
231 /** | 242 /** |
232 * Sets or updates the pref denoted by newPrefObj.key in the publicy exposed | 243 * Sets or updates the pref denoted by newPrefObj.key in the publicly |
233 * |prefs| property. | 244 * exposed |prefs| property. |
234 * @param {chrome.settingsPrivate.PrefObject} newPrefObj The pref object to | 245 * @param {chrome.settingsPrivate.PrefObject} newPrefObj The pref object to |
235 * update the pref with. | 246 * update the pref with. |
236 * @private | 247 * @private |
237 */ | 248 */ |
238 setPref_: function(newPrefObj) { | 249 setPref_: function(newPrefObj) { |
239 // Check if the pref exists already in the Polymer |prefs| object. | 250 // Add the pref to |prefs|. cr.exportPath doesn't use Polymer.Base.set, |
240 if (this.get(newPrefObj.key, this.prefs)) { | 251 // which is OK because the nested property update events aren't useful. |
241 // Update just the value, notifying listeners of the change. | 252 cr.exportPath(newPrefObj.key, newPrefObj, this.prefs); |
242 this.set('prefs.' + newPrefObj.key + '.value', newPrefObj.value); | 253 // Notify listeners of the change at the preference key. |
243 } else { | 254 this.notifyPath('prefs.' + newPrefObj.key, newPrefObj); |
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 }, | 255 }, |
251 | 256 |
252 /** | 257 /** |
253 * Creates a PrefWrapper object from a chrome.settingsPrivate pref. | 258 * Creates a PrefWrapper object from a chrome.settingsPrivate pref. |
254 * @param {!chrome.settingsPrivate.PrefObject} prefObj | 259 * @param {!chrome.settingsPrivate.PrefObject} prefObj |
255 * PrefObject received from chrome.settingsPrivate. | 260 * PrefObject received from chrome.settingsPrivate. |
256 * @return {PrefWrapper} An object containing a copy of the PrefObject's | 261 * @return {PrefWrapper} An object containing a copy of the PrefObject's |
257 * value. | 262 * value. |
258 * @private | 263 * @private |
259 */ | 264 */ |
260 createPrefWrapper_: function(prefObj) { | 265 createPrefWrapper_: function(prefObj) { |
261 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? | 266 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? |
262 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); | 267 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); |
263 }, | 268 }, |
264 }); | 269 }); |
265 })(); | 270 })(); |
OLD | NEW |