1 // Copyright 2024 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_MANAGER_SERVICE_ENDPOINT_REQUEST_IMPL_H_ 6 #define NET_DNS_HOST_RESOLVER_MANAGER_SERVICE_ENDPOINT_REQUEST_IMPL_H_ 7 8 #include <optional> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "base/containers/linked_list.h" 14 #include "base/memory/raw_ptr.h" 15 #include "base/memory/safe_ref.h" 16 #include "base/memory/weak_ptr.h" 17 #include "base/sequence_checker.h" 18 #include "base/time/tick_clock.h" 19 #include "net/base/network_anonymization_key.h" 20 #include "net/base/request_priority.h" 21 #include "net/dns/host_resolver.h" 22 #include "net/dns/host_resolver_manager.h" 23 #include "net/dns/host_resolver_manager_job.h" 24 #include "net/dns/resolve_context.h" 25 #include "net/log/net_log_with_source.h" 26 #include "url/scheme_host_port.h" 27 28 namespace net { 29 30 // Implementation of HostResolver::ServiceEndpointRequest. 31 class HostResolverManager::ServiceEndpointRequestImpl 32 : public HostResolver::ServiceEndpointRequest, 33 public base::LinkNode<HostResolverManager::ServiceEndpointRequestImpl> { 34 public: 35 ServiceEndpointRequestImpl(url::SchemeHostPort scheme_host_port, 36 NetworkAnonymizationKey network_anonymization_key, 37 NetLogWithSource net_log, 38 ResolveHostParameters parameters, 39 base::WeakPtr<ResolveContext> resolve_context, 40 base::WeakPtr<HostResolverManager> manager, 41 const base::TickClock* tick_clock); 42 43 ServiceEndpointRequestImpl(const ServiceEndpointRequestImpl&) = delete; 44 ServiceEndpointRequestImpl& operator=(const ServiceEndpointRequestImpl&) = 45 delete; 46 47 ~ServiceEndpointRequestImpl() override; 48 49 // HostResolver::ServiceEndpointRequest implementations: 50 int Start(Delegate* delegate) override; 51 const std::vector<ServiceEndpoint>& GetEndpointResults() override; 52 const std::set<std::string>& GetDnsAliasResults() override; 53 bool EndpointsCryptoReady() override; 54 55 // These should only be called from HostResolver::Job. 56 void AssignJob(base::SafeRef<Job> job); 57 void OnJobCompleted(const HostCache::Entry& results, bool obtained_securely); 58 void OnJobCancelled(); 59 void OnServiceEndpointsChanged(); 60 net_log()61 const NetLogWithSource& net_log() const { return net_log_; } 62 parameters()63 const ResolveHostParameters& parameters() const { return parameters_; } 64 65 // TODO(crbug.com/41493696): Support setting priority. priority()66 RequestPriority priority() const { return priority_; } 67 host_cache()68 HostCache* host_cache() const { 69 return resolve_context_ ? resolve_context_->host_cache() : nullptr; 70 } 71 72 base::WeakPtr<ServiceEndpointRequestImpl> GetWeakPtr(); 73 74 private: 75 void SetFinalizedResultFromLegacyResults(const HostCache::Entry& results); 76 77 void LogCancelRequest(); 78 79 const HostResolver::Host host_; 80 const NetworkAnonymizationKey network_anonymization_key_; 81 const NetLogWithSource net_log_; 82 ResolveHostParameters parameters_; 83 base::WeakPtr<ResolveContext> resolve_context_; 84 base::WeakPtr<HostResolverManager> manager_; 85 const raw_ptr<const base::TickClock> tick_clock_; 86 RequestPriority priority_; 87 88 // `delegate_` must outlive `this` unless `resolve_context_` becomes invalid. 89 raw_ptr<Delegate> delegate_; 90 91 // Holds the finalized results. 92 struct FinalizedResult { 93 FinalizedResult(std::vector<ServiceEndpoint> endpoints, 94 std::set<std::string> dns_aliases); 95 ~FinalizedResult(); 96 97 FinalizedResult(FinalizedResult&&); 98 FinalizedResult& operator=(FinalizedResult&&); 99 100 std::vector<ServiceEndpoint> endpoints; 101 std::set<std::string> dns_aliases; 102 }; 103 104 // Set when the endpoint results are finalized. 105 std::optional<FinalizedResult> finalized_result_; 106 107 // Set when a job is associated with `this`. Must be valid unless 108 // `resolve_context_` becomes invalid. Cleared when the endpoints are 109 // finalized to ensure that `job_` doesn't become a dangling pointer. 110 std::optional<base::SafeRef<Job>> job_; 111 112 ResolveErrorInfo error_info_; 113 114 SEQUENCE_CHECKER(sequence_checker_); 115 116 base::WeakPtrFactory<ServiceEndpointRequestImpl> weak_ptr_factory_{this}; 117 }; 118 119 } // namespace net 120 121 #endif // NET_DNS_HOST_RESOLVER_MANAGER_SERVICE_ENDPOINT_REQUEST_IMPL_H_ 122