Chromium Code Reviews| Index: net/url_request/url_request_job.cc |
| diff --git a/net/url_request/url_request_job.cc b/net/url_request/url_request_job.cc |
| index 682f3d18bfe954ef1fd21af5a7c5cc9196166e69..d79fdd70942ced4eac6de67782ef1e417541f197 100644 |
| --- a/net/url_request/url_request_job.cc |
| +++ b/net/url_request/url_request_job.cc |
| @@ -72,6 +72,7 @@ URLRequestJob::URLRequestJob(URLRequest* request, |
| has_handled_response_(false), |
| expected_content_size_(-1), |
| network_delegate_(network_delegate), |
| + last_notified_total_received_bytes_(0), |
| weak_factory_(this) { |
| base::PowerMonitor* power_monitor = base::PowerMonitor::Get(); |
| if (power_monitor) |
| @@ -395,6 +396,8 @@ void URLRequestJob::NotifyHeadersComplete() { |
| // survival until we can get out of this method. |
| scoped_refptr<URLRequestJob> self_preservation(this); |
| + MaybeNotifyNetworkBytes(); |
| + |
| if (request_) |
| request_->OnHeadersComplete(); |
| @@ -567,6 +570,8 @@ void URLRequestJob::NotifyDone(const URLRequestStatus &status) { |
| } |
| } |
| + MaybeNotifyNetworkBytes(); |
| + |
| // Complete this notification later. This prevents us from re-entering the |
| // delegate if we're done because of a synchronous call. |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| @@ -778,6 +783,28 @@ void URLRequestJob::SetProxyServer(const HostPortPair& proxy_server) { |
| request_->proxy_server_ = proxy_server; |
| } |
| +void URLRequestJob::MaybeNotifyNetworkBytes() { |
| + if (!request_ || !network_delegate_) |
| + return; |
| + |
| + int64_t total_received_bytes = GetTotalReceivedBytes(); |
| + // For URLRequestHttpJobs, |total_received_bytes| will reset to 0 if the |
| + // transaction is killed, such as when a request is cancelled. Check for that |
| + // here to avoid reporting negative network usage in these cases. Note that |
| + // URLRequestHttpJob calls MaybeNotifyNetworkBytes shortly before killing the |
| + // 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
|
| + if (total_received_bytes) { |
| + DCHECK_GE(total_received_bytes, last_notified_total_received_bytes_); |
| + if (total_received_bytes > last_notified_total_received_bytes_) { |
| + network_delegate_->NotifyNetworkBytesReceived( |
| + *request_, |
| + total_received_bytes - last_notified_total_received_bytes_); |
| + } |
| + } |
| + |
| + last_notified_total_received_bytes_ = total_received_bytes; |
| +} |
| + |
| bool URLRequestJob::ReadRawDataForFilter(int* bytes_read) { |
| bool rv = false; |
| @@ -864,8 +891,8 @@ void URLRequestJob::RecordBytesRead(int bytes_read) { |
| << " pre total = " << prefilter_bytes_read_ |
| << " post total = " << postfilter_bytes_read_; |
| UpdatePacketReadTimes(); // Facilitate stats recording if it is active. |
| - if (network_delegate_) |
| - network_delegate_->NotifyRawBytesRead(*request_, bytes_read); |
| + |
| + 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.
|
| } |
| bool URLRequestJob::FilterHasData() { |