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 // SSLConnectJobMessenger weak pointers are used here to ensure that |
129 base::Bind(&SSLConnectJobMessenger::OnJobFailed, base::Unretained(this))); | 133 // tasks posted with these callbacks are deregistered if the |
134 // SSLConnectJobMessenger should become invalid before they're run. | |
Ryan Sleevi
2014/07/18 23:08:35
Probably unnecessary; this is generally understood
mshelley
2014/07/21 23:08:28
Done.
| |
135 ssl_socket->SetHandshakeFailureCallback(base::Bind( | |
136 &SSLConnectJobMessenger::OnJobFailed, weak_factory_.GetWeakPtr())); | |
130 ssl_socket->SetHandshakeSuccessCallback(base::Bind( | 137 ssl_socket->SetHandshakeSuccessCallback(base::Bind( |
131 &SSLConnectJobMessenger::OnJobSucceeded, base::Unretained(this))); | 138 &SSLConnectJobMessenger::OnJobSucceeded, weak_factory_.GetWeakPtr())); |
132 } | 139 } |
133 | 140 |
134 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, | 141 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, |
135 const base::Closure& callback) { | 142 const base::Closure& callback) { |
136 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); | 143 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); |
137 } | 144 } |
138 | 145 |
139 void SSLConnectJobMessenger::OnJobSucceeded() { | 146 void SSLConnectJobMessenger::OnJobSucceeded() { |
140 SSLPendingSocketsAndCallbacks temp_list = pending_sockets_and_callbacks_; | 147 SSLPendingSocketsAndCallbacks temp_list = pending_sockets_and_callbacks_; |
141 pending_sockets_and_callbacks_.clear(); | 148 pending_sockets_and_callbacks_.clear(); |
142 connecting_sockets_.clear(); | 149 connecting_sockets_.clear(); |
143 RunAllCallbacks(temp_list); | 150 RunAllCallbacks(temp_list); |
144 } | 151 } |
145 | 152 |
146 void SSLConnectJobMessenger::OnJobFailed() { | 153 void SSLConnectJobMessenger::OnJobFailed() { |
154 connecting_sockets_.erase(connecting_sockets_.begin()); | |
155 | |
156 // If there were no valid pending sockets, return. | |
147 if (pending_sockets_and_callbacks_.empty()) | 157 if (pending_sockets_and_callbacks_.empty()) |
148 return; | 158 return; |
149 base::Closure callback = pending_sockets_and_callbacks_[0].callback; | 159 std::vector<SocketAndCallback>::iterator it = |
150 connecting_sockets_.erase(connecting_sockets_.begin()); | 160 pending_sockets_and_callbacks_.begin(); |
151 SSLClientSocket* ssl_socket = pending_sockets_and_callbacks_[0].socket; | 161 |
152 pending_sockets_and_callbacks_.erase(pending_sockets_and_callbacks_.begin()); | 162 SSLClientSocket* ssl_socket = it->socket; |
163 base::Closure& callback = it->callback; | |
Ryan Sleevi
2014/07/18 23:08:35
SECURITY BUG: You're taking a reference to it-> he
mshelley
2014/07/21 23:08:29
Done.
| |
164 | |
165 pending_sockets_and_callbacks_.erase(it); | |
166 | |
153 MonitorConnectionResult(ssl_socket); | 167 MonitorConnectionResult(ssl_socket); |
154 callback.Run(); | 168 |
169 // PostTask is used to avoid the recursive call sequence caused | |
170 // by SSLConnectJobMessengers running the callback directly. | |
171 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | |
155 } | 172 } |
156 | 173 |
157 void SSLConnectJobMessenger::RunAllCallbacks( | 174 void SSLConnectJobMessenger::RunAllCallbacks( |
158 const SSLPendingSocketsAndCallbacks& pending_sockets_and_callbacks) { | 175 const SSLPendingSocketsAndCallbacks& pending_sockets_and_callbacks) { |
176 scoped_refptr<base::TaskRunner> task_runner = | |
Ryan Sleevi
2014/07/18 23:08:35
Here I think you want SequencedTaskRunner
It's mo
mshelley
2014/07/21 23:08:28
Done.
| |
177 base::ThreadTaskRunnerHandle::Get(); | |
159 for (std::vector<SocketAndCallback>::const_iterator it = | 178 for (std::vector<SocketAndCallback>::const_iterator it = |
160 pending_sockets_and_callbacks.begin(); | 179 pending_sockets_and_callbacks.begin(); |
161 it != pending_sockets_and_callbacks.end(); | 180 it != pending_sockets_and_callbacks.end(); |
162 ++it) | 181 ++it) { |
163 it->callback.Run(); | 182 task_runner->PostTask(FROM_HERE, it->callback); |
183 } | |
164 } | 184 } |
165 | 185 |
166 // Timeout for the SSL handshake portion of the connect. | 186 // Timeout for the SSL handshake portion of the connect. |
167 static const int kSSLHandshakeTimeoutInSeconds = 30; | 187 static const int kSSLHandshakeTimeoutInSeconds = 30; |
168 | 188 |
169 SSLConnectJob::SSLConnectJob(const std::string& group_name, | 189 SSLConnectJob::SSLConnectJob(const std::string& group_name, |
170 RequestPriority priority, | 190 RequestPriority priority, |
171 const scoped_refptr<SSLSocketParams>& params, | 191 const scoped_refptr<SSLSocketParams>& params, |
172 const base::TimeDelta& timeout_duration, | 192 const base::TimeDelta& timeout_duration, |
173 TransportClientSocketPool* transport_pool, | 193 TransportClientSocketPool* transport_pool, |
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
834 if (base_.CloseOneIdleSocket()) | 854 if (base_.CloseOneIdleSocket()) |
835 return true; | 855 return true; |
836 return base_.CloseOneIdleConnectionInHigherLayeredPool(); | 856 return base_.CloseOneIdleConnectionInHigherLayeredPool(); |
837 } | 857 } |
838 | 858 |
839 void SSLClientSocketPool::OnSSLConfigChanged() { | 859 void SSLClientSocketPool::OnSSLConfigChanged() { |
840 FlushWithError(ERR_NETWORK_CHANGED); | 860 FlushWithError(ERR_NETWORK_CHANGED); |
841 } | 861 } |
842 | 862 |
843 } // namespace net | 863 } // namespace net |
OLD | NEW |