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.
13 * @extends {cr.EventTarget}
15 function Preferences() {
16 // Map of registered preferences.
17 this.registeredPreferences_
= {};
20 cr
.addSingletonGetter(Preferences
);
23 * Sets a Boolean preference and signals its new value.
24 * @param {string} name Preference name.
25 * @param {boolean} value New preference value.
26 * @param {boolean} commit Whether to commit the change to Chrome.
27 * @param {string=} opt_metric User metrics identifier.
29 Preferences
.setBooleanPref = function(name
, value
, commit
, opt_metric
) {
31 Preferences
.getInstance().setPrefNoCommit_(name
, 'bool', Boolean(value
));
35 var argumentList
= [name
, Boolean(value
)];
36 if (opt_metric
!= undefined) argumentList
.push(opt_metric
);
37 chrome
.send('setBooleanPref', argumentList
);
41 * Sets an integer preference and signals its new value.
42 * @param {string} name Preference name.
43 * @param {number} value New preference value.
44 * @param {boolean} commit Whether to commit the change to Chrome.
45 * @param {string} metric User metrics identifier.
47 Preferences
.setIntegerPref = function(name
, value
, commit
, metric
) {
49 Preferences
.getInstance().setPrefNoCommit_(name
, 'int', Number(value
));
53 var argumentList
= [name
, Number(value
)];
54 if (metric
!= undefined) argumentList
.push(metric
);
55 chrome
.send('setIntegerPref', argumentList
);
59 * Sets a double-valued preference and signals its new value.
60 * @param {string} name Preference name.
61 * @param {number} value New preference value.
62 * @param {boolean} commit Whether to commit the change to Chrome.
63 * @param {string} metric User metrics identifier.
65 Preferences
.setDoublePref = function(name
, value
, commit
, metric
) {
67 Preferences
.getInstance().setPrefNoCommit_(name
, 'double', Number(value
));
71 var argumentList
= [name
, Number(value
)];
72 if (metric
!= undefined) argumentList
.push(metric
);
73 chrome
.send('setDoublePref', argumentList
);
77 * Sets a string preference and signals its new value.
78 * @param {string} name Preference name.
79 * @param {string} value New preference value.
80 * @param {boolean} commit Whether to commit the change to Chrome.
81 * @param {string} metric User metrics identifier.
83 Preferences
.setStringPref = function(name
, value
, commit
, metric
) {
85 Preferences
.getInstance().setPrefNoCommit_(name
, 'string', String(value
));
89 var argumentList
= [name
, String(value
)];
90 if (metric
!= undefined) argumentList
.push(metric
);
91 chrome
.send('setStringPref', argumentList
);
95 * Sets a string preference that represents a URL and signals its new value.
96 * The value will be fixed to be a valid URL when it gets committed to Chrome.
97 * @param {string} name Preference name.
98 * @param {string} value New preference value.
99 * @param {boolean} commit Whether to commit the change to Chrome.
100 * @param {string} metric User metrics identifier.
102 Preferences
.setURLPref = function(name
, value
, commit
, metric
) {
104 Preferences
.getInstance().setPrefNoCommit_(name
, 'url', String(value
));
108 var argumentList
= [name
, String(value
)];
109 if (metric
!= undefined) argumentList
.push(metric
);
110 chrome
.send('setURLPref', argumentList
);
114 * Sets a JSON list preference and signals its new value.
115 * @param {string} name Preference name.
116 * @param {Array} value New preference value.
117 * @param {boolean} commit Whether to commit the change to Chrome.
118 * @param {string} metric User metrics identifier.
120 Preferences
.setListPref = function(name
, value
, commit
, metric
) {
122 Preferences
.getInstance().setPrefNoCommit_(name
, 'list', value
);
126 var argumentList
= [name
, JSON
.stringify(value
)];
127 if (metric
!= undefined) argumentList
.push(metric
);
128 chrome
.send('setListPref', argumentList
);
132 * Clears the user setting for a preference and signals its new effective
134 * @param {string} name Preference name.
135 * @param {boolean} commit Whether to commit the change to Chrome.
136 * @param {string=} opt_metric User metrics identifier.
138 Preferences
.clearPref = function(name
, commit
, opt_metric
) {
140 Preferences
.getInstance().clearPrefNoCommit_(name
);
144 var argumentList
= [name
];
145 if (opt_metric
!= undefined) argumentList
.push(opt_metric
);
146 chrome
.send('clearPref', argumentList
);
149 Preferences
.prototype = {
150 __proto__
: cr
.EventTarget
.prototype,
153 * Adds an event listener to the target.
154 * @param {string} type The name of the event.
155 * @param {EventListenerType} handler The handler for the event. This is
156 * called when the event is dispatched.
158 addEventListener: function(type
, handler
) {
159 cr
.EventTarget
.prototype.addEventListener
.call(this, type
, handler
);
160 if (!(type
in this.registeredPreferences_
))
161 this.registeredPreferences_
[type
] = {};
165 * Initializes preference reading and change notifications.
167 initialize: function() {
168 var params1
= ['Preferences.prefsFetchedCallback'];
169 var params2
= ['Preferences.prefsChangedCallback'];
170 for (var prefName
in this.registeredPreferences_
) {
171 params1
.push(prefName
);
172 params2
.push(prefName
);
174 chrome
.send('fetchPrefs', params1
);
175 chrome
.send('observePrefs', params2
);
179 * Helper function for flattening of dictionary passed via fetchPrefs
181 * @param {string} prefix Preference name prefix.
182 * @param {Object} dict Map with preference values.
185 flattenMapAndDispatchEvent_: function(prefix
, dict
) {
186 for (var prefName
in dict
) {
187 var value
= dict
[prefName
];
188 if (typeof value
== 'object' &&
189 !this.registeredPreferences_
[prefix
+ prefName
]) {
190 this.flattenMapAndDispatchEvent_(prefix
+ prefName
+ '.', value
);
192 var event
= new Event(prefix
+ prefName
);
193 this.registeredPreferences_
[prefix
+ prefName
].orig
= value
;
195 this.dispatchEvent(event
);
201 * Sets a preference and signals its new value. The change is propagated
202 * throughout the UI code but is not committed to Chrome yet. The new value
203 * and its data type are stored so that commitPref() can later be used to
204 * invoke the appropriate set*Pref() method and actually commit the change.
205 * @param {string} name Preference name.
206 * @param {string} type Preference data type.
207 * @param {*} value New preference value.
210 setPrefNoCommit_: function(name
, type
, value
) {
211 var pref
= this.registeredPreferences_
[name
];
216 var event
= new Event(name
);
217 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does.
218 event
.value
= {value
: value
, uncommitted
: true};
220 event
.value
.recommendedValue
= pref
.orig
.recommendedValue
;
221 event
.value
.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.
240 event
.value
= {controlledBy
: 'recommended', uncommitted
: true};
242 event
.value
.value
= pref
.orig
.recommendedValue
;
243 event
.value
.recommendedValue
= pref
.orig
.recommendedValue
;
244 event
.value
.disabled
= pref
.orig
.disabled
;
246 this.dispatchEvent(event
);
250 * Commits a preference change to Chrome and signals the new preference
251 * value. Does nothing if there is no uncommitted change.
252 * @param {string} name Preference name.
253 * @param {string} metric User metrics identifier.
255 commitPref: function(name
, metric
) {
256 var pref
= this.registeredPreferences_
[name
];
257 switch (pref
.action
) {
261 Preferences
.setBooleanPref(name
, pref
.value
, true, metric
);
264 Preferences
.setIntegerPref(name
, pref
.value
, true, metric
);
267 Preferences
.setDoublePref(name
, pref
.value
, true, metric
);
270 Preferences
.setStringPref(name
, pref
.value
, true, metric
);
273 Preferences
.setURLPref(name
, pref
.value
, true, metric
);
276 Preferences
.setListPref(name
, pref
.value
, true, metric
);
281 Preferences
.clearPref(name
, true, metric
);
290 * Rolls back a preference change and signals the original preference value.
291 * Does nothing if there is no uncommitted change.
292 * @param {string} name Preference name.
294 rollbackPref: function(name
) {
295 var pref
= this.registeredPreferences_
[name
];
303 var event
= new Event(name
);
304 event
.value
= pref
.orig
|| {};
305 event
.value
.uncommitted
= true;
306 this.dispatchEvent(event
);
311 * Callback for fetchPrefs method.
312 * @param {Object} dict Map of fetched property values.
314 Preferences
.prefsFetchedCallback = function(dict
) {
315 Preferences
.getInstance().flattenMapAndDispatchEvent_('', dict
);
319 * Callback for observePrefs method.
320 * @param {Array} notification An array defining changed preference values.
321 * notification[0] contains name of the change preference while its new
322 * value is stored in notification[1].
324 Preferences
.prefsChangedCallback = function(notification
) {
325 var event
= new Event(notification
[0]);
326 event
.value
= notification
[1];
327 var prefs
= Preferences
.getInstance();
328 prefs
.registeredPreferences_
[notification
[0]] = {orig
: notification
[1]};
330 prefs
.dispatchEvent(event
);
335 Preferences
: Preferences