Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/net/pref_proxy_config_service.h" | 5 #include "chrome/browser/net/pref_proxy_config_service.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/browser_thread.h" | 8 #include "chrome/browser/browser_thread.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
| 10 #include "chrome/browser/prefs/pref_set_observer.h" | 10 #include "chrome/browser/prefs/pref_set_observer.h" |
| 11 #include "chrome/common/notification_details.h" | 11 #include "chrome/common/notification_details.h" |
| 12 #include "chrome/common/notification_source.h" | 12 #include "chrome/common/notification_source.h" |
| 13 #include "chrome/common/notification_type.h" | 13 #include "chrome/common/notification_type.h" |
| 14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 | 15 |
| 16 namespace { | |
| 17 | |
| 18 const bool kProxyPrefDefaultBoolean = false; | |
| 19 const char kProxyPrefDefaultString[] = ""; | |
| 20 | |
| 21 // Determines if a value of a proxy pref is set to its default. Default values | |
| 22 // have a special role in the proxy pref system, because if all of the proxy | |
| 23 // prefs are set to their defaults, then the system proxy settings are applied. | |
| 24 // TODO(gfeher): Proxy preferences should be refactored to avoid the need | |
| 25 // for such solutions. See crbug.com/65732 | |
| 26 bool IsDefaultValue(const Value* value) { | |
| 27 bool b = false; | |
| 28 std::string s; | |
| 29 if (value->IsType(Value::TYPE_BOOLEAN) && | |
| 30 value->GetAsBoolean(&b)) { | |
| 31 return b == kProxyPrefDefaultBoolean; | |
| 32 } else if (value->IsType(Value::TYPE_STRING) && | |
| 33 value->GetAsString(&s)) { | |
| 34 return s == kProxyPrefDefaultString; | |
| 35 } else { | |
| 36 NOTREACHED() << "Invalid type for a proxy preference."; | |
| 37 return false; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 PrefProxyConfigTracker::PrefProxyConfigTracker(PrefService* pref_service) | 16 PrefProxyConfigTracker::PrefProxyConfigTracker(PrefService* pref_service) |
| 44 : pref_service_(pref_service) { | 17 : pref_service_(pref_service) { |
| 45 valid_ = ReadPrefConfig(&pref_config_); | 18 valid_ = ReadPrefConfig(&pref_config_); |
| 46 proxy_prefs_observer_.reset( | 19 proxy_prefs_observer_.reset( |
| 47 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this)); | 20 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this)); |
| 48 } | 21 } |
| 49 | 22 |
| 50 PrefProxyConfigTracker::~PrefProxyConfigTracker() { | 23 PrefProxyConfigTracker::~PrefProxyConfigTracker() { |
| 51 DCHECK(pref_service_ == NULL); | 24 DCHECK(pref_service_ == NULL); |
| 52 } | 25 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 FOR_EACH_OBSERVER(Observer, observers_, OnPrefProxyConfigChanged()); | 78 FOR_EACH_OBSERVER(Observer, observers_, OnPrefProxyConfigChanged()); |
| 106 } | 79 } |
| 107 } | 80 } |
| 108 | 81 |
| 109 bool PrefProxyConfigTracker::ReadPrefConfig(net::ProxyConfig* config) { | 82 bool PrefProxyConfigTracker::ReadPrefConfig(net::ProxyConfig* config) { |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 111 | 84 |
| 112 // Clear the configuration. | 85 // Clear the configuration. |
| 113 *config = net::ProxyConfig(); | 86 *config = net::ProxyConfig(); |
| 114 | 87 |
| 115 // Scan for all "enable" type proxy switches. | 88 PrefProxyConfigService::ProxyServerMode mode = |
| 116 static const char* proxy_prefs[] = { | 89 static_cast<PrefProxyConfigService::ProxyServerMode>( |
| 117 prefs::kProxyPacUrl, | 90 pref_service_->GetInteger(prefs::kProxyServerMode)); |
|
battre (please use the other)
2010/12/14 09:45:44
Use IntToMode and check return value?
gfeher
2010/12/16 10:42:04
Done.
| |
| 118 prefs::kProxyServer, | 91 switch (mode) { |
| 119 prefs::kProxyBypassList, | 92 case PrefProxyConfigService::SYSTEM: |
| 120 prefs::kProxyAutoDetect | 93 // Use system settings. |
| 121 }; | 94 return false; |
| 122 | 95 |
| 123 // Check whether the preference system holds a valid proxy configuration. Note | 96 case PrefProxyConfigService::DISABLED: |
| 124 // that preferences coming from a lower-priority source than the user settings | 97 // Ignore all the other proxy config preferences if the use of a proxy |
| 125 // are ignored. That's because chrome treats the system settings as the | 98 // has been explicitly disabled. |
| 126 // default values, which should apply if there's no explicit value forced by | 99 return true; |
| 127 // policy or the user. | 100 |
| 128 // Preferences that are set to their default values are also ignored, | 101 default: |
| 129 // regardless of their controlling source. This is because 'use system proxy | 102 if (pref_service_->HasPrefPath(prefs::kProxyServer)) { |
| 130 // settings' is currently encoded by all the preferences being set to their | 103 std::string proxy_server = |
| 131 // defaults. This will change when crbug.com/65732 is addressed. | 104 pref_service_->GetString(prefs::kProxyServer); |
| 132 bool found_enable_proxy_pref = false; | 105 config->proxy_rules().ParseFromString(proxy_server); |
| 133 for (size_t i = 0; i < arraysize(proxy_prefs); i++) { | 106 } |
| 134 const PrefService::Preference* pref = | 107 |
| 135 pref_service_->FindPreference(proxy_prefs[i]); | 108 if (pref_service_->HasPrefPath(prefs::kProxyPacUrl)) { |
| 136 DCHECK(pref); | 109 std::string proxy_pac = pref_service_->GetString(prefs::kProxyPacUrl); |
| 137 if (pref && (!pref->IsUserModifiable() || pref->HasUserSetting()) && | 110 config->set_pac_url(GURL(proxy_pac)); |
| 138 !IsDefaultValue(pref->GetValue())) { | 111 } |
| 139 found_enable_proxy_pref = true; | 112 |
| 140 break; | 113 config->set_auto_detect(mode == PrefProxyConfigService::AUTO_DETECT); |
| 141 } | 114 |
| 115 if (pref_service_->HasPrefPath(prefs::kProxyBypassList)) { | |
| 116 std::string proxy_bypass = | |
| 117 pref_service_->GetString(prefs::kProxyBypassList); | |
| 118 config->proxy_rules().bypass_rules.ParseFromString(proxy_bypass); | |
| 119 } | |
| 120 | |
| 121 return true; | |
| 142 } | 122 } |
| 143 | |
| 144 if (!found_enable_proxy_pref && | |
| 145 !pref_service_->GetBoolean(prefs::kNoProxyServer)) { | |
| 146 return false; | |
| 147 } | |
| 148 | |
| 149 if (pref_service_->GetBoolean(prefs::kNoProxyServer)) { | |
| 150 // Ignore all the other proxy config preferences if the use of a proxy | |
| 151 // has been explicitly disabled. | |
| 152 return true; | |
| 153 } | |
| 154 | |
| 155 if (pref_service_->HasPrefPath(prefs::kProxyServer)) { | |
| 156 std::string proxy_server = pref_service_->GetString(prefs::kProxyServer); | |
| 157 config->proxy_rules().ParseFromString(proxy_server); | |
| 158 } | |
| 159 | |
| 160 if (pref_service_->HasPrefPath(prefs::kProxyPacUrl)) { | |
| 161 std::string proxy_pac = pref_service_->GetString(prefs::kProxyPacUrl); | |
| 162 config->set_pac_url(GURL(proxy_pac)); | |
| 163 } | |
| 164 | |
| 165 config->set_auto_detect(pref_service_->GetBoolean(prefs::kProxyAutoDetect)); | |
| 166 | |
| 167 if (pref_service_->HasPrefPath(prefs::kProxyBypassList)) { | |
| 168 std::string proxy_bypass = | |
| 169 pref_service_->GetString(prefs::kProxyBypassList); | |
| 170 config->proxy_rules().bypass_rules.ParseFromString(proxy_bypass); | |
| 171 } | |
| 172 | |
| 173 return true; | |
| 174 } | 123 } |
| 175 | 124 |
| 176 PrefProxyConfigService::PrefProxyConfigService( | 125 PrefProxyConfigService::PrefProxyConfigService( |
| 177 PrefProxyConfigTracker* tracker, | 126 PrefProxyConfigTracker* tracker, |
| 178 net::ProxyConfigService* base_service) | 127 net::ProxyConfigService* base_service) |
| 179 : base_service_(base_service), | 128 : base_service_(base_service), |
| 180 pref_config_tracker_(tracker), | 129 pref_config_tracker_(tracker), |
| 181 registered_observers_(false) { | 130 registered_observers_(false) { |
| 182 } | 131 } |
| 183 | 132 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 void PrefProxyConfigService::RegisterObservers() { | 198 void PrefProxyConfigService::RegisterObservers() { |
| 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 251 if (!registered_observers_) { | 200 if (!registered_observers_) { |
| 252 base_service_->AddObserver(this); | 201 base_service_->AddObserver(this); |
| 253 pref_config_tracker_->AddObserver(this); | 202 pref_config_tracker_->AddObserver(this); |
| 254 registered_observers_ = true; | 203 registered_observers_ = true; |
| 255 } | 204 } |
| 256 } | 205 } |
| 257 | 206 |
| 258 // static | 207 // static |
| 208 bool PrefProxyConfigService::IntToMode( | |
| 209 int in_value, | |
| 210 PrefProxyConfigService::ProxyServerMode* out_value) { | |
| 211 DCHECK(out_value); | |
| 212 if (in_value == PrefProxyConfigService::DISABLED) { | |
| 213 *out_value = PrefProxyConfigService::DISABLED; | |
| 214 return true; | |
| 215 } else if (in_value == PrefProxyConfigService::SYSTEM) { | |
| 216 *out_value = PrefProxyConfigService::SYSTEM; | |
| 217 return true; | |
| 218 } else if (in_value == PrefProxyConfigService::AUTO_DETECT) { | |
| 219 *out_value = PrefProxyConfigService::AUTO_DETECT; | |
| 220 return true; | |
| 221 } else if (in_value == PrefProxyConfigService::MANUAL) { | |
| 222 *out_value = PrefProxyConfigService::MANUAL; | |
| 223 return true; | |
| 224 } else { | |
| 225 return false; | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 // static | |
| 259 void PrefProxyConfigService::RegisterUserPrefs( | 230 void PrefProxyConfigService::RegisterUserPrefs( |
| 260 PrefService* pref_service) { | 231 PrefService* pref_service) { |
| 261 pref_service->RegisterBooleanPref(prefs::kNoProxyServer, | 232 pref_service->RegisterIntegerPref(prefs::kProxyServerMode, SYSTEM); |
| 262 kProxyPrefDefaultBoolean); | 233 pref_service->RegisterStringPref(prefs::kProxyServer, ""); |
| 263 pref_service->RegisterBooleanPref(prefs::kProxyAutoDetect, | 234 pref_service->RegisterStringPref(prefs::kProxyPacUrl, ""); |
| 264 kProxyPrefDefaultBoolean); | 235 pref_service->RegisterStringPref(prefs::kProxyBypassList, ""); |
| 265 pref_service->RegisterStringPref(prefs::kProxyServer, | |
| 266 kProxyPrefDefaultString); | |
| 267 pref_service->RegisterStringPref(prefs::kProxyPacUrl, | |
| 268 kProxyPrefDefaultString); | |
| 269 pref_service->RegisterStringPref(prefs::kProxyBypassList, | |
| 270 kProxyPrefDefaultString); | |
| 271 } | 236 } |
| OLD | NEW |