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/browser/prefs/proxy_prefs.h" | |
11 #include "chrome/common/notification_details.h" | 12 #include "chrome/common/notification_details.h" |
12 #include "chrome/common/notification_source.h" | 13 #include "chrome/common/notification_source.h" |
13 #include "chrome/common/notification_type.h" | 14 #include "chrome/common/notification_type.h" |
14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
15 | 16 |
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) | 17 PrefProxyConfigTracker::PrefProxyConfigTracker(PrefService* pref_service) |
44 : pref_service_(pref_service) { | 18 : pref_service_(pref_service) { |
45 valid_ = ReadPrefConfig(&pref_config_); | 19 valid_ = ReadPrefConfig(&pref_config_); |
46 proxy_prefs_observer_.reset( | 20 proxy_prefs_observer_.reset( |
47 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this)); | 21 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this)); |
48 } | 22 } |
49 | 23 |
50 PrefProxyConfigTracker::~PrefProxyConfigTracker() { | 24 PrefProxyConfigTracker::~PrefProxyConfigTracker() { |
51 DCHECK(pref_service_ == NULL); | 25 DCHECK(pref_service_ == NULL); |
52 } | 26 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 FOR_EACH_OBSERVER(Observer, observers_, OnPrefProxyConfigChanged()); | 79 FOR_EACH_OBSERVER(Observer, observers_, OnPrefProxyConfigChanged()); |
106 } | 80 } |
107 } | 81 } |
108 | 82 |
109 bool PrefProxyConfigTracker::ReadPrefConfig(net::ProxyConfig* config) { | 83 bool PrefProxyConfigTracker::ReadPrefConfig(net::ProxyConfig* config) { |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
111 | 85 |
112 // Clear the configuration. | 86 // Clear the configuration. |
113 *config = net::ProxyConfig(); | 87 *config = net::ProxyConfig(); |
114 | 88 |
115 // Scan for all "enable" type proxy switches. | 89 ProxyPrefs::ProxyMode mode; |
116 static const char* proxy_prefs[] = { | 90 int proxy_mode = pref_service_->GetInteger(prefs::kProxyMode); |
117 prefs::kProxyPacUrl, | 91 if (!ProxyPrefs::IntToProxyMode(proxy_mode, &mode)) { |
118 prefs::kProxyServer, | 92 // Fall back to system settings if the mode preference is invalid. |
119 prefs::kProxyBypassList, | |
120 prefs::kProxyAutoDetect | |
121 }; | |
122 | |
123 // Check whether the preference system holds a valid proxy configuration. Note | |
124 // that preferences coming from a lower-priority source than the user settings | |
125 // are ignored. That's because chrome treats the system settings as the | |
126 // default values, which should apply if there's no explicit value forced by | |
127 // policy or the user. | |
128 // Preferences that are set to their default values are also ignored, | |
129 // regardless of their controlling source. This is because 'use system proxy | |
130 // settings' is currently encoded by all the preferences being set to their | |
131 // defaults. This will change when crbug.com/65732 is addressed. | |
132 bool found_enable_proxy_pref = false; | |
133 for (size_t i = 0; i < arraysize(proxy_prefs); i++) { | |
134 const PrefService::Preference* pref = | |
135 pref_service_->FindPreference(proxy_prefs[i]); | |
136 DCHECK(pref); | |
137 if (pref && (!pref->IsUserModifiable() || pref->HasUserSetting()) && | |
138 !IsDefaultValue(pref->GetValue())) { | |
139 found_enable_proxy_pref = true; | |
140 break; | |
141 } | |
142 } | |
143 | |
144 if (!found_enable_proxy_pref && | |
145 !pref_service_->GetBoolean(prefs::kNoProxyServer)) { | |
146 return false; | 93 return false; |
147 } | 94 } |
148 | 95 |
149 if (pref_service_->GetBoolean(prefs::kNoProxyServer)) { | 96 switch (mode) { |
150 // Ignore all the other proxy config preferences if the use of a proxy | 97 case ProxyPrefs::SYSTEM: |
151 // has been explicitly disabled. | 98 // Use system settings. |
152 return true; | 99 return false; |
100 case ProxyPrefs::DISABLED: | |
101 // Ignore all the other proxy config preferences if the use of a proxy | |
102 // has been explicitly disabled. | |
103 return true; | |
104 case ProxyPrefs::AUTO_DETECT: | |
105 config->set_auto_detect(true); | |
106 return true; | |
107 case ProxyPrefs::PAC_SCRIPT: | |
108 if (pref_service_->HasPrefPath(prefs::kProxyPacUrl)) { | |
109 std::string proxy_pac = pref_service_->GetString(prefs::kProxyPacUrl); | |
110 config->set_pac_url(GURL(proxy_pac)); | |
111 return true; | |
112 } else { | |
113 LOG(ERROR) << "Proxy settings request PAC script but do not specify " | |
114 << "its URL. Falling back to direct connection."; | |
115 return true; | |
116 } | |
Mattias Nissler (ping if slow)
2010/12/22 10:22:12
Can't we just do the return true after the conditi
battre
2010/12/22 14:41:16
I added a new condition (pac_url valid) and change
| |
117 case ProxyPrefs::FIXED_SERVERS: | |
118 if (pref_service_->HasPrefPath(prefs::kProxyServer)) { | |
119 std::string proxy_server = | |
120 pref_service_->GetString(prefs::kProxyServer); | |
121 config->proxy_rules().ParseFromString(proxy_server); | |
122 } else { | |
123 LOG(ERROR) << "Proxy settings request fixed proxy servers but do not " | |
124 << "specify their URLs. Falling back to direct connection."; | |
125 return true; | |
126 } | |
127 | |
128 if (pref_service_->HasPrefPath(prefs::kProxyBypassList)) { | |
129 std::string proxy_bypass = | |
130 pref_service_->GetString(prefs::kProxyBypassList); | |
131 config->proxy_rules().bypass_rules.ParseFromString(proxy_bypass); | |
132 } | |
133 return true; | |
134 default: | |
135 NOTREACHED() << "Unknown proxy mode, falling back to system settings."; | |
Mattias Nissler (ping if slow)
2010/12/22 10:22:12
Drop the default branch and move the code after th
battre
2010/12/22 14:41:16
Done.
| |
136 return false; | |
153 } | 137 } |
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 } | 138 } |
175 | 139 |
176 PrefProxyConfigService::PrefProxyConfigService( | 140 PrefProxyConfigService::PrefProxyConfigService( |
177 PrefProxyConfigTracker* tracker, | 141 PrefProxyConfigTracker* tracker, |
178 net::ProxyConfigService* base_service) | 142 net::ProxyConfigService* base_service) |
179 : base_service_(base_service), | 143 : base_service_(base_service), |
180 pref_config_tracker_(tracker), | 144 pref_config_tracker_(tracker), |
181 registered_observers_(false) { | 145 registered_observers_(false) { |
182 } | 146 } |
183 | 147 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 if (!registered_observers_) { | 215 if (!registered_observers_) { |
252 base_service_->AddObserver(this); | 216 base_service_->AddObserver(this); |
253 pref_config_tracker_->AddObserver(this); | 217 pref_config_tracker_->AddObserver(this); |
254 registered_observers_ = true; | 218 registered_observers_ = true; |
255 } | 219 } |
256 } | 220 } |
257 | 221 |
258 // static | 222 // static |
259 void PrefProxyConfigService::RegisterUserPrefs( | 223 void PrefProxyConfigService::RegisterUserPrefs( |
260 PrefService* pref_service) { | 224 PrefService* pref_service) { |
261 pref_service->RegisterBooleanPref(prefs::kNoProxyServer, | 225 pref_service->RegisterIntegerPref(prefs::kProxyMode, ProxyPrefs::SYSTEM); |
262 kProxyPrefDefaultBoolean); | 226 pref_service->RegisterStringPref(prefs::kProxyServer, ""); |
263 pref_service->RegisterBooleanPref(prefs::kProxyAutoDetect, | 227 pref_service->RegisterStringPref(prefs::kProxyPacUrl, ""); |
264 kProxyPrefDefaultBoolean); | 228 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 } | 229 } |
OLD | NEW |