| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/ui/webui/options/core_options_handler.h" | 5 #include "chrome/browser/ui/webui/options/core_options_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/string16.h" | 11 #include "base/string16.h" |
| 12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/google/google_util.h" | 16 #include "chrome/browser/google/google_util.h" |
| 17 #include "chrome/browser/net/url_fixer_upper.h" | 17 #include "chrome/browser/net/url_fixer_upper.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
| 21 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 22 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 23 #include "content/browser/user_metrics.h" | 22 #include "content/browser/user_metrics.h" |
| 24 #include "content/public/browser/notification_details.h" | 23 #include "content/public/browser/notification_details.h" |
| 25 #include "content/public/browser/notification_types.h" | 24 #include "content/public/browser/notification_types.h" |
| 26 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
| 27 #include "grit/chromium_strings.h" | 26 #include "grit/chromium_strings.h" |
| 28 #include "grit/generated_resources.h" | 27 #include "grit/generated_resources.h" |
| 29 #include "grit/locale_settings.h" | 28 #include "grit/locale_settings.h" |
| 30 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 32 | 31 |
| 33 namespace { | |
| 34 | |
| 35 DictionaryValue* CreateValueForPref(const PrefService* pref_service, | |
| 36 const PrefService::Preference* pref) { | |
| 37 DictionaryValue* dict = new DictionaryValue; | |
| 38 dict->Set("value", pref->GetValue()->DeepCopy()); | |
| 39 const PrefService::Preference* controlling_pref = pref; | |
| 40 #if defined(OS_CHROMEOS) | |
| 41 // For use-shared-proxies pref, the proxy pref determines if the former is | |
| 42 // modifiable or managed by policy/extension. | |
| 43 if (pref->name() == prefs::kUseSharedProxies) { | |
| 44 controlling_pref = pref_service->FindPreference(prefs::kProxy); | |
| 45 if (!controlling_pref) | |
| 46 return dict; | |
| 47 } | |
| 48 #endif // defined(OS_CHROMEOS) | |
| 49 if (controlling_pref->IsManaged()) { | |
| 50 dict->SetString("controlledBy", "policy"); | |
| 51 } else if (controlling_pref->IsExtensionControlled()) { | |
| 52 dict->SetString("controlledBy", "extension"); | |
| 53 } | |
| 54 dict->SetBoolean("disabled", !controlling_pref->IsUserModifiable()); | |
| 55 return dict; | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 CoreOptionsHandler::CoreOptionsHandler() | 32 CoreOptionsHandler::CoreOptionsHandler() |
| 61 : handlers_host_(NULL) { | 33 : handlers_host_(NULL) { |
| 62 } | 34 } |
| 63 | 35 |
| 64 CoreOptionsHandler::~CoreOptionsHandler() {} | 36 CoreOptionsHandler::~CoreOptionsHandler() {} |
| 65 | 37 |
| 66 void CoreOptionsHandler::Initialize() { | 38 void CoreOptionsHandler::Initialize() { |
| 67 clear_plugin_lso_data_enabled_.Init(prefs::kClearPluginLSODataEnabled, | 39 clear_plugin_lso_data_enabled_.Init(prefs::kClearPluginLSODataEnabled, |
| 68 Profile::FromWebUI(web_ui_), | 40 Profile::FromWebUI(web_ui_), |
| 69 this); | 41 this); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 WebUIMessageHandler* CoreOptionsHandler::Attach(WebUI* web_ui) { | 103 WebUIMessageHandler* CoreOptionsHandler::Attach(WebUI* web_ui) { |
| 132 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); | 104 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); |
| 133 DCHECK(web_ui_); | 105 DCHECK(web_ui_); |
| 134 registrar_.Init(Profile::FromWebUI(web_ui_)->GetPrefs()); | 106 registrar_.Init(Profile::FromWebUI(web_ui_)->GetPrefs()); |
| 135 return result; | 107 return result; |
| 136 } | 108 } |
| 137 | 109 |
| 138 void CoreOptionsHandler::Observe(int type, | 110 void CoreOptionsHandler::Observe(int type, |
| 139 const content::NotificationSource& source, | 111 const content::NotificationSource& source, |
| 140 const content::NotificationDetails& details) { | 112 const content::NotificationDetails& details) { |
| 141 if (type == chrome::NOTIFICATION_PREF_CHANGED) | 113 if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
| 142 NotifyPrefChanged(content::Details<std::string>(details).ptr()); | 114 std::string* pref_name = content::Details<std::string>(details).ptr(); |
| 115 if (*pref_name == prefs::kClearPluginLSODataEnabled) { |
| 116 // This preference is stored in Local State, not in the user preferences. |
| 117 UpdateClearPluginLSOData(); |
| 118 return; |
| 119 } |
| 120 NotifyPrefChanged(*pref_name, std::string()); |
| 121 } |
| 143 } | 122 } |
| 144 | 123 |
| 145 void CoreOptionsHandler::RegisterMessages() { | 124 void CoreOptionsHandler::RegisterMessages() { |
| 146 web_ui_->RegisterMessageCallback("coreOptionsInitialize", | 125 web_ui_->RegisterMessageCallback("coreOptionsInitialize", |
| 147 base::Bind(&CoreOptionsHandler::HandleInitialize, | 126 base::Bind(&CoreOptionsHandler::HandleInitialize, |
| 148 base::Unretained(this))); | 127 base::Unretained(this))); |
| 149 web_ui_->RegisterMessageCallback("fetchPrefs", | 128 web_ui_->RegisterMessageCallback("fetchPrefs", |
| 150 base::Bind(&CoreOptionsHandler::HandleFetchPrefs, | 129 base::Bind(&CoreOptionsHandler::HandleFetchPrefs, |
| 151 base::Unretained(this))); | 130 base::Unretained(this))); |
| 152 web_ui_->RegisterMessageCallback("observePrefs", | 131 web_ui_->RegisterMessageCallback("observePrefs", |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 } | 163 } |
| 185 | 164 |
| 186 Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) { | 165 Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) { |
| 187 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); | 166 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); |
| 188 | 167 |
| 189 const PrefService::Preference* pref = | 168 const PrefService::Preference* pref = |
| 190 pref_service->FindPreference(pref_name.c_str()); | 169 pref_service->FindPreference(pref_name.c_str()); |
| 191 if (!pref) | 170 if (!pref) |
| 192 return Value::CreateNullValue(); | 171 return Value::CreateNullValue(); |
| 193 | 172 |
| 194 return CreateValueForPref(pref_service, pref); | 173 return CreateValueForPref(pref, NULL); |
| 195 } | 174 } |
| 196 | 175 |
| 197 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { | 176 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { |
| 198 registrar_.Add(pref_name.c_str(), this); | 177 registrar_.Add(pref_name.c_str(), this); |
| 199 } | 178 } |
| 200 | 179 |
| 201 void CoreOptionsHandler::SetPref(const std::string& pref_name, | 180 void CoreOptionsHandler::SetPref(const std::string& pref_name, |
| 202 const Value* value, | 181 const Value* value, |
| 203 const std::string& metric) { | 182 const std::string& metric) { |
| 204 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); | 183 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 std::string metric_string = metric; | 217 std::string metric_string = metric; |
| 239 if (value->IsType(Value::TYPE_BOOLEAN)) { | 218 if (value->IsType(Value::TYPE_BOOLEAN)) { |
| 240 bool bool_value; | 219 bool bool_value; |
| 241 CHECK(value->GetAsBoolean(&bool_value)); | 220 CHECK(value->GetAsBoolean(&bool_value)); |
| 242 metric_string += bool_value ? "_Enable" : "_Disable"; | 221 metric_string += bool_value ? "_Enable" : "_Disable"; |
| 243 } | 222 } |
| 244 | 223 |
| 245 UserMetrics::RecordComputedAction(metric_string); | 224 UserMetrics::RecordComputedAction(metric_string); |
| 246 } | 225 } |
| 247 | 226 |
| 227 void CoreOptionsHandler::NotifyPrefChanged( |
| 228 const std::string& pref_name, |
| 229 const std::string& controlling_pref_name) { |
| 230 const PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); |
| 231 const PrefService::Preference* pref = |
| 232 pref_service->FindPreference(pref_name.c_str()); |
| 233 if (!pref) |
| 234 return; |
| 235 const PrefService::Preference* controlling_pref = |
| 236 !controlling_pref_name.empty() ? |
| 237 pref_service->FindPreference(controlling_pref_name.c_str()) : NULL; |
| 238 std::pair<PreferenceCallbackMap::const_iterator, |
| 239 PreferenceCallbackMap::const_iterator> range; |
| 240 range = pref_callback_map_.equal_range(pref_name); |
| 241 for (PreferenceCallbackMap::const_iterator iter = range.first; |
| 242 iter != range.second; ++iter) { |
| 243 const std::wstring& callback_function = iter->second; |
| 244 ListValue result_value; |
| 245 result_value.Append(Value::CreateStringValue(pref_name.c_str())); |
| 246 result_value.Append(CreateValueForPref(pref, controlling_pref)); |
| 247 web_ui_->CallJavascriptFunction(WideToASCII(callback_function), |
| 248 result_value); |
| 249 } |
| 250 } |
| 251 |
| 252 DictionaryValue* CoreOptionsHandler::CreateValueForPref( |
| 253 const PrefService::Preference* pref, |
| 254 const PrefService::Preference* controlling_pref) { |
| 255 DictionaryValue* dict = new DictionaryValue; |
| 256 dict->Set("value", pref->GetValue()->DeepCopy()); |
| 257 if (!controlling_pref) // No controlling pref is managing actual pref. |
| 258 controlling_pref = pref; // This means pref is controlling itself. |
| 259 if (controlling_pref->IsManaged()) { |
| 260 dict->SetString("controlledBy", "policy"); |
| 261 } else if (controlling_pref->IsExtensionControlled()) { |
| 262 dict->SetString("controlledBy", "extension"); |
| 263 } |
| 264 dict->SetBoolean("disabled", !controlling_pref->IsUserModifiable()); |
| 265 return dict; |
| 266 } |
| 267 |
| 248 void CoreOptionsHandler::StopObservingPref(const std::string& path) { | 268 void CoreOptionsHandler::StopObservingPref(const std::string& path) { |
| 249 registrar_.Remove(path.c_str(), this); | 269 registrar_.Remove(path.c_str(), this); |
| 250 } | 270 } |
| 251 | 271 |
| 252 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { | 272 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { |
| 253 // First param is name of callback function, so, there needs to be at least | 273 // First param is name of callback function, so, there needs to be at least |
| 254 // one more element for the actual preference identifier. | 274 // one more element for the actual preference identifier. |
| 255 DCHECK_GE(static_cast<int>(args->GetSize()), 2); | 275 DCHECK_GE(static_cast<int>(args->GetSize()), 2); |
| 256 | 276 |
| 257 // Get callback JS function name. | 277 // Get callback JS function name. |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 if (!metric.empty()) | 440 if (!metric.empty()) |
| 421 UserMetrics::RecordComputedAction(metric); | 441 UserMetrics::RecordComputedAction(metric); |
| 422 } | 442 } |
| 423 | 443 |
| 424 void CoreOptionsHandler::UpdateClearPluginLSOData() { | 444 void CoreOptionsHandler::UpdateClearPluginLSOData() { |
| 425 scoped_ptr<Value> enabled( | 445 scoped_ptr<Value> enabled( |
| 426 Value::CreateBooleanValue(clear_plugin_lso_data_enabled_.GetValue())); | 446 Value::CreateBooleanValue(clear_plugin_lso_data_enabled_.GetValue())); |
| 427 web_ui_->CallJavascriptFunction( | 447 web_ui_->CallJavascriptFunction( |
| 428 "OptionsPage.setClearPluginLSODataEnabled", *enabled); | 448 "OptionsPage.setClearPluginLSODataEnabled", *enabled); |
| 429 } | 449 } |
| 430 | |
| 431 void CoreOptionsHandler::NotifyPrefChanged(const std::string* pref_name) { | |
| 432 if (*pref_name == prefs::kClearPluginLSODataEnabled) { | |
| 433 // This preference is stored in Local State, not in the user preferences. | |
| 434 UpdateClearPluginLSOData(); | |
| 435 return; | |
| 436 } | |
| 437 | |
| 438 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); | |
| 439 const PrefService::Preference* pref = | |
| 440 pref_service->FindPreference(pref_name->c_str()); | |
| 441 if (!pref) | |
| 442 return; | |
| 443 | |
| 444 std::pair<PreferenceCallbackMap::const_iterator, | |
| 445 PreferenceCallbackMap::const_iterator> range; | |
| 446 range = pref_callback_map_.equal_range(*pref_name); | |
| 447 for (PreferenceCallbackMap::const_iterator iter = range.first; | |
| 448 iter != range.second; ++iter) { | |
| 449 const std::wstring& callback_function = iter->second; | |
| 450 ListValue result_value; | |
| 451 result_value.Append(Value::CreateStringValue(pref_name->c_str())); | |
| 452 | |
| 453 result_value.Append(CreateValueForPref(pref_service, pref)); | |
| 454 | |
| 455 web_ui_->CallJavascriptFunction(WideToASCII(callback_function), | |
| 456 result_value); | |
| 457 } | |
| 458 } | |
| OLD | NEW |