Index: content/browser/loader/resource_loader.cc |
diff --git a/content/browser/loader/resource_loader.cc b/content/browser/loader/resource_loader.cc |
index d944ea9a5e59175cca7ae78646f93242eabba6b8..7d7b56057ac4e4d91d8c49e955385c00acd811ff 100644 |
--- a/content/browser/loader/resource_loader.cc |
+++ b/content/browser/loader/resource_loader.cc |
@@ -356,7 +356,7 @@ void ResourceLoader::OnResponseStarted(net::URLRequest* unused) { |
if (is_deferred() || !request_->status().is_success()) |
return; |
- StartReading(false); // Read the first chunk. |
+ ReadMore(false); // Read the first chunk. |
} |
void ResourceLoader::OnReadCompleted(net::URLRequest* unused, int bytes_read) { |
@@ -382,7 +382,7 @@ void ResourceLoader::OnReadCompleted(net::URLRequest* unused, int bytes_read) { |
return; |
if (bytes_read > 0) { |
- StartReading(true); // Read the next chunk. |
+ ReadMore(true); // Read the next chunk. |
} else { |
// TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. |
tracked_objects::ScopedTracker tracking_profile( |
@@ -554,43 +554,7 @@ void ResourceLoader::CompleteResponseStarted() { |
} |
} |
-void ResourceLoader::StartReading(bool is_continuation) { |
- int bytes_read = 0; |
- ReadMore(&bytes_read); |
- |
- // If IO is pending, wait for the URLRequest to call OnReadCompleted. |
- // On error or cancellation, wait for notification of failure. |
- if (request_->status().is_io_pending() || !request_->status().is_success()) |
- return; |
- |
- if (!is_continuation || bytes_read <= 0) { |
- OnReadCompleted(request_.get(), bytes_read); |
- } else { |
- // Else, trigger OnReadCompleted asynchronously to avoid starving the IO |
- // thread in case the URLRequest can provide data synchronously. |
- base::ThreadTaskRunnerHandle::Get()->PostTask( |
- FROM_HERE, |
- base::Bind(&ResourceLoader::OnReadCompleted, |
- weak_ptr_factory_.GetWeakPtr(), request_.get(), bytes_read)); |
- } |
-} |
- |
-void ResourceLoader::ResumeReading() { |
- DCHECK(!is_deferred()); |
- |
- if (!read_deferral_start_time_.is_null()) { |
- UMA_HISTOGRAM_TIMES("Net.ResourceLoader.ReadDeferral", |
- base::TimeTicks::Now() - read_deferral_start_time_); |
- read_deferral_start_time_ = base::TimeTicks(); |
- } |
- if (request_->status().is_success()) { |
- StartReading(false); // Read the next chunk (OK to complete synchronously). |
- } else { |
- ResponseCompleted(); |
- } |
-} |
- |
-void ResourceLoader::ReadMore(int* bytes_read) { |
+void ResourceLoader::ReadMore(bool is_continuation) { |
Randy Smith (Not in Mondays)
2016/12/06 23:44:43
This file looks like pure refactoring rather than
mmenke
2016/12/07 16:17:15
In the cancel path from the old ReadMore(), we wan
|
TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::ReadMore", this, |
TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT); |
DCHECK(!is_deferred()); |
@@ -606,6 +570,8 @@ void ResourceLoader::ReadMore(int* bytes_read) { |
FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnWillRead()")); |
if (!handler_->OnWillRead(&buf, &buf_size, -1)) { |
+ // Cancel the request, which will then call back into |this| to it of |
Randy Smith (Not in Mondays)
2016/12/06 23:44:43
nit: "to it"?
mmenke
2016/12/07 16:17:15
Done.
|
+ // a "read error". |
Cancel(); |
return; |
} |
@@ -614,10 +580,36 @@ void ResourceLoader::ReadMore(int* bytes_read) { |
DCHECK(buf.get()); |
DCHECK(buf_size > 0); |
- request_->Read(buf.get(), buf_size, bytes_read); |
+ int result = request_->Read(buf.get(), buf_size); |
+ |
+ if (result == net::ERR_IO_PENDING) |
+ return; |
+ |
+ if (!is_continuation || result <= 0) { |
+ OnReadCompleted(request_.get(), result); |
+ } else { |
+ // Else, trigger OnReadCompleted asynchronously to avoid starving the IO |
+ // thread in case the URLRequest can provide data synchronously. |
+ base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&ResourceLoader::OnReadCompleted, |
+ weak_ptr_factory_.GetWeakPtr(), request_.get(), result)); |
+ } |
+} |
+ |
+void ResourceLoader::ResumeReading() { |
+ DCHECK(!is_deferred()); |
- // No need to check the return value here as we'll detect errors by |
- // inspecting the URLRequest's status. |
+ if (!read_deferral_start_time_.is_null()) { |
+ UMA_HISTOGRAM_TIMES("Net.ResourceLoader.ReadDeferral", |
+ base::TimeTicks::Now() - read_deferral_start_time_); |
+ read_deferral_start_time_ = base::TimeTicks(); |
+ } |
+ if (request_->status().is_success()) { |
+ ReadMore(false); // Read the next chunk (OK to complete synchronously). |
+ } else { |
+ ResponseCompleted(); |
+ } |
} |
void ResourceLoader::CompleteRead(int bytes_read) { |