Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 cr.define('options', function() { | 5 cr.define('options', function() { |
| 6 | 6 |
| 7 ///////////////////////////////////////////////////////////////////////////// | 7 ///////////////////////////////////////////////////////////////////////////// |
| 8 // Preferences class: | 8 // Preferences class: |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * Preferences class manages access to Chrome profile preferences. | 11 * Preferences class manages access to Chrome profile preferences. |
| 12 * @constructor | 12 * @constructor |
| 13 */ | 13 */ |
| 14 function Preferences() { | 14 function Preferences() { |
| 15 } | 15 } |
| 16 | 16 |
| 17 cr.addSingletonGetter(Preferences); | 17 cr.addSingletonGetter(Preferences); |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Sets value of a boolean preference. | 20 * Sets a Boolean preference and signals its new value. |
| 21 * and signals its changed value. | |
| 22 * @param {string} name Preference name. | 21 * @param {string} name Preference name. |
| 23 * @param {boolean} value New preference value. | 22 * @param {boolean} value New preference value. |
| 23 * @param {boolean} commit Whether to commit the change to Chrome. | |
| 24 * @param {string} metric User metrics identifier. | 24 * @param {string} metric User metrics identifier. |
| 25 */ | 25 */ |
| 26 Preferences.setBooleanPref = function(name, value, metric) { | 26 Preferences.setBooleanPref = function(name, value, commit, metric) { |
| 27 if (!commit) { | |
| 28 Preferences.getInstance().setPrefNoCommit_(name, 'bool', Boolean(value)); | |
| 29 return; | |
| 30 } | |
| 31 | |
| 27 var argumentList = [name, Boolean(value)]; | 32 var argumentList = [name, Boolean(value)]; |
| 28 if (metric != undefined) argumentList.push(metric); | 33 if (metric != undefined) argumentList.push(metric); |
| 29 chrome.send('setBooleanPref', argumentList); | 34 chrome.send('setBooleanPref', argumentList); |
| 30 }; | 35 }; |
| 31 | 36 |
| 32 /** | 37 /** |
| 33 * Sets value of an integer preference. | 38 * Sets an integer preference and signals its new value. |
| 34 * and signals its changed value. | |
| 35 * @param {string} name Preference name. | 39 * @param {string} name Preference name. |
| 36 * @param {number} value New preference value. | 40 * @param {number} value New preference value. |
| 41 * @param {boolean} commit Whether to commit the change to Chrome. | |
| 37 * @param {string} metric User metrics identifier. | 42 * @param {string} metric User metrics identifier. |
| 38 */ | 43 */ |
| 39 Preferences.setIntegerPref = function(name, value, metric) { | 44 Preferences.setIntegerPref = function(name, value, commit, metric) { |
| 45 if (!commit) { | |
| 46 Preferences.getInstance().setPrefNoCommit_(name, 'int', Number(value)); | |
| 47 return; | |
| 48 } | |
| 49 | |
| 40 var argumentList = [name, Number(value)]; | 50 var argumentList = [name, Number(value)]; |
| 41 if (metric != undefined) argumentList.push(metric); | 51 if (metric != undefined) argumentList.push(metric); |
| 42 chrome.send('setIntegerPref', argumentList); | 52 chrome.send('setIntegerPref', argumentList); |
| 43 }; | 53 }; |
| 44 | 54 |
| 45 /** | 55 /** |
| 46 * Sets value of a double-valued preference. | 56 * Sets a double-valued preference and signals its new value. |
| 47 * and signals its changed value. | |
| 48 * @param {string} name Preference name. | 57 * @param {string} name Preference name. |
| 49 * @param {number} value New preference value. | 58 * @param {number} value New preference value. |
| 59 * @param {boolean} commit Whether to commit the change to Chrome. | |
| 50 * @param {string} metric User metrics identifier. | 60 * @param {string} metric User metrics identifier. |
| 51 */ | 61 */ |
| 52 Preferences.setDoublePref = function(name, value, metric) { | 62 Preferences.setDoublePref = function(name, value, commit, metric) { |
| 63 if (!commit) { | |
| 64 Preferences.getInstance().setPrefNoCommit_(name, 'double', Number(value)); | |
| 65 return; | |
| 66 } | |
| 67 | |
| 53 var argumentList = [name, Number(value)]; | 68 var argumentList = [name, Number(value)]; |
| 54 if (metric != undefined) argumentList.push(metric); | 69 if (metric != undefined) argumentList.push(metric); |
| 55 chrome.send('setDoublePref', argumentList); | 70 chrome.send('setDoublePref', argumentList); |
| 56 }; | 71 }; |
| 57 | 72 |
| 58 /** | 73 /** |
| 59 * Sets value of a string preference. | 74 * Sets a string preference and signals its new value. |
| 60 * and signals its changed value. | |
| 61 * @param {string} name Preference name. | 75 * @param {string} name Preference name. |
| 62 * @param {string} value New preference value. | 76 * @param {string} value New preference value. |
| 77 * @param {boolean} commit Whether to commit the change to Chrome. | |
| 63 * @param {string} metric User metrics identifier. | 78 * @param {string} metric User metrics identifier. |
| 64 */ | 79 */ |
| 65 Preferences.setStringPref = function(name, value, metric) { | 80 Preferences.setStringPref = function(name, value, commit, metric) { |
| 81 if (!commit) { | |
| 82 Preferences.getInstance().setPrefNoCommit_(name, 'string', String(value)); | |
| 83 return; | |
| 84 } | |
| 85 | |
| 66 var argumentList = [name, String(value)]; | 86 var argumentList = [name, String(value)]; |
| 67 if (metric != undefined) argumentList.push(metric); | 87 if (metric != undefined) argumentList.push(metric); |
| 68 chrome.send('setStringPref', argumentList); | 88 chrome.send('setStringPref', argumentList); |
| 69 }; | 89 }; |
| 70 | 90 |
| 71 /** | 91 /** |
| 72 * Sets value of a string preference that represents a URL | 92 * Sets a string preference that represents a URL and signals its new value. |
| 73 * and signals its changed value. The value will be fixed to be a valid URL. | 93 * The value will be fixed to be a valid URL when it gets committed to Chrome. |
| 74 * @param {string} name Preference name. | 94 * @param {string} name Preference name. |
| 75 * @param {string} value New preference value. | 95 * @param {string} value New preference value. |
| 96 * @param {boolean} commit Whether to commit the change to Chrome. | |
| 76 * @param {string} metric User metrics identifier. | 97 * @param {string} metric User metrics identifier. |
| 77 */ | 98 */ |
| 78 Preferences.setURLPref = function(name, value, metric) { | 99 Preferences.setURLPref = function(name, value, commit, metric) { |
| 100 if (!commit) { | |
| 101 Preferences.getInstance().setPrefNoCommit_(name, 'url', String(value)); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 79 var argumentList = [name, String(value)]; | 105 var argumentList = [name, String(value)]; |
| 80 if (metric != undefined) argumentList.push(metric); | 106 if (metric != undefined) argumentList.push(metric); |
| 81 chrome.send('setURLPref', argumentList); | 107 chrome.send('setURLPref', argumentList); |
| 82 }; | 108 }; |
| 83 | 109 |
| 84 /** | 110 /** |
| 85 * Sets value of a JSON list preference. | 111 * Sets a JSON list preference and signals its new value. |
| 86 * and signals its changed value. | |
| 87 * @param {string} name Preference name. | 112 * @param {string} name Preference name. |
| 88 * @param {Array} value New preference value. | 113 * @param {Array} value New preference value. |
| 114 * @param {boolean} commit Whether to commit the change to Chrome. | |
| 89 * @param {string} metric User metrics identifier. | 115 * @param {string} metric User metrics identifier. |
| 90 */ | 116 */ |
| 91 Preferences.setListPref = function(name, value, metric) { | 117 Preferences.setListPref = function(name, value, commit, metric) { |
| 118 if (!commit) { | |
| 119 Preferences.getInstance().setPrefNoCommit_(name, 'list', value); | |
| 120 return; | |
| 121 } | |
| 122 | |
| 92 var argumentList = [name, JSON.stringify(value)]; | 123 var argumentList = [name, JSON.stringify(value)]; |
| 93 if (metric != undefined) argumentList.push(metric); | 124 if (metric != undefined) argumentList.push(metric); |
| 94 chrome.send('setListPref', argumentList); | 125 chrome.send('setListPref', argumentList); |
| 95 }; | 126 }; |
| 96 | 127 |
| 97 /** | 128 /** |
| 98 * Clears value of a JSON preference. | 129 * Clears the user setting for a preference and signals its new effective |
| 130 * value. | |
| 99 * @param {string} name Preference name. | 131 * @param {string} name Preference name. |
| 132 * @param {boolean} commit Whether to commit the change to Chrome. | |
| 100 * @param {string} metric User metrics identifier. | 133 * @param {string} metric User metrics identifier. |
| 101 */ | 134 */ |
| 102 Preferences.clearPref = function(name, metric) { | 135 Preferences.clearPref = function(name, commit, metric) { |
| 136 if (!commit) { | |
| 137 Preferences.getInstance().clearPrefNoCommit_(name); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 103 var argumentList = [name]; | 141 var argumentList = [name]; |
| 104 if (metric != undefined) argumentList.push(metric); | 142 if (metric != undefined) argumentList.push(metric); |
| 105 chrome.send('clearPref', argumentList); | 143 chrome.send('clearPref', argumentList); |
| 106 }; | 144 }; |
| 107 | 145 |
| 108 Preferences.prototype = { | 146 Preferences.prototype = { |
| 109 __proto__: cr.EventTarget.prototype, | 147 __proto__: cr.EventTarget.prototype, |
| 110 | 148 |
| 111 // Map of registered preferences. | 149 // Map of registered preferences. |
| 112 registeredPreferences_: {}, | 150 registeredPreferences_: {}, |
| 113 | 151 |
| 114 /** | 152 /** |
| 115 * Adds an event listener to the target. | 153 * Adds an event listener to the target. |
| 116 * @param {string} type The name of the event. | 154 * @param {string} type The name of the event. |
| 117 * @param {!Function|{handleEvent:Function}} handler The handler for the | 155 * @param {!Function|{handleEvent:Function}} handler The handler for the |
| 118 * event. This is called when the event is dispatched. | 156 * event. This is called when the event is dispatched. |
| 119 */ | 157 */ |
| 120 addEventListener: function(type, handler) { | 158 addEventListener: function(type, handler) { |
| 121 cr.EventTarget.prototype.addEventListener.call(this, type, handler); | 159 cr.EventTarget.prototype.addEventListener.call(this, type, handler); |
| 122 this.registeredPreferences_[type] = true; | 160 if (!(type in this.registeredPreferences_)) |
| 161 this.registeredPreferences_[type] = {}; | |
| 123 }, | 162 }, |
| 124 | 163 |
| 125 /** | 164 /** |
| 126 * Initializes preference reading and change notifications. | 165 * Initializes preference reading and change notifications. |
| 127 */ | 166 */ |
| 128 initialize: function() { | 167 initialize: function() { |
| 129 var params1 = ['Preferences.prefsFetchedCallback']; | 168 var params1 = ['Preferences.prefsFetchedCallback']; |
| 130 var params2 = ['Preferences.prefsChangedCallback']; | 169 var params2 = ['Preferences.prefsChangedCallback']; |
| 131 for (var prefName in this.registeredPreferences_) { | 170 for (var prefName in this.registeredPreferences_) { |
| 132 params1.push(prefName); | 171 params1.push(prefName); |
| 133 params2.push(prefName); | 172 params2.push(prefName); |
| 134 } | 173 } |
| 135 chrome.send('fetchPrefs', params1); | 174 chrome.send('fetchPrefs', params1); |
| 136 chrome.send('observePrefs', params2); | 175 chrome.send('observePrefs', params2); |
| 137 }, | 176 }, |
| 138 | 177 |
| 139 /** | 178 /** |
| 140 * Helper function for flattening of dictionary passed via fetchPrefs | 179 * Helper function for flattening of dictionary passed via fetchPrefs |
| 141 * callback. | 180 * callback. |
| 142 * @param {string} prefix Preference name prefix. | 181 * @param {string} prefix Preference name prefix. |
| 143 * @param {object} dict Map with preference values. | 182 * @param {object} dict Map with preference values. |
| 183 * @private | |
| 144 */ | 184 */ |
| 145 flattenMapAndDispatchEvent_: function(prefix, dict) { | 185 flattenMapAndDispatchEvent_: function(prefix, dict) { |
| 146 for (var prefName in dict) { | 186 for (var prefName in dict) { |
| 147 if (typeof dict[prefName] == 'object' && | 187 if (typeof dict[prefName] == 'object' && |
| 148 !this.registeredPreferences_[prefix + prefName]) { | 188 !this.registeredPreferences_[prefix + prefName]) { |
| 149 this.flattenMapAndDispatchEvent_(prefix + prefName + '.', | 189 this.flattenMapAndDispatchEvent_(prefix + prefName + '.', |
| 150 dict[prefName]); | 190 dict[prefName]); |
| 151 } else { | 191 } else { |
| 152 var event = new cr.Event(prefix + prefName); | 192 var event = new cr.Event(prefix + prefName); |
| 193 this.registeredPreferences_[prefix + prefName].orig = dict[prefName]; | |
| 153 event.value = dict[prefName]; | 194 event.value = dict[prefName]; |
| 154 this.dispatchEvent(event); | 195 this.dispatchEvent(event); |
| 155 } | 196 } |
| 156 } | 197 } |
| 198 }, | |
| 199 | |
| 200 /** | |
| 201 * Sets a preference and signals its new value. The change is propagated | |
|
James Hawkins
2012/08/02 20:33:32
Hmm, need to explain how this is used wrt set*Pref
bartfab (slow)
2012/08/31 14:14:30
Done.
| |
| 202 * throughout the UI code but is not committed to Chrome yet. | |
| 203 * @param {string} name Preference name. | |
| 204 * @param {string} type Preference data type. | |
| 205 * @param {*} value New preference value. | |
| 206 * @private | |
| 207 */ | |
| 208 setPrefNoCommit_: function(name, type, value) { | |
| 209 var pref = this.registeredPreferences_[name]; | |
| 210 pref.action = 'set'; | |
| 211 pref.type = type; | |
| 212 pref.value = value; | |
| 213 | |
| 214 var event = new cr.Event(name); | |
| 215 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does. | |
| 216 event.value = { | |
| 217 value: value, | |
| 218 recommendedValue: pref.orig.recommendedValue, | |
| 219 disabled: pref.orig.disabled, | |
| 220 }; | |
| 221 this.dispatchEvent(event); | |
| 222 }, | |
| 223 | |
| 224 /** | |
| 225 * Clears a preference and signals its new value. The change is propagated | |
| 226 * throughout the UI code but is not committed to Chrome yet. | |
| 227 * @param {string} name Preference name. | |
| 228 * @private | |
| 229 */ | |
| 230 clearPrefNoCommit_: function(name) { | |
| 231 var pref = this.registeredPreferences_[name]; | |
| 232 pref.action = 'clear'; | |
| 233 delete pref.type; | |
| 234 delete pref.value; | |
| 235 | |
| 236 var event = new cr.Event(name); | |
| 237 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does. | |
| 238 event.value = { | |
| 239 value: pref.orig.recommendedValue, | |
| 240 controlledBy: 'recommended', | |
| 241 recommendedValue: pref.orig.recommendedValue, | |
| 242 disabled: pref.orig.disabled, | |
| 243 }; | |
| 244 this.dispatchEvent(event); | |
| 245 }, | |
| 246 | |
| 247 /** | |
| 248 * Commits a preference change to Chrome and signals the new preference | |
| 249 * value. Does nothing if there is no uncommitted change. | |
| 250 * @param {string} name Preference name. | |
| 251 * @param {string} metric User metrics identifier. | |
| 252 */ | |
| 253 commitPref: function(name, metric) { | |
| 254 var pref = this.registeredPreferences_[name]; | |
| 255 switch (pref.action) { | |
| 256 case 'set': | |
| 257 switch (pref.type) { | |
| 258 case 'bool': | |
| 259 Preferences.setBooleanPref(name, pref.value, true, metric); | |
| 260 break; | |
| 261 case 'int': | |
| 262 Preferences.setIntegerPref(name, pref.value, true, metric); | |
| 263 break; | |
| 264 case 'double': | |
| 265 Preferences.setDoublePref(name, pref.value, true, metric); | |
| 266 break; | |
| 267 case 'string': | |
| 268 Preferences.setStringPref(name, pref.value, true, metric); | |
| 269 break; | |
| 270 case 'url': | |
| 271 Preferences.setURLPref(name, pref.value, true, metric); | |
| 272 break; | |
| 273 case 'list': | |
| 274 Preferences.setListPref(name, pref.value, true, metric); | |
| 275 break; | |
| 276 } | |
| 277 break; | |
| 278 case 'clear': | |
| 279 Preferences.clearPref(name, true, metric); | |
| 280 break; | |
| 281 } | |
| 282 delete pref.action; | |
| 283 delete pref.type; | |
| 284 delete pref.value; | |
| 285 }, | |
| 286 | |
| 287 /** | |
| 288 * Rolls back a preference change and signals the original preference value. | |
| 289 * Does nothing if there is no uncommitted change. | |
| 290 * @param {string} name Preference name. | |
| 291 */ | |
| 292 rollbackPref: function(name) { | |
| 293 var pref = this.registeredPreferences_[name]; | |
| 294 if (!pref.action) | |
| 295 return; | |
| 296 | |
| 297 delete pref.action; | |
| 298 delete pref.type; | |
| 299 delete pref.value; | |
| 300 | |
| 301 var event = new cr.Event(name); | |
| 302 event.value = pref.orig; | |
| 303 this.dispatchEvent(event); | |
| 157 } | 304 } |
| 158 }; | 305 }; |
| 159 | 306 |
| 160 /** | 307 /** |
| 161 * Callback for fetchPrefs method. | 308 * Callback for fetchPrefs method. |
| 162 * @param {object} dict Map of fetched property values. | 309 * @param {object} dict Map of fetched property values. |
| 163 */ | 310 */ |
| 164 Preferences.prefsFetchedCallback = function(dict) { | 311 Preferences.prefsFetchedCallback = function(dict) { |
| 165 Preferences.getInstance().flattenMapAndDispatchEvent_('', dict); | 312 Preferences.getInstance().flattenMapAndDispatchEvent_('', dict); |
| 166 }; | 313 }; |
| 167 | 314 |
| 168 /** | 315 /** |
| 169 * Callback for observePrefs method. | 316 * Callback for observePrefs method. |
| 170 * @param {array} notification An array defining changed preference values. | 317 * @param {array} notification An array defining changed preference values. |
| 171 * notification[0] contains name of the change preference while its new value | 318 * notification[0] contains name of the change preference while its new value |
| 172 * is stored in notification[1]. | 319 * is stored in notification[1]. |
| 173 */ | 320 */ |
| 174 Preferences.prefsChangedCallback = function(notification) { | 321 Preferences.prefsChangedCallback = function(notification) { |
| 175 var event = new cr.Event(notification[0]); | 322 var event = new cr.Event(notification[0]); |
| 176 event.value = notification[1]; | 323 event.value = notification[1]; |
| 177 Preferences.getInstance().dispatchEvent(event); | 324 prefs = Preferences.getInstance(); |
| 325 prefs.registeredPreferences_[notification[0]] = {orig: notification[1]}; | |
| 326 prefs.dispatchEvent(event); | |
| 178 }; | 327 }; |
| 179 | 328 |
| 180 // Export | 329 // Export |
| 181 return { | 330 return { |
| 182 Preferences: Preferences | 331 Preferences: Preferences |
| 183 }; | 332 }; |
| 184 | 333 |
| 185 }); | 334 }); |
| OLD | NEW |