1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_SOCKET_SOCKS_CLIENT_SOCKET_POOL_H_
6 #define NET_SOCKET_SOCKS_CLIENT_SOCKET_POOL_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "net/base/host_port_pair.h"
16 #include "net/dns/host_resolver.h"
17 #include "net/socket/client_socket_pool.h"
18 #include "net/socket/client_socket_pool_base.h"
19 #include "net/socket/client_socket_pool_histograms.h"
23 class ConnectJobFactory
;
24 class TransportClientSocketPool
;
25 class TransportSocketParams
;
27 class NET_EXPORT_PRIVATE SOCKSSocketParams
28 : public base::RefCounted
<SOCKSSocketParams
> {
30 SOCKSSocketParams(const scoped_refptr
<TransportSocketParams
>& proxy_server
,
31 bool socks_v5
, const HostPortPair
& host_port_pair
);
33 const scoped_refptr
<TransportSocketParams
>& transport_params() const {
34 return transport_params_
;
36 const HostResolver::RequestInfo
& destination() const { return destination_
; }
37 bool is_socks_v5() const { return socks_v5_
; }
38 bool ignore_limits() const { return ignore_limits_
; }
41 friend class base::RefCounted
<SOCKSSocketParams
>;
44 // The transport (likely TCP) connection must point toward the proxy server.
45 const scoped_refptr
<TransportSocketParams
> transport_params_
;
46 // This is the HTTP destination.
47 HostResolver::RequestInfo destination_
;
51 DISALLOW_COPY_AND_ASSIGN(SOCKSSocketParams
);
54 // SOCKSConnectJob handles the handshake to a socks server after setting up
55 // an underlying transport socket.
56 class SOCKSConnectJob
: public ConnectJob
{
58 SOCKSConnectJob(const std::string
& group_name
,
59 RequestPriority priority
,
60 const scoped_refptr
<SOCKSSocketParams
>& params
,
61 const base::TimeDelta
& timeout_duration
,
62 TransportClientSocketPool
* transport_pool
,
63 HostResolver
* host_resolver
,
66 virtual ~SOCKSConnectJob();
68 // ConnectJob methods.
69 virtual LoadState
GetLoadState() const OVERRIDE
;
73 STATE_TRANSPORT_CONNECT
,
74 STATE_TRANSPORT_CONNECT_COMPLETE
,
76 STATE_SOCKS_CONNECT_COMPLETE
,
80 void OnIOComplete(int result
);
82 // Runs the state transition loop.
83 int DoLoop(int result
);
85 int DoTransportConnect();
86 int DoTransportConnectComplete(int result
);
88 int DoSOCKSConnectComplete(int result
);
90 // Begins the transport connection and the SOCKS handshake. Returns OK on
91 // success and ERR_IO_PENDING if it cannot immediately service the request.
92 // Otherwise, it returns a net error code.
93 virtual int ConnectInternal() OVERRIDE
;
95 scoped_refptr
<SOCKSSocketParams
> socks_params_
;
96 TransportClientSocketPool
* const transport_pool_
;
97 HostResolver
* const resolver_
;
100 CompletionCallback callback_
;
101 scoped_ptr
<ClientSocketHandle
> transport_socket_handle_
;
102 scoped_ptr
<StreamSocket
> socket_
;
104 DISALLOW_COPY_AND_ASSIGN(SOCKSConnectJob
);
107 class NET_EXPORT_PRIVATE SOCKSClientSocketPool
108 : public ClientSocketPool
, public HigherLayeredPool
{
110 typedef SOCKSSocketParams SocketParams
;
112 SOCKSClientSocketPool(
114 int max_sockets_per_group
,
115 ClientSocketPoolHistograms
* histograms
,
116 HostResolver
* host_resolver
,
117 TransportClientSocketPool
* transport_pool
,
120 virtual ~SOCKSClientSocketPool();
122 // ClientSocketPool implementation.
123 virtual int RequestSocket(const std::string
& group_name
,
124 const void* connect_params
,
125 RequestPriority priority
,
126 ClientSocketHandle
* handle
,
127 const CompletionCallback
& callback
,
128 const BoundNetLog
& net_log
) OVERRIDE
;
130 virtual void RequestSockets(const std::string
& group_name
,
133 const BoundNetLog
& net_log
) OVERRIDE
;
135 virtual void CancelRequest(const std::string
& group_name
,
136 ClientSocketHandle
* handle
) OVERRIDE
;
138 virtual void ReleaseSocket(const std::string
& group_name
,
139 scoped_ptr
<StreamSocket
> socket
,
142 virtual void FlushWithError(int error
) OVERRIDE
;
144 virtual void CloseIdleSockets() OVERRIDE
;
146 virtual int IdleSocketCount() const OVERRIDE
;
148 virtual int IdleSocketCountInGroup(
149 const std::string
& group_name
) const OVERRIDE
;
151 virtual LoadState
GetLoadState(
152 const std::string
& group_name
,
153 const ClientSocketHandle
* handle
) const OVERRIDE
;
155 virtual base::DictionaryValue
* GetInfoAsValue(
156 const std::string
& name
,
157 const std::string
& type
,
158 bool include_nested_pools
) const OVERRIDE
;
160 virtual base::TimeDelta
ConnectionTimeout() const OVERRIDE
;
162 virtual ClientSocketPoolHistograms
* histograms() const OVERRIDE
;
164 // LowerLayeredPool implementation.
165 virtual bool IsStalled() const OVERRIDE
;
167 virtual void AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) OVERRIDE
;
169 virtual void RemoveHigherLayeredPool(HigherLayeredPool
* higher_pool
) OVERRIDE
;
171 // HigherLayeredPool implementation.
172 virtual bool CloseOneIdleConnection() OVERRIDE
;
175 typedef ClientSocketPoolBase
<SOCKSSocketParams
> PoolBase
;
177 class SOCKSConnectJobFactory
: public PoolBase::ConnectJobFactory
{
179 SOCKSConnectJobFactory(TransportClientSocketPool
* transport_pool
,
180 HostResolver
* host_resolver
,
182 : transport_pool_(transport_pool
),
183 host_resolver_(host_resolver
),
186 virtual ~SOCKSConnectJobFactory() {}
188 // ClientSocketPoolBase::ConnectJobFactory methods.
189 virtual scoped_ptr
<ConnectJob
> NewConnectJob(
190 const std::string
& group_name
,
191 const PoolBase::Request
& request
,
192 ConnectJob::Delegate
* delegate
) const OVERRIDE
;
194 virtual base::TimeDelta
ConnectionTimeout() const OVERRIDE
;
197 TransportClientSocketPool
* const transport_pool_
;
198 HostResolver
* const host_resolver_
;
201 DISALLOW_COPY_AND_ASSIGN(SOCKSConnectJobFactory
);
204 TransportClientSocketPool
* const transport_pool_
;
207 DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocketPool
);
212 #endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_POOL_H_