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::MODE_SYSTEM: |
151 // has been explicitly disabled. | 98 // Use system settings. |
152 return true; | 99 return false; |
| 100 case ProxyPrefs::MODE_DIRECT: |
| 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::MODE_AUTO_DETECT: |
| 105 config->set_auto_detect(true); |
| 106 return true; |
| 107 case ProxyPrefs::MODE_PAC_SCRIPT: { |
| 108 if (!pref_service_->HasPrefPath(prefs::kProxyPacUrl)) { |
| 109 LOG(ERROR) << "Proxy settings request PAC script but do not specify " |
| 110 << "its URL. Falling back to direct connection."; |
| 111 return true; |
| 112 } |
| 113 std::string proxy_pac = pref_service_->GetString(prefs::kProxyPacUrl); |
| 114 GURL proxy_pac_url(proxy_pac); |
| 115 if (!proxy_pac_url.is_valid()) { |
| 116 LOG(ERROR) << "Invalid proxy PAC url: " << proxy_pac; |
| 117 return true; |
| 118 } |
| 119 config->set_pac_url(proxy_pac_url); |
| 120 return true; |
| 121 } |
| 122 case ProxyPrefs::MODE_FIXED_SERVERS: { |
| 123 if (!pref_service_->HasPrefPath(prefs::kProxyServer)) { |
| 124 LOG(ERROR) << "Proxy settings request fixed proxy servers but do not " |
| 125 << "specify their URLs. Falling back to direct connection."; |
| 126 return true; |
| 127 } |
| 128 std::string proxy_server = |
| 129 pref_service_->GetString(prefs::kProxyServer); |
| 130 config->proxy_rules().ParseFromString(proxy_server); |
| 131 |
| 132 if (pref_service_->HasPrefPath(prefs::kProxyBypassList)) { |
| 133 std::string proxy_bypass = |
| 134 pref_service_->GetString(prefs::kProxyBypassList); |
| 135 config->proxy_rules().bypass_rules.ParseFromString(proxy_bypass); |
| 136 } |
| 137 return true; |
| 138 } |
| 139 case ProxyPrefs::kModeCount: { |
| 140 // Fall through to NOTREACHED(). |
| 141 } |
153 } | 142 } |
154 | 143 NOTREACHED() << "Unknown proxy mode, falling back to system settings."; |
155 if (pref_service_->HasPrefPath(prefs::kProxyServer)) { | 144 return false; |
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 } | 145 } |
175 | 146 |
176 PrefProxyConfigService::PrefProxyConfigService( | 147 PrefProxyConfigService::PrefProxyConfigService( |
177 PrefProxyConfigTracker* tracker, | 148 PrefProxyConfigTracker* tracker, |
178 net::ProxyConfigService* base_service) | 149 net::ProxyConfigService* base_service) |
179 : base_service_(base_service), | 150 : base_service_(base_service), |
180 pref_config_tracker_(tracker), | 151 pref_config_tracker_(tracker), |
181 registered_observers_(false) { | 152 registered_observers_(false) { |
182 } | 153 } |
183 | 154 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 if (!registered_observers_) { | 222 if (!registered_observers_) { |
252 base_service_->AddObserver(this); | 223 base_service_->AddObserver(this); |
253 pref_config_tracker_->AddObserver(this); | 224 pref_config_tracker_->AddObserver(this); |
254 registered_observers_ = true; | 225 registered_observers_ = true; |
255 } | 226 } |
256 } | 227 } |
257 | 228 |
258 // static | 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::kProxyMode, ProxyPrefs::MODE_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 |