| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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())); |
| 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 URLRequest that we're done because of an |
| 604 // error. |
| 601 if (!request_->status().is_success()) { | 605 if (!request_->status().is_success()) { |
| 602 // We report the error differently depending on whether we've called | 606 // We report the error differently depending on whether we've called |
| 603 // OnResponseStarted yet. | 607 // OnResponseStarted yet. |
| 604 if (has_handled_response_) { | 608 if (has_handled_response_) { |
| 605 // We signal the error by calling OnReadComplete with a bytes_read of -1. | 609 // We signal the error by calling OnReadComplete with a bytes_read of -1. |
| 606 request_->NotifyReadCompleted(-1); | 610 request_->NotifyReadCompleted(-1); |
| 607 } else { | 611 } else { |
| 608 has_handled_response_ = true; | 612 has_handled_response_ = true; |
| 609 request_->NotifyResponseStarted(URLRequestStatus()); | 613 request_->NotifyResponseStarted(URLRequestStatus()); |
| 610 } | 614 } |
| 611 } | 615 } |
| 612 } | 616 } |
| 613 | 617 |
| 614 void URLRequestJob::NotifyCanceled() { | 618 void URLRequestJob::NotifyCanceled() { |
| 615 if (!done_) { | 619 if (!done_) { |
| 616 NotifyDone(URLRequestStatus(URLRequestStatus::CANCELED, ERR_ABORTED)); | 620 OnDone(URLRequestStatus(URLRequestStatus::CANCELED, ERR_ABORTED), true); |
| 617 } | 621 } |
| 618 } | 622 } |
| 619 | 623 |
| 620 void URLRequestJob::NotifyRestartRequired() { | 624 void URLRequestJob::NotifyRestartRequired() { |
| 621 DCHECK(!has_handled_response_); | 625 DCHECK(!has_handled_response_); |
| 622 if (GetStatus().status() != URLRequestStatus::CANCELED) | 626 if (GetStatus().status() != URLRequestStatus::CANCELED) |
| 623 request_->Restart(); | 627 request_->Restart(); |
| 624 } | 628 } |
| 625 | 629 |
| 626 void URLRequestJob::OnCallToDelegate() { | 630 void URLRequestJob::OnCallToDelegate() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 662 DCHECK_NE(ERR_IO_PENDING, result); | 666 DCHECK_NE(ERR_IO_PENDING, result); |
| 663 | 667 |
| 664 if (result > 0 && request()->net_log().IsCapturing()) { | 668 if (result > 0 && request()->net_log().IsCapturing()) { |
| 665 request()->net_log().AddByteTransferEvent( | 669 request()->net_log().AddByteTransferEvent( |
| 666 NetLogEventType::URL_REQUEST_JOB_FILTERED_BYTES_READ, result, | 670 NetLogEventType::URL_REQUEST_JOB_FILTERED_BYTES_READ, result, |
| 667 pending_read_buffer_->data()); | 671 pending_read_buffer_->data()); |
| 668 } | 672 } |
| 669 pending_read_buffer_ = nullptr; | 673 pending_read_buffer_ = nullptr; |
| 670 | 674 |
| 671 if (result < 0) { | 675 if (result < 0) { |
| 672 NotifyDone(URLRequestStatus::FromError(result)); | 676 OnDone(URLRequestStatus::FromError(result), !synchronous); |
| 673 return; | 677 return; |
| 674 } | 678 } |
| 675 | 679 |
| 676 if (result > 0) { | 680 if (result > 0) { |
| 677 postfilter_bytes_read_ += result; | 681 postfilter_bytes_read_ += result; |
| 678 if (!synchronous) | 682 } else { |
| 679 request_->NotifyReadCompleted(result); | 683 DCHECK_EQ(0, result); |
| 680 return; | 684 DoneReading(); |
| 685 // In the synchronous case, the caller will notify the URLRequest of |
| 686 // completion. In the async case, the NotifyReadCompleted call will. |
| 687 // TODO(mmenke): Can this be combined with the error case? |
| 688 OnDone(URLRequestStatus(), false); |
| 681 } | 689 } |
| 682 | 690 |
| 683 DCHECK_EQ(0, result); | |
| 684 DoneReading(); | |
| 685 NotifyDone(URLRequestStatus()); | |
| 686 if (!synchronous) | 691 if (!synchronous) |
| 687 request_->NotifyReadCompleted(result); | 692 request_->NotifyReadCompleted(result); |
| 688 } | 693 } |
| 689 | 694 |
| 690 int URLRequestJob::ReadRawDataHelper(IOBuffer* buf, | 695 int URLRequestJob::ReadRawDataHelper(IOBuffer* buf, |
| 691 int buf_size, | 696 int buf_size, |
| 692 const CompletionCallback& callback) { | 697 const CompletionCallback& callback) { |
| 693 DCHECK(!raw_read_buffer_); | 698 DCHECK(!raw_read_buffer_); |
| 694 | 699 |
| 695 // Keep a pointer to the read buffer, so URLRequestJob::GatherRawReadStats() | 700 // Keep a pointer to the read buffer, so URLRequestJob::GatherRawReadStats() |
| (...skipping 10 matching lines...) Expand all Loading... |
| 706 GatherRawReadStats(result); | 711 GatherRawReadStats(result); |
| 707 } else { | 712 } else { |
| 708 read_raw_callback_ = callback; | 713 read_raw_callback_ = callback; |
| 709 } | 714 } |
| 710 return result; | 715 return result; |
| 711 } | 716 } |
| 712 | 717 |
| 713 void URLRequestJob::FollowRedirect(const RedirectInfo& redirect_info) { | 718 void URLRequestJob::FollowRedirect(const RedirectInfo& redirect_info) { |
| 714 int rv = request_->Redirect(redirect_info); | 719 int rv = request_->Redirect(redirect_info); |
| 715 if (rv != OK) | 720 if (rv != OK) |
| 716 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); | 721 OnDone(URLRequestStatus(URLRequestStatus::FAILED, rv), true); |
| 717 } | 722 } |
| 718 | 723 |
| 719 void URLRequestJob::GatherRawReadStats(int bytes_read) { | 724 void URLRequestJob::GatherRawReadStats(int bytes_read) { |
| 720 DCHECK(raw_read_buffer_ || bytes_read == 0); | 725 DCHECK(raw_read_buffer_ || bytes_read == 0); |
| 721 DCHECK_NE(ERR_IO_PENDING, bytes_read); | 726 DCHECK_NE(ERR_IO_PENDING, bytes_read); |
| 722 | 727 |
| 723 if (bytes_read > 0) { | 728 if (bytes_read > 0) { |
| 724 // If there is a filter, bytes will be logged after the filter is applied. | 729 // If there is a filter, bytes will be logged after the filter is applied. |
| 725 if (source_stream_->type() != SourceStream::TYPE_NONE && | 730 if (source_stream_->type() != SourceStream::TYPE_NONE && |
| 726 request()->net_log().IsCapturing()) { | 731 request()->net_log().IsCapturing()) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 842 int64_t total_sent_bytes = GetTotalSentBytes(); | 847 int64_t total_sent_bytes = GetTotalSentBytes(); |
| 843 DCHECK_GE(total_sent_bytes, last_notified_total_sent_bytes_); | 848 DCHECK_GE(total_sent_bytes, last_notified_total_sent_bytes_); |
| 844 if (total_sent_bytes > last_notified_total_sent_bytes_) { | 849 if (total_sent_bytes > last_notified_total_sent_bytes_) { |
| 845 network_delegate_->NotifyNetworkBytesSent( | 850 network_delegate_->NotifyNetworkBytesSent( |
| 846 request_, total_sent_bytes - last_notified_total_sent_bytes_); | 851 request_, total_sent_bytes - last_notified_total_sent_bytes_); |
| 847 } | 852 } |
| 848 last_notified_total_sent_bytes_ = total_sent_bytes; | 853 last_notified_total_sent_bytes_ = total_sent_bytes; |
| 849 } | 854 } |
| 850 | 855 |
| 851 } // namespace net | 856 } // namespace net |
| OLD | NEW |