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

Unified Diff: content/browser/loader/resource_loader.cc

Issue 2542843006: ResourceLoader: Fix a bunch of double-cancellation/double-error notification cases. (Closed)
Patch Set: Inline ReadMore Created 4 years 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/loader/resource_loader.cc
diff --git a/content/browser/loader/resource_loader.cc b/content/browser/loader/resource_loader.cc
index 05bb3a5efbf1d39f056ec962fd3a1f7f6232109a..77e3db15411a7b33d88cfaea2769832a5277abe5 100644
--- a/content/browser/loader/resource_loader.cc
+++ b/content/browser/loader/resource_loader.cc
@@ -357,7 +357,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) {
@@ -384,7 +384,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(
@@ -556,43 +556,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) {
TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::ReadMore", this,
TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT);
DCHECK(!is_deferred());
@@ -608,6 +572,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
+ // a "read error".
Cancel();
return;
}
@@ -616,10 +582,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) {

Powered by Google App Engine
This is Rietveld 408576698