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

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

Issue 2262653003: Make URLRequest::Read to return net errors or bytes read instead of a bool (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more fixes Created 4 years, 3 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
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.h" 5 #include "net/url_request/url_request.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 SSLCertRequestInfo* cert_request_info) { 152 SSLCertRequestInfo* cert_request_info) {
153 request->CancelWithError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED); 153 request->CancelWithError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
154 } 154 }
155 155
156 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request, 156 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request,
157 const SSLInfo& ssl_info, 157 const SSLInfo& ssl_info,
158 bool is_hsts_ok) { 158 bool is_hsts_ok) {
159 request->Cancel(); 159 request->Cancel();
160 } 160 }
161 161
162 void URLRequest::Delegate::NotifyOnResponseStarted(URLRequest* request,
163 int net_error) {
164 DCHECK(request);
165
166 implemented_ = true;
167
168 OnResponseStarted(request, net_error);
169 if (!implemented_)
mmenke 2016/08/26 19:36:48 The issue with the failures is that the OnResponse
maksims (do not use this acc) 2016/08/29 12:30:35 Oh, thanks! Good to know this trick!
170 OnResponseStarted(request);
171 }
172
173 void URLRequest::Delegate::OnResponseStarted(URLRequest* request,
174 int net_error) {
175 NOTIMPLEMENTED();
176 implemented_ = false;
177 }
178
179 void URLRequest::Delegate::OnResponseStarted(URLRequest* request) {}
180
162 /////////////////////////////////////////////////////////////////////////////// 181 ///////////////////////////////////////////////////////////////////////////////
163 // URLRequest 182 // URLRequest
164 183
165 URLRequest::~URLRequest() { 184 URLRequest::~URLRequest() {
166 Cancel(); 185 Cancel();
167 186
168 if (network_delegate_) { 187 if (network_delegate_) {
169 network_delegate_->NotifyURLRequestDestroyed(this); 188 network_delegate_->NotifyURLRequestDestroyed(this);
170 if (job_.get()) 189 if (job_.get())
171 job_->NotifyURLRequestDestroyed(); 190 job_->NotifyURLRequestDestroyed();
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 508
490 void URLRequest::set_delegate(Delegate* delegate) { 509 void URLRequest::set_delegate(Delegate* delegate) {
491 DCHECK(!delegate_); 510 DCHECK(!delegate_);
492 DCHECK(delegate); 511 DCHECK(delegate);
493 delegate_ = delegate; 512 delegate_ = delegate;
494 } 513 }
495 514
496 void URLRequest::Start() { 515 void URLRequest::Start() {
497 DCHECK(delegate_); 516 DCHECK(delegate_);
498 517
518 if (!status_.is_success())
519 return;
520
499 // TODO(pkasting): Remove ScopedTracker below once crbug.com/456327 is fixed. 521 // TODO(pkasting): Remove ScopedTracker below once crbug.com/456327 is fixed.
500 tracked_objects::ScopedTracker tracking_profile( 522 tracked_objects::ScopedTracker tracking_profile(
501 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start")); 523 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start"));
502 524
503 // Some values can be NULL, but the job factory must not be. 525 // Some values can be NULL, but the job factory must not be.
504 DCHECK(context_->job_factory()); 526 DCHECK(context_->job_factory());
505 527
506 // Anything that sets |blocked_by_| before start should have cleaned up after 528 // Anything that sets |blocked_by_| before start should have cleaned up after
507 // itself. 529 // itself.
508 DCHECK(blocked_by_.empty()); 530 DCHECK(blocked_by_.empty());
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 network_delegate_(network_delegate ? network_delegate 570 network_delegate_(network_delegate ? network_delegate
549 : context->network_delegate()), 571 : context->network_delegate()),
550 net_log_( 572 net_log_(
551 BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST)), 573 BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST)),
552 url_chain_(1, url), 574 url_chain_(1, url),
553 method_("GET"), 575 method_("GET"),
554 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE), 576 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
555 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL), 577 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL),
556 load_flags_(LOAD_NORMAL), 578 load_flags_(LOAD_NORMAL),
557 delegate_(delegate), 579 delegate_(delegate),
580 status_(URLRequestStatus::FromError(OK)),
558 is_pending_(false), 581 is_pending_(false),
559 is_redirecting_(false), 582 is_redirecting_(false),
560 redirect_limit_(kMaxRedirects), 583 redirect_limit_(kMaxRedirects),
561 priority_(priority), 584 priority_(priority),
562 identifier_(GenerateURLRequestIdentifier()), 585 identifier_(GenerateURLRequestIdentifier()),
563 calling_delegate_(false), 586 calling_delegate_(false),
564 use_blocked_by_as_load_param_(false), 587 use_blocked_by_as_load_param_(false),
565 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete, 588 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete,
566 base::Unretained(this))), 589 base::Unretained(this))),
567 has_notified_completion_(false), 590 has_notified_completion_(false),
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
665 RestartWithJob( 688 RestartWithJob(
666 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_)); 689 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_));
667 } 690 }
668 691
669 void URLRequest::RestartWithJob(URLRequestJob *job) { 692 void URLRequest::RestartWithJob(URLRequestJob *job) {
670 DCHECK(job->request() == this); 693 DCHECK(job->request() == this);
671 PrepareToRestart(); 694 PrepareToRestart();
672 StartJob(job); 695 StartJob(job);
673 } 696 }
674 697
675 void URLRequest::Cancel() { 698 int URLRequest::Cancel() {
676 DoCancel(ERR_ABORTED, SSLInfo()); 699 return DoCancel(ERR_ABORTED, SSLInfo());
677 } 700 }
678 701
679 void URLRequest::CancelWithError(int error) { 702 int URLRequest::CancelWithError(int error) {
680 DoCancel(error, SSLInfo()); 703 return DoCancel(error, SSLInfo());
681 } 704 }
682 705
683 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) { 706 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) {
684 // This should only be called on a started request. 707 // This should only be called on a started request.
685 if (!is_pending_ || !job_.get() || job_->has_response_started()) { 708 if (!is_pending_ || !job_.get() || job_->has_response_started()) {
686 NOTREACHED(); 709 NOTREACHED();
687 return; 710 return;
688 } 711 }
689 DoCancel(error, ssl_info); 712 DoCancel(error, ssl_info);
690 } 713 }
691 714
692 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) { 715 int URLRequest::DoCancel(int error, const SSLInfo& ssl_info) {
693 DCHECK(error < 0); 716 DCHECK(error < 0);
694 // If cancelled while calling a delegate, clear delegate info. 717 // If cancelled while calling a delegate, clear delegate info.
695 if (calling_delegate_) { 718 if (calling_delegate_) {
696 LogUnblocked(); 719 LogUnblocked();
697 OnCallToDelegateComplete(); 720 OnCallToDelegateComplete();
698 } 721 }
699 722
700 // If the URL request already has an error status, then canceling is a no-op. 723 // If the URL request already has an error status, then canceling is a no-op.
701 // Plus, we don't want to change the error status once it has been set. 724 // Plus, we don't want to change the error status once it has been set.
702 if (status_.is_success()) { 725 if (status_.is_success()) {
(...skipping 12 matching lines...) Expand all
715 job_->Kill(); 738 job_->Kill();
716 739
717 // We need to notify about the end of this job here synchronously. The 740 // We need to notify about the end of this job here synchronously. The
718 // Job sends an asynchronous notification but by the time this is processed, 741 // Job sends an asynchronous notification but by the time this is processed,
719 // our |context_| is NULL. 742 // our |context_| is NULL.
720 NotifyRequestCompleted(); 743 NotifyRequestCompleted();
721 744
722 // The Job will call our NotifyDone method asynchronously. This is done so 745 // The Job will call our NotifyDone method asynchronously. This is done so
723 // that the Delegate implementation can call Cancel without having to worry 746 // that the Delegate implementation can call Cancel without having to worry
724 // about being called recursively. 747 // about being called recursively.
748 return status_.error();
725 } 749 }
726 750
751 int URLRequest::Read(IOBuffer* dest, int dest_size) {
752 DCHECK(job_.get());
753
754 // If this is the first read, end the delegate call that may have started in
755 // OnResponseStarted.
756 OnCallToDelegateComplete();
757
758 // Once the request fails or is cancelled, read will just return 0 bytes
759 // to indicate end of stream.
760 if (!status_.is_success()) {
mmenke 2016/08/26 19:36:48 Can this happen, if job_->is_done() is false? (Al
maksims (do not use this acc) 2016/08/29 12:30:35 Yes, if URLRequestJob::NotifyStartError() is calle
761 return status_.error();
762 }
763
764 // This handles a cancel that happens while paused.
765 // TODO(ahendrickson): DCHECK() that it is not done after
766 // http://crbug.com/115705 is fixed.
767 if (job_->is_done())
768 return status_.error();
769
770 if (dest_size == 0) {
771 // Caller is not too bright. I guess we've done what they asked.
772 return OK;
773 }
774
775 int rv = job_->Read(dest, dest_size);
776 if (rv == ERR_IO_PENDING)
777 set_status(URLRequestStatus::FromError(ERR_IO_PENDING));
778
779 // If rv is not 0 or actual bytes read, the status cannot be success.
780 DCHECK(!(rv < 0) || status_.status() != URLRequestStatus::SUCCESS);
mmenke 2016/08/26 19:36:48 Think rv >= 0 is easier to read, for the first che
maksims (do not use this acc) 2016/08/29 12:30:35 Done.
781
782 if (rv == 0 && status_.is_success())
783 NotifyRequestCompleted();
784 return rv;
785 }
786
787 // Deprecated.
727 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) { 788 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) {
728 DCHECK(job_.get()); 789 DCHECK(job_.get());
729 DCHECK(bytes_read); 790 DCHECK(bytes_read);
mmenke 2016/08/26 19:36:48 Isn't everything below this the same as: int resu
maksims (do not use this acc) 2016/08/29 12:30:35 Right! Thanks!
730 *bytes_read = 0; 791 *bytes_read = 0;
731 792
732 // If this is the first read, end the delegate call that may have started in 793 // If this is the first read, end the delegate call that may have started in
733 // OnResponseStarted. 794 // OnResponseStarted.
734 OnCallToDelegateComplete(); 795 OnCallToDelegateComplete();
735 796
736 // This handles a cancel that happens while paused. 797 // This handles a cancel that happens while paused.
737 // TODO(ahendrickson): DCHECK() that it is not done after 798 // TODO(ahendrickson): DCHECK() that it is not done after
738 // http://crbug.com/115705 is fixed. 799 // http://crbug.com/115705 is fixed.
739 if (job_->is_done()) 800 if (job_->is_done())
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
819 URLRequestJob* job = 880 URLRequestJob* job =
820 URLRequestJobManager::GetInstance()->MaybeInterceptResponse( 881 URLRequestJobManager::GetInstance()->MaybeInterceptResponse(
821 this, network_delegate_); 882 this, network_delegate_);
822 if (job) { 883 if (job) {
823 RestartWithJob(job); 884 RestartWithJob(job);
824 } else { 885 } else {
825 // In some cases (e.g. an event was canceled), we might have sent the 886 // In some cases (e.g. an event was canceled), we might have sent the
826 // completion event and receive a NotifyResponseStarted() later. 887 // completion event and receive a NotifyResponseStarted() later.
827 if (!has_notified_completion_ && status_.is_success()) { 888 if (!has_notified_completion_ && status_.is_success()) {
828 if (network_delegate_) 889 if (network_delegate_)
829 network_delegate_->NotifyResponseStarted(this); 890 network_delegate_->NotifyResponseStarted(this, net_error);
830 } 891 }
831 892
832 // Notify in case the entire URL Request has been finished. 893 // Notify in case the entire URL Request has been finished.
833 if (!has_notified_completion_ && !status_.is_success()) 894 if (!has_notified_completion_ && !status_.is_success())
834 NotifyRequestCompleted(); 895 NotifyRequestCompleted();
835 896
836 OnCallToDelegate(); 897 OnCallToDelegate();
837 delegate_->OnResponseStarted(this); 898 delegate_->NotifyOnResponseStarted(this, net_error);
838 // Nothing may appear below this line as OnResponseStarted may delete 899 // Nothing may appear below this line as OnResponseStarted may delete
839 // |this|. 900 // |this|.
840 } 901 }
841 } 902 }
842 903
843 void URLRequest::FollowDeferredRedirect() { 904 void URLRequest::FollowDeferredRedirect() {
844 DCHECK(job_.get()); 905 DCHECK(job_.get());
845 DCHECK(status_.is_success()); 906 DCHECK(status_.is_success());
846 907
847 status_ = URLRequestStatus::FromError(ERR_IO_PENDING); 908 status_ = URLRequestStatus::FromError(ERR_IO_PENDING);
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
1133 } 1194 }
1134 1195
1135 1196
1136 void URLRequest::NotifyReadCompleted(int bytes_read) { 1197 void URLRequest::NotifyReadCompleted(int bytes_read) {
1137 if (bytes_read > 0) 1198 if (bytes_read > 0)
1138 set_status(URLRequestStatus()); 1199 set_status(URLRequestStatus());
1139 // Notify in case the entire URL Request has been finished. 1200 // Notify in case the entire URL Request has been finished.
1140 if (bytes_read <= 0) 1201 if (bytes_read <= 0)
1141 NotifyRequestCompleted(); 1202 NotifyRequestCompleted();
1142 1203
1204 // When URLRequestJob notices there was an error in URLRequest's |status_|,
1205 // it calls this method with |bytes_read| set to -1. Set it to a real error
1206 // here.
1207 if (bytes_read == -1)
1208 bytes_read = status_.error();
1209
1143 // Notify NetworkChangeNotifier that we just received network data. 1210 // Notify NetworkChangeNotifier that we just received network data.
1144 // This is to identify cases where the NetworkChangeNotifier thinks we 1211 // This is to identify cases where the NetworkChangeNotifier thinks we
1145 // are off-line but we are still receiving network data (crbug.com/124069), 1212 // are off-line but we are still receiving network data (crbug.com/124069),
1146 // and to get rough network connection measurements. 1213 // and to get rough network connection measurements.
1147 if (bytes_read > 0 && !was_cached()) 1214 if (bytes_read > 0 && !was_cached())
1148 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read); 1215 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read);
1149 1216
1150 delegate_->OnReadCompleted(this, bytes_read); 1217 delegate_->OnReadCompleted(this, bytes_read);
1151 1218
1152 // Nothing below this line as OnReadCompleted may delete |this|. 1219 // Nothing below this line as OnReadCompleted may delete |this|.
(...skipping 24 matching lines...) Expand all
1177 void URLRequest::NotifyRequestCompleted() { 1244 void URLRequest::NotifyRequestCompleted() {
1178 // TODO(battre): Get rid of this check, according to willchan it should 1245 // TODO(battre): Get rid of this check, according to willchan it should
1179 // not be needed. 1246 // not be needed.
1180 if (has_notified_completion_) 1247 if (has_notified_completion_)
1181 return; 1248 return;
1182 1249
1183 is_pending_ = false; 1250 is_pending_ = false;
1184 is_redirecting_ = false; 1251 is_redirecting_ = false;
1185 has_notified_completion_ = true; 1252 has_notified_completion_ = true;
1186 if (network_delegate_) 1253 if (network_delegate_)
1187 network_delegate_->NotifyCompleted(this, job_.get() != NULL); 1254 network_delegate_->NotifyCompleted(this, job_.get() != NULL,
1255 status_.error());
1188 } 1256 }
1189 1257
1190 void URLRequest::OnCallToDelegate() { 1258 void URLRequest::OnCallToDelegate() {
1191 DCHECK(!calling_delegate_); 1259 DCHECK(!calling_delegate_);
1192 DCHECK(blocked_by_.empty()); 1260 DCHECK(blocked_by_.empty());
1193 calling_delegate_ = true; 1261 calling_delegate_ = true;
1194 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE); 1262 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE);
1195 } 1263 }
1196 1264
1197 void URLRequest::OnCallToDelegateComplete() { 1265 void URLRequest::OnCallToDelegateComplete() {
(...skipping 12 matching lines...) Expand all
1210 out->clear(); 1278 out->clear();
1211 } 1279 }
1212 1280
1213 void URLRequest::set_status(URLRequestStatus status) { 1281 void URLRequest::set_status(URLRequestStatus status) {
1214 DCHECK(status_.is_io_pending() || status_.is_success() || 1282 DCHECK(status_.is_io_pending() || status_.is_success() ||
1215 (!status.is_success() && !status.is_io_pending())); 1283 (!status.is_success() && !status.is_io_pending()));
1216 status_ = status; 1284 status_ = status;
1217 } 1285 }
1218 1286
1219 } // namespace net 1287 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698