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

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

Issue 8102019: redesign and reimplement proxy config service and tracker, revise proxy ui on cros (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 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_tracker.h"
6 6
7 #include "base/bind.h"
7 #include "base/values.h" 8 #include "base/values.h"
8 #include "chrome/browser/prefs/pref_service.h" 9 #include "chrome/browser/prefs/pref_service.h"
9 #include "chrome/browser/prefs/pref_set_observer.h" 10 #include "chrome/browser/prefs/pref_set_observer.h"
10 #include "chrome/browser/prefs/proxy_config_dictionary.h" 11 #include "chrome/browser/prefs/proxy_config_dictionary.h"
11 #include "chrome/common/chrome_notification_types.h" 12 #include "chrome/common/chrome_notification_types.h"
12 #include "chrome/common/pref_names.h" 13 #include "chrome/common/pref_names.h"
13 #include "content/browser/browser_thread.h" 14 #include "content/browser/browser_thread.h"
14 #include "content/common/notification_details.h" 15 #include "content/common/notification_details.h"
15 #include "content/common/notification_source.h" 16 #include "content/common/notification_source.h"
16 17
17 PrefProxyConfigTracker::PrefProxyConfigTracker(PrefService* pref_service) 18 //============================= ChromeProxyConfigService =======================
18 : pref_service_(pref_service) { 19
20 ChromeProxyConfigService::ChromeProxyConfigService(
21 net::ProxyConfigService* base_service)
22 : base_service_(base_service),
23 pref_config_state_(ProxyPrefs::CONFIG_UNSET),
24 registered_observer_(false) {
25 }
26
27 ChromeProxyConfigService::~ChromeProxyConfigService() {
28 if (registered_observer_ && base_service_.get())
29 base_service_->RemoveObserver(this);
30 }
31
32 void ChromeProxyConfigService::AddObserver(
33 net::ProxyConfigService::Observer* observer) {
34 RegisterObserver();
35 observers_.AddObserver(observer);
36 }
37
38 void ChromeProxyConfigService::RemoveObserver(
39 net::ProxyConfigService::Observer* observer) {
40 observers_.RemoveObserver(observer);
41 }
42
43 net::ProxyConfigService::ConfigAvailability
44 ChromeProxyConfigService::GetLatestProxyConfig(net::ProxyConfig* config) {
45 RegisterObserver();
46
47 // Ask the base service if available.
48 net::ProxyConfig system_config;
49 ConfigAvailability system_availability =
50 net::ProxyConfigService::CONFIG_UNSET;
51 if (base_service_.get())
52 system_availability = base_service_->GetLatestProxyConfig(&system_config);
53
54 ProxyPrefs::ConfigState config_state;
55 return PrefProxyConfigTracker::GetEffectiveProxyConfig(
56 pref_config_state_, pref_config_,
57 system_availability, system_config, false,
58 &config_state, config);
59 }
60
61 void ChromeProxyConfigService::OnLazyPoll() {
62 if (base_service_.get())
63 base_service_->OnLazyPoll();
64 }
65
66 void ChromeProxyConfigService::UpdateProxyConfig(
67 ProxyPrefs::ConfigState config_state,
68 const net::ProxyConfig& config) {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
70 VLOG(1) << "got UpdateProxyConfig";
71
72 pref_config_state_ = config_state;
73 pref_config_ = config;
74
75 if (!observers_.size())
76 return;
77
78 // Evaluate the proxy configuration. If GetLatestProxyConfig returns
79 // CONFIG_PENDING, we are using the system proxy service, but it doesn't have
80 // a valid configuration yet. Once it is ready, OnProxyConfigChanged() will be
81 // called and broadcast the proxy configuration.
82 // Note: If a switch between a preference proxy configuration and the system
83 // proxy configuration occurs an unnecessary notification might get send if
84 // the two configurations agree. This case should be rare however, so we don't
85 // handle that case specially.
86 net::ProxyConfig new_config;
87 ConfigAvailability availability = GetLatestProxyConfig(&new_config);
88 if (availability != CONFIG_PENDING) {
89 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
90 OnProxyConfigChanged(new_config, availability));
91 }
92 }
93
94 void ChromeProxyConfigService::OnProxyConfigChanged(
95 const net::ProxyConfig& config,
96 ConfigAvailability availability) {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
98
99 // Check whether there is a proxy configuration defined by preferences. In
100 // this case that proxy configuration takes precedence and the change event
101 // from the delegate proxy service can be disregarded.
102 if (!PrefProxyConfigTracker::PrefPrecedes(pref_config_state_)) {
103 net::ProxyConfig actual_config;
104 availability = GetLatestProxyConfig(&actual_config);
105 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
106 OnProxyConfigChanged(actual_config, availability));
107 }
108 }
109
110 void ChromeProxyConfigService::RegisterObserver() {
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
112 if (!registered_observer_ && base_service_.get()) {
113 base_service_->AddObserver(this);
114 registered_observer_ = true;
115 }
116 }
117
118 //=========================== PrefProxyConfigTracker ===========================
119
120 PrefProxyConfigTracker::PrefProxyConfigTracker(
121 PrefService* pref_service)
122 : pref_service_(pref_service),
123 chrome_proxy_config_service_(NULL),
124 update_pending_(true) {
19 config_state_ = ReadPrefConfig(&pref_config_); 125 config_state_ = ReadPrefConfig(&pref_config_);
20 proxy_prefs_observer_.reset( 126 proxy_prefs_observer_.reset(
21 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this)); 127 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this));
22 } 128 }
23 129
24 PrefProxyConfigTracker::~PrefProxyConfigTracker() { 130 PrefProxyConfigTracker::~PrefProxyConfigTracker() {
25 DCHECK(pref_service_ == NULL); 131 DCHECK(pref_service_ == NULL);
26 } 132 }
27 133
28 PrefProxyConfigTracker::ConfigState 134 void PrefProxyConfigTracker::SetChromeProxyConfigService(
29 PrefProxyConfigTracker::GetProxyConfig(net::ProxyConfig* config) { 135 ChromeProxyConfigService* chrome_proxy_config_service) {
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 136 chrome_proxy_config_service_ = chrome_proxy_config_service;
31 if (config_state_ != CONFIG_UNSET) 137 if (chrome_proxy_config_service_ && update_pending_)
32 *config = pref_config_; 138 OnProxyConfigChanged(config_state_, pref_config_);
33 return config_state_;
34 } 139 }
35 140
36 void PrefProxyConfigTracker::DetachFromPrefService() { 141 void PrefProxyConfigTracker::DetachFromPrefService() {
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
38 // Stop notifications. 143 // Stop notifications.
39 proxy_prefs_observer_.reset(); 144 proxy_prefs_observer_.reset();
40 pref_service_ = NULL; 145 pref_service_ = NULL;
146 SetChromeProxyConfigService(NULL);
41 } 147 }
42 148
43 void PrefProxyConfigTracker::AddObserver( 149 // static
44 PrefProxyConfigTracker::Observer* observer) { 150 bool PrefProxyConfigTracker::PrefPrecedes(
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 151 ProxyPrefs::ConfigState config_state) {
46 observers_.AddObserver(observer); 152 return config_state == ProxyPrefs::CONFIG_POLICY ||
153 config_state == ProxyPrefs::CONFIG_EXTENSION ||
154 config_state == ProxyPrefs::CONFIG_OTHER_PRECEDE;
47 } 155 }
48 156
49 void PrefProxyConfigTracker::RemoveObserver( 157 // static
50 PrefProxyConfigTracker::Observer* observer) { 158 net::ProxyConfigService::ConfigAvailability
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 159 PrefProxyConfigTracker::GetEffectiveProxyConfig(
52 observers_.RemoveObserver(observer); 160 ProxyPrefs::ConfigState pref_state,
161 const net::ProxyConfig& pref_config,
162 net::ProxyConfigService::ConfigAvailability system_availability,
163 const net::ProxyConfig& system_config,
164 bool ignore_fallback_config,
165 ProxyPrefs::ConfigState* effective_config_state,
166 net::ProxyConfig* effective_config) {
167 *effective_config_state = pref_state;
168
169 if (PrefPrecedes(pref_state)) {
170 *effective_config = pref_config;
171 return net::ProxyConfigService::CONFIG_VALID;
172 }
173
174 // If there's no system proxy config, fall back to prefs or default.
175 if (system_availability == net::ProxyConfigService::CONFIG_UNSET) {
176 if (pref_state == ProxyPrefs::CONFIG_FALLBACK && !ignore_fallback_config)
177 *effective_config = pref_config;
178 else
179 *effective_config = net::ProxyConfig::CreateDirect();
180 return net::ProxyConfigService::CONFIG_VALID;
181 }
182
183 *effective_config_state = ProxyPrefs::CONFIG_SYSTEM;
184 *effective_config = system_config;
185 return system_availability;
53 } 186 }
54 187
55 void PrefProxyConfigTracker::Observe(int type, 188 // static
56 const NotificationSource& source, 189 void PrefProxyConfigTracker::RegisterPrefs(PrefService* pref_service) {
57 const NotificationDetails& details) { 190 DictionaryValue* default_settings = ProxyConfigDictionary::CreateSystem();
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 191 pref_service->RegisterDictionaryPref(prefs::kProxy,
59 if (type == chrome::NOTIFICATION_PREF_CHANGED && 192 default_settings,
60 Source<PrefService>(source).ptr() == pref_service_) { 193 PrefService::UNSYNCABLE_PREF);
61 net::ProxyConfig new_config;
62 ConfigState config_state = ReadPrefConfig(&new_config);
63 BrowserThread::PostTask(
64 BrowserThread::IO, FROM_HERE,
65 NewRunnableMethod(this,
66 &PrefProxyConfigTracker::InstallProxyConfig,
67 new_config, config_state));
68 } else {
69 NOTREACHED() << "Unexpected notification of type " << type;
70 }
71 } 194 }
72 195
73 void PrefProxyConfigTracker::InstallProxyConfig( 196 ProxyPrefs::ConfigState PrefProxyConfigTracker::GetProxyConfig(
74 const net::ProxyConfig& config, 197 net::ProxyConfig* config) {
75 PrefProxyConfigTracker::ConfigState config_state) { 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 199 if (config_state_ != ProxyPrefs::CONFIG_UNSET)
77 if (config_state_ != config_state || 200 *config = pref_config_;
78 (config_state_ != CONFIG_UNSET && !pref_config_.Equals(config))) { 201 return config_state_;
79 config_state_ = config_state;
80 if (config_state_ != CONFIG_UNSET)
81 pref_config_ = config;
82 FOR_EACH_OBSERVER(Observer, observers_, OnPrefProxyConfigChanged());
83 }
84 } 202 }
85 203
86 PrefProxyConfigTracker::ConfigState 204 void PrefProxyConfigTracker::OnProxyConfigChanged(
87 PrefProxyConfigTracker::ReadPrefConfig(net::ProxyConfig* config) { 205 ProxyPrefs::ConfigState config_state,
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 206 const net::ProxyConfig& config) {
89 207 if (!chrome_proxy_config_service_) {
90 // Clear the configuration. 208 VLOG(1) << "No chrome proxy config service to push to UpdateProxyConfig";
91 *config = net::ProxyConfig(); 209 update_pending_ = true;
92 210 return;
93 const PrefService::Preference* pref =
94 pref_service_->FindPreference(prefs::kProxy);
95 DCHECK(pref);
96
97 const DictionaryValue* dict = pref_service_->GetDictionary(prefs::kProxy);
98 DCHECK(dict);
99 ProxyConfigDictionary proxy_dict(dict);
100
101 if (PrefConfigToNetConfig(proxy_dict, config)) {
102 return (!pref->IsUserModifiable() || pref->HasUserSetting()) ?
103 CONFIG_PRESENT : CONFIG_FALLBACK;
104 } 211 }
105 212 update_pending_ = !BrowserThread::PostTask(
106 return CONFIG_UNSET; 213 BrowserThread::IO, FROM_HERE,
214 base::Bind(&ChromeProxyConfigService::UpdateProxyConfig,
215 base::Unretained(chrome_proxy_config_service_),
216 config_state, config));
217 if (update_pending_)
218 VLOG(1) << "Unable to push to UpdateProxyConfig";
107 } 219 }
108 220
109 bool PrefProxyConfigTracker::PrefConfigToNetConfig( 221 bool PrefProxyConfigTracker::PrefConfigToNetConfig(
110 const ProxyConfigDictionary& proxy_dict, 222 const ProxyConfigDictionary& proxy_dict,
111 net::ProxyConfig* config) { 223 net::ProxyConfig* config) {
112 ProxyPrefs::ProxyMode mode; 224 ProxyPrefs::ProxyMode mode;
113 if (!proxy_dict.GetMode(&mode)) { 225 if (!proxy_dict.GetMode(&mode)) {
114 // Fall back to system settings if the mode preference is invalid. 226 // Fall back to system settings if the mode preference is invalid.
115 return false; 227 return false;
116 } 228 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 return true; 272 return true;
161 } 273 }
162 case ProxyPrefs::kModeCount: { 274 case ProxyPrefs::kModeCount: {
163 // Fall through to NOTREACHED(). 275 // Fall through to NOTREACHED().
164 } 276 }
165 } 277 }
166 NOTREACHED() << "Unknown proxy mode, falling back to system settings."; 278 NOTREACHED() << "Unknown proxy mode, falling back to system settings.";
167 return false; 279 return false;
168 } 280 }
169 281
170 PrefProxyConfigService::PrefProxyConfigService( 282 void PrefProxyConfigTracker::Observe(int type,
171 PrefProxyConfigTracker* tracker, 283 const NotificationSource& source,
172 net::ProxyConfigService* base_service) 284 const NotificationDetails& details) {
173 : base_service_(base_service), 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
174 pref_config_tracker_(tracker), 286 if (type == chrome::NOTIFICATION_PREF_CHANGED &&
175 registered_observers_(false) { 287 Source<PrefService>(source).ptr() == pref_service_) {
176 } 288 net::ProxyConfig new_config;
177 289 ProxyPrefs::ConfigState config_state = ReadPrefConfig(&new_config);
178 PrefProxyConfigService::~PrefProxyConfigService() { 290 if (config_state_ != config_state ||
179 if (registered_observers_) { 291 (config_state_ != ProxyPrefs::CONFIG_UNSET &&
180 base_service_->RemoveObserver(this); 292 !pref_config_.Equals(new_config))) {
181 pref_config_tracker_->RemoveObserver(this); 293 config_state_ = config_state;
294 if (config_state_ != ProxyPrefs::CONFIG_UNSET)
295 pref_config_ = new_config;
296 update_pending_ = true;
297 }
298 if (update_pending_)
299 OnProxyConfigChanged(config_state, new_config);
300 } else {
301 NOTREACHED() << "Unexpected notification of type " << type;
182 } 302 }
183 } 303 }
184 304
185 void PrefProxyConfigService::AddObserver( 305 ProxyPrefs::ConfigState PrefProxyConfigTracker::ReadPrefConfig(
186 net::ProxyConfigService::Observer* observer) { 306 net::ProxyConfig* config) {
187 RegisterObservers(); 307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
188 observers_.AddObserver(observer);
189 }
190 308
191 void PrefProxyConfigService::RemoveObserver( 309 // Clear the configuration and source.
192 net::ProxyConfigService::Observer* observer) { 310 *config = net::ProxyConfig();
193 observers_.RemoveObserver(observer); 311 ProxyPrefs::ConfigState config_state = ProxyPrefs::CONFIG_UNSET;
194 }
195 312
196 net::ProxyConfigService::ConfigAvailability 313 const PrefService::Preference* pref =
197 PrefProxyConfigService::GetLatestProxyConfig(net::ProxyConfig* config) { 314 pref_service_->FindPreference(prefs::kProxy);
198 RegisterObservers(); 315 DCHECK(pref);
199 net::ProxyConfig pref_config; 316
200 PrefProxyConfigTracker::ConfigState state = 317 const DictionaryValue* dict = pref_service_->GetDictionary(prefs::kProxy);
201 pref_config_tracker_->GetProxyConfig(&pref_config); 318 DCHECK(dict);
202 if (state == PrefProxyConfigTracker::CONFIG_PRESENT) { 319 ProxyConfigDictionary proxy_dict(dict);
203 *config = pref_config; 320
204 return CONFIG_VALID; 321 if (PrefConfigToNetConfig(proxy_dict, config)) {
322 if (!pref->IsUserModifiable() || pref->HasUserSetting()) {
323 if (pref->IsManaged())
324 config_state = ProxyPrefs::CONFIG_POLICY;
325 else if (pref->IsExtensionControlled())
326 config_state = ProxyPrefs::CONFIG_EXTENSION;
327 else
328 config_state = ProxyPrefs::CONFIG_OTHER_PRECEDE;
329 } else {
330 config_state = ProxyPrefs::CONFIG_FALLBACK;
331 }
205 } 332 }
206 333
207 // Ask the base service. 334 return config_state;
208 ConfigAvailability available = base_service_->GetLatestProxyConfig(config);
209
210 // Base service doesn't have a configuration, fall back to prefs or default.
211 if (available == CONFIG_UNSET) {
212 if (state == PrefProxyConfigTracker::CONFIG_FALLBACK)
213 *config = pref_config;
214 else
215 *config = net::ProxyConfig::CreateDirect();
216 return CONFIG_VALID;
217 }
218
219 return available;
220 } 335 }
221
222 void PrefProxyConfigService::OnLazyPoll() {
223 base_service_->OnLazyPoll();
224 }
225
226 void PrefProxyConfigService::OnProxyConfigChanged(
227 const net::ProxyConfig& config,
228 ConfigAvailability availability) {
229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
230
231 // Check whether there is a proxy configuration defined by preferences. In
232 // this case that proxy configuration takes precedence and the change event
233 // from the delegate proxy service can be disregarded.
234 net::ProxyConfig actual_config;
235 if (pref_config_tracker_->GetProxyConfig(&actual_config) !=
236 PrefProxyConfigTracker::CONFIG_PRESENT) {
237 availability = GetLatestProxyConfig(&actual_config);
238 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
239 OnProxyConfigChanged(actual_config, availability));
240 }
241 }
242
243 void PrefProxyConfigService::OnPrefProxyConfigChanged() {
244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
245
246 // Evaluate the proxy configuration. If GetLatestProxyConfig returns
247 // CONFIG_PENDING, we are using the system proxy service, but it doesn't have
248 // a valid configuration yet. Once it is ready, OnProxyConfigChanged() will be
249 // called and broadcast the proxy configuration.
250 // Note: If a switch between a preference proxy configuration and the system
251 // proxy configuration occurs an unnecessary notification might get send if
252 // the two configurations agree. This case should be rare however, so we don't
253 // handle that case specially.
254 net::ProxyConfig new_config;
255 ConfigAvailability availability = GetLatestProxyConfig(&new_config);
256 if (availability != CONFIG_PENDING) {
257 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
258 OnProxyConfigChanged(new_config, availability));
259 }
260 }
261
262 void PrefProxyConfigService::RegisterObservers() {
263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
264 if (!registered_observers_) {
265 base_service_->AddObserver(this);
266 pref_config_tracker_->AddObserver(this);
267 registered_observers_ = true;
268 }
269 }
270
271 // static
272 void PrefProxyConfigService::RegisterPrefs(PrefService* pref_service) {
273 DictionaryValue* default_settings = ProxyConfigDictionary::CreateSystem();
274 pref_service->RegisterDictionaryPref(prefs::kProxy,
275 default_settings,
276 PrefService::UNSYNCABLE_PREF);
277 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698