| 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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 125 |
| 126 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request, | 126 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request, |
| 127 const SSLInfo& ssl_info, | 127 const SSLInfo& ssl_info, |
| 128 bool is_hsts_ok) { | 128 bool is_hsts_ok) { |
| 129 request->Cancel(); | 129 request->Cancel(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 /////////////////////////////////////////////////////////////////////////////// | 132 /////////////////////////////////////////////////////////////////////////////// |
| 133 // URLRequest | 133 // URLRequest |
| 134 | 134 |
| 135 URLRequest::URLRequest(const GURL& url, Delegate* delegate) | 135 URLRequest::URLRequest(const GURL& url, |
| 136 : context_(NULL), | 136 Delegate* delegate, |
| 137 const URLRequestContext* context) |
| 138 : context_(context), |
| 139 net_log_(BoundNetLog::Make(context->net_log(), |
| 140 NetLog::SOURCE_URL_REQUEST)), |
| 137 url_chain_(1, url), | 141 url_chain_(1, url), |
| 138 method_("GET"), | 142 method_("GET"), |
| 139 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE), | 143 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE), |
| 140 load_flags_(LOAD_NORMAL), | 144 load_flags_(LOAD_NORMAL), |
| 141 delegate_(delegate), | 145 delegate_(delegate), |
| 142 is_pending_(false), | 146 is_pending_(false), |
| 143 redirect_limit_(kMaxRedirects), | 147 redirect_limit_(kMaxRedirects), |
| 144 final_upload_progress_(0), | 148 final_upload_progress_(0), |
| 145 priority_(LOWEST), | 149 priority_(LOWEST), |
| 146 identifier_(GenerateURLRequestIdentifier()), | 150 identifier_(GenerateURLRequestIdentifier()), |
| 147 blocked_on_delegate_(false), | 151 blocked_on_delegate_(false), |
| 148 ALLOW_THIS_IN_INITIALIZER_LIST(before_request_callback_( | 152 ALLOW_THIS_IN_INITIALIZER_LIST(before_request_callback_( |
| 149 base::Bind(&URLRequest::BeforeRequestComplete, | 153 base::Bind(&URLRequest::BeforeRequestComplete, |
| 150 base::Unretained(this)))), | 154 base::Unretained(this)))), |
| 151 has_notified_completion_(false), | 155 has_notified_completion_(false), |
| 152 creation_time_(base::TimeTicks::Now()) { | 156 creation_time_(base::TimeTicks::Now()) { |
| 153 SIMPLE_STATS_COUNTER("URLRequestCount"); | 157 SIMPLE_STATS_COUNTER("URLRequestCount"); |
| 154 | 158 |
| 155 // Sanity check out environment. | 159 // Sanity check out environment. |
| 156 DCHECK(MessageLoop::current()) << | 160 DCHECK(MessageLoop::current()) << |
| 157 "The current MessageLoop must exist"; | 161 "The current MessageLoop must exist"; |
| 158 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << | 162 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << |
| 159 "The current MessageLoop must be TYPE_IO"; | 163 "The current MessageLoop must be TYPE_IO"; |
| 164 |
| 165 CHECK(context); |
| 166 context->url_requests()->insert(this); |
| 167 |
| 168 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); |
| 160 } | 169 } |
| 161 | 170 |
| 162 URLRequest::~URLRequest() { | 171 URLRequest::~URLRequest() { |
| 163 Cancel(); | 172 Cancel(); |
| 164 | 173 |
| 165 if (context_ && context_->network_delegate()) { | 174 if (context_ && context_->network_delegate()) { |
| 166 context_->network_delegate()->NotifyURLRequestDestroyed(this); | 175 context_->network_delegate()->NotifyURLRequestDestroyed(this); |
| 167 if (job_) | 176 if (job_) |
| 168 job_->NotifyURLRequestDestroyed(); | 177 job_->NotifyURLRequestDestroyed(); |
| 169 } | 178 } |
| 170 | 179 |
| 171 if (job_) | 180 if (job_) |
| 172 OrphanJob(); | 181 OrphanJob(); |
| 173 | 182 |
| 174 set_context(NULL); | 183 bool deleted = context_->url_requests()->erase(this); |
| 184 CHECK(deleted); |
| 185 |
| 186 int net_error = OK; |
| 187 // Log error only on failure, not cancellation, as even successful requests |
| 188 // are "cancelled" on destruction. |
| 189 if (status_.status() == URLRequestStatus::FAILED) |
| 190 net_error = status_.error(); |
| 191 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error); |
| 175 } | 192 } |
| 176 | 193 |
| 177 // static | 194 // static |
| 178 URLRequest::ProtocolFactory* URLRequest::RegisterProtocolFactory( | 195 URLRequest::ProtocolFactory* URLRequest::RegisterProtocolFactory( |
| 179 const string& scheme, ProtocolFactory* factory) { | 196 const string& scheme, ProtocolFactory* factory) { |
| 180 return URLRequestJobManager::GetInstance()->RegisterProtocolFactory(scheme, | 197 return URLRequestJobManager::GetInstance()->RegisterProtocolFactory(scheme, |
| 181 factory); | 198 factory); |
| 182 } | 199 } |
| 183 | 200 |
| 184 // static | 201 // static |
| (...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 | 737 |
| 721 PrepareToRestart(); | 738 PrepareToRestart(); |
| 722 Start(); | 739 Start(); |
| 723 return OK; | 740 return OK; |
| 724 } | 741 } |
| 725 | 742 |
| 726 const URLRequestContext* URLRequest::context() const { | 743 const URLRequestContext* URLRequest::context() const { |
| 727 return context_; | 744 return context_; |
| 728 } | 745 } |
| 729 | 746 |
| 730 void URLRequest::set_context(const URLRequestContext* context) { | |
| 731 // Update the URLRequest lists in the URLRequestContext. | |
| 732 if (context_) { | |
| 733 std::set<const URLRequest*>* url_requests = context_->url_requests(); | |
| 734 CHECK(ContainsKey(*url_requests, this)); | |
| 735 url_requests->erase(this); | |
| 736 } | |
| 737 | |
| 738 if (context) { | |
| 739 std::set<const URLRequest*>* url_requests = context->url_requests(); | |
| 740 CHECK(!ContainsKey(*url_requests, this)); | |
| 741 url_requests->insert(this); | |
| 742 } | |
| 743 | |
| 744 const URLRequestContext* prev_context = context_; | |
| 745 | |
| 746 context_ = context; | |
| 747 | |
| 748 // If the context this request belongs to has changed, update the tracker. | |
| 749 if (prev_context != context) { | |
| 750 int net_error = OK; | |
| 751 // Log error only on failure, not cancellation, as even successful requests | |
| 752 // are "cancelled" on destruction. | |
| 753 if (status_.status() == URLRequestStatus::FAILED) | |
| 754 net_error = status_.error(); | |
| 755 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error); | |
| 756 net_log_ = BoundNetLog(); | |
| 757 | |
| 758 if (context) { | |
| 759 net_log_ = BoundNetLog::Make(context->net_log(), | |
| 760 NetLog::SOURCE_URL_REQUEST); | |
| 761 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); | |
| 762 } | |
| 763 } | |
| 764 } | |
| 765 | |
| 766 int64 URLRequest::GetExpectedContentSize() const { | 747 int64 URLRequest::GetExpectedContentSize() const { |
| 767 int64 expected_content_size = -1; | 748 int64 expected_content_size = -1; |
| 768 if (job_) | 749 if (job_) |
| 769 expected_content_size = job_->expected_content_size(); | 750 expected_content_size = job_->expected_content_size(); |
| 770 | 751 |
| 771 return expected_content_size; | 752 return expected_content_size; |
| 772 } | 753 } |
| 773 | 754 |
| 774 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const { | 755 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const { |
| 775 const GURL& url = this->url(); | 756 const GURL& url = this->url(); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 923 new base::debug::StackTrace(NULL, 0); | 904 new base::debug::StackTrace(NULL, 0); |
| 924 *stack_trace_copy = stack_trace; | 905 *stack_trace_copy = stack_trace; |
| 925 stack_trace_.reset(stack_trace_copy); | 906 stack_trace_.reset(stack_trace_copy); |
| 926 } | 907 } |
| 927 | 908 |
| 928 const base::debug::StackTrace* URLRequest::stack_trace() const { | 909 const base::debug::StackTrace* URLRequest::stack_trace() const { |
| 929 return stack_trace_.get(); | 910 return stack_trace_.get(); |
| 930 } | 911 } |
| 931 | 912 |
| 932 } // namespace net | 913 } // namespace net |
| OLD | NEW |