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(); |
205 | 184 |
206 switch (value->GetType()) { | 185 switch (value->GetType()) { |
207 case Value::TYPE_BOOLEAN: | 186 case Value::TYPE_BOOLEAN: |
208 case Value::TYPE_INTEGER: | 187 case Value::TYPE_INTEGER: |
209 case Value::TYPE_DOUBLE: | 188 case Value::TYPE_DOUBLE: |
210 case Value::TYPE_STRING: | 189 case Value::TYPE_STRING: |
211 pref_service->Set(pref_name.c_str(), *value); | 190 pref_service->Set(pref_name.c_str(), *value); |
212 break; | 191 break; |
213 | 192 |
214 default: | 193 default: |
215 NOTREACHED(); | 194 NOTREACHED(); |
216 return; | 195 return; |
217 } | 196 } |
218 | 197 |
219 pref_service->ScheduleSavePersistentPrefs(); | 198 pref_service->ScheduleSavePersistentPrefs(); |
| 199 |
220 ProcessUserMetric(value, metric); | 200 ProcessUserMetric(value, metric); |
221 } | 201 } |
222 | 202 |
223 void CoreOptionsHandler::ClearPref(const std::string& pref_name, | 203 void CoreOptionsHandler::ClearPref(const std::string& pref_name, |
224 const std::string& metric) { | 204 const std::string& metric) { |
225 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); | 205 PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); |
226 pref_service->ClearPref(pref_name.c_str()); | 206 pref_service->ClearPref(pref_name.c_str()); |
227 pref_service->ScheduleSavePersistentPrefs(); | 207 pref_service->ScheduleSavePersistentPrefs(); |
228 | 208 |
229 if (!metric.empty()) | 209 if (!metric.empty()) |
230 UserMetrics::RecordComputedAction(metric); | 210 UserMetrics::RecordComputedAction(metric); |
231 } | 211 } |
232 | 212 |
233 void CoreOptionsHandler::ProcessUserMetric(const Value* value, | 213 void CoreOptionsHandler::ProcessUserMetric(const Value* value, |
234 const std::string& metric) { | 214 const std::string& metric) { |
235 if (metric.empty()) | 215 if (metric.empty()) |
236 return; | 216 return; |
237 | 217 |
238 std::string metric_string = metric; | 218 std::string metric_string = metric; |
239 if (value->IsType(Value::TYPE_BOOLEAN)) { | 219 if (value->IsType(Value::TYPE_BOOLEAN)) { |
240 bool bool_value; | 220 bool bool_value; |
241 CHECK(value->GetAsBoolean(&bool_value)); | 221 CHECK(value->GetAsBoolean(&bool_value)); |
242 metric_string += bool_value ? "_Enable" : "_Disable"; | 222 metric_string += bool_value ? "_Enable" : "_Disable"; |
243 } | 223 } |
244 | 224 |
245 UserMetrics::RecordComputedAction(metric_string); | 225 UserMetrics::RecordComputedAction(metric_string); |
246 } | 226 } |
247 | 227 |
| 228 void CoreOptionsHandler::NotifyPrefChanged( |
| 229 const std::string& pref_name, |
| 230 const std::string& controlling_pref_name) { |
| 231 const PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); |
| 232 const PrefService::Preference* pref = |
| 233 pref_service->FindPreference(pref_name.c_str()); |
| 234 if (!pref) |
| 235 return; |
| 236 const PrefService::Preference* controlling_pref = |
| 237 !controlling_pref_name.empty() ? |
| 238 pref_service->FindPreference(controlling_pref_name.c_str()) : NULL; |
| 239 std::pair<PreferenceCallbackMap::const_iterator, |
| 240 PreferenceCallbackMap::const_iterator> range; |
| 241 range = pref_callback_map_.equal_range(pref_name); |
| 242 for (PreferenceCallbackMap::const_iterator iter = range.first; |
| 243 iter != range.second; ++iter) { |
| 244 const std::wstring& callback_function = iter->second; |
| 245 ListValue result_value; |
| 246 result_value.Append(Value::CreateStringValue(pref_name.c_str())); |
| 247 result_value.Append(CreateValueForPref(pref, controlling_pref)); |
| 248 web_ui_->CallJavascriptFunction(WideToASCII(callback_function), |
| 249 result_value); |
| 250 } |
| 251 } |
| 252 |
| 253 DictionaryValue* CoreOptionsHandler::CreateValueForPref( |
| 254 const PrefService::Preference* pref, |
| 255 const PrefService::Preference* controlling_pref) { |
| 256 DictionaryValue* dict = new DictionaryValue; |
| 257 dict->Set("value", pref->GetValue()->DeepCopy()); |
| 258 if (!controlling_pref) // No controlling pref is managing actual pref. |
| 259 controlling_pref = pref; // This means pref is controlling itself. |
| 260 if (controlling_pref->IsManaged()) { |
| 261 dict->SetString("controlledBy", "policy"); |
| 262 } else if (controlling_pref->IsExtensionControlled()) { |
| 263 dict->SetString("controlledBy", "extension"); |
| 264 } |
| 265 dict->SetBoolean("disabled", !controlling_pref->IsUserModifiable()); |
| 266 return dict; |
| 267 } |
| 268 |
248 void CoreOptionsHandler::StopObservingPref(const std::string& path) { | 269 void CoreOptionsHandler::StopObservingPref(const std::string& path) { |
249 registrar_.Remove(path.c_str(), this); | 270 registrar_.Remove(path.c_str(), this); |
250 } | 271 } |
251 | 272 |
252 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { | 273 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { |
253 // First param is name of callback function, so, there needs to be at least | 274 // First param is name of callback function, so, there needs to be at least |
254 // one more element for the actual preference identifier. | 275 // one more element for the actual preference identifier. |
255 DCHECK_GE(static_cast<int>(args->GetSize()), 2); | 276 DCHECK_GE(static_cast<int>(args->GetSize()), 2); |
256 | 277 |
257 // Get callback JS function name. | 278 // Get callback JS function name. |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 if (!metric.empty()) | 441 if (!metric.empty()) |
421 UserMetrics::RecordComputedAction(metric); | 442 UserMetrics::RecordComputedAction(metric); |
422 } | 443 } |
423 | 444 |
424 void CoreOptionsHandler::UpdateClearPluginLSOData() { | 445 void CoreOptionsHandler::UpdateClearPluginLSOData() { |
425 scoped_ptr<Value> enabled( | 446 scoped_ptr<Value> enabled( |
426 Value::CreateBooleanValue(clear_plugin_lso_data_enabled_.GetValue())); | 447 Value::CreateBooleanValue(clear_plugin_lso_data_enabled_.GetValue())); |
427 web_ui_->CallJavascriptFunction( | 448 web_ui_->CallJavascriptFunction( |
428 "OptionsPage.setClearPluginLSODataEnabled", *enabled); | 449 "OptionsPage.setClearPluginLSODataEnabled", *enabled); |
429 } | 450 } |
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 |