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) { |
| 112 connecting_sockets_.push_back(ssl_socket); |
108 ssl_socket->SetIsLeader(); | 113 ssl_socket->SetIsLeader(); |
109 ssl_socket->SetSocketFailureCallback( | 114 // SSLConnectJobMessenger weak_ptrs are used here to ensure that |
110 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())); |
111 ssl_socket->WatchSessionForCompletion(base::Bind( | 119 ssl_socket->WatchSessionForCompletion(base::Bind( |
112 &SSLConnectJobMessenger::OnJobSucceeded, base::Unretained(this))); | 120 &SSLConnectJobMessenger::OnJobSucceeded, weak_factory_.GetWeakPtr())); |
113 } | 121 } |
114 | 122 |
115 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, | 123 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket* socket, |
116 const base::Closure& callback) { | 124 const base::Closure& callback) { |
117 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); | 125 pending_sockets_and_callbacks_.push_back(SocketAndCallback(socket, callback)); |
118 } | 126 } |
119 | 127 |
120 void SSLConnectJobMessenger::OnJobSucceeded() { | 128 void SSLConnectJobMessenger::OnJobSucceeded() { |
121 SSLPendingSocketsAndCallbacks temp_list = pending_sockets_and_callbacks_; | 129 SSLPendingSocketsAndCallbacks temp_list = pending_sockets_and_callbacks_; |
122 pending_sockets_and_callbacks_.clear(); | 130 pending_sockets_and_callbacks_.clear(); |
123 connecting_sockets_.clear(); | 131 connecting_sockets_.clear(); |
124 RunAllJobs(temp_list); | 132 RunAllJobs(temp_list); |
125 } | 133 } |
126 | 134 |
127 void SSLConnectJobMessenger::OnJobFailed() { | 135 void SSLConnectJobMessenger::OnJobFailed() { |
128 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) { |
| 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()) |
129 return; | 160 return; |
130 base::Closure callback = pending_sockets_and_callbacks_[0].callback; | 161 |
131 connecting_sockets_.erase(connecting_sockets_.begin()); | 162 // Erase all deleted sockets and their callbacks, as well as the socket that |
132 SSLClientSocket* ssl_socket = pending_sockets_and_callbacks_[0].socket; | 163 // will |
133 pending_sockets_and_callbacks_.erase(pending_sockets_and_callbacks_.begin()); | 164 // be connected next. |
| 165 pending_sockets_and_callbacks_.erase(pending_sockets_and_callbacks_.begin(), |
| 166 ++it); |
| 167 |
134 MonitorConnectionResult(ssl_socket); | 168 MonitorConnectionResult(ssl_socket); |
135 callback.Run(); | 169 callback.Run(); |
136 } | 170 } |
137 | 171 |
138 void SSLConnectJobMessenger::RunAllJobs( | 172 void SSLConnectJobMessenger::RunAllJobs( |
139 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(); |
140 for (std::vector<SocketAndCallback>::const_iterator it = | 176 for (std::vector<SocketAndCallback>::const_iterator it = |
141 pending_sockets_and_callbacks.begin(); | 177 pending_sockets_and_callbacks.begin(); |
142 it != pending_sockets_and_callbacks.end(); | 178 it != pending_sockets_and_callbacks.end(); |
143 ++it) | 179 ++it) |
144 it->callback.Run(); | 180 task_runner->PostTask(FROM_HERE, it->callback); |
145 } | 181 } |
146 | 182 |
147 // Timeout for the SSL handshake portion of the connect. | 183 // Timeout for the SSL handshake portion of the connect. |
148 static const int kSSLHandshakeTimeoutInSeconds = 30; | 184 static const int kSSLHandshakeTimeoutInSeconds = 30; |
149 | 185 |
150 SSLConnectJob::SSLConnectJob(const std::string& group_name, | 186 SSLConnectJob::SSLConnectJob(const std::string& group_name, |
151 RequestPriority priority, | 187 RequestPriority priority, |
152 const scoped_refptr<SSLSocketParams>& params, | 188 const scoped_refptr<SSLSocketParams>& params, |
153 const base::TimeDelta& timeout_duration, | 189 const base::TimeDelta& timeout_duration, |
154 TransportClientSocketPool* transport_pool, | 190 TransportClientSocketPool* transport_pool, |
(...skipping 648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 if (base_.CloseOneIdleSocket()) | 839 if (base_.CloseOneIdleSocket()) |
804 return true; | 840 return true; |
805 return base_.CloseOneIdleConnectionInHigherLayeredPool(); | 841 return base_.CloseOneIdleConnectionInHigherLayeredPool(); |
806 } | 842 } |
807 | 843 |
808 void SSLClientSocketPool::OnSSLConfigChanged() { | 844 void SSLClientSocketPool::OnSSLConfigChanged() { |
809 FlushWithError(ERR_NETWORK_CHANGED); | 845 FlushWithError(ERR_NETWORK_CHANGED); |
810 } | 846 } |
811 | 847 |
812 } // namespace net | 848 } // namespace net |
OLD | NEW |