OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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. | |
4 | |
5 cr.define('options', function() { | |
6 | |
7 ///////////////////////////////////////////////////////////////////////////// | |
8 // Preferences class: | |
9 | |
10 /** | |
11 * Preferences class manages access to Chrome profile preferences. | |
12 * @constructor | |
13 */ | |
14 function Preferences() { | |
15 } | |
16 | |
17 cr.addSingletonGetter(Preferences); | |
18 | |
19 /** | |
20 * Sets value of a boolean preference. | |
21 * and signals its changed value. | |
22 * @param {string} name Preference name. | |
23 * @param {boolean} value New preference value. | |
24 * @param {string} metric User metrics identifier. | |
25 */ | |
26 Preferences.setBooleanPref = function(name, value, metric) { | |
27 var argumentList = [name, Boolean(value)]; | |
28 if (metric != undefined) argumentList.push(metric); | |
29 chrome.send('setBooleanPref', argumentList); | |
30 }; | |
31 | |
32 /** | |
33 * Sets value of an integer preference. | |
34 * and signals its changed value. | |
35 * @param {string} name Preference name. | |
36 * @param {number} value New preference value. | |
37 * @param {string} metric User metrics identifier. | |
38 */ | |
39 Preferences.setIntegerPref = function(name, value, metric) { | |
40 var argumentList = [name, Number(value)]; | |
41 if (metric != undefined) argumentList.push(metric); | |
42 chrome.send('setIntegerPref', argumentList); | |
43 }; | |
44 | |
45 /** | |
46 * Sets value of a double-valued preference. | |
47 * and signals its changed value. | |
48 * @param {string} name Preference name. | |
49 * @param {number} value New preference value. | |
50 * @param {string} metric User metrics identifier. | |
51 */ | |
52 Preferences.setDoublePref = function(name, value, metric) { | |
53 var argumentList = [name, Number(value)]; | |
54 if (metric != undefined) argumentList.push(metric); | |
55 chrome.send('setDoublePref', argumentList); | |
56 }; | |
57 | |
58 /** | |
59 * Sets value of a string preference. | |
60 * and signals its changed value. | |
61 * @param {string} name Preference name. | |
62 * @param {string} value New preference value. | |
63 * @param {string} metric User metrics identifier. | |
64 */ | |
65 Preferences.setStringPref = function(name, value, metric) { | |
66 var argumentList = [name, String(value)]; | |
67 if (metric != undefined) argumentList.push(metric); | |
68 chrome.send('setStringPref', argumentList); | |
69 }; | |
70 | |
71 /** | |
72 * Sets value of a string preference that represents a URL | |
73 * and signals its changed value. The value will be fixed to be a valid URL. | |
74 * @param {string} name Preference name. | |
75 * @param {string} value New preference value. | |
76 * @param {string} metric User metrics identifier. | |
77 */ | |
78 Preferences.setURLPref = function(name, value, metric) { | |
79 var argumentList = [name, String(value)]; | |
80 if (metric != undefined) argumentList.push(metric); | |
81 chrome.send('setURLPref', argumentList); | |
82 }; | |
83 | |
84 /** | |
85 * Sets value of a JSON list preference. | |
86 * and signals its changed value. | |
87 * @param {string} name Preference name. | |
88 * @param {Array} value New preference value. | |
89 * @param {string} metric User metrics identifier. | |
90 */ | |
91 Preferences.setListPref = function(name, value, metric) { | |
92 var argumentList = [name, JSON.stringify(value)]; | |
93 if (metric != undefined) argumentList.push(metric); | |
94 chrome.send('setListPref', argumentList); | |
95 }; | |
96 | |
97 /** | |
98 * Clears value of a JSON preference. | |
99 * @param {string} name Preference name. | |
100 * @param {string} metric User metrics identifier. | |
101 */ | |
102 Preferences.clearPref = function(name, metric) { | |
103 var argumentList = [name]; | |
104 if (metric != undefined) argumentList.push(metric); | |
105 chrome.send('clearPref', argumentList); | |
106 }; | |
107 | |
108 Preferences.prototype = { | |
109 __proto__: cr.EventTarget.prototype, | |
110 | |
111 // Map of registered preferences. | |
112 registeredPreferences_: {}, | |
113 | |
114 /** | |
115 * Adds an event listener to the target. | |
116 * @param {string} type The name of the event. | |
117 * @param {!Function|{handleEvent:Function}} handler The handler for the | |
118 * event. This is called when the event is dispatched. | |
119 */ | |
120 addEventListener: function(type, handler) { | |
121 cr.EventTarget.prototype.addEventListener.call(this, type, handler); | |
122 this.registeredPreferences_[type] = true; | |
123 }, | |
124 | |
125 /** | |
126 * Initializes preference reading and change notifications. | |
127 */ | |
128 initialize: function() { | |
129 var params1 = ['Preferences.prefsFetchedCallback']; | |
130 var params2 = ['Preferences.prefsChangedCallback']; | |
131 for (var prefName in this.registeredPreferences_) { | |
132 params1.push(prefName); | |
133 params2.push(prefName); | |
134 } | |
135 chrome.send('fetchPrefs', params1); | |
136 chrome.send('observePrefs', params2); | |
137 }, | |
138 | |
139 /** | |
140 * Helper function for flattening of dictionary passed via fetchPrefs | |
141 * callback. | |
142 * @param {string} prefix Preference name prefix. | |
143 * @param {object} dict Map with preference values. | |
144 */ | |
145 flattenMapAndDispatchEvent_: function(prefix, dict) { | |
146 for (var prefName in dict) { | |
147 if (typeof dict[prefName] == 'object' && | |
148 !this.registeredPreferences_[prefix + prefName]) { | |
149 this.flattenMapAndDispatchEvent_(prefix + prefName + '.', | |
150 dict[prefName]); | |
151 } else { | |
152 var event = new cr.Event(prefix + prefName); | |
153 event.value = dict[prefName]; | |
154 this.dispatchEvent(event); | |
155 } | |
156 } | |
157 } | |
158 }; | |
159 | |
160 /** | |
161 * Callback for fetchPrefs method. | |
162 * @param {object} dict Map of fetched property values. | |
163 */ | |
164 Preferences.prefsFetchedCallback = function(dict) { | |
165 Preferences.getInstance().flattenMapAndDispatchEvent_('', dict); | |
166 }; | |
167 | |
168 /** | |
169 * Callback for observePrefs method. | |
170 * @param {array} notification An array defining changed preference values. | |
171 * notification[0] contains name of the change preference while its new value | |
172 * is stored in notification[1]. | |
173 */ | |
174 Preferences.prefsChangedCallback = function(notification) { | |
175 var event = new cr.Event(notification[0]); | |
176 event.value = notification[1]; | |
177 Preferences.getInstance().dispatchEvent(event); | |
178 }; | |
179 | |
180 // Export | |
181 return { | |
182 Preferences: Preferences | |
183 }; | |
184 | |
185 }); | |
OLD | NEW |