1 // Copyright 2019 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 #ifndef SERVICES_NETWORK_DHCP_PAC_FILE_FETCHER_MOJO_H_
6 #define SERVICES_NETWORK_DHCP_PAC_FILE_FETCHER_MOJO_H_
7 
8 #include <memory>
9 
10 #include "base/component_export.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "mojo/public/cpp/bindings/pending_remote.h"
15 #include "mojo/public/cpp/bindings/remote.h"
16 #include "net/base/completion_once_callback.h"
17 #include "net/proxy_resolution/dhcp_pac_file_fetcher.h"
18 #include "net/traffic_annotation/network_traffic_annotation.h"
19 #include "services/network/public/mojom/dhcp_wpad_url_client.mojom.h"
20 #include "url/gurl.h"
21 
22 namespace net {
23 class URLRequestContext;
24 class NetLogWithSource;
25 class PacFileFetcher;
26 }  // namespace net
27 
28 namespace network {
29 
30 // Implementation of DhcpPacFileFetcher that gets the URL of the PAC file from
31 // the default network over a mojo pipe. The default network points to a single
32 // PAC file URL, provided by Shill, as reported over DHCP.
33 // Currently only used on ChromeOS.
COMPONENT_EXPORT(NETWORK_SERVICE)34 class COMPONENT_EXPORT(NETWORK_SERVICE) DhcpPacFileFetcherMojo
35     : public net::DhcpPacFileFetcher {
36  public:
37   DhcpPacFileFetcherMojo(net::URLRequestContext* url_request_context,
38                          mojo::PendingRemote<network::mojom::DhcpWpadUrlClient>
39                              dhcp_wpad_url_client);
40 
41   ~DhcpPacFileFetcherMojo() override;
42 
43   // DhcpPacFileFetcher implementation
44   int Fetch(base::string16* utf16_text,
45             net::CompletionOnceCallback callback,
46             const net::NetLogWithSource& net_log,
47             const net::NetworkTrafficAnnotationTag traffic_annotation) override;
48   void Cancel() override;
49   void OnShutdown() override;
50   const GURL& GetPacURL() const override;
51   std::string GetFetcherName() const override;
52 
53   void SetPacFileFetcherForTesting(
54       std::unique_ptr<net::PacFileFetcher> pac_file_fetcher);
55 
56  private:
57   void ContinueFetch(base::string16* utf16_text,
58                      const net::NetworkTrafficAnnotationTag traffic_annotation,
59                      std::string pac_url);
60   void OnFetchCompleted(int result);
61   void OnPacUrlReceived(const std::string& url);
62 
63   net::CompletionOnceCallback callback_;
64   base::string16* utf16_text_;
65   GURL pac_url_;
66   net::MutableNetworkTrafficAnnotationTag traffic_annotation_;
67   std::unique_ptr<net::PacFileFetcher> pac_file_fetcher_;
68   mojo::Remote<network::mojom::DhcpWpadUrlClient> dhcp_wpad_url_client_;
69 
70   base::WeakPtrFactory<DhcpPacFileFetcherMojo> weak_ptr_factory_{this};
71 
72   DISALLOW_COPY_AND_ASSIGN(DhcpPacFileFetcherMojo);
73 };
74 
75 }  // namespace network
76 
77 #endif  // SERVICES_NETWORK_DHCP_PAC_FILE_FETCHER_MOJO_H_
78