OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/browser/dom_ui/core_options_handler.h" | 5 #include "chrome/browser/dom_ui/core_options_handler.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/string16.h" | 8 #include "base/string16.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/metrics/user_metrics.h" | 12 #include "chrome/common/notification_service.h" |
| 13 #include "chrome/common/notification_type.h" |
13 #include "chrome/browser/pref_service.h" | 14 #include "chrome/browser/pref_service.h" |
14 #include "chrome/browser/profile.h" | 15 #include "chrome/browser/profile.h" |
15 #include "chrome/common/notification_service.h" | |
16 #include "chrome/common/notification_type.h" | |
17 #include "grit/browser_resources.h" | 16 #include "grit/browser_resources.h" |
18 #include "grit/chromium_strings.h" | 17 #include "grit/chromium_strings.h" |
19 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
20 #include "grit/locale_settings.h" | 19 #include "grit/locale_settings.h" |
21 #include "grit/theme_resources.h" | 20 #include "grit/theme_resources.h" |
22 | 21 |
23 CoreOptionsHandler::CoreOptionsHandler() { | 22 CoreOptionsHandler::CoreOptionsHandler() { |
24 } | 23 } |
25 | 24 |
26 CoreOptionsHandler::~CoreOptionsHandler() { | 25 CoreOptionsHandler::~CoreOptionsHandler() { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 dom_ui_->RegisterMessageCallback("observePrefs", | 103 dom_ui_->RegisterMessageCallback("observePrefs", |
105 NewCallback(this, &CoreOptionsHandler::HandleObservePrefs)); | 104 NewCallback(this, &CoreOptionsHandler::HandleObservePrefs)); |
106 dom_ui_->RegisterMessageCallback("setBooleanPref", | 105 dom_ui_->RegisterMessageCallback("setBooleanPref", |
107 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); | 106 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); |
108 dom_ui_->RegisterMessageCallback("setIntegerPref", | 107 dom_ui_->RegisterMessageCallback("setIntegerPref", |
109 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); | 108 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); |
110 dom_ui_->RegisterMessageCallback("setStringPref", | 109 dom_ui_->RegisterMessageCallback("setStringPref", |
111 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); | 110 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); |
112 dom_ui_->RegisterMessageCallback("setObjectPref", | 111 dom_ui_->RegisterMessageCallback("setObjectPref", |
113 NewCallback(this, &CoreOptionsHandler::HandleSetObjectPref)); | 112 NewCallback(this, &CoreOptionsHandler::HandleSetObjectPref)); |
114 dom_ui_->RegisterMessageCallback("coreOptionsUserMetricsAction", | |
115 NewCallback(this, &CoreOptionsHandler::HandleUserMetricsAction)); | |
116 } | 113 } |
117 | 114 |
118 void CoreOptionsHandler::HandleInitialize(const ListValue* args) { | 115 void CoreOptionsHandler::HandleInitialize(const ListValue* args) { |
119 static_cast<OptionsUI*>(dom_ui_)->InitializeHandlers(); | 116 static_cast<OptionsUI*>(dom_ui_)->InitializeHandlers(); |
120 } | 117 } |
121 | 118 |
122 Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) { | 119 Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) { |
123 DCHECK(dom_ui_); | 120 DCHECK(dom_ui_); |
124 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 121 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
125 | 122 |
(...skipping 13 matching lines...) Expand all Loading... |
139 } | 136 } |
140 | 137 |
141 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { | 138 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { |
142 DCHECK(dom_ui_); | 139 DCHECK(dom_ui_); |
143 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 140 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
144 pref_service->AddPrefObserver(pref_name.c_str(), this); | 141 pref_service->AddPrefObserver(pref_name.c_str(), this); |
145 } | 142 } |
146 | 143 |
147 void CoreOptionsHandler::SetPref(const std::string& pref_name, | 144 void CoreOptionsHandler::SetPref(const std::string& pref_name, |
148 Value::ValueType pref_type, | 145 Value::ValueType pref_type, |
149 const std::string& value_string, | 146 const std::string& value_string) { |
150 const std::string& metric) { | |
151 DCHECK(dom_ui_); | 147 DCHECK(dom_ui_); |
152 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 148 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
153 | 149 |
154 switch (pref_type) { | 150 switch (pref_type) { |
155 case Value::TYPE_BOOLEAN: | 151 case Value::TYPE_BOOLEAN: |
156 pref_service->SetBoolean(pref_name.c_str(), value_string == "true"); | 152 pref_service->SetBoolean(pref_name.c_str(), value_string == "true"); |
157 break; | 153 break; |
158 case Value::TYPE_INTEGER: | 154 case Value::TYPE_INTEGER: |
159 int int_value; | 155 int int_value; |
160 if (base::StringToInt(value_string, &int_value)) | 156 if (base::StringToInt(value_string, &int_value)) |
161 pref_service->SetInteger(pref_name.c_str(), int_value); | 157 pref_service->SetInteger(pref_name.c_str(), int_value); |
162 break; | 158 break; |
163 case Value::TYPE_STRING: | 159 case Value::TYPE_STRING: |
164 pref_service->SetString(pref_name.c_str(), value_string); | 160 pref_service->SetString(pref_name.c_str(), value_string); |
165 break; | 161 break; |
166 default: | 162 default: |
167 NOTREACHED(); | 163 NOTREACHED(); |
168 } | 164 } |
169 | |
170 ProcessUserMetric(pref_type, value_string, metric); | |
171 } | |
172 | |
173 void CoreOptionsHandler::ProcessUserMetric(Value::ValueType pref_type, | |
174 const std::string& value_string, | |
175 const std::string& metric) { | |
176 if (metric.empty()) | |
177 return; | |
178 | |
179 std::string metric_string = metric; | |
180 if (pref_type == Value::TYPE_BOOLEAN) | |
181 metric_string += (value_string == "true" ? "_Enable" : "_Disable"); | |
182 | |
183 UserMetricsRecordAction(UserMetricsAction(metric_string.c_str()), | |
184 dom_ui_->GetProfile()->GetPrefs()); | |
185 } | 165 } |
186 | 166 |
187 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { | 167 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { |
188 // First param is name of callback function, so, there needs to be at least | 168 // First param is name of callback function, so, there needs to be at least |
189 // one more element for the actual preference identifier. | 169 // one more element for the actual preference identifier. |
190 const size_t kMinFetchPrefsParamCount = 2; | 170 const size_t kMinFetchPrefsParamCount = 2; |
191 if (args->GetSize() < kMinFetchPrefsParamCount) | 171 if (args->GetSize() < kMinFetchPrefsParamCount) |
192 return; | 172 return; |
193 | 173 |
194 // Get callback JS function name. | 174 // Get callback JS function name. |
(...skipping 20 matching lines...) Expand all Loading... |
215 if (!list_member->GetAsString(&pref_name)) | 195 if (!list_member->GetAsString(&pref_name)) |
216 continue; | 196 continue; |
217 | 197 |
218 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); | 198 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); |
219 } | 199 } |
220 dom_ui_->CallJavascriptFunction(UTF16ToWideHack(callback_function).c_str(), | 200 dom_ui_->CallJavascriptFunction(UTF16ToWideHack(callback_function).c_str(), |
221 result_value); | 201 result_value); |
222 } | 202 } |
223 | 203 |
224 void CoreOptionsHandler::HandleObservePrefs(const ListValue* args) { | 204 void CoreOptionsHandler::HandleObservePrefs(const ListValue* args) { |
| 205 DictionaryValue result_value; |
| 206 |
225 // First param is name is JS callback function name, the rest are pref | 207 // First param is name is JS callback function name, the rest are pref |
226 // identifiers that we are observing. | 208 // identifiers that we are observing. |
227 const size_t kMinObservePrefsParamCount = 2; | 209 const size_t kMinObservePrefsParamCount = 2; |
228 if (args->GetSize() < kMinObservePrefsParamCount) | 210 if (args->GetSize() < kMinObservePrefsParamCount) |
229 return; | 211 return; |
230 | 212 |
231 // Get preference change callback function name. | 213 // Get preference change callback function name. |
232 string16 callback_func_name; | 214 string16 callback_func_name; |
233 if (!args->GetString(0, &callback_func_name)) | 215 if (!args->GetString(0, &callback_func_name)) |
234 return; | 216 return; |
(...skipping 30 matching lines...) Expand all Loading... |
265 void CoreOptionsHandler::HandleSetStringPref(const ListValue* args) { | 247 void CoreOptionsHandler::HandleSetStringPref(const ListValue* args) { |
266 HandleSetPref(args, Value::TYPE_STRING); | 248 HandleSetPref(args, Value::TYPE_STRING); |
267 } | 249 } |
268 | 250 |
269 void CoreOptionsHandler::HandleSetObjectPref(const ListValue* args) { | 251 void CoreOptionsHandler::HandleSetObjectPref(const ListValue* args) { |
270 HandleSetPref(args, Value::TYPE_NULL); | 252 HandleSetPref(args, Value::TYPE_NULL); |
271 } | 253 } |
272 | 254 |
273 void CoreOptionsHandler::HandleSetPref(const ListValue* args, | 255 void CoreOptionsHandler::HandleSetPref(const ListValue* args, |
274 Value::ValueType type) { | 256 Value::ValueType type) { |
275 if (args->GetSize() < 2) | 257 if (args->GetSize() != 2) |
276 return; | 258 return; |
277 | 259 |
278 std::string pref_name; | 260 std::string pref_name; |
279 if (!args->GetString(0, &pref_name)) | 261 if (!args->GetString(0, &pref_name)) |
280 return; | 262 return; |
281 | 263 |
282 std::string value_string; | 264 std::string value_string; |
283 if (!args->GetString(1, &value_string)) | 265 if (!args->GetString(1, &value_string)) |
284 return; | 266 return; |
285 | 267 |
286 std::string metric; | 268 SetPref(pref_name, type, value_string); |
287 if (args->GetSize() > 2) | |
288 args->GetString(2, &metric); | |
289 | |
290 SetPref(pref_name, type, value_string, metric); | |
291 } | |
292 | |
293 void CoreOptionsHandler::HandleUserMetricsAction(const ListValue* args) { | |
294 std::string metric = WideToUTF8(ExtractStringValue(args)); | |
295 if (!metric.empty()) | |
296 UserMetricsRecordAction(UserMetricsAction(metric.c_str()), NULL); | |
297 } | 269 } |
298 | 270 |
299 void CoreOptionsHandler::NotifyPrefChanged(const std::string* pref_name) { | 271 void CoreOptionsHandler::NotifyPrefChanged(const std::string* pref_name) { |
300 DCHECK(pref_name); | 272 DCHECK(pref_name); |
301 DCHECK(dom_ui_); | 273 DCHECK(dom_ui_); |
302 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 274 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
303 const PrefService::Preference* pref = | 275 const PrefService::Preference* pref = |
304 pref_service->FindPreference(pref_name->c_str()); | 276 pref_service->FindPreference(pref_name->c_str()); |
305 if (pref) { | 277 if (pref) { |
306 for (PreferenceCallbackMap::const_iterator iter = | 278 for (PreferenceCallbackMap::const_iterator iter = |
307 pref_callback_map_.find(*pref_name); | 279 pref_callback_map_.find(*pref_name); |
308 iter != pref_callback_map_.end(); ++iter) { | 280 iter != pref_callback_map_.end(); ++iter) { |
309 const std::wstring& callback_function = iter->second; | 281 const std::wstring& callback_function = iter->second; |
310 ListValue result_value; | 282 ListValue result_value; |
311 result_value.Append(Value::CreateStringValue(pref_name->c_str())); | 283 result_value.Append(Value::CreateStringValue(pref_name->c_str())); |
312 | 284 |
313 DictionaryValue* dict = new DictionaryValue; | 285 DictionaryValue* dict = new DictionaryValue; |
314 dict->Set("value", pref->GetValue()->DeepCopy()); | 286 dict->Set("value", pref->GetValue()->DeepCopy()); |
315 dict->SetBoolean("managed", pref->IsManaged()); | 287 dict->SetBoolean("managed", pref->IsManaged()); |
316 result_value.Append(dict); | 288 result_value.Append(dict); |
317 | 289 |
318 dom_ui_->CallJavascriptFunction(callback_function, result_value); | 290 dom_ui_->CallJavascriptFunction(callback_function, result_value); |
319 } | 291 } |
320 } | 292 } |
321 } | 293 } |
322 | 294 |
OLD | NEW |