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> | |
9 #include <string> | 8 #include <string> |
10 #include <vector> | |
11 | 9 |
12 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
14 #include "base/time/time.h" | 12 #include "base/time/time.h" |
15 #include "net/base/privacy_mode.h" | 13 #include "net/base/privacy_mode.h" |
16 #include "net/http/http_response_info.h" | 14 #include "net/http/http_response_info.h" |
17 #include "net/socket/client_socket_pool.h" | 15 #include "net/socket/client_socket_pool.h" |
18 #include "net/socket/client_socket_pool_base.h" | 16 #include "net/socket/client_socket_pool_base.h" |
19 #include "net/socket/client_socket_pool_histograms.h" | 17 #include "net/socket/client_socket_pool_histograms.h" |
20 #include "net/socket/ssl_client_socket.h" | 18 #include "net/socket/ssl_client_socket.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 const SSLConfig ssl_config_; | 87 const SSLConfig ssl_config_; |
90 const PrivacyMode privacy_mode_; | 88 const PrivacyMode privacy_mode_; |
91 const int load_flags_; | 89 const int load_flags_; |
92 const bool force_spdy_over_ssl_; | 90 const bool force_spdy_over_ssl_; |
93 const bool want_spdy_over_npn_; | 91 const bool want_spdy_over_npn_; |
94 bool ignore_limits_; | 92 bool ignore_limits_; |
95 | 93 |
96 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams); | 94 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams); |
97 }; | 95 }; |
98 | 96 |
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 // |messenger_finished_callback| is run when a connection monitored by the | |
119 // SSLConnectJobMessenger has completed and we are finished with the | |
120 // SSLConnectJobMessenger. | |
121 explicit SSLConnectJobMessenger( | |
122 const base::Closure& messenger_finished_callback); | |
123 ~SSLConnectJobMessenger(); | |
124 | |
125 // Removes |socket| from the set of sockets being monitored. This | |
126 // guarantees that |job_resumption_callback| will not be called for | |
127 // the socket. | |
128 void RemovePendingSocket(SSLClientSocket* ssl_socket); | |
129 | |
130 // Returns true if |ssl_socket|'s Connect() method should be called. | |
131 bool CanProceed(SSLClientSocket* ssl_socket); | |
132 | |
133 // Configures the SSLConnectJobMessenger to begin monitoring |ssl_socket|'s | |
134 // connection status. After a successful connection, or an error, | |
135 // the messenger will determine which sockets that have been added | |
136 // via AddPendingSocket() to allow to proceed. | |
137 void MonitorConnectionResult(SSLClientSocket* ssl_socket); | |
138 | |
139 // Adds |socket| to the list of sockets waiting to Connect(). When | |
140 // the messenger has determined that it's an appropriate time for |socket| | |
141 // to connect, it will invoke |callback|. | |
142 // | |
143 // Note: It is an error to call AddPendingSocket() without having first | |
144 // called MonitorConnectionResult() and configuring a socket that WILL | |
145 // have Connect() called on it. | |
146 void AddPendingSocket(SSLClientSocket* ssl_socket, | |
147 const base::Closure& callback); | |
148 | |
149 private: | |
150 // Processes pending callbacks when a socket completes its SSL handshake -- | |
151 // either successfully or unsuccessfully. | |
152 void OnSSLHandshakeCompleted(); | |
153 | |
154 // Runs all callbacks stored in |pending_sockets_and_callbacks_|. | |
155 void RunAllCallbacks( | |
156 const SSLPendingSocketsAndCallbacks& pending_socket_and_callbacks); | |
157 | |
158 SSLPendingSocketsAndCallbacks pending_sockets_and_callbacks_; | |
159 // Note: this field is a vector to allow for future design changes. Currently, | |
160 // this vector should only ever have one entry. | |
161 std::vector<SSLClientSocket*> connecting_sockets_; | |
162 | |
163 base::Closure messenger_finished_callback_; | |
164 | |
165 base::WeakPtrFactory<SSLConnectJobMessenger> weak_factory_; | |
166 }; | |
167 | |
168 // SSLConnectJob handles the SSL handshake after setting up the underlying | 97 // SSLConnectJob handles the SSL handshake after setting up the underlying |
169 // connection as specified in the params. | 98 // connection as specified in the params. |
170 class SSLConnectJob : public ConnectJob { | 99 class SSLConnectJob : public ConnectJob { |
171 public: | 100 public: |
172 // Callback to allow the SSLConnectJob to obtain an SSLConnectJobMessenger to | |
173 // coordinate connecting. The SSLConnectJob will supply a unique identifer | |
174 // (ex: the SSL session cache key), with the expectation that the same | |
175 // Messenger will be returned for all such ConnectJobs. | |
176 // | |
177 // Note: It will only be called for situations where the SSL session cache | |
178 // does not already have a candidate session to resume. | |
179 typedef base::Callback<SSLConnectJobMessenger*(const std::string&)> | |
180 GetMessengerCallback; | |
181 | |
182 // Note: the SSLConnectJob does not own |messenger| so it must outlive the | 101 // Note: the SSLConnectJob does not own |messenger| so it must outlive the |
183 // job. | 102 // job. |
184 SSLConnectJob(const std::string& group_name, | 103 SSLConnectJob(const std::string& group_name, |
185 RequestPriority priority, | 104 RequestPriority priority, |
186 const scoped_refptr<SSLSocketParams>& params, | 105 const scoped_refptr<SSLSocketParams>& params, |
187 const base::TimeDelta& timeout_duration, | 106 const base::TimeDelta& timeout_duration, |
188 TransportClientSocketPool* transport_pool, | 107 TransportClientSocketPool* transport_pool, |
189 SOCKSClientSocketPool* socks_pool, | 108 SOCKSClientSocketPool* socks_pool, |
190 HttpProxyClientSocketPool* http_proxy_pool, | 109 HttpProxyClientSocketPool* http_proxy_pool, |
191 ClientSocketFactory* client_socket_factory, | 110 ClientSocketFactory* client_socket_factory, |
192 const SSLClientSocketContext& context, | 111 const SSLClientSocketContext& context, |
193 const GetMessengerCallback& get_messenger_callback, | |
194 Delegate* delegate, | 112 Delegate* delegate, |
195 NetLog* net_log); | 113 NetLog* net_log); |
196 ~SSLConnectJob() override; | 114 ~SSLConnectJob() override; |
197 | 115 |
198 // ConnectJob methods. | 116 // ConnectJob methods. |
199 LoadState GetLoadState() const override; | 117 LoadState GetLoadState() const override; |
200 | 118 |
201 void GetAdditionalErrorState(ClientSocketHandle* handle) override; | 119 void GetAdditionalErrorState(ClientSocketHandle* handle) override; |
202 | 120 |
203 private: | 121 private: |
204 enum State { | 122 enum State { |
205 STATE_TRANSPORT_CONNECT, | 123 STATE_TRANSPORT_CONNECT, |
206 STATE_TRANSPORT_CONNECT_COMPLETE, | 124 STATE_TRANSPORT_CONNECT_COMPLETE, |
207 STATE_SOCKS_CONNECT, | 125 STATE_SOCKS_CONNECT, |
208 STATE_SOCKS_CONNECT_COMPLETE, | 126 STATE_SOCKS_CONNECT_COMPLETE, |
209 STATE_TUNNEL_CONNECT, | 127 STATE_TUNNEL_CONNECT, |
210 STATE_TUNNEL_CONNECT_COMPLETE, | 128 STATE_TUNNEL_CONNECT_COMPLETE, |
211 STATE_CREATE_SSL_SOCKET, | |
212 STATE_CHECK_FOR_RESUME, | |
213 STATE_SSL_CONNECT, | 129 STATE_SSL_CONNECT, |
214 STATE_SSL_CONNECT_COMPLETE, | 130 STATE_SSL_CONNECT_COMPLETE, |
215 STATE_NONE, | 131 STATE_NONE, |
216 }; | 132 }; |
217 | 133 |
218 void OnIOComplete(int result); | 134 void OnIOComplete(int result); |
219 | 135 |
220 // Runs the state transition loop. | 136 // Runs the state transition loop. |
221 int DoLoop(int result); | 137 int DoLoop(int result); |
222 | 138 |
223 int DoTransportConnect(); | 139 int DoTransportConnect(); |
224 int DoTransportConnectComplete(int result); | 140 int DoTransportConnectComplete(int result); |
225 int DoSOCKSConnect(); | 141 int DoSOCKSConnect(); |
226 int DoSOCKSConnectComplete(int result); | 142 int DoSOCKSConnectComplete(int result); |
227 int DoTunnelConnect(); | 143 int DoTunnelConnect(); |
228 int DoTunnelConnectComplete(int result); | 144 int DoTunnelConnectComplete(int result); |
229 int DoCreateSSLSocket(); | |
230 int DoCheckForResume(); | |
231 int DoSSLConnect(); | 145 int DoSSLConnect(); |
232 int DoSSLConnectComplete(int result); | 146 int DoSSLConnectComplete(int result); |
233 | 147 |
234 // Tells a waiting SSLConnectJob to resume its SSL connection. | |
235 void ResumeSSLConnection(); | |
236 | |
237 // Returns the initial state for the state machine based on the | 148 // Returns the initial state for the state machine based on the |
238 // |connection_type|. | 149 // |connection_type|. |
239 static State GetInitialState(SSLSocketParams::ConnectionType connection_type); | 150 static State GetInitialState(SSLSocketParams::ConnectionType connection_type); |
240 | 151 |
241 // Starts the SSL connection process. Returns OK on success and | 152 // Starts the SSL connection process. Returns OK on success and |
242 // ERR_IO_PENDING if it cannot immediately service the request. | 153 // ERR_IO_PENDING if it cannot immediately service the request. |
243 // Otherwise, it returns a net error code. | 154 // Otherwise, it returns a net error code. |
244 int ConnectInternal() override; | 155 int ConnectInternal() override; |
245 | 156 |
246 scoped_refptr<SSLSocketParams> params_; | 157 scoped_refptr<SSLSocketParams> params_; |
247 TransportClientSocketPool* const transport_pool_; | 158 TransportClientSocketPool* const transport_pool_; |
248 SOCKSClientSocketPool* const socks_pool_; | 159 SOCKSClientSocketPool* const socks_pool_; |
249 HttpProxyClientSocketPool* const http_proxy_pool_; | 160 HttpProxyClientSocketPool* const http_proxy_pool_; |
250 ClientSocketFactory* const client_socket_factory_; | 161 ClientSocketFactory* const client_socket_factory_; |
251 | 162 |
252 const SSLClientSocketContext context_; | 163 const SSLClientSocketContext context_; |
253 | 164 |
254 State next_state_; | 165 State next_state_; |
255 CompletionCallback io_callback_; | 166 CompletionCallback callback_; |
davidben
2015/03/09 18:02:36
FYI, this part of the revert is just a rename. (I'
| |
256 scoped_ptr<ClientSocketHandle> transport_socket_handle_; | 167 scoped_ptr<ClientSocketHandle> transport_socket_handle_; |
257 scoped_ptr<SSLClientSocket> ssl_socket_; | 168 scoped_ptr<SSLClientSocket> ssl_socket_; |
258 | 169 |
259 SSLConnectJobMessenger* messenger_; | |
260 HttpResponseInfo error_response_info_; | 170 HttpResponseInfo error_response_info_; |
261 | 171 |
262 GetMessengerCallback get_messenger_callback_; | |
263 | |
264 base::WeakPtrFactory<SSLConnectJob> weak_factory_; | |
265 | |
266 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); | 172 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); |
267 }; | 173 }; |
268 | 174 |
269 class NET_EXPORT_PRIVATE SSLClientSocketPool | 175 class NET_EXPORT_PRIVATE SSLClientSocketPool |
270 : public ClientSocketPool, | 176 : public ClientSocketPool, |
271 public HigherLayeredPool, | 177 public HigherLayeredPool, |
272 public SSLConfigService::Observer { | 178 public SSLConfigService::Observer { |
273 public: | 179 public: |
274 typedef SSLSocketParams SocketParams; | 180 typedef SSLSocketParams SocketParams; |
275 | 181 |
276 // Only the pools that will be used are required. i.e. if you never | 182 // Only the pools that will be used are required. i.e. if you never |
277 // try to create an SSL over SOCKS socket, |socks_pool| may be NULL. | 183 // try to create an SSL over SOCKS socket, |socks_pool| may be NULL. |
278 SSLClientSocketPool(int max_sockets, | 184 SSLClientSocketPool(int max_sockets, |
279 int max_sockets_per_group, | 185 int max_sockets_per_group, |
280 ClientSocketPoolHistograms* histograms, | 186 ClientSocketPoolHistograms* histograms, |
281 CertVerifier* cert_verifier, | 187 CertVerifier* cert_verifier, |
282 ChannelIDService* channel_id_service, | 188 ChannelIDService* channel_id_service, |
283 TransportSecurityState* transport_security_state, | 189 TransportSecurityState* transport_security_state, |
284 CTVerifier* cert_transparency_verifier, | 190 CTVerifier* cert_transparency_verifier, |
285 CertPolicyEnforcer* cert_policy_enforcer, | 191 CertPolicyEnforcer* cert_policy_enforcer, |
286 const std::string& ssl_session_cache_shard, | 192 const std::string& ssl_session_cache_shard, |
287 ClientSocketFactory* client_socket_factory, | 193 ClientSocketFactory* client_socket_factory, |
288 TransportClientSocketPool* transport_pool, | 194 TransportClientSocketPool* transport_pool, |
289 SOCKSClientSocketPool* socks_pool, | 195 SOCKSClientSocketPool* socks_pool, |
290 HttpProxyClientSocketPool* http_proxy_pool, | 196 HttpProxyClientSocketPool* http_proxy_pool, |
291 SSLConfigService* ssl_config_service, | 197 SSLConfigService* ssl_config_service, |
292 bool enable_ssl_connect_job_waiting, | |
293 NetLog* net_log); | 198 NetLog* net_log); |
294 | 199 |
295 ~SSLClientSocketPool() override; | 200 ~SSLClientSocketPool() override; |
296 | 201 |
297 // ClientSocketPool implementation. | 202 // ClientSocketPool implementation. |
298 int RequestSocket(const std::string& group_name, | 203 int RequestSocket(const std::string& group_name, |
299 const void* connect_params, | 204 const void* connect_params, |
300 RequestPriority priority, | 205 RequestPriority priority, |
301 ClientSocketHandle* handle, | 206 ClientSocketHandle* handle, |
302 const CompletionCallback& callback, | 207 const CompletionCallback& callback, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
337 // LowerLayeredPool implementation. | 242 // LowerLayeredPool implementation. |
338 bool IsStalled() const override; | 243 bool IsStalled() const override; |
339 | 244 |
340 void AddHigherLayeredPool(HigherLayeredPool* higher_pool) override; | 245 void AddHigherLayeredPool(HigherLayeredPool* higher_pool) override; |
341 | 246 |
342 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) override; | 247 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) override; |
343 | 248 |
344 // HigherLayeredPool implementation. | 249 // HigherLayeredPool implementation. |
345 bool CloseOneIdleConnection() override; | 250 bool CloseOneIdleConnection() override; |
346 | 251 |
347 // Gets the SSLConnectJobMessenger for the given ssl session |cache_key|. If | |
348 // none exits, it creates one and stores it in |messenger_map_|. | |
349 SSLConnectJobMessenger* GetOrCreateSSLConnectJobMessenger( | |
350 const std::string& cache_key); | |
351 void DeleteSSLConnectJobMessenger(const std::string& cache_key); | |
352 | |
353 private: | 252 private: |
354 typedef ClientSocketPoolBase<SSLSocketParams> PoolBase; | 253 typedef ClientSocketPoolBase<SSLSocketParams> PoolBase; |
355 // Maps SSLConnectJob cache keys to SSLConnectJobMessenger objects. | |
356 typedef std::map<std::string, SSLConnectJobMessenger*> MessengerMap; | |
357 | 254 |
358 // SSLConfigService::Observer implementation. | 255 // SSLConfigService::Observer implementation. |
359 | 256 |
360 // When the user changes the SSL config, we flush all idle sockets so they | 257 // When the user changes the SSL config, we flush all idle sockets so they |
361 // won't get re-used. | 258 // won't get re-used. |
362 void OnSSLConfigChanged() override; | 259 void OnSSLConfigChanged() override; |
363 | 260 |
364 class SSLConnectJobFactory : public PoolBase::ConnectJobFactory { | 261 class SSLConnectJobFactory : public PoolBase::ConnectJobFactory { |
365 public: | 262 public: |
366 SSLConnectJobFactory( | 263 SSLConnectJobFactory( |
367 TransportClientSocketPool* transport_pool, | 264 TransportClientSocketPool* transport_pool, |
368 SOCKSClientSocketPool* socks_pool, | 265 SOCKSClientSocketPool* socks_pool, |
369 HttpProxyClientSocketPool* http_proxy_pool, | 266 HttpProxyClientSocketPool* http_proxy_pool, |
370 ClientSocketFactory* client_socket_factory, | 267 ClientSocketFactory* client_socket_factory, |
371 const SSLClientSocketContext& context, | 268 const SSLClientSocketContext& context, |
372 const SSLConnectJob::GetMessengerCallback& get_messenger_callback, | |
373 NetLog* net_log); | 269 NetLog* net_log); |
374 | 270 |
375 ~SSLConnectJobFactory() override; | 271 ~SSLConnectJobFactory() override; |
376 | 272 |
377 // ClientSocketPoolBase::ConnectJobFactory methods. | 273 // ClientSocketPoolBase::ConnectJobFactory methods. |
378 scoped_ptr<ConnectJob> NewConnectJob( | 274 scoped_ptr<ConnectJob> NewConnectJob( |
379 const std::string& group_name, | 275 const std::string& group_name, |
380 const PoolBase::Request& request, | 276 const PoolBase::Request& request, |
381 ConnectJob::Delegate* delegate) const override; | 277 ConnectJob::Delegate* delegate) const override; |
382 | 278 |
383 base::TimeDelta ConnectionTimeout() const override; | 279 base::TimeDelta ConnectionTimeout() const override; |
384 | 280 |
385 private: | 281 private: |
386 TransportClientSocketPool* const transport_pool_; | 282 TransportClientSocketPool* const transport_pool_; |
387 SOCKSClientSocketPool* const socks_pool_; | 283 SOCKSClientSocketPool* const socks_pool_; |
388 HttpProxyClientSocketPool* const http_proxy_pool_; | 284 HttpProxyClientSocketPool* const http_proxy_pool_; |
389 ClientSocketFactory* const client_socket_factory_; | 285 ClientSocketFactory* const client_socket_factory_; |
390 const SSLClientSocketContext context_; | 286 const SSLClientSocketContext context_; |
391 base::TimeDelta timeout_; | 287 base::TimeDelta timeout_; |
392 SSLConnectJob::GetMessengerCallback get_messenger_callback_; | |
393 NetLog* net_log_; | 288 NetLog* net_log_; |
394 | 289 |
395 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); | 290 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); |
396 }; | 291 }; |
397 | 292 |
398 TransportClientSocketPool* const transport_pool_; | 293 TransportClientSocketPool* const transport_pool_; |
399 SOCKSClientSocketPool* const socks_pool_; | 294 SOCKSClientSocketPool* const socks_pool_; |
400 HttpProxyClientSocketPool* const http_proxy_pool_; | 295 HttpProxyClientSocketPool* const http_proxy_pool_; |
401 PoolBase base_; | 296 PoolBase base_; |
402 const scoped_refptr<SSLConfigService> ssl_config_service_; | 297 const scoped_refptr<SSLConfigService> ssl_config_service_; |
403 MessengerMap messenger_map_; | |
404 bool enable_ssl_connect_job_waiting_; | |
405 | 298 |
406 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); | 299 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); |
407 }; | 300 }; |
408 | 301 |
409 } // namespace net | 302 } // namespace net |
410 | 303 |
411 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ | 304 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ |
OLD | NEW |