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