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

Side by Side Diff: content/browser/loader/resource_loader.cc

Issue 2543633004: Fix a pair of ResourceLoader cancellation/error bugs. (Closed)
Patch Set: Remove old test handler (No longer being used) 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/loader/resource_loader.h" 5 #include "content/browser/loader/resource_loader.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/callback_helpers.h" 9 #include "base/callback_helpers.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 344
345 DVLOG(1) << "OnResponseStarted: " << request_->url().spec(); 345 DVLOG(1) << "OnResponseStarted: " << request_->url().spec();
346 346
347 if (!request_->status().is_success()) { 347 if (!request_->status().is_success()) {
348 ResponseCompleted(); 348 ResponseCompleted();
349 return; 349 return;
350 } 350 }
351 351
352 CompleteResponseStarted(); 352 CompleteResponseStarted();
353 353
354 if (is_deferred()) 354 // If the handler deferred the request, it will resume the request later. If
355 // the request was cancelled, the request will call back into |this| with a
356 // bogus read completed error.
357 if (is_deferred() || !request_->status().is_success())
355 return; 358 return;
356 359
357 if (request_->status().is_success()) 360 StartReading(false); // Read the first chunk.
358 StartReading(false); // Read the first chunk.
359 else
360 ResponseCompleted();
361 } 361 }
362 362
363 void ResourceLoader::OnReadCompleted(net::URLRequest* unused, int bytes_read) { 363 void ResourceLoader::OnReadCompleted(net::URLRequest* unused, int bytes_read) {
364 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("loading"), 364 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("loading"),
365 "ResourceLoader::OnReadCompleted"); 365 "ResourceLoader::OnReadCompleted");
366 DCHECK_EQ(request_.get(), unused); 366 DCHECK_EQ(request_.get(), unused);
367 DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\"" 367 DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\""
368 << " bytes_read = " << bytes_read; 368 << " bytes_read = " << bytes_read;
369 369
370 // bytes_read == -1 always implies an error. 370 // bytes_read == -1 always implies an error.
371 if (bytes_read == -1 || !request_->status().is_success()) { 371 if (bytes_read == -1 || !request_->status().is_success()) {
372 ResponseCompleted(); 372 ResponseCompleted();
373 return; 373 return;
374 } 374 }
375 375
376 CompleteRead(bytes_read); 376 CompleteRead(bytes_read);
377 377
378 // If the handler cancelled or deferred the request, do not continue 378 // If the handler cancelled or deferred the request, do not continue
379 // processing the read. If cancelled, the URLRequest has already been 379 // processing the read. If cancelled, the URLRequest has already been
380 // cancelled and will schedule an erroring OnReadCompleted later. If deferred, 380 // cancelled and either the request will call into |this| with a bogus
Randy Smith (Not in Mondays) 2016/12/02 20:38:55 The "If cancelled" seems to me to refer to the URL
mmenke 2016/12/02 21:26:52 Done.
381 // do nothing until resumed. 381 // read error, or a posted task will run OnResponseCompleted, if the
382 // 382 // request was completed. If deferred, nothing to do until resumed.
Randy Smith (Not in Mondays) 2016/12/02 20:38:55 Suggest changing to ", or, if the request was comp
Randy Smith (Not in Mondays) 2016/12/02 20:38:55 I think the last sentence is redundant with the fi
mmenke 2016/12/02 21:26:52 Done.
mmenke 2016/12/02 21:26:52 Done.
383 // Note: if bytes_read is 0 (EOF) and the handler defers, resumption will call
384 // ResponseCompleted().
385 if (is_deferred() || !request_->status().is_success()) 383 if (is_deferred() || !request_->status().is_success())
386 return; 384 return;
387 385
388 if (bytes_read > 0) { 386 if (bytes_read > 0) {
389 StartReading(true); // Read the next chunk. 387 StartReading(true); // Read the next chunk.
390 } else { 388 } else {
391 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. 389 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed.
392 tracked_objects::ScopedTracker tracking_profile( 390 tracked_objects::ScopedTracker tracking_profile(
393 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 ResponseCompleted()")); 391 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 ResponseCompleted()"));
394 392
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 read_deferral_start_time_ = base::TimeTicks::Now(); 554 read_deferral_start_time_ = base::TimeTicks::Now();
557 deferred_stage_ = DEFERRED_READ; // Read first chunk when resumed. 555 deferred_stage_ = DEFERRED_READ; // Read first chunk when resumed.
558 } 556 }
559 } 557 }
560 558
561 void ResourceLoader::StartReading(bool is_continuation) { 559 void ResourceLoader::StartReading(bool is_continuation) {
562 int bytes_read = 0; 560 int bytes_read = 0;
563 ReadMore(&bytes_read); 561 ReadMore(&bytes_read);
564 562
565 // If IO is pending, wait for the URLRequest to call OnReadCompleted. 563 // If IO is pending, wait for the URLRequest to call OnReadCompleted.
566 if (request_->status().is_io_pending()) 564 // On error or cancellation, wait for notification of failure.
565 if (request_->status().is_io_pending() || !request_->status().is_success())
567 return; 566 return;
568 567
569 if (!is_continuation || bytes_read <= 0) { 568 if (!is_continuation || bytes_read <= 0) {
570 OnReadCompleted(request_.get(), bytes_read); 569 OnReadCompleted(request_.get(), bytes_read);
571 } else { 570 } else {
572 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO 571 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO
573 // thread in case the URLRequest can provide data synchronously. 572 // thread in case the URLRequest can provide data synchronously.
574 base::ThreadTaskRunnerHandle::Get()->PostTask( 573 base::ThreadTaskRunnerHandle::Get()->PostTask(
575 FROM_HERE, 574 FROM_HERE,
576 base::Bind(&ResourceLoader::OnReadCompleted, 575 base::Bind(&ResourceLoader::OnReadCompleted,
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 } 720 }
722 721
723 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", status, STATUS_MAX); 722 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", status, STATUS_MAX);
724 } else if (request_->response_info().unused_since_prefetch) { 723 } else if (request_->response_info().unused_since_prefetch) {
725 TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time(); 724 TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time();
726 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentOnPrefetchHit", total_time); 725 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentOnPrefetchHit", total_time);
727 } 726 }
728 } 727 }
729 728
730 } // namespace content 729 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698