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

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

Issue 9814030: get rid of old options pages (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more fixes Created 8 years, 9 months 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) 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/options/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 CoreOptionsHandler::CoreOptionsHandler()
35 : handlers_host_(NULL) {
36 }
37
38 CoreOptionsHandler::~CoreOptionsHandler() {}
39
40 void CoreOptionsHandler::InitializeHandler() {
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 chrome::kSettingsSearchHelpURL);
91
92 // Common
93 localized_strings->SetString("ok",
94 l10n_util::GetStringUTF16(IDS_OK));
95 localized_strings->SetString("cancel",
96 l10n_util::GetStringUTF16(IDS_CANCEL));
97 localized_strings->SetString("learnMore",
98 l10n_util::GetStringUTF16(IDS_LEARN_MORE));
99 localized_strings->SetString("close",
100 l10n_util::GetStringUTF16(IDS_CLOSE));
101 }
102
103 void CoreOptionsHandler::Uninitialize() {
104 std::string last_pref;
105 for (PreferenceCallbackMap::const_iterator iter = pref_callback_map_.begin();
106 iter != pref_callback_map_.end();
107 ++iter) {
108 if (last_pref != iter->first) {
109 StopObservingPref(iter->first);
110 last_pref = iter->first;
111 }
112 }
113 }
114
115 void CoreOptionsHandler::Observe(int type,
116 const content::NotificationSource& source,
117 const content::NotificationDetails& details) {
118 if (type == chrome::NOTIFICATION_PREF_CHANGED) {
119 std::string* pref_name = content::Details<std::string>(details).ptr();
120 if (*pref_name == prefs::kClearPluginLSODataEnabled) {
121 // This preference is stored in Local State, not in the user preferences.
122 UpdateClearPluginLSOData();
123 return;
124 }
125 NotifyPrefChanged(*pref_name, std::string());
126 }
127 }
128
129 void CoreOptionsHandler::RegisterMessages() {
130 registrar_.Init(Profile::FromWebUI(web_ui())->GetPrefs());
131
132 web_ui()->RegisterMessageCallback("coreOptionsInitialize",
133 base::Bind(&CoreOptionsHandler::HandleInitialize,
134 base::Unretained(this)));
135 web_ui()->RegisterMessageCallback("fetchPrefs",
136 base::Bind(&CoreOptionsHandler::HandleFetchPrefs,
137 base::Unretained(this)));
138 web_ui()->RegisterMessageCallback("observePrefs",
139 base::Bind(&CoreOptionsHandler::HandleObservePrefs,
140 base::Unretained(this)));
141 web_ui()->RegisterMessageCallback("setBooleanPref",
142 base::Bind(&CoreOptionsHandler::HandleSetBooleanPref,
143 base::Unretained(this)));
144 web_ui()->RegisterMessageCallback("setIntegerPref",
145 base::Bind(&CoreOptionsHandler::HandleSetIntegerPref,
146 base::Unretained(this)));
147 web_ui()->RegisterMessageCallback("setDoublePref",
148 base::Bind(&CoreOptionsHandler::HandleSetDoublePref,
149 base::Unretained(this)));
150 web_ui()->RegisterMessageCallback("setStringPref",
151 base::Bind(&CoreOptionsHandler::HandleSetStringPref,
152 base::Unretained(this)));
153 web_ui()->RegisterMessageCallback("setURLPref",
154 base::Bind(&CoreOptionsHandler::HandleSetURLPref,
155 base::Unretained(this)));
156 web_ui()->RegisterMessageCallback("setListPref",
157 base::Bind(&CoreOptionsHandler::HandleSetListPref,
158 base::Unretained(this)));
159 web_ui()->RegisterMessageCallback("clearPref",
160 base::Bind(&CoreOptionsHandler::HandleClearPref,
161 base::Unretained(this)));
162 web_ui()->RegisterMessageCallback("coreOptionsUserMetricsAction",
163 base::Bind(&CoreOptionsHandler::HandleUserMetricsAction,
164 base::Unretained(this)));
165 }
166
167 void CoreOptionsHandler::HandleInitialize(const ListValue* args) {
168 DCHECK(handlers_host_);
169 handlers_host_->InitializeHandlers();
170 }
171
172 base::Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) {
173 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
174
175 const PrefService::Preference* pref =
176 pref_service->FindPreference(pref_name.c_str());
177 if (!pref)
178 return base::Value::CreateNullValue();
179
180 return CreateValueForPref(pref, NULL);
181 }
182
183 void CoreOptionsHandler::ObservePref(const std::string& pref_name) {
184 registrar_.Add(pref_name.c_str(), this);
185 }
186
187 void CoreOptionsHandler::SetPref(const std::string& pref_name,
188 const base::Value* value,
189 const std::string& metric) {
190 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
191
192 switch (value->GetType()) {
193 case base::Value::TYPE_BOOLEAN:
194 case base::Value::TYPE_INTEGER:
195 case base::Value::TYPE_DOUBLE:
196 case base::Value::TYPE_STRING:
197 pref_service->Set(pref_name.c_str(), *value);
198 break;
199
200 default:
201 NOTREACHED();
202 return;
203 }
204
205 ProcessUserMetric(value, metric);
206 }
207
208 void CoreOptionsHandler::ClearPref(const std::string& pref_name,
209 const std::string& metric) {
210 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
211 pref_service->ClearPref(pref_name.c_str());
212
213 if (!metric.empty())
214 content::RecordComputedAction(metric);
215 }
216
217 void CoreOptionsHandler::ProcessUserMetric(const base::Value* value,
218 const std::string& metric) {
219 if (metric.empty())
220 return;
221
222 std::string metric_string = metric;
223 if (value->IsType(base::Value::TYPE_BOOLEAN)) {
224 bool bool_value;
225 CHECK(value->GetAsBoolean(&bool_value));
226 metric_string += bool_value ? "_Enable" : "_Disable";
227 }
228
229 content::RecordComputedAction(metric_string);
230 }
231
232 void CoreOptionsHandler::NotifyPrefChanged(
233 const std::string& pref_name,
234 const std::string& controlling_pref_name) {
235 const PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
236 const PrefService::Preference* pref =
237 pref_service->FindPreference(pref_name.c_str());
238 if (!pref)
239 return;
240 const PrefService::Preference* controlling_pref =
241 !controlling_pref_name.empty() ?
242 pref_service->FindPreference(controlling_pref_name.c_str()) : NULL;
243 std::pair<PreferenceCallbackMap::const_iterator,
244 PreferenceCallbackMap::const_iterator> range;
245 range = pref_callback_map_.equal_range(pref_name);
246 for (PreferenceCallbackMap::const_iterator iter = range.first;
247 iter != range.second; ++iter) {
248 const std::wstring& callback_function = iter->second;
249 ListValue result_value;
250 result_value.Append(base::Value::CreateStringValue(pref_name.c_str()));
251 result_value.Append(CreateValueForPref(pref, controlling_pref));
252 web_ui()->CallJavascriptFunction(WideToASCII(callback_function),
253 result_value);
254 }
255 }
256
257 DictionaryValue* CoreOptionsHandler::CreateValueForPref(
258 const PrefService::Preference* pref,
259 const PrefService::Preference* controlling_pref) {
260 DictionaryValue* dict = new DictionaryValue;
261 dict->Set("value", pref->GetValue()->DeepCopy());
262 if (!controlling_pref) // No controlling pref is managing actual pref.
263 controlling_pref = pref; // This means pref is controlling itself.
264 if (controlling_pref->IsManaged()) {
265 dict->SetString("controlledBy", "policy");
266 } else if (controlling_pref->IsExtensionControlled()) {
267 dict->SetString("controlledBy", "extension");
268 } else if (controlling_pref->IsRecommended()) {
269 dict->SetString("controlledBy", "recommended");
270 }
271 dict->SetBoolean("disabled", !controlling_pref->IsUserModifiable());
272 return dict;
273 }
274
275 void CoreOptionsHandler::StopObservingPref(const std::string& path) {
276 registrar_.Remove(path.c_str(), this);
277 }
278
279 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) {
280 // First param is name of callback function, so, there needs to be at least
281 // one more element for the actual preference identifier.
282 DCHECK_GE(static_cast<int>(args->GetSize()), 2);
283
284 // Get callback JS function name.
285 base::Value* callback;
286 if (!args->Get(0, &callback) || !callback->IsType(base::Value::TYPE_STRING))
287 return;
288
289 string16 callback_function;
290 if (!callback->GetAsString(&callback_function))
291 return;
292
293 // Get the list of name for prefs to build the response dictionary.
294 DictionaryValue result_value;
295 base::Value* list_member;
296
297 for (size_t i = 1; i < args->GetSize(); i++) {
298 if (!args->Get(i, &list_member))
299 break;
300
301 if (!list_member->IsType(base::Value::TYPE_STRING))
302 continue;
303
304 std::string pref_name;
305 if (!list_member->GetAsString(&pref_name))
306 continue;
307
308 result_value.Set(pref_name.c_str(), FetchPref(pref_name));
309 }
310 web_ui()->CallJavascriptFunction(UTF16ToASCII(callback_function),
311 result_value);
312 }
313
314 void CoreOptionsHandler::HandleObservePrefs(const ListValue* args) {
315 // First param is name is JS callback function name, the rest are pref
316 // identifiers that we are observing.
317 DCHECK_GE(static_cast<int>(args->GetSize()), 2);
318
319 // Get preference change callback function name.
320 string16 callback_func_name;
321 if (!args->GetString(0, &callback_func_name))
322 return;
323
324 // Get all other parameters - pref identifiers.
325 for (size_t i = 1; i < args->GetSize(); i++) {
326 base::Value* list_member;
327 if (!args->Get(i, &list_member))
328 break;
329
330 // Just ignore bad pref identifiers for now.
331 std::string pref_name;
332 if (!list_member->IsType(base::Value::TYPE_STRING) ||
333 !list_member->GetAsString(&pref_name))
334 continue;
335
336 if (pref_callback_map_.find(pref_name) == pref_callback_map_.end())
337 ObservePref(pref_name);
338
339 pref_callback_map_.insert(
340 PreferenceCallbackMap::value_type(pref_name,
341 UTF16ToWideHack(callback_func_name)));
342 }
343 }
344
345 void CoreOptionsHandler::HandleSetBooleanPref(const ListValue* args) {
346 HandleSetPref(args, TYPE_BOOLEAN);
347 }
348
349 void CoreOptionsHandler::HandleSetIntegerPref(const ListValue* args) {
350 HandleSetPref(args, TYPE_INTEGER);
351 }
352
353 void CoreOptionsHandler::HandleSetDoublePref(const ListValue* args) {
354 HandleSetPref(args, TYPE_DOUBLE);
355 }
356
357 void CoreOptionsHandler::HandleSetStringPref(const ListValue* args) {
358 HandleSetPref(args, TYPE_STRING);
359 }
360
361 void CoreOptionsHandler::HandleSetURLPref(const ListValue* args) {
362 HandleSetPref(args, TYPE_URL);
363 }
364
365 void CoreOptionsHandler::HandleSetListPref(const ListValue* args) {
366 HandleSetPref(args, TYPE_LIST);
367 }
368
369 void CoreOptionsHandler::HandleSetPref(const ListValue* args, PrefType type) {
370 DCHECK_GT(static_cast<int>(args->GetSize()), 1);
371
372 std::string pref_name;
373 if (!args->GetString(0, &pref_name))
374 return;
375
376 base::Value* value;
377 if (!args->Get(1, &value))
378 return;
379
380 scoped_ptr<base::Value> temp_value;
381
382 switch (type) {
383 case TYPE_BOOLEAN:
384 CHECK_EQ(base::Value::TYPE_BOOLEAN, value->GetType());
385 break;
386 case TYPE_INTEGER: {
387 // In JS all numbers are doubles.
388 double double_value;
389 CHECK(value->GetAsDouble(&double_value));
390 int int_value = static_cast<int>(double_value);
391 temp_value.reset(base::Value::CreateIntegerValue(int_value));
392 value = temp_value.get();
393 break;
394 }
395 case TYPE_DOUBLE:
396 CHECK_EQ(base::Value::TYPE_DOUBLE, value->GetType());
397 break;
398 case TYPE_STRING:
399 CHECK_EQ(base::Value::TYPE_STRING, value->GetType());
400 break;
401 case TYPE_URL: {
402 std::string original;
403 CHECK(value->GetAsString(&original));
404 GURL fixed = URLFixerUpper::FixupURL(original, std::string());
405 temp_value.reset(base::Value::CreateStringValue(fixed.spec()));
406 value = temp_value.get();
407 break;
408 }
409 case TYPE_LIST: {
410 // In case we have a List pref we got a JSON string.
411 std::string json_string;
412 CHECK(value->GetAsString(&json_string));
413 temp_value.reset(
414 base::JSONReader().JsonToValue(json_string,
415 false, // no check_root
416 false)); // no trailing comma
417 value = temp_value.get();
418 CHECK_EQ(base::Value::TYPE_LIST, value->GetType());
419 break;
420 }
421 default:
422 NOTREACHED();
423 }
424
425 std::string metric;
426 if (args->GetSize() > 2 && !args->GetString(2, &metric))
427 LOG(WARNING) << "Invalid metric parameter: " << pref_name;
428 SetPref(pref_name, value, metric);
429 }
430
431 void CoreOptionsHandler::HandleClearPref(const ListValue* args) {
432 DCHECK_GT(static_cast<int>(args->GetSize()), 0);
433
434 std::string pref_name;
435 if (!args->GetString(0, &pref_name))
436 return;
437
438 std::string metric;
439 if (args->GetSize() > 1)
440 args->GetString(1, &metric);
441
442 ClearPref(pref_name, metric);
443 }
444
445 void CoreOptionsHandler::HandleUserMetricsAction(const ListValue* args) {
446 std::string metric = UTF16ToUTF8(ExtractStringValue(args));
447 if (!metric.empty())
448 content::RecordComputedAction(metric);
449 }
450
451 void CoreOptionsHandler::UpdateClearPluginLSOData() {
452 scoped_ptr<base::Value> enabled(
453 base::Value::CreateBooleanValue(
454 clear_plugin_lso_data_enabled_.GetValue()));
455 web_ui()->CallJavascriptFunction(
456 "OptionsPage.setClearPluginLSODataEnabled", *enabled);
457 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698