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 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::SSLConnectJobMessenger() : weak_factory_(this) { | |
99 } | |
100 | |
97 bool SSLConnectJobMessenger::CanProceed(SSLClientSocket* ssl_socket) { | 101 bool SSLConnectJobMessenger::CanProceed(SSLClientSocket* ssl_socket) { |
98 // If the session is in the session cache, or there are no connecting | 102 // If the session is in the session cache, or there are no connecting |
99 // sockets allow the connection to proceed. | 103 // sockets allow the connection to proceed. |
100 if (ssl_socket->InSessionCache() || connecting_sockets_.empty()) { | 104 if (ssl_socket->InSessionCache() || connecting_sockets_.empty()) { |
101 return true; | 105 return true; |
102 } | 106 } |
103 return false; | 107 return false; |
104 } | 108 } |
105 | 109 |
106 void SSLConnectJobMessenger::MonitorConnectionResult( | 110 void SSLConnectJobMessenger::MonitorConnectionResult( |
107 SSLClientSocket* ssl_socket) { | 111 SSLClientSocket* ssl_socket) { |
108 connecting_sockets_.push_back(ssl_socket); | 112 connecting_sockets_.push_back(ssl_socket); |
109 ssl_socket->SetIsLeader(); | 113 ssl_socket->SetIsLeader(); |
110 ssl_socket->SetSocketFailureCallback( | 114 // SSLConnectJobMessenger weak_ptrs are used here to ensure that |
wtc
2014/07/11 18:55:51
weak_ptrs => "weak pointers" or "WeakPtrs"
mshelley
2014/07/14 20:30:06
Done.
| |
111 base::Bind(&SSLConnectJobMessenger::OnJobFailed, base::Unretained(this))); | 115 // tasks posted with these callbacks are deregistered if the |
116 // SSLConnectJobMessenger should become invalid before they're run. | |
117 ssl_socket->SetSocketFailureCallback(base::Bind( | |
118 &SSLConnectJobMessenger::OnJobFailed, weak_factory_.GetWeakPtr())); | |
112 ssl_socket->WatchSessionForCompletion(base::Bind( | 119 ssl_socket->WatchSessionForCompletion(base::Bind( |
113 &SSLConnectJobMessenger::OnJobSucceeded, base::Unretained(this))); | 120 &SSLConnectJobMessenger::OnJobSucceeded, weak_factory_.GetWeakPtr())); |
114 } | 121 } |
115 | 122 |
116 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, | 123 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, |
117 const base::Closure& callback) { | 124 const base::Closure& callback) { |
118 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); | 125 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); |
119 } | 126 } |
120 | 127 |
121 void SSLConnectJobMessenger::OnJobSucceeded() { | 128 void SSLConnectJobMessenger::OnJobSucceeded() { |
122 SSLPendingSocketsAndCallbacks temp_list = pending_sockets_and_callbacks_; | 129 SSLPendingSocketsAndCallbacks temp_list = pending_sockets_and_callbacks_; |
123 pending_sockets_and_callbacks_.clear(); | 130 pending_sockets_and_callbacks_.clear(); |
124 connecting_sockets_.clear(); | 131 connecting_sockets_.clear(); |
125 RunAllJobs(temp_list); | 132 RunAllJobs(temp_list); |
126 } | 133 } |
127 | 134 |
128 void SSLConnectJobMessenger::OnJobFailed() { | 135 void SSLConnectJobMessenger::OnJobFailed() { |
129 if (pending_sockets_and_callbacks_.empty()) | 136 base::Closure callback = base::Bind(&SSLConnectJobMessenger::ConnectNewLeader, |
137 weak_factory_.GetWeakPtr()); | |
138 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | |
139 } | |
140 | |
141 void SSLConnectJobMessenger::ConnectNewLeader() { | |
142 connecting_sockets_.erase(connecting_sockets_.begin()); | |
143 std::vector<SocketAndCallback>::iterator it; | |
144 SSLClientSocket* ssl_socket; | |
145 base::Closure callback; | |
146 | |
147 // Connect the first pending socket that has not been deleted. | |
148 for (it = pending_sockets_and_callbacks_.begin(); | |
149 it != pending_sockets_and_callbacks_.end(); | |
150 ++it) { | |
151 if (it->socket != NULL) { | |
wtc
2014/07/11 18:55:51
IMPORTANT: this null check for |it->socket| doesn'
mshelley
2014/07/14 20:30:06
This has been corrected now in https://codereview.
| |
152 ssl_socket = it->socket; | |
153 callback = it->callback; | |
154 break; | |
155 } | |
156 } | |
157 | |
158 // If there were no valid pending sockets, return. | |
159 if (it == pending_sockets_and_callbacks_.end()) | |
130 return; | 160 return; |
131 base::Closure callback = pending_sockets_and_callbacks_[0].callback; | 161 |
132 connecting_sockets_.erase(connecting_sockets_.begin()); | 162 // Erase all deleted sockets and their callbacks, as well as the socket that |
133 SSLClientSocket* ssl_socket = pending_sockets_and_callbacks_[0].socket; | 163 // will |
134 pending_sockets_and_callbacks_.erase(pending_sockets_and_callbacks_.begin()); | 164 // be connected next. |
Ryan Sleevi
2014/07/11 01:25:59
comment formating
mshelley
2014/07/14 20:30:06
Done.
| |
165 pending_sockets_and_callbacks_.erase(pending_sockets_and_callbacks_.begin(), | |
166 ++it); | |
167 | |
135 MonitorConnectionResult(ssl_socket); | 168 MonitorConnectionResult(ssl_socket); |
136 callback.Run(); | 169 callback.Run(); |
wtc
2014/07/11 18:55:51
IMPORTANT: I have to admit I don't understand why
mshelley
2014/07/14 20:30:06
The original concern was this:
Say I have pendin
| |
137 } | 170 } |
138 | 171 |
139 void SSLConnectJobMessenger::RunAllJobs( | 172 void SSLConnectJobMessenger::RunAllJobs( |
140 std::vector<SocketAndCallback>& pending_sockets_and_callbacks) { | 173 std::vector<SocketAndCallback>& pending_sockets_and_callbacks) { |
174 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | |
175 base::ThreadTaskRunnerHandle::Get(); | |
141 for (std::vector<SocketAndCallback>::const_iterator it = | 176 for (std::vector<SocketAndCallback>::const_iterator it = |
142 pending_sockets_and_callbacks.begin(); | 177 pending_sockets_and_callbacks.begin(); |
143 it != pending_sockets_and_callbacks.end(); | 178 it != pending_sockets_and_callbacks.end(); |
144 ++it) | 179 ++it) |
145 it->callback.Run(); | 180 task_runner->PostTask(FROM_HERE, it->callback); |
wtc
2014/07/11 18:55:51
Please add curly braces because the loop constrain
mshelley
2014/07/14 20:30:06
Done.
| |
146 } | 181 } |
147 | 182 |
148 // Timeout for the SSL handshake portion of the connect. | 183 // Timeout for the SSL handshake portion of the connect. |
149 static const int kSSLHandshakeTimeoutInSeconds = 30; | 184 static const int kSSLHandshakeTimeoutInSeconds = 30; |
150 | 185 |
151 SSLConnectJob::SSLConnectJob(const std::string& group_name, | 186 SSLConnectJob::SSLConnectJob(const std::string& group_name, |
152 RequestPriority priority, | 187 RequestPriority priority, |
153 const scoped_refptr<SSLSocketParams>& params, | 188 const scoped_refptr<SSLSocketParams>& params, |
154 const base::TimeDelta& timeout_duration, | 189 const base::TimeDelta& timeout_duration, |
155 TransportClientSocketPool* transport_pool, | 190 TransportClientSocketPool* transport_pool, |
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
817 // static | 852 // static |
818 bool SSLClientSocketPool::GetEnableConnectJobWaiting() { | 853 bool SSLClientSocketPool::GetEnableConnectJobWaiting() { |
819 return enable_connect_job_waiting_; | 854 return enable_connect_job_waiting_; |
820 } | 855 } |
821 | 856 |
822 void SSLClientSocketPool::OnSSLConfigChanged() { | 857 void SSLClientSocketPool::OnSSLConfigChanged() { |
823 FlushWithError(ERR_NETWORK_CHANGED); | 858 FlushWithError(ERR_NETWORK_CHANGED); |
824 } | 859 } |
825 | 860 |
826 } // namespace net | 861 } // namespace net |
OLD | NEW |