Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(375)

Side by Side Diff: chrome/browser/net/pref_proxy_config_service.cc

Issue 6597070: Allow ProxyConfigService to report "no configuration set" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cosmetics. Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_PRESENT)
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_PRESENT && !pref_config_.Equals(config))) {
77 if (valid_) 79 config_state_ = config_state;
80 if (config_state_ == CONFIG_PRESENT)
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 ProxyConfigDictionary proxy_dict(pref_service_->GetDictionary(prefs::kProxy)); 95 ProxyConfigDictionary proxy_dict(pref_service_->GetDictionary(prefs::kProxy));
90 96
97 if (PrefConfigToNetConfig(proxy_dict, config)) {
98 return (!pref->IsUserModifiable() || pref->HasUserSetting()) ?
99 CONFIG_PRESENT : CONFIG_FALLBACK;
100 }
101
102 return CONFIG_UNSET;
103 }
104
105 bool PrefProxyConfigTracker::PrefConfigToNetConfig(
106 const ProxyConfigDictionary& proxy_dict,
107 net::ProxyConfig* config) {
91 ProxyPrefs::ProxyMode mode; 108 ProxyPrefs::ProxyMode mode;
92 if (!proxy_dict.GetMode(&mode)) { 109 if (!proxy_dict.GetMode(&mode)) {
93 // Fall back to system settings if the mode preference is invalid. 110 // Fall back to system settings if the mode preference is invalid.
94 return false; 111 return false;
95 } 112 }
96 113
97 switch (mode) { 114 switch (mode) {
98 case ProxyPrefs::MODE_SYSTEM: 115 case ProxyPrefs::MODE_SYSTEM:
99 // Use system settings. 116 // Use system settings.
100 return false; 117 return true;
101 case ProxyPrefs::MODE_DIRECT: 118 case ProxyPrefs::MODE_DIRECT:
102 // Ignore all the other proxy config preferences if the use of a proxy 119 // Ignore all the other proxy config preferences if the use of a proxy
103 // has been explicitly disabled. 120 // has been explicitly disabled.
104 return true; 121 return true;
105 case ProxyPrefs::MODE_AUTO_DETECT: 122 case ProxyPrefs::MODE_AUTO_DETECT:
106 config->set_auto_detect(true); 123 config->set_auto_detect(true);
107 return true; 124 return true;
108 case ProxyPrefs::MODE_PAC_SCRIPT: { 125 case ProxyPrefs::MODE_PAC_SCRIPT: {
109 std::string proxy_pac; 126 std::string proxy_pac;
110 if (!proxy_dict.GetPacUrl(&proxy_pac)) { 127 if (!proxy_dict.GetPacUrl(&proxy_pac)) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 net::ProxyConfigService::Observer* observer) { 179 net::ProxyConfigService::Observer* observer) {
163 RegisterObservers(); 180 RegisterObservers();
164 observers_.AddObserver(observer); 181 observers_.AddObserver(observer);
165 } 182 }
166 183
167 void PrefProxyConfigService::RemoveObserver( 184 void PrefProxyConfigService::RemoveObserver(
168 net::ProxyConfigService::Observer* observer) { 185 net::ProxyConfigService::Observer* observer) {
169 observers_.RemoveObserver(observer); 186 observers_.RemoveObserver(observer);
170 } 187 }
171 188
172 bool PrefProxyConfigService::GetLatestProxyConfig(net::ProxyConfig* config) { 189 net::ProxyConfigService::ConfigAvailability
190 PrefProxyConfigService::GetLatestProxyConfig(net::ProxyConfig* config) {
173 RegisterObservers(); 191 RegisterObservers();
174 net::ProxyConfig pref_config; 192 net::ProxyConfig pref_config;
175 if (pref_config_tracker_->GetProxyConfig(&pref_config)) { 193 PrefProxyConfigTracker::ConfigState state =
194 pref_config_tracker_->GetProxyConfig(&pref_config);
195 if (state == PrefProxyConfigTracker::CONFIG_PRESENT) {
176 *config = pref_config; 196 *config = pref_config;
177 return true; 197 return CONFIG_VALID;
178 } 198 }
179 199
180 return base_service_->GetLatestProxyConfig(config); 200 // Ask the base service.
201 ConfigAvailability available = base_service_->GetLatestProxyConfig(config);
202
203 // Apply the fallback, if applicable.
204 if (available == CONFIG_UNSET &&
205 state == PrefProxyConfigTracker::CONFIG_FALLBACK) {
206 *config = pref_config;
207 available = CONFIG_VALID;
208 }
209
210 return available;
181 } 211 }
182 212
183 void PrefProxyConfigService::OnLazyPoll() { 213 void PrefProxyConfigService::OnLazyPoll() {
184 base_service_->OnLazyPoll(); 214 base_service_->OnLazyPoll();
185 } 215 }
186 216
187 void PrefProxyConfigService::OnProxyConfigChanged( 217 void PrefProxyConfigService::OnProxyConfigChanged(
188 const net::ProxyConfig& config) { 218 const net::ProxyConfig& config,
219 ConfigAvailability availability) {
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
190 221
191 // Check whether there is a proxy configuration defined by preferences. In 222 // Check whether there is a proxy configuration defined by preferences. In
192 // this case that proxy configuration takes precedence and the change event 223 // this case that proxy configuration takes precedence and the change event
193 // from the delegate proxy service can be disregarded. 224 // from the delegate proxy service can be disregarded.
194 net::ProxyConfig pref_config; 225 net::ProxyConfig actual_config;
195 if (!pref_config_tracker_->GetProxyConfig(&pref_config)) { 226 if (pref_config_tracker_->GetProxyConfig(&actual_config) !=
227 PrefProxyConfigTracker::CONFIG_PRESENT) {
228 availability = GetLatestProxyConfig(&actual_config);
196 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, 229 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
197 OnProxyConfigChanged(config)); 230 OnProxyConfigChanged(actual_config, availability));
198 } 231 }
199 } 232 }
200 233
201 void PrefProxyConfigService::OnPrefProxyConfigChanged() { 234 void PrefProxyConfigService::OnPrefProxyConfigChanged() {
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
203 236
204 // Evaluate the proxy configuration. If GetLatestProxyConfig returns false, 237 // Evaluate the proxy configuration. If GetLatestProxyConfig returns
205 // we are using the system proxy service, but it doesn't have a valid 238 // CONFIG_PENDING, we are using the system proxy service, but it doesn't have
206 // configuration yet. Once it is ready, OnProxyConfigChanged() will be called 239 // a valid configuration yet. Once it is ready, OnProxyConfigChanged() will be
207 // and broadcast the proxy configuration. 240 // called and broadcast the proxy configuration.
208 // Note: If a switch between a preference proxy configuration and the system 241 // Note: If a switch between a preference proxy configuration and the system
209 // proxy configuration occurs an unnecessary notification might get send if 242 // proxy configuration occurs an unnecessary notification might get send if
210 // the two configurations agree. This case should be rare however, so we don't 243 // the two configurations agree. This case should be rare however, so we don't
211 // handle that case specially. 244 // handle that case specially.
212 net::ProxyConfig new_config; 245 net::ProxyConfig new_config;
213 if (GetLatestProxyConfig(&new_config)) { 246 ConfigAvailability availability = GetLatestProxyConfig(&new_config);
247 if (availability != CONFIG_PENDING) {
214 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, 248 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
215 OnProxyConfigChanged(new_config)); 249 OnProxyConfigChanged(new_config, availability));
216 } 250 }
217 } 251 }
218 252
219 void PrefProxyConfigService::RegisterObservers() { 253 void PrefProxyConfigService::RegisterObservers() {
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
221 if (!registered_observers_) { 255 if (!registered_observers_) {
222 base_service_->AddObserver(this); 256 base_service_->AddObserver(this);
223 pref_config_tracker_->AddObserver(this); 257 pref_config_tracker_->AddObserver(this);
224 registered_observers_ = true; 258 registered_observers_ = true;
225 } 259 }
226 } 260 }
227 261
228 // static 262 // static
229 void PrefProxyConfigService::RegisterPrefs(PrefService* pref_service) { 263 void PrefProxyConfigService::RegisterPrefs(PrefService* pref_service) {
230 DictionaryValue* default_settings = ProxyConfigDictionary::CreateSystem(); 264 DictionaryValue* default_settings = ProxyConfigDictionary::CreateSystem();
231 pref_service->RegisterDictionaryPref(prefs::kProxy, default_settings); 265 pref_service->RegisterDictionaryPref(prefs::kProxy, default_settings);
232 } 266 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698