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

Side by Side Diff: net/proxy/proxy_service.cc

Issue 10534132: NetLogEventParameter to Callback refactoring 7. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Update unit test Created 8 years, 6 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/proxy/proxy_service.h" 5 #include "net/proxy/proxy_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 326
327 static bool IsSupported() { 327 static bool IsSupported() {
328 #if defined(OS_WIN) || defined(OS_MACOSX) 328 #if defined(OS_WIN) || defined(OS_MACOSX)
329 return true; 329 return true;
330 #else 330 #else
331 return false; 331 return false;
332 #endif 332 #endif
333 } 333 }
334 }; 334 };
335 335
336 // NetLog parameter to describe a proxy configuration change. 336 // Returns NetLog parameter describing a proxy configuration change.
337 class ProxyConfigChangedNetLogParam : public NetLog::EventParameters { 337 Value* NetLogProxyConfigChangedCallback(const ProxyConfig* old_config,
338 public: 338 const ProxyConfig* new_config,
339 ProxyConfigChangedNetLogParam(const ProxyConfig& old_config, 339 NetLog::LogLevel /* log_level */) {
340 const ProxyConfig& new_config) 340 DictionaryValue* dict = new DictionaryValue();
341 : old_config_(old_config), 341 // The "old_config" is optional -- the first notification will not have
342 new_config_(new_config) { 342 // any "previous" configuration.
343 if (old_config->is_valid())
344 dict->Set("old_config", old_config->ToValue());
345 dict->Set("new_config", new_config->ToValue());
346 return dict;
347 }
348
349 Value* NetLogBadProxyListCallback(const ProxyRetryInfoMap* retry_info,
350 NetLog::LogLevel /* log_level */) {
351 DictionaryValue* dict = new DictionaryValue();
352 ListValue* list = new ListValue();
353
354 for (ProxyRetryInfoMap::const_iterator iter = retry_info->begin();
355 iter != retry_info->end(); ++iter) {
356 list->Append(Value::CreateStringValue(iter->first));
343 } 357 }
344 358 dict->Set("bad_proxy_list", list);
345 virtual Value* ToValue() const OVERRIDE { 359 return dict;
346 DictionaryValue* dict = new DictionaryValue(); 360 }
347 // The "old_config" is optional -- the first notification will not have
348 // any "previous" configuration.
349 if (old_config_.is_valid())
350 dict->Set("old_config", old_config_.ToValue());
351 dict->Set("new_config", new_config_.ToValue());
352 return dict;
353 }
354
355 protected:
356 virtual ~ProxyConfigChangedNetLogParam() {}
357
358 private:
359 const ProxyConfig old_config_;
360 const ProxyConfig new_config_;
361 DISALLOW_COPY_AND_ASSIGN(ProxyConfigChangedNetLogParam);
362 };
363
364 class BadProxyListNetLogParam : public NetLog::EventParameters {
365 public:
366 BadProxyListNetLogParam(const ProxyRetryInfoMap& retry_info) {
367 proxy_list_.reserve(retry_info.size());
368 for (ProxyRetryInfoMap::const_iterator iter = retry_info.begin();
369 iter != retry_info.end(); ++iter) {
370 proxy_list_.push_back(iter->first);
371 }
372 }
373
374 virtual Value* ToValue() const OVERRIDE {
375 DictionaryValue* dict = new DictionaryValue();
376 ListValue* list = new ListValue();
377 for (std::vector<std::string>::const_iterator iter = proxy_list_.begin();
378 iter != proxy_list_.end(); ++iter)
379 list->Append(Value::CreateStringValue(*iter));
380 dict->Set("bad_proxy_list", list);
381 return dict;
382 }
383
384 protected:
385 virtual ~BadProxyListNetLogParam() {}
386
387 private:
388 std::vector<std::string> proxy_list_;
389 DISALLOW_COPY_AND_ASSIGN(BadProxyListNetLogParam);
390 };
391 361
392 #if defined(OS_CHROMEOS) 362 #if defined(OS_CHROMEOS)
393 class UnsetProxyConfigService : public ProxyConfigService { 363 class UnsetProxyConfigService : public ProxyConfigService {
394 public: 364 public:
395 UnsetProxyConfigService() {} 365 UnsetProxyConfigService() {}
396 virtual ~UnsetProxyConfigService() {} 366 virtual ~UnsetProxyConfigService() {}
397 367
398 virtual void AddObserver(Observer* observer) {} 368 virtual void AddObserver(Observer* observer) {}
399 virtual void RemoveObserver(Observer* observer) {} 369 virtual void RemoveObserver(Observer* observer) {}
400 virtual ConfigAvailability GetLatestProxyConfig(ProxyConfig* config) { 370 virtual ConfigAvailability GetLatestProxyConfig(ProxyConfig* config) {
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
839 809
840 void CancelResolveJob() { 810 void CancelResolveJob() {
841 DCHECK(is_started()); 811 DCHECK(is_started());
842 // The request may already be running in the resolver. 812 // The request may already be running in the resolver.
843 resolver()->CancelRequest(resolve_job_); 813 resolver()->CancelRequest(resolve_job_);
844 resolve_job_ = NULL; 814 resolve_job_ = NULL;
845 DCHECK(!is_started()); 815 DCHECK(!is_started());
846 } 816 }
847 817
848 void Cancel() { 818 void Cancel() {
849 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); 819 net_log_.AddEvent(NetLog::TYPE_CANCELLED);
850 820
851 if (is_started()) 821 if (is_started())
852 CancelResolveJob(); 822 CancelResolveJob();
853 823
854 // Mark as cancelled, to prevent accessing this again later. 824 // Mark as cancelled, to prevent accessing this again later.
855 service_ = NULL; 825 service_ = NULL;
856 user_callback_.Reset(); 826 user_callback_.Reset();
857 results_ = NULL; 827 results_ = NULL;
858 828
859 net_log_.EndEvent(NetLog::TYPE_PROXY_SERVICE, NULL); 829 net_log_.EndEvent(NetLog::TYPE_PROXY_SERVICE);
860 } 830 }
861 831
862 // Returns true if Cancel() has been called. 832 // Returns true if Cancel() has been called.
863 bool was_cancelled() const { 833 bool was_cancelled() const {
864 return user_callback_.is_null(); 834 return user_callback_.is_null();
865 } 835 }
866 836
867 // Helper to call after ProxyResolver completion (both synchronous and 837 // Helper to call after ProxyResolver completion (both synchronous and
868 // asynchronous). Fixes up the result that is to be returned to user. 838 // asynchronous). Fixes up the result that is to be returned to user.
869 int QueryDidComplete(int result_code) { 839 int QueryDidComplete(int result_code) {
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1052 } 1022 }
1053 1023
1054 int ProxyService::ResolveProxy(const GURL& raw_url, 1024 int ProxyService::ResolveProxy(const GURL& raw_url,
1055 ProxyInfo* result, 1025 ProxyInfo* result,
1056 const net::CompletionCallback& callback, 1026 const net::CompletionCallback& callback,
1057 PacRequest** pac_request, 1027 PacRequest** pac_request,
1058 const BoundNetLog& net_log) { 1028 const BoundNetLog& net_log) {
1059 DCHECK(CalledOnValidThread()); 1029 DCHECK(CalledOnValidThread());
1060 DCHECK(!callback.is_null()); 1030 DCHECK(!callback.is_null());
1061 1031
1062 net_log.BeginEvent(NetLog::TYPE_PROXY_SERVICE, NULL); 1032 net_log.BeginEvent(NetLog::TYPE_PROXY_SERVICE);
1063 1033
1064 // Notify our polling-based dependencies that a resolve is taking place. 1034 // Notify our polling-based dependencies that a resolve is taking place.
1065 // This way they can schedule their polls in response to network activity. 1035 // This way they can schedule their polls in response to network activity.
1066 config_service_->OnLazyPoll(); 1036 config_service_->OnLazyPoll();
1067 if (script_poller_.get()) 1037 if (script_poller_.get())
1068 script_poller_->OnLazyPoll(); 1038 script_poller_->OnLazyPoll();
1069 1039
1070 if (current_state_ == STATE_NONE) 1040 if (current_state_ == STATE_NONE)
1071 ApplyProxyConfigIfAvailable(); 1041 ApplyProxyConfigIfAvailable();
1072 1042
1073 // Strip away any reference fragments and the username/password, as they 1043 // Strip away any reference fragments and the username/password, as they
1074 // are not relevant to proxy resolution. 1044 // are not relevant to proxy resolution.
1075 GURL url = SimplifyUrlForRequest(raw_url); 1045 GURL url = SimplifyUrlForRequest(raw_url);
1076 1046
1077 // Check if the request can be completed right away. (This is the case when 1047 // Check if the request can be completed right away. (This is the case when
1078 // using a direct connection for example). 1048 // using a direct connection for example).
1079 int rv = TryToCompleteSynchronously(url, result); 1049 int rv = TryToCompleteSynchronously(url, result);
1080 if (rv != ERR_IO_PENDING) 1050 if (rv != ERR_IO_PENDING)
1081 return DidFinishResolvingProxy(result, rv, net_log); 1051 return DidFinishResolvingProxy(result, rv, net_log);
1082 1052
1083 scoped_refptr<PacRequest> req( 1053 scoped_refptr<PacRequest> req(
1084 new PacRequest(this, url, result, callback, net_log)); 1054 new PacRequest(this, url, result, callback, net_log));
1085 1055
1086 if (current_state_ == STATE_READY) { 1056 if (current_state_ == STATE_READY) {
1087 // Start the resolve request. 1057 // Start the resolve request.
1088 rv = req->Start(); 1058 rv = req->Start();
1089 if (rv != ERR_IO_PENDING) 1059 if (rv != ERR_IO_PENDING)
1090 return req->QueryDidComplete(rv); 1060 return req->QueryDidComplete(rv);
1091 } else { 1061 } else {
1092 req->net_log()->BeginEvent(NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC, 1062 req->net_log()->BeginEvent(NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC);
1093 NULL);
1094 } 1063 }
1095 1064
1096 DCHECK_EQ(ERR_IO_PENDING, rv); 1065 DCHECK_EQ(ERR_IO_PENDING, rv);
1097 DCHECK(!ContainsPendingRequest(req)); 1066 DCHECK(!ContainsPendingRequest(req));
1098 pending_requests_.push_back(req); 1067 pending_requests_.push_back(req);
1099 1068
1100 // Completion will be notified through |callback|, unless the caller cancels 1069 // Completion will be notified through |callback|, unless the caller cancels
1101 // the request using |pac_request|. 1070 // the request using |pac_request|.
1102 if (pac_request) 1071 if (pac_request)
1103 *pac_request = req.get(); 1072 *pac_request = req.get();
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1142 1111
1143 void ProxyService::SuspendAllPendingRequests() { 1112 void ProxyService::SuspendAllPendingRequests() {
1144 for (PendingRequests::iterator it = pending_requests_.begin(); 1113 for (PendingRequests::iterator it = pending_requests_.begin();
1145 it != pending_requests_.end(); 1114 it != pending_requests_.end();
1146 ++it) { 1115 ++it) {
1147 PacRequest* req = it->get(); 1116 PacRequest* req = it->get();
1148 if (req->is_started()) { 1117 if (req->is_started()) {
1149 req->CancelResolveJob(); 1118 req->CancelResolveJob();
1150 1119
1151 req->net_log()->BeginEvent( 1120 req->net_log()->BeginEvent(
1152 NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC, NULL); 1121 NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC);
1153 } 1122 }
1154 } 1123 }
1155 } 1124 }
1156 1125
1157 void ProxyService::SetReady() { 1126 void ProxyService::SetReady() {
1158 DCHECK(!init_proxy_resolver_.get()); 1127 DCHECK(!init_proxy_resolver_.get());
1159 current_state_ = STATE_READY; 1128 current_state_ = STATE_READY;
1160 1129
1161 // Make a copy in case |this| is deleted during the synchronous completion 1130 // Make a copy in case |this| is deleted during the synchronous completion
1162 // of one of the requests. If |this| is deleted then all of the PacRequest 1131 // of one of the requests. If |this| is deleted then all of the PacRequest
1163 // instances will be Cancel()-ed. 1132 // instances will be Cancel()-ed.
1164 PendingRequests pending_copy = pending_requests_; 1133 PendingRequests pending_copy = pending_requests_;
1165 1134
1166 for (PendingRequests::iterator it = pending_copy.begin(); 1135 for (PendingRequests::iterator it = pending_copy.begin();
1167 it != pending_copy.end(); 1136 it != pending_copy.end();
1168 ++it) { 1137 ++it) {
1169 PacRequest* req = it->get(); 1138 PacRequest* req = it->get();
1170 if (!req->is_started() && !req->was_cancelled()) { 1139 if (!req->is_started() && !req->was_cancelled()) {
1171 req->net_log()->EndEvent(NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC, 1140 req->net_log()->EndEvent(NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC);
1172 NULL);
1173 1141
1174 // Note that we re-check for synchronous completion, in case we are 1142 // Note that we re-check for synchronous completion, in case we are
1175 // no longer using a ProxyResolver (can happen if we fell-back to manual). 1143 // no longer using a ProxyResolver (can happen if we fell-back to manual).
1176 req->StartAndCompleteCheckingForSynchronous(); 1144 req->StartAndCompleteCheckingForSynchronous();
1177 } 1145 }
1178 } 1146 }
1179 } 1147 }
1180 1148
1181 void ProxyService::ApplyProxyConfigIfAvailable() { 1149 void ProxyService::ApplyProxyConfigIfAvailable() {
1182 DCHECK_EQ(STATE_NONE, current_state_); 1150 DCHECK_EQ(STATE_NONE, current_state_);
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1292 iter != new_retry_info.end(); ++iter) { 1260 iter != new_retry_info.end(); ++iter) {
1293 ProxyRetryInfoMap::iterator existing = proxy_retry_info_.find(iter->first); 1261 ProxyRetryInfoMap::iterator existing = proxy_retry_info_.find(iter->first);
1294 if (existing == proxy_retry_info_.end()) 1262 if (existing == proxy_retry_info_.end())
1295 proxy_retry_info_[iter->first] = iter->second; 1263 proxy_retry_info_[iter->first] = iter->second;
1296 else if (existing->second.bad_until < iter->second.bad_until) 1264 else if (existing->second.bad_until < iter->second.bad_until)
1297 existing->second.bad_until = iter->second.bad_until; 1265 existing->second.bad_until = iter->second.bad_until;
1298 } 1266 }
1299 if (net_log_) { 1267 if (net_log_) {
1300 net_log_->AddGlobalEntry( 1268 net_log_->AddGlobalEntry(
1301 NetLog::TYPE_BAD_PROXY_LIST_REPORTED, 1269 NetLog::TYPE_BAD_PROXY_LIST_REPORTED,
1302 make_scoped_refptr( 1270 base::Bind(&NetLogBadProxyListCallback, &new_retry_info));
1303 new BadProxyListNetLogParam(new_retry_info)));
1304 } 1271 }
1305 } 1272 }
1306 1273
1307 void ProxyService::CancelPacRequest(PacRequest* req) { 1274 void ProxyService::CancelPacRequest(PacRequest* req) {
1308 DCHECK(CalledOnValidThread()); 1275 DCHECK(CalledOnValidThread());
1309 DCHECK(req); 1276 DCHECK(req);
1310 req->Cancel(); 1277 req->Cancel();
1311 RemovePendingRequest(req); 1278 RemovePendingRequest(req);
1312 } 1279 }
1313 1280
(...skipping 15 matching lines...) Expand all
1329 pending_requests_.erase(it); 1296 pending_requests_.erase(it);
1330 } 1297 }
1331 1298
1332 int ProxyService::DidFinishResolvingProxy(ProxyInfo* result, 1299 int ProxyService::DidFinishResolvingProxy(ProxyInfo* result,
1333 int result_code, 1300 int result_code,
1334 const BoundNetLog& net_log) { 1301 const BoundNetLog& net_log) {
1335 // Log the result of the proxy resolution. 1302 // Log the result of the proxy resolution.
1336 if (result_code == OK) { 1303 if (result_code == OK) {
1337 // When logging all events is enabled, dump the proxy list. 1304 // When logging all events is enabled, dump the proxy list.
1338 if (net_log.IsLoggingAllEvents()) { 1305 if (net_log.IsLoggingAllEvents()) {
1306 std::string pac_string = result->ToPacString();
eroman 2012/06/13 18:55:33 Can this be internalized into the NetLog callback?
mmenke 2012/06/13 20:27:58 Done. Didn't do it before because of the "IsLoggi
1339 net_log.AddEvent( 1307 net_log.AddEvent(
1340 NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST, 1308 NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST,
1341 make_scoped_refptr(new NetLogStringParameter( 1309 NetLog::StringCallback("pac_string", &pac_string));
1342 "pac_string", result->ToPacString())));
1343 } 1310 }
1344 result->DeprioritizeBadProxies(proxy_retry_info_); 1311 result->DeprioritizeBadProxies(proxy_retry_info_);
1345 } else { 1312 } else {
1346 net_log.AddEvent( 1313 net_log.AddEventWithNetErrorCode(
1347 NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST, 1314 NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST, result_code);
1348 make_scoped_refptr(new NetLogIntegerParameter(
1349 "net_error", result_code)));
1350 1315
1351 if (!config_.pac_mandatory()) { 1316 if (!config_.pac_mandatory()) {
1352 // Fall-back to direct when the proxy resolver fails. This corresponds 1317 // Fall-back to direct when the proxy resolver fails. This corresponds
1353 // with a javascript runtime error in the PAC script. 1318 // with a javascript runtime error in the PAC script.
1354 // 1319 //
1355 // This implicit fall-back to direct matches Firefox 3.5 and 1320 // This implicit fall-back to direct matches Firefox 3.5 and
1356 // Internet Explorer 8. For more information, see: 1321 // Internet Explorer 8. For more information, see:
1357 // 1322 //
1358 // http://www.chromium.org/developers/design-documents/proxy-settings-fall back 1323 // http://www.chromium.org/developers/design-documents/proxy-settings-fall back
1359 result->UseDirect(); 1324 result->UseDirect();
1360 result_code = OK; 1325 result_code = OK;
1361 } else { 1326 } else {
1362 result_code = ERR_MANDATORY_PROXY_CONFIGURATION_FAILED; 1327 result_code = ERR_MANDATORY_PROXY_CONFIGURATION_FAILED;
1363 } 1328 }
1364 } 1329 }
1365 1330
1366 net_log.EndEvent(NetLog::TYPE_PROXY_SERVICE, NULL); 1331 net_log.EndEvent(NetLog::TYPE_PROXY_SERVICE);
1367 return result_code; 1332 return result_code;
1368 } 1333 }
1369 1334
1370 void ProxyService::SetProxyScriptFetchers( 1335 void ProxyService::SetProxyScriptFetchers(
1371 ProxyScriptFetcher* proxy_script_fetcher, 1336 ProxyScriptFetcher* proxy_script_fetcher,
1372 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher) { 1337 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher) {
1373 DCHECK(CalledOnValidThread()); 1338 DCHECK(CalledOnValidThread());
1374 State previous_state = ResetProxyConfig(false); 1339 State previous_state = ResetProxyConfig(false);
1375 proxy_script_fetcher_.reset(proxy_script_fetcher); 1340 proxy_script_fetcher_.reset(proxy_script_fetcher);
1376 dhcp_proxy_script_fetcher_.reset(dhcp_proxy_script_fetcher); 1341 dhcp_proxy_script_fetcher_.reset(dhcp_proxy_script_fetcher);
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1502 case ProxyConfigService::CONFIG_VALID: 1467 case ProxyConfigService::CONFIG_VALID:
1503 effective_config = config; 1468 effective_config = config;
1504 break; 1469 break;
1505 case ProxyConfigService::CONFIG_UNSET: 1470 case ProxyConfigService::CONFIG_UNSET:
1506 effective_config = ProxyConfig::CreateDirect(); 1471 effective_config = ProxyConfig::CreateDirect();
1507 break; 1472 break;
1508 } 1473 }
1509 1474
1510 // Emit the proxy settings change to the NetLog stream. 1475 // Emit the proxy settings change to the NetLog stream.
1511 if (net_log_) { 1476 if (net_log_) {
1512 scoped_refptr<NetLog::EventParameters> params( 1477 net_log_->AddGlobalEntry(
1513 new ProxyConfigChangedNetLogParam(fetched_config_, effective_config)); 1478 net::NetLog::TYPE_PROXY_CONFIG_CHANGED,
1514 net_log_->AddGlobalEntry(net::NetLog::TYPE_PROXY_CONFIG_CHANGED, 1479 base::Bind(&NetLogProxyConfigChangedCallback,
1515 params); 1480 &fetched_config_, &effective_config));
1516 } 1481 }
1517 1482
1518 // Set the new configuration as the most recently fetched one. 1483 // Set the new configuration as the most recently fetched one.
1519 fetched_config_ = effective_config; 1484 fetched_config_ = effective_config;
1520 fetched_config_.set_id(1); // Needed for a later DCHECK of is_valid(). 1485 fetched_config_.set_id(1); // Needed for a later DCHECK of is_valid().
1521 1486
1522 InitializeUsingLastFetchedConfig(); 1487 InitializeUsingLastFetchedConfig();
1523 } 1488 }
1524 1489
1525 void ProxyService::InitializeUsingLastFetchedConfig() { 1490 void ProxyService::InitializeUsingLastFetchedConfig() {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
1657 OnCompletion(result_); 1622 OnCompletion(result_);
1658 } 1623 }
1659 } 1624 }
1660 1625
1661 void SyncProxyServiceHelper::OnCompletion(int rv) { 1626 void SyncProxyServiceHelper::OnCompletion(int rv) {
1662 result_ = rv; 1627 result_ = rv;
1663 event_.Signal(); 1628 event_.Signal();
1664 } 1629 }
1665 1630
1666 } // namespace net 1631 } // namespace net
OLDNEW
« net/proxy/proxy_script_decider.cc ('K') | « net/proxy/proxy_script_decider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698