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 |
| 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. |
| 208 * @private |
| 209 */ |
| 210 setPrefNoCommit_: function(name, type, value) { |
| 211 var pref = this.registeredPreferences_[name]; |
| 212 pref.action = 'set'; |
| 213 pref.type = type; |
| 214 pref.value = value; |
| 215 |
| 216 var event = new cr.Event(name); |
| 217 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does. |
| 218 event.value = { |
| 219 value: value, |
| 220 recommendedValue: pref.orig.recommendedValue, |
| 221 disabled: pref.orig.disabled, |
| 222 }; |
| 223 this.dispatchEvent(event); |
| 224 }, |
| 225 |
| 226 /** |
| 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. |
| 230 * @private |
| 231 */ |
| 232 clearPrefNoCommit_: function(name) { |
| 233 var pref = this.registeredPreferences_[name]; |
| 234 pref.action = 'clear'; |
| 235 delete pref.type; |
| 236 delete pref.value; |
| 237 |
| 238 var event = new cr.Event(name); |
| 239 // Decorate pref value as CoreOptionsHandler::CreateValueForPref() does. |
| 240 event.value = { |
| 241 value: pref.orig.recommendedValue, |
| 242 controlledBy: 'recommended', |
| 243 recommendedValue: pref.orig.recommendedValue, |
| 244 disabled: pref.orig.disabled, |
| 245 }; |
| 246 this.dispatchEvent(event); |
| 247 }, |
| 248 |
| 249 /** |
| 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. |
| 254 */ |
| 255 commitPref: function(name, metric) { |
| 256 var pref = this.registeredPreferences_[name]; |
| 257 switch (pref.action) { |
| 258 case 'set': |
| 259 switch (pref.type) { |
| 260 case 'bool': |
| 261 Preferences.setBooleanPref(name, pref.value, true, metric); |
| 262 break; |
| 263 case 'int': |
| 264 Preferences.setIntegerPref(name, pref.value, true, metric); |
| 265 break; |
| 266 case 'double': |
| 267 Preferences.setDoublePref(name, pref.value, true, metric); |
| 268 break; |
| 269 case 'string': |
| 270 Preferences.setStringPref(name, pref.value, true, metric); |
| 271 break; |
| 272 case 'url': |
| 273 Preferences.setURLPref(name, pref.value, true, metric); |
| 274 break; |
| 275 case 'list': |
| 276 Preferences.setListPref(name, pref.value, true, metric); |
| 277 break; |
| 278 } |
| 279 break; |
| 280 case 'clear': |
| 281 Preferences.clearPref(name, true, metric); |
| 282 break; |
| 283 } |
| 284 delete pref.action; |
| 285 delete pref.type; |
| 286 delete pref.value; |
| 287 }, |
| 288 |
| 289 /** |
| 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. |
| 293 */ |
| 294 rollbackPref: function(name) { |
| 295 var pref = this.registeredPreferences_[name]; |
| 296 if (!pref.action) |
| 297 return; |
| 298 |
| 299 delete pref.action; |
| 300 delete pref.type; |
| 301 delete pref.value; |
| 302 |
| 303 var event = new cr.Event(name); |
| 304 event.value = pref.orig; |
| 305 this.dispatchEvent(event); |
157 } | 306 } |
158 }; | 307 }; |
159 | 308 |
160 /** | 309 /** |
161 * Callback for fetchPrefs method. | 310 * Callback for fetchPrefs method. |
162 * @param {object} dict Map of fetched property values. | 311 * @param {object} dict Map of fetched property values. |
163 */ | 312 */ |
164 Preferences.prefsFetchedCallback = function(dict) { | 313 Preferences.prefsFetchedCallback = function(dict) { |
165 Preferences.getInstance().flattenMapAndDispatchEvent_('', dict); | 314 Preferences.getInstance().flattenMapAndDispatchEvent_('', dict); |
166 }; | 315 }; |
167 | 316 |
168 /** | 317 /** |
169 * Callback for observePrefs method. | 318 * Callback for observePrefs method. |
170 * @param {array} notification An array defining changed preference values. | 319 * @param {array} notification An array defining changed preference values. |
171 * notification[0] contains name of the change preference while its new value | 320 * notification[0] contains name of the change preference while its new value |
172 * is stored in notification[1]. | 321 * is stored in notification[1]. |
173 */ | 322 */ |
174 Preferences.prefsChangedCallback = function(notification) { | 323 Preferences.prefsChangedCallback = function(notification) { |
175 var event = new cr.Event(notification[0]); | 324 var event = new cr.Event(notification[0]); |
176 event.value = notification[1]; | 325 event.value = notification[1]; |
177 Preferences.getInstance().dispatchEvent(event); | 326 prefs = Preferences.getInstance(); |
| 327 prefs.registeredPreferences_[notification[0]] = {orig: notification[1]}; |
| 328 prefs.dispatchEvent(event); |
178 }; | 329 }; |
179 | 330 |
180 // Export | 331 // Export |
181 return { | 332 return { |
182 Preferences: Preferences | 333 Preferences: Preferences |
183 }; | 334 }; |
184 | 335 |
185 }); | 336 }); |
OLD | NEW |