Patch 3/3 to get WebScheduler via WebThread
[chromium-blink-merge.git] / net / socket / socks_client_socket_pool.h
blob69bcf00f076851953aa9db9be8638affccb854ae
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_
8 #include <string>
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"
20 namespace net {
22 class ConnectJobFactory;
23 class TransportClientSocketPool;
24 class TransportSocketParams;
26 class NET_EXPORT_PRIVATE SOCKSSocketParams
27 : public base::RefCounted<SOCKSSocketParams> {
28 public:
29 SOCKSSocketParams(const scoped_refptr<TransportSocketParams>& proxy_server,
30 bool socks_v5, const HostPortPair& host_port_pair);
32 const scoped_refptr<TransportSocketParams>& transport_params() const {
33 return transport_params_;
35 const HostResolver::RequestInfo& destination() const { return destination_; }
36 bool is_socks_v5() const { return socks_v5_; }
37 bool ignore_limits() const { return ignore_limits_; }
39 private:
40 friend class base::RefCounted<SOCKSSocketParams>;
41 ~SOCKSSocketParams();
43 // The transport (likely TCP) connection must point toward the proxy server.
44 const scoped_refptr<TransportSocketParams> transport_params_;
45 // This is the HTTP destination.
46 HostResolver::RequestInfo destination_;
47 const bool socks_v5_;
48 bool ignore_limits_;
50 DISALLOW_COPY_AND_ASSIGN(SOCKSSocketParams);
53 // SOCKSConnectJob handles the handshake to a socks server after setting up
54 // an underlying transport socket.
55 class SOCKSConnectJob : public ConnectJob {
56 public:
57 SOCKSConnectJob(const std::string& group_name,
58 RequestPriority priority,
59 const scoped_refptr<SOCKSSocketParams>& params,
60 const base::TimeDelta& timeout_duration,
61 TransportClientSocketPool* transport_pool,
62 HostResolver* host_resolver,
63 Delegate* delegate,
64 NetLog* net_log);
65 ~SOCKSConnectJob() override;
67 // ConnectJob methods.
68 LoadState GetLoadState() const override;
70 private:
71 enum State {
72 STATE_TRANSPORT_CONNECT,
73 STATE_TRANSPORT_CONNECT_COMPLETE,
74 STATE_SOCKS_CONNECT,
75 STATE_SOCKS_CONNECT_COMPLETE,
76 STATE_NONE,
79 void OnIOComplete(int result);
81 // Runs the state transition loop.
82 int DoLoop(int result);
84 int DoTransportConnect();
85 int DoTransportConnectComplete(int result);
86 int DoSOCKSConnect();
87 int DoSOCKSConnectComplete(int result);
89 // Begins the transport connection and the SOCKS handshake. Returns OK on
90 // success and ERR_IO_PENDING if it cannot immediately service the request.
91 // Otherwise, it returns a net error code.
92 int ConnectInternal() override;
94 scoped_refptr<SOCKSSocketParams> socks_params_;
95 TransportClientSocketPool* const transport_pool_;
96 HostResolver* const resolver_;
98 State next_state_;
99 CompletionCallback callback_;
100 scoped_ptr<ClientSocketHandle> transport_socket_handle_;
101 scoped_ptr<StreamSocket> socket_;
103 DISALLOW_COPY_AND_ASSIGN(SOCKSConnectJob);
106 class NET_EXPORT_PRIVATE SOCKSClientSocketPool
107 : public ClientSocketPool, public HigherLayeredPool {
108 public:
109 typedef SOCKSSocketParams SocketParams;
111 SOCKSClientSocketPool(
112 int max_sockets,
113 int max_sockets_per_group,
114 HostResolver* host_resolver,
115 TransportClientSocketPool* transport_pool,
116 NetLog* net_log);
118 ~SOCKSClientSocketPool() override;
120 // ClientSocketPool implementation.
121 int RequestSocket(const std::string& group_name,
122 const void* connect_params,
123 RequestPriority priority,
124 ClientSocketHandle* handle,
125 const CompletionCallback& callback,
126 const BoundNetLog& net_log) override;
128 void RequestSockets(const std::string& group_name,
129 const void* params,
130 int num_sockets,
131 const BoundNetLog& net_log) override;
133 void CancelRequest(const std::string& group_name,
134 ClientSocketHandle* handle) override;
136 void ReleaseSocket(const std::string& group_name,
137 scoped_ptr<StreamSocket> socket,
138 int id) override;
140 void FlushWithError(int error) override;
142 void CloseIdleSockets() override;
144 int IdleSocketCount() const override;
146 int IdleSocketCountInGroup(const std::string& group_name) const override;
148 LoadState GetLoadState(const std::string& group_name,
149 const ClientSocketHandle* handle) const override;
151 base::DictionaryValue* GetInfoAsValue(
152 const std::string& name,
153 const std::string& type,
154 bool include_nested_pools) const override;
156 base::TimeDelta ConnectionTimeout() const override;
158 // LowerLayeredPool implementation.
159 bool IsStalled() const override;
161 void AddHigherLayeredPool(HigherLayeredPool* higher_pool) override;
163 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) override;
165 // HigherLayeredPool implementation.
166 bool CloseOneIdleConnection() override;
168 private:
169 typedef ClientSocketPoolBase<SOCKSSocketParams> PoolBase;
171 class SOCKSConnectJobFactory : public PoolBase::ConnectJobFactory {
172 public:
173 SOCKSConnectJobFactory(TransportClientSocketPool* transport_pool,
174 HostResolver* host_resolver,
175 NetLog* net_log)
176 : transport_pool_(transport_pool),
177 host_resolver_(host_resolver),
178 net_log_(net_log) {}
180 ~SOCKSConnectJobFactory() override {}
182 // ClientSocketPoolBase::ConnectJobFactory methods.
183 scoped_ptr<ConnectJob> NewConnectJob(
184 const std::string& group_name,
185 const PoolBase::Request& request,
186 ConnectJob::Delegate* delegate) const override;
188 base::TimeDelta ConnectionTimeout() const override;
190 private:
191 TransportClientSocketPool* const transport_pool_;
192 HostResolver* const host_resolver_;
193 NetLog* net_log_;
195 DISALLOW_COPY_AND_ASSIGN(SOCKSConnectJobFactory);
198 TransportClientSocketPool* const transport_pool_;
199 PoolBase base_;
201 DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocketPool);
204 } // namespace net
206 #endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_POOL_H_