Revert 271602 "Implementation of leveldb-backed PrefStore."
[chromium-blink-merge.git] / jingle / glue / proxy_resolving_client_socket.cc
blobf88f7152b335ce7b3fef71503fcf66073b7bb7b6
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
3 // found in the LICENSE file.
5 #include "jingle/glue/proxy_resolving_client_socket.h"
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
14 #include "net/http/http_network_session.h"
15 #include "net/socket/client_socket_handle.h"
16 #include "net/socket/client_socket_pool_manager.h"
17 #include "net/url_request/url_request_context.h"
18 #include "net/url_request/url_request_context_getter.h"
20 namespace jingle_glue {
22 ProxyResolvingClientSocket::ProxyResolvingClientSocket(
23 net::ClientSocketFactory* socket_factory,
24 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
25 const net::SSLConfig& ssl_config,
26 const net::HostPortPair& dest_host_port_pair)
27 : proxy_resolve_callback_(
28 base::Bind(&ProxyResolvingClientSocket::ProcessProxyResolveDone,
29 base::Unretained(this))),
30 connect_callback_(
31 base::Bind(&ProxyResolvingClientSocket::ProcessConnectDone,
32 base::Unretained(this))),
33 ssl_config_(ssl_config),
34 pac_request_(NULL),
35 dest_host_port_pair_(dest_host_port_pair),
36 // Assume that we intend to do TLS on this socket; all
37 // current use cases do.
38 proxy_url_("https://" + dest_host_port_pair_.ToString()),
39 tried_direct_connect_fallback_(false),
40 bound_net_log_(
41 net::BoundNetLog::Make(
42 request_context_getter->GetURLRequestContext()->net_log(),
43 net::NetLog::SOURCE_SOCKET)),
44 weak_factory_(this) {
45 DCHECK(request_context_getter.get());
46 net::URLRequestContext* request_context =
47 request_context_getter->GetURLRequestContext();
48 DCHECK(request_context);
49 DCHECK(!dest_host_port_pair_.host().empty());
50 DCHECK_GT(dest_host_port_pair_.port(), 0);
51 DCHECK(proxy_url_.is_valid());
53 net::HttpNetworkSession::Params session_params;
54 session_params.client_socket_factory = socket_factory;
55 session_params.host_resolver = request_context->host_resolver();
56 session_params.cert_verifier = request_context->cert_verifier();
57 session_params.transport_security_state =
58 request_context->transport_security_state();
59 // TODO(rkn): This is NULL because ServerBoundCertService is not thread safe.
60 session_params.server_bound_cert_service = NULL;
61 session_params.proxy_service = request_context->proxy_service();
62 session_params.ssl_config_service = request_context->ssl_config_service();
63 session_params.http_auth_handler_factory =
64 request_context->http_auth_handler_factory();
65 session_params.network_delegate = request_context->network_delegate();
66 session_params.http_server_properties =
67 request_context->http_server_properties();
68 session_params.net_log = request_context->net_log();
70 const net::HttpNetworkSession::Params* reference_params =
71 request_context->GetNetworkSessionParams();
72 if (reference_params) {
73 session_params.host_mapping_rules = reference_params->host_mapping_rules;
74 session_params.ignore_certificate_errors =
75 reference_params->ignore_certificate_errors;
76 session_params.http_pipelining_enabled =
77 reference_params->http_pipelining_enabled;
78 session_params.testing_fixed_http_port =
79 reference_params->testing_fixed_http_port;
80 session_params.testing_fixed_https_port =
81 reference_params->testing_fixed_https_port;
82 session_params.trusted_spdy_proxy = reference_params->trusted_spdy_proxy;
85 network_session_ = new net::HttpNetworkSession(session_params);
88 ProxyResolvingClientSocket::~ProxyResolvingClientSocket() {
89 Disconnect();
92 int ProxyResolvingClientSocket::Read(net::IOBuffer* buf, int buf_len,
93 const net::CompletionCallback& callback) {
94 if (transport_.get() && transport_->socket())
95 return transport_->socket()->Read(buf, buf_len, callback);
96 NOTREACHED();
97 return net::ERR_SOCKET_NOT_CONNECTED;
100 int ProxyResolvingClientSocket::Write(
101 net::IOBuffer* buf,
102 int buf_len,
103 const net::CompletionCallback& callback) {
104 if (transport_.get() && transport_->socket())
105 return transport_->socket()->Write(buf, buf_len, callback);
106 NOTREACHED();
107 return net::ERR_SOCKET_NOT_CONNECTED;
110 int ProxyResolvingClientSocket::SetReceiveBufferSize(int32 size) {
111 if (transport_.get() && transport_->socket())
112 return transport_->socket()->SetReceiveBufferSize(size);
113 NOTREACHED();
114 return net::ERR_SOCKET_NOT_CONNECTED;
117 int ProxyResolvingClientSocket::SetSendBufferSize(int32 size) {
118 if (transport_.get() && transport_->socket())
119 return transport_->socket()->SetSendBufferSize(size);
120 NOTREACHED();
121 return net::ERR_SOCKET_NOT_CONNECTED;
124 int ProxyResolvingClientSocket::Connect(
125 const net::CompletionCallback& callback) {
126 DCHECK(user_connect_callback_.is_null());
128 tried_direct_connect_fallback_ = false;
130 // First we try and resolve the proxy.
131 int status = network_session_->proxy_service()->ResolveProxy(
132 proxy_url_,
133 &proxy_info_,
134 proxy_resolve_callback_,
135 &pac_request_,
136 bound_net_log_);
137 if (status != net::ERR_IO_PENDING) {
138 // We defer execution of ProcessProxyResolveDone instead of calling it
139 // directly here for simplicity. From the caller's point of view,
140 // the connect always happens asynchronously.
141 base::MessageLoop* message_loop = base::MessageLoop::current();
142 CHECK(message_loop);
143 message_loop->PostTask(
144 FROM_HERE,
145 base::Bind(&ProxyResolvingClientSocket::ProcessProxyResolveDone,
146 weak_factory_.GetWeakPtr(), status));
148 user_connect_callback_ = callback;
149 return net::ERR_IO_PENDING;
152 void ProxyResolvingClientSocket::RunUserConnectCallback(int status) {
153 DCHECK_LE(status, net::OK);
154 net::CompletionCallback user_connect_callback = user_connect_callback_;
155 user_connect_callback_.Reset();
156 user_connect_callback.Run(status);
159 // Always runs asynchronously.
160 void ProxyResolvingClientSocket::ProcessProxyResolveDone(int status) {
161 pac_request_ = NULL;
163 DCHECK_NE(status, net::ERR_IO_PENDING);
164 if (status == net::OK) {
165 // Remove unsupported proxies from the list.
166 proxy_info_.RemoveProxiesWithoutScheme(
167 net::ProxyServer::SCHEME_DIRECT |
168 net::ProxyServer::SCHEME_HTTP | net::ProxyServer::SCHEME_HTTPS |
169 net::ProxyServer::SCHEME_SOCKS4 | net::ProxyServer::SCHEME_SOCKS5);
171 if (proxy_info_.is_empty()) {
172 // No proxies/direct to choose from. This happens when we don't support
173 // any of the proxies in the returned list.
174 status = net::ERR_NO_SUPPORTED_PROXIES;
178 // Since we are faking the URL, it is possible that no proxies match our URL.
179 // Try falling back to a direct connection if we have not tried that before.
180 if (status != net::OK) {
181 if (!tried_direct_connect_fallback_) {
182 tried_direct_connect_fallback_ = true;
183 proxy_info_.UseDirect();
184 } else {
185 CloseTransportSocket();
186 RunUserConnectCallback(status);
187 return;
191 transport_.reset(new net::ClientSocketHandle);
192 // Now that we have resolved the proxy, we need to connect.
193 status = net::InitSocketHandleForRawConnect(
194 dest_host_port_pair_, network_session_.get(), proxy_info_, ssl_config_,
195 ssl_config_, net::PRIVACY_MODE_DISABLED, bound_net_log_, transport_.get(),
196 connect_callback_);
197 if (status != net::ERR_IO_PENDING) {
198 // Since this method is always called asynchronously. it is OK to call
199 // ProcessConnectDone synchronously.
200 ProcessConnectDone(status);
204 void ProxyResolvingClientSocket::ProcessConnectDone(int status) {
205 if (status != net::OK) {
206 // If the connection fails, try another proxy.
207 status = ReconsiderProxyAfterError(status);
208 // ReconsiderProxyAfterError either returns an error (in which case it is
209 // not reconsidering a proxy) or returns ERR_IO_PENDING if it is considering
210 // another proxy.
211 DCHECK_NE(status, net::OK);
212 if (status == net::ERR_IO_PENDING)
213 // Proxy reconsideration pending. Return.
214 return;
215 CloseTransportSocket();
216 } else {
217 ReportSuccessfulProxyConnection();
219 RunUserConnectCallback(status);
222 // TODO(sanjeevr): This has largely been copied from
223 // HttpStreamFactoryImpl::Job::ReconsiderProxyAfterError. This should be
224 // refactored into some common place.
225 // This method reconsiders the proxy on certain errors. If it does reconsider
226 // a proxy it always returns ERR_IO_PENDING and posts a call to
227 // ProcessProxyResolveDone with the result of the reconsideration.
228 int ProxyResolvingClientSocket::ReconsiderProxyAfterError(int error) {
229 DCHECK(!pac_request_);
230 DCHECK_NE(error, net::OK);
231 DCHECK_NE(error, net::ERR_IO_PENDING);
232 // A failure to resolve the hostname or any error related to establishing a
233 // TCP connection could be grounds for trying a new proxy configuration.
235 // Why do this when a hostname cannot be resolved? Some URLs only make sense
236 // to proxy servers. The hostname in those URLs might fail to resolve if we
237 // are still using a non-proxy config. We need to check if a proxy config
238 // now exists that corresponds to a proxy server that could load the URL.
240 switch (error) {
241 case net::ERR_PROXY_CONNECTION_FAILED:
242 case net::ERR_NAME_NOT_RESOLVED:
243 case net::ERR_INTERNET_DISCONNECTED:
244 case net::ERR_ADDRESS_UNREACHABLE:
245 case net::ERR_CONNECTION_CLOSED:
246 case net::ERR_CONNECTION_RESET:
247 case net::ERR_CONNECTION_REFUSED:
248 case net::ERR_CONNECTION_ABORTED:
249 case net::ERR_TIMED_OUT:
250 case net::ERR_TUNNEL_CONNECTION_FAILED:
251 case net::ERR_SOCKS_CONNECTION_FAILED:
252 break;
253 case net::ERR_SOCKS_CONNECTION_HOST_UNREACHABLE:
254 // Remap the SOCKS-specific "host unreachable" error to a more
255 // generic error code (this way consumers like the link doctor
256 // know to substitute their error page).
258 // Note that if the host resolving was done by the SOCSK5 proxy, we can't
259 // differentiate between a proxy-side "host not found" versus a proxy-side
260 // "address unreachable" error, and will report both of these failures as
261 // ERR_ADDRESS_UNREACHABLE.
262 return net::ERR_ADDRESS_UNREACHABLE;
263 default:
264 return error;
267 if (proxy_info_.is_https() && ssl_config_.send_client_cert) {
268 network_session_->ssl_client_auth_cache()->Remove(
269 proxy_info_.proxy_server().host_port_pair());
272 int rv = network_session_->proxy_service()->ReconsiderProxyAfterError(
273 proxy_url_, &proxy_info_, proxy_resolve_callback_, &pac_request_,
274 bound_net_log_);
275 if (rv == net::OK || rv == net::ERR_IO_PENDING) {
276 CloseTransportSocket();
277 } else {
278 // If ReconsiderProxyAfterError() failed synchronously, it means
279 // there was nothing left to fall-back to, so fail the transaction
280 // with the last connection error we got.
281 rv = error;
284 // We either have new proxy info or there was an error in falling back.
285 // In both cases we want to post ProcessProxyResolveDone (in the error case
286 // we might still want to fall back a direct connection).
287 if (rv != net::ERR_IO_PENDING) {
288 base::MessageLoop* message_loop = base::MessageLoop::current();
289 CHECK(message_loop);
290 message_loop->PostTask(
291 FROM_HERE,
292 base::Bind(&ProxyResolvingClientSocket::ProcessProxyResolveDone,
293 weak_factory_.GetWeakPtr(), rv));
294 // Since we potentially have another try to go (trying the direct connect)
295 // set the return code code to ERR_IO_PENDING.
296 rv = net::ERR_IO_PENDING;
298 return rv;
301 void ProxyResolvingClientSocket::ReportSuccessfulProxyConnection() {
302 network_session_->proxy_service()->ReportSuccess(proxy_info_);
305 void ProxyResolvingClientSocket::Disconnect() {
306 CloseTransportSocket();
307 if (pac_request_) {
308 network_session_->proxy_service()->CancelPacRequest(pac_request_);
309 pac_request_ = NULL;
311 user_connect_callback_.Reset();
314 bool ProxyResolvingClientSocket::IsConnected() const {
315 if (!transport_.get() || !transport_->socket())
316 return false;
317 return transport_->socket()->IsConnected();
320 bool ProxyResolvingClientSocket::IsConnectedAndIdle() const {
321 if (!transport_.get() || !transport_->socket())
322 return false;
323 return transport_->socket()->IsConnectedAndIdle();
326 int ProxyResolvingClientSocket::GetPeerAddress(
327 net::IPEndPoint* address) const {
328 if (transport_.get() && transport_->socket())
329 return transport_->socket()->GetPeerAddress(address);
330 NOTREACHED();
331 return net::ERR_SOCKET_NOT_CONNECTED;
334 int ProxyResolvingClientSocket::GetLocalAddress(
335 net::IPEndPoint* address) const {
336 if (transport_.get() && transport_->socket())
337 return transport_->socket()->GetLocalAddress(address);
338 NOTREACHED();
339 return net::ERR_SOCKET_NOT_CONNECTED;
342 const net::BoundNetLog& ProxyResolvingClientSocket::NetLog() const {
343 if (transport_.get() && transport_->socket())
344 return transport_->socket()->NetLog();
345 NOTREACHED();
346 return bound_net_log_;
349 void ProxyResolvingClientSocket::SetSubresourceSpeculation() {
350 if (transport_.get() && transport_->socket())
351 transport_->socket()->SetSubresourceSpeculation();
352 else
353 NOTREACHED();
356 void ProxyResolvingClientSocket::SetOmniboxSpeculation() {
357 if (transport_.get() && transport_->socket())
358 transport_->socket()->SetOmniboxSpeculation();
359 else
360 NOTREACHED();
363 bool ProxyResolvingClientSocket::WasEverUsed() const {
364 if (transport_.get() && transport_->socket())
365 return transport_->socket()->WasEverUsed();
366 NOTREACHED();
367 return false;
370 bool ProxyResolvingClientSocket::UsingTCPFastOpen() const {
371 if (transport_.get() && transport_->socket())
372 return transport_->socket()->UsingTCPFastOpen();
373 NOTREACHED();
374 return false;
377 bool ProxyResolvingClientSocket::WasNpnNegotiated() const {
378 return false;
381 net::NextProto ProxyResolvingClientSocket::GetNegotiatedProtocol() const {
382 if (transport_.get() && transport_->socket())
383 return transport_->socket()->GetNegotiatedProtocol();
384 NOTREACHED();
385 return net::kProtoUnknown;
388 bool ProxyResolvingClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) {
389 return false;
392 void ProxyResolvingClientSocket::CloseTransportSocket() {
393 if (transport_.get() && transport_->socket())
394 transport_->socket()->Disconnect();
395 transport_.reset();
398 } // namespace jingle_glue