| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/metrics/stats_counters.h" | 10 #include "base/metrics/stats_counters.h" |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 URLRequest::URLRequest(const GURL& url, Delegate* delegate) | 134 URLRequest::URLRequest(const GURL& url, Delegate* delegate) |
| 135 : url_chain_(1, url), | 135 : url_chain_(1, url), |
| 136 method_("GET"), | 136 method_("GET"), |
| 137 load_flags_(LOAD_NORMAL), | 137 load_flags_(LOAD_NORMAL), |
| 138 delegate_(delegate), | 138 delegate_(delegate), |
| 139 is_pending_(false), | 139 is_pending_(false), |
| 140 redirect_limit_(kMaxRedirects), | 140 redirect_limit_(kMaxRedirects), |
| 141 final_upload_progress_(0), | 141 final_upload_progress_(0), |
| 142 priority_(LOWEST), | 142 priority_(LOWEST), |
| 143 identifier_(GenerateURLRequestIdentifier()), | 143 identifier_(GenerateURLRequestIdentifier()), |
| 144 blocked_on_delegate_(false), |
| 144 ALLOW_THIS_IN_INITIALIZER_LIST( | 145 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 145 before_request_callback_(this, &URLRequest::BeforeRequestComplete)), | 146 before_request_callback_(this, &URLRequest::BeforeRequestComplete)), |
| 146 has_notified_completion_(false) { | 147 has_notified_completion_(false) { |
| 147 SIMPLE_STATS_COUNTER("URLRequestCount"); | 148 SIMPLE_STATS_COUNTER("URLRequestCount"); |
| 148 | 149 |
| 149 // Sanity check out environment. | 150 // Sanity check out environment. |
| 150 DCHECK(MessageLoop::current()) << | 151 DCHECK(MessageLoop::current()) << |
| 151 "The current MessageLoop must exist"; | 152 "The current MessageLoop must exist"; |
| 152 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << | 153 DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) << |
| 153 "The current MessageLoop must be TYPE_IO"; | 154 "The current MessageLoop must be TYPE_IO"; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 248 |
| 248 void URLRequest::SetExtraRequestHeaders( | 249 void URLRequest::SetExtraRequestHeaders( |
| 249 const HttpRequestHeaders& headers) { | 250 const HttpRequestHeaders& headers) { |
| 250 DCHECK(!is_pending_); | 251 DCHECK(!is_pending_); |
| 251 extra_request_headers_ = headers; | 252 extra_request_headers_ = headers; |
| 252 | 253 |
| 253 // NOTE: This method will likely become non-trivial once the other setters | 254 // NOTE: This method will likely become non-trivial once the other setters |
| 254 // for request headers are implemented. | 255 // for request headers are implemented. |
| 255 } | 256 } |
| 256 | 257 |
| 257 LoadState URLRequest::GetLoadState() const { | 258 LoadStateWithParam URLRequest::GetLoadState() const { |
| 258 return job_ ? job_->GetLoadState() : LOAD_STATE_IDLE; | 259 if (blocked_on_delegate_) { |
| 260 return LoadStateWithParam(LOAD_STATE_WAITING_FOR_DELEGATE, |
| 261 load_state_param_); |
| 262 } |
| 263 return LoadStateWithParam(job_ ? job_->GetLoadState() : LOAD_STATE_IDLE, |
| 264 string16()); |
| 259 } | 265 } |
| 260 | 266 |
| 261 uint64 URLRequest::GetUploadProgress() const { | 267 uint64 URLRequest::GetUploadProgress() const { |
| 262 if (!job_) { | 268 if (!job_) { |
| 263 // We haven't started or the request was cancelled | 269 // We haven't started or the request was cancelled |
| 264 return 0; | 270 return 0; |
| 265 } | 271 } |
| 266 if (final_upload_progress_) { | 272 if (final_upload_progress_) { |
| 267 // The first job completed and none of the subsequent series of | 273 // The first job completed and none of the subsequent series of |
| 268 // GETs when following redirects will upload anything, so we return the | 274 // GETs when following redirects will upload anything, so we return the |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 } | 399 } |
| 394 | 400 |
| 395 void URLRequest::Start() { | 401 void URLRequest::Start() { |
| 396 response_info_.request_time = Time::Now(); | 402 response_info_.request_time = Time::Now(); |
| 397 | 403 |
| 398 // Only notify the delegate for the initial request. | 404 // Only notify the delegate for the initial request. |
| 399 if (context_ && context_->network_delegate()) { | 405 if (context_ && context_->network_delegate()) { |
| 400 if (context_->network_delegate()->NotifyBeforeURLRequest( | 406 if (context_->network_delegate()->NotifyBeforeURLRequest( |
| 401 this, &before_request_callback_, &delegate_redirect_url_) == | 407 this, &before_request_callback_, &delegate_redirect_url_) == |
| 402 net::ERR_IO_PENDING) { | 408 net::ERR_IO_PENDING) { |
| 403 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL); | 409 SetBlockedOnDelegate(); |
| 404 return; // paused | 410 return; // paused |
| 405 } | 411 } |
| 406 } | 412 } |
| 407 | 413 |
| 408 StartJob(URLRequestJobManager::GetInstance()->CreateJob(this)); | 414 StartJob(URLRequestJobManager::GetInstance()->CreateJob(this)); |
| 409 } | 415 } |
| 410 | 416 |
| 411 /////////////////////////////////////////////////////////////////////////////// | 417 /////////////////////////////////////////////////////////////////////////////// |
| 412 | 418 |
| 413 void URLRequest::BeforeRequestComplete(int error) { | 419 void URLRequest::BeforeRequestComplete(int error) { |
| 414 DCHECK(!job_); | 420 DCHECK(!job_); |
| 415 DCHECK_NE(ERR_IO_PENDING, error); | 421 DCHECK_NE(ERR_IO_PENDING, error); |
| 416 | 422 |
| 417 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL); | 423 SetUnblockedOnDelegate(); |
| 418 if (error != OK) { | 424 if (error != OK) { |
| 419 net_log_.AddEvent(NetLog::TYPE_CANCELLED, | 425 net_log_.AddEvent(NetLog::TYPE_CANCELLED, |
| 420 make_scoped_refptr(new NetLogStringParameter("source", "delegate"))); | 426 make_scoped_refptr(new NetLogStringParameter("source", "delegate"))); |
| 421 StartJob(new URLRequestErrorJob(this, error)); | 427 StartJob(new URLRequestErrorJob(this, error)); |
| 422 } else if (!delegate_redirect_url_.is_empty()) { | 428 } else if (!delegate_redirect_url_.is_empty()) { |
| 423 GURL new_url; | 429 GURL new_url; |
| 424 new_url.Swap(&delegate_redirect_url_); | 430 new_url.Swap(&delegate_redirect_url_); |
| 425 StartJob(new URLRequestRedirectJob(this, new_url)); | 431 StartJob(new URLRequestRedirectJob(this, new_url)); |
| 426 } else { | 432 } else { |
| 427 StartJob(URLRequestJobManager::GetInstance()->CreateJob(this)); | 433 StartJob(URLRequestJobManager::GetInstance()->CreateJob(this)); |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 // not be needed. | 803 // not be needed. |
| 798 if (has_notified_completion_) | 804 if (has_notified_completion_) |
| 799 return; | 805 return; |
| 800 | 806 |
| 801 is_pending_ = false; | 807 is_pending_ = false; |
| 802 has_notified_completion_ = true; | 808 has_notified_completion_ = true; |
| 803 if (context_ && context_->network_delegate()) | 809 if (context_ && context_->network_delegate()) |
| 804 context_->network_delegate()->NotifyCompleted(this); | 810 context_->network_delegate()->NotifyCompleted(this); |
| 805 } | 811 } |
| 806 | 812 |
| 813 void URLRequest::SetBlockedOnDelegate() { |
| 814 blocked_on_delegate_ = true; |
| 815 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL); |
| 816 } |
| 817 |
| 818 void URLRequest::SetUnblockedOnDelegate() { |
| 819 blocked_on_delegate_ = false; |
| 820 load_state_param_.clear(); |
| 821 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_BLOCKED_ON_DELEGATE, NULL); |
| 822 } |
| 823 |
| 807 } // namespace net | 824 } // namespace net |
| OLD | NEW |