Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ | 5 #ifndef NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ |
| 6 #define NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ | 6 #define NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 8 #include <string> | 9 #include <string> |
| 10 #include <vector> | |
| 9 | 11 |
| 10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 13 #include "net/base/privacy_mode.h" | 15 #include "net/base/privacy_mode.h" |
| 14 #include "net/dns/host_resolver.h" | 16 #include "net/dns/host_resolver.h" |
| 15 #include "net/http/http_response_info.h" | 17 #include "net/http/http_response_info.h" |
| 16 #include "net/socket/client_socket_pool.h" | 18 #include "net/socket/client_socket_pool.h" |
| 17 #include "net/socket/client_socket_pool_base.h" | 19 #include "net/socket/client_socket_pool_base.h" |
| 18 #include "net/socket/client_socket_pool_histograms.h" | 20 #include "net/socket/client_socket_pool_histograms.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 const SSLConfig ssl_config_; | 89 const SSLConfig ssl_config_; |
| 88 const PrivacyMode privacy_mode_; | 90 const PrivacyMode privacy_mode_; |
| 89 const int load_flags_; | 91 const int load_flags_; |
| 90 const bool force_spdy_over_ssl_; | 92 const bool force_spdy_over_ssl_; |
| 91 const bool want_spdy_over_npn_; | 93 const bool want_spdy_over_npn_; |
| 92 bool ignore_limits_; | 94 bool ignore_limits_; |
| 93 | 95 |
| 94 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams); | 96 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams); |
| 95 }; | 97 }; |
| 96 | 98 |
| 99 // SSLConnectJobMessenger handles communication between concurrent | |
| 100 // SSLConnectJobs that share the same SSL session cache key. | |
| 101 // | |
| 102 // SSLConnectJobMessengers tell the session cache when a certain | |
| 103 // connection should be monitored for success or failure, and | |
| 104 // tell SSLConnectJobs when to pause or resume their connections. | |
| 105 class SSLConnectJobMessenger { | |
| 106 public: | |
| 107 struct SocketAndCallback { | |
| 108 SocketAndCallback(SSLClientSocket* ssl_socket, | |
| 109 const base::Closure& job_resumption_callback); | |
| 110 ~SocketAndCallback(); | |
| 111 | |
| 112 SSLClientSocket* socket; | |
| 113 base::Closure callback; | |
| 114 }; | |
| 115 | |
| 116 typedef std::vector<SocketAndCallback> SSLPendingSocketsAndCallbacks; | |
| 117 | |
| 118 SSLConnectJobMessenger(); | |
| 119 ~SSLConnectJobMessenger(); | |
| 120 | |
| 121 // Removes |socket| from the set of sockets being monitored. This | |
| 122 // guarantees that |job_resumption_callback| will not be called for | |
| 123 // the socket. | |
| 124 void RemovePendingSocket(SSLClientSocket* ssl_socket); | |
| 125 | |
| 126 // Returns true if |ssl_socket|'s Connect() method should be called. | |
| 127 bool CanProceed(SSLClientSocket* ssl_socket); | |
| 128 | |
| 129 // Configures the SSLConnectJobMessenger to begin monitoring |ssl_socket|'s | |
| 130 // connection status. After a successful connection, or an error, | |
| 131 // the messenger will determine which sockets that have been added | |
| 132 // via AddPendingSocket() to allow to proceed. | |
| 133 void MonitorConnectionResult(SSLClientSocket* ssl_socket); | |
| 134 | |
| 135 // Adds |socket| to the list of sockets waiting to Connect(). When | |
| 136 // the messenger has determined that it's an appropriate time for |socket| | |
| 137 // to connect, it will asynchronously invoke |callback|. | |
| 138 // | |
| 139 // Note: It is an error to call AddPendingSocket() without having first | |
| 140 // called MonitorConnectionResult() and configuring a socket that WILL | |
| 141 // have Connect() called on it. | |
| 142 void AddPendingSocket(SSLClientSocket* ssl_socket, | |
| 143 const base::Closure& callback); | |
| 144 | |
| 145 private: | |
| 146 // Processes pending callbacks when a socket completes its SSL Handshake -- | |
|
Ryan Sleevi
2014/07/29 23:19:22
s/Handshake/handshake
mshelley
2014/07/30 18:33:54
Done.
| |
| 147 // either successfully or unsuccessfully. | |
| 148 void OnSSLHandshakeCompleted(); | |
| 149 | |
| 150 // Runs all callbacks stored in |pending_sockets_and_callbacks_|. | |
| 151 void RunAllCallbacks( | |
| 152 const SSLPendingSocketsAndCallbacks& pending_socket_and_callbacks); | |
| 153 | |
| 154 SSLPendingSocketsAndCallbacks pending_sockets_and_callbacks_; | |
| 155 // Note: this field is a vector to allow for future design changes. Currently, | |
| 156 // this vector should only ever have one entry. | |
| 157 std::vector<SSLClientSocket*> connecting_sockets_; | |
| 158 }; | |
| 159 | |
| 97 // SSLConnectJob handles the SSL handshake after setting up the underlying | 160 // SSLConnectJob handles the SSL handshake after setting up the underlying |
| 98 // connection as specified in the params. | 161 // connection as specified in the params. |
| 99 class SSLConnectJob : public ConnectJob { | 162 class SSLConnectJob : public ConnectJob { |
| 100 public: | 163 public: |
| 101 SSLConnectJob( | 164 // Note: the SSLConnectJob does not own |messenger| so it must outlive the |
| 102 const std::string& group_name, | 165 // job. |
| 103 RequestPriority priority, | 166 SSLConnectJob(const std::string& group_name, |
| 104 const scoped_refptr<SSLSocketParams>& params, | 167 RequestPriority priority, |
| 105 const base::TimeDelta& timeout_duration, | 168 const scoped_refptr<SSLSocketParams>& params, |
| 106 TransportClientSocketPool* transport_pool, | 169 const base::TimeDelta& timeout_duration, |
| 107 SOCKSClientSocketPool* socks_pool, | 170 TransportClientSocketPool* transport_pool, |
| 108 HttpProxyClientSocketPool* http_proxy_pool, | 171 SOCKSClientSocketPool* socks_pool, |
| 109 ClientSocketFactory* client_socket_factory, | 172 HttpProxyClientSocketPool* http_proxy_pool, |
| 110 HostResolver* host_resolver, | 173 ClientSocketFactory* client_socket_factory, |
| 111 const SSLClientSocketContext& context, | 174 HostResolver* host_resolver, |
| 112 Delegate* delegate, | 175 const SSLClientSocketContext& context, |
| 113 NetLog* net_log); | 176 SSLConnectJobMessenger* messenger, |
| 177 Delegate* delegate, | |
| 178 NetLog* net_log); | |
| 114 virtual ~SSLConnectJob(); | 179 virtual ~SSLConnectJob(); |
| 115 | 180 |
| 116 // ConnectJob methods. | 181 // ConnectJob methods. |
| 117 virtual LoadState GetLoadState() const OVERRIDE; | 182 virtual LoadState GetLoadState() const OVERRIDE; |
| 118 | 183 |
| 119 virtual void GetAdditionalErrorState(ClientSocketHandle * handle) OVERRIDE; | 184 virtual void GetAdditionalErrorState(ClientSocketHandle * handle) OVERRIDE; |
| 120 | 185 |
| 121 private: | 186 private: |
| 122 enum State { | 187 enum State { |
| 123 STATE_TRANSPORT_CONNECT, | 188 STATE_TRANSPORT_CONNECT, |
| 124 STATE_TRANSPORT_CONNECT_COMPLETE, | 189 STATE_TRANSPORT_CONNECT_COMPLETE, |
| 125 STATE_SOCKS_CONNECT, | 190 STATE_SOCKS_CONNECT, |
| 126 STATE_SOCKS_CONNECT_COMPLETE, | 191 STATE_SOCKS_CONNECT_COMPLETE, |
| 127 STATE_TUNNEL_CONNECT, | 192 STATE_TUNNEL_CONNECT, |
| 128 STATE_TUNNEL_CONNECT_COMPLETE, | 193 STATE_TUNNEL_CONNECT_COMPLETE, |
| 194 STATE_CREATE_SSL_SOCKET, | |
| 195 STATE_CHECK_FOR_RESUME, | |
| 129 STATE_SSL_CONNECT, | 196 STATE_SSL_CONNECT, |
| 130 STATE_SSL_CONNECT_COMPLETE, | 197 STATE_SSL_CONNECT_COMPLETE, |
| 131 STATE_NONE, | 198 STATE_NONE, |
| 132 }; | 199 }; |
| 133 | 200 |
| 134 void OnIOComplete(int result); | 201 void OnIOComplete(int result); |
| 135 | 202 |
| 136 // Runs the state transition loop. | 203 // Runs the state transition loop. |
| 137 int DoLoop(int result); | 204 int DoLoop(int result); |
| 138 | 205 |
| 139 int DoTransportConnect(); | 206 int DoTransportConnect(); |
| 140 int DoTransportConnectComplete(int result); | 207 int DoTransportConnectComplete(int result); |
| 141 int DoSOCKSConnect(); | 208 int DoSOCKSConnect(); |
| 142 int DoSOCKSConnectComplete(int result); | 209 int DoSOCKSConnectComplete(int result); |
| 143 int DoTunnelConnect(); | 210 int DoTunnelConnect(); |
| 144 int DoTunnelConnectComplete(int result); | 211 int DoTunnelConnectComplete(int result); |
| 212 int DoCreateSSLSocket(); | |
| 213 int DoCheckForResume(); | |
| 145 int DoSSLConnect(); | 214 int DoSSLConnect(); |
| 146 int DoSSLConnectComplete(int result); | 215 int DoSSLConnectComplete(int result); |
| 147 | 216 |
| 217 // Tells a waiting SSLConnectJob to resume its SSL connection. | |
| 218 void ResumeSSLConnection(); | |
| 219 | |
| 148 // Returns the initial state for the state machine based on the | 220 // Returns the initial state for the state machine based on the |
| 149 // |connection_type|. | 221 // |connection_type|. |
| 150 static State GetInitialState(SSLSocketParams::ConnectionType connection_type); | 222 static State GetInitialState(SSLSocketParams::ConnectionType connection_type); |
| 151 | 223 |
| 152 // Starts the SSL connection process. Returns OK on success and | 224 // Starts the SSL connection process. Returns OK on success and |
| 153 // ERR_IO_PENDING if it cannot immediately service the request. | 225 // ERR_IO_PENDING if it cannot immediately service the request. |
| 154 // Otherwise, it returns a net error code. | 226 // Otherwise, it returns a net error code. |
| 155 virtual int ConnectInternal() OVERRIDE; | 227 virtual int ConnectInternal() OVERRIDE; |
| 156 | 228 |
| 157 scoped_refptr<SSLSocketParams> params_; | 229 scoped_refptr<SSLSocketParams> params_; |
| 158 TransportClientSocketPool* const transport_pool_; | 230 TransportClientSocketPool* const transport_pool_; |
| 159 SOCKSClientSocketPool* const socks_pool_; | 231 SOCKSClientSocketPool* const socks_pool_; |
| 160 HttpProxyClientSocketPool* const http_proxy_pool_; | 232 HttpProxyClientSocketPool* const http_proxy_pool_; |
| 161 ClientSocketFactory* const client_socket_factory_; | 233 ClientSocketFactory* const client_socket_factory_; |
| 162 HostResolver* const host_resolver_; | 234 HostResolver* const host_resolver_; |
| 163 | 235 |
| 164 const SSLClientSocketContext context_; | 236 const SSLClientSocketContext context_; |
| 165 | 237 |
| 166 State next_state_; | 238 State next_state_; |
| 167 CompletionCallback callback_; | 239 CompletionCallback io_callback_; |
| 168 scoped_ptr<ClientSocketHandle> transport_socket_handle_; | 240 scoped_ptr<ClientSocketHandle> transport_socket_handle_; |
| 169 scoped_ptr<SSLClientSocket> ssl_socket_; | 241 scoped_ptr<SSLClientSocket> ssl_socket_; |
| 170 | 242 |
| 243 SSLConnectJobMessenger* messenger_; | |
| 171 HttpResponseInfo error_response_info_; | 244 HttpResponseInfo error_response_info_; |
| 172 | 245 |
| 246 base::WeakPtrFactory<SSLConnectJob> weak_factory_; | |
| 247 | |
| 173 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); | 248 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); |
| 174 }; | 249 }; |
| 175 | 250 |
| 176 class NET_EXPORT_PRIVATE SSLClientSocketPool | 251 class NET_EXPORT_PRIVATE SSLClientSocketPool |
| 177 : public ClientSocketPool, | 252 : public ClientSocketPool, |
| 178 public HigherLayeredPool, | 253 public HigherLayeredPool, |
| 179 public SSLConfigService::Observer { | 254 public SSLConfigService::Observer { |
| 180 public: | 255 public: |
| 181 typedef SSLSocketParams SocketParams; | 256 typedef SSLSocketParams SocketParams; |
| 182 | 257 |
| 183 // Only the pools that will be used are required. i.e. if you never | 258 // Only the pools that will be used are required. i.e. if you never |
| 184 // try to create an SSL over SOCKS socket, |socks_pool| may be NULL. | 259 // try to create an SSL over SOCKS socket, |socks_pool| may be NULL. |
| 185 SSLClientSocketPool( | 260 SSLClientSocketPool(int max_sockets, |
| 186 int max_sockets, | 261 int max_sockets_per_group, |
| 187 int max_sockets_per_group, | 262 ClientSocketPoolHistograms* histograms, |
| 188 ClientSocketPoolHistograms* histograms, | 263 HostResolver* host_resolver, |
| 189 HostResolver* host_resolver, | 264 CertVerifier* cert_verifier, |
| 190 CertVerifier* cert_verifier, | 265 ServerBoundCertService* server_bound_cert_service, |
| 191 ServerBoundCertService* server_bound_cert_service, | 266 TransportSecurityState* transport_security_state, |
| 192 TransportSecurityState* transport_security_state, | 267 CTVerifier* cert_transparency_verifier, |
| 193 CTVerifier* cert_transparency_verifier, | 268 const std::string& ssl_session_cache_shard, |
| 194 const std::string& ssl_session_cache_shard, | 269 ClientSocketFactory* client_socket_factory, |
| 195 ClientSocketFactory* client_socket_factory, | 270 TransportClientSocketPool* transport_pool, |
| 196 TransportClientSocketPool* transport_pool, | 271 SOCKSClientSocketPool* socks_pool, |
| 197 SOCKSClientSocketPool* socks_pool, | 272 HttpProxyClientSocketPool* http_proxy_pool, |
| 198 HttpProxyClientSocketPool* http_proxy_pool, | 273 SSLConfigService* ssl_config_service, |
| 199 SSLConfigService* ssl_config_service, | 274 bool enable_ssl_connect_job_waiting, |
| 200 NetLog* net_log); | 275 NetLog* net_log); |
| 201 | 276 |
| 202 virtual ~SSLClientSocketPool(); | 277 virtual ~SSLClientSocketPool(); |
| 203 | 278 |
| 204 // ClientSocketPool implementation. | 279 // ClientSocketPool implementation. |
| 205 virtual int RequestSocket(const std::string& group_name, | 280 virtual int RequestSocket(const std::string& group_name, |
| 206 const void* connect_params, | 281 const void* connect_params, |
| 207 RequestPriority priority, | 282 RequestPriority priority, |
| 208 ClientSocketHandle* handle, | 283 ClientSocketHandle* handle, |
| 209 const CompletionCallback& callback, | 284 const CompletionCallback& callback, |
| 210 const BoundNetLog& net_log) OVERRIDE; | 285 const BoundNetLog& net_log) OVERRIDE; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 typedef ClientSocketPoolBase<SSLSocketParams> PoolBase; | 332 typedef ClientSocketPoolBase<SSLSocketParams> PoolBase; |
| 258 | 333 |
| 259 // SSLConfigService::Observer implementation. | 334 // SSLConfigService::Observer implementation. |
| 260 | 335 |
| 261 // When the user changes the SSL config, we flush all idle sockets so they | 336 // When the user changes the SSL config, we flush all idle sockets so they |
| 262 // won't get re-used. | 337 // won't get re-used. |
| 263 virtual void OnSSLConfigChanged() OVERRIDE; | 338 virtual void OnSSLConfigChanged() OVERRIDE; |
| 264 | 339 |
| 265 class SSLConnectJobFactory : public PoolBase::ConnectJobFactory { | 340 class SSLConnectJobFactory : public PoolBase::ConnectJobFactory { |
| 266 public: | 341 public: |
| 267 SSLConnectJobFactory( | 342 SSLConnectJobFactory(TransportClientSocketPool* transport_pool, |
| 268 TransportClientSocketPool* transport_pool, | 343 SOCKSClientSocketPool* socks_pool, |
| 269 SOCKSClientSocketPool* socks_pool, | 344 HttpProxyClientSocketPool* http_proxy_pool, |
| 270 HttpProxyClientSocketPool* http_proxy_pool, | 345 ClientSocketFactory* client_socket_factory, |
| 271 ClientSocketFactory* client_socket_factory, | 346 HostResolver* host_resolver, |
| 272 HostResolver* host_resolver, | 347 const SSLClientSocketContext& context, |
| 273 const SSLClientSocketContext& context, | 348 bool enable_ssl_connect_job_waiting, |
| 274 NetLog* net_log); | 349 NetLog* net_log); |
| 275 | 350 |
| 276 virtual ~SSLConnectJobFactory() {} | 351 virtual ~SSLConnectJobFactory(); |
| 277 | 352 |
| 278 // ClientSocketPoolBase::ConnectJobFactory methods. | 353 // ClientSocketPoolBase::ConnectJobFactory methods. |
| 279 virtual scoped_ptr<ConnectJob> NewConnectJob( | 354 virtual scoped_ptr<ConnectJob> NewConnectJob( |
| 280 const std::string& group_name, | 355 const std::string& group_name, |
| 281 const PoolBase::Request& request, | 356 const PoolBase::Request& request, |
| 282 ConnectJob::Delegate* delegate) const OVERRIDE; | 357 ConnectJob::Delegate* delegate) const OVERRIDE; |
| 283 | 358 |
| 284 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE; | 359 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE; |
| 285 | 360 |
| 286 private: | 361 private: |
| 362 // Maps SSLConnectJob cache keys to SSLConnectJobMessenger objects. | |
| 363 typedef std::map<std::string, SSLConnectJobMessenger*> MessengerMap; | |
| 364 | |
| 287 TransportClientSocketPool* const transport_pool_; | 365 TransportClientSocketPool* const transport_pool_; |
| 288 SOCKSClientSocketPool* const socks_pool_; | 366 SOCKSClientSocketPool* const socks_pool_; |
| 289 HttpProxyClientSocketPool* const http_proxy_pool_; | 367 HttpProxyClientSocketPool* const http_proxy_pool_; |
| 290 ClientSocketFactory* const client_socket_factory_; | 368 ClientSocketFactory* const client_socket_factory_; |
| 291 HostResolver* const host_resolver_; | 369 HostResolver* const host_resolver_; |
| 292 const SSLClientSocketContext context_; | 370 const SSLClientSocketContext context_; |
| 293 base::TimeDelta timeout_; | 371 base::TimeDelta timeout_; |
| 372 bool enable_ssl_connect_job_waiting_; | |
| 294 NetLog* net_log_; | 373 NetLog* net_log_; |
| 374 // |messenger_map_| is currently a pointer so that an element can be | |
| 375 // added to it inside of the const method NewConnectJob. In the future, | |
| 376 // elements will be added in a different method. | |
| 377 // TODO(mshelley) Change this to a non-pointer. | |
| 378 scoped_ptr<MessengerMap> messenger_map_; | |
| 295 | 379 |
| 296 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); | 380 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); |
| 297 }; | 381 }; |
| 298 | 382 |
| 299 TransportClientSocketPool* const transport_pool_; | 383 TransportClientSocketPool* const transport_pool_; |
| 300 SOCKSClientSocketPool* const socks_pool_; | 384 SOCKSClientSocketPool* const socks_pool_; |
| 301 HttpProxyClientSocketPool* const http_proxy_pool_; | 385 HttpProxyClientSocketPool* const http_proxy_pool_; |
| 302 PoolBase base_; | 386 PoolBase base_; |
| 303 const scoped_refptr<SSLConfigService> ssl_config_service_; | 387 const scoped_refptr<SSLConfigService> ssl_config_service_; |
| 304 | 388 |
| 305 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); | 389 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); |
| 306 }; | 390 }; |
| 307 | 391 |
| 308 } // namespace net | 392 } // namespace net |
| 309 | 393 |
| 310 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ | 394 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ |
| OLD | NEW |