OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/webui/options2/core_options_handler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/json/json_reader.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/string16.h" | |
12 #include "base/string_number_conversions.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "base/values.h" | |
15 #include "chrome/browser/browser_process.h" | |
16 #include "chrome/browser/net/url_fixer_upper.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/common/chrome_notification_types.h" | |
19 #include "chrome/common/pref_names.h" | |
20 #include "chrome/common/url_constants.h" | |
21 #include "content/public/browser/notification_details.h" | |
22 #include "content/public/browser/notification_types.h" | |
23 #include "content/public/browser/user_metrics.h" | |
24 #include "content/public/browser/web_ui.h" | |
25 #include "googleurl/src/gurl.h" | |
26 #include "grit/chromium_strings.h" | |
27 #include "grit/generated_resources.h" | |
28 #include "grit/locale_settings.h" | |
29 #include "grit/theme_resources.h" | |
30 #include "ui/base/l10n/l10n_util.h" | |
31 | |
32 using content::UserMetricsAction; | |
33 | |
34 namespace options { | |
35 | |
36 CoreOptionsHandler::CoreOptionsHandler() | |
37 : handlers_host_(NULL) { | |
38 } | |
39 | |
40 CoreOptionsHandler::~CoreOptionsHandler() {} | |
41 | |
42 void CoreOptionsHandler::InitializeHandler() { | |
43 plugin_status_pref_setter_.Init(Profile::FromWebUI(web_ui()), this); | |
44 } | |
45 | |
46 void CoreOptionsHandler::InitializePage() { | |
47 UpdateClearPluginLSOData(); | |
48 UpdatePepperFlashSettingsEnabled(); | |
49 } | |
50 | |
51 void CoreOptionsHandler::GetLocalizedValues( | |
52 DictionaryValue* localized_strings) { | |
53 GetStaticLocalizedValues(localized_strings); | |
54 } | |
55 | |
56 void CoreOptionsHandler::GetStaticLocalizedValues( | |
57 base::DictionaryValue* localized_strings) { | |
58 DCHECK(localized_strings); | |
59 // Main | |
60 localized_strings->SetString("optionsPageTitle", | |
61 l10n_util::GetStringUTF16(IDS_SETTINGS_TITLE)); | |
62 | |
63 // Managed prefs | |
64 localized_strings->SetString("policyManagedPrefsBannerText", | |
65 l10n_util::GetStringUTF16(IDS_OPTIONS_POLICY_MANAGED_PREFS)); | |
66 localized_strings->SetString("extensionManagedPrefsBannerText", | |
67 l10n_util::GetStringUTF16(IDS_OPTIONS_EXTENSION_MANAGED_PREFS)); | |
68 localized_strings->SetString("policyAndExtensionManagedPrefsBannerText", | |
69 l10n_util::GetStringUTF16(IDS_OPTIONS_POLICY_EXTENSION_MANAGED_PREFS)); | |
70 | |
71 // Controlled settings bubble. | |
72 localized_strings->SetString("controlledSettingPolicy", | |
73 l10n_util::GetStringUTF16(IDS_OPTIONS_CONTROLLED_SETTING_POLICY)); | |
74 localized_strings->SetString("controlledSettingExtension", | |
75 l10n_util::GetStringUTF16(IDS_OPTIONS_CONTROLLED_SETTING_EXTENSION)); | |
76 localized_strings->SetString("controlledSettingRecommended", | |
77 l10n_util::GetStringUTF16(IDS_OPTIONS_CONTROLLED_SETTING_RECOMMENDED)); | |
78 localized_strings->SetString("controlledSettingApplyRecommendation", | |
79 l10n_util::GetStringUTF16( | |
80 IDS_OPTIONS_CONTROLLED_SETTING_APPLY_RECOMMENDATION)); | |
81 | |
82 // Search | |
83 RegisterTitle(localized_strings, "searchPage", IDS_OPTIONS_SEARCH_PAGE_TITLE); | |
84 localized_strings->SetString("searchPlaceholder", | |
85 l10n_util::GetStringUTF16(IDS_OPTIONS_SEARCH_PLACEHOLDER)); | |
86 localized_strings->SetString("searchPageNoMatches", | |
87 l10n_util::GetStringUTF16(IDS_OPTIONS_SEARCH_PAGE_NO_MATCHES)); | |
88 localized_strings->SetString("searchPageHelpLabel", | |
89 l10n_util::GetStringUTF16(IDS_OPTIONS_SEARCH_PAGE_HELP_LABEL)); | |
90 localized_strings->SetString("searchPageHelpTitle", | |
91 l10n_util::GetStringFUTF16(IDS_OPTIONS_SEARCH_PAGE_HELP_TITLE, | |
92 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); | |
93 localized_strings->SetString("searchPageHelpURL", | |
94 chrome::kSettingsSearchHelpURL); | |
95 | |
96 // Common | |
97 localized_strings->SetString("ok", | |
98 l10n_util::GetStringUTF16(IDS_OK)); | |
99 localized_strings->SetString("cancel", | |
100 l10n_util::GetStringUTF16(IDS_CANCEL)); | |
101 localized_strings->SetString("learnMore", | |
102 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); | |
103 localized_strings->SetString("close", | |
104 l10n_util::GetStringUTF16(IDS_CLOSE)); | |
105 } | |
106 | |
107 void CoreOptionsHandler::Uninitialize() { | |
108 std::string last_pref; | |
109 for (PreferenceCallbackMap::const_iterator iter = pref_callback_map_.begin(); | |
110 iter != pref_callback_map_.end(); | |
111 ++iter) { | |
112 if (last_pref != iter->first) { | |
113 StopObservingPref(iter->first); | |
114 last_pref = iter->first; | |
115 } | |
116 } | |
117 } | |
118 | |
119 void CoreOptionsHandler::Observe(int type, | |
120 const content::NotificationSource& source, | |
121 const content::NotificationDetails& details) { | |
122 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | |
123 std::string* pref_name = content::Details<std::string>(details).ptr(); | |
124 if (*pref_name == prefs::kClearPluginLSODataEnabled) { | |
125 // This preference is stored in Local State, not in the user preferences. | |
126 UpdateClearPluginLSOData(); | |
127 return; | |
128 } | |
129 if (*pref_name == prefs::kPepperFlashSettingsEnabled) { | |
130 UpdatePepperFlashSettingsEnabled(); | |
131 return; | |
132 } | |
133 NotifyPrefChanged(*pref_name, std::string()); | |
134 } | |
135 } | |
136 | |
137 void CoreOptionsHandler::RegisterMessages() { | |
138 registrar_.Init(Profile::FromWebUI(web_ui())->GetPrefs()); | |
139 | |
140 web_ui()->RegisterMessageCallback("coreOptionsInitialize", | |
141 base::Bind(&CoreOptionsHandler::HandleInitialize, | |
142 base::Unretained(this))); | |
143 web_ui()->RegisterMessageCallback("fetchPrefs", | |
144 base::Bind(&CoreOptionsHandler::HandleFetchPrefs, | |
145 base::Unretained(this))); | |
146 web_ui()->RegisterMessageCallback("observePrefs", | |
147 base::Bind(&CoreOptionsHandler::HandleObservePrefs, | |
148 base::Unretained(this))); | |
149 web_ui()->RegisterMessageCallback("setBooleanPref", | |
150 base::Bind(&CoreOptionsHandler::HandleSetBooleanPref, | |
151 base::Unretained(this))); | |
152 web_ui()->RegisterMessageCallback("setIntegerPref", | |
153 base::Bind(&CoreOptionsHandler::HandleSetIntegerPref, | |
154 base::Unretained(this))); | |
155 web_ui()->RegisterMessageCallback("setDoublePref", | |
156 base::Bind(&CoreOptionsHandler::HandleSetDoublePref, | |
157 base::Unretained(this))); | |
158 web_ui()->RegisterMessageCallback("setStringPref", | |
159 base::Bind(&CoreOptionsHandler::HandleSetStringPref, | |
160 base::Unretained(this))); | |
161 web_ui()->RegisterMessageCallback("setURLPref", | |
162 base::Bind(&CoreOptionsHandler::HandleSetURLPref, | |
163 base::Unretained(this))); | |
164 web_ui()->RegisterMessageCallback("setListPref", | |
165 base::Bind(&CoreOptionsHandler::HandleSetListPref, | |
166 base::Unretained(this))); | |
167 web_ui()->RegisterMessageCallback("clearPref", | |
168 base::Bind(&CoreOptionsHandler::HandleClearPref, | |
169 base::Unretained(this))); | |
170 web_ui()->RegisterMessageCallback("coreOptionsUserMetricsAction", | |
171 base::Bind(&CoreOptionsHandler::HandleUserMetricsAction, | |
172 base::Unretained(this))); | |
173 } | |
174 | |
175 void CoreOptionsHandler::HandleInitialize(const ListValue* args) { | |
176 DCHECK(handlers_host_); | |
177 handlers_host_->InitializeHandlers(); | |
178 } | |
179 | |
180 base::Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) { | |
181 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
182 | |
183 const PrefService::Preference* pref = | |
184 pref_service->FindPreference(pref_name.c_str()); | |
185 if (!pref) | |
186 return base::Value::CreateNullValue(); | |
187 | |
188 return CreateValueForPref(pref, NULL); | |
189 } | |
190 | |
191 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { | |
192 registrar_.Add(pref_name.c_str(), this); | |
193 } | |
194 | |
195 void CoreOptionsHandler::SetPref(const std::string& pref_name, | |
196 const base::Value* value, | |
197 const std::string& metric) { | |
198 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
199 | |
200 switch (value->GetType()) { | |
201 case base::Value::TYPE_BOOLEAN: | |
202 case base::Value::TYPE_INTEGER: | |
203 case base::Value::TYPE_DOUBLE: | |
204 case base::Value::TYPE_STRING: | |
205 pref_service->Set(pref_name.c_str(), *value); | |
206 break; | |
207 | |
208 default: | |
209 NOTREACHED(); | |
210 return; | |
211 } | |
212 | |
213 ProcessUserMetric(value, metric); | |
214 } | |
215 | |
216 void CoreOptionsHandler::ClearPref(const std::string& pref_name, | |
217 const std::string& metric) { | |
218 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
219 pref_service->ClearPref(pref_name.c_str()); | |
220 | |
221 if (!metric.empty()) | |
222 content::RecordComputedAction(metric); | |
223 } | |
224 | |
225 void CoreOptionsHandler::ProcessUserMetric(const base::Value* value, | |
226 const std::string& metric) { | |
227 if (metric.empty()) | |
228 return; | |
229 | |
230 std::string metric_string = metric; | |
231 if (value->IsType(base::Value::TYPE_BOOLEAN)) { | |
232 bool bool_value; | |
233 CHECK(value->GetAsBoolean(&bool_value)); | |
234 metric_string += bool_value ? "_Enable" : "_Disable"; | |
235 } | |
236 | |
237 content::RecordComputedAction(metric_string); | |
238 } | |
239 | |
240 void CoreOptionsHandler::NotifyPrefChanged( | |
241 const std::string& pref_name, | |
242 const std::string& controlling_pref_name) { | |
243 const PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
244 const PrefService::Preference* pref = | |
245 pref_service->FindPreference(pref_name.c_str()); | |
246 if (!pref) | |
247 return; | |
248 const PrefService::Preference* controlling_pref = | |
249 !controlling_pref_name.empty() ? | |
250 pref_service->FindPreference(controlling_pref_name.c_str()) : NULL; | |
251 | |
252 scoped_ptr<base::Value> value(CreateValueForPref(pref, controlling_pref)); | |
253 DispatchPrefChangeNotification(pref_name, value.Pass()); | |
254 } | |
255 | |
256 void CoreOptionsHandler::DispatchPrefChangeNotification( | |
257 const std::string& name, | |
258 scoped_ptr<base::Value> value) { | |
259 std::pair<PreferenceCallbackMap::const_iterator, | |
260 PreferenceCallbackMap::const_iterator> range = | |
261 pref_callback_map_.equal_range(name); | |
262 ListValue result_value; | |
263 result_value.Append(base::Value::CreateStringValue(name.c_str())); | |
264 result_value.Append(value.release()); | |
265 for (PreferenceCallbackMap::const_iterator iter = range.first; | |
266 iter != range.second; ++iter) { | |
267 const std::wstring& callback_function = iter->second; | |
268 web_ui()->CallJavascriptFunction(WideToASCII(callback_function), | |
269 result_value); | |
270 } | |
271 } | |
272 | |
273 DictionaryValue* CoreOptionsHandler::CreateValueForPref( | |
274 const PrefService::Preference* pref, | |
275 const PrefService::Preference* controlling_pref) { | |
276 DictionaryValue* dict = new DictionaryValue; | |
277 dict->Set("value", pref->GetValue()->DeepCopy()); | |
278 if (!controlling_pref) // No controlling pref is managing actual pref. | |
279 controlling_pref = pref; // This means pref is controlling itself. | |
280 if (controlling_pref->IsManaged()) { | |
281 dict->SetString("controlledBy", "policy"); | |
282 } else if (controlling_pref->IsExtensionControlled()) { | |
283 dict->SetString("controlledBy", "extension"); | |
284 } else if (controlling_pref->IsRecommended()) { | |
285 dict->SetString("controlledBy", "recommended"); | |
286 } | |
287 dict->SetBoolean("disabled", !controlling_pref->IsUserModifiable()); | |
288 return dict; | |
289 } | |
290 | |
291 void CoreOptionsHandler::StopObservingPref(const std::string& path) { | |
292 registrar_.Remove(path.c_str(), this); | |
293 } | |
294 | |
295 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { | |
296 // First param is name of callback function, so, there needs to be at least | |
297 // one more element for the actual preference identifier. | |
298 DCHECK_GE(static_cast<int>(args->GetSize()), 2); | |
299 | |
300 // Get callback JS function name. | |
301 const base::Value* callback; | |
302 if (!args->Get(0, &callback) || !callback->IsType(base::Value::TYPE_STRING)) | |
303 return; | |
304 | |
305 string16 callback_function; | |
306 if (!callback->GetAsString(&callback_function)) | |
307 return; | |
308 | |
309 // Get the list of name for prefs to build the response dictionary. | |
310 DictionaryValue result_value; | |
311 const base::Value* list_member; | |
312 | |
313 for (size_t i = 1; i < args->GetSize(); i++) { | |
314 if (!args->Get(i, &list_member)) | |
315 break; | |
316 | |
317 if (!list_member->IsType(base::Value::TYPE_STRING)) | |
318 continue; | |
319 | |
320 std::string pref_name; | |
321 if (!list_member->GetAsString(&pref_name)) | |
322 continue; | |
323 | |
324 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); | |
325 } | |
326 web_ui()->CallJavascriptFunction(UTF16ToASCII(callback_function), | |
327 result_value); | |
328 } | |
329 | |
330 void CoreOptionsHandler::HandleObservePrefs(const ListValue* args) { | |
331 // First param is name is JS callback function name, the rest are pref | |
332 // identifiers that we are observing. | |
333 DCHECK_GE(static_cast<int>(args->GetSize()), 2); | |
334 | |
335 // Get preference change callback function name. | |
336 string16 callback_func_name; | |
337 if (!args->GetString(0, &callback_func_name)) | |
338 return; | |
339 | |
340 // Get all other parameters - pref identifiers. | |
341 for (size_t i = 1; i < args->GetSize(); i++) { | |
342 const base::Value* list_member; | |
343 if (!args->Get(i, &list_member)) | |
344 break; | |
345 | |
346 // Just ignore bad pref identifiers for now. | |
347 std::string pref_name; | |
348 if (!list_member->IsType(base::Value::TYPE_STRING) || | |
349 !list_member->GetAsString(&pref_name)) | |
350 continue; | |
351 | |
352 if (pref_callback_map_.find(pref_name) == pref_callback_map_.end()) | |
353 ObservePref(pref_name); | |
354 | |
355 pref_callback_map_.insert( | |
356 PreferenceCallbackMap::value_type(pref_name, | |
357 UTF16ToWideHack(callback_func_name))); | |
358 } | |
359 } | |
360 | |
361 void CoreOptionsHandler::HandleSetBooleanPref(const ListValue* args) { | |
362 HandleSetPref(args, TYPE_BOOLEAN); | |
363 } | |
364 | |
365 void CoreOptionsHandler::HandleSetIntegerPref(const ListValue* args) { | |
366 HandleSetPref(args, TYPE_INTEGER); | |
367 } | |
368 | |
369 void CoreOptionsHandler::HandleSetDoublePref(const ListValue* args) { | |
370 HandleSetPref(args, TYPE_DOUBLE); | |
371 } | |
372 | |
373 void CoreOptionsHandler::HandleSetStringPref(const ListValue* args) { | |
374 HandleSetPref(args, TYPE_STRING); | |
375 } | |
376 | |
377 void CoreOptionsHandler::HandleSetURLPref(const ListValue* args) { | |
378 HandleSetPref(args, TYPE_URL); | |
379 } | |
380 | |
381 void CoreOptionsHandler::HandleSetListPref(const ListValue* args) { | |
382 HandleSetPref(args, TYPE_LIST); | |
383 } | |
384 | |
385 void CoreOptionsHandler::HandleSetPref(const ListValue* args, PrefType type) { | |
386 DCHECK_GT(static_cast<int>(args->GetSize()), 1); | |
387 | |
388 std::string pref_name; | |
389 if (!args->GetString(0, &pref_name)) | |
390 return; | |
391 | |
392 const base::Value* value; | |
393 if (!args->Get(1, &value)) | |
394 return; | |
395 | |
396 scoped_ptr<base::Value> temp_value; | |
397 | |
398 switch (type) { | |
399 case TYPE_BOOLEAN: | |
400 CHECK_EQ(base::Value::TYPE_BOOLEAN, value->GetType()); | |
401 break; | |
402 case TYPE_INTEGER: { | |
403 // In JS all numbers are doubles. | |
404 double double_value; | |
405 CHECK(value->GetAsDouble(&double_value)); | |
406 int int_value = static_cast<int>(double_value); | |
407 temp_value.reset(base::Value::CreateIntegerValue(int_value)); | |
408 value = temp_value.get(); | |
409 break; | |
410 } | |
411 case TYPE_DOUBLE: | |
412 CHECK_EQ(base::Value::TYPE_DOUBLE, value->GetType()); | |
413 break; | |
414 case TYPE_STRING: | |
415 CHECK_EQ(base::Value::TYPE_STRING, value->GetType()); | |
416 break; | |
417 case TYPE_URL: { | |
418 std::string original; | |
419 CHECK(value->GetAsString(&original)); | |
420 GURL fixed = URLFixerUpper::FixupURL(original, std::string()); | |
421 temp_value.reset(base::Value::CreateStringValue(fixed.spec())); | |
422 value = temp_value.get(); | |
423 break; | |
424 } | |
425 case TYPE_LIST: { | |
426 // In case we have a List pref we got a JSON string. | |
427 std::string json_string; | |
428 CHECK(value->GetAsString(&json_string)); | |
429 temp_value.reset( | |
430 base::JSONReader::Read(json_string)); | |
431 value = temp_value.get(); | |
432 CHECK_EQ(base::Value::TYPE_LIST, value->GetType()); | |
433 break; | |
434 } | |
435 default: | |
436 NOTREACHED(); | |
437 } | |
438 | |
439 std::string metric; | |
440 if (args->GetSize() > 2 && !args->GetString(2, &metric)) | |
441 LOG(WARNING) << "Invalid metric parameter: " << pref_name; | |
442 SetPref(pref_name, value, metric); | |
443 } | |
444 | |
445 void CoreOptionsHandler::HandleClearPref(const ListValue* args) { | |
446 DCHECK_GT(static_cast<int>(args->GetSize()), 0); | |
447 | |
448 std::string pref_name; | |
449 if (!args->GetString(0, &pref_name)) | |
450 return; | |
451 | |
452 std::string metric; | |
453 if (args->GetSize() > 1) { | |
454 if (!args->GetString(1, &metric)) | |
455 NOTREACHED(); | |
456 } | |
457 | |
458 ClearPref(pref_name, metric); | |
459 } | |
460 | |
461 void CoreOptionsHandler::HandleUserMetricsAction(const ListValue* args) { | |
462 std::string metric = UTF16ToUTF8(ExtractStringValue(args)); | |
463 if (!metric.empty()) | |
464 content::RecordComputedAction(metric); | |
465 } | |
466 | |
467 void CoreOptionsHandler::UpdateClearPluginLSOData() { | |
468 scoped_ptr<base::Value> enabled( | |
469 base::Value::CreateBooleanValue( | |
470 plugin_status_pref_setter_.IsClearPluginLSODataEnabled())); | |
471 web_ui()->CallJavascriptFunction( | |
472 "OptionsPage.setClearPluginLSODataEnabled", *enabled); | |
473 } | |
474 | |
475 void CoreOptionsHandler::UpdatePepperFlashSettingsEnabled() { | |
476 scoped_ptr<base::Value> enabled( | |
477 base::Value::CreateBooleanValue( | |
478 plugin_status_pref_setter_.IsPepperFlashSettingsEnabled())); | |
479 web_ui()->CallJavascriptFunction( | |
480 "OptionsPage.setPepperFlashSettingsEnabled", *enabled); | |
481 } | |
482 | |
483 } // namespace options | |
OLD | NEW |