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

Side by Side Diff: chrome/browser/net/pref_proxy_config_service.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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/net/pref_proxy_config_service.h"
6
7 #include "base/values.h"
8 #include "chrome/browser/prefs/pref_service.h"
9 #include "chrome/browser/prefs/pref_set_observer.h"
10 #include "chrome/browser/prefs/proxy_config_dictionary.h"
11 #include "chrome/common/chrome_notification_types.h"
12 #include "chrome/common/pref_names.h"
13 #include "content/browser/browser_thread.h"
14 #include "content/common/notification_details.h"
15 #include "content/common/notification_source.h"
16
17 PrefProxyConfigTracker::PrefProxyConfigTracker(PrefService* pref_service)
18 : pref_service_(pref_service) {
19 config_state_ = ReadPrefConfig(&pref_config_);
20 proxy_prefs_observer_.reset(
21 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this));
22 }
23
24 PrefProxyConfigTracker::~PrefProxyConfigTracker() {
25 DCHECK(pref_service_ == NULL);
26 }
27
28 PrefProxyConfigTracker::ConfigState
29 PrefProxyConfigTracker::GetProxyConfig(net::ProxyConfig* config) {
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
31 if (config_state_ != CONFIG_UNSET)
32 *config = pref_config_;
33 return config_state_;
34 }
35
36 void PrefProxyConfigTracker::DetachFromPrefService() {
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
38 // Stop notifications.
39 proxy_prefs_observer_.reset();
40 pref_service_ = NULL;
41 }
42
43 void PrefProxyConfigTracker::AddObserver(
44 PrefProxyConfigTracker::Observer* observer) {
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
46 observers_.AddObserver(observer);
47 }
48
49 void PrefProxyConfigTracker::RemoveObserver(
50 PrefProxyConfigTracker::Observer* observer) {
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
52 observers_.RemoveObserver(observer);
53 }
54
55 void PrefProxyConfigTracker::Observe(int type,
56 const NotificationSource& source,
57 const NotificationDetails& details) {
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
59 if (type == chrome::NOTIFICATION_PREF_CHANGED &&
60 Source<PrefService>(source).ptr() == pref_service_) {
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 }
72
73 void PrefProxyConfigTracker::InstallProxyConfig(
74 const net::ProxyConfig& config,
75 PrefProxyConfigTracker::ConfigState config_state) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
77 if (config_state_ != config_state ||
78 (config_state_ != CONFIG_UNSET && !pref_config_.Equals(config))) {
79 config_state_ = config_state;
80 if (config_state_ != CONFIG_UNSET)
81 pref_config_ = config;
82 FOR_EACH_OBSERVER(Observer, observers_, OnPrefProxyConfigChanged());
83 }
84 }
85
86 PrefProxyConfigTracker::ConfigState
87 PrefProxyConfigTracker::ReadPrefConfig(net::ProxyConfig* config) {
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
89
90 // Clear the configuration.
91 *config = net::ProxyConfig();
92
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 }
105
106 return CONFIG_UNSET;
107 }
108
109 bool PrefProxyConfigTracker::PrefConfigToNetConfig(
110 const ProxyConfigDictionary& proxy_dict,
111 net::ProxyConfig* config) {
112 ProxyPrefs::ProxyMode mode;
113 if (!proxy_dict.GetMode(&mode)) {
114 // Fall back to system settings if the mode preference is invalid.
115 return false;
116 }
117
118 switch (mode) {
119 case ProxyPrefs::MODE_SYSTEM:
120 // Use system settings.
121 return false;
122 case ProxyPrefs::MODE_DIRECT:
123 // Ignore all the other proxy config preferences if the use of a proxy
124 // has been explicitly disabled.
125 return true;
126 case ProxyPrefs::MODE_AUTO_DETECT:
127 config->set_auto_detect(true);
128 return true;
129 case ProxyPrefs::MODE_PAC_SCRIPT: {
130 std::string proxy_pac;
131 if (!proxy_dict.GetPacUrl(&proxy_pac)) {
132 LOG(ERROR) << "Proxy settings request PAC script but do not specify "
133 << "its URL. Falling back to direct connection.";
134 return true;
135 }
136 GURL proxy_pac_url(proxy_pac);
137 if (!proxy_pac_url.is_valid()) {
138 LOG(ERROR) << "Invalid proxy PAC url: " << proxy_pac;
139 return true;
140 }
141 config->set_pac_url(proxy_pac_url);
142 bool pac_mandatory = false;
143 proxy_dict.GetPacMandatory(&pac_mandatory);
144 config->set_pac_mandatory(pac_mandatory);
145 return true;
146 }
147 case ProxyPrefs::MODE_FIXED_SERVERS: {
148 std::string proxy_server;
149 if (!proxy_dict.GetProxyServer(&proxy_server)) {
150 LOG(ERROR) << "Proxy settings request fixed proxy servers but do not "
151 << "specify their URLs. Falling back to direct connection.";
152 return true;
153 }
154 config->proxy_rules().ParseFromString(proxy_server);
155
156 std::string proxy_bypass;
157 if (proxy_dict.GetBypassList(&proxy_bypass)) {
158 config->proxy_rules().bypass_rules.ParseFromString(proxy_bypass);
159 }
160 return true;
161 }
162 case ProxyPrefs::kModeCount: {
163 // Fall through to NOTREACHED().
164 }
165 }
166 NOTREACHED() << "Unknown proxy mode, falling back to system settings.";
167 return false;
168 }
169
170 PrefProxyConfigService::PrefProxyConfigService(
171 PrefProxyConfigTracker* tracker,
172 net::ProxyConfigService* base_service)
173 : base_service_(base_service),
174 pref_config_tracker_(tracker),
175 registered_observers_(false) {
176 }
177
178 PrefProxyConfigService::~PrefProxyConfigService() {
179 if (registered_observers_) {
180 base_service_->RemoveObserver(this);
181 pref_config_tracker_->RemoveObserver(this);
182 }
183 }
184
185 void PrefProxyConfigService::AddObserver(
186 net::ProxyConfigService::Observer* observer) {
187 RegisterObservers();
188 observers_.AddObserver(observer);
189 }
190
191 void PrefProxyConfigService::RemoveObserver(
192 net::ProxyConfigService::Observer* observer) {
193 observers_.RemoveObserver(observer);
194 }
195
196 net::ProxyConfigService::ConfigAvailability
197 PrefProxyConfigService::GetLatestProxyConfig(net::ProxyConfig* config) {
198 RegisterObservers();
199 net::ProxyConfig pref_config;
200 PrefProxyConfigTracker::ConfigState state =
201 pref_config_tracker_->GetProxyConfig(&pref_config);
202 if (state == PrefProxyConfigTracker::CONFIG_PRESENT) {
203 *config = pref_config;
204 return CONFIG_VALID;
205 }
206
207 // Ask the base service.
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 }
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