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

Unified Diff: components/cronet/android/url_request_adapter.cc

Issue 743713002: Cronet Fix Channel Write after Close when request is canceled after success. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Windows linker error. Created 6 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
« no previous file with comments | « components/cronet/android/url_request_adapter.h ('k') | net/BUILD.gn » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/cronet/android/url_request_adapter.cc
diff --git a/components/cronet/android/url_request_adapter.cc b/components/cronet/android/url_request_adapter.cc
index 5d8f84965fc9c59dc616399aa77d4637234728f8..ff3505b3a3d0ccb03d73006d5c3aef2538358f0c 100644
--- a/components/cronet/android/url_request_adapter.cc
+++ b/components/cronet/android/url_request_adapter.cc
@@ -22,15 +22,13 @@
namespace cronet {
-static const size_t kBufferSizeIncrement = 8192;
+static const size_t kReadBufferSize = 32768;
URLRequestAdapter::URLRequestAdapter(URLRequestContextAdapter* context,
URLRequestAdapterDelegate* delegate,
GURL url,
net::RequestPriority priority)
: method_("GET"),
- read_buffer_(new net::GrowableIOBuffer()),
- bytes_read_(0),
total_bytes_read_(0),
error_code_(0),
http_status_code_(0),
@@ -171,12 +169,6 @@ void URLRequestAdapter::OnInitiateConnection() {
}
void URLRequestAdapter::Cancel() {
- if (canceled_) {
- return;
- }
-
- canceled_ = true;
-
context_->PostTaskToNetworkThread(
FROM_HERE,
base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this)));
@@ -184,13 +176,15 @@ void URLRequestAdapter::Cancel() {
void URLRequestAdapter::OnCancelRequest() {
DCHECK(OnNetworkThread());
+ DCHECK(!canceled_);
VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec();
+ canceled_ = true;
+ // Check whether request has already completed.
+ if (url_request_ == nullptr)
+ return;
- if (url_request_ != NULL) {
- url_request_->Cancel();
- }
-
- OnRequestCanceled();
+ url_request_->Cancel();
+ OnRequestCompleted();
}
void URLRequestAdapter::Destroy() {
@@ -231,54 +225,42 @@ void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) {
// Reads all available data or starts an asynchronous read.
void URLRequestAdapter::Read() {
DCHECK(OnNetworkThread());
- while (true) {
- if (read_buffer_->RemainingCapacity() == 0) {
- int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement;
- read_buffer_->SetCapacity(new_capacity);
- }
-
- int bytes_read;
- if (url_request_->Read(read_buffer_.get(),
- read_buffer_->RemainingCapacity(),
- &bytes_read)) {
- if (bytes_read == 0) {
- OnRequestSucceeded();
- break;
- }
-
- VLOG(1) << "Synchronously read: " << bytes_read << " bytes";
- OnBytesRead(bytes_read);
- } else if (url_request_->status().status() ==
- net::URLRequestStatus::IO_PENDING) {
- if (bytes_read_ != 0) {
- VLOG(1) << "Flushing buffer: " << bytes_read_ << " bytes";
-
- delegate_->OnBytesRead(this);
- read_buffer_->set_offset(0);
- bytes_read_ = 0;
- }
- VLOG(1) << "Started async read";
- break;
- } else {
- OnRequestFailed();
- break;
- }
+ if (!read_buffer_.get())
+ read_buffer_ = new net::IOBufferWithSize(kReadBufferSize);
+
+ while(true) {
+ int bytes_read = 0;
+ url_request_->Read(read_buffer_.get(), kReadBufferSize, &bytes_read);
+ // If IO is pending, wait for the URLRequest to call OnReadCompleted.
+ if (url_request_->status().is_io_pending())
+ return;
+ // Stop when request has failed or succeeded.
+ if (!HandleReadResult(bytes_read))
+ return;
}
}
-void URLRequestAdapter::OnReadCompleted(net::URLRequest* request,
- int bytes_read) {
+bool URLRequestAdapter::HandleReadResult(int bytes_read) {
DCHECK(OnNetworkThread());
- VLOG(1) << "Asynchronously read: " << bytes_read << " bytes";
- if (bytes_read < 0) {
+ if (!url_request_->status().is_success()) {
OnRequestFailed();
- return;
+ return false;
} else if (bytes_read == 0) {
OnRequestSucceeded();
- return;
+ return false;
}
- OnBytesRead(bytes_read);
+ total_bytes_read_ += bytes_read;
+ delegate_->OnBytesRead(this, bytes_read);
+
+ return true;
+}
+
+void URLRequestAdapter::OnReadCompleted(net::URLRequest* request,
+ int bytes_read) {
+ if (!HandleReadResult(bytes_read))
+ return;
+
Read();
}
@@ -296,13 +278,6 @@ void URLRequestAdapter::OnReceivedRedirect(net::URLRequest* request,
}
}
-void URLRequestAdapter::OnBytesRead(int bytes_read) {
- DCHECK(OnNetworkThread());
- read_buffer_->set_offset(read_buffer_->offset() + bytes_read);
- bytes_read_ += bytes_read;
- total_bytes_read_ += bytes_read;
-}
-
void URLRequestAdapter::OnRequestSucceeded() {
DCHECK(OnNetworkThread());
if (canceled_) {
@@ -327,23 +302,19 @@ void URLRequestAdapter::OnRequestFailed() {
OnRequestCompleted();
}
-void URLRequestAdapter::OnRequestCanceled() {
- DCHECK(OnNetworkThread());
- OnRequestCompleted();
-}
-
void URLRequestAdapter::OnRequestCompleted() {
DCHECK(OnNetworkThread());
VLOG(1) << "Completed: " << url_.possibly_invalid_spec();
- delegate_->OnBytesRead(this);
+ DCHECK(url_request_ != nullptr);
+
delegate_->OnRequestFinished(this);
url_request_.reset();
}
unsigned char* URLRequestAdapter::Data() const {
DCHECK(OnNetworkThread());
- return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer());
+ return reinterpret_cast<unsigned char*>(read_buffer_->data());
}
bool URLRequestAdapter::OnNetworkThread() const {
« no previous file with comments | « components/cronet/android/url_request_adapter.h ('k') | net/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698