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
3 // found in the LICENSE file.
5 cr.define('options', function() {
7 /////////////////////////////////////////////////////////////////////////////
11 * Preferences class manages access to Chrome profile preferences.
14 function Preferences() {
15 // Map of registered preferences.
16 this.registeredPreferences_ = {};
19 cr.addSingletonGetter(Preferences);
22 * Sets a Boolean preference and signals its new value.
23 * @param {string} name Preference name.
24 * @param {boolean} value New preference value.
25 * @param {boolean} commit Whether to commit the change to Chrome.
26 * @param {string} metric User metrics identifier.
28 Preferences.setBooleanPref = function(name, value, commit, metric) {
30 Preferences.getInstance().setPrefNoCommit_(name, 'bool', Boolean(value));
34 var argumentList = [name, Boolean(value)];
35 if (metric != undefined) argumentList.push(metric);
36 chrome.send('setBooleanPref', argumentList);
40 * Sets an integer preference and signals its new value.
41 * @param {string} name Preference name.
42 * @param {number} value New preference value.
43 * @param {boolean} commit Whether to commit the change to Chrome.
44 * @param {string} metric User metrics identifier.
46 Preferences.setIntegerPref = function(name, value, commit, metric) {
48 Preferences.getInstance().setPrefNoCommit_(name, 'int', Number(value));
52 var argumentList = [name, Number(value)];
53 if (metric != undefined) argumentList.push(metric);
54 chrome.send('setIntegerPref', argumentList);
58 * Sets a double-valued preference and signals its new value.
59 * @param {string} name Preference name.
60 * @param {number} value New preference value.
61 * @param {boolean} commit Whether to commit the change to Chrome.
62 * @param {string} metric User metrics identifier.
64 Preferences.setDoublePref = function(name, value, commit, metric) {
66 Preferences.getInstance().setPrefNoCommit_(name, 'double', Number(value));
70 var argumentList = [name, Number(value)];
71 if (metric != undefined) argumentList.push(metric);
72 chrome.send('setDoublePref', argumentList);
76 * Sets a string preference and signals its new value.
77 * @param {string} name Preference name.
78 * @param {string} value New preference value.
79 * @param {boolean} commit Whether to commit the change to Chrome.
80 * @param {string} metric User metrics identifier.
82 Preferences.setStringPref = function(name, value, commit, metric) {
84 Preferences.getInstance().setPrefNoCommit_(name, 'string', String(value));
88 var argumentList = [name, String(value)];
89 if (metric != undefined) argumentList.push(metric);
90 chrome.send('setStringPref', argumentList);
94 * Sets a string preference that represents a URL and signals its new value.
95 * The value will be fixed to be a valid URL when it gets committed to Chrome.
96 * @param {string} name Preference name.
97 * @param {string} value New preference value.
98 * @param {boolean} commit Whether to commit the change to Chrome.
99 * @param {string} metric User metrics identifier.
101 Preferences.setURLPref = function(name, value, commit, metric) {
103 Preferences.getInstance().setPrefNoCommit_(name, 'url', String(value));
107 var argumentList = [name, String(value)];
108 if (metric != undefined) argumentList.push(metric);
109 chrome.send('setURLPref', argumentList);
113 * Sets a JSON list preference and signals its new value.
114 * @param {string} name Preference name.
115 * @param {Array} value New preference value.
116 * @param {boolean} commit Whether to commit the change to Chrome.
117 * @param {string} metric User metrics identifier.
119 Preferences.setListPref = function(name, value, commit, metric) {
121 Preferences.getInstance().setPrefNoCommit_(name, 'list', value);
125 var argumentList = [name, JSON.stringify(value)];
126 if (metric != undefined) argumentList.push(metric);
127 chrome.send('setListPref', argumentList);
131 * Clears the user setting for a preference and signals its new effective
133 * @param {string} name Preference name.
134 * @param {boolean} commit Whether to commit the change to Chrome.
135 * @param {string} metric User metrics identifier.
137 Preferences.clearPref = function(name, commit, metric) {
139 Preferences.getInstance().clearPrefNoCommit_(name);
143 var argumentList = [name];
144 if (metric != undefined) argumentList.push(metric);
145 chrome.send('clearPref', argumentList);
148 Preferences.prototype = {
149 __proto__: cr.EventTarget.prototype,
152 * Adds an event listener to the target.
153 * @param {string} type The name of the event.
154 * @param {!Function|{handleEvent:Function}} handler The handler for the
155 * event. This is called when the event is dispatched.
157 addEventListener: function(type, handler) {
158 cr.EventTarget.prototype.addEventListener.call(this, type, handler);
159 if (!(type in this.registeredPreferences_))
160 this.registeredPreferences_[type] = {};
164 * Initializes preference reading and change notifications.
166 initialize: function() {
167 var params1 = ['Preferences.prefsFetchedCallback'];
168 var params2 = ['Preferences.prefsChangedCallback'];
169 for (var prefName in this.registeredPreferences_) {
170 params1.push(prefName);
171 params2.push(prefName);
173 chrome.send('fetchPrefs', params1);
174 chrome.send('observePrefs', params2);
178 * Helper function for flattening of dictionary passed via fetchPrefs
180 * @param {string} prefix Preference name prefix.
181 * @param {object} dict Map with preference values.
184 flattenMapAndDispatchEvent_: function(prefix, dict) {
185 for (var prefName in dict) {
186 if (typeof dict[prefName] == 'object' &&
187 !this.registeredPreferences_[prefix + prefName]) {
188 this.flattenMapAndDispatchEvent_(prefix + prefName + '.',
191 var event = new Event(prefix + prefName);
192 this.registeredPreferences_[prefix + prefName].orig = dict[prefName];
193 event.value = dict[prefName];
194 this.dispatchEvent(event);
200 * Sets a preference and signals its new value. The change is propagated
201 * throughout the UI code but is not committed to Chrome yet. The new value
202 * and its data type are stored so that commitPref() can later be used to
203 * invoke the appropriate set*Pref() method and actually commit the change.
204 * @param {string} name Preference name.
205 * @param {string} type Preference data type.
206 * @param {*} value New preference value.
209 setPrefNoCommit_: function(name, type, value) {
210 var pref = this.registeredPreferences_[name];
215 var event = new Event(name);
216 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does.
219 recommendedValue: pref.orig.recommendedValue,
220 disabled: pref.orig.disabled,
223 this.dispatchEvent(event);
227 * Clears a preference and signals its new value. The change is propagated
228 * throughout the UI code but is not committed to Chrome yet.
229 * @param {string} name Preference name.
232 clearPrefNoCommit_: function(name) {
233 var pref = this.registeredPreferences_[name];
234 pref.action = 'clear';
238 var event = new Event(name);
239 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does.
241 value: pref.orig.recommendedValue,
242 controlledBy: 'recommended',
243 recommendedValue: pref.orig.recommendedValue,
244 disabled: pref.orig.disabled,
247 this.dispatchEvent(event);
251 * Commits a preference change to Chrome and signals the new preference
252 * value. Does nothing if there is no uncommitted change.
253 * @param {string} name Preference name.
254 * @param {string} metric User metrics identifier.
256 commitPref: function(name, metric) {
257 var pref = this.registeredPreferences_[name];
258 switch (pref.action) {
262 Preferences.setBooleanPref(name, pref.value, true, metric);
265 Preferences.setIntegerPref(name, pref.value, true, metric);
268 Preferences.setDoublePref(name, pref.value, true, metric);
271 Preferences.setStringPref(name, pref.value, true, metric);
274 Preferences.setURLPref(name, pref.value, true, metric);
277 Preferences.setListPref(name, pref.value, true, metric);
282 Preferences.clearPref(name, true, metric);
291 * Rolls back a preference change and signals the original preference value.
292 * Does nothing if there is no uncommitted change.
293 * @param {string} name Preference name.
295 rollbackPref: function(name) {
296 var pref = this.registeredPreferences_[name];
304 var event = new Event(name);
305 event.value = pref.orig;
306 event.value.uncommitted = true;
307 this.dispatchEvent(event);
312 * Callback for fetchPrefs method.
313 * @param {object} dict Map of fetched property values.
315 Preferences.prefsFetchedCallback = function(dict) {
316 Preferences.getInstance().flattenMapAndDispatchEvent_('', dict);
320 * Callback for observePrefs method.
321 * @param {array} notification An array defining changed preference values.
322 * notification[0] contains name of the change preference while its new value
323 * is stored in notification[1].
325 Preferences.prefsChangedCallback = function(notification) {
326 var event = new Event(notification[0]);
327 event.value = notification[1];
328 prefs = Preferences.getInstance();
329 prefs.registeredPreferences_[notification[0]] = {orig: notification[1]};
330 prefs.dispatchEvent(event);
335 Preferences: Preferences