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

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

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

Powered by Google App Engine
This is Rietveld 408576698