1 // Copyright 2018 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 "services/network/proxy_lookup_request.h"
6 
7 #include <string>
8 
9 #include "base/bind.h"
10 #include "base/optional.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/http_network_session.h"
13 #include "net/http/http_transaction_factory.h"
14 #include "net/log/net_log_with_source.h"
15 #include "net/proxy_resolution/configured_proxy_resolution_service.h"
16 #include "net/proxy_resolution/proxy_resolution_request.h"
17 #include "net/url_request/url_request_context.h"
18 #include "services/network/network_context.h"
19 #include "services/proxy_resolver/public/mojom/proxy_resolver.mojom.h"
20 #include "url/gurl.h"
21 
22 namespace network {
23 
ProxyLookupRequest(mojo::PendingRemote<mojom::ProxyLookupClient> proxy_lookup_client,NetworkContext * network_context,const net::NetworkIsolationKey & network_isolation_key)24 ProxyLookupRequest::ProxyLookupRequest(
25     mojo::PendingRemote<mojom::ProxyLookupClient> proxy_lookup_client,
26     NetworkContext* network_context,
27     const net::NetworkIsolationKey& network_isolation_key)
28     : network_context_(network_context),
29       network_isolation_key_(network_isolation_key),
30       proxy_lookup_client_(std::move(proxy_lookup_client)) {
31   DCHECK(proxy_lookup_client_);
32 }
33 
~ProxyLookupRequest()34 ProxyLookupRequest::~ProxyLookupRequest() {
35   // |request_| should be non-null only when the network service is being torn
36   // down.
37   if (request_)
38     proxy_lookup_client_->OnProxyLookupComplete(net::ERR_ABORTED,
39                                                 base::nullopt);
40 }
41 
Start(const GURL & url)42 void ProxyLookupRequest::Start(const GURL& url) {
43   proxy_lookup_client_.set_disconnect_handler(
44       base::BindOnce(&ProxyLookupRequest::DestroySelf, base::Unretained(this)));
45   // TODO(mmenke): The NetLogWithSource() means nothing is logged. Fix that.
46   //
47   // TODO(https://crbug.com/1023435): Pass along a NetworkIsolationKey.
48   int result = network_context_->url_request_context()
49                    ->proxy_resolution_service()
50                    ->ResolveProxy(
51                        url, std::string(), network_isolation_key_, &proxy_info_,
52                        base::BindOnce(&ProxyLookupRequest::OnResolveComplete,
53                                       base::Unretained(this)),
54                        &request_, net::NetLogWithSource());
55   if (result != net::ERR_IO_PENDING)
56     OnResolveComplete(result);
57 }
58 
OnResolveComplete(int result)59 void ProxyLookupRequest::OnResolveComplete(int result) {
60   if (result == net::OK) {
61     proxy_lookup_client_->OnProxyLookupComplete(
62         net::OK, base::Optional<net::ProxyInfo>(std::move(proxy_info_)));
63   } else {
64     proxy_lookup_client_->OnProxyLookupComplete(result, base::nullopt);
65   }
66   DestroySelf();
67 }
68 
DestroySelf()69 void ProxyLookupRequest::DestroySelf() {
70   request_.reset();
71   network_context_->OnProxyLookupComplete(this);
72 }
73 
74 }  // namespace network
75