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

Side by Side Diff: chrome/browser/chromeos/proxy_config_service_impl.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, 1 month 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/chromeos/proxy_config_service_impl.h" 5 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
6 6
7 #include <ostream> 7 #include <ostream>
8 8
9 #include "base/bind.h"
9 #include "base/json/json_value_serializer.h" 10 #include "base/json/json_value_serializer.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/string_util.h" 12 #include "base/string_util.h"
12 #include "base/task.h"
13 #include "chrome/browser/chromeos/cros/cros_library.h" 13 #include "chrome/browser/chromeos/cros/cros_library.h"
14 #include "chrome/browser/chromeos/cros_settings_names.h" 14 #include "chrome/browser/chromeos/cros_settings_names.h"
15 #include "chrome/browser/chromeos/login/user_manager.h"
15 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" 16 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h"
17 #include "chrome/browser/prefs/pref_service.h"
16 #include "chrome/browser/prefs/proxy_config_dictionary.h" 18 #include "chrome/browser/prefs/proxy_config_dictionary.h"
17 #include "chrome/browser/prefs/proxy_prefs.h" 19 #include "chrome/browser/prefs/proxy_prefs.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/common/chrome_notification_types.h"
22 #include "chrome/common/pref_names.h"
18 #include "content/browser/browser_thread.h" 23 #include "content/browser/browser_thread.h"
24 #include "content/public/browser/notification_details.h"
25 #include "content/public/browser/notification_source.h"
19 #include "grit/generated_resources.h" 26 #include "grit/generated_resources.h"
20 #include "ui/base/l10n/l10n_util.h" 27 #include "ui/base/l10n/l10n_util.h"
21 28
22 namespace em = enterprise_management; 29 namespace em = enterprise_management;
23 30
24 namespace chromeos { 31 namespace chromeos {
25 32
26 namespace { 33 namespace {
27 34
28 const char* SourceToString(ProxyConfigServiceImpl::ProxyConfig::Source source) { 35 const char* ModeToString(ProxyConfigServiceImpl::ProxyConfig::Mode mode) {
29 switch (source) { 36 switch (mode) {
30 case ProxyConfigServiceImpl::ProxyConfig::SOURCE_NONE: 37 case ProxyConfigServiceImpl::ProxyConfig::MODE_DIRECT:
31 return "SOURCE_NONE"; 38 return "direct";
32 case ProxyConfigServiceImpl::ProxyConfig::SOURCE_POLICY: 39 case ProxyConfigServiceImpl::ProxyConfig::MODE_AUTO_DETECT:
33 return "SOURCE_POLICY"; 40 return "auto-detect";
34 case ProxyConfigServiceImpl::ProxyConfig::SOURCE_OWNER: 41 case ProxyConfigServiceImpl::ProxyConfig::MODE_PAC_SCRIPT:
35 return "SOURCE_OWNER"; 42 return "pacurl";
43 case ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY:
44 return "single-proxy";
45 case ProxyConfigServiceImpl::ProxyConfig::MODE_PROXY_PER_SCHEME:
46 return "proxy-per-scheme";
36 } 47 }
37 NOTREACHED() << "Unrecognized source type"; 48 NOTREACHED() << "Unrecognized mode type";
38 return ""; 49 return "";
39 } 50 }
40 51
41 std::ostream& operator<<(std::ostream& out, 52 const char* ConfigStateToString(ProxyPrefs::ConfigState state) {
53 switch (state) {
54 case ProxyPrefs::CONFIG_POLICY:
55 return "config_policy";
56 case ProxyPrefs::CONFIG_EXTENSION:
57 return "config_extension";
58 case ProxyPrefs::CONFIG_OTHER_PRECEDE:
59 return "config_other_precede";
60 case ProxyPrefs::CONFIG_SYSTEM:
61 return "config_network"; // For ChromeOS, system is network.
62 case ProxyPrefs::CONFIG_FALLBACK:
63 return "config_recommended"; // Fallback is recommended.
64 case ProxyPrefs::CONFIG_UNSET:
65 return "config_unset";
66 }
67 NOTREACHED() << "Unrecognized config state type";
68 return "";
69 }
70
71 // Only unblock if needed for debugging.
72 #if defined(NEED_DEBUG_LOG)
73 std::ostream& operator<<(
74 std::ostream& out,
42 const ProxyConfigServiceImpl::ProxyConfig::ManualProxy& proxy) { 75 const ProxyConfigServiceImpl::ProxyConfig::ManualProxy& proxy) {
43 out << " " << SourceToString(proxy.source) << "\n" 76 out << (proxy.server.is_valid() ? proxy.server.ToURI() : "") << "\n";
44 << " server: " << (proxy.server.is_valid() ? proxy.server.ToURI() : "")
45 << "\n";
46 return out; 77 return out;
47 } 78 }
48 79
49 std::ostream& operator<<(std::ostream& out, 80 std::ostream& operator<<(std::ostream& out,
50 const ProxyConfigServiceImpl::ProxyConfig& config) { 81 const ProxyConfigServiceImpl::ProxyConfig& config) {
51 switch (config.mode) { 82 switch (config.mode) {
52 case ProxyConfigServiceImpl::ProxyConfig::MODE_DIRECT: 83 case ProxyConfigServiceImpl::ProxyConfig::MODE_DIRECT:
53 out << "Direct connection:\n "
54 << SourceToString(config.automatic_proxy.source) << "\n";
55 break;
56 case ProxyConfigServiceImpl::ProxyConfig::MODE_AUTO_DETECT: 84 case ProxyConfigServiceImpl::ProxyConfig::MODE_AUTO_DETECT:
57 out << "Auto detection:\n " 85 out << ModeToString(config.mode) << ", "
58 << SourceToString(config.automatic_proxy.source) << "\n"; 86 << ConfigStateToString(config.state) << "\n";
59 break; 87 break;
60 case ProxyConfigServiceImpl::ProxyConfig::MODE_PAC_SCRIPT: 88 case ProxyConfigServiceImpl::ProxyConfig::MODE_PAC_SCRIPT:
61 out << "Custom PAC script:\n " 89 out << ModeToString(config.mode) << ", "
62 << SourceToString(config.automatic_proxy.source) 90 << ConfigStateToString(config.state)
63 << "\n PAC: " << config.automatic_proxy.pac_url << "\n"; 91 << "\n PAC: " << config.automatic_proxy.pac_url << "\n";
64 break; 92 break;
65 case ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY: 93 case ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY:
66 out << "Single proxy:\n" << config.single_proxy; 94 out << ModeToString(config.mode) << ", "
95 << ConfigStateToString(config.state) << "\n " << config.single_proxy;
67 break; 96 break;
68 case ProxyConfigServiceImpl::ProxyConfig::MODE_PROXY_PER_SCHEME: 97 case ProxyConfigServiceImpl::ProxyConfig::MODE_PROXY_PER_SCHEME:
69 out << "HTTP proxy: " << config.http_proxy; 98 out << ModeToString(config.mode) << ", "
70 out << "HTTPS proxy: " << config.https_proxy; 99 << ConfigStateToString(config.state) << "\n"
71 out << "FTP proxy: " << config.ftp_proxy; 100 << " HTTP: " << config.http_proxy
72 out << "SOCKS proxy: " << config.socks_proxy; 101 << " HTTPS: " << config.https_proxy
102 << " FTP: " << config.ftp_proxy
103 << " SOCKS: " << config.socks_proxy;
73 break; 104 break;
74 default: 105 default:
75 NOTREACHED() << "Unrecognized proxy config mode"; 106 NOTREACHED() << "Unrecognized proxy config mode";
76 break; 107 break;
77 } 108 }
78 if (config.mode == ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY || 109 if (config.mode == ProxyConfigServiceImpl::ProxyConfig::MODE_SINGLE_PROXY ||
79 config.mode == 110 config.mode ==
80 ProxyConfigServiceImpl::ProxyConfig::MODE_PROXY_PER_SCHEME) { 111 ProxyConfigServiceImpl::ProxyConfig::MODE_PROXY_PER_SCHEME) {
81 out << "Bypass list: "; 112 out << "Bypass list: ";
82 if (config.bypass_rules.rules().empty()) { 113 if (config.bypass_rules.rules().empty()) {
83 out << "[None]"; 114 out << "[None]";
84 } else { 115 } else {
85 const net::ProxyBypassRules& bypass_rules = config.bypass_rules; 116 const net::ProxyBypassRules& bypass_rules = config.bypass_rules;
86 net::ProxyBypassRules::RuleList::const_iterator it; 117 net::ProxyBypassRules::RuleList::const_iterator it;
87 for (it = bypass_rules.rules().begin(); 118 for (it = bypass_rules.rules().begin();
88 it != bypass_rules.rules().end(); ++it) { 119 it != bypass_rules.rules().end(); ++it) {
89 out << "\n " << (*it)->ToString(); 120 out << "\n " << (*it)->ToString();
90 } 121 }
91 } 122 }
92 } 123 }
93 return out; 124 return out;
94 } 125 }
95 126
96 std::string ProxyConfigToString( 127 std::string ProxyConfigToString(
97 const ProxyConfigServiceImpl::ProxyConfig& proxy_config) { 128 const ProxyConfigServiceImpl::ProxyConfig& proxy_config) {
98 std::ostringstream stream; 129 std::ostringstream stream;
99 stream << proxy_config; 130 stream << proxy_config;
100 return stream.str(); 131 return stream.str();
101 } 132 }
133 #endif // defined(NEED_DEBUG_LOG)
102 134
103 } // namespace 135 } // namespace
104 136
105 //---------- ProxyConfigServiceImpl::ProxyConfig::Setting methods --------------
106
107 bool ProxyConfigServiceImpl::ProxyConfig::Setting::CanBeWrittenByUser(
108 bool user_is_owner) {
109 // Setting can only be written by user if user is owner and setting is not
110 // from policy.
111 return user_is_owner && source != ProxyConfig::SOURCE_POLICY;
112 }
113
114 //----------- ProxyConfigServiceImpl::ProxyConfig: public methods -------------- 137 //----------- ProxyConfigServiceImpl::ProxyConfig: public methods --------------
115 138
116 ProxyConfigServiceImpl::ProxyConfig::ProxyConfig() : mode(MODE_DIRECT) {} 139 ProxyConfigServiceImpl::ProxyConfig::ProxyConfig()
140 : mode(MODE_DIRECT),
141 state(ProxyPrefs::CONFIG_UNSET),
142 user_modifiable(true) {}
117 143
118 ProxyConfigServiceImpl::ProxyConfig::~ProxyConfig() {} 144 ProxyConfigServiceImpl::ProxyConfig::~ProxyConfig() {}
119 145
120 void ProxyConfigServiceImpl::ProxyConfig::ToNetProxyConfig( 146 bool ProxyConfigServiceImpl::ProxyConfig::FromNetProxyConfig(
121 net::ProxyConfig* net_config) { 147 const net::ProxyConfig& net_config) {
122 switch (mode) { 148 *this = ProxyConfigServiceImpl::ProxyConfig(); // Reset to default.
123 case MODE_DIRECT: 149 const net::ProxyConfig::ProxyRules& rules = net_config.proxy_rules();
124 *net_config = net::ProxyConfig::CreateDirect(); 150 switch (rules.type) {
125 break; 151 case net::ProxyConfig::ProxyRules::TYPE_NO_RULES:
126 case MODE_AUTO_DETECT: 152 if (!net_config.HasAutomaticSettings()) {
127 *net_config = net::ProxyConfig::CreateAutoDetect(); 153 mode = ProxyConfig::MODE_DIRECT;
128 break; 154 } else if (net_config.auto_detect()) {
129 case MODE_PAC_SCRIPT: 155 mode = ProxyConfig::MODE_AUTO_DETECT;
130 *net_config = net::ProxyConfig::CreateFromCustomPacURL( 156 } else if (net_config.has_pac_url()) {
131 automatic_proxy.pac_url); 157 mode = ProxyConfig::MODE_PAC_SCRIPT;
132 break; 158 automatic_proxy.pac_url = net_config.pac_url();
133 case MODE_SINGLE_PROXY: 159 } else {
134 *net_config = net::ProxyConfig(); 160 return false;
135 net_config->proxy_rules().type = 161 }
136 net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; 162 return true;
137 net_config->proxy_rules().single_proxy = single_proxy.server; 163 case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY:
138 net_config->proxy_rules().bypass_rules = bypass_rules; 164 if (!rules.single_proxy.is_valid())
139 break; 165 return false;
140 case MODE_PROXY_PER_SCHEME: 166 mode = MODE_SINGLE_PROXY;
141 *net_config = net::ProxyConfig(); 167 single_proxy.server = rules.single_proxy;
142 net_config->proxy_rules().type = 168 bypass_rules = rules.bypass_rules;
143 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME; 169 return true;
144 net_config->proxy_rules().proxy_for_http = http_proxy.server; 170 case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME:
145 net_config->proxy_rules().proxy_for_https = https_proxy.server; 171 // Make sure we have valid server for at least one of the protocols.
146 net_config->proxy_rules().proxy_for_ftp = ftp_proxy.server; 172 if (!rules.proxy_for_http.is_valid() &&
147 net_config->proxy_rules().fallback_proxy = socks_proxy.server; 173 !rules.proxy_for_https.is_valid() &&
148 net_config->proxy_rules().bypass_rules = bypass_rules; 174 !rules.proxy_for_ftp.is_valid() &&
149 break; 175 !rules.fallback_proxy.is_valid()) {
176 return false;
177 }
178 mode = MODE_PROXY_PER_SCHEME;
179 if (rules.proxy_for_http.is_valid())
180 http_proxy.server = rules.proxy_for_http;
181 if (rules.proxy_for_https.is_valid())
182 https_proxy.server = rules.proxy_for_https;
183 if (rules.proxy_for_ftp.is_valid())
184 ftp_proxy.server = rules.proxy_for_ftp;
185 if (rules.fallback_proxy.is_valid())
186 socks_proxy.server = rules.fallback_proxy;
187 bypass_rules = rules.bypass_rules;
188 return true;
150 default: 189 default:
151 NOTREACHED() << "Unrecognized proxy config mode"; 190 NOTREACHED() << "Unrecognized proxy config mode";
152 break; 191 break;
153 } 192 }
193 return false;
154 } 194 }
155 195
156 bool ProxyConfigServiceImpl::ProxyConfig::CanBeWrittenByUser( 196 DictionaryValue* ProxyConfigServiceImpl::ProxyConfig::ToPrefProxyConfig() {
157 bool user_is_owner, const std::string& scheme) {
158 // Setting can only be written by user if user is owner and setting is not
159 // from policy.
160 Setting* setting = NULL;
161 switch (mode) { 197 switch (mode) {
162 case MODE_DIRECT: 198 case MODE_DIRECT: {
163 case MODE_AUTO_DETECT: 199 return ProxyConfigDictionary::CreateDirect();
164 case MODE_PAC_SCRIPT: 200 }
165 setting = &automatic_proxy; 201 case MODE_AUTO_DETECT: {
166 break; 202 return ProxyConfigDictionary::CreateAutoDetect();
167 case MODE_SINGLE_PROXY: 203 }
168 setting = &single_proxy; 204 case MODE_PAC_SCRIPT: {
169 break; 205 return ProxyConfigDictionary::CreatePacScript(
170 case MODE_PROXY_PER_SCHEME: 206 automatic_proxy.pac_url.spec(), false);
171 setting = MapSchemeToProxy(scheme); 207 }
172 break; 208 case MODE_SINGLE_PROXY: {
209 std::string spec;
210 if (single_proxy.server.is_valid())
211 spec = single_proxy.server.ToURI();
212 return ProxyConfigDictionary::CreateFixedServers(
213 spec, bypass_rules.ToString());
214 }
215 case MODE_PROXY_PER_SCHEME: {
216 std::string spec;
217 EncodeAndAppendProxyServer("http", http_proxy.server, &spec);
218 EncodeAndAppendProxyServer("https", https_proxy.server, &spec);
219 EncodeAndAppendProxyServer("ftp", ftp_proxy.server, &spec);
220 EncodeAndAppendProxyServer("socks", socks_proxy.server, &spec);
221 return ProxyConfigDictionary::CreateFixedServers(
222 spec, bypass_rules.ToString());
223 }
173 default: 224 default:
174 break; 225 break;
175 } 226 }
176 if (!setting) { 227 NOTREACHED() << "Unrecognized proxy config mode for preference";
177 NOTREACHED() << "Unrecognized proxy config mode"; 228 return NULL;
178 return false;
179 }
180 return setting->CanBeWrittenByUser(user_is_owner);
181 } 229 }
182 230
183 ProxyConfigServiceImpl::ProxyConfig::ManualProxy* 231 ProxyConfigServiceImpl::ProxyConfig::ManualProxy*
184 ProxyConfigServiceImpl::ProxyConfig::MapSchemeToProxy( 232 ProxyConfigServiceImpl::ProxyConfig::MapSchemeToProxy(
185 const std::string& scheme) { 233 const std::string& scheme) {
186 if (scheme == "http") 234 if (scheme == "http")
187 return &http_proxy; 235 return &http_proxy;
188 if (scheme == "https") 236 if (scheme == "https")
189 return &https_proxy; 237 return &https_proxy;
190 if (scheme == "ftp") 238 if (scheme == "ftp")
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 302
255 bool ProxyConfigServiceImpl::ProxyConfig::SerializeForNetwork( 303 bool ProxyConfigServiceImpl::ProxyConfig::SerializeForNetwork(
256 std::string* output) { 304 std::string* output) {
257 scoped_ptr<DictionaryValue> proxy_dict(ToPrefProxyConfig()); 305 scoped_ptr<DictionaryValue> proxy_dict(ToPrefProxyConfig());
258 if (!proxy_dict.get()) 306 if (!proxy_dict.get())
259 return false; 307 return false;
260 JSONStringValueSerializer serializer(output); 308 JSONStringValueSerializer serializer(output);
261 return serializer.Serialize(*proxy_dict.get()); 309 return serializer.Serialize(*proxy_dict.get());
262 } 310 }
263 311
264 bool ProxyConfigServiceImpl::ProxyConfig::DeserializeForNetwork(
265 const std::string& input) {
266 JSONStringValueSerializer serializer(input);
267 scoped_ptr<Value> value(serializer.Deserialize(NULL, NULL));
268 if (!value.get() || value->GetType() != Value::TYPE_DICTIONARY)
269 return false;
270 DictionaryValue* proxy_dict = static_cast<DictionaryValue*>(value.get());
271 return FromPrefProxyConfig(proxy_dict);
272 }
273
274 bool ProxyConfigServiceImpl::ProxyConfig::Equals(
275 const ProxyConfig& other) const {
276 if (mode != other.mode)
277 return false;
278 switch (mode) {
279 case MODE_DIRECT:
280 case MODE_AUTO_DETECT:
281 return true;
282 case MODE_PAC_SCRIPT:
283 return automatic_proxy.pac_url == other.automatic_proxy.pac_url;
284 case MODE_SINGLE_PROXY:
285 return single_proxy.server == other.single_proxy.server &&
286 bypass_rules.Equals(other.bypass_rules);
287 case MODE_PROXY_PER_SCHEME:
288 return http_proxy.server == other.http_proxy.server &&
289 https_proxy.server == other.https_proxy.server &&
290 ftp_proxy.server == other.ftp_proxy.server &&
291 socks_proxy.server == other.socks_proxy.server &&
292 bypass_rules.Equals(other.bypass_rules);
293 default: {
294 NOTREACHED() << "Unrecognized proxy config mode";
295 break;
296 }
297 }
298 return false;
299 }
300
301 std::string ProxyConfigServiceImpl::ProxyConfig::ToString() const {
302 return ProxyConfigToString(*this);
303 }
304
305 //----------- ProxyConfigServiceImpl::ProxyConfig: private methods ------------- 312 //----------- ProxyConfigServiceImpl::ProxyConfig: private methods -------------
306 313
307 // static 314 // static
308 void ProxyConfigServiceImpl::ProxyConfig::EncodeAndAppendProxyServer( 315 void ProxyConfigServiceImpl::ProxyConfig::EncodeAndAppendProxyServer(
309 const std::string& scheme, 316 const std::string& scheme,
310 const net::ProxyServer& server, 317 const net::ProxyServer& server,
311 std::string* spec) { 318 std::string* spec) {
312 if (!server.is_valid()) 319 if (!server.is_valid())
313 return; 320 return;
314 321
315 if (!spec->empty()) 322 if (!spec->empty())
316 *spec += ';'; 323 *spec += ';';
317 324
318 if (!scheme.empty()) { 325 if (!scheme.empty()) {
319 *spec += scheme; 326 *spec += scheme;
320 *spec += "="; 327 *spec += "=";
321 } 328 }
322 *spec += server.ToURI(); 329 *spec += server.ToURI();
323 } 330 }
324 331
325 DictionaryValue* ProxyConfigServiceImpl::ProxyConfig::ToPrefProxyConfig() {
326 switch (mode) {
327 case MODE_DIRECT: {
328 return ProxyConfigDictionary::CreateDirect();
329 }
330 case MODE_AUTO_DETECT: {
331 return ProxyConfigDictionary::CreateAutoDetect();
332 }
333 case MODE_PAC_SCRIPT: {
334 return ProxyConfigDictionary::CreatePacScript(
335 automatic_proxy.pac_url.spec(), false);
336 }
337 case MODE_SINGLE_PROXY: {
338 std::string spec;
339 if (single_proxy.server.is_valid())
340 spec = single_proxy.server.ToURI();
341 return ProxyConfigDictionary::CreateFixedServers(
342 spec, bypass_rules.ToString());
343 }
344 case MODE_PROXY_PER_SCHEME: {
345 std::string spec;
346 EncodeAndAppendProxyServer("http", http_proxy.server, &spec);
347 EncodeAndAppendProxyServer("https", https_proxy.server, &spec);
348 EncodeAndAppendProxyServer("ftp", ftp_proxy.server, &spec);
349 EncodeAndAppendProxyServer("socks", socks_proxy.server, &spec);
350 return ProxyConfigDictionary::CreateFixedServers(
351 spec, bypass_rules.ToString());
352 }
353 default:
354 break;
355 }
356 NOTREACHED() << "Unrecognized proxy config mode for preference";
357 return NULL;
358 }
359
360 bool ProxyConfigServiceImpl::ProxyConfig::FromPrefProxyConfig(
361 const DictionaryValue* dict) {
362 ProxyConfigDictionary proxy_dict(dict);
363
364 *this = ProxyConfigServiceImpl::ProxyConfig(); // Reset to default.
365
366 ProxyPrefs::ProxyMode proxy_mode;
367 if (!proxy_dict.GetMode(&proxy_mode)) {
368 // Fall back to system settings if the mode preference is invalid.
369 return false;
370 }
371
372 switch (proxy_mode) {
373 case ProxyPrefs::MODE_SYSTEM:
374 // Use system settings, so we shouldn't use |this| proxy config.
375 return false;
376 case ProxyPrefs::MODE_DIRECT:
377 // Ignore all the other proxy config preferences if the use of a proxy
378 // has been explicitly disabled.
379 return true;
380 case ProxyPrefs::MODE_AUTO_DETECT:
381 mode = MODE_AUTO_DETECT;
382 return true;
383 case ProxyPrefs::MODE_PAC_SCRIPT: {
384 std::string proxy_pac;
385 if (!proxy_dict.GetPacUrl(&proxy_pac)) {
386 LOG(ERROR) << "Proxy settings request PAC script but do not specify "
387 << "its URL. Falling back to direct connection.";
388 return true;
389 }
390 GURL proxy_pac_url(proxy_pac);
391 if (!proxy_pac_url.is_valid()) {
392 LOG(ERROR) << "Invalid proxy PAC url: " << proxy_pac;
393 return true;
394 }
395 mode = MODE_PAC_SCRIPT;
396 automatic_proxy.pac_url = proxy_pac_url;
397 return true;
398 }
399 case ProxyPrefs::MODE_FIXED_SERVERS: {
400 std::string proxy_server;
401 if (!proxy_dict.GetProxyServer(&proxy_server)) {
402 LOG(ERROR) << "Proxy settings request fixed proxy servers but do not "
403 << "specify their URLs. Falling back to direct connection.";
404 return true;
405 }
406 net::ProxyConfig::ProxyRules proxy_rules;
407 proxy_rules.ParseFromString(proxy_server);
408 if (proxy_rules.type == net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY) {
409 mode = MODE_SINGLE_PROXY;
410 single_proxy.server = proxy_rules.single_proxy;
411 } else if (proxy_rules.type ==
412 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME) {
413 mode = MODE_PROXY_PER_SCHEME;
414 http_proxy.server = proxy_rules.proxy_for_http;
415 https_proxy.server = proxy_rules.proxy_for_https;
416 ftp_proxy.server = proxy_rules.proxy_for_ftp;
417 socks_proxy.server = proxy_rules.fallback_proxy;
418 } else {
419 LOG(ERROR) << "Proxy settings request fixed proxy servers but do not "
420 << "have valid proxy rules type. "
421 << "Falling back to direct connection.";
422 return true;
423 }
424
425 std::string proxy_bypass;
426 if (proxy_dict.GetBypassList(&proxy_bypass)) {
427 bypass_rules.ParseFromString(proxy_bypass);
428 }
429 return true;
430 }
431 case ProxyPrefs::kModeCount: {
432 // Fall through to NOTREACHED().
433 }
434 }
435 NOTREACHED() << "Unknown proxy mode, falling back to system settings.";
436 return false;
437 }
438
439 //------------------- ProxyConfigServiceImpl: public methods ------------------- 332 //------------------- ProxyConfigServiceImpl: public methods -------------------
440 333
441 ProxyConfigServiceImpl::ProxyConfigServiceImpl() 334 ProxyConfigServiceImpl::ProxyConfigServiceImpl(PrefService* pref_service)
442 : testing_(false), 335 : PrefProxyConfigTracker(pref_service),
443 can_post_task_(false), 336 active_config_state_(ProxyPrefs::CONFIG_UNSET),
444 config_availability_(net::ProxyConfigService::CONFIG_UNSET), 337 constructing_(true) {
445 use_shared_proxies_(true) { 338 // Register for notifications of UseSharedProxies user preference, change
339 // default value if necessary.
340 const PrefService::Preference* use_shared_proxies_pref =
341 pref_service->FindPreference(prefs::kUseSharedProxies);
342 if (use_shared_proxies_pref) {
343 use_shared_proxies_.Init(prefs::kUseSharedProxies, pref_service, this);
344 bool default_use_shared_proxies = !UserManager::Get()->user_is_logged_in();
345 if (use_shared_proxies_pref->IsDefaultValue() &&
346 use_shared_proxies_.GetValue() != default_use_shared_proxies) {
347 pref_service->SetBoolean(prefs::kUseSharedProxies,
348 default_use_shared_proxies);
Mattias Nissler (ping if slow) 2011/10/25 16:17:52 I don't think we should actually write the default
kuan 2011/10/25 17:17:33 Done.
349 }
350 }
351
446 // Start async fetch of proxy config from settings persisted on device. 352 // Start async fetch of proxy config from settings persisted on device.
447 if (CrosLibrary::Get()->EnsureLoaded()) { 353 if (CrosLibrary::Get()->libcros_loaded()) {
448 retrieve_property_op_ = SignedSettings::CreateRetrievePropertyOp( 354 retrieve_property_op_ = SignedSettings::CreateRetrievePropertyOp(
449 kSettingProxyEverywhere, this); 355 kSettingProxyEverywhere, this);
450 if (retrieve_property_op_) { 356 if (retrieve_property_op_) {
451 retrieve_property_op_->Execute(); 357 retrieve_property_op_->Execute();
452 VLOG(1) << "Start retrieving proxy setting from device"; 358 VLOG(1) << this << ": Start retrieving proxy setting from device";
453 } else { 359 } else {
454 VLOG(1) << "Fail to retrieve proxy setting from device"; 360 VLOG(1) << this << ": Fail to retrieve proxy setting from device";
455 } 361 }
456 } 362 }
457 363
364 // Register for flimflam network notifications.
458 NetworkLibrary* network_lib = CrosLibrary::Get()->GetNetworkLibrary(); 365 NetworkLibrary* network_lib = CrosLibrary::Get()->GetNetworkLibrary();
459 OnActiveNetworkChanged(network_lib, network_lib->active_network()); 366 OnActiveNetworkChanged(network_lib, network_lib->active_network());
460 network_lib->AddNetworkManagerObserver(this); 367 network_lib->AddNetworkManagerObserver(this);
461 368
462 can_post_task_ = true; 369 constructing_ = false;
463 }
464
465 ProxyConfigServiceImpl::ProxyConfigServiceImpl(const ProxyConfig& init_config)
466 : testing_(false),
467 can_post_task_(true),
468 config_availability_(net::ProxyConfigService::CONFIG_VALID),
469 use_shared_proxies_(true) {
470 active_config_ = init_config;
471 // Update the IO-accessible copy in |cached_config_| as well.
472 cached_config_ = active_config_;
473 } 370 }
474 371
475 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() { 372 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() {
476 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary(); 373 NetworkLibrary* netlib = CrosLibrary::Get()->GetNetworkLibrary();
477 if (netlib) { 374 if (netlib) {
478 netlib->RemoveNetworkManagerObserver(this); 375 netlib->RemoveNetworkManagerObserver(this);
479 netlib->RemoveObserverForAllNetworks(this); 376 netlib->RemoveObserverForAllNetworks(this);
480 } 377 }
481 } 378 }
482 379
380 void ProxyConfigServiceImpl::UISetCurrentNetwork(
381 const std::string& current_network) {
382 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
383 current_network);
384 if (!network) {
385 ResetUICache();
386 LOG(ERROR) << "can't find requested network " << current_network;
387 return;
388 }
389 current_ui_network_ = current_network;
390 OnUISetCurrentNetwork(network);
391 }
392
393 void ProxyConfigServiceImpl::UIMakeActiveNetworkCurrent() {
394 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
395 active_network_);
396 if (!network) {
397 ResetUICache();
398 LOG(ERROR) << "can't find requested network " << active_network_;
399 return;
400 }
401 current_ui_network_ = active_network_;
402 OnUISetCurrentNetwork(network);
403 }
404
405 void ProxyConfigServiceImpl::UIGetCurrentNetworkName(
406 std::string* network_name) {
407 if (!network_name)
408 return;
409 network_name->clear();
410 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
411 current_ui_network_);
412 if (!network) {
413 LOG(ERROR) << "can't find requested network " << current_ui_network_;
414 return;
415 }
416 if (network->name().empty() && network->type() == chromeos::TYPE_ETHERNET) {
417 *network_name =
418 l10n_util::GetStringUTF8(IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET);
419 } else {
420 *network_name = network->name();
421 }
422 }
423
483 void ProxyConfigServiceImpl::UIGetProxyConfig(ProxyConfig* config) { 424 void ProxyConfigServiceImpl::UIGetProxyConfig(ProxyConfig* config) {
484 // Should be called from UI thread. 425 // Simply returns the copy last set from UI via UISetCurrentNetwork or
485 CheckCurrentlyOnUIThread(); 426 // UIMakeActiveNetworkCurrent.
486 // Simply returns the copy on the UI thread.
487 *config = current_ui_config_; 427 *config = current_ui_config_;
488 } 428 }
489 429
490 bool ProxyConfigServiceImpl::UISetCurrentNetwork(
491 const std::string& current_network) {
492 // Should be called from UI thread.
493 CheckCurrentlyOnUIThread();
494 if (current_ui_network_ == current_network)
495 return false;
496 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
497 current_network);
498 if (!network) {
499 LOG(ERROR) << "can't find requested network " << current_network;
500 return false;
501 }
502 current_ui_network_ = current_network;
503 current_ui_config_ = ProxyConfig();
504 SetCurrentNetworkName(network);
505 if (!network->proxy_config().empty())
506 current_ui_config_.DeserializeForNetwork(network->proxy_config());
507 VLOG(1) << "current ui network: "
508 << (current_ui_network_name_.empty() ?
509 current_ui_network_ : current_ui_network_name_)
510 << ", proxy mode: " << current_ui_config_.mode;
511 return true;
512 }
513
514 bool ProxyConfigServiceImpl::UIMakeActiveNetworkCurrent() {
515 // Should be called from UI thread.
516 CheckCurrentlyOnUIThread();
517 if (current_ui_network_ == active_network_)
518 return false;
519 Network* network = NULL;
520 if (!testing_) {
521 network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
522 active_network_);
523 if (!network) {
524 LOG(ERROR) << "can't find requested network " << active_network_;
525 return false;
526 }
527 }
528 current_ui_network_ = active_network_;
529 current_ui_config_ = active_config_;
530 SetCurrentNetworkName(network);
531 VLOG(1) << "current ui network: "
532 << (current_ui_network_name_.empty() ?
533 current_ui_network_ : current_ui_network_name_)
534 << ", proxy mode: " << current_ui_config_.mode;
535 return true;
536 }
537
538 void ProxyConfigServiceImpl::UISetUseSharedProxies(bool use_shared) {
539 // Should be called from UI thread.
540 CheckCurrentlyOnUIThread();
541
542 // Reset all UI-related variables so that the next opening of proxy
543 // configuration dialog of any network will trigger javascript reloading of
544 // (possibly) new proxy settings.
545 current_ui_network_.clear();
546 current_ui_network_name_.clear();
547 current_ui_config_ = ProxyConfig();
548
549 if (use_shared_proxies_ == use_shared) {
550 VLOG(1) << "same use_shared_proxies = " << use_shared_proxies_;
551 return;
552 }
553 use_shared_proxies_ = use_shared;
554 VLOG(1) << "new use_shared_proxies = " << use_shared_proxies_;
555 if (active_network_.empty())
556 return;
557 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
558 active_network_);
559 if (!network) {
560 LOG(ERROR) << "can't find requested network " << active_network_;
561 return;
562 }
563 DetermineConfigFromNetwork(network);
564 }
565
566 bool ProxyConfigServiceImpl::UISetProxyConfigToDirect() { 430 bool ProxyConfigServiceImpl::UISetProxyConfigToDirect() {
567 // Should be called from UI thread.
568 CheckCurrentlyOnUIThread();
569 current_ui_config_.mode = ProxyConfig::MODE_DIRECT; 431 current_ui_config_.mode = ProxyConfig::MODE_DIRECT;
570 OnUISetProxyConfig(); 432 OnUISetProxyConfig();
571 return true; 433 return true;
572 } 434 }
573 435
574 bool ProxyConfigServiceImpl::UISetProxyConfigToAutoDetect() { 436 bool ProxyConfigServiceImpl::UISetProxyConfigToAutoDetect() {
575 // Should be called from UI thread.
576 CheckCurrentlyOnUIThread();
577 current_ui_config_.mode = ProxyConfig::MODE_AUTO_DETECT; 437 current_ui_config_.mode = ProxyConfig::MODE_AUTO_DETECT;
578 OnUISetProxyConfig(); 438 OnUISetProxyConfig();
579 return true; 439 return true;
580 } 440 }
581 441
582 bool ProxyConfigServiceImpl::UISetProxyConfigToPACScript(const GURL& pac_url) { 442 bool ProxyConfigServiceImpl::UISetProxyConfigToPACScript(const GURL& pac_url) {
583 // Should be called from UI thread.
584 CheckCurrentlyOnUIThread();
585 current_ui_config_.mode = ProxyConfig::MODE_PAC_SCRIPT; 443 current_ui_config_.mode = ProxyConfig::MODE_PAC_SCRIPT;
586 current_ui_config_.automatic_proxy.pac_url = pac_url; 444 current_ui_config_.automatic_proxy.pac_url = pac_url;
587 OnUISetProxyConfig(); 445 OnUISetProxyConfig();
588 return true; 446 return true;
589 } 447 }
590 448
591 bool ProxyConfigServiceImpl::UISetProxyConfigToSingleProxy( 449 bool ProxyConfigServiceImpl::UISetProxyConfigToSingleProxy(
592 const net::ProxyServer& server) { 450 const net::ProxyServer& server) {
593 // Should be called from UI thread.
594 CheckCurrentlyOnUIThread();
595 current_ui_config_.mode = ProxyConfig::MODE_SINGLE_PROXY; 451 current_ui_config_.mode = ProxyConfig::MODE_SINGLE_PROXY;
596 current_ui_config_.single_proxy.server = server; 452 current_ui_config_.single_proxy.server = server;
597 OnUISetProxyConfig(); 453 OnUISetProxyConfig();
598 return true; 454 return true;
599 } 455 }
600 456
601 bool ProxyConfigServiceImpl::UISetProxyConfigToProxyPerScheme( 457 bool ProxyConfigServiceImpl::UISetProxyConfigToProxyPerScheme(
602 const std::string& scheme, const net::ProxyServer& server) { 458 const std::string& scheme, const net::ProxyServer& server) {
603 // Should be called from UI thread.
604 CheckCurrentlyOnUIThread();
605 ProxyConfig::ManualProxy* proxy = current_ui_config_.MapSchemeToProxy(scheme); 459 ProxyConfig::ManualProxy* proxy = current_ui_config_.MapSchemeToProxy(scheme);
606 if (!proxy) { 460 if (!proxy) {
607 NOTREACHED() << "Cannot set proxy: invalid scheme [" << scheme << "]"; 461 NOTREACHED() << "Cannot set proxy: invalid scheme [" << scheme << "]";
608 return false; 462 return false;
609 } 463 }
610 current_ui_config_.mode = ProxyConfig::MODE_PROXY_PER_SCHEME; 464 current_ui_config_.mode = ProxyConfig::MODE_PROXY_PER_SCHEME;
611 proxy->server = server; 465 proxy->server = server;
612 OnUISetProxyConfig(); 466 OnUISetProxyConfig();
613 return true; 467 return true;
614 } 468 }
615 469
616 bool ProxyConfigServiceImpl::UISetProxyConfigBypassRules( 470 bool ProxyConfigServiceImpl::UISetProxyConfigBypassRules(
617 const net::ProxyBypassRules& bypass_rules) { 471 const net::ProxyBypassRules& bypass_rules) {
618 // Should be called from UI thread.
619 CheckCurrentlyOnUIThread();
620 if (current_ui_config_.mode != ProxyConfig::MODE_SINGLE_PROXY && 472 if (current_ui_config_.mode != ProxyConfig::MODE_SINGLE_PROXY &&
621 current_ui_config_.mode != ProxyConfig::MODE_PROXY_PER_SCHEME) { 473 current_ui_config_.mode != ProxyConfig::MODE_PROXY_PER_SCHEME) {
622 NOTREACHED(); 474 NOTREACHED();
623 VLOG(1) << "Cannot set bypass rules for proxy mode [" 475 VLOG(1) << "Cannot set bypass rules for proxy mode ["
624 << current_ui_config_.mode << "]"; 476 << current_ui_config_.mode << "]";
625 return false; 477 return false;
626 } 478 }
627 current_ui_config_.bypass_rules = bypass_rules; 479 current_ui_config_.bypass_rules = bypass_rules;
628 OnUISetProxyConfig(); 480 OnUISetProxyConfig();
629 return true; 481 return true;
630 } 482 }
631 483
632 void ProxyConfigServiceImpl::AddObserver( 484 void ProxyConfigServiceImpl::OnProxyConfigChanged(
633 net::ProxyConfigService::Observer* observer) { 485 ProxyPrefs::ConfigState config_state,
634 // Should be called from IO thread. 486 const net::ProxyConfig& config) {
635 CheckCurrentlyOnIOThread(); 487 VLOG(1) << this << ": got prefs change: " << ConfigStateToString(config_state)
636 observers_.AddObserver(observer); 488 << ", mode=" << config.proxy_rules().type;
637 } 489 Network* network = NULL;
638 490 if (!active_network_.empty()) {
639 void ProxyConfigServiceImpl::RemoveObserver( 491 network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
640 net::ProxyConfigService::Observer* observer) { 492 active_network_);
641 // Should be called from IO thread. 493 if (!network)
642 CheckCurrentlyOnIOThread(); 494 LOG(ERROR) << "can't find requested network " << active_network_;
643 observers_.RemoveObserver(observer);
644 }
645
646 net::ProxyConfigService::ConfigAvailability
647 ProxyConfigServiceImpl::IOGetProxyConfig(net::ProxyConfig* net_config) {
648 // Should be called from IO thread.
649 CheckCurrentlyOnIOThread();
650 if (config_availability_ == net::ProxyConfigService::CONFIG_VALID) {
651 VLOG(1) << "returning proxy mode=" << cached_config_.mode;
652 cached_config_.ToNetProxyConfig(net_config);
653 } 495 }
654 return config_availability_; 496 DetermineEffectiveConfig(network, true);
655 } 497 }
656 498
657 void ProxyConfigServiceImpl::OnSettingsOpCompleted( 499 void ProxyConfigServiceImpl::OnSettingsOpCompleted(
658 SignedSettings::ReturnCode code, 500 SignedSettings::ReturnCode code,
659 std::string value) { 501 std::string value) {
660 retrieve_property_op_ = NULL; 502 retrieve_property_op_ = NULL;
661 if (code != SignedSettings::SUCCESS) { 503 if (code != SignedSettings::SUCCESS) {
662 LOG(WARNING) << "Error retrieving proxy setting from device"; 504 LOG(WARNING) << "Error retrieving proxy setting from device";
663 device_config_.clear(); 505 device_config_.clear();
664 return; 506 return;
665 } 507 }
666 VLOG(1) << "Retrieved proxy setting from device, value=[" << value << "]"; 508 VLOG(1) << this << ": Retrieved proxy setting from device, value=["
509 << value << "]";
667 ProxyConfig device_config; 510 ProxyConfig device_config;
668 if (!device_config.DeserializeForDevice(value) || 511 if (!device_config.DeserializeForDevice(value) ||
669 !device_config.SerializeForNetwork(&device_config_)) { 512 !device_config.SerializeForNetwork(&device_config_)) {
670 LOG(WARNING) << "Can't deserialize device setting or serialize for network"; 513 LOG(WARNING) << "Can't deserialize device setting or serialize for network";
671 device_config_.clear(); 514 device_config_.clear();
672 return; 515 return;
673 } 516 }
674 if (!active_network_.empty()) { 517 if (!active_network_.empty()) {
675 VLOG(1) << "try migrating device config to " << active_network_; 518 VLOG(1) << this << ": try migrating device config to " << active_network_;
676 SetProxyConfigForNetwork(active_network_, device_config_, true); 519 SetProxyConfigForNetwork(active_network_, device_config_, true);
677 } 520 }
678 } 521 }
679 522
680 void ProxyConfigServiceImpl::OnNetworkManagerChanged( 523 void ProxyConfigServiceImpl::OnNetworkManagerChanged(
681 NetworkLibrary* network_lib) { 524 NetworkLibrary* network_lib) {
682 VLOG(1) << "OnNetworkManagerChanged: use-shared-proxies=" 525 VLOG(1) << this << " OnNetworkManagerChanged: use-shared-proxies="
683 << use_shared_proxies_; 526 << use_shared_proxies_.GetValue();
684 OnActiveNetworkChanged(network_lib, network_lib->active_network()); 527 OnActiveNetworkChanged(network_lib, network_lib->active_network());
685 } 528 }
686 529
687 void ProxyConfigServiceImpl::OnNetworkChanged(NetworkLibrary* network_lib, 530 void ProxyConfigServiceImpl::OnNetworkChanged(NetworkLibrary* network_lib,
688 const Network* network) { 531 const Network* network) {
689 if (!network) 532 if (!network)
690 return; 533 return;
691 VLOG(1) << "OnNetworkChanged: " 534 VLOG(1) << this << " OnNetworkChanged: "
692 << (network->name().empty() ? network->service_path() : 535 << (network->name().empty() ? network->service_path() :
693 network->name()) 536 network->name())
694 << ", use-shared-proxies=" << use_shared_proxies_; 537 << ", use-shared-proxies=" << use_shared_proxies_.GetValue();
695 // We only care about active network. 538 // We only care about active network.
696 if (network == network_lib->active_network()) 539 if (network == network_lib->active_network())
697 OnActiveNetworkChanged(network_lib, network); 540 OnActiveNetworkChanged(network_lib, network);
698 } 541 }
699 542
543 // static
544 void ProxyConfigServiceImpl::RegisterPrefs(PrefService* pref_service) {
545 // Use shared proxies default to off.
546 // ProxyConfigServiceImpl constructor will set the correct value based on
547 // pre-login and login.
548 pref_service->RegisterBooleanPref(prefs::kUseSharedProxies,
549 false,
550 PrefService::SYNCABLE_PREF);
Mattias Nissler (ping if slow) 2011/10/25 16:17:52 Do you actually want this to be synced?
kuan 2011/10/25 17:17:33 Done.
551 }
552
700 //------------------ ProxyConfigServiceImpl: private methods ------------------- 553 //------------------ ProxyConfigServiceImpl: private methods -------------------
701 554
702 void ProxyConfigServiceImpl::OnUISetProxyConfig() { 555 void ProxyConfigServiceImpl::Observe(
703 if (testing_) { 556 int type,
704 active_config_ = current_ui_config_; 557 const content::NotificationSource& source,
705 IOSetProxyConfig(active_config_, net::ProxyConfigService::CONFIG_VALID); 558 const content::NotificationDetails& details) {
559 if (!constructing_ && type == chrome::NOTIFICATION_PREF_CHANGED &&
560 content::Source<PrefService>(source).ptr() == prefs() &&
561 *(content::Details<std::string>(details).ptr()) ==
562 prefs::kUseSharedProxies) {
563 OnUseSharedProxiesChanged();
706 return; 564 return;
707 } 565 }
566 PrefProxyConfigTracker::Observe(type, source, details);
567 }
568
569 void ProxyConfigServiceImpl::OnUISetProxyConfig() {
708 if (current_ui_network_.empty()) 570 if (current_ui_network_.empty())
709 return; 571 return;
710 // Update config to flimflam. 572 // Update config to flimflam.
711 std::string value; 573 std::string value;
712 if (current_ui_config_.SerializeForNetwork(&value)) { 574 if (current_ui_config_.SerializeForNetwork(&value)) {
713 VLOG(1) << "set proxy (mode=" << current_ui_config_.mode 575 VLOG(1) << this << ": set proxy (mode=" << current_ui_config_.mode
714 << ") for " << current_ui_network_; 576 << ") for " << current_ui_network_;
577 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
715 SetProxyConfigForNetwork(current_ui_network_, value, false); 578 SetProxyConfigForNetwork(current_ui_network_, value, false);
716 } 579 }
717 } 580 }
718 581
719 void ProxyConfigServiceImpl::IOSetProxyConfig(
720 const ProxyConfig& new_config,
721 net::ProxyConfigService::ConfigAvailability new_availability) {
722 if (!BrowserThread::CurrentlyOn(BrowserThread::IO) && can_post_task_) {
723 // Posts a task to IO thread with the new config, so it can update
724 // |cached_config_|.
725 Task* task = NewRunnableMethod(this,
726 &ProxyConfigServiceImpl::IOSetProxyConfig,
727 new_config,
728 new_availability);
729 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, task))
730 VLOG(1) << "Couldn't post task to IO thread to set new proxy config";
731 return;
732 }
733
734 // Now guaranteed to be on the correct thread.
735
736 if (config_availability_ == new_availability &&
737 cached_config_.Equals(new_config))
738 return;
739
740 VLOG(1) << "Proxy changed: mode=" << new_config.mode
741 << ", avail=" << new_availability;
742 cached_config_ = new_config;
743 config_availability_ = new_availability;
744 // Notify observers of new proxy config.
745 net::ProxyConfig net_config;
746 cached_config_.ToNetProxyConfig(&net_config);
747 if (net_config.proxy_rules().type !=
748 net::ProxyConfig::ProxyRules::TYPE_NO_RULES) {
749 net_config.proxy_rules().bypass_rules.AddRuleToBypassLocal();
750 }
751 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
752 OnProxyConfigChanged(net_config, config_availability_));
753 }
754
755 void ProxyConfigServiceImpl::OnActiveNetworkChanged(NetworkLibrary* network_lib, 582 void ProxyConfigServiceImpl::OnActiveNetworkChanged(NetworkLibrary* network_lib,
756 const Network* active_network) { 583 const Network* active_network) {
757 std::string new_network; 584 std::string new_network;
758 if (active_network) 585 if (active_network)
759 new_network = active_network->service_path(); 586 new_network = active_network->service_path();
760 587
761 if (active_network_ == new_network) { // Same active network. 588 if (active_network_ == new_network) { // Same active network.
762 VLOG(1) << "same active network: " 589 VLOG(1) << this << ": same active network: "
763 << (new_network.empty() ? "empty" : 590 << (new_network.empty() ? "empty" :
764 (active_network->name().empty() ? 591 (active_network->name().empty() ?
765 new_network : active_network->name())); 592 new_network : active_network->name()));
593 // If last proxy update to network stack wasn't completed, do it now.
594 if (active_network && update_pending())
595 DetermineEffectiveConfig(active_network, true);
766 return; 596 return;
767 } 597 }
768 598
769 // If there was a previous active network, remove it as observer. 599 // If there was a previous active network, remove it as observer.
770 if (!active_network_.empty()) 600 if (!active_network_.empty())
771 network_lib->RemoveNetworkObserver(active_network_, this); 601 network_lib->RemoveNetworkObserver(active_network_, this);
772 602
773 active_network_ = new_network; 603 active_network_ = new_network;
774 604
775 if (active_network_.empty()) { 605 if (active_network_.empty()) {
776 VLOG(1) << "new active network: empty"; 606 VLOG(1) << this << ": new active network: empty";
777 active_config_ = ProxyConfig(); 607 DetermineEffectiveConfig(active_network, true);
778 IOSetProxyConfig(active_config_, net::ProxyConfigService::CONFIG_UNSET);
779 return; 608 return;
780 } 609 }
781 610
782 VLOG(1) << "new active network: path=" << active_network->service_path() 611 VLOG(1) << this << ": new active network: path="
783 << ", name=" << active_network->name() 612 << active_network->service_path()
784 << ", profile=" << active_network->profile_path() 613 << ", name=" << active_network->name()
785 << ", proxy=" << active_network->proxy_config(); 614 << ", profile=" << active_network->profile_path()
615 << ", proxy=" << active_network->proxy_config();
786 616
787 // Register observer for new network. 617 // Register observer for new network.
788 network_lib->AddNetworkObserver(active_network_, this); 618 network_lib->AddNetworkObserver(active_network_, this);
789 619
790 // If necessary, migrate config to flimflam. 620 // If necessary, migrate config to flimflam.
791 if (active_network->proxy_config().empty() && !device_config_.empty()) { 621 if (active_network->proxy_config().empty() && !device_config_.empty()) {
792 VLOG(1) << "try migrating device config to " << active_network_; 622 VLOG(1) << this << ": try migrating device config to " << active_network_;
793 SetProxyConfigForNetwork(active_network_, device_config_, true); 623 SetProxyConfigForNetwork(active_network_, device_config_, true);
794 } else { 624 } else {
795 DetermineConfigFromNetwork(active_network); 625 // Otherwise, determine and activate possibly new effective proxy config.
626 DetermineEffectiveConfig(active_network, true);
796 } 627 }
797 } 628 }
798 629
799 void ProxyConfigServiceImpl::SetProxyConfigForNetwork( 630 void ProxyConfigServiceImpl::SetProxyConfigForNetwork(
800 const std::string& network_path, const std::string& value, 631 const std::string& network_path, const std::string& value,
801 bool only_set_if_empty) { 632 bool only_set_if_empty) {
802 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( 633 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
803 network_path); 634 network_path);
804 if (!network) { 635 if (!network) {
805 NOTREACHED() << "can't find requested network " << network_path; 636 NOTREACHED() << "can't find requested network " << network_path;
806 return; 637 return;
807 } 638 }
808 if (!only_set_if_empty || network->proxy_config().empty()) { 639 if (!only_set_if_empty || network->proxy_config().empty()) {
809 network->SetProxyConfig(value); 640 network->SetProxyConfig(value);
810 VLOG(1) << "set proxy for " 641 VLOG(1) << this << ": set proxy for " << (network->name().empty() ?
811 << (network->name().empty() ? network_path : network->name()) 642 network_path : network->name())
812 << ", value=" << value; 643 << ", value=" << value;
813 if (network_path == active_network_) 644 if (network_path == active_network_)
814 DetermineConfigFromNetwork(network); 645 DetermineEffectiveConfig(network, true);
815 } 646 }
816 } 647 }
817 648
818 void ProxyConfigServiceImpl::DetermineConfigFromNetwork( 649 void ProxyConfigServiceImpl::OnUseSharedProxiesChanged() {
819 const Network* network) { 650 VLOG(1) << this << ": new use-shared-proxies = "
820 active_config_ = ProxyConfig(); // Default is DIRECT mode (i.e. no proxy). 651 << use_shared_proxies_.GetValue();
821 net::ProxyConfigService::ConfigAvailability available = 652
822 net::ProxyConfigService::CONFIG_UNSET; 653 // Determine new proxy config which may have changed because of new
823 // If network is shared but user doesn't use shared proxies, use direct mode. 654 // use-shared-proxies. If necessary, activate it.
824 if (network->profile_type() == PROFILE_SHARED && !use_shared_proxies_) { 655 Network* network = NULL;
825 VLOG(1) << "shared network and !use_shared_proxies, using direct"; 656 if (!active_network_.empty()) {
826 available = net::ProxyConfigService::CONFIG_VALID; 657 network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
827 } else if (!network->proxy_config().empty() && 658 active_network_);
828 active_config_.DeserializeForNetwork(network->proxy_config())) { 659 if (!network)
829 // Network is private or shared with user using shared proxies. 660 LOG(WARNING) << "can't find requested network " << active_network_;
830 VLOG(1) << "using network proxy: " << network->proxy_config();
831 available = net::ProxyConfigService::CONFIG_VALID;
832 } 661 }
833 IOSetProxyConfig(active_config_, available); 662 DetermineEffectiveConfig(network, true);
834 } 663 }
835 664
836 void ProxyConfigServiceImpl::SetCurrentNetworkName(const Network* network) { 665 void ProxyConfigServiceImpl::DetermineEffectiveConfig(const Network* network,
837 if (!network) { 666 bool activate) {
838 if (testing_) 667 // Get prefs proxy config if available.
839 current_ui_network_name_ = "test"; 668 net::ProxyConfig pref_config;
840 return; 669 ProxyPrefs::ConfigState pref_state = GetProxyConfig(&pref_config);
670
671 // Get network proxy config if available.
672 net::ProxyConfig network_config;
673 net::ProxyConfigService::ConfigAvailability network_availability =
674 net::ProxyConfigService::CONFIG_UNSET;
675 bool ignore_proxy = activate;
676 if (network) {
677 // If we're activating proxy, ignore proxy if necessary;
678 // otherwise, for ui, get actual proxy to show user.
679 ignore_proxy = activate ? IgnoreProxy(network) : false;
680 // If network is shared but use-shared-proxies is off, use direct mode.
681 if (ignore_proxy) {
682 VLOG(1) << this << ": shared network && !use-shared-proxies, use direct";
683 network_availability = net::ProxyConfigService::CONFIG_VALID;
684 } else if (!network->proxy_config().empty()) {
685 // Network is private or shared with user using shared proxies.
686 JSONStringValueSerializer serializer(network->proxy_config());
687 scoped_ptr<Value> value(serializer.Deserialize(NULL, NULL));
688 if (value.get() && value->GetType() == Value::TYPE_DICTIONARY) {
689 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get());
690 ProxyConfigDictionary proxy_dict(dict);
691 if (PrefConfigToNetConfig(proxy_dict, &network_config)) {
692 VLOG(1) << this << ": using network proxy: "
693 << network->proxy_config();
694 network_availability = net::ProxyConfigService::CONFIG_VALID;
695 }
696 }
697 }
841 } 698 }
842 if (network->name().empty() && network->type() == chromeos::TYPE_ETHERNET) { 699
843 current_ui_network_name_ = l10n_util::GetStringUTF8( 700 // Determine effective proxy config, either from prefs or network.
844 IDS_STATUSBAR_NETWORK_DEVICE_ETHERNET); 701 ProxyPrefs::ConfigState effective_config_state;
845 } else { 702 net::ProxyConfig effective_config;
846 current_ui_network_name_ = network->name(); 703 GetEffectiveProxyConfig(pref_state, pref_config,
704 network_availability, network_config, ignore_proxy,
705 &effective_config_state, &effective_config);
706
707 // Determine if we should activate effective proxy and which proxy config to
708 // store it.
709 if (activate) { // Activate effective proxy and store into |active_config_|.
710 // If last update didn't complete, we definitely update now.
711 bool update_now = update_pending();
712 if (!update_now) { // Otherwise, only update now if there're changes.
713 update_now = active_config_state_ != effective_config_state ||
714 (active_config_state_ != ProxyPrefs::CONFIG_UNSET &&
715 !active_config_.Equals(effective_config));
716 }
717 if (update_now) { // Activate and store new effective config.
718 active_config_state_ = effective_config_state;
719 if (active_config_state_ != ProxyPrefs::CONFIG_UNSET)
720 active_config_ = effective_config;
721 // If effective config is from system (i.e. network), it's considered a
722 // special kind of prefs that ranks below policy/extension but above
723 // others, so bump it up to CONFIG_OTHER_PRECEDE to force its precedence
724 // when PrefProxyConfigTracker pushes it to ChromeProxyConfigService.
725 if (effective_config_state == ProxyPrefs::CONFIG_SYSTEM)
726 effective_config_state = ProxyPrefs::CONFIG_OTHER_PRECEDE;
727 // If config is manual, add rule to bypass local host.
728 if (effective_config.proxy_rules().type !=
729 net::ProxyConfig::ProxyRules::TYPE_NO_RULES)
730 effective_config.proxy_rules().bypass_rules.AddRuleToBypassLocal();
731 PrefProxyConfigTracker::OnProxyConfigChanged(effective_config_state,
732 effective_config);
733 if (VLOG_IS_ON(1) && !update_pending()) { // Update was successful.
734 scoped_ptr<DictionaryValue> config_dict(static_cast<DictionaryValue*>(
735 effective_config.ToValue()));
736 std::string config_value;
737 JSONStringValueSerializer serializer(&config_value);
738 serializer.Serialize(*config_dict.get());
739 VLOG(1) << this << ": Proxy changed: "
740 << ConfigStateToString(active_config_state_)
741 << ", " << config_value;
742 }
743 }
744 } else { // For UI, store effective proxy into |current_ui_config_|.
745 current_ui_config_.FromNetProxyConfig(effective_config);
746 current_ui_config_.state = effective_config_state;
747 if (PrefPrecedes(effective_config_state))
748 current_ui_config_.user_modifiable = false;
749 else
750 current_ui_config_.user_modifiable = !network || !IgnoreProxy(network);
847 } 751 }
848 } 752 }
849 753
850 void ProxyConfigServiceImpl::CheckCurrentlyOnIOThread() { 754 void ProxyConfigServiceImpl::OnUISetCurrentNetwork(const Network* network) {
851 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 755 DetermineEffectiveConfig(network, false);
756 VLOG(1) << this << ": current ui network: "
757 << (network->name().empty() ?
758 current_ui_network_ : network->name())
759 << ", " << ModeToString(current_ui_config_.mode)
760 << ", " << ConfigStateToString(current_ui_config_.state)
761 << ", modifiable:" << current_ui_config_.user_modifiable;
852 } 762 }
853 763
854 void ProxyConfigServiceImpl::CheckCurrentlyOnUIThread() { 764 void ProxyConfigServiceImpl::ResetUICache() {
855 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 765 current_ui_network_.clear();
766 current_ui_config_ = ProxyConfig();
856 } 767 }
857 768
858 } // namespace chromeos 769 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698