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/common/notification_details.h" | 23 #include "content/common/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 NotificationSource& source, | 111 const NotificationSource& source, |
140 const NotificationDetails& details) { | 112 const NotificationDetails& details) { |
141 if (type == chrome::NOTIFICATION_PREF_CHANGED) | 113 if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
142 NotifyPrefChanged(Details<std::string>(details).ptr()); | 114 std::string* pref_name = 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(const std::string& pref_name, | |
228 const std::string& controlling_pref_name) { | |
Mattias Nissler (ping if slow)
2011/10/19 19:27:36
parameters should be aligned
kuan
2011/10/20 00:41:11
Done.
| |
229 const PrefService* pref_service = Profile::FromWebUI(web_ui_)->GetPrefs(); | |
230 const PrefService::Preference* pref = | |
231 pref_service->FindPreference(pref_name.c_str()); | |
232 if (!pref) | |
233 return; | |
234 const PrefService::Preference* controlling_pref = | |
235 !controlling_pref_name.empty() ? | |
236 pref_service->FindPreference(controlling_pref_name.c_str()) : NULL; | |
237 std::pair<PreferenceCallbackMap::const_iterator, | |
238 PreferenceCallbackMap::const_iterator> range; | |
239 range = pref_callback_map_.equal_range(pref_name); | |
240 for (PreferenceCallbackMap::const_iterator iter = range.first; | |
241 iter != range.second; ++iter) { | |
242 const std::wstring& callback_function = iter->second; | |
243 ListValue result_value; | |
244 result_value.Append(Value::CreateStringValue(pref_name.c_str())); | |
245 result_value.Append(CreateValueForPref(pref, controlling_pref)); | |
246 web_ui_->CallJavascriptFunction(WideToASCII(callback_function), | |
247 result_value); | |
248 } | |
249 } | |
250 | |
251 DictionaryValue* CoreOptionsHandler::CreateValueForPref( | |
252 const PrefService::Preference* pref, | |
253 const PrefService::Preference* controlling_pref) { | |
254 DictionaryValue* dict = new DictionaryValue; | |
255 dict->Set("value", pref->GetValue()->DeepCopy()); | |
256 if (!controlling_pref) // No controlling pref is managing actual pref. | |
257 controlling_pref = pref; // This means pref is controlling itself. | |
258 if (controlling_pref->IsManaged()) { | |
259 dict->SetString("controlledBy", "policy"); | |
260 } else if (controlling_pref->IsExtensionControlled()) { | |
261 dict->SetString("controlledBy", "extension"); | |
262 } | |
263 dict->SetBoolean("disabled", !controlling_pref->IsUserModifiable()); | |
264 return dict; | |
265 } | |
266 | |
248 void CoreOptionsHandler::StopObservingPref(const std::string& path) { | 267 void CoreOptionsHandler::StopObservingPref(const std::string& path) { |
249 registrar_.Remove(path.c_str(), this); | 268 registrar_.Remove(path.c_str(), this); |
250 } | 269 } |
251 | 270 |
252 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { | 271 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { |
253 // First param is name of callback function, so, there needs to be at least | 272 // First param is name of callback function, so, there needs to be at least |
254 // one more element for the actual preference identifier. | 273 // one more element for the actual preference identifier. |
255 DCHECK_GE(static_cast<int>(args->GetSize()), 2); | 274 DCHECK_GE(static_cast<int>(args->GetSize()), 2); |
256 | 275 |
257 // Get callback JS function name. | 276 // Get callback JS function name. |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 if (!metric.empty()) | 439 if (!metric.empty()) |
421 UserMetrics::RecordComputedAction(metric); | 440 UserMetrics::RecordComputedAction(metric); |
422 } | 441 } |
423 | 442 |
424 void CoreOptionsHandler::UpdateClearPluginLSOData() { | 443 void CoreOptionsHandler::UpdateClearPluginLSOData() { |
425 scoped_ptr<Value> enabled( | 444 scoped_ptr<Value> enabled( |
426 Value::CreateBooleanValue(clear_plugin_lso_data_enabled_.GetValue())); | 445 Value::CreateBooleanValue(clear_plugin_lso_data_enabled_.GetValue())); |
427 web_ui_->CallJavascriptFunction( | 446 web_ui_->CallJavascriptFunction( |
428 "OptionsPage.setClearPluginLSODataEnabled", *enabled); | 447 "OptionsPage.setClearPluginLSODataEnabled", *enabled); |
429 } | 448 } |
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 |