Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 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 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 | |
Matt Menke | c35d163 | 2018-11-29 12:43:49 | [diff] [blame] | 12 | #include "base/callback_forward.h" |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 13 | #include "base/containers/unique_ptr_adapters.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
| 16 | #include "base/sequence_checker.h" |
Matt Menke | c35d163 | 2018-11-29 12:43:49 | [diff] [blame] | 17 | #include "net/dns/host_cache.h" |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 18 | #include "net/dns/host_resolver.h" |
| 19 | #include "net/dns/mdns_client.h" |
Eric Orth | 192e3bb | 2018-11-14 19:30:32 | [diff] [blame] | 20 | #include "net/dns/public/dns_query_type.h" |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 21 | |
| 22 | namespace net { |
| 23 | |
Eric Orth | 026776a | 2019-01-18 00:13:28 | [diff] [blame] | 24 | class RecordParsed; |
| 25 | |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 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|. |
Eric Orth | 192e3bb | 2018-11-14 19:30:32 | [diff] [blame] | 32 | HostResolverMdnsTask(MDnsClient* mdns_client, |
Eric Orth | c4cca5d | 2021-07-02 19:59:01 | [diff] [blame] | 33 | std::string hostname, |
Dan McArdle | e8b05055 | 2022-01-15 00:48:23 | [diff] [blame] | 34 | DnsQueryTypeSet query_types); |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 35 | |
| 36 | HostResolverMdnsTask(const HostResolverMdnsTask&) = delete; |
| 37 | HostResolverMdnsTask& operator=(const HostResolverMdnsTask&) = delete; |
| 38 | |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 39 | ~HostResolverMdnsTask(); |
| 40 | |
Matt Menke | c35d163 | 2018-11-29 12:43:49 | [diff] [blame] | 41 | // Starts the task. |completion_closure| will be called asynchronously. |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 42 | // |
| 43 | // Should only be called once. |
Matt Menke | c35d163 | 2018-11-29 12:43:49 | [diff] [blame] | 44 | void Start(base::OnceClosure completion_closure); |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 45 | |
Matt Menke | c35d163 | 2018-11-29 12:43:49 | [diff] [blame] | 46 | // Results only available after invocation of the completion closure. |
| 47 | HostCache::Entry GetResults() const; |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 48 | |
Eric Orth | 026776a | 2019-01-18 00:13:28 | [diff] [blame] | 49 | static HostCache::Entry ParseResult(int error, |
| 50 | DnsQueryType query_type, |
| 51 | const RecordParsed* parsed, |
| 52 | const std::string& expected_hostname); |
| 53 | |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 54 | private: |
| 55 | class Transaction; |
| 56 | |
| 57 | void CheckCompletion(bool post_needed); |
Matt Menke | c35d163 | 2018-11-29 12:43:49 | [diff] [blame] | 58 | void Complete(bool post_needed); |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 59 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 60 | const raw_ptr<MDnsClient> mdns_client_; |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 61 | |
| 62 | const std::string hostname_; |
| 63 | |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 64 | std::vector<Transaction> transactions_; |
| 65 | |
Matt Menke | c35d163 | 2018-11-29 12:43:49 | [diff] [blame] | 66 | base::OnceClosure completion_closure_; |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 67 | |
| 68 | SEQUENCE_CHECKER(sequence_checker_); |
| 69 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 70 | base::WeakPtrFactory<HostResolverMdnsTask> weak_ptr_factory_{this}; |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | } // namespace net |
| 74 | |
| 75 | #endif // NET_DNS_HOST_RESOLVER_MDNS_TASK_H_ |