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, | |
michaelpg
2015/09/05 07:11:59
is this type-able? {chrome.settingsPrivate} itself
| |
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); |
136 if (!key) | |
137 return; | |
138 | |
130 var prefWrapper = this.prefWrappers_[key]; | 139 var prefWrapper = this.prefWrappers_[key]; |
131 if (!prefWrapper) | 140 if (!prefWrapper) |
132 return; | 141 return; |
133 | 142 |
134 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( | 143 var prefObj = /** @type {chrome.settingsPrivate.PrefObject} */( |
135 this.get(key, this.prefs)); | 144 this.get(key, this.prefs)); |
136 | 145 |
137 // If settingsPrivate already has this value, do nothing. (Otherwise, | 146 // If settingsPrivate already has this value, do nothing. (Otherwise, |
138 // a change event from settingsPrivate could make us call | 147 // a change event from settingsPrivate could make us call |
139 // settingsPrivate.setPref and potentially trigger an IPC loop.) | 148 // settingsPrivate.setPref and potentially trigger an IPC loop.) |
140 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) | 149 if (prefWrapper.equals(this.createPrefWrapper_(prefObj))) |
141 return; | 150 return; |
142 | 151 |
143 chrome.settingsPrivate.setPref( | 152 this.settingsApi_.setPref( |
144 key, | 153 key, |
145 prefObj.value, | 154 prefObj.value, |
146 /* pageId */ '', | 155 /* pageId */ '', |
147 /* callback */ this.setPrefCallback_.bind(this, key)); | 156 /* callback */ this.setPrefCallback_.bind(this, key)); |
148 }, | 157 }, |
149 | 158 |
150 /** | 159 /** |
151 * Called when prefs in the underlying Chrome pref store are changed. | 160 * Called when prefs in the underlying Chrome pref store are changed. |
152 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 161 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
153 * The prefs that changed. | 162 * The prefs that changed. |
(...skipping 21 matching lines...) Expand all Loading... | |
175 * @param {string} key The key used in the call to setPref. | 184 * @param {string} key The key used in the call to setPref. |
176 * @param {boolean} success True if setting the pref succeeded. | 185 * @param {boolean} success True if setting the pref succeeded. |
177 * @private | 186 * @private |
178 */ | 187 */ |
179 setPrefCallback_: function(key, success) { | 188 setPrefCallback_: function(key, success) { |
180 if (success) | 189 if (success) |
181 return; | 190 return; |
182 | 191 |
183 // Get the current pref value from chrome.settingsPrivate to ensure the | 192 // Get the current pref value from chrome.settingsPrivate to ensure the |
184 // UI stays up to date. | 193 // UI stays up to date. |
185 chrome.settingsPrivate.getPref(key, function(pref) { | 194 this.settingsApi_.getPref(key, function(pref) { |
186 this.updatePrefs_([pref]); | 195 this.updatePrefs_([pref]); |
187 }.bind(this)); | 196 }.bind(this)); |
188 }, | 197 }, |
189 | 198 |
190 /** | 199 /** |
191 * Updates the prefs model with the given prefs. | 200 * Updates the prefs model with the given prefs. |
192 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs | 201 * @param {!Array<!chrome.settingsPrivate.PrefObject>} prefs |
193 * @private | 202 * @private |
194 */ | 203 */ |
195 updatePrefs_: function(prefs) { | 204 updatePrefs_: function(prefs) { |
(...skipping 26 matching lines...) Expand all Loading... | |
222 for (let i = 1; i <= parts.length; i++) { | 231 for (let i = 1; i <= parts.length; i++) { |
223 let key = parts.slice(0, i).join('.'); | 232 let key = parts.slice(0, i).join('.'); |
224 // The prefWrappers_ keys match the pref keys. | 233 // The prefWrappers_ keys match the pref keys. |
225 if (this.prefWrappers_[key] != undefined) | 234 if (this.prefWrappers_[key] != undefined) |
226 return key; | 235 return key; |
227 } | 236 } |
228 return ''; | 237 return ''; |
229 }, | 238 }, |
230 | 239 |
231 /** | 240 /** |
232 * Sets or updates the pref denoted by newPrefObj.key in the publicy exposed | 241 * Sets or updates the pref denoted by newPrefObj.key in the publicly |
233 * |prefs| property. | 242 * exposed |prefs| property. |
234 * @param {chrome.settingsPrivate.PrefObject} newPrefObj The pref object to | 243 * @param {chrome.settingsPrivate.PrefObject} newPrefObj The pref object to |
235 * update the pref with. | 244 * update the pref with. |
236 * @private | 245 * @private |
237 */ | 246 */ |
238 setPref_: function(newPrefObj) { | 247 setPref_: function(newPrefObj) { |
239 // Check if the pref exists already in the Polymer |prefs| object. | 248 // Check if the pref exists already in the Polymer |prefs| object. |
240 if (this.get(newPrefObj.key, this.prefs)) { | 249 if (this.get(newPrefObj.key, this.prefs)) { |
241 // Update just the value, notifying listeners of the change. | 250 // Update just the value, notifying listeners of the change. |
242 this.set('prefs.' + newPrefObj.key + '.value', newPrefObj.value); | 251 this.set('prefs.' + newPrefObj.key + '.value', newPrefObj.value); |
243 } else { | 252 } else { |
(...skipping 12 matching lines...) Expand all Loading... | |
256 * @return {PrefWrapper} An object containing a copy of the PrefObject's | 265 * @return {PrefWrapper} An object containing a copy of the PrefObject's |
257 * value. | 266 * value. |
258 * @private | 267 * @private |
259 */ | 268 */ |
260 createPrefWrapper_: function(prefObj) { | 269 createPrefWrapper_: function(prefObj) { |
261 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? | 270 return prefObj.type == chrome.settingsPrivate.PrefType.LIST ? |
262 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); | 271 new ListPrefWrapper(prefObj) : new PrefWrapper(prefObj); |
263 }, | 272 }, |
264 }); | 273 }); |
265 })(); | 274 })(); |
OLD | NEW |