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