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/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/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
9 #include "chrome/browser/prefs/pref_set_observer.h" | 9 #include "chrome/browser/prefs/pref_set_observer.h" |
10 #include "chrome/browser/prefs/proxy_config_dictionary.h" | 10 #include "chrome/browser/prefs/proxy_config_dictionary.h" |
11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
12 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
13 #include "content/common/notification_details.h" | 13 #include "content/common/notification_details.h" |
14 #include "content/common/notification_source.h" | 14 #include "content/common/notification_source.h" |
15 #include "content/common/notification_type.h" | 15 #include "content/common/notification_type.h" |
16 | 16 |
17 PrefProxyConfigTracker::PrefProxyConfigTracker(PrefService* pref_service) | 17 PrefProxyConfigTracker::PrefProxyConfigTracker(PrefService* pref_service) |
18 : pref_service_(pref_service) { | 18 : pref_service_(pref_service) { |
19 valid_ = ReadPrefConfig(&pref_config_); | 19 config_state_ = ReadPrefConfig(&pref_config_); |
20 proxy_prefs_observer_.reset( | 20 proxy_prefs_observer_.reset( |
21 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this)); | 21 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this)); |
22 } | 22 } |
23 | 23 |
24 PrefProxyConfigTracker::~PrefProxyConfigTracker() { | 24 PrefProxyConfigTracker::~PrefProxyConfigTracker() { |
25 DCHECK(pref_service_ == NULL); | 25 DCHECK(pref_service_ == NULL); |
26 } | 26 } |
27 | 27 |
28 bool PrefProxyConfigTracker::GetProxyConfig(net::ProxyConfig* config) { | 28 PrefProxyConfigTracker::ConfigState |
| 29 PrefProxyConfigTracker::GetProxyConfig(net::ProxyConfig* config) { |
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
30 if (valid_) | 31 if (config_state_ != CONFIG_UNSET) |
31 *config = pref_config_; | 32 *config = pref_config_; |
32 return valid_; | 33 return config_state_; |
33 } | 34 } |
34 | 35 |
35 void PrefProxyConfigTracker::DetachFromPrefService() { | 36 void PrefProxyConfigTracker::DetachFromPrefService() { |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
37 // Stop notifications. | 38 // Stop notifications. |
38 proxy_prefs_observer_.reset(); | 39 proxy_prefs_observer_.reset(); |
39 pref_service_ = NULL; | 40 pref_service_ = NULL; |
40 } | 41 } |
41 | 42 |
42 void PrefProxyConfigTracker::AddObserver( | 43 void PrefProxyConfigTracker::AddObserver( |
43 PrefProxyConfigTracker::Observer* observer) { | 44 PrefProxyConfigTracker::Observer* observer) { |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
45 observers_.AddObserver(observer); | 46 observers_.AddObserver(observer); |
46 } | 47 } |
47 | 48 |
48 void PrefProxyConfigTracker::RemoveObserver( | 49 void PrefProxyConfigTracker::RemoveObserver( |
49 PrefProxyConfigTracker::Observer* observer) { | 50 PrefProxyConfigTracker::Observer* observer) { |
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
51 observers_.RemoveObserver(observer); | 52 observers_.RemoveObserver(observer); |
52 } | 53 } |
53 | 54 |
54 void PrefProxyConfigTracker::Observe(NotificationType type, | 55 void PrefProxyConfigTracker::Observe(NotificationType type, |
55 const NotificationSource& source, | 56 const NotificationSource& source, |
56 const NotificationDetails& details) { | 57 const NotificationDetails& details) { |
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
58 if (type == NotificationType::PREF_CHANGED && | 59 if (type == NotificationType::PREF_CHANGED && |
59 Source<PrefService>(source).ptr() == pref_service_) { | 60 Source<PrefService>(source).ptr() == pref_service_) { |
60 net::ProxyConfig new_config; | 61 net::ProxyConfig new_config; |
61 bool valid = ReadPrefConfig(&new_config); | 62 ConfigState config_state = ReadPrefConfig(&new_config); |
62 BrowserThread::PostTask( | 63 BrowserThread::PostTask( |
63 BrowserThread::IO, FROM_HERE, | 64 BrowserThread::IO, FROM_HERE, |
64 NewRunnableMethod(this, | 65 NewRunnableMethod(this, |
65 &PrefProxyConfigTracker::InstallProxyConfig, | 66 &PrefProxyConfigTracker::InstallProxyConfig, |
66 new_config, valid)); | 67 new_config, config_state)); |
67 } else { | 68 } else { |
68 NOTREACHED() << "Unexpected notification of type " << type.value; | 69 NOTREACHED() << "Unexpected notification of type " << type.value; |
69 } | 70 } |
70 } | 71 } |
71 | 72 |
72 void PrefProxyConfigTracker::InstallProxyConfig(const net::ProxyConfig& config, | 73 void PrefProxyConfigTracker::InstallProxyConfig( |
73 bool valid) { | 74 const net::ProxyConfig& config, |
| 75 PrefProxyConfigTracker::ConfigState config_state) { |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
75 if (valid_ != valid || (valid && !pref_config_.Equals(config))) { | 77 if (config_state_ != config_state || |
76 valid_ = valid; | 78 (config_state_ != CONFIG_UNSET && !pref_config_.Equals(config))) { |
77 if (valid_) | 79 config_state_ = config_state; |
| 80 if (config_state_ != CONFIG_UNSET) |
78 pref_config_ = config; | 81 pref_config_ = config; |
79 FOR_EACH_OBSERVER(Observer, observers_, OnPrefProxyConfigChanged()); | 82 FOR_EACH_OBSERVER(Observer, observers_, OnPrefProxyConfigChanged()); |
80 } | 83 } |
81 } | 84 } |
82 | 85 |
83 bool PrefProxyConfigTracker::ReadPrefConfig(net::ProxyConfig* config) { | 86 PrefProxyConfigTracker::ConfigState |
| 87 PrefProxyConfigTracker::ReadPrefConfig(net::ProxyConfig* config) { |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
85 | 89 |
86 // Clear the configuration. | 90 // Clear the configuration. |
87 *config = net::ProxyConfig(); | 91 *config = net::ProxyConfig(); |
88 | 92 |
| 93 const PrefService::Preference* pref = |
| 94 pref_service_->FindPreference(prefs::kProxy); |
89 const DictionaryValue* dict = pref_service_->GetDictionary(prefs::kProxy); | 95 const DictionaryValue* dict = pref_service_->GetDictionary(prefs::kProxy); |
90 DCHECK(dict); | 96 DCHECK(dict); |
91 ProxyConfigDictionary proxy_dict(dict); | 97 ProxyConfigDictionary proxy_dict(dict); |
92 | 98 |
| 99 if (PrefConfigToNetConfig(proxy_dict, config)) { |
| 100 return (!pref->IsUserModifiable() || pref->HasUserSetting()) ? |
| 101 CONFIG_PRESENT : CONFIG_FALLBACK; |
| 102 } |
| 103 |
| 104 return CONFIG_UNSET; |
| 105 } |
| 106 |
| 107 bool PrefProxyConfigTracker::PrefConfigToNetConfig( |
| 108 const ProxyConfigDictionary& proxy_dict, |
| 109 net::ProxyConfig* config) { |
93 ProxyPrefs::ProxyMode mode; | 110 ProxyPrefs::ProxyMode mode; |
94 if (!proxy_dict.GetMode(&mode)) { | 111 if (!proxy_dict.GetMode(&mode)) { |
95 // Fall back to system settings if the mode preference is invalid. | 112 // Fall back to system settings if the mode preference is invalid. |
96 return false; | 113 return false; |
97 } | 114 } |
98 | 115 |
99 switch (mode) { | 116 switch (mode) { |
100 case ProxyPrefs::MODE_SYSTEM: | 117 case ProxyPrefs::MODE_SYSTEM: |
101 // Use system settings. | 118 // Use system settings. |
102 return false; | 119 return true; |
103 case ProxyPrefs::MODE_DIRECT: | 120 case ProxyPrefs::MODE_DIRECT: |
104 // Ignore all the other proxy config preferences if the use of a proxy | 121 // Ignore all the other proxy config preferences if the use of a proxy |
105 // has been explicitly disabled. | 122 // has been explicitly disabled. |
106 return true; | 123 return true; |
107 case ProxyPrefs::MODE_AUTO_DETECT: | 124 case ProxyPrefs::MODE_AUTO_DETECT: |
108 config->set_auto_detect(true); | 125 config->set_auto_detect(true); |
109 return true; | 126 return true; |
110 case ProxyPrefs::MODE_PAC_SCRIPT: { | 127 case ProxyPrefs::MODE_PAC_SCRIPT: { |
111 std::string proxy_pac; | 128 std::string proxy_pac; |
112 if (!proxy_dict.GetPacUrl(&proxy_pac)) { | 129 if (!proxy_dict.GetPacUrl(&proxy_pac)) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 net::ProxyConfigService::Observer* observer) { | 181 net::ProxyConfigService::Observer* observer) { |
165 RegisterObservers(); | 182 RegisterObservers(); |
166 observers_.AddObserver(observer); | 183 observers_.AddObserver(observer); |
167 } | 184 } |
168 | 185 |
169 void PrefProxyConfigService::RemoveObserver( | 186 void PrefProxyConfigService::RemoveObserver( |
170 net::ProxyConfigService::Observer* observer) { | 187 net::ProxyConfigService::Observer* observer) { |
171 observers_.RemoveObserver(observer); | 188 observers_.RemoveObserver(observer); |
172 } | 189 } |
173 | 190 |
174 bool PrefProxyConfigService::GetLatestProxyConfig(net::ProxyConfig* config) { | 191 net::ProxyConfigService::ConfigAvailability |
| 192 PrefProxyConfigService::GetLatestProxyConfig(net::ProxyConfig* config) { |
175 RegisterObservers(); | 193 RegisterObservers(); |
176 net::ProxyConfig pref_config; | 194 net::ProxyConfig pref_config; |
177 if (pref_config_tracker_->GetProxyConfig(&pref_config)) { | 195 PrefProxyConfigTracker::ConfigState state = |
| 196 pref_config_tracker_->GetProxyConfig(&pref_config); |
| 197 if (state == PrefProxyConfigTracker::CONFIG_PRESENT) { |
178 *config = pref_config; | 198 *config = pref_config; |
179 return true; | 199 return CONFIG_VALID; |
180 } | 200 } |
181 | 201 |
182 return base_service_->GetLatestProxyConfig(config); | 202 // Ask the base service. |
| 203 ConfigAvailability available = base_service_->GetLatestProxyConfig(config); |
| 204 |
| 205 // Base service doesn't have a configuration, fall back to prefs or default. |
| 206 if (available == CONFIG_UNSET) { |
| 207 if (state == PrefProxyConfigTracker::CONFIG_FALLBACK) |
| 208 *config = pref_config; |
| 209 else |
| 210 *config = net::ProxyConfig::CreateDirect(); |
| 211 return CONFIG_VALID; |
| 212 } |
| 213 |
| 214 return available; |
183 } | 215 } |
184 | 216 |
185 void PrefProxyConfigService::OnLazyPoll() { | 217 void PrefProxyConfigService::OnLazyPoll() { |
186 base_service_->OnLazyPoll(); | 218 base_service_->OnLazyPoll(); |
187 } | 219 } |
188 | 220 |
189 void PrefProxyConfigService::OnProxyConfigChanged( | 221 void PrefProxyConfigService::OnProxyConfigChanged( |
190 const net::ProxyConfig& config) { | 222 const net::ProxyConfig& config, |
| 223 ConfigAvailability availability) { |
191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
192 | 225 |
193 // Check whether there is a proxy configuration defined by preferences. In | 226 // Check whether there is a proxy configuration defined by preferences. In |
194 // this case that proxy configuration takes precedence and the change event | 227 // this case that proxy configuration takes precedence and the change event |
195 // from the delegate proxy service can be disregarded. | 228 // from the delegate proxy service can be disregarded. |
196 net::ProxyConfig pref_config; | 229 net::ProxyConfig actual_config; |
197 if (!pref_config_tracker_->GetProxyConfig(&pref_config)) { | 230 if (pref_config_tracker_->GetProxyConfig(&actual_config) != |
| 231 PrefProxyConfigTracker::CONFIG_PRESENT) { |
| 232 availability = GetLatestProxyConfig(&actual_config); |
198 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, | 233 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, |
199 OnProxyConfigChanged(config)); | 234 OnProxyConfigChanged(actual_config, availability)); |
200 } | 235 } |
201 } | 236 } |
202 | 237 |
203 void PrefProxyConfigService::OnPrefProxyConfigChanged() { | 238 void PrefProxyConfigService::OnPrefProxyConfigChanged() { |
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
205 | 240 |
206 // Evaluate the proxy configuration. If GetLatestProxyConfig returns false, | 241 // Evaluate the proxy configuration. If GetLatestProxyConfig returns |
207 // we are using the system proxy service, but it doesn't have a valid | 242 // CONFIG_PENDING, we are using the system proxy service, but it doesn't have |
208 // configuration yet. Once it is ready, OnProxyConfigChanged() will be called | 243 // a valid configuration yet. Once it is ready, OnProxyConfigChanged() will be |
209 // and broadcast the proxy configuration. | 244 // called and broadcast the proxy configuration. |
210 // Note: If a switch between a preference proxy configuration and the system | 245 // Note: If a switch between a preference proxy configuration and the system |
211 // proxy configuration occurs an unnecessary notification might get send if | 246 // proxy configuration occurs an unnecessary notification might get send if |
212 // the two configurations agree. This case should be rare however, so we don't | 247 // the two configurations agree. This case should be rare however, so we don't |
213 // handle that case specially. | 248 // handle that case specially. |
214 net::ProxyConfig new_config; | 249 net::ProxyConfig new_config; |
215 if (GetLatestProxyConfig(&new_config)) { | 250 ConfigAvailability availability = GetLatestProxyConfig(&new_config); |
| 251 if (availability != CONFIG_PENDING) { |
216 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, | 252 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, |
217 OnProxyConfigChanged(new_config)); | 253 OnProxyConfigChanged(new_config, availability)); |
218 } | 254 } |
219 } | 255 } |
220 | 256 |
221 void PrefProxyConfigService::RegisterObservers() { | 257 void PrefProxyConfigService::RegisterObservers() { |
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
223 if (!registered_observers_) { | 259 if (!registered_observers_) { |
224 base_service_->AddObserver(this); | 260 base_service_->AddObserver(this); |
225 pref_config_tracker_->AddObserver(this); | 261 pref_config_tracker_->AddObserver(this); |
226 registered_observers_ = true; | 262 registered_observers_ = true; |
227 } | 263 } |
228 } | 264 } |
229 | 265 |
230 // static | 266 // static |
231 void PrefProxyConfigService::RegisterPrefs(PrefService* pref_service) { | 267 void PrefProxyConfigService::RegisterPrefs(PrefService* pref_service) { |
232 DictionaryValue* default_settings = ProxyConfigDictionary::CreateSystem(); | 268 DictionaryValue* default_settings = ProxyConfigDictionary::CreateSystem(); |
233 pref_service->RegisterDictionaryPref(prefs::kProxy, default_settings); | 269 pref_service->RegisterDictionaryPref(prefs::kProxy, default_settings); |
234 } | 270 } |
OLD | NEW |