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 created: 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)); |
118 }, | 124 }, |
119 | 125 |
120 /** | 126 /** |
121 * Polymer callback for changes to this.prefs. | 127 * Polymer callback for changes to this.prefs. |
122 * @param {!{path: string, value: *}} change | 128 * @param {!{path: string, value: *}} change |
123 * @private | 129 * @private |
124 */ | 130 */ |
125 prefsChanged_: function(change) { | 131 prefsChanged_: function(change) { |
126 if (!CrSettingsPrefs.isInitialized) | 132 if (!CrSettingsPrefs.isInitialized) |
127 return; | 133 return; |
128 | 134 |
129 var key = this.getPrefKeyFromPath_(change.path); | 135 var key = this.getPrefKeyFromPath_(change.path); |
130 var prefWrapper = this.prefWrappers_[key]; | 136 var prefWrapper = this.prefWrappers_[key]; |
131 if (!prefWrapper) | 137 if (!prefWrapper) |
132 return; | 138 return; |
133 | 139 |
134 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( | 140 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( |
135 this.get(key, this.prefs)); | 141 this.get(key, this.prefs)); |
136 | 142 |
137 // If settingsPrivate already has this value, do nothing. (Otherwise, | 143 // If settingsPrivate already has this value, do nothing. (Otherwise, |
138 // a change event from settingsPrivate could make us call | 144 // a change event from settingsPrivate could make us call |
139 // settingsPrivate.setPref and potentially trigger an IPC loop.) | 145 // settingsPrivate.setPref and potentially trigger an IPC loop.) |
140 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) | 146 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) |
141 return; | 147 return; |
142 | 148 |
143 chrome.settingsPrivate.setPref( | 149 this.settingsApi_.setPref( |
144 key, | 150 key, |
145 prefObj.value, | 151 prefObj.value, |
146 /* pageId */ '', | 152 /* pageId */ '', |
147 /* callback */ this.setPrefCallback_.bind(this, key)); | 153 /* callback */ this.setPrefCallback_.bind(this, key)); |
148 }, | 154 }, |
149 | 155 |
150 /** | 156 /** |
151 * Called when prefs in the underlying Chrome pref store are changed. | 157 * Called when prefs in the underlying Chrome pref store are changed. |
152 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 158 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
153 * The prefs that changed. | 159 * The prefs that changed. |
(...skipping 21 matching lines...) Expand all Loading... |
175 * @param {string} key The key used in the call to setPref. | 181 * @param {string} key The key used in the call to setPref. |
176 * @param {boolean} success True if setting the pref succeeded. | 182 * @param {boolean} success True if setting the pref succeeded. |
177 * @private | 183 * @private |
178 */ | 184 */ |
179 setPrefCallback_: function(key, success) { | 185 setPrefCallback_: function(key, success) { |
180 if (success) | 186 if (success) |
181 return; | 187 return; |
182 | 188 |
183 // Get the current pref value from chrome.settingsPrivate to ensure the | 189 // Get the current pref value from chrome.settingsPrivate to ensure the |
184 // UI stays up to date. | 190 // UI stays up to date. |
185 chrome.settingsPrivate.getPref(key, function(pref) { | 191 this.settingsApi_.getPref(key, function(pref) { |
186 this.updatePrefs_([pref]); | 192 this.updatePrefs_([pref]); |
187 }.bind(this)); | 193 }.bind(this)); |
188 }, | 194 }, |
189 | 195 |
190 /** | 196 /** |
191 * Updates the prefs model with the given prefs. | 197 * Updates the prefs model with the given prefs. |
192 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 198 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
193 * @private | 199 * @private |
194 */ | 200 */ |
195 updatePrefs_: function(prefs) { | 201 updatePrefs_: function(prefs) { |
(...skipping 26 matching lines...) Expand all Loading... |
222 for (let i = 1; i <= parts.length; i++) { | 228 for (let i = 1; i <= parts.length; i++) { |
223 let key = parts.slice(0, i).join('.'); | 229 let key = parts.slice(0, i).join('.'); |
224 // The prefWrappers_ keys match the pref keys. | 230 // The prefWrappers_ keys match the pref keys. |
225 if (this.prefWrappers_[key] != undefined) | 231 if (this.prefWrappers_[key] != undefined) |
226 return key; | 232 return key; |
227 } | 233 } |
228 return ''; | 234 return ''; |
229 }, | 235 }, |
230 | 236 |
231 /** | 237 /** |
232 * Sets or updates the pref denoted by newPrefObj.key in the publicy exposed | 238 * Sets or updates the pref denoted by newPrefObj.key in the publicly |
233 * |prefs| property. | 239 * exposed |prefs| property. |
234 * @param {chrome.settingsPrivate.PrefObject} newPrefObj The pref object to | 240 * @param {chrome.settingsPrivate.PrefObject} newPrefObj The pref object to |
235 * update the pref with. | 241 * update the pref with. |
236 * @private | 242 * @private |
237 */ | 243 */ |
238 setPref_: function(newPrefObj) { | 244 setPref_: function(newPrefObj) { |
239 // Check if the pref exists already in the Polymer |prefs| object. | 245 // Add the pref to |prefs|. cr.exportPath doesn't use Polymer.Base.set, |
240 if (this.get(newPrefObj.key, this.prefs)) { | 246 // which is OK because the nested property update events aren't useful. |
241 // Update just the value, notifying listeners of the change. | 247 cr.exportPath(newPrefObj.key, newPrefObj, this.prefs); |
242 this.set('prefs.' + newPrefObj.key + '.value', newPrefObj.value); | 248 // Notify listeners of the change at the preference key. |
243 } else { | 249 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 }, | 250 }, |
251 | 251 |
252 /** | 252 /** |
253 * Creates a PrefWrapper object from a chrome.settingsPrivate pref. | 253 * Creates a PrefWrapper object from a chrome.settingsPrivate pref. |
254 * @param {!chrome.settingsPrivate.PrefObject} prefObj | 254 * @param {!chrome.settingsPrivate.PrefObject} prefObj |
255 * PrefObject received from chrome.settingsPrivate. | 255 * PrefObject received from chrome.settingsPrivate. |
256 * @return {PrefWrapper} An object containing a copy of the PrefObject's | 256 * @return {PrefWrapper} An object containing a copy of the PrefObject's |
257 * value. | 257 * value. |
258 * @private | 258 * @private |
259 */ | 259 */ |
260 createPrefWrapper_: function(prefObj) { | 260 createPrefWrapper_: function(prefObj) { |
261 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? | 261 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? |
262 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); | 262 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); |
263 }, | 263 }, |
264 }); | 264 }); |
265 })(); | 265 })(); |
OLD | NEW |