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 thier connections. | |
105 class NET_EXPORT_PRIVATE SSLConnectJobMessenger { | |
106 public: | |
107 struct SocketAndCallback { | |
108 SocketAndCallback(SSLClientSocket* ssl_socket, | |
109 base::Closure job_resumption_callback) | |
110 : socket(ssl_socket), callback(job_resumption_callback) {} | |
111 | |
112 SSLClientSocket* socket; | |
113 base::Closure callback; | |
114 }; | |
115 | |
116 typedef std::vector<SocketAndCallback> SSLPendingSocketsAndCallbacks; | |
117 | |
118 // Removes |socket| from |connecting_sockets_| or | |
119 // |pending_sockets_and_callbacks_| | |
120 // if it is present in either. | |
wtc
2014/07/15 19:28:00
Nit: reformat this comment block.
mshelley
2014/07/17 00:28:46
Done.
| |
121 void RemoveSocket(SSLClientSocket* ssl_socket); | |
122 | |
123 // Returns true if |ssl_socket|'s Connect() method should be called. | |
124 bool CanProceed(SSLClientSocket* ssl_socket); | |
125 | |
126 // Configure the SSLConnectJobMessenger to begin monitoring |ssl_socket|'s | |
127 // connection status. After a successful connection, or an error, | |
128 // the messenger will determine which sockets that have been added | |
129 // via AddPendingSocket() to allow to proceed, notifying the associated | |
130 // callback asynchronously, so that the caller can call Connect() on them. | |
131 void MonitorConnectionResult(SSLClientSocket* ssl_socket); | |
132 | |
133 // Adds |socket| to the list of sockets waiting to Connect(). When | |
134 // the messenger has determined that it's an appropriate time for |socket| | |
135 // to connect, it will asynchronously invoke |callback|. Callers should | |
136 // then proceed to call Connect() and establish the socket. | |
137 // | |
138 // Note: It is an error to call AddPendingSocket() without having first | |
139 // called MonitorConnectionResult() and configuring a socket that WILL | |
140 // have Connect() called on it. | |
141 void AddPendingSocket(SSLClientSocket* socket, const base::Closure& callback); | |
142 | |
143 private: | |
144 // Processes pending callbacks when a socket successfully completes | |
145 // its connection. | |
146 void OnJobSucceeded(); | |
147 | |
148 // Processes pending callbacks when a socket encounters an error | |
149 // while completing its connection. | |
150 void OnJobFailed(); | |
151 | |
152 // Runs all callbacks stored in |pending_sockets_and_callbacks_|. | |
153 void RunAllCallbacks( | |
154 const SSLPendingSocketsAndCallbacks& pending_socket_and_callbacks); | |
155 | |
156 SSLPendingSocketsAndCallbacks pending_sockets_and_callbacks_; | |
157 // Note: this field is a vector to allow for future design changes. Currently, | |
158 // this vector should only ever have one entry. | |
159 std::vector<SSLClientSocket*> connecting_sockets_; | |
160 }; | |
161 | |
97 // SSLConnectJob handles the SSL handshake after setting up the underlying | 162 // SSLConnectJob handles the SSL handshake after setting up the underlying |
98 // connection as specified in the params. | 163 // connection as specified in the params. |
99 class SSLConnectJob : public ConnectJob { | 164 class SSLConnectJob : public ConnectJob { |
100 public: | 165 public: |
101 SSLConnectJob( | 166 // Note: the SSLConnectJob does not own |messenger|. |
102 const std::string& group_name, | 167 // so it must outlive the job. |
wtc
2014/07/15 19:28:00
Nit: reformat this comment block.
mshelley
2014/07/17 00:28:45
Done.
| |
103 RequestPriority priority, | 168 SSLConnectJob(const std::string& group_name, |
104 const scoped_refptr<SSLSocketParams>& params, | 169 RequestPriority priority, |
105 const base::TimeDelta& timeout_duration, | 170 const scoped_refptr<SSLSocketParams>& params, |
106 TransportClientSocketPool* transport_pool, | 171 const base::TimeDelta& timeout_duration, |
107 SOCKSClientSocketPool* socks_pool, | 172 TransportClientSocketPool* transport_pool, |
108 HttpProxyClientSocketPool* http_proxy_pool, | 173 SOCKSClientSocketPool* socks_pool, |
109 ClientSocketFactory* client_socket_factory, | 174 HttpProxyClientSocketPool* http_proxy_pool, |
110 HostResolver* host_resolver, | 175 ClientSocketFactory* client_socket_factory, |
111 const SSLClientSocketContext& context, | 176 HostResolver* host_resolver, |
112 Delegate* delegate, | 177 const SSLClientSocketContext& context, |
113 NetLog* net_log); | 178 SSLConnectJobMessenger* messenger, |
179 Delegate* delegate, | |
180 NetLog* net_log); | |
114 virtual ~SSLConnectJob(); | 181 virtual ~SSLConnectJob(); |
115 | 182 |
116 // ConnectJob methods. | 183 // ConnectJob methods. |
117 virtual LoadState GetLoadState() const OVERRIDE; | 184 virtual LoadState GetLoadState() const OVERRIDE; |
118 | 185 |
119 virtual void GetAdditionalErrorState(ClientSocketHandle * handle) OVERRIDE; | 186 virtual void GetAdditionalErrorState(ClientSocketHandle * handle) OVERRIDE; |
120 | 187 |
121 private: | 188 private: |
122 enum State { | 189 enum State { |
123 STATE_TRANSPORT_CONNECT, | 190 STATE_TRANSPORT_CONNECT, |
124 STATE_TRANSPORT_CONNECT_COMPLETE, | 191 STATE_TRANSPORT_CONNECT_COMPLETE, |
125 STATE_SOCKS_CONNECT, | 192 STATE_SOCKS_CONNECT, |
126 STATE_SOCKS_CONNECT_COMPLETE, | 193 STATE_SOCKS_CONNECT_COMPLETE, |
127 STATE_TUNNEL_CONNECT, | 194 STATE_TUNNEL_CONNECT, |
128 STATE_TUNNEL_CONNECT_COMPLETE, | 195 STATE_TUNNEL_CONNECT_COMPLETE, |
196 STATE_CREATE_SSL_SOCKET, | |
197 STATE_CHECK_FOR_RESUME, | |
129 STATE_SSL_CONNECT, | 198 STATE_SSL_CONNECT, |
130 STATE_SSL_CONNECT_COMPLETE, | 199 STATE_SSL_CONNECT_COMPLETE, |
131 STATE_NONE, | 200 STATE_NONE, |
132 }; | 201 }; |
133 | 202 |
134 void OnIOComplete(int result); | 203 void OnIOComplete(int result); |
135 | 204 |
136 // Runs the state transition loop. | 205 // Runs the state transition loop. |
137 int DoLoop(int result); | 206 int DoLoop(int result); |
138 | 207 |
139 int DoTransportConnect(); | 208 int DoTransportConnect(); |
140 int DoTransportConnectComplete(int result); | 209 int DoTransportConnectComplete(int result); |
141 int DoSOCKSConnect(); | 210 int DoSOCKSConnect(); |
142 int DoSOCKSConnectComplete(int result); | 211 int DoSOCKSConnectComplete(int result); |
143 int DoTunnelConnect(); | 212 int DoTunnelConnect(); |
144 int DoTunnelConnectComplete(int result); | 213 int DoTunnelConnectComplete(int result); |
214 int DoCreateSSLSocket(); | |
215 int DoCheckForResume(); | |
145 int DoSSLConnect(); | 216 int DoSSLConnect(); |
146 int DoSSLConnectComplete(int result); | 217 int DoSSLConnectComplete(int result); |
147 | 218 |
219 // Tells a waiting SSLConnectJob to resume its SSL connection. | |
220 void ResumeSSLConnection(); | |
221 | |
148 // Returns the initial state for the state machine based on the | 222 // Returns the initial state for the state machine based on the |
149 // |connection_type|. | 223 // |connection_type|. |
150 static State GetInitialState(SSLSocketParams::ConnectionType connection_type); | 224 static State GetInitialState(SSLSocketParams::ConnectionType connection_type); |
151 | 225 |
152 // Starts the SSL connection process. Returns OK on success and | 226 // Starts the SSL connection process. Returns OK on success and |
153 // ERR_IO_PENDING if it cannot immediately service the request. | 227 // ERR_IO_PENDING if it cannot immediately service the request. |
154 // Otherwise, it returns a net error code. | 228 // Otherwise, it returns a net error code. |
155 virtual int ConnectInternal() OVERRIDE; | 229 virtual int ConnectInternal() OVERRIDE; |
156 | 230 |
157 scoped_refptr<SSLSocketParams> params_; | 231 scoped_refptr<SSLSocketParams> params_; |
158 TransportClientSocketPool* const transport_pool_; | 232 TransportClientSocketPool* const transport_pool_; |
159 SOCKSClientSocketPool* const socks_pool_; | 233 SOCKSClientSocketPool* const socks_pool_; |
160 HttpProxyClientSocketPool* const http_proxy_pool_; | 234 HttpProxyClientSocketPool* const http_proxy_pool_; |
161 ClientSocketFactory* const client_socket_factory_; | 235 ClientSocketFactory* const client_socket_factory_; |
162 HostResolver* const host_resolver_; | 236 HostResolver* const host_resolver_; |
163 | 237 |
164 const SSLClientSocketContext context_; | 238 const SSLClientSocketContext context_; |
165 | 239 |
166 State next_state_; | 240 State next_state_; |
167 CompletionCallback callback_; | 241 CompletionCallback io_callback_; |
168 scoped_ptr<ClientSocketHandle> transport_socket_handle_; | 242 scoped_ptr<ClientSocketHandle> transport_socket_handle_; |
169 scoped_ptr<SSLClientSocket> ssl_socket_; | 243 scoped_ptr<SSLClientSocket> ssl_socket_; |
170 | 244 |
245 SSLConnectJobMessenger* messenger_; | |
171 HttpResponseInfo error_response_info_; | 246 HttpResponseInfo error_response_info_; |
172 | 247 |
248 base::WeakPtrFactory<SSLConnectJob> weak_factory_; | |
249 | |
173 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); | 250 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); |
174 }; | 251 }; |
175 | 252 |
176 class NET_EXPORT_PRIVATE SSLClientSocketPool | 253 class NET_EXPORT_PRIVATE SSLClientSocketPool |
177 : public ClientSocketPool, | 254 : public ClientSocketPool, |
178 public HigherLayeredPool, | 255 public HigherLayeredPool, |
179 public SSLConfigService::Observer { | 256 public SSLConfigService::Observer { |
180 public: | 257 public: |
181 typedef SSLSocketParams SocketParams; | 258 typedef SSLSocketParams SocketParams; |
182 | 259 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 // LowerLayeredPool implementation. | 323 // LowerLayeredPool implementation. |
247 virtual bool IsStalled() const OVERRIDE; | 324 virtual bool IsStalled() const OVERRIDE; |
248 | 325 |
249 virtual void AddHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE; | 326 virtual void AddHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE; |
250 | 327 |
251 virtual void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE; | 328 virtual void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE; |
252 | 329 |
253 // HigherLayeredPool implementation. | 330 // HigherLayeredPool implementation. |
254 virtual bool CloseOneIdleConnection() OVERRIDE; | 331 virtual bool CloseOneIdleConnection() OVERRIDE; |
255 | 332 |
333 // Enable SSLConnectJob waiting if |enable| is true. | |
334 static NET_EXPORT void set_enable_connect_job_waiting(bool enable); | |
335 | |
336 static NET_EXPORT bool get_enable_connect_job_waiting(); | |
337 | |
256 private: | 338 private: |
257 typedef ClientSocketPoolBase<SSLSocketParams> PoolBase; | 339 typedef ClientSocketPoolBase<SSLSocketParams> PoolBase; |
258 | 340 |
259 // SSLConfigService::Observer implementation. | 341 // SSLConfigService::Observer implementation. |
260 | 342 |
261 // When the user changes the SSL config, we flush all idle sockets so they | 343 // When the user changes the SSL config, we flush all idle sockets so they |
262 // won't get re-used. | 344 // won't get re-used. |
263 virtual void OnSSLConfigChanged() OVERRIDE; | 345 virtual void OnSSLConfigChanged() OVERRIDE; |
264 | 346 |
265 class SSLConnectJobFactory : public PoolBase::ConnectJobFactory { | 347 class SSLConnectJobFactory : public PoolBase::ConnectJobFactory { |
266 public: | 348 public: |
267 SSLConnectJobFactory( | 349 SSLConnectJobFactory( |
268 TransportClientSocketPool* transport_pool, | 350 TransportClientSocketPool* transport_pool, |
269 SOCKSClientSocketPool* socks_pool, | 351 SOCKSClientSocketPool* socks_pool, |
270 HttpProxyClientSocketPool* http_proxy_pool, | 352 HttpProxyClientSocketPool* http_proxy_pool, |
271 ClientSocketFactory* client_socket_factory, | 353 ClientSocketFactory* client_socket_factory, |
272 HostResolver* host_resolver, | 354 HostResolver* host_resolver, |
273 const SSLClientSocketContext& context, | 355 const SSLClientSocketContext& context, |
274 NetLog* net_log); | 356 NetLog* net_log); |
275 | 357 |
276 virtual ~SSLConnectJobFactory() {} | 358 virtual ~SSLConnectJobFactory() {} |
wtc
2014/07/15 19:28:00
We should delete the SSLConnectJobMessengers in me
mshelley
2014/07/17 00:28:45
Done.
| |
277 | 359 |
278 // ClientSocketPoolBase::ConnectJobFactory methods. | 360 // ClientSocketPoolBase::ConnectJobFactory methods. |
279 virtual scoped_ptr<ConnectJob> NewConnectJob( | 361 virtual scoped_ptr<ConnectJob> NewConnectJob( |
280 const std::string& group_name, | 362 const std::string& group_name, |
281 const PoolBase::Request& request, | 363 const PoolBase::Request& request, |
282 ConnectJob::Delegate* delegate) const OVERRIDE; | 364 ConnectJob::Delegate* delegate) const OVERRIDE; |
283 | 365 |
284 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE; | 366 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE; |
285 | 367 |
286 private: | 368 private: |
369 // Maps SSLConnectJob cache keys to SSLConnectJobMessenger objects. | |
370 typedef std::map<std::string, SSLConnectJobMessenger*> MessengerMap; | |
371 | |
287 TransportClientSocketPool* const transport_pool_; | 372 TransportClientSocketPool* const transport_pool_; |
288 SOCKSClientSocketPool* const socks_pool_; | 373 SOCKSClientSocketPool* const socks_pool_; |
289 HttpProxyClientSocketPool* const http_proxy_pool_; | 374 HttpProxyClientSocketPool* const http_proxy_pool_; |
290 ClientSocketFactory* const client_socket_factory_; | 375 ClientSocketFactory* const client_socket_factory_; |
291 HostResolver* const host_resolver_; | 376 HostResolver* const host_resolver_; |
292 const SSLClientSocketContext context_; | 377 const SSLClientSocketContext context_; |
293 base::TimeDelta timeout_; | 378 base::TimeDelta timeout_; |
294 NetLog* net_log_; | 379 NetLog* net_log_; |
380 scoped_ptr<MessengerMap> messenger_map_; | |
wtc
2014/07/15 19:28:00
Please add a TODO comment that this will be change
mshelley
2014/07/17 00:28:46
Done.
| |
295 | 381 |
296 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); | 382 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); |
297 }; | 383 }; |
298 | 384 |
299 TransportClientSocketPool* const transport_pool_; | 385 TransportClientSocketPool* const transport_pool_; |
300 SOCKSClientSocketPool* const socks_pool_; | 386 SOCKSClientSocketPool* const socks_pool_; |
301 HttpProxyClientSocketPool* const http_proxy_pool_; | 387 HttpProxyClientSocketPool* const http_proxy_pool_; |
302 PoolBase base_; | 388 PoolBase base_; |
303 const scoped_refptr<SSLConfigService> ssl_config_service_; | 389 const scoped_refptr<SSLConfigService> ssl_config_service_; |
304 | 390 |
391 static bool enable_connect_job_waiting_; | |
392 | |
305 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); | 393 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); |
306 }; | 394 }; |
307 | 395 |
308 } // namespace net | 396 } // namespace net |
309 | 397 |
310 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ | 398 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ |
OLD | NEW |