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/thread_task_runner_handle.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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 pending_sockets_and_callbacks_.begin(); | 109 pending_sockets_and_callbacks_.begin(); |
| 109 it != pending_sockets_and_callbacks_.end(); | 110 it != pending_sockets_and_callbacks_.end(); |
| 110 ++it) { | 111 ++it) { |
| 111 if (it->socket == ssl_socket) { | 112 if (it->socket == ssl_socket) { |
| 112 pending_sockets_and_callbacks_.erase(it); | 113 pending_sockets_and_callbacks_.erase(it); |
| 113 break; | 114 break; |
| 114 } | 115 } |
| 115 } | 116 } |
| 116 } | 117 } |
| 117 | 118 |
| 119 SSLConnectJobMessenger::SSLConnectJobMessenger() : weak_factory_(this) { | |
| 120 } | |
| 121 | |
| 118 bool SSLConnectJobMessenger::CanProceed(SSLClientSocket* ssl_socket) { | 122 bool SSLConnectJobMessenger::CanProceed(SSLClientSocket* ssl_socket) { |
| 119 // If the session is in the session cache, or there are no connecting | 123 // If the session is in the session cache, or there are no connecting |
| 120 // sockets allow the connection to proceed. | 124 // sockets allow the connection to proceed. |
| 121 return ssl_socket->InSessionCache() || connecting_sockets_.empty(); | 125 return ssl_socket->InSessionCache() || connecting_sockets_.empty(); |
| 122 } | 126 } |
| 123 | 127 |
| 124 void SSLConnectJobMessenger::MonitorConnectionResult( | 128 void SSLConnectJobMessenger::MonitorConnectionResult( |
| 125 SSLClientSocket* ssl_socket) { | 129 SSLClientSocket* ssl_socket) { |
| 126 connecting_sockets_.push_back(ssl_socket); | 130 connecting_sockets_.push_back(ssl_socket); |
| 127 // TODO(mshelley): Both of these callbacks will use weak_ptr in future CL. | 131 |
| 128 ssl_socket->SetHandshakeFailureCallback( | 132 ssl_socket->SetHandshakeFailureCallback(base::Bind( |
| 129 base::Bind(&SSLConnectJobMessenger::OnJobFailed, base::Unretained(this))); | 133 &SSLConnectJobMessenger::OnJobFailed, weak_factory_.GetWeakPtr())); |
| 130 ssl_socket->SetHandshakeSuccessCallback(base::Bind( | 134 ssl_socket->SetHandshakeSuccessCallback(base::Bind( |
| 131 &SSLConnectJobMessenger::OnJobSucceeded, base::Unretained(this))); | 135 &SSLConnectJobMessenger::OnJobSucceeded, weak_factory_.GetWeakPtr())); |
| 132 } | 136 } |
| 133 | 137 |
| 134 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, | 138 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, |
| 135 const base::Closure& callback) { | 139 const base::Closure& callback) { |
| 136 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); | 140 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); |
| 137 } | 141 } |
| 138 | 142 |
| 139 void SSLConnectJobMessenger::OnJobSucceeded() { | 143 void SSLConnectJobMessenger::OnJobSucceeded() { |
| 140 SSLPendingSocketsAndCallbacks temp_list = pending_sockets_and_callbacks_; | 144 SSLPendingSocketsAndCallbacks temp_list = pending_sockets_and_callbacks_; |
| 141 pending_sockets_and_callbacks_.clear(); | 145 pending_sockets_and_callbacks_.clear(); |
| 142 connecting_sockets_.clear(); | 146 connecting_sockets_.clear(); |
| 143 RunAllCallbacks(temp_list); | 147 RunAllCallbacks(temp_list); |
| 144 } | 148 } |
| 145 | 149 |
| 146 void SSLConnectJobMessenger::OnJobFailed() { | 150 void SSLConnectJobMessenger::OnJobFailed() { |
| 151 connecting_sockets_.erase(connecting_sockets_.begin()); | |
| 152 | |
| 153 // If there were no valid pending sockets, return. | |
| 147 if (pending_sockets_and_callbacks_.empty()) | 154 if (pending_sockets_and_callbacks_.empty()) |
| 148 return; | 155 return; |
| 149 base::Closure callback = pending_sockets_and_callbacks_[0].callback; | 156 |
| 150 connecting_sockets_.erase(connecting_sockets_.begin()); | 157 SocketAndCallback socket_and_callback = |
| 151 SSLClientSocket* ssl_socket = pending_sockets_and_callbacks_[0].socket; | 158 pending_sockets_and_callbacks_.front(); |
| 152 pending_sockets_and_callbacks_.erase(pending_sockets_and_callbacks_.begin()); | 159 pending_sockets_and_callbacks_.erase(pending_sockets_and_callbacks_.begin()); |
| 153 MonitorConnectionResult(ssl_socket); | 160 |
| 154 callback.Run(); | 161 MonitorConnectionResult(socket_and_callback.socket); |
| 162 | |
| 163 // PostTask is used to avoid the recursive call sequence caused | |
| 164 // by SSLConnectJobMessengers running the callback directly. | |
| 165 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 166 socket_and_callback.callback); | |
|
mmenke
2014/07/22 18:06:32
Hrm... Looks like we're fine if the second socket
mshelley
2014/07/23 22:09:07
Just to confirm: by "second case" you mean the cas
| |
| 155 } | 167 } |
| 156 | 168 |
| 157 void SSLConnectJobMessenger::RunAllCallbacks( | 169 void SSLConnectJobMessenger::RunAllCallbacks( |
| 158 const SSLPendingSocketsAndCallbacks& pending_sockets_and_callbacks) { | 170 const SSLPendingSocketsAndCallbacks& pending_sockets_and_callbacks) { |
| 171 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
| 172 base::ThreadTaskRunnerHandle::Get(); | |
| 159 for (std::vector<SocketAndCallback>::const_iterator it = | 173 for (std::vector<SocketAndCallback>::const_iterator it = |
| 160 pending_sockets_and_callbacks.begin(); | 174 pending_sockets_and_callbacks.begin(); |
| 161 it != pending_sockets_and_callbacks.end(); | 175 it != pending_sockets_and_callbacks.end(); |
| 162 ++it) | 176 ++it) { |
| 163 it->callback.Run(); | 177 task_runner->PostTask(FROM_HERE, it->callback); |
| 178 } | |
| 164 } | 179 } |
| 165 | 180 |
| 166 // Timeout for the SSL handshake portion of the connect. | 181 // Timeout for the SSL handshake portion of the connect. |
| 167 static const int kSSLHandshakeTimeoutInSeconds = 30; | 182 static const int kSSLHandshakeTimeoutInSeconds = 30; |
| 168 | 183 |
| 169 SSLConnectJob::SSLConnectJob(const std::string& group_name, | 184 SSLConnectJob::SSLConnectJob(const std::string& group_name, |
| 170 RequestPriority priority, | 185 RequestPriority priority, |
| 171 const scoped_refptr<SSLSocketParams>& params, | 186 const scoped_refptr<SSLSocketParams>& params, |
| 172 const base::TimeDelta& timeout_duration, | 187 const base::TimeDelta& timeout_duration, |
| 173 TransportClientSocketPool* transport_pool, | 188 TransportClientSocketPool* transport_pool, |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 834 if (base_.CloseOneIdleSocket()) | 849 if (base_.CloseOneIdleSocket()) |
| 835 return true; | 850 return true; |
| 836 return base_.CloseOneIdleConnectionInHigherLayeredPool(); | 851 return base_.CloseOneIdleConnectionInHigherLayeredPool(); |
| 837 } | 852 } |
| 838 | 853 |
| 839 void SSLClientSocketPool::OnSSLConfigChanged() { | 854 void SSLClientSocketPool::OnSSLConfigChanged() { |
| 840 FlushWithError(ERR_NETWORK_CHANGED); | 855 FlushWithError(ERR_NETWORK_CHANGED); |
| 841 } | 856 } |
| 842 | 857 |
| 843 } // namespace net | 858 } // namespace net |
| OLD | NEW |