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" |
14 #include "base/memory/scoped_vector.h" | |
12 #include "base/time/time.h" | 15 #include "base/time/time.h" |
13 #include "net/base/privacy_mode.h" | 16 #include "net/base/privacy_mode.h" |
14 #include "net/dns/host_resolver.h" | 17 #include "net/dns/host_resolver.h" |
15 #include "net/http/http_response_info.h" | 18 #include "net/http/http_response_info.h" |
16 #include "net/socket/client_socket_pool.h" | 19 #include "net/socket/client_socket_pool.h" |
17 #include "net/socket/client_socket_pool_base.h" | 20 #include "net/socket/client_socket_pool_base.h" |
18 #include "net/socket/client_socket_pool_histograms.h" | 21 #include "net/socket/client_socket_pool_histograms.h" |
19 #include "net/socket/ssl_client_socket.h" | 22 #include "net/socket/ssl_client_socket.h" |
20 #include "net/ssl/ssl_config_service.h" | 23 #include "net/ssl/ssl_config_service.h" |
21 | 24 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 const SSLConfig ssl_config_; | 90 const SSLConfig ssl_config_; |
88 const PrivacyMode privacy_mode_; | 91 const PrivacyMode privacy_mode_; |
89 const int load_flags_; | 92 const int load_flags_; |
90 const bool force_spdy_over_ssl_; | 93 const bool force_spdy_over_ssl_; |
91 const bool want_spdy_over_npn_; | 94 const bool want_spdy_over_npn_; |
92 bool ignore_limits_; | 95 bool ignore_limits_; |
93 | 96 |
94 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams); | 97 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams); |
95 }; | 98 }; |
96 | 99 |
100 // SSLConnectJobMessenger handles communication between concurrent | |
101 // SSLConnectJobs. | |
wtc
2014/07/08 01:25:42
Please point out these SSLConnectJobs have the sam
mshelley
2014/07/09 19:51:01
Done.
| |
102 // | |
103 // SSLConnectJobMessengers tell the session cache when a certain | |
104 // connection should be monitored for success or failure, and | |
105 // tell SSLConnectJobs when to pause or resume thier connections. | |
106 class NET_EXPORT SSLConnectJobMessenger { | |
wtc
2014/07/08 01:25:43
Use NET_EXPORT_PRIVATE instead.
mshelley
2014/07/09 19:51:01
Done.
| |
107 public: | |
108 // Sets rv to true if the given |ssl_socket| should continue its | |
wtc
2014/07/08 01:25:43
By "Sets rv to true", did you mean "Returns true"?
mshelley
2014/07/09 19:51:02
Done.
| |
109 // SSL connection. | |
110 bool CanProceed(SSLClientSocket* ssl_socket); | |
111 | |
112 // Informs the session cache that it should notify the SSLConnectJobMessenger | |
113 // upon the completion of |ssl_socket|'s connection. | |
114 void MonitorConnectionResult(SSLClientSocket* ssl_socket); | |
115 | |
116 // Adds the socket and its associated callback to the SSLConnectJobMessenger's | |
117 // list of pending sockets and callbacks. | |
118 void AddPendingSocket(SSLClientSocket* socket, const base::Closure& callback); | |
119 | |
120 // Processes pending callbacks when a socket successfully completes | |
121 // its connection. | |
122 void OnJobSucceeded(); | |
123 | |
124 // Processes pending callbacks when a socket encounters an error | |
125 // while completing its connection. | |
126 void OnJobFailed(); | |
127 | |
128 private: | |
129 // SSLPendingSocketsAndCallbacks manages the callback and socket | |
130 // associated with a pending ssl connection. | |
131 class SSLPendingSocketsAndCallbacks { | |
132 public: | |
133 SSLPendingSocketsAndCallbacks() {} | |
134 | |
135 SSLPendingSocketsAndCallbacks(const SSLPendingSocketsAndCallbacks& ref) { | |
136 std::vector<SSLClientSocket*>::const_iterator socket_it = | |
137 ref.pending_sockets_.begin(); | |
138 std::vector<base::Closure>::const_iterator callback_it = | |
139 ref.callbacks_.begin(); | |
140 while (socket_it != ref.pending_sockets_.end() && | |
141 callback_it != ref.callbacks_.end()) { | |
wtc
2014/07/08 01:25:43
BUG: this while loop is wrong. You need two while
mshelley
2014/07/09 19:51:01
Done.
| |
142 pending_sockets_.push_back(*socket_it); | |
143 callbacks_.push_back(*callback_it); | |
144 } | |
145 } | |
146 | |
147 SSLPendingSocketsAndCallbacks& operator=( | |
148 const SSLPendingSocketsAndCallbacks& ref) { | |
149 SSLPendingSocketsAndCallbacks temp(ref); | |
150 temp.Swap(*this); | |
wtc
2014/07/08 01:25:43
I don't know why the Swap method is needed. You sh
mshelley
2014/07/09 19:51:02
Done.
| |
151 return *this; | |
152 } | |
153 | |
154 void Swap(SSLPendingSocketsAndCallbacks ref) throw() { | |
wtc
2014/07/08 01:25:43
Do we need "throw()" here? This is the first time
mshelley
2014/07/09 19:51:01
Done.
| |
155 std::swap(pending_sockets_, this->pending_sockets_); | |
156 std::swap(callbacks_, this->callbacks_); | |
157 } | |
158 | |
159 void AddSocket(SSLClientSocket* socket, const base::Closure& cb) { | |
160 pending_sockets_.push_back(socket); | |
161 callbacks_.push_back(cb); | |
162 } | |
163 | |
164 void Clear() { | |
165 pending_sockets_.clear(); | |
166 callbacks_.clear(); | |
167 } | |
168 | |
169 // Tells all pending connections to resume. | |
170 void RunAllJobs() { | |
171 for (std::vector<base::Closure>::iterator it = callbacks_.begin(); | |
wtc
2014/07/08 01:25:42
Use const_iterator?
mshelley
2014/07/09 19:51:01
Done.
| |
172 it != callbacks_.end(); | |
173 it++) | |
174 it->Run(); | |
175 } | |
176 | |
177 SSLClientSocket* GetFirstSocket() { | |
178 DCHECK(!pending_sockets_.empty()); | |
179 return pending_sockets_[0]; | |
180 } | |
181 | |
182 base::Closure GetFirstCallback() { | |
183 DCHECK(!callbacks_.empty()); | |
184 return callbacks_[0]; | |
185 } | |
186 | |
187 bool Empty() { | |
188 if (pending_sockets_.empty() || callbacks_.empty()) | |
189 return true; | |
190 return false; | |
191 } | |
192 | |
193 void EraseFirstEntry() { | |
194 if (pending_sockets_.begin() == pending_sockets_.end() || | |
195 callbacks_.begin() == callbacks_.end()) | |
196 return; | |
197 pending_sockets_.erase(pending_sockets_.begin()); | |
198 callbacks_.erase(callbacks_.begin()); | |
199 } | |
200 | |
201 private: | |
202 std::vector<SSLClientSocket*> pending_sockets_; | |
203 std::vector<base::Closure> callbacks_; | |
wtc
2014/07/08 01:25:42
IMPORTANT: it seems that you should instead define
mshelley
2014/07/09 19:51:01
Done.
| |
204 }; | |
205 | |
206 SSLPendingSocketsAndCallbacks pending_sockets_and_callbacks_; | |
207 std::vector<SSLClientSocket*> connecting_sockets_; | |
208 }; | |
209 | |
97 // SSLConnectJob handles the SSL handshake after setting up the underlying | 210 // SSLConnectJob handles the SSL handshake after setting up the underlying |
98 // connection as specified in the params. | 211 // connection as specified in the params. |
99 class SSLConnectJob : public ConnectJob { | 212 class SSLConnectJob : public ConnectJob { |
100 public: | 213 public: |
101 SSLConnectJob( | 214 // Note: the SSLConnectJob does not own |messenger|. |
102 const std::string& group_name, | 215 // so it must outlive the job. |
103 RequestPriority priority, | 216 SSLConnectJob(const std::string& group_name, |
104 const scoped_refptr<SSLSocketParams>& params, | 217 RequestPriority priority, |
105 const base::TimeDelta& timeout_duration, | 218 const scoped_refptr<SSLSocketParams>& params, |
106 TransportClientSocketPool* transport_pool, | 219 const base::TimeDelta& timeout_duration, |
107 SOCKSClientSocketPool* socks_pool, | 220 TransportClientSocketPool* transport_pool, |
108 HttpProxyClientSocketPool* http_proxy_pool, | 221 SOCKSClientSocketPool* socks_pool, |
109 ClientSocketFactory* client_socket_factory, | 222 HttpProxyClientSocketPool* http_proxy_pool, |
110 HostResolver* host_resolver, | 223 ClientSocketFactory* client_socket_factory, |
111 const SSLClientSocketContext& context, | 224 HostResolver* host_resolver, |
112 Delegate* delegate, | 225 const SSLClientSocketContext& context, |
113 NetLog* net_log); | 226 SSLConnectJobMessenger* messenger, |
227 Delegate* delegate, | |
228 NetLog* net_log); | |
114 virtual ~SSLConnectJob(); | 229 virtual ~SSLConnectJob(); |
115 | 230 |
116 // ConnectJob methods. | 231 // ConnectJob methods. |
117 virtual LoadState GetLoadState() const OVERRIDE; | 232 virtual LoadState GetLoadState() const OVERRIDE; |
118 | 233 |
119 virtual void GetAdditionalErrorState(ClientSocketHandle * handle) OVERRIDE; | 234 virtual void GetAdditionalErrorState(ClientSocketHandle * handle) OVERRIDE; |
120 | 235 |
236 base::WeakPtr<SSLConnectJob> GetWeakPtr(); | |
237 | |
121 private: | 238 private: |
122 enum State { | 239 enum State { |
123 STATE_TRANSPORT_CONNECT, | 240 STATE_TRANSPORT_CONNECT, |
124 STATE_TRANSPORT_CONNECT_COMPLETE, | 241 STATE_TRANSPORT_CONNECT_COMPLETE, |
125 STATE_SOCKS_CONNECT, | 242 STATE_SOCKS_CONNECT, |
126 STATE_SOCKS_CONNECT_COMPLETE, | 243 STATE_SOCKS_CONNECT_COMPLETE, |
127 STATE_TUNNEL_CONNECT, | 244 STATE_TUNNEL_CONNECT, |
128 STATE_TUNNEL_CONNECT_COMPLETE, | 245 STATE_TUNNEL_CONNECT_COMPLETE, |
246 STATE_CREATE_SSL_SOCKET, | |
247 STATE_CHECK_FOR_RESUME, | |
129 STATE_SSL_CONNECT, | 248 STATE_SSL_CONNECT, |
130 STATE_SSL_CONNECT_COMPLETE, | 249 STATE_SSL_CONNECT_COMPLETE, |
131 STATE_NONE, | 250 STATE_NONE, |
132 }; | 251 }; |
133 | 252 |
134 void OnIOComplete(int result); | 253 void OnIOComplete(int result); |
135 | 254 |
136 // Runs the state transition loop. | 255 // Runs the state transition loop. |
137 int DoLoop(int result); | 256 int DoLoop(int result); |
138 | 257 |
139 int DoTransportConnect(); | 258 int DoTransportConnect(); |
140 int DoTransportConnectComplete(int result); | 259 int DoTransportConnectComplete(int result); |
141 int DoSOCKSConnect(); | 260 int DoSOCKSConnect(); |
142 int DoSOCKSConnectComplete(int result); | 261 int DoSOCKSConnectComplete(int result); |
143 int DoTunnelConnect(); | 262 int DoTunnelConnect(); |
144 int DoTunnelConnectComplete(int result); | 263 int DoTunnelConnectComplete(int result); |
264 int DoCreateSSLSocket(); | |
265 int DoCheckForResume(); | |
145 int DoSSLConnect(); | 266 int DoSSLConnect(); |
146 int DoSSLConnectComplete(int result); | 267 int DoSSLConnectComplete(int result); |
147 | 268 |
269 // Tells a waiting SSLConnectJob to resume its SSL connection. | |
270 void ResumeSSLConnection(); | |
271 | |
148 // Returns the initial state for the state machine based on the | 272 // Returns the initial state for the state machine based on the |
149 // |connection_type|. | 273 // |connection_type|. |
150 static State GetInitialState(SSLSocketParams::ConnectionType connection_type); | 274 static State GetInitialState(SSLSocketParams::ConnectionType connection_type); |
151 | 275 |
152 // Starts the SSL connection process. Returns OK on success and | 276 // Starts the SSL connection process. Returns OK on success and |
153 // ERR_IO_PENDING if it cannot immediately service the request. | 277 // ERR_IO_PENDING if it cannot immediately service the request. |
154 // Otherwise, it returns a net error code. | 278 // Otherwise, it returns a net error code. |
155 virtual int ConnectInternal() OVERRIDE; | 279 virtual int ConnectInternal() OVERRIDE; |
156 | 280 |
157 scoped_refptr<SSLSocketParams> params_; | 281 scoped_refptr<SSLSocketParams> params_; |
158 TransportClientSocketPool* const transport_pool_; | 282 TransportClientSocketPool* const transport_pool_; |
159 SOCKSClientSocketPool* const socks_pool_; | 283 SOCKSClientSocketPool* const socks_pool_; |
160 HttpProxyClientSocketPool* const http_proxy_pool_; | 284 HttpProxyClientSocketPool* const http_proxy_pool_; |
161 ClientSocketFactory* const client_socket_factory_; | 285 ClientSocketFactory* const client_socket_factory_; |
162 HostResolver* const host_resolver_; | 286 HostResolver* const host_resolver_; |
163 | 287 |
164 const SSLClientSocketContext context_; | 288 const SSLClientSocketContext context_; |
165 | 289 |
166 State next_state_; | 290 State next_state_; |
167 CompletionCallback callback_; | 291 CompletionCallback io_callback_; |
168 scoped_ptr<ClientSocketHandle> transport_socket_handle_; | 292 scoped_ptr<ClientSocketHandle> transport_socket_handle_; |
169 scoped_ptr<SSLClientSocket> ssl_socket_; | 293 scoped_ptr<SSLClientSocket> ssl_socket_; |
170 | 294 |
295 SSLConnectJobMessenger* messenger_; | |
171 HttpResponseInfo error_response_info_; | 296 HttpResponseInfo error_response_info_; |
172 | 297 |
298 base::WeakPtrFactory<SSLConnectJob> weak_factory_; | |
299 | |
173 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); | 300 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob); |
174 }; | 301 }; |
175 | 302 |
176 class NET_EXPORT_PRIVATE SSLClientSocketPool | 303 class NET_EXPORT_PRIVATE SSLClientSocketPool |
177 : public ClientSocketPool, | 304 : public ClientSocketPool, |
178 public HigherLayeredPool, | 305 public HigherLayeredPool, |
179 public SSLConfigService::Observer { | 306 public SSLConfigService::Observer { |
180 public: | 307 public: |
181 typedef SSLSocketParams SocketParams; | 308 typedef SSLSocketParams SocketParams; |
182 | 309 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 | 404 |
278 // ClientSocketPoolBase::ConnectJobFactory methods. | 405 // ClientSocketPoolBase::ConnectJobFactory methods. |
279 virtual scoped_ptr<ConnectJob> NewConnectJob( | 406 virtual scoped_ptr<ConnectJob> NewConnectJob( |
280 const std::string& group_name, | 407 const std::string& group_name, |
281 const PoolBase::Request& request, | 408 const PoolBase::Request& request, |
282 ConnectJob::Delegate* delegate) const OVERRIDE; | 409 ConnectJob::Delegate* delegate) const OVERRIDE; |
283 | 410 |
284 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE; | 411 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE; |
285 | 412 |
286 private: | 413 private: |
414 // Maps SSLConnectJob cache keys to SSLConnectJobMessenger objects. | |
415 typedef std::map<std::string, SSLConnectJobMessenger*> MessengerMap; | |
416 | |
287 TransportClientSocketPool* const transport_pool_; | 417 TransportClientSocketPool* const transport_pool_; |
288 SOCKSClientSocketPool* const socks_pool_; | 418 SOCKSClientSocketPool* const socks_pool_; |
289 HttpProxyClientSocketPool* const http_proxy_pool_; | 419 HttpProxyClientSocketPool* const http_proxy_pool_; |
290 ClientSocketFactory* const client_socket_factory_; | 420 ClientSocketFactory* const client_socket_factory_; |
291 HostResolver* const host_resolver_; | 421 HostResolver* const host_resolver_; |
292 const SSLClientSocketContext context_; | 422 const SSLClientSocketContext context_; |
293 base::TimeDelta timeout_; | 423 base::TimeDelta timeout_; |
294 NetLog* net_log_; | 424 NetLog* net_log_; |
425 scoped_ptr<MessengerMap> messenger_map_; | |
mmenke
2014/07/08 21:02:05
There doesn't seem to be a need for this to be a s
mshelley
2014/07/09 19:51:01
The idea was that I didn't want it to be a plain p
| |
295 | 426 |
296 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); | 427 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory); |
297 }; | 428 }; |
298 | 429 |
299 TransportClientSocketPool* const transport_pool_; | 430 TransportClientSocketPool* const transport_pool_; |
300 SOCKSClientSocketPool* const socks_pool_; | 431 SOCKSClientSocketPool* const socks_pool_; |
301 HttpProxyClientSocketPool* const http_proxy_pool_; | 432 HttpProxyClientSocketPool* const http_proxy_pool_; |
302 PoolBase base_; | 433 PoolBase base_; |
303 const scoped_refptr<SSLConfigService> ssl_config_service_; | 434 const scoped_refptr<SSLConfigService> ssl_config_service_; |
304 | 435 |
305 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); | 436 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool); |
306 }; | 437 }; |
307 | 438 |
308 } // namespace net | 439 } // namespace net |
309 | 440 |
310 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ | 441 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_ |
OLD | NEW |