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

Side by Side 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: Address Matt's comments. 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "url_request_adapter.h" 5 #include "url_request_adapter.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "components/cronet/android/url_request_context_adapter.h" 14 #include "components/cronet/android/url_request_context_adapter.h"
15 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" 15 #include "components/cronet/android/wrapped_channel_upload_element_reader.h"
16 #include "net/base/elements_upload_data_stream.h" 16 #include "net/base/elements_upload_data_stream.h"
17 #include "net/base/load_flags.h" 17 #include "net/base/load_flags.h"
18 #include "net/base/net_errors.h" 18 #include "net/base/net_errors.h"
19 #include "net/base/upload_bytes_element_reader.h" 19 #include "net/base/upload_bytes_element_reader.h"
20 #include "net/http/http_response_headers.h" 20 #include "net/http/http_response_headers.h"
21 #include "net/http/http_status_code.h" 21 #include "net/http/http_status_code.h"
22 22
23 namespace cronet { 23 namespace cronet {
24 24
25 static const size_t kBufferSizeIncrement = 8192; 25 static const size_t kBufferSizeIncrement = 32768;
mmenke 2014/12/04 20:57:29 No longer incrementing. kReadBufferSize?
mef 2014/12/05 23:25:07 Done.
26 26
27 URLRequestAdapter::URLRequestAdapter(URLRequestContextAdapter* context, 27 URLRequestAdapter::URLRequestAdapter(URLRequestContextAdapter* context,
28 URLRequestAdapterDelegate* delegate, 28 URLRequestAdapterDelegate* delegate,
29 GURL url, 29 GURL url,
30 net::RequestPriority priority) 30 net::RequestPriority priority)
31 : method_("GET"), 31 : method_("GET"),
32 read_buffer_(new net::GrowableIOBuffer()), 32 read_buffer_(new net::GrowableIOBuffer()),
33 bytes_read_(0),
34 total_bytes_read_(0), 33 total_bytes_read_(0),
35 error_code_(0), 34 error_code_(0),
36 http_status_code_(0), 35 http_status_code_(0),
37 canceled_(false), 36 canceled_(false),
38 expected_size_(0), 37 expected_size_(0),
39 chunked_upload_(false), 38 chunked_upload_(false),
40 disable_redirect_(false) { 39 disable_redirect_(false) {
41 context_ = context; 40 context_ = context;
42 delegate_ = delegate; 41 delegate_ = delegate;
43 url_ = url; 42 url_ = url;
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 } else if (chunked_upload_) { 163 } else if (chunked_upload_) {
165 url_request_->EnableChunkedUpload(); 164 url_request_->EnableChunkedUpload();
166 } 165 }
167 166
168 url_request_->SetPriority(priority_); 167 url_request_->SetPriority(priority_);
169 168
170 url_request_->Start(); 169 url_request_->Start();
171 } 170 }
172 171
173 void URLRequestAdapter::Cancel() { 172 void URLRequestAdapter::Cancel() {
174 if (canceled_) {
175 return;
176 }
177
178 canceled_ = true;
179
180 context_->PostTaskToNetworkThread( 173 context_->PostTaskToNetworkThread(
181 FROM_HERE, 174 FROM_HERE,
182 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this))); 175 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this)));
183 } 176 }
184 177
185 void URLRequestAdapter::OnCancelRequest() { 178 void URLRequestAdapter::OnCancelRequest() {
186 DCHECK(OnNetworkThread()); 179 DCHECK(OnNetworkThread());
187 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); 180 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec();
181 if (canceled_)
182 return;
183 canceled_ = true;
184 // Check whether request has already completed.
185 if (url_request_ == nullptr)
186 return;
188 187
189 if (url_request_ != NULL) { 188 url_request_->Cancel();
190 url_request_->Cancel(); 189 OnRequestCompleted();
191 }
192
193 OnRequestCanceled();
194 } 190 }
195 191
196 void URLRequestAdapter::Destroy() { 192 void URLRequestAdapter::Destroy() {
197 context_->PostTaskToNetworkThread( 193 context_->PostTaskToNetworkThread(
198 FROM_HERE, base::Bind(&URLRequestAdapter::OnDestroyRequest, this)); 194 FROM_HERE, base::Bind(&URLRequestAdapter::OnDestroyRequest, this));
199 } 195 }
200 196
201 // static 197 // static
202 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { 198 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) {
203 DCHECK(self->OnNetworkThread()); 199 DCHECK(self->OnNetworkThread());
(...skipping 20 matching lines...) Expand all
224 request->GetResponseHeaderByName("Content-Type", &content_type_); 220 request->GetResponseHeaderByName("Content-Type", &content_type_);
225 expected_size_ = request->GetExpectedContentSize(); 221 expected_size_ = request->GetExpectedContentSize();
226 delegate_->OnResponseStarted(this); 222 delegate_->OnResponseStarted(this);
227 223
228 Read(); 224 Read();
229 } 225 }
230 226
231 // Reads all available data or starts an asynchronous read. 227 // Reads all available data or starts an asynchronous read.
232 void URLRequestAdapter::Read() { 228 void URLRequestAdapter::Read() {
233 DCHECK(OnNetworkThread()); 229 DCHECK(OnNetworkThread());
234 while (true) { 230 if (read_buffer_->RemainingCapacity() == 0) {
mmenke 2014/12/04 20:57:29 Instead of this, can we just create a fixed buffer
mef 2014/12/05 23:25:07 Done.
235 if (read_buffer_->RemainingCapacity() == 0) { 231 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement;
236 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; 232 read_buffer_->SetCapacity(new_capacity);
237 read_buffer_->SetCapacity(new_capacity); 233 }
238 }
239 234
240 int bytes_read; 235 int bytes_read = 0;
241 if (url_request_->Read(read_buffer_.get(), 236 do {
242 read_buffer_->RemainingCapacity(), 237 bytes_read = 0;
243 &bytes_read)) { 238 url_request_->Read(
244 if (bytes_read == 0) { 239 read_buffer_.get(), read_buffer_->RemainingCapacity(), &bytes_read);
mmenke 2014/12/04 20:57:29 read_buffer_->RemainingCapacity -> kBufferSizeIncr
mef 2014/12/05 23:25:07 Done.
245 OnRequestSucceeded(); 240 // If IO is pending, wait for the URLRequest to call OnReadCompleted.
246 break; 241 if (url_request_->status().is_io_pending())
247 } 242 return;
243 } while (HandleReadResult(url_request_.get(), bytes_read));
244 }
248 245
249 VLOG(1) << "Synchronously read: " << bytes_read << " bytes"; 246 bool URLRequestAdapter::HandleReadResult(net::URLRequest* request,
250 OnBytesRead(bytes_read); 247 int bytes_read) {
251 } else if (url_request_->status().status() == 248 DCHECK(OnNetworkThread());
252 net::URLRequestStatus::IO_PENDING) { 249 if (!url_request_->status().is_success()) {
253 if (bytes_read_ != 0) { 250 OnRequestFailed();
254 VLOG(1) << "Flushing buffer: " << bytes_read_ << " bytes"; 251 return false;
252 } else if (bytes_read == 0) {
253 OnRequestSucceeded();
254 return false;
255 }
mmenke 2014/12/04 20:57:29 Fix indent.
mef 2014/12/05 23:25:07 Done.
255 256
256 delegate_->OnBytesRead(this); 257 total_bytes_read_ += bytes_read;
257 read_buffer_->set_offset(0); 258 delegate_->OnBytesRead(this, bytes_read);
258 bytes_read_ = 0; 259 read_buffer_->set_offset(0);
mmenke 2014/12/04 20:57:29 I don't think this does anything? Once this is re
mef 2014/12/05 23:25:07 Done.
259 } 260
260 VLOG(1) << "Started async read"; 261 return true;
261 break;
262 } else {
263 OnRequestFailed();
264 break;
265 }
266 }
267 } 262 }
268 263
269 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, 264 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request,
270 int bytes_read) { 265 int bytes_read) {
271 DCHECK(OnNetworkThread()); 266 if (!HandleReadResult(request, bytes_read))
272 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes";
273 if (bytes_read < 0) {
274 OnRequestFailed();
275 return; 267 return;
276 } else if (bytes_read == 0) {
277 OnRequestSucceeded();
278 return;
279 }
280 268
281 OnBytesRead(bytes_read);
282 Read(); 269 Read();
283 } 270 }
284 271
285 void URLRequestAdapter::OnReceivedRedirect(net::URLRequest* request, 272 void URLRequestAdapter::OnReceivedRedirect(net::URLRequest* request,
286 const net::RedirectInfo& info, 273 const net::RedirectInfo& info,
287 bool* defer_redirect) { 274 bool* defer_redirect) {
288 DCHECK(OnNetworkThread()); 275 DCHECK(OnNetworkThread());
289 if (disable_redirect_) { 276 if (disable_redirect_) {
290 http_status_code_ = request->GetResponseCode(); 277 http_status_code_ = request->GetResponseCode();
291 request->CancelWithError(net::ERR_TOO_MANY_REDIRECTS); 278 request->CancelWithError(net::ERR_TOO_MANY_REDIRECTS);
292 error_code_ = net::ERR_TOO_MANY_REDIRECTS; 279 error_code_ = net::ERR_TOO_MANY_REDIRECTS;
293 canceled_ = true; 280 canceled_ = true;
294 *defer_redirect = false; 281 *defer_redirect = false;
295 OnRequestCompleted(); 282 OnRequestCompleted();
296 } 283 }
297 } 284 }
298 285
299 void URLRequestAdapter::OnBytesRead(int bytes_read) {
300 DCHECK(OnNetworkThread());
301 read_buffer_->set_offset(read_buffer_->offset() + bytes_read);
302 bytes_read_ += bytes_read;
303 total_bytes_read_ += bytes_read;
304 }
305
306 void URLRequestAdapter::OnRequestSucceeded() { 286 void URLRequestAdapter::OnRequestSucceeded() {
307 DCHECK(OnNetworkThread()); 287 DCHECK(OnNetworkThread());
308 if (canceled_) { 288 if (canceled_) {
309 return; 289 return;
310 } 290 }
311 291
312 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ 292 VLOG(1) << "Request completed with HTTP status: " << http_status_code_
313 << ". Total bytes read: " << total_bytes_read_; 293 << ". Total bytes read: " << total_bytes_read_;
314 294
315 OnRequestCompleted(); 295 OnRequestCompleted();
316 } 296 }
317 297
318 void URLRequestAdapter::OnRequestFailed() { 298 void URLRequestAdapter::OnRequestFailed() {
319 DCHECK(OnNetworkThread()); 299 DCHECK(OnNetworkThread());
320 if (canceled_) { 300 if (canceled_) {
321 return; 301 return;
322 } 302 }
323 303
324 error_code_ = url_request_->status().error(); 304 error_code_ = url_request_->status().error();
325 VLOG(1) << "Request failed with status: " << url_request_->status().status() 305 VLOG(1) << "Request failed with status: " << url_request_->status().status()
326 << " and error: " << net::ErrorToString(error_code_); 306 << " and error: " << net::ErrorToString(error_code_);
327 OnRequestCompleted(); 307 OnRequestCompleted();
328 } 308 }
329 309
330 void URLRequestAdapter::OnRequestCanceled() {
331 DCHECK(OnNetworkThread());
332 OnRequestCompleted();
333 }
334
335 void URLRequestAdapter::OnRequestCompleted() { 310 void URLRequestAdapter::OnRequestCompleted() {
336 DCHECK(OnNetworkThread()); 311 DCHECK(OnNetworkThread());
337 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); 312 VLOG(1) << "Completed: " << url_.possibly_invalid_spec();
338 313
339 delegate_->OnBytesRead(this); 314 DCHECK(url_request_ != nullptr);
315
340 delegate_->OnRequestFinished(this); 316 delegate_->OnRequestFinished(this);
341 url_request_.reset(); 317 url_request_.reset();
342 } 318 }
343 319
344 unsigned char* URLRequestAdapter::Data() const { 320 unsigned char* URLRequestAdapter::Data() const {
345 DCHECK(OnNetworkThread()); 321 DCHECK(OnNetworkThread());
346 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); 322 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer());
347 } 323 }
348 324
349 bool URLRequestAdapter::OnNetworkThread() const { 325 bool URLRequestAdapter::OnNetworkThread() const {
350 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread(); 326 return context_->GetNetworkTaskRunner()->BelongsToCurrentThread();
351 } 327 }
352 328
353 } // namespace cronet 329 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698