Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: chrome/browser/ui/webui/options2/core_options_handler.cc

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

Powered by Google App Engine
This is Rietveld 408576698