OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #ifndef NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ |
| 6 #define NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/ref_counted.h" |
| 11 #include "base/scoped_ptr.h" |
| 12 #include "base/time.h" |
| 13 #include "net/base/host_resolver.h" |
| 14 #include "net/base/ssl_config_service.h" |
| 15 #include "net/http/http_proxy_client_socket.h" |
| 16 #include "net/http/http_proxy_client_socket_pool.h" |
| 17 #include "net/proxy/proxy_server.h" |
| 18 #include "net/socket/client_socket_factory.h" |
| 19 #include "net/socket/client_socket_pool_base.h" |
| 20 #include "net/socket/client_socket_pool_histograms.h" |
| 21 #include "net/socket/client_socket_pool.h" |
| 22 #include "net/socket/socks_client_socket_pool.h" |
| 23 #include "net/socket/ssl_client_socket.h" |
| 24 #include "net/socket/tcp_client_socket_pool.h" |
| 25 |
| 26 namespace net { |
| 27 |
| 28 class ClientSocketFactory; |
| 29 class ConnectJobFactory; |
| 30 |
| 31 // SSLSocketParams only needs the socket params for the transport socket |
| 32 // that will be used (denoted by |proxy|). |
| 33 class SSLSocketParams : public base::RefCounted<SSLSocketParams> { |
| 34 public: |
| 35 SSLSocketParams(const scoped_refptr<TCPSocketParams>& tcp_params, |
| 36 const scoped_refptr<HttpProxySocketParams>& http_proxy_params, |
| 37 const scoped_refptr<SOCKSSocketParams>& socks_params, |
| 38 ProxyServer::Scheme proxy, |
| 39 const std::string& hostname, |
| 40 const SSLConfig& ssl_config, |
| 41 int load_flags, |
| 42 bool want_spdy); |
| 43 |
| 44 const scoped_refptr<TCPSocketParams>& tcp_params() { return tcp_params_; } |
| 45 const scoped_refptr<HttpProxySocketParams>& http_proxy_params () { |
| 46 return http_proxy_params_; |
| 47 } |
| 48 const scoped_refptr<SOCKSSocketParams>& socks_params() { |
| 49 return socks_params_; |
| 50 } |
| 51 ProxyServer::Scheme proxy() const { return proxy_; } |
| 52 const std::string& hostname() const { return hostname_; } |
| 53 const SSLConfig& ssl_config() const { return ssl_config_; } |
| 54 int load_flags() const { return load_flags_; } |
| 55 bool want_spdy() const { return want_spdy_; } |
| 56 |
| 57 private: |
| 58 friend class base::RefCounted<SSLSocketParams>; |
| 59 ~SSLSocketParams(); |
| 60 |
| 61 const scoped_refptr<TCPSocketParams> tcp_params_; |
| 62 const scoped_refptr<HttpProxySocketParams> http_proxy_params_; |
| 63 const scoped_refptr<SOCKSSocketParams> socks_params_; |
| 64 const ProxyServer::Scheme proxy_; |
| 65 const std::string hostname_; |
| 66 const SSLConfig ssl_config_; |
| 67 const int load_flags_; |
| 68 const bool want_spdy_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams); |
| 71 }; |
| 72 |
| 73 // SSLConnectJob handles the SSL handshake after setting up the underlying |
| 74 // connection as specified in the params. |
| 75 class SSLConnectJob : public ConnectJob { |
| 76 public: |
| 77 SSLConnectJob( |
| 78 const std::string& group_name, |
| 79 const scoped_refptr<SSLSocketParams>& params, |
| 80 const base::TimeDelta& timeout_duration, |
| 81 const scoped_refptr<TCPClientSocketPool>& tcp_pool, |
| 82 const scoped_refptr<HttpProxyClientSocketPool>& http_proxy_pool, |
| 83 const scoped_refptr<SOCKSClientSocketPool>& socks_pool, |
| 84 ClientSocketFactory* client_socket_factory, |
| 85 const scoped_refptr<HostResolver>& host_resolver, |
| 86 Delegate* delegate, |
| 87 NetLog* net_log); |
| 88 virtual ~SSLConnectJob(); |
| 89 |
| 90 // ConnectJob methods. |
| 91 virtual LoadState GetLoadState() const; |
| 92 |
| 93 virtual void GetAdditionalErrorState(ClientSocketHandle * handle); |
| 94 |
| 95 private: |
| 96 enum State { |
| 97 STATE_TCP_CONNECT, |
| 98 STATE_TCP_CONNECT_COMPLETE, |
| 99 STATE_SOCKS_CONNECT, |
| 100 STATE_SOCKS_CONNECT_COMPLETE, |
| 101 STATE_TUNNEL_CONNECT, |
| 102 STATE_TUNNEL_CONNECT_COMPLETE, |
| 103 STATE_SSL_CONNECT, |
| 104 STATE_SSL_CONNECT_COMPLETE, |
| 105 STATE_NONE, |
| 106 }; |
| 107 |
| 108 // Starts the SSL connection process. Returns OK on success and |
| 109 // ERR_IO_PENDING if it cannot immediately service the request. |
| 110 // Otherwise, it returns a net error code. |
| 111 virtual int ConnectInternal(); |
| 112 |
| 113 void DetermineFirstState(); |
| 114 |
| 115 void OnIOComplete(int result); |
| 116 |
| 117 // Runs the state transition loop. |
| 118 int DoLoop(int result); |
| 119 |
| 120 int DoTCPConnect(); |
| 121 int DoTCPConnectComplete(int result); |
| 122 int DoSOCKSConnect(); |
| 123 int DoSOCKSConnectComplete(int result); |
| 124 int DoTunnelConnect(); |
| 125 int DoTunnelConnectComplete(int result); |
| 126 int DoSSLConnect(); |
| 127 int DoSSLConnectComplete(int result); |
| 128 |
| 129 scoped_refptr<SSLSocketParams> params_; |
| 130 const scoped_refptr<TCPClientSocketPool> tcp_pool_; |
| 131 const scoped_refptr<HttpProxyClientSocketPool> http_proxy_pool_; |
| 132 const scoped_refptr<SOCKSClientSocketPool> socks_pool_; |
| 133 ClientSocketFactory* const client_socket_factory_; |
| 134 const scoped_refptr<HostResolver> resolver_; |
| 135 |
| 136 State next_state_; |
| 137 CompletionCallbackImpl<SSLConnectJob> callback_; |
| 138 scoped_ptr<ClientSocketHandle> transport_socket_handle_; |
| 139 scoped_ptr<SSLClientSocket> ssl_socket_; |
| 140 |
| 141 // The time the DoSSLConnect() method was called. |
| 142 base::TimeTicks ssl_connect_start_time_; |
| 143 |
| 144 scoped_refptr<HttpResponseHeaders> http_auth_response_headers_; |
| 145 scoped_refptr<AuthChallengeInfo> http_auth_auth_challenge_; |
| 146 |
| 147 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); |
| 148 }; |
| 149 |
| 150 class SSLClientSocketPool : public ClientSocketPool { |
| 151 public: |
| 152 // Only the pools that will be used are required. i.e. if you never |
| 153 // try to create an SSL over SOCKS socket, |socks_pool| may be NULL. |
| 154 SSLClientSocketPool( |
| 155 int max_sockets, |
| 156 int max_sockets_per_group, |
| 157 const scoped_refptr<ClientSocketPoolHistograms>& histograms, |
| 158 const scoped_refptr<HostResolver>& host_resolver, |
| 159 ClientSocketFactory* client_socket_factory, |
| 160 const scoped_refptr<TCPClientSocketPool>& tcp_pool, |
| 161 const scoped_refptr<HttpProxyClientSocketPool>& http_proxy_pool, |
| 162 const scoped_refptr<SOCKSClientSocketPool>& socks_pool, |
| 163 NetLog* net_log); |
| 164 |
| 165 // ClientSocketPool methods: |
| 166 virtual int RequestSocket(const std::string& group_name, |
| 167 const void* connect_params, |
| 168 RequestPriority priority, |
| 169 ClientSocketHandle* handle, |
| 170 CompletionCallback* callback, |
| 171 const BoundNetLog& net_log); |
| 172 |
| 173 virtual void CancelRequest(const std::string& group_name, |
| 174 const ClientSocketHandle* handle); |
| 175 |
| 176 virtual void ReleaseSocket(const std::string& group_name, |
| 177 ClientSocket* socket, |
| 178 int id); |
| 179 |
| 180 virtual void Flush(); |
| 181 |
| 182 virtual void CloseIdleSockets(); |
| 183 |
| 184 virtual int IdleSocketCount() const { |
| 185 return base_.idle_socket_count(); |
| 186 } |
| 187 |
| 188 virtual int IdleSocketCountInGroup(const std::string& group_name) const; |
| 189 |
| 190 virtual LoadState GetLoadState(const std::string& group_name, |
| 191 const ClientSocketHandle* handle) const; |
| 192 |
| 193 virtual base::TimeDelta ConnectionTimeout() const { |
| 194 return base_.ConnectionTimeout(); |
| 195 } |
| 196 |
| 197 virtual scoped_refptr<ClientSocketPoolHistograms> histograms() const { |
| 198 return base_.histograms(); |
| 199 }; |
| 200 |
| 201 protected: |
| 202 virtual ~SSLClientSocketPool(); |
| 203 |
| 204 private: |
| 205 typedef ClientSocketPoolBase<SSLSocketParams> PoolBase; |
| 206 |
| 207 class SSLConnectJobFactory : public PoolBase::ConnectJobFactory { |
| 208 public: |
| 209 SSLConnectJobFactory( |
| 210 const scoped_refptr<TCPClientSocketPool>& tcp_pool, |
| 211 const scoped_refptr<HttpProxyClientSocketPool>& http_proxy_pool, |
| 212 const scoped_refptr<SOCKSClientSocketPool>& socks_pool, |
| 213 ClientSocketFactory* client_socket_factory, |
| 214 HostResolver* host_resolver, |
| 215 NetLog* net_log); |
| 216 |
| 217 virtual ~SSLConnectJobFactory() {} |
| 218 |
| 219 // ClientSocketPoolBase::ConnectJobFactory methods. |
| 220 virtual ConnectJob* NewConnectJob( |
| 221 const std::string& group_name, |
| 222 const PoolBase::Request& request, |
| 223 ConnectJob::Delegate* delegate) const; |
| 224 |
| 225 virtual base::TimeDelta ConnectionTimeout() const { return timeout_; } |
| 226 |
| 227 private: |
| 228 const scoped_refptr<TCPClientSocketPool> tcp_pool_; |
| 229 const scoped_refptr<HttpProxyClientSocketPool> http_proxy_pool_; |
| 230 const scoped_refptr<SOCKSClientSocketPool> socks_pool_; |
| 231 ClientSocketFactory* const client_socket_factory_; |
| 232 const scoped_refptr<HostResolver> host_resolver_; |
| 233 base::TimeDelta timeout_; |
| 234 NetLog* net_log_; |
| 235 |
| 236 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); |
| 237 }; |
| 238 |
| 239 PoolBase base_; |
| 240 |
| 241 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); |
| 242 }; |
| 243 |
| 244 REGISTER_SOCKET_PARAMS_FOR_POOL(SSLClientSocketPool, SSLSocketParams); |
| 245 |
| 246 } // namespace net |
| 247 |
| 248 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ |
OLD | NEW |