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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request, | 193 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request, |
194 bool* defer) { | 194 bool* defer) { |
195 } | 195 } |
196 | 196 |
197 /////////////////////////////////////////////////////////////////////////////// | 197 /////////////////////////////////////////////////////////////////////////////// |
198 // URLRequest | 198 // URLRequest |
199 | 199 |
200 URLRequest::URLRequest(const GURL& url, | 200 URLRequest::URLRequest(const GURL& url, |
201 RequestPriority priority, | 201 RequestPriority priority, |
202 Delegate* delegate, | 202 Delegate* delegate, |
203 const URLRequestContext* context) | |
204 : identifier_(GenerateURLRequestIdentifier()) { | |
205 Init(url, priority, delegate, context, NULL); | |
206 } | |
207 | |
208 URLRequest::URLRequest(const GURL& url, | |
209 RequestPriority priority, | |
210 Delegate* delegate, | |
211 const URLRequestContext* context, | 203 const URLRequestContext* context, |
212 CookieStore* cookie_store) | 204 CookieStore* cookie_store, |
213 : identifier_(GenerateURLRequestIdentifier()) { | 205 NetworkDelegate* network_delegate) |
214 Init(url, priority, delegate, context, cookie_store); | 206 : context_(context), |
| 207 network_delegate_(network_delegate ? network_delegate |
| 208 : context->network_delegate()), |
| 209 net_log_(BoundNetLog::Make(context->net_log(), |
| 210 NetLog::SOURCE_URL_REQUEST)), |
| 211 url_chain_(1, url), |
| 212 method_("GET"), |
| 213 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE), |
| 214 load_flags_(LOAD_NORMAL), |
| 215 delegate_(delegate), |
| 216 is_pending_(false), |
| 217 is_redirecting_(false), |
| 218 redirect_limit_(kMaxRedirects), |
| 219 priority_(priority), |
| 220 identifier_(GenerateURLRequestIdentifier()), |
| 221 calling_delegate_(false), |
| 222 use_blocked_by_as_load_param_(false), |
| 223 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete, |
| 224 base::Unretained(this))), |
| 225 has_notified_completion_(false), |
| 226 received_response_content_length_(0), |
| 227 creation_time_(base::TimeTicks::Now()), |
| 228 notified_before_network_start_(false), |
| 229 cookie_store_(cookie_store ? cookie_store : context->cookie_store()) { |
| 230 SIMPLE_STATS_COUNTER("URLRequestCount"); |
| 231 |
| 232 // Sanity check out environment. |
| 233 DCHECK(base::MessageLoop::current()) |
| 234 << "The current base::MessageLoop must exist"; |
| 235 |
| 236 context->url_requests()->insert(this); |
| 237 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); |
215 } | 238 } |
216 | 239 |
217 URLRequest::~URLRequest() { | 240 URLRequest::~URLRequest() { |
218 Cancel(); | 241 Cancel(); |
219 | 242 |
220 if (network_delegate_) { | 243 if (network_delegate_) { |
221 network_delegate_->NotifyURLRequestDestroyed(this); | 244 network_delegate_->NotifyURLRequestDestroyed(this); |
222 if (job_.get()) | 245 if (job_.get()) |
223 job_->NotifyURLRequestDestroyed(); | 246 job_->NotifyURLRequestDestroyed(); |
224 } | 247 } |
(...skipping 16 matching lines...) Expand all Loading... |
241 void URLRequest::RegisterRequestInterceptor(Interceptor* interceptor) { | 264 void URLRequest::RegisterRequestInterceptor(Interceptor* interceptor) { |
242 URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(interceptor); | 265 URLRequestJobManager::GetInstance()->RegisterRequestInterceptor(interceptor); |
243 } | 266 } |
244 | 267 |
245 // static | 268 // static |
246 void URLRequest::UnregisterRequestInterceptor(Interceptor* interceptor) { | 269 void URLRequest::UnregisterRequestInterceptor(Interceptor* interceptor) { |
247 URLRequestJobManager::GetInstance()->UnregisterRequestInterceptor( | 270 URLRequestJobManager::GetInstance()->UnregisterRequestInterceptor( |
248 interceptor); | 271 interceptor); |
249 } | 272 } |
250 | 273 |
251 void URLRequest::Init(const GURL& url, | |
252 RequestPriority priority, | |
253 Delegate* delegate, | |
254 const URLRequestContext* context, | |
255 CookieStore* cookie_store) { | |
256 context_ = context; | |
257 network_delegate_ = context->network_delegate(); | |
258 net_log_ = BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST); | |
259 url_chain_.push_back(url); | |
260 method_ = "GET"; | |
261 referrer_policy_ = CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE; | |
262 load_flags_ = LOAD_NORMAL; | |
263 delegate_ = delegate; | |
264 is_pending_ = false; | |
265 is_redirecting_ = false; | |
266 redirect_limit_ = kMaxRedirects; | |
267 priority_ = priority; | |
268 calling_delegate_ = false; | |
269 use_blocked_by_as_load_param_ =false; | |
270 before_request_callback_ = base::Bind(&URLRequest::BeforeRequestComplete, | |
271 base::Unretained(this)); | |
272 has_notified_completion_ = false; | |
273 received_response_content_length_ = 0; | |
274 creation_time_ = base::TimeTicks::Now(); | |
275 notified_before_network_start_ = false; | |
276 | |
277 SIMPLE_STATS_COUNTER("URLRequestCount"); | |
278 | |
279 // Sanity check out environment. | |
280 DCHECK(base::MessageLoop::current()) | |
281 << "The current base::MessageLoop must exist"; | |
282 | |
283 CHECK(context); | |
284 context->url_requests()->insert(this); | |
285 cookie_store_ = cookie_store; | |
286 if (cookie_store_ == NULL) | |
287 cookie_store_ = context->cookie_store(); | |
288 | |
289 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); | |
290 } | |
291 | |
292 void URLRequest::EnableChunkedUpload() { | 274 void URLRequest::EnableChunkedUpload() { |
293 DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked()); | 275 DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked()); |
294 if (!upload_data_stream_) { | 276 if (!upload_data_stream_) { |
295 upload_data_stream_.reset( | 277 upload_data_stream_.reset( |
296 new UploadDataStream(UploadDataStream::CHUNKED, 0)); | 278 new UploadDataStream(UploadDataStream::CHUNKED, 0)); |
297 } | 279 } |
298 } | 280 } |
299 | 281 |
300 void URLRequest::AppendChunkToUpload(const char* bytes, | 282 void URLRequest::AppendChunkToUpload(const char* bytes, |
301 int bytes_len, | 283 int bytes_len, |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 } | 589 } |
608 | 590 |
609 void URLRequest::set_delegate(Delegate* delegate) { | 591 void URLRequest::set_delegate(Delegate* delegate) { |
610 delegate_ = delegate; | 592 delegate_ = delegate; |
611 } | 593 } |
612 | 594 |
613 void URLRequest::Start() { | 595 void URLRequest::Start() { |
614 // Some values can be NULL, but the job factory must not be. | 596 // Some values can be NULL, but the job factory must not be. |
615 DCHECK(context_->job_factory()); | 597 DCHECK(context_->job_factory()); |
616 | 598 |
617 DCHECK_EQ(network_delegate_, context_->network_delegate()); | |
618 // Anything that sets |blocked_by_| before start should have cleaned up after | 599 // Anything that sets |blocked_by_| before start should have cleaned up after |
619 // itself. | 600 // itself. |
620 DCHECK(blocked_by_.empty()); | 601 DCHECK(blocked_by_.empty()); |
621 | 602 |
622 g_url_requests_started = true; | 603 g_url_requests_started = true; |
623 response_info_.request_time = base::Time::Now(); | 604 response_info_.request_time = base::Time::Now(); |
624 | 605 |
625 load_timing_info_ = LoadTimingInfo(); | 606 load_timing_info_ = LoadTimingInfo(); |
626 load_timing_info_.request_start_time = response_info_.request_time; | 607 load_timing_info_.request_start_time = response_info_.request_time; |
627 load_timing_info_.request_start = base::TimeTicks::Now(); | 608 load_timing_info_.request_start = base::TimeTicks::Now(); |
(...skipping 12 matching lines...) Expand all Loading... |
640 | 621 |
641 StartJob(URLRequestJobManager::GetInstance()->CreateJob( | 622 StartJob(URLRequestJobManager::GetInstance()->CreateJob( |
642 this, network_delegate_)); | 623 this, network_delegate_)); |
643 } | 624 } |
644 | 625 |
645 /////////////////////////////////////////////////////////////////////////////// | 626 /////////////////////////////////////////////////////////////////////////////// |
646 | 627 |
647 void URLRequest::BeforeRequestComplete(int error) { | 628 void URLRequest::BeforeRequestComplete(int error) { |
648 DCHECK(!job_.get()); | 629 DCHECK(!job_.get()); |
649 DCHECK_NE(ERR_IO_PENDING, error); | 630 DCHECK_NE(ERR_IO_PENDING, error); |
650 DCHECK_EQ(network_delegate_, context_->network_delegate()); | |
651 | 631 |
652 // Check that there are no callbacks to already canceled requests. | 632 // Check that there are no callbacks to already canceled requests. |
653 DCHECK_NE(URLRequestStatus::CANCELED, status_.status()); | 633 DCHECK_NE(URLRequestStatus::CANCELED, status_.status()); |
654 | 634 |
655 OnCallToDelegateComplete(); | 635 OnCallToDelegateComplete(); |
656 | 636 |
657 if (error != OK) { | 637 if (error != OK) { |
658 std::string source("delegate"); | 638 std::string source("delegate"); |
659 net_log_.AddEvent(NetLog::TYPE_CANCELLED, | 639 net_log_.AddEvent(NetLog::TYPE_CANCELLED, |
660 NetLog::StringCallback("source", &source)); | 640 NetLog::StringCallback("source", &source)); |
(...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1232 new base::debug::StackTrace(NULL, 0); | 1212 new base::debug::StackTrace(NULL, 0); |
1233 *stack_trace_copy = stack_trace; | 1213 *stack_trace_copy = stack_trace; |
1234 stack_trace_.reset(stack_trace_copy); | 1214 stack_trace_.reset(stack_trace_copy); |
1235 } | 1215 } |
1236 | 1216 |
1237 const base::debug::StackTrace* URLRequest::stack_trace() const { | 1217 const base::debug::StackTrace* URLRequest::stack_trace() const { |
1238 return stack_trace_.get(); | 1218 return stack_trace_.get(); |
1239 } | 1219 } |
1240 | 1220 |
1241 } // namespace net | 1221 } // namespace net |
OLD | NEW |