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

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

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