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

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

Powered by Google App Engine
This is Rietveld 408576698