| OLD | NEW |
| 1 // Copyright (c) 2012 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 * @extends {cr.EventTarget} |
| 13 */ | 14 */ |
| 14 function Preferences() { | 15 function Preferences() { |
| 15 // Map of registered preferences. | 16 // Map of registered preferences. |
| 16 this.registeredPreferences_ = {}; | 17 this.registeredPreferences_ = {}; |
| 17 } | 18 } |
| 18 | 19 |
| 19 cr.addSingletonGetter(Preferences); | 20 cr.addSingletonGetter(Preferences); |
| 20 | 21 |
| 21 /** | 22 /** |
| 22 * Sets a Boolean preference and signals its new value. | 23 * Sets a Boolean preference and signals its new value. |
| 23 * @param {string} name Preference name. | 24 * @param {string} name Preference name. |
| 24 * @param {boolean} value New preference value. | 25 * @param {boolean} value New preference value. |
| 25 * @param {boolean} commit Whether to commit the change to Chrome. | 26 * @param {boolean} commit Whether to commit the change to Chrome. |
| 26 * @param {string} metric User metrics identifier. | 27 * @param {string=} opt_metric User metrics identifier. |
| 27 */ | 28 */ |
| 28 Preferences.setBooleanPref = function(name, value, commit, metric) { | 29 Preferences.setBooleanPref = function(name, value, commit, opt_metric) { |
| 29 if (!commit) { | 30 if (!commit) { |
| 30 Preferences.getInstance().setPrefNoCommit_(name, 'bool', Boolean(value)); | 31 Preferences.getInstance().setPrefNoCommit_(name, 'bool', Boolean(value)); |
| 31 return; | 32 return; |
| 32 } | 33 } |
| 33 | 34 |
| 34 var argumentList = [name, Boolean(value)]; | 35 var argumentList = [name, Boolean(value)]; |
| 35 if (metric != undefined) argumentList.push(metric); | 36 if (opt_metric != undefined) argumentList.push(opt_metric); |
| 36 chrome.send('setBooleanPref', argumentList); | 37 chrome.send('setBooleanPref', argumentList); |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 /** | 40 /** |
| 40 * Sets an integer preference and signals its new value. | 41 * Sets an integer preference and signals its new value. |
| 41 * @param {string} name Preference name. | 42 * @param {string} name Preference name. |
| 42 * @param {number} value New preference value. | 43 * @param {number} value New preference value. |
| 43 * @param {boolean} commit Whether to commit the change to Chrome. | 44 * @param {boolean} commit Whether to commit the change to Chrome. |
| 44 * @param {string} metric User metrics identifier. | 45 * @param {string} metric User metrics identifier. |
| 45 */ | 46 */ |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 var argumentList = [name, JSON.stringify(value)]; | 126 var argumentList = [name, JSON.stringify(value)]; |
| 126 if (metric != undefined) argumentList.push(metric); | 127 if (metric != undefined) argumentList.push(metric); |
| 127 chrome.send('setListPref', argumentList); | 128 chrome.send('setListPref', argumentList); |
| 128 }; | 129 }; |
| 129 | 130 |
| 130 /** | 131 /** |
| 131 * Clears the user setting for a preference and signals its new effective | 132 * Clears the user setting for a preference and signals its new effective |
| 132 * value. | 133 * value. |
| 133 * @param {string} name Preference name. | 134 * @param {string} name Preference name. |
| 134 * @param {boolean} commit Whether to commit the change to Chrome. | 135 * @param {boolean} commit Whether to commit the change to Chrome. |
| 135 * @param {string} metric User metrics identifier. | 136 * @param {string=} opt_metric User metrics identifier. |
| 136 */ | 137 */ |
| 137 Preferences.clearPref = function(name, commit, metric) { | 138 Preferences.clearPref = function(name, commit, opt_metric) { |
| 138 if (!commit) { | 139 if (!commit) { |
| 139 Preferences.getInstance().clearPrefNoCommit_(name); | 140 Preferences.getInstance().clearPrefNoCommit_(name); |
| 140 return; | 141 return; |
| 141 } | 142 } |
| 142 | 143 |
| 143 var argumentList = [name]; | 144 var argumentList = [name]; |
| 144 if (metric != undefined) argumentList.push(metric); | 145 if (opt_metric != undefined) argumentList.push(opt_metric); |
| 145 chrome.send('clearPref', argumentList); | 146 chrome.send('clearPref', argumentList); |
| 146 }; | 147 }; |
| 147 | 148 |
| 148 Preferences.prototype = { | 149 Preferences.prototype = { |
| 149 __proto__: cr.EventTarget.prototype, | 150 __proto__: cr.EventTarget.prototype, |
| 150 | 151 |
| 151 /** | 152 /** |
| 152 * Adds an event listener to the target. | 153 * Adds an event listener to the target. |
| 153 * @param {string} type The name of the event. | 154 * @param {string} type The name of the event. |
| 154 * @param {!Function|{handleEvent:Function}} handler The handler for the | 155 * @param {!Function|{handleEvent:Function}} handler The handler for the |
| (...skipping 16 matching lines...) Expand all Loading... |
| 171 params2.push(prefName); | 172 params2.push(prefName); |
| 172 } | 173 } |
| 173 chrome.send('fetchPrefs', params1); | 174 chrome.send('fetchPrefs', params1); |
| 174 chrome.send('observePrefs', params2); | 175 chrome.send('observePrefs', params2); |
| 175 }, | 176 }, |
| 176 | 177 |
| 177 /** | 178 /** |
| 178 * Helper function for flattening of dictionary passed via fetchPrefs | 179 * Helper function for flattening of dictionary passed via fetchPrefs |
| 179 * callback. | 180 * callback. |
| 180 * @param {string} prefix Preference name prefix. | 181 * @param {string} prefix Preference name prefix. |
| 181 * @param {object} dict Map with preference values. | 182 * @param {Object} dict Map with preference values. |
| 182 * @private | 183 * @private |
| 183 */ | 184 */ |
| 184 flattenMapAndDispatchEvent_: function(prefix, dict) { | 185 flattenMapAndDispatchEvent_: function(prefix, dict) { |
| 185 for (var prefName in dict) { | 186 for (var prefName in dict) { |
| 186 var value = dict[prefName]; | 187 var value = dict[prefName]; |
| 187 if (typeof value == 'object' && | 188 if (typeof value == 'object' && |
| 188 !this.registeredPreferences_[prefix + prefName]) { | 189 !this.registeredPreferences_[prefix + prefName]) { |
| 189 this.flattenMapAndDispatchEvent_(prefix + prefName + '.', value); | 190 this.flattenMapAndDispatchEvent_(prefix + prefName + '.', value); |
| 190 } else if (value) { | 191 } else if (value) { |
| 191 var event = new Event(prefix + prefName); | 192 var event = new Event(prefix + prefName); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 | 302 |
| 302 var event = new Event(name); | 303 var event = new Event(name); |
| 303 event.value = pref.orig || {}; | 304 event.value = pref.orig || {}; |
| 304 event.value.uncommitted = true; | 305 event.value.uncommitted = true; |
| 305 this.dispatchEvent(event); | 306 this.dispatchEvent(event); |
| 306 } | 307 } |
| 307 }; | 308 }; |
| 308 | 309 |
| 309 /** | 310 /** |
| 310 * Callback for fetchPrefs method. | 311 * Callback for fetchPrefs method. |
| 311 * @param {object} dict Map of fetched property values. | 312 * @param {Object} dict Map of fetched property values. |
| 312 */ | 313 */ |
| 313 Preferences.prefsFetchedCallback = function(dict) { | 314 Preferences.prefsFetchedCallback = function(dict) { |
| 314 Preferences.getInstance().flattenMapAndDispatchEvent_('', dict); | 315 Preferences.getInstance().flattenMapAndDispatchEvent_('', dict); |
| 315 }; | 316 }; |
| 316 | 317 |
| 317 /** | 318 /** |
| 318 * Callback for observePrefs method. | 319 * Callback for observePrefs method. |
| 319 * @param {array} notification An array defining changed preference values. | 320 * @param {Array} notification An array defining changed preference values. |
| 320 * notification[0] contains name of the change preference while its new value | 321 * notification[0] contains name of the change preference while its new |
| 321 * is stored in notification[1]. | 322 * value is stored in notification[1]. |
| 322 */ | 323 */ |
| 323 Preferences.prefsChangedCallback = function(notification) { | 324 Preferences.prefsChangedCallback = function(notification) { |
| 324 var event = new Event(notification[0]); | 325 var event = new Event(notification[0]); |
| 325 event.value = notification[1]; | 326 event.value = notification[1]; |
| 326 prefs = Preferences.getInstance(); | 327 var prefs = Preferences.getInstance(); |
| 327 prefs.registeredPreferences_[notification[0]] = {orig: notification[1]}; | 328 prefs.registeredPreferences_[notification[0]] = {orig: notification[1]}; |
| 328 if (event.value) | 329 if (event.value) |
| 329 prefs.dispatchEvent(event); | 330 prefs.dispatchEvent(event); |
| 330 }; | 331 }; |
| 331 | 332 |
| 332 // Export | 333 // Export |
| 333 return { | 334 return { |
| 334 Preferences: Preferences | 335 Preferences: Preferences |
| 335 }; | 336 }; |
| 336 | 337 |
| 337 }); | 338 }); |
| OLD | NEW |