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

Side by Side Diff: net/url_request/url_request_job.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 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 "net/url_request/url_request_job.h" 5 #include "net/url_request/url_request_job.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 return; 488 return;
489 } 489 }
490 } 490 }
491 491
492 has_handled_response_ = true; 492 has_handled_response_ = true;
493 if (request_->status().is_success()) { 493 if (request_->status().is_success()) {
494 DCHECK(!source_stream_); 494 DCHECK(!source_stream_);
495 source_stream_ = SetUpSourceStream(); 495 source_stream_ = SetUpSourceStream();
496 496
497 if (!source_stream_) { 497 if (!source_stream_) {
498 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 498 OnDone(URLRequestStatus(URLRequestStatus::FAILED,
499 ERR_CONTENT_DECODING_INIT_FAILED)); 499 ERR_CONTENT_DECODING_INIT_FAILED),
500 true);
500 return; 501 return;
501 } 502 }
502 if (source_stream_->type() == SourceStream::TYPE_NONE) { 503 if (source_stream_->type() == SourceStream::TYPE_NONE) {
503 std::string content_length; 504 std::string content_length;
504 request_->GetResponseHeaderByName("content-length", &content_length); 505 request_->GetResponseHeaderByName("content-length", &content_length);
505 if (!content_length.empty()) 506 if (!content_length.empty())
506 base::StringToInt64(content_length, &expected_content_size_); 507 base::StringToInt64(content_length, &expected_content_size_);
507 } else { 508 } else {
508 request_->net_log().AddEvent( 509 request_->net_log().AddEvent(
509 NetLogEventType::URL_REQUEST_FILTERS_SET, 510 NetLogEventType::URL_REQUEST_FILTERS_SET,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 // There may be relevant information in the response info even in the 557 // There may be relevant information in the response info even in the
557 // error case. 558 // error case.
558 GetResponseInfo(&request_->response_info_); 559 GetResponseInfo(&request_->response_info_);
559 560
560 MaybeNotifyNetworkBytes(); 561 MaybeNotifyNetworkBytes();
561 562
562 request_->NotifyResponseStarted(status); 563 request_->NotifyResponseStarted(status);
563 // |this| may have been deleted here. 564 // |this| may have been deleted here.
564 } 565 }
565 566
566 void URLRequestJob::NotifyDone(const URLRequestStatus &status) { 567 void URLRequestJob::OnDone(const URLRequestStatus& status, bool notify_done) {
567 DCHECK(!done_) << "Job sending done notification twice"; 568 DCHECK(!done_) << "Job sending done notification twice";
568 if (done_) 569 if (done_)
569 return; 570 return;
570 done_ = true; 571 done_ = true;
571 572
572 // Unless there was an error, we should have at least tried to handle 573 // Unless there was an error, we should have at least tried to handle
573 // the response before getting here. 574 // the response before getting here.
574 DCHECK(has_handled_response_ || !status.is_success()); 575 DCHECK(has_handled_response_ || !status.is_success());
575 576
576 request_->set_is_pending(false); 577 request_->set_is_pending(false);
577 // With async IO, it's quite possible to have a few outstanding 578 // With async IO, it's quite possible to have a few outstanding
578 // requests. We could receive a request to Cancel, followed shortly 579 // requests. We could receive a request to Cancel, followed shortly
579 // by a successful IO. For tracking the status(), once there is 580 // by a successful IO. For tracking the status(), once there is
580 // an error, we do not change the status back to success. To 581 // an error, we do not change the status back to success. To
581 // enforce this, only set the status if the job is so far 582 // enforce this, only set the status if the job is so far
582 // successful. 583 // successful.
583 if (request_->status().is_success()) { 584 if (request_->status().is_success()) {
584 if (status.status() == URLRequestStatus::FAILED) 585 if (status.status() == URLRequestStatus::FAILED)
585 request_->net_log().AddEventWithNetErrorCode(NetLogEventType::FAILED, 586 request_->net_log().AddEventWithNetErrorCode(NetLogEventType::FAILED,
586 status.error()); 587 status.error());
587 request_->set_status(status); 588 request_->set_status(status);
588 } 589 }
589 590
590 MaybeNotifyNetworkBytes(); 591 MaybeNotifyNetworkBytes();
591 592
592 // Complete this notification later. This prevents us from re-entering the 593 if (notify_done) {
593 // delegate if we're done because of a synchronous call. 594 // Complete this notification later. This prevents us from re-entering the
594 base::ThreadTaskRunnerHandle::Get()->PostTask( 595 // delegate if we're done because of a synchronous call.
595 FROM_HERE, base::Bind(&URLRequestJob::CompleteNotifyDone, 596 base::ThreadTaskRunnerHandle::Get()->PostTask(
596 weak_factory_.GetWeakPtr())); 597 FROM_HERE,
598 base::Bind(&URLRequestJob::NotifyDone, weak_factory_.GetWeakPtr()));
mmenke 2016/12/02 15:20:08 Note that I want to get rid of NotifyDone entirely
599 }
597 } 600 }
598 601
599 void URLRequestJob::CompleteNotifyDone() { 602 void URLRequestJob::NotifyDone() {
600 // Check if we should notify the delegate that we're done because of an error. 603 // Check if we should notify the delegate that we're done because of an error.
601 if (!request_->status().is_success()) { 604 if (!request_->status().is_success()) {
602 // We report the error differently depending on whether we've called 605 // We report the error differently depending on whether we've called
603 // OnResponseStarted yet. 606 // OnResponseStarted yet.
604 if (has_handled_response_) { 607 if (has_handled_response_) {
605 // We signal the error by calling OnReadComplete with a bytes_read of -1. 608 // We signal the error by calling OnReadComplete with a bytes_read of -1.
606 request_->NotifyReadCompleted(-1); 609 request_->NotifyReadCompleted(-1);
607 } else { 610 } else {
608 has_handled_response_ = true; 611 has_handled_response_ = true;
609 request_->NotifyResponseStarted(URLRequestStatus()); 612 request_->NotifyResponseStarted(URLRequestStatus());
610 } 613 }
611 } 614 }
612 } 615 }
613 616
614 void URLRequestJob::NotifyCanceled() { 617 void URLRequestJob::NotifyCanceled() {
615 if (!done_) { 618 if (!done_) {
616 NotifyDone(URLRequestStatus(URLRequestStatus::CANCELED, ERR_ABORTED)); 619 OnDone(URLRequestStatus(URLRequestStatus::CANCELED, ERR_ABORTED), true);
617 } 620 }
618 } 621 }
619 622
620 void URLRequestJob::NotifyRestartRequired() { 623 void URLRequestJob::NotifyRestartRequired() {
621 DCHECK(!has_handled_response_); 624 DCHECK(!has_handled_response_);
622 if (GetStatus().status() != URLRequestStatus::CANCELED) 625 if (GetStatus().status() != URLRequestStatus::CANCELED)
623 request_->Restart(); 626 request_->Restart();
624 } 627 }
625 628
626 void URLRequestJob::OnCallToDelegate() { 629 void URLRequestJob::OnCallToDelegate() {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 DCHECK_NE(ERR_IO_PENDING, result); 665 DCHECK_NE(ERR_IO_PENDING, result);
663 666
664 if (result > 0 && request()->net_log().IsCapturing()) { 667 if (result > 0 && request()->net_log().IsCapturing()) {
665 request()->net_log().AddByteTransferEvent( 668 request()->net_log().AddByteTransferEvent(
666 NetLogEventType::URL_REQUEST_JOB_FILTERED_BYTES_READ, result, 669 NetLogEventType::URL_REQUEST_JOB_FILTERED_BYTES_READ, result,
667 pending_read_buffer_->data()); 670 pending_read_buffer_->data());
668 } 671 }
669 pending_read_buffer_ = nullptr; 672 pending_read_buffer_ = nullptr;
670 673
671 if (result < 0) { 674 if (result < 0) {
672 NotifyDone(URLRequestStatus::FromError(result)); 675 OnDone(URLRequestStatus::FromError(result), !synchronous);
673 return; 676 return;
674 } 677 }
675 678
676 if (result > 0) { 679 if (result > 0) {
677 postfilter_bytes_read_ += result; 680 postfilter_bytes_read_ += result;
678 if (!synchronous) 681 if (!synchronous)
679 request_->NotifyReadCompleted(result); 682 request_->NotifyReadCompleted(result);
680 return; 683 return;
681 } 684 }
682 685
683 DCHECK_EQ(0, result); 686 DCHECK_EQ(0, result);
684 DoneReading(); 687 DoneReading();
685 NotifyDone(URLRequestStatus()); 688 OnDone(URLRequestStatus(), !synchronous);
686 if (!synchronous) 689 if (!synchronous)
687 request_->NotifyReadCompleted(result); 690 request_->NotifyReadCompleted(result);
688 } 691 }
689 692
690 int URLRequestJob::ReadRawDataHelper(IOBuffer* buf, 693 int URLRequestJob::ReadRawDataHelper(IOBuffer* buf,
691 int buf_size, 694 int buf_size,
692 const CompletionCallback& callback) { 695 const CompletionCallback& callback) {
693 DCHECK(!raw_read_buffer_); 696 DCHECK(!raw_read_buffer_);
694 697
695 // Keep a pointer to the read buffer, so URLRequestJob::GatherRawReadStats() 698 // Keep a pointer to the read buffer, so URLRequestJob::GatherRawReadStats()
(...skipping 10 matching lines...) Expand all
706 GatherRawReadStats(result); 709 GatherRawReadStats(result);
707 } else { 710 } else {
708 read_raw_callback_ = callback; 711 read_raw_callback_ = callback;
709 } 712 }
710 return result; 713 return result;
711 } 714 }
712 715
713 void URLRequestJob::FollowRedirect(const RedirectInfo& redirect_info) { 716 void URLRequestJob::FollowRedirect(const RedirectInfo& redirect_info) {
714 int rv = request_->Redirect(redirect_info); 717 int rv = request_->Redirect(redirect_info);
715 if (rv != OK) 718 if (rv != OK)
716 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); 719 OnDone(URLRequestStatus(URLRequestStatus::FAILED, rv), true);
717 } 720 }
718 721
719 void URLRequestJob::GatherRawReadStats(int bytes_read) { 722 void URLRequestJob::GatherRawReadStats(int bytes_read) {
720 DCHECK(raw_read_buffer_ || bytes_read == 0); 723 DCHECK(raw_read_buffer_ || bytes_read == 0);
721 DCHECK_NE(ERR_IO_PENDING, bytes_read); 724 DCHECK_NE(ERR_IO_PENDING, bytes_read);
722 725
723 if (bytes_read > 0) { 726 if (bytes_read > 0) {
724 // If there is a filter, bytes will be logged after the filter is applied. 727 // If there is a filter, bytes will be logged after the filter is applied.
725 if (source_stream_->type() != SourceStream::TYPE_NONE && 728 if (source_stream_->type() != SourceStream::TYPE_NONE &&
726 request()->net_log().IsCapturing()) { 729 request()->net_log().IsCapturing()) {
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 int64_t total_sent_bytes = GetTotalSentBytes(); 845 int64_t total_sent_bytes = GetTotalSentBytes();
843 DCHECK_GE(total_sent_bytes, last_notified_total_sent_bytes_); 846 DCHECK_GE(total_sent_bytes, last_notified_total_sent_bytes_);
844 if (total_sent_bytes > last_notified_total_sent_bytes_) { 847 if (total_sent_bytes > last_notified_total_sent_bytes_) {
845 network_delegate_->NotifyNetworkBytesSent( 848 network_delegate_->NotifyNetworkBytesSent(
846 request_, total_sent_bytes - last_notified_total_sent_bytes_); 849 request_, total_sent_bytes - last_notified_total_sent_bytes_);
847 } 850 }
848 last_notified_total_sent_bytes_ = total_sent_bytes; 851 last_notified_total_sent_bytes_ = total_sent_bytes;
849 } 852 }
850 853
851 } // namespace net 854 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698