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

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

Issue 10559036: Added URLRequestContext to constructor for URLRequest. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Updated unittests Created 8 years, 6 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 "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
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),
137 url_chain_(1, url), 139 url_chain_(1, url),
138 method_("GET"), 140 method_("GET"),
139 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE), 141 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE),
140 load_flags_(LOAD_NORMAL), 142 load_flags_(LOAD_NORMAL),
141 delegate_(delegate), 143 delegate_(delegate),
142 is_pending_(false), 144 is_pending_(false),
143 redirect_limit_(kMaxRedirects), 145 redirect_limit_(kMaxRedirects),
144 final_upload_progress_(0), 146 final_upload_progress_(0),
145 priority_(LOWEST), 147 priority_(LOWEST),
146 identifier_(GenerateURLRequestIdentifier()), 148 identifier_(GenerateURLRequestIdentifier()),
147 blocked_on_delegate_(false), 149 blocked_on_delegate_(false),
148 ALLOW_THIS_IN_INITIALIZER_LIST(before_request_callback_( 150 ALLOW_THIS_IN_INITIALIZER_LIST(before_request_callback_(
149 base::Bind(&URLRequest::BeforeRequestComplete, 151 base::Bind(&URLRequest::BeforeRequestComplete,
150 base::Unretained(this)))), 152 base::Unretained(this)))),
151 has_notified_completion_(false), 153 has_notified_completion_(false),
152 creation_time_(base::TimeTicks::Now()) { 154 creation_time_(base::TimeTicks::Now()) {
153 SIMPLE_STATS_COUNTER("URLRequestCount"); 155 SIMPLE_STATS_COUNTER("URLRequestCount");
154 156
155 // Sanity check out environment. 157 // Sanity check out environment.
156 DCHECK(MessageLoop::current()) << 158 DCHECK(MessageLoop::current()) <<
157 "The current MessageLoop must exist"; 159 "The current MessageLoop must exist";
158 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << 160 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) <<
159 "The current MessageLoop must be TYPE_IO"; 161 "The current MessageLoop must be TYPE_IO";
162
163 std::set<const URLRequest*>* url_requests = context->url_requests();
erikwright (departed) 2012/06/19 21:06:22 since we are removing the DCHECK, you can remove t
shalev 2012/06/20 20:13:12 Done.
164 CHECK(!ContainsKey(*url_requests, this));
erikwright (departed) 2012/06/19 21:06:22 This CHECK can be removed now, since it's pretty m
shalev 2012/06/20 20:13:12 Done.
shalev 2012/06/20 20:13:12 Done.
165 url_requests->insert(this);
166
167 net_log_ = BoundNetLog::Make(context->net_log(),
168 NetLog::SOURCE_URL_REQUEST);
169 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE, NULL);
160 } 170 }
161 171
162 URLRequest::~URLRequest() { 172 URLRequest::~URLRequest() {
163 Cancel(); 173 Cancel();
164 174
165 if (context_ && context_->network_delegate()) { 175 if (context_ && context_->network_delegate()) {
166 context_->network_delegate()->NotifyURLRequestDestroyed(this); 176 context_->network_delegate()->NotifyURLRequestDestroyed(this);
167 if (job_) 177 if (job_)
168 job_->NotifyURLRequestDestroyed(); 178 job_->NotifyURLRequestDestroyed();
169 } 179 }
170 180
171 if (job_) 181 if (job_)
172 OrphanJob(); 182 OrphanJob();
173 183
174 set_context(NULL); 184 std::set<const URLRequest*>* url_requests = context_->url_requests();
185 CHECK(ContainsKey(*url_requests, this));
186 url_requests->erase(this);
erikwright (departed) 2012/06/19 21:06:22 inline url_requests()
shalev 2012/06/20 20:13:12 Done.
187
188 int net_error = OK;
189 // Log error only on failure, not cancellation, as even successful requests
190 // are "cancelled" on destruction.
191 if (status_.status() == URLRequestStatus::FAILED)
192 net_error = status_.error();
193 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error);
175 } 194 }
176 195
177 // static 196 // static
178 URLRequest::ProtocolFactory* URLRequest::RegisterProtocolFactory( 197 URLRequest::ProtocolFactory* URLRequest::RegisterProtocolFactory(
179 const string& scheme, ProtocolFactory* factory) { 198 const string& scheme, ProtocolFactory* factory) {
180 return URLRequestJobManager::GetInstance()->RegisterProtocolFactory(scheme, 199 return URLRequestJobManager::GetInstance()->RegisterProtocolFactory(scheme,
181 factory); 200 factory);
182 } 201 }
183 202
184 // static 203 // static
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 738
720 PrepareToRestart(); 739 PrepareToRestart();
721 Start(); 740 Start();
722 return OK; 741 return OK;
723 } 742 }
724 743
725 const URLRequestContext* URLRequest::context() const { 744 const URLRequestContext* URLRequest::context() const {
726 return context_; 745 return context_;
727 } 746 }
728 747
729 void URLRequest::set_context(const URLRequestContext* context) {
730 // Update the URLRequest lists in the URLRequestContext.
731 if (context_) {
732 std::set<const URLRequest*>* url_requests = context_->url_requests();
733 CHECK(ContainsKey(*url_requests, this));
734 url_requests->erase(this);
735 }
736
737 if (context) {
738 std::set<const URLRequest*>* url_requests = context->url_requests();
739 CHECK(!ContainsKey(*url_requests, this));
740 url_requests->insert(this);
741 }
742
743 const URLRequestContext* prev_context = context_;
744
745 context_ = context;
746
747 // If the context this request belongs to has changed, update the tracker.
748 if (prev_context != context) {
749 int net_error = OK;
750 // Log error only on failure, not cancellation, as even successful requests
751 // are "cancelled" on destruction.
752 if (status_.status() == URLRequestStatus::FAILED)
753 net_error = status_.error();
754 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error);
755 net_log_ = BoundNetLog();
756
757 if (context) {
758 net_log_ = BoundNetLog::Make(context->net_log(),
759 NetLog::SOURCE_URL_REQUEST);
760 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE, NULL);
761 }
762 }
763 }
764
765 int64 URLRequest::GetExpectedContentSize() const { 748 int64 URLRequest::GetExpectedContentSize() const {
766 int64 expected_content_size = -1; 749 int64 expected_content_size = -1;
767 if (job_) 750 if (job_)
768 expected_content_size = job_->expected_content_size(); 751 expected_content_size = job_->expected_content_size();
769 752
770 return expected_content_size; 753 return expected_content_size;
771 } 754 }
772 755
773 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const { 756 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const {
774 const GURL& url = this->url(); 757 const GURL& url = this->url();
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
922 new base::debug::StackTrace(NULL, 0); 905 new base::debug::StackTrace(NULL, 0);
923 *stack_trace_copy = stack_trace; 906 *stack_trace_copy = stack_trace;
924 stack_trace_.reset(stack_trace_copy); 907 stack_trace_.reset(stack_trace_copy);
925 } 908 }
926 909
927 const base::debug::StackTrace* URLRequest::stack_trace() const { 910 const base::debug::StackTrace* URLRequest::stack_trace() const {
928 return stack_trace_.get(); 911 return stack_trace_.get();
929 } 912 }
930 913
931 } // namespace net 914 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698