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_HOST_RESOLVER_MDNS_LISTENER_H_
6 #define SERVICES_NETWORK_HOST_RESOLVER_MDNS_LISTENER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/callback_forward.h"
13 #include "base/macros.h"
14 #include "mojo/public/cpp/bindings/pending_remote.h"
15 #include "mojo/public/cpp/bindings/remote.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/dns/host_resolver.h"
18 #include "net/dns/public/dns_query_type.h"
19 #include "services/network/public/mojom/host_resolver.mojom.h"
20 
21 namespace net {
22 class HostPortPair;
23 }  // namespace net
24 
25 namespace network {
26 
27 class HostResolverMdnsListener
28     : public net::HostResolver::MdnsListener::Delegate {
29  public:
30   HostResolverMdnsListener(net::HostResolver* resolver,
31                            const net::HostPortPair& host,
32                            net::DnsQueryType query_type);
33   ~HostResolverMdnsListener() override;
34 
35   int Start(mojo::PendingRemote<mojom::MdnsListenClient> response_client,
36             base::OnceClosure cancellation_callback);
37 
38   // net::HostResolver::MdnsListenerDelegate implementation
39   void OnAddressResult(
40       net::HostResolver::MdnsListener::Delegate::UpdateType update_type,
41       net::DnsQueryType query_type,
42       net::IPEndPoint address) override;
43   void OnTextResult(
44       net::HostResolver::MdnsListener::Delegate::UpdateType update_type,
45       net::DnsQueryType query_type,
46       std::vector<std::string> text_records) override;
47   void OnHostnameResult(
48       net::HostResolver::MdnsListener::Delegate::UpdateType update_type,
49       net::DnsQueryType query_type,
50       net::HostPortPair host) override;
51   void OnUnhandledResult(
52       net::HostResolver::MdnsListener::Delegate::UpdateType update_type,
53       net::DnsQueryType query_type) override;
54 
55  private:
56   void OnConnectionError();
57 
58   std::unique_ptr<net::HostResolver::MdnsListener> internal_listener_;
59   mojo::Remote<mojom::MdnsListenClient> response_client_;
60 
61   base::OnceClosure cancellation_callback_;
62 
63   DISALLOW_COPY_AND_ASSIGN(HostResolverMdnsListener);
64 };
65 
66 }  // namespace network
67 
68 #endif  // SERVICES_NETWORK_HOST_RESOLVER_MDNS_LISTENER_H_
69