diff options
author | tburkard@chromium.org <tburkard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-12 17:55:16 +0000 |
---|---|---|
committer | tburkard@chromium.org <tburkard@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-12 17:55:16 +0000 |
commit | a96f4c5795e7cf0d2778a3d77316614de6fcba69 (patch) | |
tree | 88e5448c6a50f9f83b478751682d3f9930f47aa2 /net | |
parent | 8a76c8f5fc87008d0b14877076b83d29b35b1846 (diff) | |
download | chromium_src-a96f4c5795e7cf0d2778a3d77316614de6fcba69.zip chromium_src-a96f4c5795e7cf0d2778a3d77316614de6fcba69.tar.gz chromium_src-a96f4c5795e7cf0d2778a3d77316614de6fcba69.tar.bz2 |
Allow the content browser client to specify a special cookie store to be
used for a given render process id. This special cookie store will then
be used for renderer messages pertaining to cookies, url fetches in net,
and websockets. If the special cookie store is NULL, a default cookie store
will be used.
R=erikwright@chromium.org, jam@chromium.org, lambroslambrou@chromium.org, mmenke@chromium.org, tyoshino@chromium.org
Review URL: https://codereview.chromium.org/188693003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@256579 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/proxy/proxy_script_fetcher_impl.cc | 2 | ||||
-rw-r--r-- | net/socket_stream/socket_stream.cc | 56 | ||||
-rw-r--r-- | net/socket_stream/socket_stream.h | 15 | ||||
-rw-r--r-- | net/socket_stream/socket_stream_job.cc | 12 | ||||
-rw-r--r-- | net/socket_stream/socket_stream_job.h | 16 | ||||
-rw-r--r-- | net/socket_stream/socket_stream_job_manager.cc | 10 | ||||
-rw-r--r-- | net/socket_stream/socket_stream_job_manager.h | 3 | ||||
-rw-r--r-- | net/socket_stream/socket_stream_unittest.cc | 81 | ||||
-rw-r--r-- | net/url_request/url_fetcher_core.cc | 2 | ||||
-rw-r--r-- | net/url_request/url_request.cc | 82 | ||||
-rw-r--r-- | net/url_request/url_request.h | 24 | ||||
-rw-r--r-- | net/url_request/url_request_context.cc | 6 | ||||
-rw-r--r-- | net/url_request/url_request_context.h | 6 | ||||
-rw-r--r-- | net/url_request/url_request_http_job.cc | 9 | ||||
-rw-r--r-- | net/url_request/url_request_job.cc | 6 | ||||
-rw-r--r-- | net/url_request/url_request_job.h | 4 | ||||
-rw-r--r-- | net/websockets/websocket_job.cc | 13 | ||||
-rw-r--r-- | net/websockets/websocket_job_test.cc | 15 | ||||
-rw-r--r-- | net/websockets/websocket_throttle_test.cc | 27 |
19 files changed, 234 insertions, 155 deletions
diff --git a/net/proxy/proxy_script_fetcher_impl.cc b/net/proxy/proxy_script_fetcher_impl.cc index 705bbbf..0887d8c 100644 --- a/net/proxy/proxy_script_fetcher_impl.cc +++ b/net/proxy/proxy_script_fetcher_impl.cc @@ -135,7 +135,7 @@ int ProxyScriptFetcherImpl::Fetch( } cur_request_ = - url_request_context_->CreateRequest(url, DEFAULT_PRIORITY, this); + url_request_context_->CreateRequest(url, DEFAULT_PRIORITY, this, NULL); cur_request_->set_method("GET"); // Make sure that the PAC script is downloaded using a direct connection, diff --git a/net/socket_stream/socket_stream.cc b/net/socket_stream/socket_stream.cc index 1682ddc..5aacd4a 100644 --- a/net/socket_stream/socket_stream.cc +++ b/net/socket_stream/socket_stream.cc @@ -87,11 +87,13 @@ void SocketStream::ResponseHeaders::Realloc(size_t new_size) { SocketStream::ResponseHeaders::~ResponseHeaders() { data_ = NULL; } -SocketStream::SocketStream(const GURL& url, Delegate* delegate) +SocketStream::SocketStream(const GURL& url, Delegate* delegate, + URLRequestContext* context, + CookieStore* cookie_store) : delegate_(delegate), url_(url), max_pending_send_allowed_(kMaxPendingSendAllowed), - context_(NULL), + context_(context), next_state_(STATE_NONE), factory_(ClientSocketFactory::GetDefaultFactory()), proxy_mode_(kDirectConnection), @@ -108,12 +110,24 @@ SocketStream::SocketStream(const GURL& url, Delegate* delegate) waiting_for_write_completion_(false), closing_(false), server_closed_(false), - metrics_(new SocketStreamMetrics(url)) { + metrics_(new SocketStreamMetrics(url)), + cookie_store_(cookie_store) { DCHECK(base::MessageLoop::current()) << "The current base::MessageLoop must exist"; DCHECK(base::MessageLoopForIO::IsCurrent()) << "The current base::MessageLoop must be TYPE_IO"; DCHECK(delegate_); + + if (context_) { + if (!cookie_store_) + cookie_store_ = context_->cookie_store(); + + net_log_ = BoundNetLog::Make( + context->net_log(), + NetLog::SOURCE_SOCKET_STREAM); + + net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); + } } SocketStream::UserData* SocketStream::GetUserData( @@ -132,28 +146,20 @@ bool SocketStream::is_secure() const { return url_.SchemeIs("wss"); } -void SocketStream::set_context(URLRequestContext* context) { - const URLRequestContext* prev_context = context_; - - context_ = context; - - if (prev_context != context) { - if (prev_context && pac_request_) { - prev_context->proxy_service()->CancelPacRequest(pac_request_); - pac_request_ = NULL; - } +void SocketStream::DetachContext() { + if (!context_) + return; - net_log_.EndEvent(NetLog::TYPE_REQUEST_ALIVE); - net_log_ = BoundNetLog(); + if (pac_request_) { + context_->proxy_service()->CancelPacRequest(pac_request_); + pac_request_ = NULL; + } - if (context) { - net_log_ = BoundNetLog::Make( - context->net_log(), - NetLog::SOURCE_SOCKET_STREAM); + net_log_.EndEvent(NetLog::TYPE_REQUEST_ALIVE); + net_log_ = BoundNetLog(); - net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); - } - } + context_ = NULL; + cookie_store_ = NULL; } void SocketStream::CheckPrivacyMode() { @@ -318,7 +324,7 @@ void SocketStream::ContinueDespiteError() { } SocketStream::~SocketStream() { - set_context(NULL); + DetachContext(); DCHECK(!delegate_); DCHECK(!pac_request_); } @@ -1342,4 +1348,8 @@ int SocketStream::HandleCertificateError(int result) { return ERR_IO_PENDING; } +CookieStore* SocketStream::cookie_store() const { + return cookie_store_; +} + } // namespace net diff --git a/net/socket_stream/socket_stream.h b/net/socket_stream/socket_stream.h index b86f20a..cfc764d 100644 --- a/net/socket_stream/socket_stream.h +++ b/net/socket_stream/socket_stream.h @@ -19,6 +19,7 @@ #include "net/base/net_export.h" #include "net/base/net_log.h" #include "net/base/privacy_mode.h" +#include "net/cookies/cookie_store.h" #include "net/proxy/proxy_service.h" #include "net/ssl/ssl_config_service.h" #include "net/url_request/url_request.h" @@ -114,7 +115,8 @@ class NET_EXPORT SocketStream virtual ~Delegate() {} }; - SocketStream(const GURL& url, Delegate* delegate); + SocketStream(const GURL& url, Delegate* delegate, URLRequestContext* context, + CookieStore* cookie_store); // The user data allows the clients to associate data with this job. // Multiple user data values can be stored under different keys. @@ -130,9 +132,6 @@ class NET_EXPORT SocketStream int max_pending_send_allowed() const { return max_pending_send_allowed_; } URLRequestContext* context() { return context_; } - // There're some asynchronous operations and members that are constructed from - // |context|. Be careful when you use this for the second time or more. - void set_context(URLRequestContext* context); const SSLConfig& server_ssl_config() const { return server_ssl_config_; } PrivacyMode privacy_mode() const { return privacy_mode_; } @@ -162,6 +161,9 @@ class NET_EXPORT SocketStream // back. virtual void DetachDelegate(); + // Detach the context. + virtual void DetachContext(); + const ProxyServer& proxy_server() const; // Sets an alternative ClientSocketFactory. Doesn't take ownership of @@ -180,6 +182,8 @@ class NET_EXPORT SocketStream // actions on alert dialog or browser cached such kinds of user actions. void ContinueDespiteError(); + CookieStore* cookie_store() const; + protected: friend class base::RefCountedThreadSafe<SocketStream>; virtual ~SocketStream(); @@ -389,6 +393,9 @@ class NET_EXPORT SocketStream scoped_ptr<SocketStreamMetrics> metrics_; + // Cookie store to use for this socket stream. + scoped_refptr<CookieStore> cookie_store_; + DISALLOW_COPY_AND_ASSIGN(SocketStream); }; diff --git a/net/socket_stream/socket_stream_job.cc b/net/socket_stream/socket_stream_job.cc index 9c13a8f..f2de823 100644 --- a/net/socket_stream/socket_stream_job.cc +++ b/net/socket_stream/socket_stream_job.cc @@ -24,7 +24,9 @@ SocketStreamJob* SocketStreamJob::CreateSocketStreamJob( const GURL& url, SocketStream::Delegate* delegate, TransportSecurityState* sts, - SSLConfigService* ssl) { + SSLConfigService* ssl, + URLRequestContext* context, + CookieStore* cookie_store) { GURL socket_url(url); TransportSecurityState::DomainState domain_state; if (url.scheme() == "ws" && sts && sts->GetDomainState( @@ -36,7 +38,8 @@ SocketStreamJob* SocketStreamJob::CreateSocketStreamJob( url_parse::Component(0, strlen(kNewScheme))); socket_url = url.ReplaceComponents(replacements); } - return SocketStreamJobManager::GetInstance()->CreateJob(socket_url, delegate); + return SocketStreamJobManager::GetInstance()->CreateJob( + socket_url, delegate, context, cookie_store); } SocketStreamJob::SocketStreamJob() {} @@ -82,6 +85,11 @@ void SocketStreamJob::DetachDelegate() { socket_->DetachDelegate(); } +void SocketStreamJob::DetachContext() { + if (socket_.get()) + socket_->DetachContext(); +} + SocketStreamJob::~SocketStreamJob() {} } // namespace net diff --git a/net/socket_stream/socket_stream_job.h b/net/socket_stream/socket_stream_job.h index d12e73a..9fc27d9 100644 --- a/net/socket_stream/socket_stream_job.h +++ b/net/socket_stream/socket_stream_job.h @@ -15,6 +15,7 @@ class GURL; namespace net { +class CookieStore; class SSLConfigService; class SSLInfo; class TransportSecurityState; @@ -30,7 +31,9 @@ class NET_EXPORT SocketStreamJob public: // Callback function implemented by protocol handlers to create new jobs. typedef SocketStreamJob* (ProtocolFactory)(const GURL& url, - SocketStream::Delegate* delegate); + SocketStream::Delegate* delegate, + URLRequestContext* context, + CookieStore* cookie_store); static ProtocolFactory* RegisterProtocolFactory(const std::string& scheme, ProtocolFactory* factory); @@ -39,7 +42,9 @@ class NET_EXPORT SocketStreamJob const GURL& url, SocketStream::Delegate* delegate, TransportSecurityState* sts, - SSLConfigService* ssl); + SSLConfigService* ssl, + URLRequestContext* context, + CookieStore* cookie_store); SocketStreamJob(); void InitSocketStream(SocketStream* socket) { @@ -52,9 +57,8 @@ class NET_EXPORT SocketStreamJob URLRequestContext* context() const { return socket_.get() ? socket_->context() : 0; } - void set_context(URLRequestContext* context) { - if (socket_.get()) - socket_->set_context(context); + CookieStore* cookie_store() const { + return socket_.get() ? socket_->cookie_store() : 0; } virtual void Connect(); @@ -73,6 +77,8 @@ class NET_EXPORT SocketStreamJob virtual void DetachDelegate(); + virtual void DetachContext(); + protected: friend class WebSocketJobTest; friend class base::RefCountedThreadSafe<SocketStreamJob>; diff --git a/net/socket_stream/socket_stream_job_manager.cc b/net/socket_stream/socket_stream_job_manager.cc index 7f66a4a..6418be4 100644 --- a/net/socket_stream/socket_stream_job_manager.cc +++ b/net/socket_stream/socket_stream_job_manager.cc @@ -20,12 +20,14 @@ SocketStreamJobManager* SocketStreamJobManager::GetInstance() { } SocketStreamJob* SocketStreamJobManager::CreateJob( - const GURL& url, SocketStream::Delegate* delegate) const { + const GURL& url, SocketStream::Delegate* delegate, + URLRequestContext* context, CookieStore* cookie_store) const { // If url is invalid, create plain SocketStreamJob, which will close // the socket immediately. if (!url.is_valid()) { SocketStreamJob* job = new SocketStreamJob(); - job->InitSocketStream(new SocketStream(url, delegate)); + job->InitSocketStream(new SocketStream(url, delegate, context, + cookie_store)); return job; } @@ -34,12 +36,12 @@ SocketStreamJob* SocketStreamJobManager::CreateJob( base::AutoLock locked(lock_); FactoryMap::const_iterator found = factories_.find(scheme); if (found != factories_.end()) { - SocketStreamJob* job = found->second(url, delegate); + SocketStreamJob* job = found->second(url, delegate, context, cookie_store); if (job) return job; } SocketStreamJob* job = new SocketStreamJob(); - job->InitSocketStream(new SocketStream(url, delegate)); + job->InitSocketStream(new SocketStream(url, delegate, context, cookie_store)); return job; } diff --git a/net/socket_stream/socket_stream_job_manager.h b/net/socket_stream/socket_stream_job_manager.h index aa0cd40..2363fb5 100644 --- a/net/socket_stream/socket_stream_job_manager.h +++ b/net/socket_stream/socket_stream_job_manager.h @@ -22,7 +22,8 @@ class SocketStreamJobManager { static SocketStreamJobManager* GetInstance(); SocketStreamJob* CreateJob( - const GURL& url, SocketStream::Delegate* delegate) const; + const GURL& url, SocketStream::Delegate* delegate, + URLRequestContext* context, CookieStore* cookie_store) const; SocketStreamJob::ProtocolFactory* RegisterProtocolFactory( const std::string& scheme, SocketStreamJob::ProtocolFactory* factory); diff --git a/net/socket_stream/socket_stream_unittest.cc b/net/socket_stream/socket_stream_unittest.cc index 4a6a9e2..069f92e 100644 --- a/net/socket_stream/socket_stream_unittest.cc +++ b/net/socket_stream/socket_stream_unittest.cc @@ -318,7 +318,7 @@ class SocketStreamTest : public PlatformTest { virtual void DoCloseFlushPendingWriteTestWithSetContextNull( SocketStreamEvent* event) { - event->socket->set_context(NULL); + event->socket->DetachContext(); // handshake response received. for (size_t i = 0; i < messages_.size(); i++) { std::vector<char> frame; @@ -400,9 +400,8 @@ TEST_F(SocketStreamTest, CloseFlushPendingWrite) { TestURLRequestContext context; scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); - - socket_stream->set_context(&context); + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); MockWrite data_writes[] = { MockWrite(SocketStreamTest::kWebSocketHandshakeRequest), @@ -453,16 +452,16 @@ TEST_F(SocketStreamTest, ResolveFailure) { scoped_ptr<SocketStreamEventRecorder> delegate( new SocketStreamEventRecorder(test_callback.callback())); - scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); - // Make resolver fail. TestURLRequestContext context; scoped_ptr<MockHostResolver> mock_host_resolver( new MockHostResolver()); mock_host_resolver->rules()->AddSimulatedFailure("example.com"); context.set_host_resolver(mock_host_resolver.get()); - socket_stream->set_context(&context); + + scoped_refptr<SocketStream> socket_stream( + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); // No read/write on socket is expected. StaticSocketDataProvider data_provider(NULL, 0, NULL, 0); @@ -493,9 +492,8 @@ TEST_F(SocketStreamTest, ExceedMaxPendingSendAllowed) { TestURLRequestContext context; scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); - - socket_stream->set_context(&context); + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); DelayedSocketData data_provider(1, NULL, 0, NULL, 0); @@ -566,12 +564,12 @@ TEST_F(SocketStreamTest, BasicAuthProxy) { &SocketStreamEventRecorder::DoRestartWithAuth, base::Unretained(delegate.get()))); - scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); - TestURLRequestContextWithProxy context("myproxy:70"); - socket_stream->set_context(&context); + scoped_refptr<SocketStream> socket_stream( + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); + socket_stream->SetClientSocketFactory(&mock_socket_factory); socket_stream->Connect(); @@ -618,9 +616,6 @@ TEST_F(SocketStreamTest, BasicAuthProxyWithAuthCache) { delegate->SetOnConnected(base::Bind(&SocketStreamEventRecorder::DoClose, base::Unretained(delegate.get()))); - scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); - TestURLRequestContextWithProxy context("myproxy:70"); HttpAuthCache* auth_cache = context.http_transaction_factory()->GetSession()->http_auth_cache(); @@ -632,7 +627,10 @@ TEST_F(SocketStreamTest, BasicAuthProxyWithAuthCache) { ASCIIToUTF16("bar")), "/"); - socket_stream->set_context(&context); + scoped_refptr<SocketStream> socket_stream( + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); + socket_stream->SetClientSocketFactory(&mock_socket_factory); socket_stream->Connect(); @@ -675,9 +673,6 @@ TEST_F(SocketStreamTest, WSSBasicAuthProxyWithAuthCache) { delegate->SetOnConnected(base::Bind(&SocketStreamEventRecorder::DoClose, base::Unretained(delegate.get()))); - scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("wss://example.com/demo"), delegate.get())); - TestURLRequestContextWithProxy context("myproxy:70"); HttpAuthCache* auth_cache = context.http_transaction_factory()->GetSession()->http_auth_cache(); @@ -689,7 +684,10 @@ TEST_F(SocketStreamTest, WSSBasicAuthProxyWithAuthCache) { ASCIIToUTF16("bar")), "/"); - socket_stream->set_context(&context); + scoped_refptr<SocketStream> socket_stream( + new SocketStream(GURL("wss://example.com/demo"), delegate.get(), + &context, NULL)); + socket_stream->SetClientSocketFactory(&mock_socket_factory); socket_stream->Connect(); @@ -721,9 +719,8 @@ TEST_F(SocketStreamTest, IOPending) { TestURLRequestContext context; scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); - - socket_stream->set_context(&context); + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); MockWrite data_writes[] = { MockWrite(SocketStreamTest::kWebSocketHandshakeRequest), @@ -783,9 +780,8 @@ TEST_F(SocketStreamTest, SwitchToSpdy) { TestURLRequestContext context; scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); - - socket_stream->set_context(&context); + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); socket_stream->Connect(); @@ -811,9 +807,8 @@ TEST_F(SocketStreamTest, SwitchAfterPending) { TestURLRequestContext context; scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); - - socket_stream->set_context(&context); + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); socket_stream->Connect(); io_test_callback_.WaitForResult(); @@ -865,9 +860,9 @@ TEST_F(SocketStreamTest, SecureProxyConnectError) { base::Unretained(delegate.get()))); scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); - socket_stream->set_context(&context); socket_stream->SetClientSocketFactory(&mock_socket_factory); socket_stream->Connect(); @@ -916,9 +911,9 @@ TEST_F(SocketStreamTest, SecureProxyConnect) { base::Unretained(delegate.get()))); scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); - socket_stream->set_context(&context); socket_stream->SetClientSocketFactory(&mock_socket_factory); socket_stream->Connect(); @@ -948,9 +943,8 @@ TEST_F(SocketStreamTest, BeforeConnectFailed) { context.set_network_delegate(&network_delegate); scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); - - socket_stream->set_context(&context); + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); socket_stream->Connect(); @@ -981,8 +975,8 @@ TEST_F(SocketStreamTest, OnErrorDetachDelegate) { TestURLRequestContext context; scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://localhost:9998/echo"), delegate)); - socket_stream->set_context(&context); + new SocketStream(GURL("ws://localhost:9998/echo"), delegate, + &context, NULL)); socket_stream->SetClientSocketFactory(&mock_socket_factory); delegate->set_socket_stream(socket_stream); // The delegate pointer will become invalid during the test. Set it to NULL to @@ -1001,7 +995,8 @@ TEST_F(SocketStreamTest, NullContextSocketStreamShouldNotCrash) { new SocketStreamEventRecorder(test_callback.callback())); TestURLRequestContext context; scoped_refptr<SocketStream> socket_stream( - new SocketStream(GURL("ws://example.com/demo"), delegate.get())); + new SocketStream(GURL("ws://example.com/demo"), delegate.get(), + &context, NULL)); delegate->SetOnStartOpenConnection(base::Bind( &SocketStreamTest::DoIOPending, base::Unretained(this))); delegate->SetOnConnected(base::Bind( @@ -1010,8 +1005,6 @@ TEST_F(SocketStreamTest, NullContextSocketStreamShouldNotCrash) { &SocketStreamTest::DoCloseFlushPendingWriteTestWithSetContextNull, base::Unretained(this))); - socket_stream->set_context(&context); - MockWrite data_writes[] = { MockWrite(SocketStreamTest::kWebSocketHandshakeRequest), }; diff --git a/net/url_request/url_fetcher_core.cc b/net/url_request/url_fetcher_core.cc index eea2e81..eb45ecf 100644 --- a/net/url_request/url_fetcher_core.cc +++ b/net/url_request/url_fetcher_core.cc @@ -507,7 +507,7 @@ void URLFetcherCore::StartURLRequest() { g_registry.Get().AddURLFetcherCore(this); current_response_bytes_ = 0; request_ = request_context_getter_->GetURLRequestContext()->CreateRequest( - original_url_, DEFAULT_PRIORITY, this); + original_url_, DEFAULT_PRIORITY, this, NULL); 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 531e8e8..e2be4c2 100644 --- a/net/url_request/url_request.cc +++ b/net/url_request/url_request.cc @@ -209,38 +209,17 @@ URLRequest::URLRequest(const GURL& url, RequestPriority priority, 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), - is_redirecting_(false), - redirect_limit_(kMaxRedirects), - priority_(priority), - identifier_(GenerateURLRequestIdentifier()), - calling_delegate_(false), - use_blocked_by_as_load_param_(false), - before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete, - base::Unretained(this))), - has_notified_completion_(false), - received_response_content_length_(0), - creation_time_(base::TimeTicks::Now()), - notified_before_network_start_(false) { - SIMPLE_STATS_COUNTER("URLRequestCount"); - - // Sanity check out environment. - DCHECK(base::MessageLoop::current()) - << "The current base::MessageLoop must exist"; - - CHECK(context); - context->url_requests()->insert(this); + : identifier_(GenerateURLRequestIdentifier()) { + Init(url, priority, delegate, context, NULL); +} - net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); +URLRequest::URLRequest(const GURL& url, + RequestPriority priority, + Delegate* delegate, + const URLRequestContext* context, + CookieStore* cookie_store) + : identifier_(GenerateURLRequestIdentifier()) { + Init(url, priority, delegate, context, cookie_store); } URLRequest::~URLRequest() { @@ -284,6 +263,47 @@ void URLRequest::UnregisterRequestInterceptor(Interceptor* interceptor) { interceptor); } +void URLRequest::Init(const GURL& url, + RequestPriority priority, + Delegate* delegate, + const URLRequestContext* context, + CookieStore* cookie_store) { + context_ = context; + network_delegate_ = context->network_delegate(); + net_log_ = BoundNetLog::Make(context->net_log(), NetLog::SOURCE_URL_REQUEST); + url_chain_.push_back(url); + method_ = "GET"; + referrer_policy_ = CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE; + load_flags_ = LOAD_NORMAL; + delegate_ = delegate; + is_pending_ = false; + is_redirecting_ = false; + redirect_limit_ = kMaxRedirects; + priority_ = priority; + calling_delegate_ = false; + use_blocked_by_as_load_param_ =false; + before_request_callback_ = base::Bind(&URLRequest::BeforeRequestComplete, + base::Unretained(this)); + has_notified_completion_ = false; + received_response_content_length_ = 0; + creation_time_ = base::TimeTicks::Now(); + notified_before_network_start_ = false; + + SIMPLE_STATS_COUNTER("URLRequestCount"); + + // Sanity check out environment. + DCHECK(base::MessageLoop::current()) + << "The current base::MessageLoop must exist"; + + CHECK(context); + context->url_requests()->insert(this); + cookie_store_ = cookie_store; + if (cookie_store_ == NULL) + cookie_store_ = context->cookie_store(); + + net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); +} + void URLRequest::EnableChunkedUpload() { DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked()); if (!upload_data_stream_) { diff --git a/net/url_request/url_request.h b/net/url_request/url_request.h index 7c8e1a9..f4e8c40 100644 --- a/net/url_request/url_request.h +++ b/net/url_request/url_request.h @@ -25,6 +25,7 @@ #include "net/base/request_priority.h" #include "net/base/upload_progress.h" #include "net/cookies/canonical_cookie.h" +#include "net/cookies/cookie_store.h" #include "net/http/http_request_headers.h" #include "net/http/http_response_info.h" #include "net/url_request/url_request_status.h" @@ -291,11 +292,20 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe), virtual ~Delegate() {} }; + // TODO(tburkard): we should get rid of this constructor, and have each + // creator of a URLRequest specifically list the cookie store to be used. + // For now, this constructor will use the cookie store in |context|. URLRequest(const GURL& url, RequestPriority priority, Delegate* delegate, const URLRequestContext* context); + URLRequest(const GURL& url, + RequestPriority priority, + Delegate* delegate, + const URLRequestContext* context, + CookieStore* cookie_store); + // 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. @@ -691,6 +701,8 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe), // Allow the URLRequestJob class to set our status too void set_status(const URLRequestStatus& value) { status_ = value; } + CookieStore* cookie_store() const { return cookie_store_; } + // Allow the URLRequestJob to redirect this request. Returns OK if // successful, otherwise an error code is returned. int Redirect(const GURL& location, int http_status_code); @@ -730,6 +742,15 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe), static void RegisterRequestInterceptor(Interceptor* interceptor); static void UnregisterRequestInterceptor(Interceptor* interceptor); + // Initializes the URLRequest. Code shared between the two constructors. + // TODO(tburkard): This can ultimately be folded into a single constructor + // again. + void Init(const GURL& url, + RequestPriority priotity, + Delegate* delegate, + const URLRequestContext* context, + CookieStore* cookie_store); + // Resumes or blocks a request paused by the NetworkDelegate::OnBeforeRequest // handler. If |blocked| is true, the request is blocked and an error page is // returned indicating so. This should only be called after Start is called @@ -895,6 +916,9 @@ class NET_EXPORT URLRequest : NON_EXPORTED_BASE(public base::NonThreadSafe), // Keeps track of whether or not OnBeforeNetworkStart has been called yet. bool notified_before_network_start_; + // The cookie store to be used for this request. + scoped_refptr<CookieStore> cookie_store_; + DISALLOW_COPY_AND_ASSIGN(URLRequest); }; diff --git a/net/url_request/url_request_context.cc b/net/url_request/url_request_context.cc index 3542b16..d6a4d2f 100644 --- a/net/url_request/url_request_context.cc +++ b/net/url_request/url_request_context.cc @@ -73,8 +73,10 @@ const HttpNetworkSession::Params* URLRequestContext::GetNetworkSessionParams( scoped_ptr<URLRequest> URLRequestContext::CreateRequest( const GURL& url, RequestPriority priority, - URLRequest::Delegate* delegate) const { - return scoped_ptr<URLRequest>(new URLRequest(url, priority, delegate, this)); + URLRequest::Delegate* delegate, + CookieStore* cookie_store) const { + return scoped_ptr<URLRequest>( + new URLRequest(url, priority, delegate, this, cookie_store)); } void URLRequestContext::set_cookie_store(CookieStore* cookie_store) { diff --git a/net/url_request/url_request_context.h b/net/url_request/url_request_context.h index 5bb5b51..a05c9ae 100644 --- a/net/url_request/url_request_context.h +++ b/net/url_request/url_request_context.h @@ -58,9 +58,13 @@ class NET_EXPORT URLRequestContext // May return NULL if this context doesn't have an associated network session. const HttpNetworkSession::Params* GetNetworkSessionParams() const; + // Creates a URLRequest. |cookie_store| optionally specifies a cookie store + // to be used rather than the one represented by the context, or NULL + // otherwise. scoped_ptr<URLRequest> CreateRequest(const GURL& url, RequestPriority priority, - URLRequest::Delegate* delegate) const; + URLRequest::Delegate* delegate, + CookieStore* cookie_store) const; NetLog* net_log() const { return net_log_; diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc index 82ef633..044e5f0 100644 --- a/net/url_request/url_request_http_job.cc +++ b/net/url_request/url_request_http_job.cc @@ -539,7 +539,7 @@ void URLRequestHttpJob::AddCookieHeaderAndStart() { if (!request_) return; - CookieStore* cookie_store = request_->context()->cookie_store(); + CookieStore* cookie_store = GetCookieStore(); if (cookie_store && !(request_info_.load_flags & LOAD_DO_NOT_SEND_COOKIES)) { net::CookieMonster* cookie_monster = cookie_store->GetCookieMonster(); if (cookie_monster) { @@ -558,7 +558,7 @@ void URLRequestHttpJob::AddCookieHeaderAndStart() { void URLRequestHttpJob::DoLoadCookies() { CookieOptions options; options.set_include_httponly(); - request_->context()->cookie_store()->GetCookiesWithOptionsAsync( + GetCookieStore()->GetCookiesWithOptionsAsync( request_->url(), options, base::Bind(&URLRequestHttpJob::OnCookiesLoaded, weak_factory_.GetWeakPtr())); @@ -638,8 +638,7 @@ void URLRequestHttpJob::SaveNextCookie() { new SharedBoolean(true); if (!(request_info_.load_flags & LOAD_DO_NOT_SAVE_COOKIES) && - request_->context()->cookie_store() && - response_cookies_.size() > 0) { + GetCookieStore() && response_cookies_.size() > 0) { CookieOptions options; options.set_include_httponly(); options.set_server_time(response_date_); @@ -657,7 +656,7 @@ void URLRequestHttpJob::SaveNextCookie() { if (CanSetCookie( response_cookies_[response_cookies_save_index_], &options)) { callback_pending->data = true; - request_->context()->cookie_store()->SetCookieWithOptionsAsync( + GetCookieStore()->SetCookieWithOptionsAsync( request_->url(), response_cookies_[response_cookies_save_index_], options, callback); } diff --git a/net/url_request/url_request_job.cc b/net/url_request/url_request_job.cc index 78f704a..c4b34c2 100644 --- a/net/url_request/url_request_job.cc +++ b/net/url_request/url_request_job.cc @@ -286,6 +286,12 @@ bool URLRequestJob::CanEnablePrivacyMode() const { return request_->CanEnablePrivacyMode(); } +CookieStore* URLRequestJob::GetCookieStore() const { + DCHECK(request_); + + return request_->cookie_store(); +} + void URLRequestJob::NotifyBeforeNetworkStart(bool* defer) { if (!request_) return; diff --git a/net/url_request/url_request_job.h b/net/url_request/url_request_job.h index 3194ef6..9bb763e 100644 --- a/net/url_request/url_request_job.h +++ b/net/url_request/url_request_job.h @@ -26,6 +26,7 @@ namespace net { class AuthChallengeInfo; class AuthCredentials; class CookieOptions; +class CookieStore; class Filter; class HttpRequestHeaders; class HttpResponseInfo; @@ -238,6 +239,9 @@ class NET_EXPORT URLRequestJob // Delegates to URLRequest::Delegate. bool CanEnablePrivacyMode() const; + // Returns the cookie store to be used for the request. + CookieStore* GetCookieStore() const; + // Notifies the job that the network is about to be used. void NotifyBeforeNetworkStart(bool* defer); diff --git a/net/websockets/websocket_job.cc b/net/websockets/websocket_job.cc index b0f5be8..63e65a4 100644 --- a/net/websockets/websocket_job.cc +++ b/net/websockets/websocket_job.cc @@ -36,9 +36,10 @@ const char* const kSetCookieHeaders[] = { }; net::SocketStreamJob* WebSocketJobFactory( - const GURL& url, net::SocketStream::Delegate* delegate) { + const GURL& url, net::SocketStream::Delegate* delegate, + net::URLRequestContext* context, net::CookieStore* cookie_store) { net::WebSocketJob* job = new net::WebSocketJob(delegate); - job->InitSocketStream(new net::SocketStream(url, job)); + job->InitSocketStream(new net::SocketStream(url, job, context, cookie_store)); return job; } @@ -370,11 +371,11 @@ void WebSocketJob::AddCookieHeaderAndSend() { if (socket_.get() && delegate_ && state_ == CONNECTING) { handshake_request_->RemoveHeaders(kCookieHeaders, arraysize(kCookieHeaders)); - if (allow && socket_->context()->cookie_store()) { + if (allow && socket_->cookie_store()) { // Add cookies, including HttpOnly cookies. CookieOptions cookie_options; cookie_options.set_include_httponly(); - socket_->context()->cookie_store()->GetCookiesWithOptionsAsync( + socket_->cookie_store()->GetCookiesWithOptionsAsync( GetURLForCookies(), cookie_options, base::Bind(&WebSocketJob::LoadCookieCallback, weak_ptr_factory_.GetWeakPtr())); @@ -505,7 +506,7 @@ void WebSocketJob::SaveNextCookie() { callback_pending_ = false; save_next_cookie_running_ = true; - if (socket_->context()->cookie_store()) { + if (socket_->cookie_store()) { GURL url_for_cookies = GetURLForCookies(); CookieOptions options; @@ -526,7 +527,7 @@ void WebSocketJob::SaveNextCookie() { continue; callback_pending_ = true; - socket_->context()->cookie_store()->SetCookieWithOptionsAsync( + socket_->cookie_store()->SetCookieWithOptionsAsync( url_for_cookies, cookie, options, base::Bind(&WebSocketJob::OnCookieSaved, weak_ptr_factory_.GetWeakPtr())); diff --git a/net/websockets/websocket_job_test.cc b/net/websockets/websocket_job_test.cc index 31bd9d6..3d7d424 100644 --- a/net/websockets/websocket_job_test.cc +++ b/net/websockets/websocket_job_test.cc @@ -41,8 +41,9 @@ namespace { class MockSocketStream : public SocketStream { public: - MockSocketStream(const GURL& url, SocketStream::Delegate* delegate) - : SocketStream(url, delegate) {} + MockSocketStream(const GURL& url, SocketStream::Delegate* delegate, + URLRequestContext* context, CookieStore* cookie_store) + : SocketStream(url, delegate, context, cookie_store) {} virtual void Connect() OVERRIDE {} virtual bool SendData(const char* data, int len) OVERRIDE { @@ -364,7 +365,8 @@ class WebSocketJobTest : public PlatformTest, websocket_ = new WebSocketJob(delegate); if (stream_type == STREAM_MOCK_SOCKET) - socket_ = new MockSocketStream(url, websocket_.get()); + socket_ = new MockSocketStream(url, websocket_.get(), context_.get(), + NULL); if (stream_type == STREAM_SOCKET || stream_type == STREAM_SPDY_WEBSOCKET) { if (stream_type == STREAM_SPDY_WEBSOCKET) { @@ -380,7 +382,7 @@ class WebSocketJobTest : public PlatformTest, host_resolver_.reset(new MockHostResolver); context_->set_host_resolver(host_resolver_.get()); - socket_ = new SocketStream(url, websocket_.get()); + socket_ = new SocketStream(url, websocket_.get(), context_.get(), NULL); socket_factory_.reset(new MockClientSocketFactory); DCHECK(data_.get()); socket_factory_->AddSocketDataProvider(data_.get()); @@ -388,7 +390,6 @@ class WebSocketJobTest : public PlatformTest, } websocket_->InitSocketStream(socket_.get()); - websocket_->set_context(context_.get()); // MockHostResolver resolves all hosts to 127.0.0.1; however, when we create // a WebSocketJob purely to block another one in a throttling test, we don't // perform a real connect. In that case, the following address is used @@ -738,14 +739,14 @@ void WebSocketJobTest::TestHSTSUpgrade() { scoped_refptr<SocketStreamJob> job = SocketStreamJob::CreateSocketStreamJob( url, &delegate, context_->transport_security_state(), - context_->ssl_config_service()); + context_->ssl_config_service(), NULL, NULL); EXPECT_TRUE(GetSocket(job.get())->is_secure()); job->DetachDelegate(); url = GURL("ws://donotupgrademe.com/"); job = SocketStreamJob::CreateSocketStreamJob( url, &delegate, context_->transport_security_state(), - context_->ssl_config_service()); + context_->ssl_config_service(), NULL, NULL); EXPECT_FALSE(GetSocket(job.get())->is_secure()); job->DetachDelegate(); } diff --git a/net/websockets/websocket_throttle_test.cc b/net/websockets/websocket_throttle_test.cc index 14237b9..7b33883 100644 --- a/net/websockets/websocket_throttle_test.cc +++ b/net/websockets/websocket_throttle_test.cc @@ -73,8 +73,7 @@ TEST_F(WebSocketThrottleTest, Throttle) { addr.push_back(MakeAddr(1, 2, 3, 6)); scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); scoped_refptr<SocketStream> s1( - new SocketStream(GURL("ws://host1/"), w1.get())); - s1->set_context(&context); + new SocketStream(GURL("ws://host1/"), w1.get(), &context, NULL)); w1->InitSocketStream(s1.get()); WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr); @@ -94,8 +93,7 @@ TEST_F(WebSocketThrottleTest, Throttle) { addr.push_back(MakeAddr(1, 2, 3, 4)); scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); scoped_refptr<SocketStream> s2( - new SocketStream(GURL("ws://host2/"), w2.get())); - s2->set_context(&context); + new SocketStream(GURL("ws://host2/"), w2.get(), &context, NULL)); w2->InitSocketStream(s2.get()); WebSocketThrottleTest::MockSocketStreamConnect(s2.get(), addr); @@ -115,8 +113,7 @@ TEST_F(WebSocketThrottleTest, Throttle) { addr.push_back(MakeAddr(1, 2, 3, 5)); scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); scoped_refptr<SocketStream> s3( - new SocketStream(GURL("ws://host3/"), w3.get())); - s3->set_context(&context); + new SocketStream(GURL("ws://host3/"), w3.get(), &context, NULL)); w3->InitSocketStream(s3.get()); WebSocketThrottleTest::MockSocketStreamConnect(s3.get(), addr); @@ -136,8 +133,7 @@ TEST_F(WebSocketThrottleTest, Throttle) { addr.push_back(MakeAddr(1, 2, 3, 6)); scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); scoped_refptr<SocketStream> s4( - new SocketStream(GURL("ws://host4/"), w4.get())); - s4->set_context(&context); + new SocketStream(GURL("ws://host4/"), w4.get(), &context, NULL)); w4->InitSocketStream(s4.get()); WebSocketThrottleTest::MockSocketStreamConnect(s4.get(), addr); @@ -156,8 +152,7 @@ TEST_F(WebSocketThrottleTest, Throttle) { addr.push_back(MakeAddr(1, 2, 3, 6)); scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); scoped_refptr<SocketStream> s5( - new SocketStream(GURL("ws://host5/"), w5.get())); - s5->set_context(&context); + new SocketStream(GURL("ws://host5/"), w5.get(), &context, NULL)); w5->InitSocketStream(s5.get()); WebSocketThrottleTest::MockSocketStreamConnect(s5.get(), addr); @@ -176,8 +171,7 @@ TEST_F(WebSocketThrottleTest, Throttle) { addr.push_back(MakeAddr(1, 2, 3, 6)); scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); scoped_refptr<SocketStream> s6( - new SocketStream(GURL("ws://host6/"), w6.get())); - s6->set_context(&context); + new SocketStream(GURL("ws://host6/"), w6.get(), &context, NULL)); w6->InitSocketStream(s6.get()); WebSocketThrottleTest::MockSocketStreamConnect(s6.get(), addr); @@ -289,8 +283,7 @@ TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { addr.push_back(MakeAddr(127, 0, 0, 1)); scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); scoped_refptr<SocketStream> s1( - new SocketStream(GURL("ws://localhost/"), w1.get())); - s1->set_context(&context); + new SocketStream(GURL("ws://localhost/"), w1.get(), &context, NULL)); w1->InitSocketStream(s1.get()); WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr); @@ -318,8 +311,7 @@ TEST_F(WebSocketThrottleTest, NoThrottleForDistinctPort) { // socket1: 127.0.0.1:80 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); scoped_refptr<SocketStream> s1( - new SocketStream(GURL("ws://localhost:80/"), w1.get())); - s1->set_context(&context); + new SocketStream(GURL("ws://localhost:80/"), w1.get(), &context, NULL)); w1->InitSocketStream(s1.get()); MockSocketStreamConnect(s1.get(), AddressList::CreateFromIPAddress(localhost, 80)); @@ -332,8 +324,7 @@ TEST_F(WebSocketThrottleTest, NoThrottleForDistinctPort) { // socket2: 127.0.0.1:81 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); scoped_refptr<SocketStream> s2( - new SocketStream(GURL("ws://localhost:81/"), w2.get())); - s2->set_context(&context); + new SocketStream(GURL("ws://localhost:81/"), w2.get(), &context, NULL)); w2->InitSocketStream(s2.get()); MockSocketStreamConnect(s2.get(), AddressList::CreateFromIPAddress(localhost, 81)); |