xref: /aosp_15_r20/external/cronet/net/dns/host_resolver_mdns_task.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 The Chromium Authors
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 NET_DNS_HOST_RESOLVER_MDNS_TASK_H_
6 #define NET_DNS_HOST_RESOLVER_MDNS_TASK_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/containers/unique_ptr_adapters.h"
13 #include "base/functional/callback_forward.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/sequence_checker.h"
17 #include "net/dns/host_cache.h"
18 #include "net/dns/host_resolver.h"
19 #include "net/dns/mdns_client.h"
20 #include "net/dns/public/dns_query_type.h"
21 
22 namespace net {
23 
24 class RecordParsed;
25 
26 // Representation of a single HostResolverImpl::Job task to resolve the hostname
27 // using multicast DNS transactions.  Destruction cancels the task and prevents
28 // any callbacks from being invoked.
29 class HostResolverMdnsTask {
30  public:
31   // |mdns_client| must outlive |this|.
32   HostResolverMdnsTask(MDnsClient* mdns_client,
33                        std::string hostname,
34                        DnsQueryTypeSet query_types);
35 
36   HostResolverMdnsTask(const HostResolverMdnsTask&) = delete;
37   HostResolverMdnsTask& operator=(const HostResolverMdnsTask&) = delete;
38 
39   ~HostResolverMdnsTask();
40 
41   // Starts the task. |completion_closure| will be called asynchronously.
42   //
43   // Should only be called once.
44   void Start(base::OnceClosure completion_closure);
45 
46   // Results only available after invocation of the completion closure.
47   HostCache::Entry GetResults() const;
48 
49   static HostCache::Entry ParseResult(int error,
50                                       DnsQueryType query_type,
51                                       const RecordParsed* parsed,
52                                       const std::string& expected_hostname);
53 
54  private:
55   class Transaction;
56 
57   void CheckCompletion(bool post_needed);
58   void Complete(bool post_needed);
59 
60   const raw_ptr<MDnsClient> mdns_client_;
61 
62   const std::string hostname_;
63 
64   std::vector<Transaction> transactions_;
65 
66   base::OnceClosure completion_closure_;
67 
68   SEQUENCE_CHECKER(sequence_checker_);
69 
70   base::WeakPtrFactory<HostResolverMdnsTask> weak_ptr_factory_{this};
71 };
72 
73 }  // namespace net
74 
75 #endif  // NET_DNS_HOST_RESOLVER_MDNS_TASK_H_
76