OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ios/web/net/request_tracker_impl.h" | 5 #include "ios/web/net/request_tracker_impl.h" |
6 | 6 |
7 #include <pthread.h> | 7 #include <pthread.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/mac/bind_objc_block.h" | 15 #include "base/mac/bind_objc_block.h" |
16 #include "base/mac/scoped_nsobject.h" | 16 #include "base/mac/scoped_nsobject.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
19 #include "base/strings/sys_string_conversions.h" | 19 #include "base/strings/sys_string_conversions.h" |
20 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
21 #import "ios/net/clients/crn_forwarding_network_client.h" | 21 #import "ios/net/clients/crn_forwarding_network_client.h" |
22 #import "ios/net/clients/crn_forwarding_network_client_factory.h" | 22 #import "ios/net/clients/crn_forwarding_network_client_factory.h" |
23 #import "ios/web/crw_network_activity_indicator_manager.h" | |
24 #import "ios/web/history_state_util.h" | 23 #import "ios/web/history_state_util.h" |
25 #import "ios/web/net/crw_request_tracker_delegate.h" | 24 #import "ios/web/net/crw_request_tracker_delegate.h" |
26 #include "ios/web/public/browser_state.h" | 25 #include "ios/web/public/browser_state.h" |
27 #include "ios/web/public/certificate_policy_cache.h" | 26 #include "ios/web/public/certificate_policy_cache.h" |
28 #include "ios/web/public/ssl_status.h" | 27 #include "ios/web/public/ssl_status.h" |
29 #include "ios/web/public/url_util.h" | 28 #include "ios/web/public/url_util.h" |
30 #include "ios/web/public/web_thread.h" | 29 #include "ios/web/public/web_thread.h" |
31 #import "net/base/mac/url_conversions.h" | 30 #import "net/base/mac/url_conversions.h" |
32 #include "net/base/net_errors.h" | 31 #include "net/base/net_errors.h" |
33 #include "net/http/http_response_headers.h" | 32 #include "net/http/http_response_headers.h" |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 net::URLRequestContext* RequestTrackerImpl::GetRequestContext() { | 472 net::URLRequestContext* RequestTrackerImpl::GetRequestContext() { |
474 DCHECK_CURRENTLY_ON(web::WebThread::IO); | 473 DCHECK_CURRENTLY_ON(web::WebThread::IO); |
475 return request_context_getter_->GetURLRequestContext(); | 474 return request_context_getter_->GetURLRequestContext(); |
476 } | 475 } |
477 | 476 |
478 void RequestTrackerImpl::StartRequest(net::URLRequest* request) { | 477 void RequestTrackerImpl::StartRequest(net::URLRequest* request) { |
479 DCHECK_CURRENTLY_ON(web::WebThread::IO); | 478 DCHECK_CURRENTLY_ON(web::WebThread::IO); |
480 DCHECK(!counts_by_request_.count(request)); | 479 DCHECK(!counts_by_request_.count(request)); |
481 DCHECK_EQ(is_for_static_file_requests_, request->url().SchemeIsFile()); | 480 DCHECK_EQ(is_for_static_file_requests_, request->url().SchemeIsFile()); |
482 | 481 |
483 bool addedRequest = live_requests_.insert(request).second; | |
484 if (!is_for_static_file_requests_ && addedRequest) { | |
485 NSString* networkActivityKey = GetNetworkActivityKey(); | |
486 web::WebThread::PostTask( | |
487 web::WebThread::UI, FROM_HERE, base::BindBlockArc(^{ | |
488 [[CRWNetworkActivityIndicatorManager sharedInstance] | |
489 startNetworkTaskForGroup:networkActivityKey]; | |
490 })); | |
491 } | |
492 | |
493 if (new_estimate_round_) { | 482 if (new_estimate_round_) { |
494 // Starting a new estimate round. Ignore the previous requests for the | 483 // Starting a new estimate round. Ignore the previous requests for the |
495 // calculation. | 484 // calculation. |
496 counts_by_request_.clear(); | 485 counts_by_request_.clear(); |
497 estimate_start_index_ = counts_.size(); | 486 estimate_start_index_ = counts_.size(); |
498 new_estimate_round_ = false; | 487 new_estimate_round_ = false; |
499 } | 488 } |
500 const GURL& url = request->original_url(); | 489 const GURL& url = request->original_url(); |
501 TrackerCounts* counts = new TrackerCounts( | 490 TrackerCounts* counts = new TrackerCounts( |
502 GURLByRemovingRefFromGURL(url), request); | 491 GURLByRemovingRefFromGURL(url), request); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 // Something is wrong with the estimate, it is too low. Ignore it. | 542 // Something is wrong with the estimate, it is too low. Ignore it. |
554 counts->expected_length = 0; | 543 counts->expected_length = 0; |
555 } | 544 } |
556 Notify(); | 545 Notify(); |
557 } | 546 } |
558 } | 547 } |
559 | 548 |
560 void RequestTrackerImpl::StopRequest(net::URLRequest* request) { | 549 void RequestTrackerImpl::StopRequest(net::URLRequest* request) { |
561 DCHECK_CURRENTLY_ON(web::WebThread::IO); | 550 DCHECK_CURRENTLY_ON(web::WebThread::IO); |
562 | 551 |
563 int removedRequests = live_requests_.erase(request); | |
564 if (!is_for_static_file_requests_ && removedRequests > 0) { | |
565 NSString* networkActivityKey = GetNetworkActivityKey(); | |
566 web::WebThread::PostTask( | |
567 web::WebThread::UI, FROM_HERE, base::BindBlockArc(^{ | |
568 [[CRWNetworkActivityIndicatorManager sharedInstance] | |
569 stopNetworkTaskForGroup:networkActivityKey]; | |
570 })); | |
571 } | |
572 | |
573 if (counts_by_request_.count(request)) { | 552 if (counts_by_request_.count(request)) { |
574 StopRedirectedRequest(request); | 553 StopRedirectedRequest(request); |
575 Notify(); | 554 Notify(); |
576 } | 555 } |
577 } | 556 } |
578 | 557 |
579 void RequestTrackerImpl::StopRedirectedRequest(net::URLRequest* request) { | 558 void RequestTrackerImpl::StopRedirectedRequest(net::URLRequest* request) { |
580 DCHECK_CURRENTLY_ON(web::WebThread::IO); | 559 DCHECK_CURRENTLY_ON(web::WebThread::IO); |
581 | 560 |
582 int removedRequests = live_requests_.erase(request); | |
583 if (!is_for_static_file_requests_ && removedRequests > 0) { | |
584 NSString* networkActivityKey = GetNetworkActivityKey(); | |
585 web::WebThread::PostTask( | |
586 web::WebThread::UI, FROM_HERE, base::BindBlockArc(^{ | |
587 [[CRWNetworkActivityIndicatorManager sharedInstance] | |
588 stopNetworkTaskForGroup:networkActivityKey]; | |
589 })); | |
590 } | |
591 | |
592 if (counts_by_request_.count(request)) { | 561 if (counts_by_request_.count(request)) { |
593 TrackerCounts* counts = counts_by_request_[request]; | 562 TrackerCounts* counts = counts_by_request_[request]; |
594 DCHECK(!counts->done); | 563 DCHECK(!counts->done); |
595 const net::SSLInfo& ssl_info = request->ssl_info(); | 564 const net::SSLInfo& ssl_info = request->ssl_info(); |
596 if (ssl_info.is_valid()) | 565 if (ssl_info.is_valid()) |
597 counts->ssl_info = ssl_info; | 566 counts->ssl_info = ssl_info; |
598 counts->done = true; | 567 counts->done = true; |
599 counts_by_request_.erase(request); | 568 counts_by_request_.erase(request); |
600 } | 569 } |
601 } | 570 } |
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1267 ScopedVector<TrackerCounts>::iterator it; | 1236 ScopedVector<TrackerCounts>::iterator it; |
1268 for (it = counts_.begin(); it != counts_.end(); ++it) | 1237 for (it = counts_.begin(); it != counts_.end(); ++it) |
1269 [urls addObject:(*it)->Description()]; | 1238 [urls addObject:(*it)->Description()]; |
1270 | 1239 |
1271 return [NSString stringWithFormat:@"RequestGroupID %@\n%@\n%@", | 1240 return [NSString stringWithFormat:@"RequestGroupID %@\n%@\n%@", |
1272 request_group_id_.get(), | 1241 request_group_id_.get(), |
1273 net::NSURLWithGURL(page_url_), | 1242 net::NSURLWithGURL(page_url_), |
1274 [urls componentsJoinedByString:@"\n"]]; | 1243 [urls componentsJoinedByString:@"\n"]]; |
1275 } | 1244 } |
1276 | 1245 |
1277 NSString* RequestTrackerImpl::GetNetworkActivityKey() { | |
1278 return [NSString | |
1279 stringWithFormat:@"RequestTrackerImpl.NetworkActivityIndicatorKey.%@", | |
1280 request_group_id_.get()]; | |
1281 } | |
1282 | |
1283 void RequestTrackerImpl::CancelRequests() { | 1246 void RequestTrackerImpl::CancelRequests() { |
1284 DCHECK_CURRENTLY_ON(web::WebThread::IO); | 1247 DCHECK_CURRENTLY_ON(web::WebThread::IO); |
1285 std::set<net::URLRequest*>::iterator it; | 1248 std::set<net::URLRequest*>::iterator it; |
1286 // TODO(droger): When canceling the request, we should in theory make sure | 1249 // TODO(droger): When canceling the request, we should in theory make sure |
1287 // that the NSURLProtocol client method |didFailWithError| is called, | 1250 // that the NSURLProtocol client method |didFailWithError| is called, |
1288 // otherwise the iOS system may wait indefinitely for the request to complete. | 1251 // otherwise the iOS system may wait indefinitely for the request to complete. |
1289 // However, as we currently only cancel the requests when closing a tab, the | 1252 // However, as we currently only cancel the requests when closing a tab, the |
1290 // requests are all canceled by the system shortly after and nothing bad | 1253 // requests are all canceled by the system shortly after and nothing bad |
1291 // happens. | 1254 // happens. |
1292 for (it = live_requests_.begin(); it != live_requests_.end(); ++it) | 1255 for (it = live_requests_.begin(); it != live_requests_.end(); ++it) |
1293 (*it)->Cancel(); | 1256 (*it)->Cancel(); |
1294 | 1257 |
1295 int removedRequests = live_requests_.size(); | |
1296 live_requests_.clear(); | 1258 live_requests_.clear(); |
1297 if (!is_for_static_file_requests_ && removedRequests > 0) { | |
1298 NSString* networkActivityKey = GetNetworkActivityKey(); | |
1299 web::WebThread::PostTask( | |
1300 web::WebThread::UI, FROM_HERE, base::BindBlockArc(^{ | |
1301 [[CRWNetworkActivityIndicatorManager sharedInstance] | |
1302 clearNetworkTasksForGroup:networkActivityKey]; | |
1303 })); | |
1304 } | |
1305 } | 1259 } |
1306 | 1260 |
1307 void RequestTrackerImpl::SetCertificatePolicyCacheForTest( | 1261 void RequestTrackerImpl::SetCertificatePolicyCacheForTest( |
1308 web::CertificatePolicyCache* cache) { | 1262 web::CertificatePolicyCache* cache) { |
1309 policy_cache_ = cache; | 1263 policy_cache_ = cache; |
1310 } | 1264 } |
1311 | 1265 |
1312 } // namespace web | 1266 } // namespace web |
OLD | NEW |