Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/url_request/url_request_job.h" | 5 #include "net/url_request/url_request_job.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 NetworkDelegate* network_delegate) | 65 NetworkDelegate* network_delegate) |
| 66 : request_(request), | 66 : request_(request), |
| 67 done_(false), | 67 done_(false), |
| 68 prefilter_bytes_read_(0), | 68 prefilter_bytes_read_(0), |
| 69 postfilter_bytes_read_(0), | 69 postfilter_bytes_read_(0), |
| 70 filter_needs_more_output_space_(false), | 70 filter_needs_more_output_space_(false), |
| 71 filtered_read_buffer_len_(0), | 71 filtered_read_buffer_len_(0), |
| 72 has_handled_response_(false), | 72 has_handled_response_(false), |
| 73 expected_content_size_(-1), | 73 expected_content_size_(-1), |
| 74 network_delegate_(network_delegate), | 74 network_delegate_(network_delegate), |
| 75 last_notified_total_received_bytes_(0), | |
| 75 weak_factory_(this) { | 76 weak_factory_(this) { |
| 76 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); | 77 base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); |
| 77 if (power_monitor) | 78 if (power_monitor) |
| 78 power_monitor->AddObserver(this); | 79 power_monitor->AddObserver(this); |
| 79 } | 80 } |
| 80 | 81 |
| 81 void URLRequestJob::SetUpload(UploadDataStream* upload) { | 82 void URLRequestJob::SetUpload(UploadDataStream* upload) { |
| 82 } | 83 } |
| 83 | 84 |
| 84 void URLRequestJob::SetExtraRequestHeaders(const HttpRequestHeaders& headers) { | 85 void URLRequestJob::SetExtraRequestHeaders(const HttpRequestHeaders& headers) { |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 request_->response_info_.response_time = base::Time::Now(); | 389 request_->response_info_.response_time = base::Time::Now(); |
| 389 GetResponseInfo(&request_->response_info_); | 390 GetResponseInfo(&request_->response_info_); |
| 390 | 391 |
| 391 // When notifying the delegate, the delegate can release the request | 392 // When notifying the delegate, the delegate can release the request |
| 392 // (and thus release 'this'). After calling to the delgate, we must | 393 // (and thus release 'this'). After calling to the delgate, we must |
| 393 // check the request pointer to see if it still exists, and return | 394 // check the request pointer to see if it still exists, and return |
| 394 // immediately if it has been destroyed. self_preservation ensures our | 395 // immediately if it has been destroyed. self_preservation ensures our |
| 395 // survival until we can get out of this method. | 396 // survival until we can get out of this method. |
| 396 scoped_refptr<URLRequestJob> self_preservation(this); | 397 scoped_refptr<URLRequestJob> self_preservation(this); |
| 397 | 398 |
| 399 MaybeNotifyNetworkBytes(); | |
| 400 | |
| 398 if (request_) | 401 if (request_) |
| 399 request_->OnHeadersComplete(); | 402 request_->OnHeadersComplete(); |
| 400 | 403 |
| 401 GURL new_location; | 404 GURL new_location; |
| 402 int http_status_code; | 405 int http_status_code; |
| 403 if (IsRedirectResponse(&new_location, &http_status_code)) { | 406 if (IsRedirectResponse(&new_location, &http_status_code)) { |
| 404 // Redirect response bodies are not read. Notify the transaction | 407 // Redirect response bodies are not read. Notify the transaction |
| 405 // so it does not treat being stopped as an error. | 408 // so it does not treat being stopped as an error. |
| 406 DoneReadingRedirectResponse(); | 409 DoneReadingRedirectResponse(); |
| 407 | 410 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 560 UMA_HISTOGRAM_BOOLEAN("Net.ErrorResponseHasContentMainFrame", | 563 UMA_HISTOGRAM_BOOLEAN("Net.ErrorResponseHasContentMainFrame", |
| 561 page_has_content); | 564 page_has_content); |
| 562 } else { | 565 } else { |
| 563 UMA_HISTOGRAM_BOOLEAN("Net.ErrorResponseHasContentNonMainFrame", | 566 UMA_HISTOGRAM_BOOLEAN("Net.ErrorResponseHasContentNonMainFrame", |
| 564 page_has_content); | 567 page_has_content); |
| 565 } | 568 } |
| 566 } | 569 } |
| 567 } | 570 } |
| 568 } | 571 } |
| 569 | 572 |
| 573 MaybeNotifyNetworkBytes(); | |
| 574 | |
| 570 // Complete this notification later. This prevents us from re-entering the | 575 // Complete this notification later. This prevents us from re-entering the |
| 571 // delegate if we're done because of a synchronous call. | 576 // delegate if we're done because of a synchronous call. |
| 572 base::ThreadTaskRunnerHandle::Get()->PostTask( | 577 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 573 FROM_HERE, base::Bind(&URLRequestJob::CompleteNotifyDone, | 578 FROM_HERE, base::Bind(&URLRequestJob::CompleteNotifyDone, |
| 574 weak_factory_.GetWeakPtr())); | 579 weak_factory_.GetWeakPtr())); |
| 575 } | 580 } |
| 576 | 581 |
| 577 void URLRequestJob::CompleteNotifyDone() { | 582 void URLRequestJob::CompleteNotifyDone() { |
| 578 // Check if we should notify the delegate that we're done because of an error. | 583 // Check if we should notify the delegate that we're done because of an error. |
| 579 if (request_ && | 584 if (request_ && |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 771 request_->status().is_success() || | 776 request_->status().is_success() || |
| 772 (!status.is_success() && !status.is_io_pending())); | 777 (!status.is_success() && !status.is_io_pending())); |
| 773 request_->set_status(status); | 778 request_->set_status(status); |
| 774 } | 779 } |
| 775 } | 780 } |
| 776 | 781 |
| 777 void URLRequestJob::SetProxyServer(const HostPortPair& proxy_server) { | 782 void URLRequestJob::SetProxyServer(const HostPortPair& proxy_server) { |
| 778 request_->proxy_server_ = proxy_server; | 783 request_->proxy_server_ = proxy_server; |
| 779 } | 784 } |
| 780 | 785 |
| 786 void URLRequestJob::MaybeNotifyNetworkBytes() { | |
| 787 if (!request_ || !network_delegate_) | |
| 788 return; | |
| 789 | |
| 790 int64_t total_received_bytes = GetTotalReceivedBytes(); | |
| 791 // For URLRequestHttpJobs, |total_received_bytes| will reset to 0 if the | |
| 792 // transaction is killed, such as when a request is cancelled. Check for that | |
| 793 // here to avoid reporting negative network usage in these cases. Note that | |
| 794 // URLRequestHttpJob calls MaybeNotifyNetworkBytes shortly before killing the | |
| 795 // transaction in these cases. | |
|
mmenke
2015/08/19 15:48:41
Having the class have to know about that behavior
sclittle
2015/08/20 01:10:49
I've updated URLRequestHttpJob such that GetTotalR
| |
| 796 if (total_received_bytes) { | |
| 797 DCHECK_GE(total_received_bytes, last_notified_total_received_bytes_); | |
| 798 if (total_received_bytes > last_notified_total_received_bytes_) { | |
| 799 network_delegate_->NotifyNetworkBytesReceived( | |
| 800 *request_, | |
| 801 total_received_bytes - last_notified_total_received_bytes_); | |
| 802 } | |
| 803 } | |
| 804 | |
| 805 last_notified_total_received_bytes_ = total_received_bytes; | |
| 806 } | |
| 807 | |
| 781 bool URLRequestJob::ReadRawDataForFilter(int* bytes_read) { | 808 bool URLRequestJob::ReadRawDataForFilter(int* bytes_read) { |
| 782 bool rv = false; | 809 bool rv = false; |
| 783 | 810 |
| 784 DCHECK(bytes_read); | 811 DCHECK(bytes_read); |
| 785 DCHECK(filter_.get()); | 812 DCHECK(filter_.get()); |
| 786 | 813 |
| 787 *bytes_read = 0; | 814 *bytes_read = 0; |
| 788 | 815 |
| 789 // Get more pre-filtered data if needed. | 816 // Get more pre-filtered data if needed. |
| 790 // TODO(mbelshe): is it possible that the filter needs *MORE* data | 817 // TODO(mbelshe): is it possible that the filter needs *MORE* data |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 857 } | 884 } |
| 858 | 885 |
| 859 if (!filter_.get()) | 886 if (!filter_.get()) |
| 860 postfilter_bytes_read_ += bytes_read; | 887 postfilter_bytes_read_ += bytes_read; |
| 861 DVLOG(2) << __FUNCTION__ << "() " | 888 DVLOG(2) << __FUNCTION__ << "() " |
| 862 << "\"" << (request_ ? request_->url().spec() : "???") << "\"" | 889 << "\"" << (request_ ? request_->url().spec() : "???") << "\"" |
| 863 << " pre bytes read = " << bytes_read | 890 << " pre bytes read = " << bytes_read |
| 864 << " pre total = " << prefilter_bytes_read_ | 891 << " pre total = " << prefilter_bytes_read_ |
| 865 << " post total = " << postfilter_bytes_read_; | 892 << " post total = " << postfilter_bytes_read_; |
| 866 UpdatePacketReadTimes(); // Facilitate stats recording if it is active. | 893 UpdatePacketReadTimes(); // Facilitate stats recording if it is active. |
| 867 if (network_delegate_) | 894 |
| 868 network_delegate_->NotifyRawBytesRead(*request_, bytes_read); | 895 MaybeNotifyNetworkBytes(); |
|
mmenke
2015/08/19 15:48:41
Think it's worth commenting on the difference betw
sclittle
2015/08/20 01:10:49
Done.
| |
| 869 } | 896 } |
| 870 | 897 |
| 871 bool URLRequestJob::FilterHasData() { | 898 bool URLRequestJob::FilterHasData() { |
| 872 return filter_.get() && filter_->stream_data_len(); | 899 return filter_.get() && filter_->stream_data_len(); |
| 873 } | 900 } |
| 874 | 901 |
| 875 void URLRequestJob::UpdatePacketReadTimes() { | 902 void URLRequestJob::UpdatePacketReadTimes() { |
| 876 } | 903 } |
| 877 | 904 |
| 878 RedirectInfo URLRequestJob::ComputeRedirectInfo(const GURL& location, | 905 RedirectInfo URLRequestJob::ComputeRedirectInfo(const GURL& location, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 913 // Alter the referrer if redirecting cross-origin (especially HTTP->HTTPS). | 940 // Alter the referrer if redirecting cross-origin (especially HTTP->HTTPS). |
| 914 redirect_info.new_referrer = | 941 redirect_info.new_referrer = |
| 915 ComputeReferrerForRedirect(request_->referrer_policy(), | 942 ComputeReferrerForRedirect(request_->referrer_policy(), |
| 916 request_->referrer(), | 943 request_->referrer(), |
| 917 redirect_info.new_url).spec(); | 944 redirect_info.new_url).spec(); |
| 918 | 945 |
| 919 return redirect_info; | 946 return redirect_info; |
| 920 } | 947 } |
| 921 | 948 |
| 922 } // namespace net | 949 } // namespace net |
| OLD | NEW |