Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: jingle/notifier/base/proxy_resolving_client_socket.cc

Issue 6833031: Changed the jingle network code in ChromeAsyncSocket to use the client socket pool. This also all... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Review comments addressed Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "jingle/notifier/base/proxy_resolving_client_socket.h"
6
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/logging.h"
10 #include "googleurl/src/gurl.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/http/http_network_session.h"
14 #include "net/socket/client_socket_handle.h"
15 #include "net/socket/client_socket_pool_manager.h"
16 #include "net/url_request/url_request_context.h"
17 #include "net/url_request/url_request_context_getter.h"
18
19 namespace notifier {
20
21 ProxyResolvingClientSocket::ProxyResolvingClientSocket(
22 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
23 const net::SSLConfig& ssl_config,
24 const net::HostPortPair& dest_host_port_pair,
25 net::NetLog* net_log)
26 : proxy_resolve_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this),
27 &ProxyResolvingClientSocket::ProcessProxyResolveDone),
28 connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this),
29 &ProxyResolvingClientSocket::ProcessConnectDone),
30 ssl_config_(ssl_config),
31 pac_request_(NULL),
32 dest_host_port_pair_(dest_host_port_pair),
33 bound_net_log_(
akalin 2011/04/15 01:47:33 need to init tried_direct_connect_
sanjeevr 2011/04/15 04:23:35 Done.
34 net::BoundNetLog::Make(net_log, net::NetLog::SOURCE_SOCKET)),
35 scoped_runnable_method_factory_(
36 ALLOW_THIS_IN_INITIALIZER_LIST(this)),
37 user_connect_callback_(NULL) {
38 net::HttpNetworkSession::Params session_params;
39 session_params.host_resolver =
40 request_context_getter->GetURLRequestContext()->host_resolver();
akalin 2011/04/15 01:47:33 seems cleaner to have an intermediate var for the
sanjeevr 2011/04/15 04:23:35 Done.
41 session_params.cert_verifier =
42 request_context_getter->GetURLRequestContext()->cert_verifier();
43 session_params.dnsrr_resolver =
44 request_context_getter->GetURLRequestContext()->dnsrr_resolver();
45 session_params.proxy_service =
46 request_context_getter->GetURLRequestContext()->proxy_service();
47 session_params.ssl_config_service =
48 request_context_getter->GetURLRequestContext()->ssl_config_service();
49 session_params.http_auth_handler_factory =
50 request_context_getter->GetURLRequestContext()->
51 http_auth_handler_factory();
52 network_session_ = new net::HttpNetworkSession(session_params);
53 }
54
55 ProxyResolvingClientSocket::~ProxyResolvingClientSocket() {
56 if (pac_request_)
akalin 2011/04/15 01:47:33 I think this clause belongs to Disconnect(), since
sanjeevr 2011/04/15 04:23:35 Done.
57 network_session_->proxy_service()->CancelPacRequest(pac_request_);
58 }
59
60 int ProxyResolvingClientSocket::Read(net::IOBuffer* buf, int buf_len,
61 net::CompletionCallback* callback) {
62 if (transport_.get() && transport_->socket())
63 return transport_->socket()->Read(buf, buf_len, callback);
64 NOTREACHED();
65 return net::ERR_SOCKET_NOT_CONNECTED;
66 }
67
68 int ProxyResolvingClientSocket::Write(net::IOBuffer* buf, int buf_len,
69 net::CompletionCallback* callback) {
70 if (transport_.get() && transport_->socket())
71 return transport_->socket()->Write(buf, buf_len, callback);
72 NOTREACHED();
73 return net::ERR_SOCKET_NOT_CONNECTED;
74 }
75
76 bool ProxyResolvingClientSocket::SetReceiveBufferSize(int32 size) {
77 if (transport_.get() && transport_->socket())
78 return transport_->socket()->SetReceiveBufferSize(size);
79 NOTREACHED();
80 return false;
81 }
82
83 bool ProxyResolvingClientSocket::SetSendBufferSize(int32 size) {
84 if (transport_.get() && transport_->socket())
85 return transport_->socket()->SetSendBufferSize(size);
86 NOTREACHED();
87 return false;
88 }
89
90 int ProxyResolvingClientSocket::Connect(net::CompletionCallback* callback) {
91 DCHECK(!user_connect_callback_);
92
93 // First we try and resolve the proxy.
94 GURL url = GURL("http://" + dest_host_port_pair_.ToString());
95 int status = network_session_->proxy_service()->ResolveProxy(
96 url,
97 &proxy_info_,
98 &proxy_resolve_callback_,
99 &pac_request_,
100 bound_net_log_);
101 if (status != net::ERR_IO_PENDING) {
102 // We defer execution of ProcessProxyResolveDone instead of calling it
103 // directly here for simplicity. From the caller's point of view,
104 // the connect always happens asynchronously.
105 MessageLoop* message_loop = MessageLoop::current();
106 CHECK(message_loop);
107 message_loop->PostTask(
108 FROM_HERE,
109 scoped_runnable_method_factory_.NewRunnableMethod(
110 &ProxyResolvingClientSocket::ProcessProxyResolveDone, status));
111 }
112 user_connect_callback_ = callback;
113 return net::ERR_IO_PENDING;
114 }
115
116 void ProxyResolvingClientSocket::RunUserConnectCallback(int status) {
117 DCHECK_LE(status, net::OK);
118 net::CompletionCallback* user_connect_callback = user_connect_callback_;
119 user_connect_callback_ = NULL;
120 user_connect_callback->Run(status);
121 }
122
123 // Always runs asynchronously.
124 void ProxyResolvingClientSocket::ProcessProxyResolveDone(int status) {
125 pac_request_ = NULL;
126
127 DCHECK_NE(status, net::ERR_IO_PENDING);
128 if (status == net::OK) {
129 // Remove unsupported proxies from the list.
130 proxy_info_.RemoveProxiesWithoutScheme(
131 net::ProxyServer::SCHEME_DIRECT |
132 net::ProxyServer::SCHEME_HTTP | net::ProxyServer::SCHEME_HTTPS |
133 net::ProxyServer::SCHEME_SOCKS4 | net::ProxyServer::SCHEME_SOCKS5);
134
135 if (proxy_info_.is_empty()) {
136 // No proxies/direct to choose from. This happens when we don't support
137 // any of the proxies in the returned list.
138 status = net::ERR_NO_SUPPORTED_PROXIES;
139 }
140 }
141
142 // Since we are faking the URL, it is possible that no proxies match our URL.
143 // Try falling back to a direct connection if we have not tried that before.
144 if (status != net::OK) {
145 if (!tried_direct_connect_fallback_) {
146 tried_direct_connect_fallback_ = true;
147 proxy_info_.UseDirect();
148 } else {
149 CloseTransportSocket();
150 RunUserConnectCallback(status);
151 return;
152 }
153 }
154
155 transport_.reset(new net::ClientSocketHandle);
156 // Now that we have resolved the proxy, we need to connect.
157 status = net::ClientSocketPoolManager::InitSocketHandleForRawConnect(
158 dest_host_port_pair_,
159 network_session_.get(),
160 proxy_info_,
161 ssl_config_,
162 ssl_config_,
163 bound_net_log_,
164 transport_.get(),
165 &connect_callback_);
166 if (status != net::ERR_IO_PENDING) {
167 // Since this method is always called asynchronously. it is OK to call
168 // ProcessConnectDone synchronously.
169 ProcessConnectDone(status);
170 }
171 }
172
173 void ProxyResolvingClientSocket::ProcessConnectDone(int status) {
174 if (status != net::OK) {
175 // If the connection fails, try another proxy.
176 status = ReconsiderProxyAfterError(status);
177 if ((status == net::OK) || (status == net::ERR_IO_PENDING)) {
akalin 2011/04/15 01:47:33 DCHECK that status is either the original status o
sanjeevr 2011/04/15 04:23:35 Actually it does not always preserve the original
178 // Proxy reconsideration pending. Return.
179 return;
180 }
181 }
182 if (status != net::OK)
akalin 2011/04/15 01:47:33 combine with previous clause
sanjeevr 2011/04/15 04:23:35 Done.
183 CloseTransportSocket();
184 RunUserConnectCallback(status);
185 }
186
187 // TODO(sanjeevr): This has largely been copied from
akalin 2011/04/15 01:47:33 Can you document the expected behavior of this fn?
sanjeevr 2011/04/15 04:23:35 Done.
188 // HttpStreamFactoryImpl::Job::ReconsiderProxyAfterError. This should be
189 // refactored into some common place.
190 int ProxyResolvingClientSocket::ReconsiderProxyAfterError(int error) {
191 DCHECK(!pac_request_);
akalin 2011/04/15 01:47:33 add DCHECK error != OK, DCHECK error != PENDING
sanjeevr 2011/04/15 04:23:35 Done.
192 // A failure to resolve the hostname or any error related to establishing a
193 // TCP connection could be grounds for trying a new proxy configuration.
194 //
195 // Why do this when a hostname cannot be resolved? Some URLs only make sense
196 // to proxy servers. The hostname in those URLs might fail to resolve if we
197 // are still using a non-proxy config. We need to check if a proxy config
198 // now exists that corresponds to a proxy server that could load the URL.
199 //
200 switch (error) {
201 case net::ERR_PROXY_CONNECTION_FAILED:
202 case net::ERR_NAME_NOT_RESOLVED:
203 case net::ERR_INTERNET_DISCONNECTED:
204 case net::ERR_ADDRESS_UNREACHABLE:
205 case net::ERR_CONNECTION_CLOSED:
206 case net::ERR_CONNECTION_RESET:
207 case net::ERR_CONNECTION_REFUSED:
208 case net::ERR_CONNECTION_ABORTED:
209 case net::ERR_TIMED_OUT:
210 case net::ERR_TUNNEL_CONNECTION_FAILED:
211 case net::ERR_SOCKS_CONNECTION_FAILED:
212 break;
213 case net::ERR_SOCKS_CONNECTION_HOST_UNREACHABLE:
214 // Remap the SOCKS-specific "host unreachable" error to a more
215 // generic error code (this way consumers like the link doctor
216 // know to substitute their error page).
217 //
218 // Note that if the host resolving was done by the SOCSK5 proxy, we can't
219 // differentiate between a proxy-side "host not found" versus a proxy-side
220 // "address unreachable" error, and will report both of these failures as
221 // ERR_ADDRESS_UNREACHABLE.
222 return net::ERR_ADDRESS_UNREACHABLE;
223 default:
224 return error;
225 }
226
227 if (proxy_info_.is_https() && ssl_config_.send_client_cert) {
228 network_session_->ssl_client_auth_cache()->Remove(
229 proxy_info_.proxy_server().host_port_pair().ToString());
230 }
231
232 GURL url = GURL("http://" + dest_host_port_pair_.ToString());
233 int rv = network_session_->proxy_service()->ReconsiderProxyAfterError(
234 url, &proxy_info_, &proxy_resolve_callback_, &pac_request_,
235 bound_net_log_);
236 if (rv == net::OK || rv == net::ERR_IO_PENDING) {
237 CloseTransportSocket();
238 } else {
239 // If ReconsiderProxyAfterError() failed synchronously, it means
240 // there was nothing left to fall-back to, so fail the transaction
241 // with the last connection error we got.
242 rv = error;
243 }
244
245 // We either have new proxy info or there was an error in falling back.
246 // In both cases we want to post ProcessProxyResolveDone (in the error case
247 // we might still want to fall back a direct connection).
248 if (rv != net::ERR_IO_PENDING) {
249 MessageLoop* message_loop = MessageLoop::current();
250 CHECK(message_loop);
251 message_loop->PostTask(
252 FROM_HERE,
253 scoped_runnable_method_factory_.NewRunnableMethod(
254 &ProxyResolvingClientSocket::ProcessProxyResolveDone, rv));
255 // Since we potentially have another try to go (trying the direct connect)
256 // set the return code code to ERR_IO_PENDING.
257 rv = net::ERR_IO_PENDING;
258 }
259 return rv;
260 }
261
262 void ProxyResolvingClientSocket::Disconnect() {
263 CloseTransportSocket();
264 user_connect_callback_ = NULL;
265 }
266
267 bool ProxyResolvingClientSocket::IsConnected() const {
268 if (!transport_.get() || !transport_->socket())
269 return false;
270 return transport_->socket()->IsConnected();
271 }
272
273 bool ProxyResolvingClientSocket::IsConnectedAndIdle() const {
274 if (!transport_.get() || !transport_->socket())
275 return false;
276 return transport_->socket()->IsConnectedAndIdle();
277 }
278
279 int ProxyResolvingClientSocket::GetPeerAddress(
280 net::AddressList* address) const {
281 if (transport_.get() && transport_->socket())
282 return transport_->socket()->GetPeerAddress(address);
283 NOTREACHED();
284 return net::ERR_SOCKET_NOT_CONNECTED;
285 }
286
287 const net::BoundNetLog& ProxyResolvingClientSocket::NetLog() const {
288 if (transport_.get() && transport_->socket())
289 return transport_->socket()->NetLog();
290 NOTREACHED();
291 return bound_net_log_;
292 }
293
294 void ProxyResolvingClientSocket::SetSubresourceSpeculation() {
295 if (transport_.get() && transport_->socket())
296 transport_->socket()->SetSubresourceSpeculation();
297 else
298 NOTREACHED();
299 }
300
301 void ProxyResolvingClientSocket::SetOmniboxSpeculation() {
302 if (transport_.get() && transport_->socket())
303 transport_->socket()->SetOmniboxSpeculation();
304 else
305 NOTREACHED();
306 }
307
308 bool ProxyResolvingClientSocket::WasEverUsed() const {
309 if (transport_.get() && transport_->socket())
310 return transport_->socket()->WasEverUsed();
311 NOTREACHED();
312 return false;
313 }
314
315 bool ProxyResolvingClientSocket::UsingTCPFastOpen() const {
316 if (transport_.get() && transport_->socket())
317 return transport_->socket()->UsingTCPFastOpen();
318 NOTREACHED();
319 return false;
320 }
321
322 void ProxyResolvingClientSocket::CloseTransportSocket() {
323 if (transport_.get() && transport_->socket())
324 transport_->socket()->Disconnect();
325 transport_.reset();
326 }
327
328 } // namespace notifier
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698