diff options
Diffstat (limited to 'net/http')
-rw-r--r-- | net/http/http_cache.cc | 8 | ||||
-rw-r--r-- | net/http/http_network_layer.cc | 8 | ||||
-rw-r--r-- | net/http/http_network_layer.h | 2 | ||||
-rw-r--r-- | net/http/http_network_session.cc | 65 | ||||
-rw-r--r-- | net/http/http_network_session.h | 21 | ||||
-rw-r--r-- | net/http/http_network_session_unittest.cc | 82 | ||||
-rw-r--r-- | net/http/http_network_transaction_unittest.cc | 11 |
7 files changed, 145 insertions, 52 deletions
diff --git a/net/http/http_cache.cc b/net/http/http_cache.cc index 8b0811f..8a44565 100644 --- a/net/http/http_cache.cc +++ b/net/http/http_cache.cc @@ -377,12 +377,8 @@ void HttpCache::CloseCurrentConnections() { net::HttpNetworkLayer* network = static_cast<net::HttpNetworkLayer*>(network_layer_.get()); HttpNetworkSession* session = network->GetSession(); - if (session) { - session->tcp_socket_pool()->CloseIdleSockets(); - if (session->spdy_session_pool()) - session->spdy_session_pool()->CloseAllSessions(); - session->ReplaceTCPSocketPool(); - } + if (session) + session->Flush(); } //----------------------------------------------------------------------------- diff --git a/net/http/http_network_layer.cc b/net/http/http_network_layer.cc index 068b276..8a22514 100644 --- a/net/http/http_network_layer.cc +++ b/net/http/http_network_layer.cc @@ -12,7 +12,6 @@ #include "net/spdy/spdy_framer.h" #include "net/spdy/spdy_network_transaction.h" #include "net/spdy/spdy_session.h" -#include "net/spdy/spdy_session_pool.h" namespace net { @@ -57,7 +56,6 @@ HttpNetworkLayer::HttpNetworkLayer( proxy_service_(proxy_service), ssl_config_service_(ssl_config_service), session_(NULL), - spdy_session_pool_(NULL), http_auth_handler_factory_(http_auth_handler_factory), suspended_(false) { DCHECK(proxy_service_); @@ -69,7 +67,6 @@ HttpNetworkLayer::HttpNetworkLayer(HttpNetworkSession* session) network_change_notifier_(NULL), ssl_config_service_(NULL), session_(session), - spdy_session_pool_(session->spdy_session_pool()), http_auth_handler_factory_(NULL), suspended_(false) { DCHECK(session_.get()); @@ -97,16 +94,15 @@ void HttpNetworkLayer::Suspend(bool suspend) { suspended_ = suspend; if (suspend && session_) - session_->tcp_socket_pool()->CloseIdleSockets(); + session_->Flush(); } HttpNetworkSession* HttpNetworkLayer::GetSession() { if (!session_) { DCHECK(proxy_service_); - SpdySessionPool* spdy_pool = new SpdySessionPool; session_ = new HttpNetworkSession( network_change_notifier_, host_resolver_, proxy_service_, - socket_factory_, ssl_config_service_, spdy_pool, + socket_factory_, ssl_config_service_, http_auth_handler_factory_); // These were just temps for lazy-initializing HttpNetworkSession. network_change_notifier_ = NULL; diff --git a/net/http/http_network_layer.h b/net/http/http_network_layer.h index 2d4130d..63df00b 100644 --- a/net/http/http_network_layer.h +++ b/net/http/http_network_layer.h @@ -20,7 +20,6 @@ class HttpNetworkSession; class NetworkChangeNotifier; class ProxyInfo; class ProxyService; -class SpdySessionPool; class SSLConfigService; class HttpNetworkLayer : public HttpTransactionFactory { @@ -82,7 +81,6 @@ class HttpNetworkLayer : public HttpTransactionFactory { scoped_refptr<SSLConfigService> ssl_config_service_; scoped_refptr<HttpNetworkSession> session_; - scoped_refptr<SpdySessionPool> spdy_session_pool_; HttpAuthHandlerFactory* http_auth_handler_factory_; diff --git a/net/http/http_network_session.cc b/net/http/http_network_session.cc index 3c44b81..90e865f 100644 --- a/net/http/http_network_session.cc +++ b/net/http/http_network_session.cc @@ -29,30 +29,26 @@ HttpNetworkSession::HttpNetworkSession( ProxyService* proxy_service, ClientSocketFactory* client_socket_factory, SSLConfigService* ssl_config_service, - SpdySessionPool* spdy_session_pool, HttpAuthHandlerFactory* http_auth_handler_factory) : network_change_notifier_(network_change_notifier), - tcp_socket_pool_(new TCPClientSocketPool( - max_sockets_, max_sockets_per_group_, "Transport", - host_resolver, client_socket_factory, network_change_notifier_)), - socks_socket_pool_(new SOCKSClientSocketPool( - max_sockets_, max_sockets_per_group_, "SOCKS", host_resolver, - new TCPClientSocketPool(max_sockets_, max_sockets_per_group_, - "TCPForSOCKS", host_resolver, - client_socket_factory, - network_change_notifier_), - network_change_notifier_)), socket_factory_(client_socket_factory), host_resolver_(host_resolver), + tcp_socket_pool_(CreateNewTCPSocketPool()), + socks_socket_pool_(CreateNewSOCKSSocketPool()), proxy_service_(proxy_service), ssl_config_service_(ssl_config_service), - spdy_session_pool_(spdy_session_pool), + spdy_session_pool_(new SpdySessionPool()), http_auth_handler_factory_(http_auth_handler_factory) { DCHECK(proxy_service); DCHECK(ssl_config_service); + + if (network_change_notifier) + network_change_notifier_->AddObserver(this); } HttpNetworkSession::~HttpNetworkSession() { + if (network_change_notifier_) + network_change_notifier_->RemoveObserver(this); } URLSecurityManager* HttpNetworkSession::GetURLSecurityManager() { @@ -67,21 +63,44 @@ URLSecurityManager* HttpNetworkSession::GetURLSecurityManager() { // static void HttpNetworkSession::set_max_sockets_per_group(int socket_count) { - DCHECK(0 < socket_count); + DCHECK_LT(0, socket_count); // The following is a sanity check... but we should NEVER be near this value. - DCHECK(100 > socket_count); + DCHECK_GT(100, socket_count); max_sockets_per_group_ = socket_count; } -// TODO(vandebo) when we've completely converted to pools, the base TCP -// pool name should get changed to TCP instead of Transport. -void HttpNetworkSession::ReplaceTCPSocketPool() { - tcp_socket_pool_ = new TCPClientSocketPool(max_sockets_, - max_sockets_per_group_, - "Transport", - host_resolver_, - socket_factory_, - network_change_notifier_); +void HttpNetworkSession::Flush() { + host_resolver()->Flush(); + tcp_socket_pool()->CloseIdleSockets(); + tcp_socket_pool_ = CreateNewTCPSocketPool(); + socks_socket_pool()->CloseIdleSockets(); + socks_socket_pool_ = CreateNewSOCKSSocketPool(); + spdy_session_pool_->CloseAllSessions(); + spdy_session_pool_ = new SpdySessionPool; +} + +void HttpNetworkSession::OnIPAddressChanged() { + Flush(); +} + +scoped_refptr<TCPClientSocketPool> +HttpNetworkSession::CreateNewTCPSocketPool() { + // TODO(vandebo) when we've completely converted to pools, the base TCP + // pool name should get changed to TCP instead of Transport. + return new TCPClientSocketPool(max_sockets_, + max_sockets_per_group_, + "Transport", + host_resolver_, + socket_factory_); +} + +scoped_refptr<SOCKSClientSocketPool> +HttpNetworkSession::CreateNewSOCKSSocketPool() { + return new SOCKSClientSocketPool( + max_sockets_, max_sockets_per_group_, "SOCKS", host_resolver_, + new TCPClientSocketPool(max_sockets_, max_sockets_per_group_, + "TCPForSOCKS", host_resolver_, + socket_factory_)); } } // namespace net diff --git a/net/http/http_network_session.h b/net/http/http_network_session.h index fcfaac4..540c59b 100644 --- a/net/http/http_network_session.h +++ b/net/http/http_network_session.h @@ -8,6 +8,7 @@ #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "net/base/host_resolver.h" +#include "net/base/network_change_notifier.h" #include "net/base/ssl_client_auth_cache.h" #include "net/base/ssl_config_service.h" #include "net/http/http_alternate_protocols.h" @@ -25,7 +26,9 @@ class NetworkChangeNotifier; class URLSecurityManager; // This class holds session objects used by HttpNetworkTransaction objects. -class HttpNetworkSession : public base::RefCounted<HttpNetworkSession> { +class HttpNetworkSession + : public base::RefCounted<HttpNetworkSession>, + public NetworkChangeNotifier::Observer { public: HttpNetworkSession( NetworkChangeNotifier* network_change_notifier, @@ -33,7 +36,6 @@ class HttpNetworkSession : public base::RefCounted<HttpNetworkSession> { ProxyService* proxy_service, ClientSocketFactory* client_socket_factory, SSLConfigService* ssl_config_service, - SpdySessionPool* spdy_session_pool, HttpAuthHandlerFactory* http_auth_handler_factory); HttpAuthCache* auth_cache() { return &auth_cache_; } @@ -70,9 +72,11 @@ class HttpNetworkSession : public base::RefCounted<HttpNetworkSession> { // Returns a pointer to the URL security manager. URLSecurityManager* GetURLSecurityManager(); - // Replace the current socket pool with a new one. This effectively - // abandons the current pool. This is only used for debugging. - void ReplaceTCPSocketPool(); + // Flushes cached data in the HttpNetworkSession. + void Flush(); + + // NetworkChangeNotifier::Observer methods: + virtual void OnIPAddressChanged(); static void set_max_sockets_per_group(int socket_count); @@ -88,6 +92,9 @@ class HttpNetworkSession : public base::RefCounted<HttpNetworkSession> { ~HttpNetworkSession(); + scoped_refptr<TCPClientSocketPool> CreateNewTCPSocketPool(); + scoped_refptr<SOCKSClientSocketPool> CreateNewSOCKSSocketPool(); + // Total limit of sockets. Not a constant to allow experiments. static int max_sockets_; @@ -103,10 +110,10 @@ class HttpNetworkSession : public base::RefCounted<HttpNetworkSession> { SSLClientAuthCache ssl_client_auth_cache_; HttpAlternateProtocols alternate_protocols_; NetworkChangeNotifier* const network_change_notifier_; - scoped_refptr<TCPClientSocketPool> tcp_socket_pool_; - scoped_refptr<SOCKSClientSocketPool> socks_socket_pool_; ClientSocketFactory* socket_factory_; scoped_refptr<HostResolver> host_resolver_; + scoped_refptr<TCPClientSocketPool> tcp_socket_pool_; + scoped_refptr<SOCKSClientSocketPool> socks_socket_pool_; scoped_refptr<ProxyService> proxy_service_; scoped_refptr<SSLConfigService> ssl_config_service_; scoped_refptr<SpdySessionPool> spdy_session_pool_; diff --git a/net/http/http_network_session_unittest.cc b/net/http/http_network_session_unittest.cc new file mode 100644 index 0000000..329f394c --- /dev/null +++ b/net/http/http_network_session_unittest.cc @@ -0,0 +1,82 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/http/http_network_session.h" + +#include "base/ref_counted.h" +#include "net/base/mock_host_resolver.h" +#include "net/base/mock_network_change_notifier.h" +#include "net/base/ssl_config_service_defaults.h" +#include "net/base/test_completion_callback.h" +#include "net/http/http_auth_handler_factory.h" +#include "net/proxy/proxy_service.h" +#include "net/socket/client_socket_handle.h" +#include "net/socket/socket_test_util.h" +#include "net/socket/tcp_client_socket_pool.h" +#include "net/spdy/spdy_session.h" +#include "net/spdy/spdy_session_pool.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace net { +namespace { + +TEST(HttpNetworkSessionTest, FlushOnNetworkChange) { + MockNetworkChangeNotifier mock_notifier; + scoped_refptr<MockCachingHostResolver> mock_resolver( + new MockCachingHostResolver); + MockClientSocketFactory mock_factory; + scoped_ptr<HttpAuthHandlerFactory> auth_handler_factory( + HttpAuthHandlerFactory::CreateDefault()); + scoped_refptr<HttpNetworkSession> session( + new HttpNetworkSession(&mock_notifier, + mock_resolver, + ProxyService::CreateNull(), + &mock_factory, + new SSLConfigServiceDefaults, + auth_handler_factory.get())); + + scoped_refptr<TCPClientSocketPool> tcp_socket_pool( + session->tcp_socket_pool()); + + StaticSocketDataProvider data; + mock_factory.AddSocketDataProvider(&data); + + TestCompletionCallback callback; + ClientSocketHandle handle; + TCPSocketParams dest("www.google.com", 80, LOW, GURL(), false); + int rv = handle.Init("a", dest, LOW, &callback, tcp_socket_pool, NULL); + EXPECT_EQ(ERR_IO_PENDING, rv); + EXPECT_FALSE(handle.is_initialized()); + EXPECT_FALSE(handle.socket()); + + EXPECT_EQ(OK, callback.WaitForResult()); + EXPECT_TRUE(handle.is_initialized()); + EXPECT_TRUE(handle.socket()); + + handle.Reset(); + + // Need to run all pending to release the socket back to the pool. + MessageLoop::current()->RunAllPending(); + + // Now we should have 1 idle socket. + EXPECT_EQ(1, tcp_socket_pool->IdleSocketCount()); + + HostPortPair host_port_pair("www.google.com", 80); + + scoped_refptr<SpdySession> spdy_session(session->spdy_session_pool()->Get( + host_port_pair, session)); + + EXPECT_TRUE(session->spdy_session_pool()->HasSession(host_port_pair)); + + EXPECT_EQ(1u, mock_resolver->cache()->size()); + + // After an IP address change, we should have 0 idle sockets. + mock_notifier.NotifyIPAddressChange(); + EXPECT_EQ(0, tcp_socket_pool->IdleSocketCount()); + EXPECT_FALSE(session->spdy_session_pool()->HasSession(host_port_pair)); + EXPECT_EQ(0u, mock_resolver->cache()->size()); +} + +} // namespace +} // namespace net diff --git a/net/http/http_network_transaction_unittest.cc b/net/http/http_network_transaction_unittest.cc index 02b784e8..aa18df5 100644 --- a/net/http/http_network_transaction_unittest.cc +++ b/net/http/http_network_transaction_unittest.cc @@ -43,23 +43,20 @@ class SessionDependencies { : host_resolver(new MockHostResolver), proxy_service(ProxyService::CreateNull()), ssl_config_service(new SSLConfigServiceDefaults), - http_auth_handler_factory(HttpAuthHandlerFactory::CreateDefault()), - spdy_session_pool(new SpdySessionPool) {} + http_auth_handler_factory(HttpAuthHandlerFactory::CreateDefault()) {} // Custom proxy service dependency. explicit SessionDependencies(ProxyService* proxy_service) : host_resolver(new MockHostResolver), proxy_service(proxy_service), ssl_config_service(new SSLConfigServiceDefaults), - http_auth_handler_factory(HttpAuthHandlerFactory::CreateDefault()), - spdy_session_pool(new SpdySessionPool) {} + http_auth_handler_factory(HttpAuthHandlerFactory::CreateDefault()) {} scoped_refptr<MockHostResolverBase> host_resolver; scoped_refptr<ProxyService> proxy_service; scoped_refptr<SSLConfigService> ssl_config_service; MockClientSocketFactory socket_factory; scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; - scoped_refptr<SpdySessionPool> spdy_session_pool; }; ProxyService* CreateFixedProxyService(const std::string& proxy) { @@ -75,7 +72,6 @@ HttpNetworkSession* CreateSession(SessionDependencies* session_deps) { session_deps->proxy_service, &session_deps->socket_factory, session_deps->ssl_config_service, - session_deps->spdy_session_pool, session_deps->http_auth_handler_factory.get()); } @@ -189,8 +185,7 @@ class CaptureGroupNameSocketPool : public EmulatedClientSocketPool { CaptureGroupNameSocketPool(HttpNetworkSession* session, SocketSourceType* socket_source) : EmulatedClientSocketPool(0, 0, "CaptureGroupNameTestPool", - session->host_resolver(), socket_source, - NULL) {} + session->host_resolver(), socket_source) {} const std::string last_group_name_received() const { return last_group_name_; } |