summaryrefslogtreecommitdiff
path: root/chromium/services/network/dhcp_pac_file_fetcher_mojo.h
blob: 47f86a82d825ac067d228f58f2e81f14ec37fb55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SERVICES_NETWORK_DHCP_PAC_FILE_FETCHER_MOJO_H_
#define SERVICES_NETWORK_DHCP_PAC_FILE_FETCHER_MOJO_H_

#include <memory>

#include "base/component_export.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/completion_once_callback.h"
#include "net/proxy_resolution/dhcp_pac_file_fetcher.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/mojom/dhcp_wpad_url_client.mojom.h"
#include "url/gurl.h"

namespace net {
class URLRequestContext;
class NetLogWithSource;
class PacFileFetcher;
}  // namespace net

namespace network {

// Implementation of DhcpPacFileFetcher that gets the URL of the PAC file from
// the default network over a mojo pipe. The default network points to a single
// PAC file URL, provided by Shill, as reported over DHCP.
// Currently only used on ChromeOS.
class COMPONENT_EXPORT(NETWORK_SERVICE) DhcpPacFileFetcherMojo
    : public net::DhcpPacFileFetcher {
 public:
  DhcpPacFileFetcherMojo(net::URLRequestContext* url_request_context,
                         mojo::PendingRemote<network::mojom::DhcpWpadUrlClient>
                             dhcp_wpad_url_client);

  DhcpPacFileFetcherMojo(const DhcpPacFileFetcherMojo&) = delete;
  DhcpPacFileFetcherMojo& operator=(const DhcpPacFileFetcherMojo&) = delete;

  ~DhcpPacFileFetcherMojo() override;

  // DhcpPacFileFetcher implementation
  int Fetch(std::u16string* utf16_text,
            net::CompletionOnceCallback callback,
            const net::NetLogWithSource& net_log,
            const net::NetworkTrafficAnnotationTag traffic_annotation) override;
  void Cancel() override;
  void OnShutdown() override;
  const GURL& GetPacURL() const override;
  std::string GetFetcherName() const override;

  void SetPacFileFetcherForTesting(
      std::unique_ptr<net::PacFileFetcher> pac_file_fetcher);

 private:
  void ContinueFetch(std::u16string* utf16_text,
                     const net::NetworkTrafficAnnotationTag traffic_annotation,
                     std::string pac_url);
  void OnFetchCompleted(int result);
  void OnPacUrlReceived(const std::string& url);

  net::CompletionOnceCallback callback_;
  std::u16string* utf16_text_;
  GURL pac_url_;
  net::MutableNetworkTrafficAnnotationTag traffic_annotation_;
  std::unique_ptr<net::PacFileFetcher> pac_file_fetcher_;
  mojo::Remote<network::mojom::DhcpWpadUrlClient> dhcp_wpad_url_client_;

  base::WeakPtrFactory<DhcpPacFileFetcherMojo> weak_ptr_factory_{this};
};

}  // namespace network

#endif  // SERVICES_NETWORK_DHCP_PAC_FILE_FETCHER_MOJO_H_