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 #ifndef SERVICES_NETWORK_HOST_RESOLVER_H_
6 #define SERVICES_NETWORK_HOST_RESOLVER_H_
7 
8 #include <memory>
9 #include <set>
10 #include <string>
11 
12 #include "base/callback.h"
13 #include "base/component_export.h"
14 #include "base/containers/unique_ptr_adapters.h"
15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h"
17 #include "mojo/public/cpp/bindings/pending_receiver.h"
18 #include "mojo/public/cpp/bindings/pending_remote.h"
19 #include "mojo/public/cpp/bindings/receiver.h"
20 #include "net/dns/public/dns_query_type.h"
21 #include "services/network/public/mojom/host_resolver.mojom.h"
22 
23 namespace net {
24 class HostResolver;
25 class HostPortPair;
26 class NetLog;
27 class NetworkIsolationKey;
28 }  // namespace net
29 
30 namespace network {
31 class HostResolverMdnsListener;
32 class ResolveHostRequest;
33 
COMPONENT_EXPORT(NETWORK_SERVICE)34 class COMPONENT_EXPORT(NETWORK_SERVICE) HostResolver
35     : public mojom::HostResolver {
36  public:
37   using ConnectionShutdownCallback = base::OnceCallback<void(HostResolver*)>;
38 
39   // Constructs and binds to the given mojom::HostResolver pipe. On pipe close,
40   // cancels all outstanding receivers (whether made through the pipe or by
41   // directly calling ResolveHost()) with ERR_FAILED. Also on pipe close, calls
42   // |connection_shutdown_callback| and passes |this| to notify that the
43   // resolver has cancelled all receivers and may be cleaned up.
44   HostResolver(mojo::PendingReceiver<mojom::HostResolver> resolver_receiver,
45                ConnectionShutdownCallback connection_shutdown_callback,
46                net::HostResolver* internal_resolver,
47                net::NetLog* net_log);
48   // Constructor for when the resolver will not be bound to a
49   // mojom::HostResolver pipe, eg because it is handling ResolveHost requests
50   // made directly on NetworkContext.
51   HostResolver(net::HostResolver* internal_resolver, net::NetLog* net_log);
52   ~HostResolver() override;
53 
54   void ResolveHost(
55       const net::HostPortPair& host,
56       const net::NetworkIsolationKey& network_isolation_key,
57       mojom::ResolveHostParametersPtr optional_parameters,
58       mojo::PendingRemote<mojom::ResolveHostClient> response_client) override;
59   void MdnsListen(const net::HostPortPair& host,
60                   net::DnsQueryType query_type,
61                   mojo::PendingRemote<mojom::MdnsListenClient> response_client,
62                   MdnsListenCallback callback) override;
63 
64   size_t GetNumOutstandingRequestsForTesting() const;
65 
66   // Sets a global callback when a ResolveHost call arrives.
67   using ResolveHostCallback =
68       base::RepeatingCallback<void(const std::string& host)>;
69   static void SetResolveHostCallbackForTesting(ResolveHostCallback callback);
70 
71  private:
72   void AsyncSetUp();
73   void OnResolveHostComplete(ResolveHostRequest* request, int error);
74   void OnMdnsListenerCancelled(HostResolverMdnsListener* listener);
75   void OnConnectionError();
76 
77   mojo::Receiver<mojom::HostResolver> receiver_;
78   mojo::PendingReceiver<mojom::HostResolver> pending_receiver_;
79   ConnectionShutdownCallback connection_shutdown_callback_;
80   std::set<std::unique_ptr<ResolveHostRequest>, base::UniquePtrComparator>
81       requests_;
82   std::set<std::unique_ptr<HostResolverMdnsListener>, base::UniquePtrComparator>
83       listeners_;
84 
85   net::HostResolver* const internal_resolver_;
86   net::NetLog* const net_log_;
87 
88   base::WeakPtrFactory<HostResolver> weak_factory_{this};
89 
90   DISALLOW_COPY_AND_ASSIGN(HostResolver);
91 };
92 
93 }  // namespace network
94 
95 #endif  // SERVICES_NETWORK_HOST_RESOLVER_H_
96