Chromium Code Reviews| Index: chrome/browser/ui/webui/options/chromeos/core_chromeos_options_handler.cc |
| diff --git a/chrome/browser/ui/webui/options/chromeos/core_chromeos_options_handler.cc b/chrome/browser/ui/webui/options/chromeos/core_chromeos_options_handler.cc |
| index 831a5b122bbaa98ec27ad0bb551daf88df943a2d..6150c865102ed9ea36c5a59a72c9b37543fea36a 100644 |
| --- a/chrome/browser/ui/webui/options/chromeos/core_chromeos_options_handler.cc |
| +++ b/chrome/browser/ui/webui/options/chromeos/core_chromeos_options_handler.cc |
| @@ -17,8 +17,37 @@ |
| #include "content/public/browser/notification_details.h" |
| #include "content/public/browser/notification_source.h" |
| +#include "chrome/browser/chromeos/proxy_config_service_impl.h" |
| +#include "chrome/browser/chromeos/proxy_cros_settings_parser.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "content/public/browser/notification_service.h" |
| + |
| namespace chromeos { |
| +namespace { |
| + |
| +const char kProxyPrefsPrefix[] = "cros.session.proxy"; |
| + |
| +const char* const kProxySettings[] = { |
| + kProxyPacUrl, |
| + kProxySingleHttp, |
| + kProxySingleHttpPort, |
| + kProxyHttpUrl, |
| + kProxyHttpPort, |
| + kProxyHttpsUrl, |
| + kProxyHttpsPort, |
| + kProxyType, |
| + kProxySingle, |
| + kProxyFtpUrl, |
| + kProxyFtpPort, |
| + kProxySocks, |
| + kProxySocksPort, |
| + kProxyIgnoreList, |
| +}; |
| + |
| +} |
| + |
| CoreChromeOSOptionsHandler::CoreChromeOSOptionsHandler() |
| : handling_change_(false) { |
| } |
| @@ -26,11 +55,22 @@ CoreChromeOSOptionsHandler::CoreChromeOSOptionsHandler() |
| CoreChromeOSOptionsHandler::~CoreChromeOSOptionsHandler() {} |
| void CoreChromeOSOptionsHandler::Initialize() { |
| + proxy_registrar_clients_ = 0; |
| proxy_prefs_.reset(PrefSetObserver::CreateProxyPrefSetObserver( |
| Profile::FromWebUI(web_ui_)->GetPrefs(), this)); |
| } |
| -Value* CoreChromeOSOptionsHandler::FetchPref(const std::string& pref_name) { |
| +base::Value* CoreChromeOSOptionsHandler::FetchPref( |
| + const std::string& pref_name) { |
| + if (pref_name.find(kProxyPrefsPrefix) == 0) { |
|
Mattias Nissler (ping if slow)
2011/11/08 09:25:21
Should be StartsWith()
|
| + base::Value *value = NULL; |
| + ProxyCrosSettingsParser::GetProxyPrefValue(Profile::FromWebUI(web_ui_), |
| + pref_name, &value); |
| + if (!value) |
| + return base::Value::CreateNullValue(); |
| + |
| + return value; |
| + } |
| if (!CrosSettings::IsCrosSettings(pref_name)) { |
| // Specially handle kUseSharedProxies because kProxy controls it to |
| // determine if it's managed by policy/extension. |
| @@ -39,7 +79,7 @@ Value* CoreChromeOSOptionsHandler::FetchPref(const std::string& pref_name) { |
| const PrefService::Preference* pref = |
| pref_service->FindPreference(prefs::kUseSharedProxies); |
| if (!pref) |
| - return Value::CreateNullValue(); |
| + return base::Value::CreateNullValue(); |
| const PrefService::Preference* controlling_pref = |
| pref_service->FindPreference(prefs::kProxy); |
| return CreateValueForPref(pref, controlling_pref); |
| @@ -47,27 +87,41 @@ Value* CoreChromeOSOptionsHandler::FetchPref(const std::string& pref_name) { |
| return ::CoreOptionsHandler::FetchPref(pref_name); |
| } |
| - Value* pref_value = NULL; |
| + base::Value* pref_value = NULL; |
| CrosSettings::Get()->Get(pref_name, &pref_value); |
| - return pref_value ? pref_value : Value::CreateNullValue(); |
| + return pref_value ? pref_value : base::Value::CreateNullValue(); |
| } |
| void CoreChromeOSOptionsHandler::ObservePref(const std::string& pref_name) { |
| + if (pref_name.find(kProxyPrefsPrefix) == 0) { |
| + // We already listen for all events that concern proxies. |
| + if (proxy_registrar_.IsEmpty()) { |
| + proxy_registrar_clients_++; |
| + proxy_registrar_.Add(this, chrome::NOTIFICATION_CURRENT_NETWORK_CHANGED, |
| + content::NotificationService::AllSources()); |
| + } |
| + return; |
| + } |
| if (!CrosSettings::IsCrosSettings(pref_name)) |
| return ::CoreOptionsHandler::ObservePref(pref_name); |
| - |
| // TODO(xiyuan): Change this when CrosSettings supports observers. |
| CrosSettings::Get()->AddSettingsObserver(pref_name.c_str(), this); |
| } |
| void CoreChromeOSOptionsHandler::SetPref(const std::string& pref_name, |
| - const Value* value, |
| + const base::Value* value, |
| const std::string& metric) { |
| + if (pref_name.find(kProxyPrefsPrefix) == 0) { |
| + ProxyCrosSettingsParser::SetProxyPrefValue(Profile::FromWebUI(web_ui_), |
| + pref_name, value); |
| + ProcessUserMetric(value, metric); |
| + return; |
| + } |
| if (!CrosSettings::IsCrosSettings(pref_name)) |
| return ::CoreOptionsHandler::SetPref(pref_name, value, metric); |
| handling_change_ = true; |
| // CrosSettings takes ownership of its value so we need to copy it. |
| - Value* pref_value = value->DeepCopy(); |
| + base::Value* pref_value = value->DeepCopy(); |
| CrosSettings::Get()->Set(pref_name, pref_value); |
| handling_change_ = false; |
| @@ -76,10 +130,19 @@ void CoreChromeOSOptionsHandler::SetPref(const std::string& pref_name, |
| void CoreChromeOSOptionsHandler::StopObservingPref(const std::string& path) { |
| // Unregister this instance from observing prefs of chrome os settings. |
| - if (CrosSettings::IsCrosSettings(path)) |
| + if (CrosSettings::IsCrosSettings(path)) { |
| CrosSettings::Get()->RemoveSettingsObserver(path.c_str(), this); |
| - else // Call base class to handle regular preferences. |
| + } else if (path.find(kProxyPrefsPrefix) == 0) { |
| + // We listen for all events that concern proxies. |
| + if (--proxy_registrar_clients_ <= 0) { |
| + proxy_registrar_clients_ = 0; // Safeguard the value to not go below 0. |
| + proxy_registrar_.RemoveAll(); |
| + } |
| + return; |
| + } else { |
| + // Call base class to handle regular preferences. |
| ::CoreOptionsHandler::StopObservingPref(path); |
| + } |
| } |
| void CoreChromeOSOptionsHandler::Observe( |
| @@ -93,6 +156,10 @@ void CoreChromeOSOptionsHandler::Observe( |
| NotifySettingsChanged(content::Details<std::string>(details).ptr()); |
| return; |
| } |
| + if (type == chrome::NOTIFICATION_CURRENT_NETWORK_CHANGED) { |
| + NotifyNetworkChanged(); |
| + return; |
| + } |
| // Special handling for preferences kUseSharedProxies and kProxy, the latter |
| // controls the former and decides if it's managed by policy/extension. |
| if (type == chrome::NOTIFICATION_PREF_CHANGED) { |
| @@ -112,7 +179,7 @@ void CoreChromeOSOptionsHandler::NotifySettingsChanged( |
| const std::string* setting_name) { |
| DCHECK(web_ui_); |
| DCHECK(CrosSettings::Get()->IsCrosSettings(*setting_name)); |
| - Value* value = NULL; |
| + base::Value* value = NULL; |
| if (!CrosSettings::Get()->Get(*setting_name, &value)) { |
| NOTREACHED(); |
| if (value) |
| @@ -124,7 +191,7 @@ void CoreChromeOSOptionsHandler::NotifySettingsChanged( |
| iter != pref_callback_map_.end(); ++iter) { |
| const std::wstring& callback_function = iter->second; |
| ListValue result_value; |
| - result_value.Append(Value::CreateStringValue(setting_name->c_str())); |
| + result_value.Append(base::Value::CreateStringValue(setting_name->c_str())); |
| result_value.Append(value->DeepCopy()); |
| web_ui_->CallJavascriptFunction(WideToASCII(callback_function), |
| result_value); |
| @@ -133,4 +200,26 @@ void CoreChromeOSOptionsHandler::NotifySettingsChanged( |
| delete value; |
| } |
| +void CoreChromeOSOptionsHandler::NotifyNetworkChanged() { |
| + DCHECK(web_ui_); |
| + for (size_t i = 0; i < arraysize(kProxySettings); ++i) { |
| + base::Value* value = NULL; |
| + ProxyCrosSettingsParser::GetProxyPrefValue( |
| + Profile::FromWebUI(web_ui_),kProxySettings[i], &value); |
| + DCHECK(value); |
| + for (PreferenceCallbackMap::const_iterator iter = |
| + pref_callback_map_.find(kProxySettings[i]); |
| + iter != pref_callback_map_.end(); ++iter) { |
| + const std::wstring& callback_function = iter->second; |
| + ListValue result_value; |
| + result_value.Append(base::Value::CreateStringValue(kProxySettings[i])); |
| + result_value.Append(value->DeepCopy()); |
| + web_ui_->CallJavascriptFunction(WideToASCII(callback_function), |
| + result_value); |
| + } |
| + if (value) |
| + delete value; |
| + } |
| +} |
| + |
| } // namespace chromeos |