OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/url_request.h" | 5 #include "net/url_request/url_request.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 referrer_ = referrer; | 486 referrer_ = referrer; |
487 } | 487 } |
488 } | 488 } |
489 | 489 |
490 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) { | 490 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) { |
491 DCHECK(!is_pending_); | 491 DCHECK(!is_pending_); |
492 referrer_policy_ = referrer_policy; | 492 referrer_policy_ = referrer_policy; |
493 } | 493 } |
494 | 494 |
495 void URLRequest::set_delegate(Delegate* delegate) { | 495 void URLRequest::set_delegate(Delegate* delegate) { |
| 496 DCHECK(!delegate_); |
| 497 DCHECK(delegate); |
496 delegate_ = delegate; | 498 delegate_ = delegate; |
497 } | 499 } |
498 | 500 |
499 void URLRequest::Start() { | 501 void URLRequest::Start() { |
| 502 DCHECK(delegate_); |
| 503 |
500 // TODO(pkasting): Remove ScopedTracker below once crbug.com/456327 is fixed. | 504 // TODO(pkasting): Remove ScopedTracker below once crbug.com/456327 is fixed. |
501 tracked_objects::ScopedTracker tracking_profile( | 505 tracked_objects::ScopedTracker tracking_profile( |
502 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start")); | 506 FROM_HERE_WITH_EXPLICIT_FUNCTION("456327 URLRequest::Start")); |
503 | 507 |
504 // Some values can be NULL, but the job factory must not be. | 508 // Some values can be NULL, but the job factory must not be. |
505 DCHECK(context_->job_factory()); | 509 DCHECK(context_->job_factory()); |
506 | 510 |
507 // Anything that sets |blocked_by_| before start should have cleaned up after | 511 // Anything that sets |blocked_by_| before start should have cleaned up after |
508 // itself. | 512 // itself. |
509 DCHECK(blocked_by_.empty()); | 513 DCHECK(blocked_by_.empty()); |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info, | 789 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info, |
786 bool* defer_redirect) { | 790 bool* defer_redirect) { |
787 is_redirecting_ = true; | 791 is_redirecting_ = true; |
788 | 792 |
789 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect? | 793 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect? |
790 URLRequestJob* job = | 794 URLRequestJob* job = |
791 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect( | 795 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect( |
792 this, network_delegate_, redirect_info.new_url); | 796 this, network_delegate_, redirect_info.new_url); |
793 if (job) { | 797 if (job) { |
794 RestartWithJob(job); | 798 RestartWithJob(job); |
795 } else if (delegate_) { | 799 } else { |
796 OnCallToDelegate(); | 800 OnCallToDelegate(); |
797 delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect); | 801 delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect); |
798 // |this| may be have been destroyed here. | 802 // |this| may be have been destroyed here. |
799 } | 803 } |
800 } | 804 } |
801 | 805 |
802 void URLRequest::NotifyBeforeNetworkStart(bool* defer) { | 806 void URLRequest::NotifyBeforeNetworkStart(bool* defer) { |
803 if (delegate_ && !notified_before_network_start_) { | 807 if (!notified_before_network_start_) { |
804 OnCallToDelegate(); | 808 OnCallToDelegate(); |
805 delegate_->OnBeforeNetworkStart(this, defer); | 809 delegate_->OnBeforeNetworkStart(this, defer); |
806 if (!*defer) | 810 if (!*defer) |
807 OnCallToDelegateComplete(); | 811 OnCallToDelegateComplete(); |
808 notified_before_network_start_ = true; | 812 notified_before_network_start_ = true; |
809 } | 813 } |
810 } | 814 } |
811 | 815 |
812 void URLRequest::ResumeNetworkStart() { | 816 void URLRequest::ResumeNetworkStart() { |
813 DCHECK(job_.get()); | 817 DCHECK(job_.get()); |
814 DCHECK(notified_before_network_start_); | 818 DCHECK(notified_before_network_start_); |
815 | 819 |
816 OnCallToDelegateComplete(); | 820 OnCallToDelegateComplete(); |
817 job_->ResumeNetworkStart(); | 821 job_->ResumeNetworkStart(); |
818 } | 822 } |
819 | 823 |
820 void URLRequest::NotifyResponseStarted() { | 824 void URLRequest::NotifyResponseStarted() { |
821 int net_error = OK; | 825 int net_error = OK; |
822 if (!status_.is_success()) | 826 if (!status_.is_success()) |
823 net_error = status_.error(); | 827 net_error = status_.error(); |
824 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB, | 828 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB, |
825 net_error); | 829 net_error); |
826 | 830 |
827 URLRequestJob* job = | 831 URLRequestJob* job = |
828 URLRequestJobManager::GetInstance()->MaybeInterceptResponse( | 832 URLRequestJobManager::GetInstance()->MaybeInterceptResponse( |
829 this, network_delegate_); | 833 this, network_delegate_); |
830 if (job) { | 834 if (job) { |
831 RestartWithJob(job); | 835 RestartWithJob(job); |
832 } else { | 836 } else { |
833 if (delegate_) { | 837 // In some cases (e.g. an event was canceled), we might have sent the |
834 // In some cases (e.g. an event was canceled), we might have sent the | 838 // completion event and receive a NotifyResponseStarted() later. |
835 // completion event and receive a NotifyResponseStarted() later. | 839 if (!has_notified_completion_ && status_.is_success()) { |
836 if (!has_notified_completion_ && status_.is_success()) { | 840 if (network_delegate_) |
837 if (network_delegate_) | 841 network_delegate_->NotifyResponseStarted(this); |
838 network_delegate_->NotifyResponseStarted(this); | 842 } |
839 } | |
840 | 843 |
841 // Notify in case the entire URL Request has been finished. | 844 // Notify in case the entire URL Request has been finished. |
842 if (!has_notified_completion_ && !status_.is_success()) | 845 if (!has_notified_completion_ && !status_.is_success()) |
843 NotifyRequestCompleted(); | 846 NotifyRequestCompleted(); |
844 | 847 |
845 OnCallToDelegate(); | 848 OnCallToDelegate(); |
846 delegate_->OnResponseStarted(this); | 849 delegate_->OnResponseStarted(this); |
847 // Nothing may appear below this line as OnResponseStarted may delete | 850 // Nothing may appear below this line as OnResponseStarted may delete |
848 // |this|. | 851 // |this|. |
849 } | |
850 } | 852 } |
851 } | 853 } |
852 | 854 |
853 void URLRequest::FollowDeferredRedirect() { | 855 void URLRequest::FollowDeferredRedirect() { |
854 CHECK(job_.get()); | 856 CHECK(job_.get()); |
855 CHECK(status_.is_success()); | 857 CHECK(status_.is_success()); |
856 | 858 |
857 job_->FollowDeferredRedirect(); | 859 job_->FollowDeferredRedirect(); |
858 } | 860 } |
859 | 861 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1081 // so it can be reset on another round. | 1083 // so it can be reset on another round. |
1082 AuthCredentials credentials = auth_credentials_; | 1084 AuthCredentials credentials = auth_credentials_; |
1083 auth_credentials_ = AuthCredentials(); | 1085 auth_credentials_ = AuthCredentials(); |
1084 scoped_refptr<AuthChallengeInfo> auth_info; | 1086 scoped_refptr<AuthChallengeInfo> auth_info; |
1085 auth_info.swap(auth_info_); | 1087 auth_info.swap(auth_info_); |
1086 | 1088 |
1087 switch (result) { | 1089 switch (result) { |
1088 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION: | 1090 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION: |
1089 // Defer to the URLRequest::Delegate, since the NetworkDelegate | 1091 // Defer to the URLRequest::Delegate, since the NetworkDelegate |
1090 // didn't take an action. | 1092 // didn't take an action. |
1091 if (delegate_) | 1093 delegate_->OnAuthRequired(this, auth_info.get()); |
1092 delegate_->OnAuthRequired(this, auth_info.get()); | |
1093 break; | 1094 break; |
1094 | 1095 |
1095 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH: | 1096 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH: |
1096 SetAuth(credentials); | 1097 SetAuth(credentials); |
1097 break; | 1098 break; |
1098 | 1099 |
1099 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: | 1100 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: |
1100 CancelAuth(); | 1101 CancelAuth(); |
1101 break; | 1102 break; |
1102 | 1103 |
1103 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING: | 1104 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING: |
1104 NOTREACHED(); | 1105 NOTREACHED(); |
1105 break; | 1106 break; |
1106 } | 1107 } |
1107 } | 1108 } |
1108 | 1109 |
1109 void URLRequest::NotifyCertificateRequested( | 1110 void URLRequest::NotifyCertificateRequested( |
1110 SSLCertRequestInfo* cert_request_info) { | 1111 SSLCertRequestInfo* cert_request_info) { |
1111 if (delegate_) | 1112 delegate_->OnCertificateRequested(this, cert_request_info); |
1112 delegate_->OnCertificateRequested(this, cert_request_info); | |
1113 } | 1113 } |
1114 | 1114 |
1115 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info, | 1115 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info, |
1116 bool fatal) { | 1116 bool fatal) { |
1117 if (delegate_) | 1117 delegate_->OnSSLCertificateError(this, ssl_info, fatal); |
1118 delegate_->OnSSLCertificateError(this, ssl_info, fatal); | |
1119 } | 1118 } |
1120 | 1119 |
1121 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const { | 1120 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const { |
1122 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES)); | 1121 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES)); |
1123 if (network_delegate_) { | 1122 if (network_delegate_) { |
1124 return network_delegate_->CanGetCookies(*this, cookie_list); | 1123 return network_delegate_->CanGetCookies(*this, cookie_list); |
1125 } | 1124 } |
1126 return g_default_can_use_cookies; | 1125 return g_default_can_use_cookies; |
1127 } | 1126 } |
1128 | 1127 |
(...skipping 20 matching lines...) Expand all Loading... |
1149 if (bytes_read <= 0) | 1148 if (bytes_read <= 0) |
1150 NotifyRequestCompleted(); | 1149 NotifyRequestCompleted(); |
1151 | 1150 |
1152 // Notify NetworkChangeNotifier that we just received network data. | 1151 // Notify NetworkChangeNotifier that we just received network data. |
1153 // This is to identify cases where the NetworkChangeNotifier thinks we | 1152 // This is to identify cases where the NetworkChangeNotifier thinks we |
1154 // are off-line but we are still receiving network data (crbug.com/124069), | 1153 // are off-line but we are still receiving network data (crbug.com/124069), |
1155 // and to get rough network connection measurements. | 1154 // and to get rough network connection measurements. |
1156 if (bytes_read > 0 && !was_cached()) | 1155 if (bytes_read > 0 && !was_cached()) |
1157 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read); | 1156 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read); |
1158 | 1157 |
1159 if (delegate_) | 1158 delegate_->OnReadCompleted(this, bytes_read); |
1160 delegate_->OnReadCompleted(this, bytes_read); | |
1161 | 1159 |
1162 // Nothing below this line as OnReadCompleted may delete |this|. | 1160 // Nothing below this line as OnReadCompleted may delete |this|. |
1163 } | 1161 } |
1164 | 1162 |
1165 void URLRequest::OnHeadersComplete() { | 1163 void URLRequest::OnHeadersComplete() { |
1166 // Cache load timing information now, as information will be lost once the | 1164 // Cache load timing information now, as information will be lost once the |
1167 // socket is closed and the ClientSocketHandle is Reset, which will happen | 1165 // socket is closed and the ClientSocketHandle is Reset, which will happen |
1168 // once the body is complete. The start times should already be populated. | 1166 // once the body is complete. The start times should already be populated. |
1169 if (job_.get()) { | 1167 if (job_.get()) { |
1170 // Keep a copy of the two times the URLRequest sets. | 1168 // Keep a copy of the two times the URLRequest sets. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1221 } | 1219 } |
1222 | 1220 |
1223 void URLRequest::GetConnectionAttempts(ConnectionAttempts* out) const { | 1221 void URLRequest::GetConnectionAttempts(ConnectionAttempts* out) const { |
1224 if (job_) | 1222 if (job_) |
1225 job_->GetConnectionAttempts(out); | 1223 job_->GetConnectionAttempts(out); |
1226 else | 1224 else |
1227 out->clear(); | 1225 out->clear(); |
1228 } | 1226 } |
1229 | 1227 |
1230 } // namespace net | 1228 } // namespace net |
OLD | NEW |