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

Side by Side Diff: net/url_request/url_request_job.cc

Issue 1467603002: URLRequestJob: change ReadRawData contract (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add done_reading_called_ to MockNetworkTransaction Created 5 years, 1 month 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
« no previous file with comments | « net/url_request/url_request_job.h ('k') | net/url_request/url_request_job_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 // Make sure the request is notified that we are done. We assume that the 94 // Make sure the request is notified that we are done. We assume that the
95 // request took care of setting its error status before calling Kill. 95 // request took care of setting its error status before calling Kill.
96 if (request_) 96 if (request_)
97 NotifyCanceled(); 97 NotifyCanceled();
98 } 98 }
99 99
100 void URLRequestJob::DetachRequest() { 100 void URLRequestJob::DetachRequest() {
101 request_ = NULL; 101 request_ = NULL;
102 } 102 }
103 103
104 // This function calls ReadData to get stream data. If a filter exists, passes 104 // This function calls ReadRawData to get stream data. If a filter exists, it
105 // the data to the attached filter. Then returns the output from filter back to 105 // passes the data to the attached filter. It then returns the output from
106 // the caller. 106 // filter back to the caller.
107 bool URLRequestJob::Read(IOBuffer* buf, int buf_size, int *bytes_read) { 107 bool URLRequestJob::Read(IOBuffer* buf, int buf_size, int *bytes_read) {
108 bool rv = false;
109
110 DCHECK_LT(buf_size, 1000000); // Sanity check. 108 DCHECK_LT(buf_size, 1000000); // Sanity check.
111 DCHECK(buf); 109 DCHECK(buf);
112 DCHECK(bytes_read); 110 DCHECK(bytes_read);
113 DCHECK(filtered_read_buffer_.get() == NULL); 111 DCHECK(filtered_read_buffer_.get() == NULL);
114 DCHECK_EQ(0, filtered_read_buffer_len_); 112 DCHECK_EQ(0, filtered_read_buffer_len_);
115 113
114 Error error = OK;
116 *bytes_read = 0; 115 *bytes_read = 0;
117 116
118 // Skip Filter if not present. 117 // Skip Filter if not present.
119 if (!filter_.get()) { 118 if (!filter_) {
120 rv = ReadRawDataHelper(buf, buf_size, bytes_read); 119 error = ReadRawDataHelper(buf, buf_size, bytes_read);
121 } else { 120 } else {
122 // Save the caller's buffers while we do IO 121 // Save the caller's buffers while we do IO
123 // in the filter's buffers. 122 // in the filter's buffers.
124 filtered_read_buffer_ = buf; 123 filtered_read_buffer_ = buf;
125 filtered_read_buffer_len_ = buf_size; 124 filtered_read_buffer_len_ = buf_size;
126 125
127 if (ReadFilteredData(bytes_read)) { 126 error = ReadFilteredData(bytes_read);
128 rv = true; // We have data to return.
129 127
130 // It is fine to call DoneReading even if ReadFilteredData receives 0 128 // Synchronous EOF from the filter.
131 // bytes from the net, but we avoid making that call if we know for 129 if (error == OK && *bytes_read == 0)
132 // sure that's the case (ReadRawDataHelper path). 130 DoneReading();
133 if (*bytes_read == 0)
134 DoneReading();
135 } else {
136 rv = false; // Error, or a new IO is pending.
137 }
138 } 131 }
139 132
140 if (rv && *bytes_read == 0) 133 if (error == OK) {
141 NotifyDone(URLRequestStatus()); 134 // If URLRequestJob read zero bytes, the job is at EOF.
142 return rv; 135 if (*bytes_read == 0)
136 NotifyDone(URLRequestStatus());
137 } else if (error == ERR_IO_PENDING) {
138 SetStatus(URLRequestStatus::FromError(ERR_IO_PENDING));
139 } else {
140 NotifyDone(URLRequestStatus::FromError(error));
141 *bytes_read = -1;
142 }
143 return error == OK;
143 } 144 }
144 145
145 void URLRequestJob::StopCaching() { 146 void URLRequestJob::StopCaching() {
146 // Nothing to do here. 147 // Nothing to do here.
147 } 148 }
148 149
149 bool URLRequestJob::GetFullRequestHeaders(HttpRequestHeaders* headers) const { 150 bool URLRequestJob::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
150 // Most job types don't send request headers. 151 // Most job types don't send request headers.
151 return false; 152 return false;
152 } 153 }
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 base::StringToInt64(content_length, &expected_content_size_); 474 base::StringToInt64(content_length, &expected_content_size_);
474 } else { 475 } else {
475 request_->net_log().AddEvent( 476 request_->net_log().AddEvent(
476 NetLog::TYPE_URL_REQUEST_FILTERS_SET, 477 NetLog::TYPE_URL_REQUEST_FILTERS_SET,
477 base::Bind(&FiltersSetCallback, base::Unretained(filter_.get()))); 478 base::Bind(&FiltersSetCallback, base::Unretained(filter_.get())));
478 } 479 }
479 480
480 request_->NotifyResponseStarted(); 481 request_->NotifyResponseStarted();
481 } 482 }
482 483
483 void URLRequestJob::NotifyReadComplete(int bytes_read) { 484 void URLRequestJob::ConvertResultToError(int result, Error* error, int* count) {
485 if (result >= 0) {
486 *error = OK;
487 *count = result;
488 } else {
489 *error = static_cast<Error>(result);
490 *count = 0;
491 }
492 }
493
494 void URLRequestJob::ReadRawDataComplete(int result) {
484 // TODO(cbentzel): Remove ScopedTracker below once crbug.com/475755 is fixed. 495 // TODO(cbentzel): Remove ScopedTracker below once crbug.com/475755 is fixed.
485 tracked_objects::ScopedTracker tracking_profile( 496 tracked_objects::ScopedTracker tracking_profile(
486 FROM_HERE_WITH_EXPLICIT_FUNCTION( 497 FROM_HERE_WITH_EXPLICIT_FUNCTION(
487 "475755 URLRequestJob::NotifyReadComplete")); 498 "475755 URLRequestJob::RawReadCompleted"));
488 499
489 if (!request_ || !request_->has_delegate()) 500 if (!request_ || !request_->has_delegate())
490 return; // The request was destroyed, so there is no more work to do. 501 return; // The request was destroyed, so there is no more work to do.
491 502
492 // TODO(darin): Bug 1004233. Re-enable this test once all of the chrome 503 // TODO(darin): Bug 1004233. Re-enable this test once all of the chrome
493 // unit_tests have been fixed to not trip this. 504 // unit_tests have been fixed to not trip this.
494 #if 0 505 #if 0
495 DCHECK(!request_->status().is_io_pending()); 506 DCHECK(!request_->status().is_io_pending());
496 #endif 507 #endif
497 // The headers should be complete before reads complete 508 // The headers should be complete before reads complete
498 DCHECK(has_handled_response_); 509 DCHECK(has_handled_response_);
499 510
500 OnRawReadComplete(bytes_read); 511 Error error;
512 int bytes_read;
513 ConvertResultToError(result, &error, &bytes_read);
501 514
502 // Don't notify if we had an error. 515 DCHECK_NE(ERR_IO_PENDING, error);
503 if (!request_->status().is_success()) 516
504 return; 517 // Synchronize the URLRequest state machine with the URLRequestJob state
518 // machine. If this read succeeded, either the request is at EOF and the
519 // URLRequest state machine goes to 'finished', or it is not and the
520 // URLRequest state machine goes to 'success'. If the read failed, the
521 // URLRequest state machine goes directly to 'finished'.
522 //
523 // Update the URLRequest's status first, so that NotifyReadCompleted has an
524 // accurate view of the request.
525 if (error == OK && bytes_read > 0) {
526 SetStatus(URLRequestStatus());
527 } else {
528 NotifyDone(URLRequestStatus::FromError(error));
529 }
530
531 GatherRawReadStats(error, bytes_read);
532
533 if (filter_.get() && error == OK) {
534 int filter_bytes_read = 0;
535 // Tell the filter that it has more data.
536 PushInputToFilter(bytes_read);
537
538 // Filter the data.
539 error = ReadFilteredData(&filter_bytes_read);
540
541 if (error == OK && !filter_bytes_read)
542 DoneReading();
543
544 DVLOG(1) << __FUNCTION__ << "() "
545 << "\"" << (request_ ? request_->url().spec() : "???") << "\""
546 << " pre bytes read = " << bytes_read
547 << " pre total = " << prefilter_bytes_read_
548 << " post total = " << postfilter_bytes_read_;
549 bytes_read = filter_bytes_read;
550 } else {
551 DVLOG(1) << __FUNCTION__ << "() "
552 << "\"" << (request_ ? request_->url().spec() : "???") << "\""
553 << " pre bytes read = " << bytes_read
554 << " pre total = " << prefilter_bytes_read_
555 << " post total = " << postfilter_bytes_read_;
556 }
505 557
506 // When notifying the delegate, the delegate can release the request 558 // When notifying the delegate, the delegate can release the request
507 // (and thus release 'this'). After calling to the delegate, we must 559 // (and thus release 'this'). After calling to the delegate, we must
508 // check the request pointer to see if it still exists, and return 560 // check the request pointer to see if it still exists, and return
509 // immediately if it has been destroyed. self_preservation ensures our 561 // immediately if it has been destroyed. self_preservation ensures our
510 // survival until we can get out of this method. 562 // survival until we can get out of this method.
511 scoped_refptr<URLRequestJob> self_preservation(this); 563 scoped_refptr<URLRequestJob> self_preservation(this);
512 564
513 if (filter_.get()) { 565 // NotifyReadCompleted should be called after SetStatus or NotifyDone updates
514 // Tell the filter that it has more data 566 // the status.
515 FilteredDataRead(bytes_read); 567 if (error == OK)
516
517 // Filter the data.
518 int filter_bytes_read = 0;
519 if (ReadFilteredData(&filter_bytes_read)) {
520 if (!filter_bytes_read)
521 DoneReading();
522 request_->NotifyReadCompleted(filter_bytes_read);
523 }
524 } else {
525 request_->NotifyReadCompleted(bytes_read); 568 request_->NotifyReadCompleted(bytes_read);
526 }
527 DVLOG(1) << __FUNCTION__ << "() "
528 << "\"" << (request_ ? request_->url().spec() : "???") << "\""
529 << " pre bytes read = " << bytes_read
530 << " pre total = " << prefilter_bytes_read_
531 << " post total = " << postfilter_bytes_read_;
532 } 569 }
533 570
534 void URLRequestJob::NotifyStartError(const URLRequestStatus &status) { 571 void URLRequestJob::NotifyStartError(const URLRequestStatus &status) {
535 DCHECK(!has_handled_response_); 572 DCHECK(!has_handled_response_);
536 has_handled_response_ = true; 573 has_handled_response_ = true;
537 if (request_) { 574 if (request_) {
538 // There may be relevant information in the response info even in the 575 // There may be relevant information in the response info even in the
539 // error case. 576 // error case.
540 GetResponseInfo(&request_->response_info_); 577 GetResponseInfo(&request_->response_info_);
541 578
542 request_->set_status(status); 579 request_->set_status(status);
543 request_->NotifyResponseStarted(); 580 request_->NotifyResponseStarted();
544 // We may have been deleted. 581 // We may have been deleted.
545 } 582 }
546 } 583 }
547 584
548 void URLRequestJob::NotifyDone(const URLRequestStatus &status) { 585 void URLRequestJob::NotifyDone(const URLRequestStatus &status) {
549 DCHECK(!done_) << "Job sending done notification twice"; 586 DCHECK(!done_) << "Job sending done notification twice";
550 if (done_) 587 if (done_)
551 return; 588 return;
552 done_ = true; 589 done_ = true;
553 590
554 // Unless there was an error, we should have at least tried to handle 591 // Unless there was an error, we should have at least tried to handle
555 // the response before getting here. 592 // the response before getting here.
556 DCHECK(has_handled_response_ || !status.is_success()); 593 DCHECK(has_handled_response_ || !status.is_success());
557 594
558 // As with NotifyReadComplete, we need to take care to notice if we were 595 // As with RawReadCompleted, we need to take care to notice if we were
559 // destroyed during a delegate callback. 596 // destroyed during a delegate callback.
560 if (request_) { 597 if (request_) {
561 request_->set_is_pending(false); 598 request_->set_is_pending(false);
562 // With async IO, it's quite possible to have a few outstanding 599 // With async IO, it's quite possible to have a few outstanding
563 // requests. We could receive a request to Cancel, followed shortly 600 // requests. We could receive a request to Cancel, followed shortly
564 // by a successful IO. For tracking the status(), once there is 601 // by a successful IO. For tracking the status(), once there is
565 // an error, we do not change the status back to success. To 602 // an error, we do not change the status back to success. To
566 // enforce this, only set the status if the job is so far 603 // enforce this, only set the status if the job is so far
567 // successful. 604 // successful.
568 if (request_->status().is_success()) { 605 if (request_->status().is_success()) {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 } 668 }
632 669
633 void URLRequestJob::OnCallToDelegate() { 670 void URLRequestJob::OnCallToDelegate() {
634 request_->OnCallToDelegate(); 671 request_->OnCallToDelegate();
635 } 672 }
636 673
637 void URLRequestJob::OnCallToDelegateComplete() { 674 void URLRequestJob::OnCallToDelegateComplete() {
638 request_->OnCallToDelegateComplete(); 675 request_->OnCallToDelegateComplete();
639 } 676 }
640 677
641 bool URLRequestJob::ReadRawData(IOBuffer* buf, int buf_size, int* bytes_read) { 678 int URLRequestJob::ReadRawData(IOBuffer* buf, int buf_size) {
642 DCHECK(bytes_read); 679 return 0;
643 *bytes_read = 0;
644 return true;
645 } 680 }
646 681
647 void URLRequestJob::DoneReading() { 682 void URLRequestJob::DoneReading() {
648 // Do nothing. 683 // Do nothing.
649 } 684 }
650 685
651 void URLRequestJob::DoneReadingRedirectResponse() { 686 void URLRequestJob::DoneReadingRedirectResponse() {
652 } 687 }
653 688
654 void URLRequestJob::FilteredDataRead(int bytes_read) { 689 void URLRequestJob::PushInputToFilter(int bytes_read) {
655 DCHECK(filter_); 690 DCHECK(filter_);
656 filter_->FlushStreamBuffer(bytes_read); 691 filter_->FlushStreamBuffer(bytes_read);
657 } 692 }
658 693
659 bool URLRequestJob::ReadFilteredData(int* bytes_read) { 694 Error URLRequestJob::ReadFilteredData(int* bytes_read) {
660 DCHECK(filter_); 695 DCHECK(filter_);
661 DCHECK(filtered_read_buffer_.get()); 696 DCHECK(filtered_read_buffer_.get());
662 DCHECK_GT(filtered_read_buffer_len_, 0); 697 DCHECK_GT(filtered_read_buffer_len_, 0);
663 DCHECK_LT(filtered_read_buffer_len_, 1000000); // Sanity check. 698 DCHECK_LT(filtered_read_buffer_len_, 1000000); // Sanity check.
664 DCHECK(!raw_read_buffer_.get()); 699 DCHECK(!raw_read_buffer_);
665 700
666 *bytes_read = 0; 701 *bytes_read = 0;
667 bool rv = false; 702 Error error = ERR_FAILED;
668 703
669 for (;;) { 704 for (;;) {
670 if (is_done()) 705 if (is_done())
671 return true; 706 return OK;
672 707
673 if (!filter_needs_more_output_space_ && !filter_->stream_data_len()) { 708 if (!filter_needs_more_output_space_ && !filter_->stream_data_len()) {
674 // We don't have any raw data to work with, so read from the transaction. 709 // We don't have any raw data to work with, so read from the transaction.
675 int filtered_data_read; 710 int filtered_data_read;
676 if (ReadRawDataForFilter(&filtered_data_read)) { 711 error = ReadRawDataForFilter(&filtered_data_read);
677 if (filtered_data_read > 0) { 712 // If ReadRawDataForFilter returned some data, fall through to the case
678 // Give data to filter. 713 // below; otherwise, return early.
679 filter_->FlushStreamBuffer(filtered_data_read); 714 if (error != OK || filtered_data_read == 0)
680 } else { 715 return error;
681 return true; // EOF. 716 filter_->FlushStreamBuffer(filtered_data_read);
682 }
683 } else {
684 return false; // IO Pending (or error).
685 }
686 } 717 }
687 718
688 if ((filter_->stream_data_len() || filter_needs_more_output_space_) && 719 if ((filter_->stream_data_len() || filter_needs_more_output_space_) &&
689 !is_done()) { 720 !is_done()) {
690 // Get filtered data. 721 // Get filtered data.
691 int filtered_data_len = filtered_read_buffer_len_; 722 int filtered_data_len = filtered_read_buffer_len_;
692 int output_buffer_size = filtered_data_len; 723 int output_buffer_size = filtered_data_len;
693 Filter::FilterStatus status = 724 Filter::FilterStatus status =
694 filter_->ReadData(filtered_read_buffer_->data(), &filtered_data_len); 725 filter_->ReadData(filtered_read_buffer_->data(), &filtered_data_len);
695 726
696 if (filter_needs_more_output_space_ && !filtered_data_len) { 727 if (filter_needs_more_output_space_ && !filtered_data_len) {
697 // filter_needs_more_output_space_ was mistaken... there are no more 728 // filter_needs_more_output_space_ was mistaken... there are no more
698 // bytes and we should have at least tried to fill up the filter's input 729 // bytes and we should have at least tried to fill up the filter's input
699 // buffer. Correct the state, and try again. 730 // buffer. Correct the state, and try again.
700 filter_needs_more_output_space_ = false; 731 filter_needs_more_output_space_ = false;
701 continue; 732 continue;
702 } 733 }
703 filter_needs_more_output_space_ = 734 filter_needs_more_output_space_ =
704 (filtered_data_len == output_buffer_size); 735 (filtered_data_len == output_buffer_size);
705 736
706 switch (status) { 737 switch (status) {
707 case Filter::FILTER_DONE: { 738 case Filter::FILTER_DONE: {
708 filter_needs_more_output_space_ = false; 739 filter_needs_more_output_space_ = false;
709 *bytes_read = filtered_data_len; 740 *bytes_read = filtered_data_len;
710 postfilter_bytes_read_ += filtered_data_len; 741 postfilter_bytes_read_ += filtered_data_len;
711 rv = true; 742 error = OK;
712 break; 743 break;
713 } 744 }
714 case Filter::FILTER_NEED_MORE_DATA: { 745 case Filter::FILTER_NEED_MORE_DATA: {
715 // We have finished filtering all data currently in the buffer. 746 // We have finished filtering all data currently in the buffer.
716 // There might be some space left in the output buffer. One can 747 // There might be some space left in the output buffer. One can
717 // consider reading more data from the stream to feed the filter 748 // consider reading more data from the stream to feed the filter
718 // and filling up the output buffer. This leads to more complicated 749 // and filling up the output buffer. This leads to more complicated
719 // buffer management and data notification mechanisms. 750 // buffer management and data notification mechanisms.
720 // We can revisit this issue if there is a real perf need. 751 // We can revisit this issue if there is a real perf need.
721 if (filtered_data_len > 0) { 752 if (filtered_data_len > 0) {
722 *bytes_read = filtered_data_len; 753 *bytes_read = filtered_data_len;
723 postfilter_bytes_read_ += filtered_data_len; 754 postfilter_bytes_read_ += filtered_data_len;
724 rv = true; 755 error = OK;
725 } else { 756 } else {
726 // Read again since we haven't received enough data yet (e.g., we 757 // Read again since we haven't received enough data yet (e.g., we
727 // may not have a complete gzip header yet). 758 // may not have a complete gzip header yet).
728 continue; 759 continue;
729 } 760 }
730 break; 761 break;
731 } 762 }
732 case Filter::FILTER_OK: { 763 case Filter::FILTER_OK: {
733 *bytes_read = filtered_data_len; 764 *bytes_read = filtered_data_len;
734 postfilter_bytes_read_ += filtered_data_len; 765 postfilter_bytes_read_ += filtered_data_len;
735 rv = true; 766 error = OK;
736 break; 767 break;
737 } 768 }
738 case Filter::FILTER_ERROR: { 769 case Filter::FILTER_ERROR: {
739 DVLOG(1) << __FUNCTION__ << "() " 770 DVLOG(1) << __FUNCTION__ << "() "
740 << "\"" << (request_ ? request_->url().spec() : "???") 771 << "\"" << (request_ ? request_->url().spec() : "???")
741 << "\"" << " Filter Error"; 772 << "\"" << " Filter Error";
742 filter_needs_more_output_space_ = false; 773 filter_needs_more_output_space_ = false;
743 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, 774 error = ERR_CONTENT_DECODING_FAILED;
744 ERR_CONTENT_DECODING_FAILED));
745 rv = false;
746 break; 775 break;
747 } 776 }
748 default: { 777 default: {
749 NOTREACHED(); 778 NOTREACHED();
750 filter_needs_more_output_space_ = false; 779 filter_needs_more_output_space_ = false;
751 rv = false; 780 error = ERR_FAILED;
752 break; 781 break;
753 } 782 }
754 } 783 }
755 784
756 // If logging all bytes is enabled, log the filtered bytes read. 785 // If logging all bytes is enabled, log the filtered bytes read.
757 if (rv && request() && filtered_data_len > 0 && 786 if (error == OK && request() && filtered_data_len > 0 &&
758 request()->net_log().IsCapturing()) { 787 request()->net_log().IsCapturing()) {
759 request()->net_log().AddByteTransferEvent( 788 request()->net_log().AddByteTransferEvent(
760 NetLog::TYPE_URL_REQUEST_JOB_FILTERED_BYTES_READ, filtered_data_len, 789 NetLog::TYPE_URL_REQUEST_JOB_FILTERED_BYTES_READ, filtered_data_len,
761 filtered_read_buffer_->data()); 790 filtered_read_buffer_->data());
762 } 791 }
763 } else { 792 } else {
764 // we are done, or there is no data left. 793 // we are done, or there is no data left.
765 rv = true; 794 error = OK;
766 } 795 }
767 break; 796 break;
768 } 797 }
769 798
770 if (rv) { 799 if (error == OK) {
771 // When we successfully finished a read, we no longer need to save the 800 // When we successfully finished a read, we no longer need to save the
772 // caller's buffers. Release our reference. 801 // caller's buffers. Release our reference.
773 filtered_read_buffer_ = NULL; 802 filtered_read_buffer_ = NULL;
774 filtered_read_buffer_len_ = 0; 803 filtered_read_buffer_len_ = 0;
775 } 804 }
776 return rv; 805 return error;
777 } 806 }
778 807
779 void URLRequestJob::DestroyFilters() { 808 void URLRequestJob::DestroyFilters() {
780 filter_.reset(); 809 filter_.reset();
781 } 810 }
782 811
783 const URLRequestStatus URLRequestJob::GetStatus() { 812 const URLRequestStatus URLRequestJob::GetStatus() {
784 if (request_) 813 if (request_)
785 return request_->status(); 814 return request_->status();
786 // If the request is gone, we must be cancelled. 815 // If the request is gone, we must be cancelled.
(...skipping 12 matching lines...) Expand all
799 request_->status().is_success() || 828 request_->status().is_success() ||
800 (!status.is_success() && !status.is_io_pending())); 829 (!status.is_success() && !status.is_io_pending()));
801 request_->set_status(status); 830 request_->set_status(status);
802 } 831 }
803 } 832 }
804 833
805 void URLRequestJob::SetProxyServer(const HostPortPair& proxy_server) { 834 void URLRequestJob::SetProxyServer(const HostPortPair& proxy_server) {
806 request_->proxy_server_ = proxy_server; 835 request_->proxy_server_ = proxy_server;
807 } 836 }
808 837
809 bool URLRequestJob::ReadRawDataForFilter(int* bytes_read) { 838 Error URLRequestJob::ReadRawDataForFilter(int* bytes_read) {
810 bool rv = false; 839 Error error = ERR_FAILED;
811
812 DCHECK(bytes_read); 840 DCHECK(bytes_read);
813 DCHECK(filter_.get()); 841 DCHECK(filter_.get());
814 842
815 *bytes_read = 0; 843 *bytes_read = 0;
816 844
817 // Get more pre-filtered data if needed. 845 // Get more pre-filtered data if needed.
818 // TODO(mbelshe): is it possible that the filter needs *MORE* data 846 // TODO(mbelshe): is it possible that the filter needs *MORE* data
819 // when there is some data already in the buffer? 847 // when there is some data already in the buffer?
820 if (!filter_->stream_data_len() && !is_done()) { 848 if (!filter_->stream_data_len() && !is_done()) {
821 IOBuffer* stream_buffer = filter_->stream_buffer(); 849 IOBuffer* stream_buffer = filter_->stream_buffer();
822 int stream_buffer_size = filter_->stream_buffer_size(); 850 int stream_buffer_size = filter_->stream_buffer_size();
823 rv = ReadRawDataHelper(stream_buffer, stream_buffer_size, bytes_read); 851 error = ReadRawDataHelper(stream_buffer, stream_buffer_size, bytes_read);
824 } 852 }
825 return rv; 853 return error;
826 } 854 }
827 855
828 bool URLRequestJob::ReadRawDataHelper(IOBuffer* buf, 856 Error URLRequestJob::ReadRawDataHelper(IOBuffer* buf,
829 int buf_size, 857 int buf_size,
830 int* bytes_read) { 858 int* bytes_read) {
831 DCHECK(!request_->status().is_io_pending()); 859 DCHECK(!raw_read_buffer_);
832 DCHECK(raw_read_buffer_.get() == NULL);
833 860
834 // Keep a pointer to the read buffer, so we have access to it in the 861 // Keep a pointer to the read buffer, so we have access to it in
835 // OnRawReadComplete() callback in the event that the read completes 862 // GatherRawReadStats() in the event that the read completes asynchronously.
836 // asynchronously.
837 raw_read_buffer_ = buf; 863 raw_read_buffer_ = buf;
838 bool rv = ReadRawData(buf, buf_size, bytes_read); 864 Error error;
865 ConvertResultToError(ReadRawData(buf, buf_size), &error, bytes_read);
839 866
840 if (!request_->status().is_io_pending()) { 867 if (error != ERR_IO_PENDING) {
841 // If the read completes synchronously, either success or failure, 868 // If the read completes synchronously, either success or failure, invoke
842 // invoke the OnRawReadComplete callback so we can account for the 869 // GatherRawReadStats so we can account for the completed read.
843 // completed read. 870 GatherRawReadStats(error, *bytes_read);
844 OnRawReadComplete(*bytes_read);
845 } 871 }
846 return rv; 872 return error;
847 } 873 }
848 874
849 void URLRequestJob::FollowRedirect(const RedirectInfo& redirect_info) { 875 void URLRequestJob::FollowRedirect(const RedirectInfo& redirect_info) {
850 int rv = request_->Redirect(redirect_info); 876 int rv = request_->Redirect(redirect_info);
851 if (rv != OK) 877 if (rv != OK)
852 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv)); 878 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
853 } 879 }
854 880
855 void URLRequestJob::OnRawReadComplete(int bytes_read) { 881 void URLRequestJob::GatherRawReadStats(Error error, int bytes_read) {
856 DCHECK(raw_read_buffer_.get()); 882 DCHECK(raw_read_buffer_ || bytes_read == 0);
857 // If |filter_| is non-NULL, bytes will be logged after it is applied instead. 883 DCHECK_NE(ERR_IO_PENDING, error);
884
885 if (error != OK) {
886 raw_read_buffer_ = nullptr;
887 return;
888 }
889 // If |filter_| is non-NULL, bytes will be logged after it is applied
890 // instead.
858 if (!filter_.get() && request() && bytes_read > 0 && 891 if (!filter_.get() && request() && bytes_read > 0 &&
859 request()->net_log().IsCapturing()) { 892 request()->net_log().IsCapturing()) {
860 request()->net_log().AddByteTransferEvent( 893 request()->net_log().AddByteTransferEvent(
861 NetLog::TYPE_URL_REQUEST_JOB_BYTES_READ, bytes_read, 894 NetLog::TYPE_URL_REQUEST_JOB_BYTES_READ, bytes_read,
862 raw_read_buffer_->data()); 895 raw_read_buffer_->data());
863 } 896 }
864 897
865 if (bytes_read > 0) { 898 if (bytes_read > 0) {
866 RecordBytesRead(bytes_read); 899 RecordBytesRead(bytes_read);
867 } 900 }
868 raw_read_buffer_ = NULL; 901 raw_read_buffer_ = nullptr;
869 } 902 }
870 903
871 void URLRequestJob::RecordBytesRead(int bytes_read) { 904 void URLRequestJob::RecordBytesRead(int bytes_read) {
872 DCHECK_GT(bytes_read, 0); 905 DCHECK_GT(bytes_read, 0);
873 prefilter_bytes_read_ += bytes_read; 906 prefilter_bytes_read_ += bytes_read;
874 907
875 // On first read, notify NetworkQualityEstimator that response headers have 908 // On first read, notify NetworkQualityEstimator that response headers have
876 // been received. 909 // been received.
877 // TODO(tbansal): Move this to url_request_http_job.cc. This may catch 910 // TODO(tbansal): Move this to url_request_http_job.cc. This may catch
878 // Service Worker jobs twice. 911 // Service Worker jobs twice.
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
970 int64_t total_sent_bytes = GetTotalSentBytes(); 1003 int64_t total_sent_bytes = GetTotalSentBytes();
971 DCHECK_GE(total_sent_bytes, last_notified_total_sent_bytes_); 1004 DCHECK_GE(total_sent_bytes, last_notified_total_sent_bytes_);
972 if (total_sent_bytes > last_notified_total_sent_bytes_) { 1005 if (total_sent_bytes > last_notified_total_sent_bytes_) {
973 network_delegate_->NotifyNetworkBytesSent( 1006 network_delegate_->NotifyNetworkBytesSent(
974 request_, total_sent_bytes - last_notified_total_sent_bytes_); 1007 request_, total_sent_bytes - last_notified_total_sent_bytes_);
975 } 1008 }
976 last_notified_total_sent_bytes_ = total_sent_bytes; 1009 last_notified_total_sent_bytes_ = total_sent_bytes;
977 } 1010 }
978 1011
979 } // namespace net 1012 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_job.h ('k') | net/url_request/url_request_job_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698