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