diff options
author | shalev@chromium.org <shalev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-24 21:49:20 +0000 |
---|---|---|
committer | shalev@chromium.org <shalev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-24 21:49:20 +0000 |
commit | 8a26ff60a6831b1c932353b35d8b7181771054ef (patch) | |
tree | eb841261055bedf1f96eb8d5ad3086264eb03e04 /net | |
parent | a1f52889a7eb791892e55fe6fd6e8567b6842ca1 (diff) | |
download | chromium_src-8a26ff60a6831b1c932353b35d8b7181771054ef.zip chromium_src-8a26ff60a6831b1c932353b35d8b7181771054ef.tar.gz chromium_src-8a26ff60a6831b1c932353b35d8b7181771054ef.tar.bz2 |
Added URLRequestContext::CreateRequest which can be used to create URLRequests
BUG=142945
Review URL: https://chromiumcodereview.appspot.com/10873056
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@153302 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/proxy/proxy_script_fetcher_impl.cc | 2 | ||||
-rw-r--r-- | net/test/spawner_communicator.cc | 5 | ||||
-rw-r--r-- | net/url_request/url_fetcher_core.cc | 6 | ||||
-rw-r--r-- | net/url_request/url_request.cc | 92 | ||||
-rw-r--r-- | net/url_request/url_request.h | 10 | ||||
-rw-r--r-- | net/url_request/url_request_context.cc | 5 | ||||
-rw-r--r-- | net/url_request/url_request_context.h | 4 |
7 files changed, 89 insertions, 35 deletions
diff --git a/net/proxy/proxy_script_fetcher_impl.cc b/net/proxy/proxy_script_fetcher_impl.cc index 45adb6a..6c1b4f2 100644 --- a/net/proxy/proxy_script_fetcher_impl.cc +++ b/net/proxy/proxy_script_fetcher_impl.cc @@ -134,7 +134,7 @@ int ProxyScriptFetcherImpl::Fetch( return OK; } - cur_request_.reset(new URLRequest(url, this, url_request_context_)); + cur_request_.reset(url_request_context_->CreateRequest(url, this)); cur_request_->set_method("GET"); // Make sure that the PAC script is downloaded using a direct connection, diff --git a/net/test/spawner_communicator.cc b/net/test/spawner_communicator.cc index 7b12e24..3a5874a 100644 --- a/net/test/spawner_communicator.cc +++ b/net/test/spawner_communicator.cc @@ -169,9 +169,8 @@ void SpawnerCommunicator::SendCommandAndWaitForResultOnIOThread( // Prepare the URLRequest for sending the command. DCHECK(!cur_request_.get()); context_.reset(new TestURLRequestContext); - cur_request_.reset(new URLRequest(GenerateSpawnerCommandURL(command, port_), - this, - context_.get())); + cur_request_.reset(context_->CreateRequest( + GenerateSpawnerCommandURL(command, port_), this)); DCHECK(cur_request_.get()); int current_request_id = ++next_id_; SpawnerRequestData* data = new SpawnerRequestData(current_request_id, diff --git a/net/url_request/url_fetcher_core.cc b/net/url_request/url_fetcher_core.cc index 055dce0..9c64c6e 100644 --- a/net/url_request/url_fetcher_core.cc +++ b/net/url_request/url_fetcher_core.cc @@ -663,10 +663,8 @@ void URLFetcherCore::StartURLRequest() { g_registry.Get().AddURLFetcherCore(this); current_response_bytes_ = 0; - request_.reset(new URLRequest( - original_url_, - this, - request_context_getter_->GetURLRequestContext())); + request_.reset(request_context_getter_->GetURLRequestContext()->CreateRequest( + original_url_, this)); request_->set_stack_trace(stack_trace_); int flags = request_->load_flags() | load_flags_; if (!g_interception_enabled) diff --git a/net/url_request/url_request.cc b/net/url_request/url_request.cc index ec48c6a..9084053 100644 --- a/net/url_request/url_request.cc +++ b/net/url_request/url_request.cc @@ -134,10 +134,50 @@ void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request, /////////////////////////////////////////////////////////////////////////////// // URLRequest +// TODO(shalev): Get rid of this constructor in favour of the one below it. URLRequest::URLRequest(const GURL& url, Delegate* delegate, const URLRequestContext* context) : context_(context), + network_delegate_(context->network_delegate()), + net_log_(BoundNetLog::Make(context->net_log(), + NetLog::SOURCE_URL_REQUEST)), + url_chain_(1, url), + method_("GET"), + referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE), + load_flags_(LOAD_NORMAL), + delegate_(delegate), + is_pending_(false), + redirect_limit_(kMaxRedirects), + final_upload_progress_(0), + priority_(LOWEST), + identifier_(GenerateURLRequestIdentifier()), + blocked_on_delegate_(false), + ALLOW_THIS_IN_INITIALIZER_LIST(before_request_callback_( + base::Bind(&URLRequest::BeforeRequestComplete, + base::Unretained(this)))), + has_notified_completion_(false), + creation_time_(base::TimeTicks::Now()) { + SIMPLE_STATS_COUNTER("URLRequestCount"); + + // Sanity check out environment. + DCHECK(MessageLoop::current()) << "The current MessageLoop must exist"; + + DCHECK(MessageLoop::current()->IsType(MessageLoop::TYPE_IO)) << "" + "The current MessageLoop must be TYPE_IO"; + + CHECK(context); + context->url_requests()->insert(this); + + net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); +} + +URLRequest::URLRequest(const GURL& url, + Delegate* delegate, + const URLRequestContext* context, + NetworkDelegate* network_delegate) + : context_(context), + network_delegate_(network_delegate), net_log_(BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST)), url_chain_(1, url), @@ -173,8 +213,8 @@ URLRequest::URLRequest(const GURL& url, URLRequest::~URLRequest() { Cancel(); - if (context_->network_delegate()) { - context_->network_delegate()->NotifyURLRequestDestroyed(this); + if (network_delegate_) { + network_delegate_->NotifyURLRequestDestroyed(this); if (job_) job_->NotifyURLRequestDestroyed(); } @@ -413,12 +453,14 @@ void URLRequest::set_delegate(Delegate* delegate) { } void URLRequest::Start() { + DCHECK_EQ(network_delegate_, context_->network_delegate()); + g_url_requests_started = true; response_info_.request_time = Time::Now(); // Only notify the delegate for the initial request. - if (context_->network_delegate()) { - int error = context_->network_delegate()->NotifyBeforeURLRequest( + if (network_delegate_) { + int error = network_delegate_->NotifyBeforeURLRequest( this, before_request_callback_, &delegate_redirect_url_); if (error == net::ERR_IO_PENDING) { // Paused on the delegate, will invoke |before_request_callback_| later. @@ -430,7 +472,7 @@ void URLRequest::Start() { } StartJob(URLRequestJobManager::GetInstance()->CreateJob( - this, context_->network_delegate())); + this, network_delegate_)); } /////////////////////////////////////////////////////////////////////////////// @@ -438,6 +480,7 @@ void URLRequest::Start() { void URLRequest::BeforeRequestComplete(int error) { DCHECK(!job_); DCHECK_NE(ERR_IO_PENDING, error); + DCHECK_EQ(network_delegate_, context_->network_delegate()); // Check that there are no callbacks to already canceled requests. DCHECK_NE(URLRequestStatus::CANCELED, status_.status()); @@ -449,20 +492,20 @@ void URLRequest::BeforeRequestComplete(int error) { std::string source("delegate"); net_log_.AddEvent(NetLog::TYPE_CANCELLED, NetLog::StringCallback("source", &source)); - StartJob(new URLRequestErrorJob(this, context_->network_delegate(), error)); + StartJob(new URLRequestErrorJob(this, network_delegate_, error)); } else if (!delegate_redirect_url_.is_empty()) { GURL new_url; new_url.Swap(&delegate_redirect_url_); URLRequestRedirectJob* job = new URLRequestRedirectJob( - this, context_->network_delegate(), new_url); + this, network_delegate_, new_url); // Use status code 307 to preserve the method, so POST requests work. job->set_redirect_code( URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT); StartJob(job); } else { StartJob(URLRequestJobManager::GetInstance()->CreateJob( - this, context_->network_delegate())); + this, network_delegate_)); } } @@ -496,7 +539,7 @@ void URLRequest::Restart() { // Should only be called if the original job didn't make any progress. DCHECK(job_ && !job_->has_response_started()); RestartWithJob(URLRequestJobManager::GetInstance()->CreateJob( - this, context_->network_delegate())); + this, network_delegate_)); } void URLRequest::RestartWithJob(URLRequestJob *job) { @@ -592,7 +635,7 @@ void URLRequest::NotifyReceivedRedirect(const GURL& location, bool* defer_redirect) { URLRequestJob* job = URLRequestJobManager::GetInstance()->MaybeInterceptRedirect( - this, context_->network_delegate(), location); + this, network_delegate_, location); if (job) { RestartWithJob(job); } else if (delegate_) { @@ -609,7 +652,7 @@ void URLRequest::NotifyResponseStarted() { URLRequestJob* job = URLRequestJobManager::GetInstance()->MaybeInterceptResponse( - this, context_->network_delegate()); + this, network_delegate_); if (job) { RestartWithJob(job); } else { @@ -617,8 +660,8 @@ void URLRequest::NotifyResponseStarted() { // In some cases (e.g. an event was canceled), we might have sent the // completion event and receive a NotifyResponseStarted() later. if (!has_notified_completion_ && status_.is_success()) { - if (context_->network_delegate()) - context_->network_delegate()->NotifyResponseStarted(this); + if (network_delegate_) + network_delegate_->NotifyResponseStarted(this); } // Notify in case the entire URL Request has been finished. @@ -701,8 +744,8 @@ int URLRequest::Redirect(const GURL& location, int http_status_code) { NetLog::StringCallback("location", &location.possibly_invalid_spec())); } - if (context_->network_delegate()) - context_->network_delegate()->NotifyBeforeRedirect(this, location); + if (network_delegate_) + network_delegate_->NotifyBeforeRedirect(this, location); if (redirect_limit_ <= 0) { DVLOG(1) << "disallowing redirect: exceeds limit"; @@ -795,8 +838,8 @@ void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) { NetworkDelegate::AuthRequiredResponse rv = NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; auth_info_ = auth_info; - if (context_->network_delegate()) { - rv = context_->network_delegate()->NotifyAuthRequired( + if (network_delegate_) { + rv = network_delegate_->NotifyAuthRequired( this, *auth_info, base::Bind(&URLRequest::NotifyAuthRequiredComplete, @@ -862,9 +905,8 @@ void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info, bool URLRequest::CanGetCookies(const CookieList& cookie_list) const { DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES)); - if (context_->network_delegate()) { - return context_->network_delegate()->CanGetCookies(*this, - cookie_list); + if (network_delegate_) { + return network_delegate_->CanGetCookies(*this, cookie_list); } return g_default_can_use_cookies; } @@ -872,10 +914,8 @@ bool URLRequest::CanGetCookies(const CookieList& cookie_list) const { bool URLRequest::CanSetCookie(const std::string& cookie_line, CookieOptions* options) const { DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES)); - if (context_->network_delegate()) { - return context_->network_delegate()->CanSetCookie(*this, - cookie_line, - options); + if (network_delegate_) { + return network_delegate_->CanSetCookie(*this, cookie_line, options); } return g_default_can_use_cookies; } @@ -906,8 +946,8 @@ void URLRequest::NotifyRequestCompleted() { is_pending_ = false; has_notified_completion_ = true; - if (context_->network_delegate()) - context_->network_delegate()->NotifyCompleted(this, job_ != NULL); + if (network_delegate_) + network_delegate_->NotifyCompleted(this, job_ != NULL); } void URLRequest::SetBlockedOnDelegate() { diff --git a/net/url_request/url_request.h b/net/url_request/url_request.h index 091f23f2..4a2a2e4 100644 --- a/net/url_request/url_request.h +++ b/net/url_request/url_request.h @@ -312,11 +312,17 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe), virtual ~Delegate() {} }; + // TODO(shalev): Get rid of this constructor in favour of the one below it. // Initialize an URL request. URLRequest(const GURL& url, Delegate* delegate, const URLRequestContext* context); + URLRequest(const GURL& url, + Delegate* delegate, + const URLRequestContext* context, + NetworkDelegate* network_delegate); + // If destroyed after Start() has been called but while IO is pending, // then the request will be effectively canceled and the delegate // will not have any more of its methods called. @@ -734,11 +740,13 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe), void SetBlockedOnDelegate(); void SetUnblockedOnDelegate(); - // Contextual information used for this request (can be NULL). This contains + // Contextual information used for this request. Cannot be NULL. This contains // most of the dependencies which are shared between requests (disk cache, // cookie store, socket pool, etc.) const URLRequestContext* context_; + NetworkDelegate* network_delegate_; + // Tracks the time spent in various load states throughout this request. BoundNetLog net_log_; diff --git a/net/url_request/url_request_context.cc b/net/url_request/url_request_context.cc index 40dad2b..e0df04e 100644 --- a/net/url_request/url_request_context.cc +++ b/net/url_request/url_request_context.cc @@ -63,6 +63,11 @@ void URLRequestContext::CopyFrom(const URLRequestContext* other) { set_throttler_manager(other->throttler_manager_); } +URLRequest* URLRequestContext::CreateRequest( + const GURL& url, URLRequest::Delegate* delegate) const { + return new URLRequest(url, delegate, this, network_delegate_); +} + void URLRequestContext::set_cookie_store(CookieStore* cookie_store) { cookie_store_ = cookie_store; } diff --git a/net/url_request/url_request_context.h b/net/url_request/url_request_context.h index be8214b..3863540 100644 --- a/net/url_request/url_request_context.h +++ b/net/url_request/url_request_context.h @@ -22,6 +22,7 @@ #include "net/base/transport_security_state.h" #include "net/http/http_server_properties.h" #include "net/ftp/ftp_auth_cache.h" +#include "net/url_request/url_request.h" namespace net { class CertVerifier; @@ -51,6 +52,9 @@ class NET_EXPORT URLRequestContext // Copies the state from |other| into this context. void CopyFrom(const URLRequestContext* other); + URLRequest* CreateRequest( + const GURL& url, URLRequest::Delegate* delegate) const; + NetLog* net_log() const { return net_log_; } |