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

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

Issue 2542843006: ResourceLoader: Fix a bunch of double-cancellation/double-error notification cases. (Closed)
Patch Set: Move comment 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 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 } 349 }
350 350
351 CompleteResponseStarted(); 351 CompleteResponseStarted();
352 352
353 // If the handler deferred the request, it will resume the request later. If 353 // If the handler deferred the request, it will resume the request later. If
354 // the request was cancelled, the request will call back into |this| with a 354 // the request was cancelled, the request will call back into |this| with a
355 // bogus read completed error. 355 // bogus read completed error.
356 if (is_deferred() || !request_->status().is_success()) 356 if (is_deferred() || !request_->status().is_success())
357 return; 357 return;
358 358
359 StartReading(false); // Read the first chunk. 359 ReadMore(false); // Read the first chunk.
360 } 360 }
361 361
362 void ResourceLoader::OnReadCompleted(net::URLRequest* unused, int bytes_read) { 362 void ResourceLoader::OnReadCompleted(net::URLRequest* unused, int bytes_read) {
363 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("loading"), 363 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("loading"),
364 "ResourceLoader::OnReadCompleted"); 364 "ResourceLoader::OnReadCompleted");
365 DCHECK_EQ(request_.get(), unused); 365 DCHECK_EQ(request_.get(), unused);
366 DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\"" 366 DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\""
367 << " bytes_read = " << bytes_read; 367 << " bytes_read = " << bytes_read;
368 368
369 // bytes_read == -1 always implies an error. 369 // bytes_read == -1 always implies an error.
370 if (bytes_read == -1 || !request_->status().is_success()) { 370 if (bytes_read == -1 || !request_->status().is_success()) {
371 ResponseCompleted(); 371 ResponseCompleted();
372 return; 372 return;
373 } 373 }
374 374
375 CompleteRead(bytes_read); 375 CompleteRead(bytes_read);
376 376
377 // If the handler cancelled or deferred the request, do not continue 377 // If the handler cancelled or deferred the request, do not continue
378 // processing the read. If canceled, either the request will call into |this| 378 // processing the read. If canceled, either the request will call into |this|
379 // with a bogus read error, or, if the request was completed, a task posted 379 // with a bogus read error, or, if the request was completed, a task posted
380 // from ResourceLoader::CancelREquestInternal will run OnResponseCompleted. 380 // from ResourceLoader::CancelREquestInternal will run OnResponseCompleted.
381 if (is_deferred() || !request_->status().is_success()) 381 if (is_deferred() || !request_->status().is_success())
382 return; 382 return;
383 383
384 if (bytes_read > 0) { 384 if (bytes_read > 0) {
385 StartReading(true); // Read the next chunk. 385 ReadMore(true); // Read the next chunk.
386 } else { 386 } else {
387 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. 387 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed.
388 tracked_objects::ScopedTracker tracking_profile( 388 tracked_objects::ScopedTracker tracking_profile(
389 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 ResponseCompleted()")); 389 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 ResponseCompleted()"));
390 390
391 // URLRequest reported an EOF. Call ResponseCompleted. 391 // URLRequest reported an EOF. Call ResponseCompleted.
392 DCHECK_EQ(0, bytes_read); 392 DCHECK_EQ(0, bytes_read);
393 ResponseCompleted(); 393 ResponseCompleted();
394 } 394 }
395 } 395 }
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 547
548 bool defer = false; 548 bool defer = false;
549 if (!handler_->OnResponseStarted(response.get(), &defer)) { 549 if (!handler_->OnResponseStarted(response.get(), &defer)) {
550 Cancel(); 550 Cancel();
551 } else if (defer) { 551 } else if (defer) {
552 read_deferral_start_time_ = base::TimeTicks::Now(); 552 read_deferral_start_time_ = base::TimeTicks::Now();
553 deferred_stage_ = DEFERRED_READ; // Read first chunk when resumed. 553 deferred_stage_ = DEFERRED_READ; // Read first chunk when resumed.
554 } 554 }
555 } 555 }
556 556
557 void ResourceLoader::StartReading(bool is_continuation) { 557 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
558 int bytes_read = 0;
559 ReadMore(&bytes_read);
560
561 // If IO is pending, wait for the URLRequest to call OnReadCompleted.
562 // On error or cancellation, wait for notification of failure.
563 if (request_->status().is_io_pending() || !request_->status().is_success())
564 return;
565
566 if (!is_continuation || bytes_read <= 0) {
567 OnReadCompleted(request_.get(), bytes_read);
568 } else {
569 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO
570 // thread in case the URLRequest can provide data synchronously.
571 base::ThreadTaskRunnerHandle::Get()->PostTask(
572 FROM_HERE,
573 base::Bind(&ResourceLoader::OnReadCompleted,
574 weak_ptr_factory_.GetWeakPtr(), request_.get(), bytes_read));
575 }
576 }
577
578 void ResourceLoader::ResumeReading() {
579 DCHECK(!is_deferred());
580
581 if (!read_deferral_start_time_.is_null()) {
582 UMA_HISTOGRAM_TIMES("Net.ResourceLoader.ReadDeferral",
583 base::TimeTicks::Now() - read_deferral_start_time_);
584 read_deferral_start_time_ = base::TimeTicks();
585 }
586 if (request_->status().is_success()) {
587 StartReading(false); // Read the next chunk (OK to complete synchronously).
588 } else {
589 ResponseCompleted();
590 }
591 }
592
593 void ResourceLoader::ReadMore(int* bytes_read) {
594 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::ReadMore", this, 558 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::ReadMore", this,
595 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT); 559 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT);
596 DCHECK(!is_deferred()); 560 DCHECK(!is_deferred());
597 561
598 // Make sure we track the buffer in at least one place. This ensures it gets 562 // Make sure we track the buffer in at least one place. This ensures it gets
599 // deleted even in the case the request has already finished its job and 563 // deleted even in the case the request has already finished its job and
600 // doesn't use the buffer. 564 // doesn't use the buffer.
601 scoped_refptr<net::IOBuffer> buf; 565 scoped_refptr<net::IOBuffer> buf;
602 int buf_size; 566 int buf_size;
603 { 567 {
604 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. 568 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed.
605 tracked_objects::ScopedTracker tracking_profile2( 569 tracked_objects::ScopedTracker tracking_profile2(
606 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnWillRead()")); 570 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnWillRead()"));
607 571
608 if (!handler_->OnWillRead(&buf, &buf_size, -1)) { 572 if (!handler_->OnWillRead(&buf, &buf_size, -1)) {
573 // 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.
574 // a "read error".
609 Cancel(); 575 Cancel();
610 return; 576 return;
611 } 577 }
612 } 578 }
613 579
614 DCHECK(buf.get()); 580 DCHECK(buf.get());
615 DCHECK(buf_size > 0); 581 DCHECK(buf_size > 0);
616 582
617 request_->Read(buf.get(), buf_size, bytes_read); 583 int result = request_->Read(buf.get(), buf_size);
618 584
619 // No need to check the return value here as we'll detect errors by 585 if (result == net::ERR_IO_PENDING)
620 // inspecting the URLRequest's status. 586 return;
587
588 if (!is_continuation || result <= 0) {
589 OnReadCompleted(request_.get(), result);
590 } else {
591 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO
592 // thread in case the URLRequest can provide data synchronously.
593 base::ThreadTaskRunnerHandle::Get()->PostTask(
594 FROM_HERE,
595 base::Bind(&ResourceLoader::OnReadCompleted,
596 weak_ptr_factory_.GetWeakPtr(), request_.get(), result));
597 }
598 }
599
600 void ResourceLoader::ResumeReading() {
601 DCHECK(!is_deferred());
602
603 if (!read_deferral_start_time_.is_null()) {
604 UMA_HISTOGRAM_TIMES("Net.ResourceLoader.ReadDeferral",
605 base::TimeTicks::Now() - read_deferral_start_time_);
606 read_deferral_start_time_ = base::TimeTicks();
607 }
608 if (request_->status().is_success()) {
609 ReadMore(false); // Read the next chunk (OK to complete synchronously).
610 } else {
611 ResponseCompleted();
612 }
621 } 613 }
622 614
623 void ResourceLoader::CompleteRead(int bytes_read) { 615 void ResourceLoader::CompleteRead(int bytes_read) {
624 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::CompleteRead", this, 616 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::CompleteRead", this,
625 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT); 617 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT);
626 618
627 DCHECK(bytes_read >= 0); 619 DCHECK(bytes_read >= 0);
628 DCHECK(request_->status().is_success()); 620 DCHECK(request_->status().is_success());
629 621
630 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. 622 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed.
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", prefetch_status, 727 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", prefetch_status,
736 STATUS_MAX); 728 STATUS_MAX);
737 } 729 }
738 } else if (request_->response_info().unused_since_prefetch) { 730 } else if (request_->response_info().unused_since_prefetch) {
739 TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time(); 731 TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time();
740 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentOnPrefetchHit", total_time); 732 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentOnPrefetchHit", total_time);
741 } 733 }
742 } 734 }
743 735
744 } // namespace content 736 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698