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 <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 SocketAndCallback(SSLClientSocket* ssl_socket, | 108 SocketAndCallback(SSLClientSocket* ssl_socket, |
109 const base::Closure& job_resumption_callback); | 109 const base::Closure& job_resumption_callback); |
110 ~SocketAndCallback(); | 110 ~SocketAndCallback(); |
111 | 111 |
112 SSLClientSocket* socket; | 112 SSLClientSocket* socket; |
113 base::Closure callback; | 113 base::Closure callback; |
114 }; | 114 }; |
115 | 115 |
116 typedef std::vector<SocketAndCallback> SSLPendingSocketsAndCallbacks; | 116 typedef std::vector<SocketAndCallback> SSLPendingSocketsAndCallbacks; |
117 | 117 |
118 SSLConnectJobMessenger(); | 118 // |messenger_finished_callback| is used to inform the client socket pool |
119 // that a connection monitored by the SSLConnectJobMessenger has completed. | |
wtc
2014/08/15 18:48:29
Nit: this comment should ideally avoid explicit re
| |
120 SSLConnectJobMessenger(const base::Closure& messenger_finished_callback); | |
119 ~SSLConnectJobMessenger(); | 121 ~SSLConnectJobMessenger(); |
120 | 122 |
121 // Removes |socket| from the set of sockets being monitored. This | 123 // Removes |socket| from the set of sockets being monitored. This |
122 // guarantees that |job_resumption_callback| will not be called for | 124 // guarantees that |job_resumption_callback| will not be called for |
123 // the socket. | 125 // the socket. |
124 void RemovePendingSocket(SSLClientSocket* ssl_socket); | 126 void RemovePendingSocket(SSLClientSocket* ssl_socket); |
125 | 127 |
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 | 128 // Configures the SSLConnectJobMessenger to begin monitoring |ssl_socket|'s |
130 // connection status. After a successful connection, or an error, | 129 // connection status. After a successful connection, or an error, |
131 // the messenger will determine which sockets that have been added | 130 // the messenger will determine which sockets that have been added |
132 // via AddPendingSocket() to allow to proceed. | 131 // via AddPendingSocket() to allow to proceed. |
133 void MonitorConnectionResult(SSLClientSocket* ssl_socket); | 132 void MonitorConnectionResult(SSLClientSocket* ssl_socket); |
134 | 133 |
135 // Adds |socket| to the list of sockets waiting to Connect(). When | 134 // Adds |socket| to the list of sockets waiting to Connect(). When |
136 // the messenger has determined that it's an appropriate time for |socket| | 135 // the messenger has determined that it's an appropriate time for |socket| |
137 // to connect, it will invoke |callback|. | 136 // to connect, it will invoke |callback|. |
138 // | 137 // |
139 // Note: It is an error to call AddPendingSocket() without having first | 138 // Note: It is an error to call AddPendingSocket() without having first |
140 // called MonitorConnectionResult() and configuring a socket that WILL | 139 // called MonitorConnectionResult() and configuring a socket that WILL |
141 // have Connect() called on it. | 140 // have Connect() called on it. |
142 void AddPendingSocket(SSLClientSocket* ssl_socket, | 141 void AddPendingSocket(SSLClientSocket* ssl_socket, |
143 const base::Closure& callback); | 142 const base::Closure& callback); |
144 | 143 |
144 std::vector<SSLClientSocket*> connecting_sockets() { | |
wtc
2014/08/15 18:48:29
IMPORTANT: I didn't realize that the removal of Ca
| |
145 return connecting_sockets_; | |
146 }; | |
147 | |
145 private: | 148 private: |
146 // Processes pending callbacks when a socket completes its SSL handshake -- | 149 // Processes pending callbacks when a socket completes its SSL handshake -- |
147 // either successfully or unsuccessfully. | 150 // either successfully or unsuccessfully. |
148 void OnSSLHandshakeCompleted(); | 151 void OnSSLHandshakeCompleted(); |
149 | 152 |
150 // Runs all callbacks stored in |pending_sockets_and_callbacks_|. | 153 // Runs all callbacks stored in |pending_sockets_and_callbacks_|. |
151 void RunAllCallbacks( | 154 void RunAllCallbacks( |
152 const SSLPendingSocketsAndCallbacks& pending_socket_and_callbacks); | 155 const SSLPendingSocketsAndCallbacks& pending_socket_and_callbacks); |
153 | 156 |
154 base::WeakPtrFactory<SSLConnectJobMessenger> weak_factory_; | |
155 | |
156 SSLPendingSocketsAndCallbacks pending_sockets_and_callbacks_; | 157 SSLPendingSocketsAndCallbacks pending_sockets_and_callbacks_; |
157 // Note: this field is a vector to allow for future design changes. Currently, | 158 // Note: this field is a vector to allow for future design changes. Currently, |
158 // this vector should only ever have one entry. | 159 // this vector should only ever have one entry. |
159 std::vector<SSLClientSocket*> connecting_sockets_; | 160 std::vector<SSLClientSocket*> connecting_sockets_; |
161 | |
162 base::Closure messenger_finished_callback_; | |
163 | |
164 base::WeakPtrFactory<SSLConnectJobMessenger> weak_factory_; | |
160 }; | 165 }; |
161 | 166 |
162 // SSLConnectJob handles the SSL handshake after setting up the underlying | 167 // SSLConnectJob handles the SSL handshake after setting up the underlying |
163 // connection as specified in the params. | 168 // connection as specified in the params. |
164 class SSLConnectJob : public ConnectJob { | 169 class SSLConnectJob : public ConnectJob { |
165 public: | 170 public: |
171 // Callback to allow the SSLConnectJob to obtain an SSLConnectJobMessenger to | |
172 // coordinate connecting. The SSLConnectJob will supply a unique identifer | |
173 // (ex: the SSL session cache key), with the expectation that the same | |
174 // Messenger will be returned for all such ConnectJobs. | |
175 // | |
176 // Note: It will only be called for situations where the SSL session cache | |
177 // does not already have a candidate session to resume. | |
178 typedef base::Callback<SSLConnectJobMessenger*(const std::string&)> | |
179 GetMessengerCallback; | |
180 | |
166 // Note: the SSLConnectJob does not own |messenger| so it must outlive the | 181 // Note: the SSLConnectJob does not own |messenger| so it must outlive the |
167 // job. | 182 // job. |
168 SSLConnectJob(const std::string& group_name, | 183 SSLConnectJob(const std::string& group_name, |
169 RequestPriority priority, | 184 RequestPriority priority, |
170 const scoped_refptr<SSLSocketParams>& params, | 185 const scoped_refptr<SSLSocketParams>& params, |
171 const base::TimeDelta& timeout_duration, | 186 const base::TimeDelta& timeout_duration, |
172 TransportClientSocketPool* transport_pool, | 187 TransportClientSocketPool* transport_pool, |
173 SOCKSClientSocketPool* socks_pool, | 188 SOCKSClientSocketPool* socks_pool, |
174 HttpProxyClientSocketPool* http_proxy_pool, | 189 HttpProxyClientSocketPool* http_proxy_pool, |
175 ClientSocketFactory* client_socket_factory, | 190 ClientSocketFactory* client_socket_factory, |
176 HostResolver* host_resolver, | 191 HostResolver* host_resolver, |
177 const SSLClientSocketContext& context, | 192 const SSLClientSocketContext& context, |
178 SSLConnectJobMessenger* messenger, | 193 const GetMessengerCallback& get_messenger_callback, |
179 Delegate* delegate, | 194 Delegate* delegate, |
180 NetLog* net_log); | 195 NetLog* net_log); |
181 virtual ~SSLConnectJob(); | 196 virtual ~SSLConnectJob(); |
182 | 197 |
183 // ConnectJob methods. | 198 // ConnectJob methods. |
184 virtual LoadState GetLoadState() const OVERRIDE; | 199 virtual LoadState GetLoadState() const OVERRIDE; |
185 | 200 |
186 virtual void GetAdditionalErrorState(ClientSocketHandle * handle) OVERRIDE; | 201 virtual void GetAdditionalErrorState(ClientSocketHandle * handle) OVERRIDE; |
187 | 202 |
188 private: | 203 private: |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 const SSLClientSocketContext context_; | 253 const SSLClientSocketContext context_; |
239 | 254 |
240 State next_state_; | 255 State next_state_; |
241 CompletionCallback io_callback_; | 256 CompletionCallback io_callback_; |
242 scoped_ptr<ClientSocketHandle> transport_socket_handle_; | 257 scoped_ptr<ClientSocketHandle> transport_socket_handle_; |
243 scoped_ptr<SSLClientSocket> ssl_socket_; | 258 scoped_ptr<SSLClientSocket> ssl_socket_; |
244 | 259 |
245 SSLConnectJobMessenger* messenger_; | 260 SSLConnectJobMessenger* messenger_; |
246 HttpResponseInfo error_response_info_; | 261 HttpResponseInfo error_response_info_; |
247 | 262 |
263 GetMessengerCallback get_messenger_callback_; | |
264 | |
248 base::WeakPtrFactory<SSLConnectJob> weak_factory_; | 265 base::WeakPtrFactory<SSLConnectJob> weak_factory_; |
249 | 266 |
250 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); | 267 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); |
251 }; | 268 }; |
252 | 269 |
253 class NET_EXPORT_PRIVATE SSLClientSocketPool | 270 class NET_EXPORT_PRIVATE SSLClientSocketPool |
254 : public ClientSocketPool, | 271 : public ClientSocketPool, |
255 public HigherLayeredPool, | 272 public HigherLayeredPool, |
256 public SSLConfigService::Observer { | 273 public SSLConfigService::Observer { |
257 public: | 274 public: |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 // LowerLayeredPool implementation. | 340 // LowerLayeredPool implementation. |
324 virtual bool IsStalled() const OVERRIDE; | 341 virtual bool IsStalled() const OVERRIDE; |
325 | 342 |
326 virtual void AddHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE; | 343 virtual void AddHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE; |
327 | 344 |
328 virtual void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE; | 345 virtual void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE; |
329 | 346 |
330 // HigherLayeredPool implementation. | 347 // HigherLayeredPool implementation. |
331 virtual bool CloseOneIdleConnection() OVERRIDE; | 348 virtual bool CloseOneIdleConnection() OVERRIDE; |
332 | 349 |
350 // Gets the SSLConnectJobMessenger for the given ssl session |cache_key|. If | |
351 // none exits, it creates one and stores it in |messenger_map_|. | |
352 SSLConnectJobMessenger* GetOrCreateSSLConnectJobMessenger( | |
353 const std::string& cache_key); | |
354 void DeleteSSLConnectJobMessenger(const std::string& cache_key); | |
355 | |
333 private: | 356 private: |
334 typedef ClientSocketPoolBase<SSLSocketParams> PoolBase; | 357 typedef ClientSocketPoolBase<SSLSocketParams> PoolBase; |
358 // Maps SSLConnectJob cache keys to SSLConnectJobMessenger objects. | |
359 typedef std::map<std::string, SSLConnectJobMessenger*> MessengerMap; | |
335 | 360 |
336 // SSLConfigService::Observer implementation. | 361 // SSLConfigService::Observer implementation. |
337 | 362 |
338 // When the user changes the SSL config, we flush all idle sockets so they | 363 // When the user changes the SSL config, we flush all idle sockets so they |
339 // won't get re-used. | 364 // won't get re-used. |
340 virtual void OnSSLConfigChanged() OVERRIDE; | 365 virtual void OnSSLConfigChanged() OVERRIDE; |
341 | 366 |
342 class SSLConnectJobFactory : public PoolBase::ConnectJobFactory { | 367 class SSLConnectJobFactory : public PoolBase::ConnectJobFactory { |
343 public: | 368 public: |
344 SSLConnectJobFactory(TransportClientSocketPool* transport_pool, | 369 SSLConnectJobFactory( |
345 SOCKSClientSocketPool* socks_pool, | 370 TransportClientSocketPool* transport_pool, |
346 HttpProxyClientSocketPool* http_proxy_pool, | 371 SOCKSClientSocketPool* socks_pool, |
347 ClientSocketFactory* client_socket_factory, | 372 HttpProxyClientSocketPool* http_proxy_pool, |
348 HostResolver* host_resolver, | 373 ClientSocketFactory* client_socket_factory, |
349 const SSLClientSocketContext& context, | 374 HostResolver* host_resolver, |
350 bool enable_ssl_connect_job_waiting, | 375 const SSLClientSocketContext& context, |
351 NetLog* net_log); | 376 const SSLConnectJob::GetMessengerCallback& get_messenger_callback, |
377 NetLog* net_log); | |
352 | 378 |
353 virtual ~SSLConnectJobFactory(); | 379 virtual ~SSLConnectJobFactory(); |
354 | 380 |
355 // ClientSocketPoolBase::ConnectJobFactory methods. | 381 // ClientSocketPoolBase::ConnectJobFactory methods. |
356 virtual scoped_ptr<ConnectJob> NewConnectJob( | 382 virtual scoped_ptr<ConnectJob> NewConnectJob( |
357 const std::string& group_name, | 383 const std::string& group_name, |
358 const PoolBase::Request& request, | 384 const PoolBase::Request& request, |
359 ConnectJob::Delegate* delegate) const OVERRIDE; | 385 ConnectJob::Delegate* delegate) const OVERRIDE; |
360 | 386 |
361 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE; | 387 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE; |
362 | 388 |
363 private: | 389 private: |
364 // Maps SSLConnectJob cache keys to SSLConnectJobMessenger objects. | |
365 typedef std::map<std::string, SSLConnectJobMessenger*> MessengerMap; | |
366 | |
367 TransportClientSocketPool* const transport_pool_; | 390 TransportClientSocketPool* const transport_pool_; |
368 SOCKSClientSocketPool* const socks_pool_; | 391 SOCKSClientSocketPool* const socks_pool_; |
369 HttpProxyClientSocketPool* const http_proxy_pool_; | 392 HttpProxyClientSocketPool* const http_proxy_pool_; |
370 ClientSocketFactory* const client_socket_factory_; | 393 ClientSocketFactory* const client_socket_factory_; |
371 HostResolver* const host_resolver_; | 394 HostResolver* const host_resolver_; |
372 const SSLClientSocketContext context_; | 395 const SSLClientSocketContext context_; |
373 base::TimeDelta timeout_; | 396 base::TimeDelta timeout_; |
374 bool enable_ssl_connect_job_waiting_; | 397 SSLConnectJob::GetMessengerCallback get_messenger_callback_; |
375 NetLog* net_log_; | 398 NetLog* net_log_; |
376 // |messenger_map_| is currently a pointer so that an element can be | |
377 // added to it inside of the const method NewConnectJob. In the future, | |
378 // elements will be added in a different method. | |
379 // TODO(mshelley) Change this to a non-pointer. | |
380 scoped_ptr<MessengerMap> messenger_map_; | |
381 | 399 |
382 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); | 400 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); |
383 }; | 401 }; |
384 | 402 |
385 TransportClientSocketPool* const transport_pool_; | 403 TransportClientSocketPool* const transport_pool_; |
386 SOCKSClientSocketPool* const socks_pool_; | 404 SOCKSClientSocketPool* const socks_pool_; |
387 HttpProxyClientSocketPool* const http_proxy_pool_; | 405 HttpProxyClientSocketPool* const http_proxy_pool_; |
388 PoolBase base_; | 406 PoolBase base_; |
389 const scoped_refptr<SSLConfigService> ssl_config_service_; | 407 const scoped_refptr<SSLConfigService> ssl_config_service_; |
408 MessengerMap messenger_map_; | |
409 bool enable_ssl_connect_job_waiting_; | |
390 | 410 |
391 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); | 411 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); |
392 }; | 412 }; |
393 | 413 |
394 } // namespace net | 414 } // namespace net |
395 | 415 |
396 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ | 416 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ |
OLD | NEW |