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" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 DCHECK_EQ(GetConnectionType(), SOCKS_PROXY); | 87 DCHECK_EQ(GetConnectionType(), SOCKS_PROXY); |
88 return socks_proxy_params_; | 88 return socks_proxy_params_; |
89 } | 89 } |
90 | 90 |
91 const scoped_refptr<HttpProxySocketParams>& | 91 const scoped_refptr<HttpProxySocketParams>& |
92 SSLSocketParams::GetHttpProxyConnectionParams() const { | 92 SSLSocketParams::GetHttpProxyConnectionParams() const { |
93 DCHECK_EQ(GetConnectionType(), HTTP_PROXY); | 93 DCHECK_EQ(GetConnectionType(), HTTP_PROXY); |
94 return http_proxy_params_; | 94 return http_proxy_params_; |
95 } | 95 } |
96 | 96 |
97 SSLConnectJobMessenger::SSLConnectJobMessenger() : weak_factory_(this) { | |
98 } | |
99 | |
97 bool SSLConnectJobMessenger::CanProceed(SSLClientSocket* ssl_socket) { | 100 bool SSLConnectJobMessenger::CanProceed(SSLClientSocket* ssl_socket) { |
98 // If the session is in the session cache, or there are no connecting | 101 // If the session is in the session cache, or there are no connecting |
99 // sockets allow the connection to proceed. | 102 // sockets allow the connection to proceed. |
100 if (ssl_socket->InSessionCache() || connecting_sockets_.empty()) { | 103 if (ssl_socket->InSessionCache() || connecting_sockets_.empty()) { |
101 return true; | 104 return true; |
102 } | 105 } |
103 return false; | 106 return false; |
104 } | 107 } |
105 | 108 |
106 void SSLConnectJobMessenger::MonitorConnectionResult( | 109 void SSLConnectJobMessenger::MonitorConnectionResult( |
107 SSLClientSocket* ssl_socket) { | 110 SSLClientSocket* ssl_socket) { |
111 connecting_sockets_.push_back(ssl_socket); | |
108 ssl_socket->SetIsLeader(); | 112 ssl_socket->SetIsLeader(); |
109 ssl_socket->SetSocketFailureCallback( | 113 ssl_socket->SetSocketFailureCallback(base::Bind( |
110 base::Bind(&SSLConnectJobMessenger::OnJobFailed, base::Unretained(this))); | 114 &SSLConnectJobMessenger::OnJobFailed, weak_factory_.GetWeakPtr())); |
111 ssl_socket->WatchSessionForCompletion(base::Bind( | 115 ssl_socket->WatchSessionForCompletion(base::Bind( |
112 &SSLConnectJobMessenger::OnJobSucceeded, base::Unretained(this))); | 116 &SSLConnectJobMessenger::OnJobSucceeded, weak_factory_.GetWeakPtr())); |
Ryan Sleevi
2014/07/10 00:03:22
It would be good to document why you're using Weak
mshelley
2014/07/10 00:44:20
Done.
| |
113 } | 117 } |
114 | 118 |
115 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, | 119 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, |
116 const base::Closure& callback) { | 120 const base::Closure& callback) { |
117 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); | 121 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); |
118 } | 122 } |
119 | 123 |
120 void SSLConnectJobMessenger::OnJobSucceeded() { | 124 void SSLConnectJobMessenger::OnJobSucceeded() { |
121 SSLPendingSocketsAndCallbacks temp_list = pending_sockets_and_callbacks_; | 125 SSLPendingSocketsAndCallbacks temp_list = pending_sockets_and_callbacks_; |
122 pending_sockets_and_callbacks_.clear(); | 126 pending_sockets_and_callbacks_.clear(); |
123 connecting_sockets_.clear(); | 127 connecting_sockets_.clear(); |
124 RunAllJobs(temp_list); | 128 RunAllJobs(temp_list); |
125 } | 129 } |
126 | 130 |
127 void SSLConnectJobMessenger::OnJobFailed() { | 131 void SSLConnectJobMessenger::OnJobFailed() { |
128 if (pending_sockets_and_callbacks_.empty()) | 132 base::Closure callback = base::Bind(&SSLConnectJobMessenger::ConnectNewLeader, |
133 weak_factory_.GetWeakPtr()); | |
134 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | |
135 } | |
136 | |
137 void SSLConnectJobMessenger::ConnectNewLeader() { | |
138 connecting_sockets_.erase(connecting_sockets_.begin()); | |
139 SSLClientSocket* ssl_socket; | |
140 base::Closure callback; | |
141 std::vector<SocketAndCallback>::iterator it; | |
Ryan Sleevi
2014/07/10 00:03:22
anal retentive: Would nice to have declaration ord
mshelley
2014/07/10 00:44:20
Done.
| |
142 | |
143 // Connect the first pending socket that has not been deleted. | |
144 for (it = pending_sockets_and_callbacks_.begin(); | |
145 it != pending_sockets_and_callbacks_.end(); | |
146 ++it) { | |
147 if (it->socket != NULL) { | |
148 ssl_socket = it->socket; | |
149 callback = it->callback; | |
150 break; | |
151 } | |
152 } | |
153 | |
154 // If there were no valid pending sockets, return. | |
155 if (it == pending_sockets_and_callbacks_.end()) | |
129 return; | 156 return; |
130 base::Closure callback = pending_sockets_and_callbacks_[0].callback; | 157 |
131 connecting_sockets_.erase(connecting_sockets_.begin()); | 158 // Erase all deleted sockets and their callbacks, as well as the socket that |
132 SSLClientSocket* ssl_socket = pending_sockets_and_callbacks_[0].socket; | 159 // will |
133 pending_sockets_and_callbacks_.erase(pending_sockets_and_callbacks_.begin()); | 160 // be connected next. |
161 pending_sockets_and_callbacks_.erase(pending_sockets_and_callbacks_.begin(), | |
162 ++it); | |
163 | |
134 MonitorConnectionResult(ssl_socket); | 164 MonitorConnectionResult(ssl_socket); |
135 callback.Run(); | 165 callback.Run(); |
136 } | 166 } |
137 | 167 |
138 void SSLConnectJobMessenger::RunAllJobs( | 168 void SSLConnectJobMessenger::RunAllJobs( |
139 std::vector<SocketAndCallback>& pending_sockets_and_callbacks) { | 169 std::vector<SocketAndCallback>& pending_sockets_and_callbacks) { |
140 for (std::vector<SocketAndCallback>::const_iterator it = | 170 for (std::vector<SocketAndCallback>::const_iterator it = |
141 pending_sockets_and_callbacks.begin(); | 171 pending_sockets_and_callbacks.begin(); |
142 it != pending_sockets_and_callbacks.end(); | 172 it != pending_sockets_and_callbacks.end(); |
143 ++it) | 173 ++it) |
144 it->callback.Run(); | 174 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, it->callback); |
Ryan Sleevi
2014/07/10 00:03:22
Lifetime question: This seems another situation wh
mshelley
2014/07/10 00:44:20
I believe the only way that one of these callbacks
| |
145 } | 175 } |
146 | 176 |
147 // Timeout for the SSL handshake portion of the connect. | 177 // Timeout for the SSL handshake portion of the connect. |
148 static const int kSSLHandshakeTimeoutInSeconds = 30; | 178 static const int kSSLHandshakeTimeoutInSeconds = 30; |
149 | 179 |
150 SSLConnectJob::SSLConnectJob(const std::string& group_name, | 180 SSLConnectJob::SSLConnectJob(const std::string& group_name, |
151 RequestPriority priority, | 181 RequestPriority priority, |
152 const scoped_refptr<SSLSocketParams>& params, | 182 const scoped_refptr<SSLSocketParams>& params, |
153 const base::TimeDelta& timeout_duration, | 183 const base::TimeDelta& timeout_duration, |
154 TransportClientSocketPool* transport_pool, | 184 TransportClientSocketPool* transport_pool, |
(...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
803 if (base_.CloseOneIdleSocket()) | 833 if (base_.CloseOneIdleSocket()) |
804 return true; | 834 return true; |
805 return base_.CloseOneIdleConnectionInHigherLayeredPool(); | 835 return base_.CloseOneIdleConnectionInHigherLayeredPool(); |
806 } | 836 } |
807 | 837 |
808 void SSLClientSocketPool::OnSSLConfigChanged() { | 838 void SSLClientSocketPool::OnSSLConfigChanged() { |
809 FlushWithError(ERR_NETWORK_CHANGED); | 839 FlushWithError(ERR_NETWORK_CHANGED); |
810 } | 840 } |
811 | 841 |
812 } // namespace net | 842 } // namespace net |
OLD | NEW |