blob: 2352b867649e8ca5f8f432a30a5e756803449964 [file] [log] [blame]
// Copyright (c) 2012 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.
#ifndef NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_
#define NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "net/base/net_export.h"
#include "net/socket/client_socket_pool.h"
#include "net/socket/client_socket_pool_base.h"
#include "net/socket/connection_attempts.h"
#include "net/socket/socket_tag.h"
#include "net/socket/ssl_client_socket.h"
#include "net/ssl/ssl_config_service.h"
namespace net {
class CertVerifier;
class ChannelIDService;
class ClientSocketFactory;
class CTVerifier;
class CTPolicyEnforcer;
class HostResolver;
class HttpProxySocketParams;
class NetLog;
class NetLogWithSource;
class NetworkQualityEstimator;
class ProxyDelegate;
class SocketPerformanceWatcherFactory;
class SOCKSSocketParams;
class SSLSocketParams;
class TransportSecurityState;
class TransportSocketParams;
class NET_EXPORT_PRIVATE TransportClientSocketPool
: public ClientSocketPool,
public HigherLayeredPool,
public SSLConfigService::Observer {
public:
// Callback to create a ConnectJob using the provided arguments. The lower
// level parameters used to construct the ConnectJob (like hostname, type of
// socket, proxy, etc) are all already bound to the callback. If
// |websocket_endpoint_lock_manager| is non-null, a ConnectJob for use by
// WebSockets should be created.
//
// |http_proxy_pool_for_ssl_pool| is for the case of SSLConnectJobs that will
// be layered on top of an http proxy socket pool.
//
// |transport_pool_for_http_proxy_pool| and |ssl_pool_for_http_proxy_pool| are
// socket pools that sit beneath the current pool, for the HTTP proxy case.
//
// TODO(https://crbug.com/927084): Remove the TransportClientSocketPool
// arguments.
using CreateConnectJobCallback =
base::RepeatingCallback<std::unique_ptr<ConnectJob>(
RequestPriority priority,
const CommonConnectJobParams& common_connect_job_params,
ConnectJob::Delegate* delegate,
TransportClientSocketPool* http_proxy_pool_for_ssl_pool,
TransportClientSocketPool* transport_pool_for_http_proxy_pool,
TransportClientSocketPool* ssl_pool_for_http_proxy_pool)>;
// "Parameters" that own a single callback for creating a ConnectJob that can
// be of any type.
//
// TODO(mmenke): Once all the socket pool subclasses have been merged, replace
// this class with a callback.
class NET_EXPORT_PRIVATE SocketParams
: public base::RefCounted<SocketParams> {
public:
explicit SocketParams(
const CreateConnectJobCallback& create_connect_job_callback);
const CreateConnectJobCallback& create_connect_job_callback() {
return create_connect_job_callback_;
}
static scoped_refptr<SocketParams> CreateFromTransportSocketParams(
scoped_refptr<TransportSocketParams> transport_client_params);
static scoped_refptr<SocketParams> CreateFromSOCKSSocketParams(
scoped_refptr<SOCKSSocketParams> socks_socket_params);
static scoped_refptr<SocketParams> CreateFromSSLSocketParams(
scoped_refptr<SSLSocketParams> ssl_socket_params);
static scoped_refptr<SocketParams> CreateFromHttpProxySocketParams(
scoped_refptr<HttpProxySocketParams> http_proxy_socket_params);
private:
friend class base::RefCounted<SocketParams>;
~SocketParams();
const CreateConnectJobCallback create_connect_job_callback_;
DISALLOW_COPY_AND_ASSIGN(SocketParams);
};
// If this is being used for an SSL socket pool, the
// |http_proxy_pool_for_ssl_pool| socket pool is used for HTTP proxy tunnels
// beneath the SSL socket pool.
//
// If this is being used for an HTTP proxy socket pool, the
// |transport_pool_for_http_proxy_pool| and |ssl_pool_for_http_proxy_pool| are
// used for SSL and TCP connections beneath the HTTP proxy socket pool.
//
// TODO(https://crbug.com/927084): Remove all pool arguments, once
// the socket pools are flat.
TransportClientSocketPool(
int max_sockets,
int max_sockets_per_group,
ClientSocketFactory* client_socket_factory,
HostResolver* host_resolver,
ProxyDelegate* proxy_delegate,
CertVerifier* cert_verifier,
ChannelIDService* channel_id_service,
TransportSecurityState* transport_security_state,
CTVerifier* cert_transparency_verifier,
CTPolicyEnforcer* ct_policy_enforcer,
SSLClientSessionCache* ssl_client_session_cache,
const std::string& ssl_session_cache_shard,
SSLConfigService* ssl_config_service,
SocketPerformanceWatcherFactory* socket_performance_watcher_factory,
NetworkQualityEstimator* network_quality_estimator,
NetLog* net_log,
TransportClientSocketPool* http_proxy_pool_for_ssl_pool = nullptr,
TransportClientSocketPool* transport_pool_for_http_proxy_pool = nullptr,
TransportClientSocketPool* ssl_pool_for_http_proxy_pool = nullptr);
~TransportClientSocketPool() override;
// ClientSocketPool implementation.
int RequestSocket(const std::string& group_name,
const void* resolve_info,
RequestPriority priority,
const SocketTag& socket_tag,
RespectLimits respect_limits,
ClientSocketHandle* handle,
CompletionOnceCallback callback,
const NetLogWithSource& net_log) override;
void RequestSockets(const std::string& group_name,
const void* params,
int num_sockets,
const NetLogWithSource& net_log) override;
void SetPriority(const std::string& group_name,
ClientSocketHandle* handle,
RequestPriority priority) override;
void CancelRequest(const std::string& group_name,
ClientSocketHandle* handle) override;
void ReleaseSocket(const std::string& group_name,
std::unique_ptr<StreamSocket> socket,
int id) override;
void FlushWithError(int error) override;
void CloseIdleSockets() override;
void CloseIdleSocketsInGroup(const std::string& group_name) override;
int IdleSocketCount() const override;
int IdleSocketCountInGroup(const std::string& group_name) const override;
LoadState GetLoadState(const std::string& group_name,
const ClientSocketHandle* handle) const override;
std::unique_ptr<base::DictionaryValue> GetInfoAsValue(
const std::string& name,
const std::string& type,
bool include_nested_pools) const override;
// LowerLayeredPool implementation.
bool IsStalled() const override;
void AddHigherLayeredPool(HigherLayeredPool* higher_pool) override;
void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) override;
// HigherLayeredPool implementation.
bool CloseOneIdleConnection() override;
void DumpMemoryStats(base::trace_event::ProcessMemoryDump* pmd,
const std::string& parent_dump_absolute_name) const;
ClientSocketFactory* client_socket_factory() {
return client_socket_factory_;
}
protected:
// Methods shared with WebSocketTransportClientSocketPool
void NetLogTcpClientSocketPoolRequestedSocket(const NetLogWithSource& net_log,
const std::string& group_name);
private:
typedef ClientSocketPoolBase<SocketParams> PoolBase;
class TransportConnectJobFactory
: public PoolBase::ConnectJobFactory {
public:
TransportConnectJobFactory(
ClientSocketFactory* client_socket_factory,
HostResolver* host_resolver,
ProxyDelegate* proxy_delegate,
const SSLClientSocketContext& ssl_client_socket_context,
SocketPerformanceWatcherFactory* socket_performance_watcher_factory,
NetworkQualityEstimator* network_quality_estimator,
NetLog* net_log,
TransportClientSocketPool* http_proxy_pool_for_ssl_pool,
TransportClientSocketPool* transport_pool_for_http_proxy_pool,
TransportClientSocketPool* ssl_pool_for_http_proxy_pool);
~TransportConnectJobFactory() override;
// ClientSocketPoolBase::ConnectJobFactory methods.
std::unique_ptr<ConnectJob> NewConnectJob(
const std::string& group_name,
const PoolBase::Request& request,
ConnectJob::Delegate* delegate) const override;
private:
ClientSocketFactory* const client_socket_factory_;
HostResolver* const host_resolver_;
ProxyDelegate* const proxy_delegate_;
const SSLClientSocketContext ssl_client_socket_context_;
SocketPerformanceWatcherFactory* const socket_performance_watcher_factory_;
NetworkQualityEstimator* const network_quality_estimator_;
NetLog* const net_log_;
TransportClientSocketPool* const http_proxy_pool_for_ssl_pool_;
TransportClientSocketPool* const transport_pool_for_http_proxy_pool_;
TransportClientSocketPool* const ssl_pool_for_http_proxy_pool_;
DISALLOW_COPY_AND_ASSIGN(TransportConnectJobFactory);
};
// SSLConfigService::Observer methods.
void OnSSLConfigChanged() override;
PoolBase base_;
ClientSocketFactory* const client_socket_factory_;
SSLConfigService* const ssl_config_service_;
DISALLOW_COPY_AND_ASSIGN(TransportClientSocketPool);
};
} // namespace net
#endif // NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_H_