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 #include "net/socket/ssl_client_socket_pool.h" | 5 #include "net/socket/ssl_client_socket_pool.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/metrics/sparse_histogram.h" | 11 #include "base/metrics/sparse_histogram.h" |
12 #include "base/stl_util.h" | |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 #include "net/base/host_port_pair.h" | 14 #include "net/base/host_port_pair.h" |
14 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
15 #include "net/http/http_proxy_client_socket.h" | 16 #include "net/http/http_proxy_client_socket.h" |
16 #include "net/http/http_proxy_client_socket_pool.h" | 17 #include "net/http/http_proxy_client_socket_pool.h" |
17 #include "net/socket/client_socket_factory.h" | 18 #include "net/socket/client_socket_factory.h" |
18 #include "net/socket/client_socket_handle.h" | 19 #include "net/socket/client_socket_handle.h" |
19 #include "net/socket/socks_client_socket_pool.h" | 20 #include "net/socket/socks_client_socket_pool.h" |
20 #include "net/socket/ssl_client_socket.h" | 21 #include "net/socket/ssl_client_socket.h" |
21 #include "net/socket/transport_client_socket_pool.h" | 22 #include "net/socket/transport_client_socket_pool.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 DCHECK_EQ(GetConnectionType(), SOCKS_PROXY); | 88 DCHECK_EQ(GetConnectionType(), SOCKS_PROXY); |
88 return socks_proxy_params_; | 89 return socks_proxy_params_; |
89 } | 90 } |
90 | 91 |
91 const scoped_refptr<HttpProxySocketParams>& | 92 const scoped_refptr<HttpProxySocketParams>& |
92 SSLSocketParams::GetHttpProxyConnectionParams() const { | 93 SSLSocketParams::GetHttpProxyConnectionParams() const { |
93 DCHECK_EQ(GetConnectionType(), HTTP_PROXY); | 94 DCHECK_EQ(GetConnectionType(), HTTP_PROXY); |
94 return http_proxy_params_; | 95 return http_proxy_params_; |
95 } | 96 } |
96 | 97 |
98 void SSLConnectJobMessenger::RemovePendingSocket(SSLClientSocket* ssl_socket) { | |
99 // Sockets do not need to be removed from connecting_sockets_ because | |
100 // either OnJobSucceeded or OnJobFailed will do this. | |
101 for (SSLPendingSocketsAndCallbacks::iterator it = | |
102 pending_sockets_and_callbacks_.begin(); | |
103 it != pending_sockets_and_callbacks_.end(); | |
104 ++it) { | |
105 if (it->socket == ssl_socket) { | |
106 pending_sockets_and_callbacks_.erase(it); | |
107 return; | |
108 } | |
109 } | |
110 } | |
111 | |
112 bool SSLConnectJobMessenger::CanProceed(SSLClientSocket* ssl_socket) { | |
113 // If the session is in the session cache, or there are no connecting | |
114 // sockets, allow the connection to proceed. | |
115 return ssl_socket->InSessionCache() || connecting_sockets_.empty(); | |
116 } | |
117 | |
118 void SSLConnectJobMessenger::MonitorConnectionResult( | |
119 SSLClientSocket* ssl_socket) { | |
120 connecting_sockets_.push_back(ssl_socket); | |
121 // TODO(mshelley): Both of these callbacks will use WeakPtr in future CL. | |
122 ssl_socket->SetHandshakeFailureCallback( | |
123 base::Bind(&SSLConnectJobMessenger::OnJobFailed, base::Unretained(this))); | |
124 ssl_socket->SetHandshakeSuccessCallback(base::Bind( | |
125 &SSLConnectJobMessenger::OnJobSucceeded, base::Unretained(this))); | |
126 } | |
127 | |
128 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, | |
129 const base::Closure& callback) { | |
130 DCHECK(connecting_sockets_.empty()); | |
131 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); | |
132 } | |
133 | |
134 void SSLConnectJobMessenger::OnJobSucceeded() { | |
135 connecting_sockets_.clear(); | |
136 SSLPendingSocketsAndCallbacks temp_list; | |
137 pending_sockets_and_callbacks_.swap(temp_list); | |
Ryan Sleevi
2014/07/18 22:01:17
Typically, when using the swap pattern, it's commo
mshelley
2014/07/21 23:00:08
Done.
| |
138 RunAllCallbacks(temp_list); | |
139 } | |
140 | |
141 void SSLConnectJobMessenger::OnJobFailed() { | |
142 connecting_sockets_.clear(); | |
143 if (pending_sockets_and_callbacks_.empty()) | |
144 return; | |
145 SSLClientSocket* ssl_socket = pending_sockets_and_callbacks_[0].socket; | |
146 base::Closure callback = pending_sockets_and_callbacks_[0].callback; | |
147 pending_sockets_and_callbacks_.erase(pending_sockets_and_callbacks_.begin()); | |
Ryan Sleevi
2014/07/18 22:01:17
Simplified
SocketAndCallback socket_and_callback
mshelley
2014/07/21 23:00:08
Done.
| |
148 MonitorConnectionResult(ssl_socket); | |
149 callback.Run(); | |
150 } | |
151 | |
152 void SSLConnectJobMessenger::RunAllCallbacks( | |
153 const SSLPendingSocketsAndCallbacks& pending_sockets_and_callbacks) { | |
154 for (std::vector<SocketAndCallback>::const_iterator it = | |
155 pending_sockets_and_callbacks.begin(); | |
156 it != pending_sockets_and_callbacks.end(); | |
157 ++it) { | |
158 it->callback.Run(); | |
159 } | |
160 } | |
161 | |
97 // Timeout for the SSL handshake portion of the connect. | 162 // Timeout for the SSL handshake portion of the connect. |
98 static const int kSSLHandshakeTimeoutInSeconds = 30; | 163 static const int kSSLHandshakeTimeoutInSeconds = 30; |
99 | 164 |
100 SSLConnectJob::SSLConnectJob(const std::string& group_name, | 165 SSLConnectJob::SSLConnectJob(const std::string& group_name, |
101 RequestPriority priority, | 166 RequestPriority priority, |
102 const scoped_refptr<SSLSocketParams>& params, | 167 const scoped_refptr<SSLSocketParams>& params, |
103 const base::TimeDelta& timeout_duration, | 168 const base::TimeDelta& timeout_duration, |
104 TransportClientSocketPool* transport_pool, | 169 TransportClientSocketPool* transport_pool, |
105 SOCKSClientSocketPool* socks_pool, | 170 SOCKSClientSocketPool* socks_pool, |
106 HttpProxyClientSocketPool* http_proxy_pool, | 171 HttpProxyClientSocketPool* http_proxy_pool, |
107 ClientSocketFactory* client_socket_factory, | 172 ClientSocketFactory* client_socket_factory, |
108 HostResolver* host_resolver, | 173 HostResolver* host_resolver, |
109 const SSLClientSocketContext& context, | 174 const SSLClientSocketContext& context, |
175 SSLConnectJobMessenger* messenger, | |
110 Delegate* delegate, | 176 Delegate* delegate, |
111 NetLog* net_log) | 177 NetLog* net_log) |
112 : ConnectJob(group_name, | 178 : ConnectJob(group_name, |
113 timeout_duration, | 179 timeout_duration, |
114 priority, | 180 priority, |
115 delegate, | 181 delegate, |
116 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)), | 182 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)), |
117 params_(params), | 183 params_(params), |
118 transport_pool_(transport_pool), | 184 transport_pool_(transport_pool), |
119 socks_pool_(socks_pool), | 185 socks_pool_(socks_pool), |
120 http_proxy_pool_(http_proxy_pool), | 186 http_proxy_pool_(http_proxy_pool), |
121 client_socket_factory_(client_socket_factory), | 187 client_socket_factory_(client_socket_factory), |
122 host_resolver_(host_resolver), | 188 host_resolver_(host_resolver), |
123 context_(context.cert_verifier, | 189 context_(context.cert_verifier, |
124 context.server_bound_cert_service, | 190 context.server_bound_cert_service, |
125 context.transport_security_state, | 191 context.transport_security_state, |
126 context.cert_transparency_verifier, | 192 context.cert_transparency_verifier, |
127 (params->privacy_mode() == PRIVACY_MODE_ENABLED | 193 (params->privacy_mode() == PRIVACY_MODE_ENABLED |
128 ? "pm/" + context.ssl_session_cache_shard | 194 ? "pm/" + context.ssl_session_cache_shard |
129 : context.ssl_session_cache_shard)), | 195 : context.ssl_session_cache_shard)), |
130 callback_(base::Bind(&SSLConnectJob::OnIOComplete, | 196 io_callback_( |
131 base::Unretained(this))) {} | 197 base::Bind(&SSLConnectJob::OnIOComplete, base::Unretained(this))), |
198 messenger_(messenger), | |
199 weak_factory_(this) { | |
200 } | |
132 | 201 |
133 SSLConnectJob::~SSLConnectJob() {} | 202 SSLConnectJob::~SSLConnectJob() { |
203 if (ssl_socket_.get() != NULL && messenger_ != NULL) | |
Ryan Sleevi
2014/07/18 22:01:17
if (ssl_socket.get() && messenger_)
mshelley
2014/07/21 23:00:08
Done.
| |
204 messenger_->RemovePendingSocket(ssl_socket_.get()); | |
205 } | |
134 | 206 |
135 LoadState SSLConnectJob::GetLoadState() const { | 207 LoadState SSLConnectJob::GetLoadState() const { |
136 switch (next_state_) { | 208 switch (next_state_) { |
137 case STATE_TUNNEL_CONNECT_COMPLETE: | 209 case STATE_TUNNEL_CONNECT_COMPLETE: |
138 if (transport_socket_handle_->socket()) | 210 if (transport_socket_handle_->socket()) |
139 return LOAD_STATE_ESTABLISHING_PROXY_TUNNEL; | 211 return LOAD_STATE_ESTABLISHING_PROXY_TUNNEL; |
140 // else, fall through. | 212 // else, fall through. |
141 case STATE_TRANSPORT_CONNECT: | 213 case STATE_TRANSPORT_CONNECT: |
142 case STATE_TRANSPORT_CONNECT_COMPLETE: | 214 case STATE_TRANSPORT_CONNECT_COMPLETE: |
143 case STATE_SOCKS_CONNECT: | 215 case STATE_SOCKS_CONNECT: |
144 case STATE_SOCKS_CONNECT_COMPLETE: | 216 case STATE_SOCKS_CONNECT_COMPLETE: |
145 case STATE_TUNNEL_CONNECT: | 217 case STATE_TUNNEL_CONNECT: |
146 return transport_socket_handle_->GetLoadState(); | 218 return transport_socket_handle_->GetLoadState(); |
219 case STATE_CREATE_SSL_SOCKET: | |
220 case STATE_CHECK_FOR_RESUME: | |
147 case STATE_SSL_CONNECT: | 221 case STATE_SSL_CONNECT: |
148 case STATE_SSL_CONNECT_COMPLETE: | 222 case STATE_SSL_CONNECT_COMPLETE: |
149 return LOAD_STATE_SSL_HANDSHAKE; | 223 return LOAD_STATE_SSL_HANDSHAKE; |
150 default: | 224 default: |
151 NOTREACHED(); | 225 NOTREACHED(); |
152 return LOAD_STATE_IDLE; | 226 return LOAD_STATE_IDLE; |
153 } | 227 } |
154 } | 228 } |
155 | 229 |
156 void SSLConnectJob::GetAdditionalErrorState(ClientSocketHandle* handle) { | 230 void SSLConnectJob::GetAdditionalErrorState(ClientSocketHandle* handle) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 case STATE_SOCKS_CONNECT_COMPLETE: | 267 case STATE_SOCKS_CONNECT_COMPLETE: |
194 rv = DoSOCKSConnectComplete(rv); | 268 rv = DoSOCKSConnectComplete(rv); |
195 break; | 269 break; |
196 case STATE_TUNNEL_CONNECT: | 270 case STATE_TUNNEL_CONNECT: |
197 DCHECK_EQ(OK, rv); | 271 DCHECK_EQ(OK, rv); |
198 rv = DoTunnelConnect(); | 272 rv = DoTunnelConnect(); |
199 break; | 273 break; |
200 case STATE_TUNNEL_CONNECT_COMPLETE: | 274 case STATE_TUNNEL_CONNECT_COMPLETE: |
201 rv = DoTunnelConnectComplete(rv); | 275 rv = DoTunnelConnectComplete(rv); |
202 break; | 276 break; |
277 case STATE_CREATE_SSL_SOCKET: | |
278 rv = DoCreateSSLSocket(); | |
279 break; | |
280 case STATE_CHECK_FOR_RESUME: | |
281 rv = DoCheckForResume(); | |
282 break; | |
203 case STATE_SSL_CONNECT: | 283 case STATE_SSL_CONNECT: |
204 DCHECK_EQ(OK, rv); | 284 DCHECK_EQ(OK, rv); |
205 rv = DoSSLConnect(); | 285 rv = DoSSLConnect(); |
206 break; | 286 break; |
207 case STATE_SSL_CONNECT_COMPLETE: | 287 case STATE_SSL_CONNECT_COMPLETE: |
208 rv = DoSSLConnectComplete(rv); | 288 rv = DoSSLConnectComplete(rv); |
209 break; | 289 break; |
210 default: | 290 default: |
211 NOTREACHED() << "bad state"; | 291 NOTREACHED() << "bad state"; |
212 rv = ERR_FAILED; | 292 rv = ERR_FAILED; |
213 break; | 293 break; |
214 } | 294 } |
215 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 295 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
216 | 296 |
217 return rv; | 297 return rv; |
218 } | 298 } |
219 | 299 |
220 int SSLConnectJob::DoTransportConnect() { | 300 int SSLConnectJob::DoTransportConnect() { |
221 DCHECK(transport_pool_); | 301 DCHECK(transport_pool_); |
222 | 302 |
223 next_state_ = STATE_TRANSPORT_CONNECT_COMPLETE; | 303 next_state_ = STATE_TRANSPORT_CONNECT_COMPLETE; |
224 transport_socket_handle_.reset(new ClientSocketHandle()); | 304 transport_socket_handle_.reset(new ClientSocketHandle()); |
225 scoped_refptr<TransportSocketParams> direct_params = | 305 scoped_refptr<TransportSocketParams> direct_params = |
226 params_->GetDirectConnectionParams(); | 306 params_->GetDirectConnectionParams(); |
227 return transport_socket_handle_->Init(group_name(), | 307 return transport_socket_handle_->Init(group_name(), |
228 direct_params, | 308 direct_params, |
229 priority(), | 309 priority(), |
230 callback_, | 310 io_callback_, |
231 transport_pool_, | 311 transport_pool_, |
232 net_log()); | 312 net_log()); |
233 } | 313 } |
234 | 314 |
235 int SSLConnectJob::DoTransportConnectComplete(int result) { | 315 int SSLConnectJob::DoTransportConnectComplete(int result) { |
236 if (result == OK) | 316 if (result != OK) |
237 next_state_ = STATE_SSL_CONNECT; | 317 return result; |
318 | |
319 next_state_ = STATE_CREATE_SSL_SOCKET; | |
238 | 320 |
239 return result; | 321 return result; |
240 } | 322 } |
241 | 323 |
242 int SSLConnectJob::DoSOCKSConnect() { | 324 int SSLConnectJob::DoSOCKSConnect() { |
243 DCHECK(socks_pool_); | 325 DCHECK(socks_pool_); |
244 next_state_ = STATE_SOCKS_CONNECT_COMPLETE; | 326 next_state_ = STATE_SOCKS_CONNECT_COMPLETE; |
245 transport_socket_handle_.reset(new ClientSocketHandle()); | 327 transport_socket_handle_.reset(new ClientSocketHandle()); |
246 scoped_refptr<SOCKSSocketParams> socks_proxy_params = | 328 scoped_refptr<SOCKSSocketParams> socks_proxy_params = |
247 params_->GetSocksProxyConnectionParams(); | 329 params_->GetSocksProxyConnectionParams(); |
248 return transport_socket_handle_->Init(group_name(), | 330 return transport_socket_handle_->Init(group_name(), |
249 socks_proxy_params, | 331 socks_proxy_params, |
250 priority(), | 332 priority(), |
251 callback_, | 333 io_callback_, |
252 socks_pool_, | 334 socks_pool_, |
253 net_log()); | 335 net_log()); |
254 } | 336 } |
255 | 337 |
256 int SSLConnectJob::DoSOCKSConnectComplete(int result) { | 338 int SSLConnectJob::DoSOCKSConnectComplete(int result) { |
257 if (result == OK) | 339 if (result != OK) |
258 next_state_ = STATE_SSL_CONNECT; | 340 return result; |
341 | |
342 next_state_ = STATE_CREATE_SSL_SOCKET; | |
259 | 343 |
260 return result; | 344 return result; |
261 } | 345 } |
262 | 346 |
263 int SSLConnectJob::DoTunnelConnect() { | 347 int SSLConnectJob::DoTunnelConnect() { |
264 DCHECK(http_proxy_pool_); | 348 DCHECK(http_proxy_pool_); |
265 next_state_ = STATE_TUNNEL_CONNECT_COMPLETE; | 349 next_state_ = STATE_TUNNEL_CONNECT_COMPLETE; |
266 | 350 |
267 transport_socket_handle_.reset(new ClientSocketHandle()); | 351 transport_socket_handle_.reset(new ClientSocketHandle()); |
268 scoped_refptr<HttpProxySocketParams> http_proxy_params = | 352 scoped_refptr<HttpProxySocketParams> http_proxy_params = |
269 params_->GetHttpProxyConnectionParams(); | 353 params_->GetHttpProxyConnectionParams(); |
270 return transport_socket_handle_->Init(group_name(), | 354 return transport_socket_handle_->Init(group_name(), |
271 http_proxy_params, | 355 http_proxy_params, |
272 priority(), | 356 priority(), |
273 callback_, | 357 io_callback_, |
274 http_proxy_pool_, | 358 http_proxy_pool_, |
275 net_log()); | 359 net_log()); |
276 } | 360 } |
277 | 361 |
278 int SSLConnectJob::DoTunnelConnectComplete(int result) { | 362 int SSLConnectJob::DoTunnelConnectComplete(int result) { |
279 // Extract the information needed to prompt for appropriate proxy | 363 // Extract the information needed to prompt for appropriate proxy |
280 // authentication so that when ClientSocketPoolBaseHelper calls | 364 // authentication so that when ClientSocketPoolBaseHelper calls |
281 // |GetAdditionalErrorState|, we can easily set the state. | 365 // |GetAdditionalErrorState|, we can easily set the state. |
282 if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { | 366 if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
283 error_response_info_ = transport_socket_handle_->ssl_error_response_info(); | 367 error_response_info_ = transport_socket_handle_->ssl_error_response_info(); |
284 } else if (result == ERR_PROXY_AUTH_REQUESTED || | 368 } else if (result == ERR_PROXY_AUTH_REQUESTED || |
285 result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) { | 369 result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) { |
286 StreamSocket* socket = transport_socket_handle_->socket(); | 370 StreamSocket* socket = transport_socket_handle_->socket(); |
287 HttpProxyClientSocket* tunnel_socket = | 371 HttpProxyClientSocket* tunnel_socket = |
288 static_cast<HttpProxyClientSocket*>(socket); | 372 static_cast<HttpProxyClientSocket*>(socket); |
289 error_response_info_ = *tunnel_socket->GetConnectResponseInfo(); | 373 error_response_info_ = *tunnel_socket->GetConnectResponseInfo(); |
290 } | 374 } |
291 if (result < 0) | 375 if (result < 0) |
292 return result; | 376 return result; |
293 | 377 |
294 next_state_ = STATE_SSL_CONNECT; | 378 next_state_ = STATE_CREATE_SSL_SOCKET; |
295 return result; | 379 return result; |
296 } | 380 } |
297 | 381 |
298 int SSLConnectJob::DoSSLConnect() { | 382 int SSLConnectJob::DoCreateSSLSocket() { |
299 next_state_ = STATE_SSL_CONNECT_COMPLETE; | 383 if (messenger_ != NULL) |
Ryan Sleevi
2014/07/18 22:01:17
if (messenger_)
mshelley
2014/07/21 23:00:08
Done.
| |
384 next_state_ = STATE_CHECK_FOR_RESUME; | |
385 else | |
386 next_state_ = STATE_SSL_CONNECT; | |
300 // Reset the timeout to just the time allowed for the SSL handshake. | 387 // Reset the timeout to just the time allowed for the SSL handshake. |
301 ResetTimer(base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds)); | 388 ResetTimer(base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds)); |
302 | 389 |
303 // If the handle has a fresh socket, get its connect start and DNS times. | 390 // If the handle has a fresh socket, get its connect start and DNS times. |
304 // This should always be the case. | 391 // This should always be the case. |
305 const LoadTimingInfo::ConnectTiming& socket_connect_timing = | 392 const LoadTimingInfo::ConnectTiming& socket_connect_timing = |
306 transport_socket_handle_->connect_timing(); | 393 transport_socket_handle_->connect_timing(); |
307 if (!transport_socket_handle_->is_reused() && | 394 if (!transport_socket_handle_->is_reused() && |
308 !socket_connect_timing.connect_start.is_null()) { | 395 !socket_connect_timing.connect_start.is_null()) { |
309 // Overwriting |connect_start| serves two purposes - it adjusts timing so | 396 // Overwriting |connect_start| serves two purposes - it adjusts timing so |
310 // |connect_start| doesn't include dns times, and it adjusts the time so | 397 // |connect_start| doesn't include dns times, and it adjusts the time so |
311 // as not to include time spent waiting for an idle socket. | 398 // as not to include time spent waiting for an idle socket. |
312 connect_timing_.connect_start = socket_connect_timing.connect_start; | 399 connect_timing_.connect_start = socket_connect_timing.connect_start; |
313 connect_timing_.dns_start = socket_connect_timing.dns_start; | 400 connect_timing_.dns_start = socket_connect_timing.dns_start; |
314 connect_timing_.dns_end = socket_connect_timing.dns_end; | 401 connect_timing_.dns_end = socket_connect_timing.dns_end; |
315 } | 402 } |
316 | 403 |
317 connect_timing_.ssl_start = base::TimeTicks::Now(); | |
318 | |
319 ssl_socket_ = client_socket_factory_->CreateSSLClientSocket( | 404 ssl_socket_ = client_socket_factory_->CreateSSLClientSocket( |
320 transport_socket_handle_.Pass(), | 405 transport_socket_handle_.Pass(), |
321 params_->host_and_port(), | 406 params_->host_and_port(), |
322 params_->ssl_config(), | 407 params_->ssl_config(), |
323 context_); | 408 context_); |
324 return ssl_socket_->Connect(callback_); | 409 return OK; |
410 } | |
411 | |
412 int SSLConnectJob::DoCheckForResume() { | |
413 next_state_ = STATE_SSL_CONNECT; | |
414 // TODO(mshelley): Remove duplicate InSessionCache() calls. | |
415 if (messenger_->CanProceed(ssl_socket_.get())) { | |
416 if (!ssl_socket_->InSessionCache()) | |
417 messenger_->MonitorConnectionResult(ssl_socket_.get()); | |
418 return OK; | |
419 } | |
420 messenger_->AddPendingSocket(ssl_socket_.get(), | |
421 base::Bind(&SSLConnectJob::ResumeSSLConnection, | |
422 weak_factory_.GetWeakPtr())); | |
423 return ERR_IO_PENDING; | |
424 } | |
425 | |
426 int SSLConnectJob::DoSSLConnect() { | |
427 next_state_ = STATE_SSL_CONNECT_COMPLETE; | |
428 | |
429 connect_timing_.ssl_start = base::TimeTicks::Now(); | |
430 | |
431 return ssl_socket_->Connect(io_callback_); | |
325 } | 432 } |
326 | 433 |
327 int SSLConnectJob::DoSSLConnectComplete(int result) { | 434 int SSLConnectJob::DoSSLConnectComplete(int result) { |
328 connect_timing_.ssl_end = base::TimeTicks::Now(); | 435 connect_timing_.ssl_end = base::TimeTicks::Now(); |
329 | 436 |
330 SSLClientSocket::NextProtoStatus status = | 437 SSLClientSocket::NextProtoStatus status = |
331 SSLClientSocket::kNextProtoUnsupported; | 438 SSLClientSocket::kNextProtoUnsupported; |
332 std::string proto; | 439 std::string proto; |
333 std::string server_protos; | 440 std::string server_protos; |
334 // GetNextProto will fail and and trigger a NOTREACHED if we pass in a socket | 441 // GetNextProto will fail and and trigger a NOTREACHED if we pass in a socket |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
404 100); | 511 100); |
405 } else if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_FULL) { | 512 } else if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_FULL) { |
406 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Full_Handshake", | 513 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Full_Handshake", |
407 connect_duration, | 514 connect_duration, |
408 base::TimeDelta::FromMilliseconds(1), | 515 base::TimeDelta::FromMilliseconds(1), |
409 base::TimeDelta::FromMinutes(1), | 516 base::TimeDelta::FromMinutes(1), |
410 100); | 517 100); |
411 } | 518 } |
412 | 519 |
413 const std::string& host = params_->host_and_port().host(); | 520 const std::string& host = params_->host_and_port().host(); |
414 bool is_google = host == "google.com" || | 521 bool is_google = |
415 (host.size() > 11 && | 522 host == "google.com" || |
416 host.rfind(".google.com") == host.size() - 11); | 523 (host.size() > 11 && host.rfind(".google.com") == host.size() - 11); |
417 if (is_google) { | 524 if (is_google) { |
418 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google2", | 525 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google2", |
419 connect_duration, | 526 connect_duration, |
420 base::TimeDelta::FromMilliseconds(1), | 527 base::TimeDelta::FromMilliseconds(1), |
421 base::TimeDelta::FromMinutes(1), | 528 base::TimeDelta::FromMinutes(1), |
422 100); | 529 100); |
423 if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_RESUME) { | 530 if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_RESUME) { |
424 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google_" | 531 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google_" |
425 "Resume_Handshake", | 532 "Resume_Handshake", |
426 connect_duration, | 533 connect_duration, |
(...skipping 15 matching lines...) Expand all Loading... | |
442 SetSocket(ssl_socket_.PassAs<StreamSocket>()); | 549 SetSocket(ssl_socket_.PassAs<StreamSocket>()); |
443 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { | 550 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
444 error_response_info_.cert_request_info = new SSLCertRequestInfo; | 551 error_response_info_.cert_request_info = new SSLCertRequestInfo; |
445 ssl_socket_->GetSSLCertRequestInfo( | 552 ssl_socket_->GetSSLCertRequestInfo( |
446 error_response_info_.cert_request_info.get()); | 553 error_response_info_.cert_request_info.get()); |
447 } | 554 } |
448 | 555 |
449 return result; | 556 return result; |
450 } | 557 } |
451 | 558 |
559 void SSLConnectJob::ResumeSSLConnection() { | |
560 DCHECK_EQ(next_state_, STATE_SSL_CONNECT); | |
561 OnIOComplete(OK); | |
562 } | |
563 | |
452 SSLConnectJob::State SSLConnectJob::GetInitialState( | 564 SSLConnectJob::State SSLConnectJob::GetInitialState( |
453 SSLSocketParams::ConnectionType connection_type) { | 565 SSLSocketParams::ConnectionType connection_type) { |
454 switch (connection_type) { | 566 switch (connection_type) { |
455 case SSLSocketParams::DIRECT: | 567 case SSLSocketParams::DIRECT: |
456 return STATE_TRANSPORT_CONNECT; | 568 return STATE_TRANSPORT_CONNECT; |
457 case SSLSocketParams::HTTP_PROXY: | 569 case SSLSocketParams::HTTP_PROXY: |
458 return STATE_TUNNEL_CONNECT; | 570 return STATE_TUNNEL_CONNECT; |
459 case SSLSocketParams::SOCKS_PROXY: | 571 case SSLSocketParams::SOCKS_PROXY: |
460 return STATE_SOCKS_CONNECT; | 572 return STATE_SOCKS_CONNECT; |
461 } | 573 } |
(...skipping 13 matching lines...) Expand all Loading... | |
475 ClientSocketFactory* client_socket_factory, | 587 ClientSocketFactory* client_socket_factory, |
476 HostResolver* host_resolver, | 588 HostResolver* host_resolver, |
477 const SSLClientSocketContext& context, | 589 const SSLClientSocketContext& context, |
478 NetLog* net_log) | 590 NetLog* net_log) |
479 : transport_pool_(transport_pool), | 591 : transport_pool_(transport_pool), |
480 socks_pool_(socks_pool), | 592 socks_pool_(socks_pool), |
481 http_proxy_pool_(http_proxy_pool), | 593 http_proxy_pool_(http_proxy_pool), |
482 client_socket_factory_(client_socket_factory), | 594 client_socket_factory_(client_socket_factory), |
483 host_resolver_(host_resolver), | 595 host_resolver_(host_resolver), |
484 context_(context), | 596 context_(context), |
485 net_log_(net_log) { | 597 net_log_(net_log), |
598 messenger_map_(new MessengerMap) { | |
486 base::TimeDelta max_transport_timeout = base::TimeDelta(); | 599 base::TimeDelta max_transport_timeout = base::TimeDelta(); |
487 base::TimeDelta pool_timeout; | 600 base::TimeDelta pool_timeout; |
488 if (transport_pool_) | 601 if (transport_pool_) |
489 max_transport_timeout = transport_pool_->ConnectionTimeout(); | 602 max_transport_timeout = transport_pool_->ConnectionTimeout(); |
490 if (socks_pool_) { | 603 if (socks_pool_) { |
491 pool_timeout = socks_pool_->ConnectionTimeout(); | 604 pool_timeout = socks_pool_->ConnectionTimeout(); |
492 if (pool_timeout > max_transport_timeout) | 605 if (pool_timeout > max_transport_timeout) |
493 max_transport_timeout = pool_timeout; | 606 max_transport_timeout = pool_timeout; |
494 } | 607 } |
495 if (http_proxy_pool_) { | 608 if (http_proxy_pool_) { |
496 pool_timeout = http_proxy_pool_->ConnectionTimeout(); | 609 pool_timeout = http_proxy_pool_->ConnectionTimeout(); |
497 if (pool_timeout > max_transport_timeout) | 610 if (pool_timeout > max_transport_timeout) |
498 max_transport_timeout = pool_timeout; | 611 max_transport_timeout = pool_timeout; |
499 } | 612 } |
500 timeout_ = max_transport_timeout + | 613 timeout_ = max_transport_timeout + |
501 base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds); | 614 base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds); |
502 } | 615 } |
503 | 616 |
617 SSLClientSocketPool::SSLConnectJobFactory::~SSLConnectJobFactory() { | |
618 STLDeleteValues(messenger_map_.get()); | |
619 } | |
620 | |
621 // static | |
622 bool SSLClientSocketPool::enable_connect_job_waiting_ = false; | |
623 | |
504 SSLClientSocketPool::SSLClientSocketPool( | 624 SSLClientSocketPool::SSLClientSocketPool( |
505 int max_sockets, | 625 int max_sockets, |
506 int max_sockets_per_group, | 626 int max_sockets_per_group, |
507 ClientSocketPoolHistograms* histograms, | 627 ClientSocketPoolHistograms* histograms, |
508 HostResolver* host_resolver, | 628 HostResolver* host_resolver, |
509 CertVerifier* cert_verifier, | 629 CertVerifier* cert_verifier, |
510 ServerBoundCertService* server_bound_cert_service, | 630 ServerBoundCertService* server_bound_cert_service, |
511 TransportSecurityState* transport_security_state, | 631 TransportSecurityState* transport_security_state, |
512 CTVerifier* cert_transparency_verifier, | 632 CTVerifier* cert_transparency_verifier, |
513 const std::string& ssl_session_cache_shard, | 633 const std::string& ssl_session_cache_shard, |
514 ClientSocketFactory* client_socket_factory, | 634 ClientSocketFactory* client_socket_factory, |
515 TransportClientSocketPool* transport_pool, | 635 TransportClientSocketPool* transport_pool, |
516 SOCKSClientSocketPool* socks_pool, | 636 SOCKSClientSocketPool* socks_pool, |
517 HttpProxyClientSocketPool* http_proxy_pool, | 637 HttpProxyClientSocketPool* http_proxy_pool, |
518 SSLConfigService* ssl_config_service, | 638 SSLConfigService* ssl_config_service, |
519 NetLog* net_log) | 639 NetLog* net_log) |
520 : transport_pool_(transport_pool), | 640 : transport_pool_(transport_pool), |
521 socks_pool_(socks_pool), | 641 socks_pool_(socks_pool), |
522 http_proxy_pool_(http_proxy_pool), | 642 http_proxy_pool_(http_proxy_pool), |
523 base_(this, max_sockets, max_sockets_per_group, histograms, | 643 base_(this, |
644 max_sockets, | |
645 max_sockets_per_group, | |
646 histograms, | |
524 ClientSocketPool::unused_idle_socket_timeout(), | 647 ClientSocketPool::unused_idle_socket_timeout(), |
525 ClientSocketPool::used_idle_socket_timeout(), | 648 ClientSocketPool::used_idle_socket_timeout(), |
526 new SSLConnectJobFactory(transport_pool, | 649 new SSLConnectJobFactory( |
527 socks_pool, | 650 transport_pool, |
528 http_proxy_pool, | 651 socks_pool, |
529 client_socket_factory, | 652 http_proxy_pool, |
530 host_resolver, | 653 client_socket_factory, |
531 SSLClientSocketContext( | 654 host_resolver, |
532 cert_verifier, | 655 SSLClientSocketContext(cert_verifier, |
533 server_bound_cert_service, | 656 server_bound_cert_service, |
534 transport_security_state, | 657 transport_security_state, |
535 cert_transparency_verifier, | 658 cert_transparency_verifier, |
536 ssl_session_cache_shard), | 659 ssl_session_cache_shard), |
537 net_log)), | 660 net_log)), |
538 ssl_config_service_(ssl_config_service) { | 661 ssl_config_service_(ssl_config_service) { |
539 if (ssl_config_service_.get()) | 662 if (ssl_config_service_.get()) |
540 ssl_config_service_->AddObserver(this); | 663 ssl_config_service_->AddObserver(this); |
541 if (transport_pool_) | 664 if (transport_pool_) |
542 base_.AddLowerLayeredPool(transport_pool_); | 665 base_.AddLowerLayeredPool(transport_pool_); |
543 if (socks_pool_) | 666 if (socks_pool_) |
544 base_.AddLowerLayeredPool(socks_pool_); | 667 base_.AddLowerLayeredPool(socks_pool_); |
545 if (http_proxy_pool_) | 668 if (http_proxy_pool_) |
546 base_.AddLowerLayeredPool(http_proxy_pool_); | 669 base_.AddLowerLayeredPool(http_proxy_pool_); |
547 } | 670 } |
548 | 671 |
549 SSLClientSocketPool::~SSLClientSocketPool() { | 672 SSLClientSocketPool::~SSLClientSocketPool() { |
550 if (ssl_config_service_.get()) | 673 if (ssl_config_service_.get()) |
551 ssl_config_service_->RemoveObserver(this); | 674 ssl_config_service_->RemoveObserver(this); |
552 } | 675 } |
553 | 676 |
554 scoped_ptr<ConnectJob> | 677 scoped_ptr<ConnectJob> |
555 SSLClientSocketPool::SSLConnectJobFactory::NewConnectJob( | 678 SSLClientSocketPool::SSLConnectJobFactory::NewConnectJob( |
556 const std::string& group_name, | 679 const std::string& group_name, |
557 const PoolBase::Request& request, | 680 const PoolBase::Request& request, |
558 ConnectJob::Delegate* delegate) const { | 681 ConnectJob::Delegate* delegate) const { |
559 return scoped_ptr<ConnectJob>( | 682 SSLConnectJobMessenger* messenger = NULL; |
560 new SSLConnectJob(group_name, request.priority(), request.params(), | 683 if (SSLClientSocketPool::get_enable_connect_job_waiting()) { |
561 ConnectionTimeout(), transport_pool_, socks_pool_, | 684 std::string cache_key = SSLClientSocket::GetSessionCacheKey( |
562 http_proxy_pool_, client_socket_factory_, | 685 request.params()->host_and_port(), context_.ssl_session_cache_shard); |
563 host_resolver_, context_, delegate, net_log_)); | 686 MessengerMap::const_iterator it = messenger_map_->find(cache_key); |
687 if (it == messenger_map_->end()) { | |
688 std::pair<MessengerMap::iterator, bool> iter = messenger_map_->insert( | |
689 MessengerMap::value_type(cache_key, new SSLConnectJobMessenger())); | |
690 it = iter.first; | |
691 } | |
692 messenger = it->second; | |
693 } | |
694 | |
695 return scoped_ptr<ConnectJob>(new SSLConnectJob(group_name, | |
696 request.priority(), | |
697 request.params(), | |
698 ConnectionTimeout(), | |
699 transport_pool_, | |
700 socks_pool_, | |
701 http_proxy_pool_, | |
702 client_socket_factory_, | |
703 host_resolver_, | |
704 context_, | |
705 messenger, | |
706 delegate, | |
707 net_log_)); | |
564 } | 708 } |
565 | 709 |
566 base::TimeDelta | 710 base::TimeDelta |
567 SSLClientSocketPool::SSLConnectJobFactory::ConnectionTimeout() const { | 711 SSLClientSocketPool::SSLConnectJobFactory::ConnectionTimeout() const { |
568 return timeout_; | 712 return timeout_; |
569 } | 713 } |
570 | 714 |
571 int SSLClientSocketPool::RequestSocket(const std::string& group_name, | 715 int SSLClientSocketPool::RequestSocket(const std::string& group_name, |
572 const void* socket_params, | 716 const void* socket_params, |
573 RequestPriority priority, | 717 RequestPriority priority, |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
672 HigherLayeredPool* higher_pool) { | 816 HigherLayeredPool* higher_pool) { |
673 base_.RemoveHigherLayeredPool(higher_pool); | 817 base_.RemoveHigherLayeredPool(higher_pool); |
674 } | 818 } |
675 | 819 |
676 bool SSLClientSocketPool::CloseOneIdleConnection() { | 820 bool SSLClientSocketPool::CloseOneIdleConnection() { |
677 if (base_.CloseOneIdleSocket()) | 821 if (base_.CloseOneIdleSocket()) |
678 return true; | 822 return true; |
679 return base_.CloseOneIdleConnectionInHigherLayeredPool(); | 823 return base_.CloseOneIdleConnectionInHigherLayeredPool(); |
680 } | 824 } |
681 | 825 |
826 // static | |
827 void SSLClientSocketPool::set_enable_connect_job_waiting(bool enable) { | |
828 enable_connect_job_waiting_ = enable; | |
829 } | |
830 | |
831 // static | |
832 bool SSLClientSocketPool::get_enable_connect_job_waiting() { | |
833 return enable_connect_job_waiting_; | |
834 } | |
835 | |
682 void SSLClientSocketPool::OnSSLConfigChanged() { | 836 void SSLClientSocketPool::OnSSLConfigChanged() { |
683 FlushWithError(ERR_NETWORK_CHANGED); | 837 FlushWithError(ERR_NETWORK_CHANGED); |
684 } | 838 } |
685 | 839 |
686 } // namespace net | 840 } // namespace net |
OLD | NEW |