[email protected] | e34400c3 | 2012-01-24 02:49:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/socket/ssl_client_socket_pool.h" |
| 6 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
[email protected] | c63248d4 | 2011-02-18 17:54:39 | [diff] [blame] | 9 | #include "base/metrics/field_trial.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 10 | #include "base/metrics/histogram.h" |
[email protected] | 92a3124 | 2013-06-28 18:08:08 | [diff] [blame] | 11 | #include "base/metrics/sparse_histogram.h" |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 12 | #include "base/stl_util.h" |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 13 | #include "base/values.h" |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 14 | #include "net/base/host_port_pair.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 15 | #include "net/base/net_errors.h" |
[email protected] | 33b511c | 2010-08-11 00:04:43 | [diff] [blame] | 16 | #include "net/http/http_proxy_client_socket.h" |
| 17 | #include "net/http/http_proxy_client_socket_pool.h" |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 18 | #include "net/socket/client_socket_factory.h" |
| 19 | #include "net/socket/client_socket_handle.h" |
[email protected] | 33b511c | 2010-08-11 00:04:43 | [diff] [blame] | 20 | #include "net/socket/socks_client_socket_pool.h" |
| 21 | #include "net/socket/ssl_client_socket.h" |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 22 | #include "net/socket/transport_client_socket_pool.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 23 | #include "net/ssl/ssl_cert_request_info.h" |
[email protected] | 92a3124 | 2013-06-28 18:08:08 | [diff] [blame] | 24 | #include "net/ssl/ssl_connection_status_flags.h" |
| 25 | #include "net/ssl/ssl_info.h" |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 26 | |
| 27 | namespace net { |
| 28 | |
| 29 | SSLSocketParams::SSLSocketParams( |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 30 | const scoped_refptr<TransportSocketParams>& direct_params, |
| 31 | const scoped_refptr<SOCKSSocketParams>& socks_proxy_params, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 32 | const scoped_refptr<HttpProxySocketParams>& http_proxy_params, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 33 | const HostPortPair& host_and_port, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 34 | const SSLConfig& ssl_config, |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 35 | PrivacyMode privacy_mode, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 36 | int load_flags, |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 37 | bool force_spdy_over_ssl, |
| 38 | bool want_spdy_over_npn) |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 39 | : direct_params_(direct_params), |
| 40 | socks_proxy_params_(socks_proxy_params), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 41 | http_proxy_params_(http_proxy_params), |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 42 | host_and_port_(host_and_port), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 43 | ssl_config_(ssl_config), |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 44 | privacy_mode_(privacy_mode), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 45 | load_flags_(load_flags), |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 46 | force_spdy_over_ssl_(force_spdy_over_ssl), |
[email protected] | 0e14e87d | 2011-06-21 21:24:19 | [diff] [blame] | 47 | want_spdy_over_npn_(want_spdy_over_npn), |
| 48 | ignore_limits_(false) { |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 49 | if (direct_params_) { |
| 50 | DCHECK(!socks_proxy_params_); |
| 51 | DCHECK(!http_proxy_params_); |
| 52 | ignore_limits_ = direct_params_->ignore_limits(); |
| 53 | } else if (socks_proxy_params_) { |
| 54 | DCHECK(!http_proxy_params_); |
| 55 | ignore_limits_ = socks_proxy_params_->ignore_limits(); |
| 56 | } else { |
| 57 | DCHECK(http_proxy_params_); |
| 58 | ignore_limits_ = http_proxy_params_->ignore_limits(); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | SSLSocketParams::~SSLSocketParams() {} |
| 63 | |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 64 | SSLSocketParams::ConnectionType SSLSocketParams::GetConnectionType() const { |
| 65 | if (direct_params_) { |
| 66 | DCHECK(!socks_proxy_params_); |
| 67 | DCHECK(!http_proxy_params_); |
| 68 | return DIRECT; |
| 69 | } |
| 70 | |
| 71 | if (socks_proxy_params_) { |
| 72 | DCHECK(!http_proxy_params_); |
| 73 | return SOCKS_PROXY; |
| 74 | } |
| 75 | |
| 76 | DCHECK(http_proxy_params_); |
| 77 | return HTTP_PROXY; |
| 78 | } |
| 79 | |
| 80 | const scoped_refptr<TransportSocketParams>& |
| 81 | SSLSocketParams::GetDirectConnectionParams() const { |
| 82 | DCHECK_EQ(GetConnectionType(), DIRECT); |
| 83 | return direct_params_; |
| 84 | } |
| 85 | |
| 86 | const scoped_refptr<SOCKSSocketParams>& |
| 87 | SSLSocketParams::GetSocksProxyConnectionParams() const { |
| 88 | DCHECK_EQ(GetConnectionType(), SOCKS_PROXY); |
| 89 | return socks_proxy_params_; |
| 90 | } |
| 91 | |
| 92 | const scoped_refptr<HttpProxySocketParams>& |
| 93 | SSLSocketParams::GetHttpProxyConnectionParams() const { |
| 94 | DCHECK_EQ(GetConnectionType(), HTTP_PROXY); |
| 95 | return http_proxy_params_; |
| 96 | } |
| 97 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 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 | |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 107 | SSLConnectJobMessenger::SSLConnectJobMessenger( |
| 108 | const base::Closure& messenger_finished_callback) |
| 109 | : messenger_finished_callback_(messenger_finished_callback), |
| 110 | weak_factory_(this) { |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | SSLConnectJobMessenger::~SSLConnectJobMessenger() { |
| 114 | } |
| 115 | |
| 116 | void SSLConnectJobMessenger::RemovePendingSocket(SSLClientSocket* ssl_socket) { |
| 117 | // Sockets do not need to be removed from connecting_sockets_ because |
| 118 | // OnSSLHandshakeCompleted will do this. |
| 119 | for (SSLPendingSocketsAndCallbacks::iterator it = |
| 120 | pending_sockets_and_callbacks_.begin(); |
| 121 | it != pending_sockets_and_callbacks_.end(); |
| 122 | ++it) { |
| 123 | if (it->socket == ssl_socket) { |
| 124 | pending_sockets_and_callbacks_.erase(it); |
| 125 | return; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | bool SSLConnectJobMessenger::CanProceed(SSLClientSocket* ssl_socket) { |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 131 | // If there are no connecting sockets, allow the connection to proceed. |
| 132 | return connecting_sockets_.empty(); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void SSLConnectJobMessenger::MonitorConnectionResult( |
| 136 | SSLClientSocket* ssl_socket) { |
| 137 | connecting_sockets_.push_back(ssl_socket); |
| 138 | ssl_socket->SetHandshakeCompletionCallback( |
| 139 | base::Bind(&SSLConnectJobMessenger::OnSSLHandshakeCompleted, |
| 140 | weak_factory_.GetWeakPtr())); |
| 141 | } |
| 142 | |
| 143 | void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* ssl_socket, |
| 144 | const base::Closure& callback) { |
| 145 | DCHECK(!connecting_sockets_.empty()); |
| 146 | pending_sockets_and_callbacks_.push_back( |
| 147 | SocketAndCallback(ssl_socket, callback)); |
| 148 | } |
| 149 | |
| 150 | void SSLConnectJobMessenger::OnSSLHandshakeCompleted() { |
| 151 | connecting_sockets_.clear(); |
| 152 | SSLPendingSocketsAndCallbacks temp_list; |
| 153 | temp_list.swap(pending_sockets_and_callbacks_); |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 154 | base::Closure messenger_finished_callback = messenger_finished_callback_; |
| 155 | messenger_finished_callback.Run(); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 156 | RunAllCallbacks(temp_list); |
| 157 | } |
| 158 | |
| 159 | void SSLConnectJobMessenger::RunAllCallbacks( |
| 160 | const SSLPendingSocketsAndCallbacks& pending_sockets_and_callbacks) { |
| 161 | for (std::vector<SocketAndCallback>::const_iterator it = |
| 162 | pending_sockets_and_callbacks.begin(); |
| 163 | it != pending_sockets_and_callbacks.end(); |
| 164 | ++it) { |
| 165 | it->callback.Run(); |
| 166 | } |
| 167 | } |
| 168 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 169 | // Timeout for the SSL handshake portion of the connect. |
| 170 | static const int kSSLHandshakeTimeoutInSeconds = 30; |
| 171 | |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 172 | SSLConnectJob::SSLConnectJob(const std::string& group_name, |
[email protected] | 3f6007ab | 2013-08-22 19:45:39 | [diff] [blame] | 173 | RequestPriority priority, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 174 | const scoped_refptr<SSLSocketParams>& params, |
| 175 | const base::TimeDelta& timeout_duration, |
| 176 | TransportClientSocketPool* transport_pool, |
| 177 | SOCKSClientSocketPool* socks_pool, |
| 178 | HttpProxyClientSocketPool* http_proxy_pool, |
| 179 | ClientSocketFactory* client_socket_factory, |
| 180 | HostResolver* host_resolver, |
| 181 | const SSLClientSocketContext& context, |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 182 | const GetMessengerCallback& get_messenger_callback, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 183 | Delegate* delegate, |
| 184 | NetLog* net_log) |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 185 | : ConnectJob(group_name, |
| 186 | timeout_duration, |
[email protected] | 3f6007ab | 2013-08-22 19:45:39 | [diff] [blame] | 187 | priority, |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 188 | delegate, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 189 | BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)), |
| 190 | params_(params), |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 191 | transport_pool_(transport_pool), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 192 | socks_pool_(socks_pool), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 193 | http_proxy_pool_(http_proxy_pool), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 194 | client_socket_factory_(client_socket_factory), |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 195 | host_resolver_(host_resolver), |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 196 | context_(context.cert_verifier, |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 197 | context.channel_id_service, |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 198 | context.transport_security_state, |
[email protected] | 284303b6 | 2013-11-28 15:11:54 | [diff] [blame] | 199 | context.cert_transparency_verifier, |
[email protected] | 314b0399 | 2014-04-01 01:28:53 | [diff] [blame] | 200 | (params->privacy_mode() == PRIVACY_MODE_ENABLED |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 201 | ? "pm/" + context.ssl_session_cache_shard |
| 202 | : context.ssl_session_cache_shard)), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 203 | io_callback_( |
| 204 | base::Bind(&SSLConnectJob::OnIOComplete, base::Unretained(this))), |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 205 | messenger_(NULL), |
| 206 | get_messenger_callback_(get_messenger_callback), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 207 | weak_factory_(this) { |
| 208 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 209 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 210 | SSLConnectJob::~SSLConnectJob() { |
| 211 | if (ssl_socket_.get() && messenger_) |
| 212 | messenger_->RemovePendingSocket(ssl_socket_.get()); |
| 213 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 214 | |
| 215 | LoadState SSLConnectJob::GetLoadState() const { |
| 216 | switch (next_state_) { |
[email protected] | 135e226 | 2010-07-17 00:32:04 | [diff] [blame] | 217 | case STATE_TUNNEL_CONNECT_COMPLETE: |
| 218 | if (transport_socket_handle_->socket()) |
| 219 | return LOAD_STATE_ESTABLISHING_PROXY_TUNNEL; |
| 220 | // else, fall through. |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 221 | case STATE_TRANSPORT_CONNECT: |
| 222 | case STATE_TRANSPORT_CONNECT_COMPLETE: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 223 | case STATE_SOCKS_CONNECT: |
| 224 | case STATE_SOCKS_CONNECT_COMPLETE: |
| 225 | case STATE_TUNNEL_CONNECT: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 226 | return transport_socket_handle_->GetLoadState(); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 227 | case STATE_CREATE_SSL_SOCKET: |
| 228 | case STATE_CHECK_FOR_RESUME: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 229 | case STATE_SSL_CONNECT: |
| 230 | case STATE_SSL_CONNECT_COMPLETE: |
| 231 | return LOAD_STATE_SSL_HANDSHAKE; |
| 232 | default: |
| 233 | NOTREACHED(); |
| 234 | return LOAD_STATE_IDLE; |
| 235 | } |
| 236 | } |
| 237 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 238 | void SSLConnectJob::GetAdditionalErrorState(ClientSocketHandle* handle) { |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 239 | // Headers in |error_response_info_| indicate a proxy tunnel setup |
| 240 | // problem. See DoTunnelConnectComplete. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 241 | if (error_response_info_.headers.get()) { |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 242 | handle->set_pending_http_proxy_connection( |
| 243 | transport_socket_handle_.release()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 244 | } |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 245 | handle->set_ssl_error_response_info(error_response_info_); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 246 | if (!connect_timing_.ssl_start.is_null()) |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 247 | handle->set_is_ssl_error(true); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void SSLConnectJob::OnIOComplete(int result) { |
| 251 | int rv = DoLoop(result); |
| 252 | if (rv != ERR_IO_PENDING) |
| 253 | NotifyDelegateOfCompletion(rv); // Deletes |this|. |
| 254 | } |
| 255 | |
| 256 | int SSLConnectJob::DoLoop(int result) { |
| 257 | DCHECK_NE(next_state_, STATE_NONE); |
| 258 | |
| 259 | int rv = result; |
| 260 | do { |
| 261 | State state = next_state_; |
| 262 | next_state_ = STATE_NONE; |
| 263 | switch (state) { |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 264 | case STATE_TRANSPORT_CONNECT: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 265 | DCHECK_EQ(OK, rv); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 266 | rv = DoTransportConnect(); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 267 | break; |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 268 | case STATE_TRANSPORT_CONNECT_COMPLETE: |
| 269 | rv = DoTransportConnectComplete(rv); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 270 | break; |
| 271 | case STATE_SOCKS_CONNECT: |
| 272 | DCHECK_EQ(OK, rv); |
| 273 | rv = DoSOCKSConnect(); |
| 274 | break; |
| 275 | case STATE_SOCKS_CONNECT_COMPLETE: |
| 276 | rv = DoSOCKSConnectComplete(rv); |
| 277 | break; |
| 278 | case STATE_TUNNEL_CONNECT: |
| 279 | DCHECK_EQ(OK, rv); |
| 280 | rv = DoTunnelConnect(); |
| 281 | break; |
| 282 | case STATE_TUNNEL_CONNECT_COMPLETE: |
| 283 | rv = DoTunnelConnectComplete(rv); |
| 284 | break; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 285 | case STATE_CREATE_SSL_SOCKET: |
| 286 | rv = DoCreateSSLSocket(); |
| 287 | break; |
| 288 | case STATE_CHECK_FOR_RESUME: |
| 289 | rv = DoCheckForResume(); |
| 290 | break; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 291 | case STATE_SSL_CONNECT: |
| 292 | DCHECK_EQ(OK, rv); |
| 293 | rv = DoSSLConnect(); |
| 294 | break; |
| 295 | case STATE_SSL_CONNECT_COMPLETE: |
| 296 | rv = DoSSLConnectComplete(rv); |
| 297 | break; |
| 298 | default: |
| 299 | NOTREACHED() << "bad state"; |
| 300 | rv = ERR_FAILED; |
| 301 | break; |
| 302 | } |
| 303 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 304 | |
| 305 | return rv; |
| 306 | } |
| 307 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 308 | int SSLConnectJob::DoTransportConnect() { |
| 309 | DCHECK(transport_pool_); |
[email protected] | 899c3e9 | 2010-08-28 15:53:50 | [diff] [blame] | 310 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 311 | next_state_ = STATE_TRANSPORT_CONNECT_COMPLETE; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 312 | transport_socket_handle_.reset(new ClientSocketHandle()); |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 313 | scoped_refptr<TransportSocketParams> direct_params = |
| 314 | params_->GetDirectConnectionParams(); |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 315 | return transport_socket_handle_->Init(group_name(), |
| 316 | direct_params, |
[email protected] | 3f6007ab | 2013-08-22 19:45:39 | [diff] [blame] | 317 | priority(), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 318 | io_callback_, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 319 | transport_pool_, |
| 320 | net_log()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 321 | } |
| 322 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 323 | int SSLConnectJob::DoTransportConnectComplete(int result) { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 324 | if (result == OK) |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 325 | next_state_ = STATE_CREATE_SSL_SOCKET; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 326 | |
| 327 | return result; |
| 328 | } |
| 329 | |
| 330 | int SSLConnectJob::DoSOCKSConnect() { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 331 | DCHECK(socks_pool_); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 332 | next_state_ = STATE_SOCKS_CONNECT_COMPLETE; |
| 333 | transport_socket_handle_.reset(new ClientSocketHandle()); |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 334 | scoped_refptr<SOCKSSocketParams> socks_proxy_params = |
| 335 | params_->GetSocksProxyConnectionParams(); |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 336 | return transport_socket_handle_->Init(group_name(), |
| 337 | socks_proxy_params, |
[email protected] | 3f6007ab | 2013-08-22 19:45:39 | [diff] [blame] | 338 | priority(), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 339 | io_callback_, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 340 | socks_pool_, |
| 341 | net_log()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | int SSLConnectJob::DoSOCKSConnectComplete(int result) { |
| 345 | if (result == OK) |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 346 | next_state_ = STATE_CREATE_SSL_SOCKET; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 347 | |
| 348 | return result; |
| 349 | } |
| 350 | |
| 351 | int SSLConnectJob::DoTunnelConnect() { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 352 | DCHECK(http_proxy_pool_); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 353 | next_state_ = STATE_TUNNEL_CONNECT_COMPLETE; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 354 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 355 | transport_socket_handle_.reset(new ClientSocketHandle()); |
| 356 | scoped_refptr<HttpProxySocketParams> http_proxy_params = |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 357 | params_->GetHttpProxyConnectionParams(); |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 358 | return transport_socket_handle_->Init(group_name(), |
| 359 | http_proxy_params, |
[email protected] | 3f6007ab | 2013-08-22 19:45:39 | [diff] [blame] | 360 | priority(), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 361 | io_callback_, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 362 | http_proxy_pool_, |
| 363 | net_log()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | int SSLConnectJob::DoTunnelConnectComplete(int result) { |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 367 | // Extract the information needed to prompt for appropriate proxy |
| 368 | // authentication so that when ClientSocketPoolBaseHelper calls |
| 369 | // |GetAdditionalErrorState|, we can easily set the state. |
| 370 | if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 371 | error_response_info_ = transport_socket_handle_->ssl_error_response_info(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 372 | } else if (result == ERR_PROXY_AUTH_REQUESTED || |
| 373 | result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) { |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 374 | StreamSocket* socket = transport_socket_handle_->socket(); |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 375 | HttpProxyClientSocket* tunnel_socket = |
| 376 | static_cast<HttpProxyClientSocket*>(socket); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 377 | error_response_info_ = *tunnel_socket->GetConnectResponseInfo(); |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 378 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 379 | if (result < 0) |
| 380 | return result; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 381 | next_state_ = STATE_CREATE_SSL_SOCKET; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 382 | return result; |
| 383 | } |
| 384 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 385 | int SSLConnectJob::DoCreateSSLSocket() { |
| 386 | next_state_ = STATE_CHECK_FOR_RESUME; |
| 387 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 388 | // Reset the timeout to just the time allowed for the SSL handshake. |
| 389 | ResetTimer(base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 390 | |
| 391 | // If the handle has a fresh socket, get its connect start and DNS times. |
| 392 | // This should always be the case. |
| 393 | const LoadTimingInfo::ConnectTiming& socket_connect_timing = |
| 394 | transport_socket_handle_->connect_timing(); |
| 395 | if (!transport_socket_handle_->is_reused() && |
| 396 | !socket_connect_timing.connect_start.is_null()) { |
| 397 | // Overwriting |connect_start| serves two purposes - it adjusts timing so |
| 398 | // |connect_start| doesn't include dns times, and it adjusts the time so |
| 399 | // as not to include time spent waiting for an idle socket. |
| 400 | connect_timing_.connect_start = socket_connect_timing.connect_start; |
| 401 | connect_timing_.dns_start = socket_connect_timing.dns_start; |
| 402 | connect_timing_.dns_end = socket_connect_timing.dns_end; |
| 403 | } |
| 404 | |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 405 | ssl_socket_ = client_socket_factory_->CreateSSLClientSocket( |
| 406 | transport_socket_handle_.Pass(), |
[email protected] | 5e5021a | 2013-07-17 05:23:36 | [diff] [blame] | 407 | params_->host_and_port(), |
| 408 | params_->ssl_config(), |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 409 | context_); |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 410 | |
| 411 | if (!ssl_socket_->InSessionCache()) |
| 412 | messenger_ = get_messenger_callback_.Run(ssl_socket_->GetSessionCacheKey()); |
| 413 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 414 | return OK; |
| 415 | } |
| 416 | |
| 417 | int SSLConnectJob::DoCheckForResume() { |
| 418 | next_state_ = STATE_SSL_CONNECT; |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 419 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 420 | if (!messenger_) |
| 421 | return OK; |
| 422 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 423 | if (messenger_->CanProceed(ssl_socket_.get())) { |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 424 | messenger_->MonitorConnectionResult(ssl_socket_.get()); |
| 425 | // The SSLConnectJob no longer needs access to the messenger after this |
| 426 | // point. |
| 427 | messenger_ = NULL; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 428 | return OK; |
| 429 | } |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 430 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 431 | messenger_->AddPendingSocket(ssl_socket_.get(), |
| 432 | base::Bind(&SSLConnectJob::ResumeSSLConnection, |
| 433 | weak_factory_.GetWeakPtr())); |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 434 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 435 | return ERR_IO_PENDING; |
| 436 | } |
| 437 | |
| 438 | int SSLConnectJob::DoSSLConnect() { |
| 439 | next_state_ = STATE_SSL_CONNECT_COMPLETE; |
| 440 | |
| 441 | connect_timing_.ssl_start = base::TimeTicks::Now(); |
| 442 | |
| 443 | return ssl_socket_->Connect(io_callback_); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | int SSLConnectJob::DoSSLConnectComplete(int result) { |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 447 | connect_timing_.ssl_end = base::TimeTicks::Now(); |
| 448 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 449 | SSLClientSocket::NextProtoStatus status = |
| 450 | SSLClientSocket::kNextProtoUnsupported; |
| 451 | std::string proto; |
| 452 | // GetNextProto will fail and and trigger a NOTREACHED if we pass in a socket |
| 453 | // that hasn't had SSL_ImportFD called on it. If we get a certificate error |
| 454 | // here, then we know that we called SSL_ImportFD. |
| 455 | if (result == OK || IsCertificateError(result)) |
[email protected] | abc44b75 | 2014-07-30 03:52:15 | [diff] [blame] | 456 | status = ssl_socket_->GetNextProto(&proto); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 457 | |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 458 | // If we want spdy over npn, make sure it succeeded. |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 459 | if (status == SSLClientSocket::kNextProtoNegotiated) { |
[email protected] | d7c9f42 | 2010-08-27 22:54:53 | [diff] [blame] | 460 | ssl_socket_->set_was_npn_negotiated(true); |
[email protected] | 8e3c78cb | 2012-03-31 03:58:46 | [diff] [blame] | 461 | NextProto protocol_negotiated = |
[email protected] | bace48c | 2010-08-03 20:52:02 | [diff] [blame] | 462 | SSLClientSocket::NextProtoFromString(proto); |
[email protected] | c30bcce | 2011-12-20 17:50:51 | [diff] [blame] | 463 | ssl_socket_->set_protocol_negotiated(protocol_negotiated); |
[email protected] | 63bf966 | 2013-03-05 20:46:01 | [diff] [blame] | 464 | // If we negotiated a SPDY version, it must have been present in |
| 465 | // SSLConfig::next_protos. |
| 466 | // TODO(mbelshe): Verify this. |
| 467 | if (protocol_negotiated >= kProtoSPDYMinimumVersion && |
| 468 | protocol_negotiated <= kProtoSPDYMaximumVersion) { |
[email protected] | d7c9f42 | 2010-08-27 22:54:53 | [diff] [blame] | 469 | ssl_socket_->set_was_spdy_negotiated(true); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 470 | } |
| 471 | } |
[email protected] | d7c9f42 | 2010-08-27 22:54:53 | [diff] [blame] | 472 | if (params_->want_spdy_over_npn() && !ssl_socket_->was_spdy_negotiated()) |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 473 | return ERR_NPN_NEGOTIATION_FAILED; |
| 474 | |
[email protected] | 9e9e842e | 2010-07-23 23:09:15 | [diff] [blame] | 475 | // Spdy might be turned on by default, or it might be over npn. |
| 476 | bool using_spdy = params_->force_spdy_over_ssl() || |
| 477 | params_->want_spdy_over_npn(); |
| 478 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 479 | if (result == OK || |
| 480 | ssl_socket_->IgnoreCertError(result, params_->load_flags())) { |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 481 | DCHECK(!connect_timing_.ssl_start.is_null()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 482 | base::TimeDelta connect_duration = |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 483 | connect_timing_.ssl_end - connect_timing_.ssl_start; |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 484 | if (using_spdy) { |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 485 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SpdyConnectionLatency_2", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 486 | connect_duration, |
| 487 | base::TimeDelta::FromMilliseconds(1), |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 488 | base::TimeDelta::FromMinutes(1), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 489 | 100); |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 490 | } |
[email protected] | 17ed3b8 | 2013-07-12 19:21:26 | [diff] [blame] | 491 | #if defined(SPDY_PROXY_AUTH_ORIGIN) |
| 492 | bool using_data_reduction_proxy = params_->host_and_port().Equals( |
| 493 | HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN))); |
| 494 | if (using_data_reduction_proxy) { |
| 495 | UMA_HISTOGRAM_CUSTOM_TIMES( |
| 496 | "Net.SSL_Connection_Latency_DataReductionProxy", |
| 497 | connect_duration, |
| 498 | base::TimeDelta::FromMilliseconds(1), |
| 499 | base::TimeDelta::FromMinutes(1), |
| 500 | 100); |
| 501 | } |
| 502 | #endif |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 503 | |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 504 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_2", |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 505 | connect_duration, |
| 506 | base::TimeDelta::FromMilliseconds(1), |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 507 | base::TimeDelta::FromMinutes(1), |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 508 | 100); |
| 509 | |
[email protected] | b076d6c | 2011-06-29 14:47:51 | [diff] [blame] | 510 | SSLInfo ssl_info; |
| 511 | ssl_socket_->GetSSLInfo(&ssl_info); |
| 512 | |
[email protected] | 92a3124 | 2013-06-28 18:08:08 | [diff] [blame] | 513 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSL_CipherSuite", |
| 514 | SSLConnectionStatusToCipherSuite( |
| 515 | ssl_info.connection_status)); |
| 516 | |
[email protected] | b076d6c | 2011-06-29 14:47:51 | [diff] [blame] | 517 | if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_RESUME) { |
| 518 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Resume_Handshake", |
| 519 | connect_duration, |
| 520 | base::TimeDelta::FromMilliseconds(1), |
| 521 | base::TimeDelta::FromMinutes(1), |
| 522 | 100); |
| 523 | } else if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_FULL) { |
| 524 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Full_Handshake", |
| 525 | connect_duration, |
| 526 | base::TimeDelta::FromMilliseconds(1), |
| 527 | base::TimeDelta::FromMinutes(1), |
| 528 | 100); |
| 529 | } |
| 530 | |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 531 | const std::string& host = params_->host_and_port().host(); |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 532 | bool is_google = |
| 533 | host == "google.com" || |
| 534 | (host.size() > 11 && host.rfind(".google.com") == host.size() - 11); |
[email protected] | f906bfe | 2011-06-09 16:35:24 | [diff] [blame] | 535 | if (is_google) { |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 536 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google2", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 537 | connect_duration, |
| 538 | base::TimeDelta::FromMilliseconds(1), |
[email protected] | 84727696 | 2013-03-08 23:53:17 | [diff] [blame] | 539 | base::TimeDelta::FromMinutes(1), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 540 | 100); |
[email protected] | b076d6c | 2011-06-29 14:47:51 | [diff] [blame] | 541 | if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_RESUME) { |
| 542 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google_" |
| 543 | "Resume_Handshake", |
| 544 | connect_duration, |
| 545 | base::TimeDelta::FromMilliseconds(1), |
| 546 | base::TimeDelta::FromMinutes(1), |
| 547 | 100); |
| 548 | } else if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_FULL) { |
| 549 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google_" |
| 550 | "Full_Handshake", |
| 551 | connect_duration, |
| 552 | base::TimeDelta::FromMilliseconds(1), |
| 553 | base::TimeDelta::FromMinutes(1), |
| 554 | 100); |
| 555 | } |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 556 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 557 | } |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 558 | |
| 559 | if (result == OK || IsCertificateError(result)) { |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 560 | SetSocket(ssl_socket_.PassAs<StreamSocket>()); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 561 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 562 | error_response_info_.cert_request_info = new SSLCertRequestInfo; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 563 | ssl_socket_->GetSSLCertRequestInfo( |
| 564 | error_response_info_.cert_request_info.get()); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 565 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 566 | |
| 567 | return result; |
| 568 | } |
| 569 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 570 | void SSLConnectJob::ResumeSSLConnection() { |
| 571 | DCHECK_EQ(next_state_, STATE_SSL_CONNECT); |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 572 | messenger_ = NULL; |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 573 | OnIOComplete(OK); |
| 574 | } |
| 575 | |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 576 | SSLConnectJob::State SSLConnectJob::GetInitialState( |
| 577 | SSLSocketParams::ConnectionType connection_type) { |
| 578 | switch (connection_type) { |
| 579 | case SSLSocketParams::DIRECT: |
| 580 | return STATE_TRANSPORT_CONNECT; |
| 581 | case SSLSocketParams::HTTP_PROXY: |
| 582 | return STATE_TUNNEL_CONNECT; |
| 583 | case SSLSocketParams::SOCKS_PROXY: |
| 584 | return STATE_SOCKS_CONNECT; |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 585 | } |
[email protected] | ea79ba9 | 2013-08-15 21:56:20 | [diff] [blame] | 586 | NOTREACHED(); |
| 587 | return STATE_NONE; |
| 588 | } |
| 589 | |
| 590 | int SSLConnectJob::ConnectInternal() { |
| 591 | next_state_ = GetInitialState(params_->GetConnectionType()); |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 592 | return DoLoop(OK); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | SSLClientSocketPool::SSLConnectJobFactory::SSLConnectJobFactory( |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 596 | TransportClientSocketPool* transport_pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 597 | SOCKSClientSocketPool* socks_pool, |
| 598 | HttpProxyClientSocketPool* http_proxy_pool, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 599 | ClientSocketFactory* client_socket_factory, |
| 600 | HostResolver* host_resolver, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 601 | const SSLClientSocketContext& context, |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 602 | const SSLConnectJob::GetMessengerCallback& get_messenger_callback, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 603 | NetLog* net_log) |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 604 | : transport_pool_(transport_pool), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 605 | socks_pool_(socks_pool), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 606 | http_proxy_pool_(http_proxy_pool), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 607 | client_socket_factory_(client_socket_factory), |
| 608 | host_resolver_(host_resolver), |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 609 | context_(context), |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 610 | get_messenger_callback_(get_messenger_callback), |
| 611 | net_log_(net_log) { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 612 | base::TimeDelta max_transport_timeout = base::TimeDelta(); |
| 613 | base::TimeDelta pool_timeout; |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 614 | if (transport_pool_) |
| 615 | max_transport_timeout = transport_pool_->ConnectionTimeout(); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 616 | if (socks_pool_) { |
| 617 | pool_timeout = socks_pool_->ConnectionTimeout(); |
| 618 | if (pool_timeout > max_transport_timeout) |
| 619 | max_transport_timeout = pool_timeout; |
| 620 | } |
| 621 | if (http_proxy_pool_) { |
| 622 | pool_timeout = http_proxy_pool_->ConnectionTimeout(); |
| 623 | if (pool_timeout > max_transport_timeout) |
| 624 | max_transport_timeout = pool_timeout; |
| 625 | } |
| 626 | timeout_ = max_transport_timeout + |
| 627 | base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds); |
| 628 | } |
| 629 | |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 630 | SSLClientSocketPool::SSLConnectJobFactory::~SSLConnectJobFactory() { |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 631 | } |
| 632 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 633 | SSLClientSocketPool::SSLClientSocketPool( |
| 634 | int max_sockets, |
| 635 | int max_sockets_per_group, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 636 | ClientSocketPoolHistograms* histograms, |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 637 | HostResolver* host_resolver, |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 638 | CertVerifier* cert_verifier, |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 639 | ChannelIDService* channel_id_service, |
[email protected] | a2a4197 | 2011-12-07 17:47:27 | [diff] [blame] | 640 | TransportSecurityState* transport_security_state, |
[email protected] | 284303b6 | 2013-11-28 15:11:54 | [diff] [blame] | 641 | CTVerifier* cert_transparency_verifier, |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 642 | const std::string& ssl_session_cache_shard, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 643 | ClientSocketFactory* client_socket_factory, |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 644 | TransportClientSocketPool* transport_pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 645 | SOCKSClientSocketPool* socks_pool, |
| 646 | HttpProxyClientSocketPool* http_proxy_pool, |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 647 | SSLConfigService* ssl_config_service, |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 648 | bool enable_ssl_connect_job_waiting, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 649 | NetLog* net_log) |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 650 | : transport_pool_(transport_pool), |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 651 | socks_pool_(socks_pool), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 652 | http_proxy_pool_(http_proxy_pool), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 653 | base_(this, |
| 654 | max_sockets, |
| 655 | max_sockets_per_group, |
| 656 | histograms, |
[email protected] | 82b8c96 | 2011-10-12 09:17:30 | [diff] [blame] | 657 | ClientSocketPool::unused_idle_socket_timeout(), |
| 658 | ClientSocketPool::used_idle_socket_timeout(), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 659 | new SSLConnectJobFactory( |
| 660 | transport_pool, |
| 661 | socks_pool, |
| 662 | http_proxy_pool, |
| 663 | client_socket_factory, |
| 664 | host_resolver, |
| 665 | SSLClientSocketContext(cert_verifier, |
| 666 | channel_id_service, |
| 667 | transport_security_state, |
| 668 | cert_transparency_verifier, |
| 669 | ssl_session_cache_shard), |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 670 | base::Bind( |
| 671 | &SSLClientSocketPool::GetOrCreateSSLConnectJobMessenger, |
| 672 | base::Unretained(this)), |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 673 | net_log)), |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 674 | ssl_config_service_(ssl_config_service), |
| 675 | enable_ssl_connect_job_waiting_(enable_ssl_connect_job_waiting) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 676 | if (ssl_config_service_.get()) |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 677 | ssl_config_service_->AddObserver(this); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 678 | if (transport_pool_) |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 679 | base_.AddLowerLayeredPool(transport_pool_); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 680 | if (socks_pool_) |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 681 | base_.AddLowerLayeredPool(socks_pool_); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 682 | if (http_proxy_pool_) |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 683 | base_.AddLowerLayeredPool(http_proxy_pool_); |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 684 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 685 | |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 686 | SSLClientSocketPool::~SSLClientSocketPool() { |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 687 | STLDeleteContainerPairSecondPointers(messenger_map_.begin(), |
| 688 | messenger_map_.end()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 689 | if (ssl_config_service_.get()) |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 690 | ssl_config_service_->RemoveObserver(this); |
| 691 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 692 | |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 693 | scoped_ptr<ConnectJob> SSLClientSocketPool::SSLConnectJobFactory::NewConnectJob( |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 694 | const std::string& group_name, |
| 695 | const PoolBase::Request& request, |
| 696 | ConnectJob::Delegate* delegate) const { |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 697 | return scoped_ptr<ConnectJob>(new SSLConnectJob(group_name, |
| 698 | request.priority(), |
| 699 | request.params(), |
| 700 | ConnectionTimeout(), |
| 701 | transport_pool_, |
| 702 | socks_pool_, |
| 703 | http_proxy_pool_, |
| 704 | client_socket_factory_, |
| 705 | host_resolver_, |
| 706 | context_, |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 707 | get_messenger_callback_, |
[email protected] | 8e45855 | 2014-08-05 00:02:15 | [diff] [blame] | 708 | delegate, |
| 709 | net_log_)); |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 710 | } |
| 711 | |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 712 | base::TimeDelta SSLClientSocketPool::SSLConnectJobFactory::ConnectionTimeout() |
| 713 | const { |
[email protected] | 2a848e0e | 2012-08-09 22:27:31 | [diff] [blame] | 714 | return timeout_; |
| 715 | } |
| 716 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 717 | int SSLClientSocketPool::RequestSocket(const std::string& group_name, |
| 718 | const void* socket_params, |
| 719 | RequestPriority priority, |
| 720 | ClientSocketHandle* handle, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 721 | const CompletionCallback& callback, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 722 | const BoundNetLog& net_log) { |
| 723 | const scoped_refptr<SSLSocketParams>* casted_socket_params = |
| 724 | static_cast<const scoped_refptr<SSLSocketParams>*>(socket_params); |
| 725 | |
| 726 | return base_.RequestSocket(group_name, *casted_socket_params, priority, |
| 727 | handle, callback, net_log); |
| 728 | } |
| 729 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 730 | void SSLClientSocketPool::RequestSockets( |
| 731 | const std::string& group_name, |
| 732 | const void* params, |
| 733 | int num_sockets, |
| 734 | const BoundNetLog& net_log) { |
| 735 | const scoped_refptr<SSLSocketParams>* casted_params = |
| 736 | static_cast<const scoped_refptr<SSLSocketParams>*>(params); |
| 737 | |
| 738 | base_.RequestSockets(group_name, *casted_params, num_sockets, net_log); |
| 739 | } |
| 740 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 741 | void SSLClientSocketPool::CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 742 | ClientSocketHandle* handle) { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 743 | base_.CancelRequest(group_name, handle); |
| 744 | } |
| 745 | |
| 746 | void SSLClientSocketPool::ReleaseSocket(const std::string& group_name, |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 747 | scoped_ptr<StreamSocket> socket, |
| 748 | int id) { |
| 749 | base_.ReleaseSocket(group_name, socket.Pass(), id); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 750 | } |
| 751 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 752 | void SSLClientSocketPool::FlushWithError(int error) { |
| 753 | base_.FlushWithError(error); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | void SSLClientSocketPool::CloseIdleSockets() { |
| 757 | base_.CloseIdleSockets(); |
| 758 | } |
| 759 | |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 760 | int SSLClientSocketPool::IdleSocketCount() const { |
| 761 | return base_.idle_socket_count(); |
| 762 | } |
| 763 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 764 | int SSLClientSocketPool::IdleSocketCountInGroup( |
| 765 | const std::string& group_name) const { |
| 766 | return base_.IdleSocketCountInGroup(group_name); |
| 767 | } |
| 768 | |
| 769 | LoadState SSLClientSocketPool::GetLoadState( |
| 770 | const std::string& group_name, const ClientSocketHandle* handle) const { |
| 771 | return base_.GetLoadState(group_name, handle); |
| 772 | } |
| 773 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 774 | base::DictionaryValue* SSLClientSocketPool::GetInfoAsValue( |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 775 | const std::string& name, |
| 776 | const std::string& type, |
| 777 | bool include_nested_pools) const { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 778 | base::DictionaryValue* dict = base_.GetInfoAsValue(name, type); |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 779 | if (include_nested_pools) { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 780 | base::ListValue* list = new base::ListValue(); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 781 | if (transport_pool_) { |
| 782 | list->Append(transport_pool_->GetInfoAsValue("transport_socket_pool", |
| 783 | "transport_socket_pool", |
| 784 | false)); |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 785 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 786 | if (socks_pool_) { |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 787 | list->Append(socks_pool_->GetInfoAsValue("socks_pool", |
| 788 | "socks_pool", |
| 789 | true)); |
| 790 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 791 | if (http_proxy_pool_) { |
| 792 | list->Append(http_proxy_pool_->GetInfoAsValue("http_proxy_pool", |
| 793 | "http_proxy_pool", |
| 794 | true)); |
| 795 | } |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 796 | dict->Set("nested_pools", list); |
| 797 | } |
| 798 | return dict; |
| 799 | } |
| 800 | |
[email protected] | ddb1e5a | 2010-12-13 20:10:45 | [diff] [blame] | 801 | base::TimeDelta SSLClientSocketPool::ConnectionTimeout() const { |
| 802 | return base_.ConnectionTimeout(); |
| 803 | } |
| 804 | |
| 805 | ClientSocketPoolHistograms* SSLClientSocketPool::histograms() const { |
| 806 | return base_.histograms(); |
| 807 | } |
| 808 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 809 | bool SSLClientSocketPool::IsStalled() const { |
| 810 | return base_.IsStalled(); |
| 811 | } |
| 812 | |
| 813 | void SSLClientSocketPool::AddHigherLayeredPool(HigherLayeredPool* higher_pool) { |
| 814 | base_.AddHigherLayeredPool(higher_pool); |
| 815 | } |
| 816 | |
| 817 | void SSLClientSocketPool::RemoveHigherLayeredPool( |
| 818 | HigherLayeredPool* higher_pool) { |
| 819 | base_.RemoveHigherLayeredPool(higher_pool); |
[email protected] | ad74a59 | 2011-01-21 18:40:55 | [diff] [blame] | 820 | } |
| 821 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 822 | bool SSLClientSocketPool::CloseOneIdleConnection() { |
| 823 | if (base_.CloseOneIdleSocket()) |
| 824 | return true; |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 825 | return base_.CloseOneIdleConnectionInHigherLayeredPool(); |
| 826 | } |
| 827 | |
[email protected] | cffd7f9 | 2014-08-21 21:30:50 | [diff] [blame^] | 828 | SSLConnectJobMessenger* SSLClientSocketPool::GetOrCreateSSLConnectJobMessenger( |
| 829 | const std::string& cache_key) { |
| 830 | if (!enable_ssl_connect_job_waiting_) |
| 831 | return NULL; |
| 832 | MessengerMap::const_iterator it = messenger_map_.find(cache_key); |
| 833 | if (it == messenger_map_.end()) { |
| 834 | std::pair<MessengerMap::iterator, bool> iter = |
| 835 | messenger_map_.insert(MessengerMap::value_type( |
| 836 | cache_key, |
| 837 | new SSLConnectJobMessenger( |
| 838 | base::Bind(&SSLClientSocketPool::DeleteSSLConnectJobMessenger, |
| 839 | base::Unretained(this), |
| 840 | cache_key)))); |
| 841 | it = iter.first; |
| 842 | } |
| 843 | return it->second; |
| 844 | } |
| 845 | |
| 846 | void SSLClientSocketPool::DeleteSSLConnectJobMessenger( |
| 847 | const std::string& cache_key) { |
| 848 | MessengerMap::iterator it = messenger_map_.find(cache_key); |
| 849 | CHECK(it != messenger_map_.end()); |
| 850 | delete it->second; |
| 851 | messenger_map_.erase(it); |
| 852 | } |
| 853 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 854 | void SSLClientSocketPool::OnSSLConfigChanged() { |
| 855 | FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 856 | } |
| 857 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 858 | } // namespace net |