1 // Copyright 2014 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.
5 #ifndef CHROME_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_
6 #define CHROME_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_
12 #include "base/callback.h"
13 #include "base/cancelable_callback.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "chrome/common/local_discovery/service_discovery_client.h"
18 #include "net/dns/mdns_client.h"
20 namespace local_discovery
{
22 class ServiceDiscoveryClientImpl
: public ServiceDiscoveryClient
{
24 // |mdns_client| must outlive the Service Discovery Client.
25 explicit ServiceDiscoveryClientImpl(net::MDnsClient
* mdns_client
);
26 ~ServiceDiscoveryClientImpl() override
;
28 // ServiceDiscoveryClient implementation:
29 scoped_ptr
<ServiceWatcher
> CreateServiceWatcher(
30 const std::string
& service_type
,
31 const ServiceWatcher::UpdatedCallback
& callback
) override
;
33 scoped_ptr
<ServiceResolver
> CreateServiceResolver(
34 const std::string
& service_name
,
35 const ServiceResolver::ResolveCompleteCallback
& callback
) override
;
37 scoped_ptr
<LocalDomainResolver
> CreateLocalDomainResolver(
38 const std::string
& domain
,
39 net::AddressFamily address_family
,
40 const LocalDomainResolver::IPAddressCallback
& callback
) override
;
43 net::MDnsClient
* mdns_client_
;
45 DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryClientImpl
);
48 class ServiceWatcherImpl
: public ServiceWatcher
,
49 public net::MDnsListener::Delegate
,
50 public base::SupportsWeakPtr
<ServiceWatcherImpl
> {
52 ServiceWatcherImpl(const std::string
& service_type
,
53 const ServiceWatcher::UpdatedCallback
& callback
,
54 net::MDnsClient
* mdns_client
);
55 // Listening will automatically stop when the destructor is called.
56 ~ServiceWatcherImpl() override
;
58 // ServiceWatcher implementation:
59 void Start() override
;
61 void DiscoverNewServices(bool force_update
) override
;
63 void SetActivelyRefreshServices(bool actively_refresh_services
) override
;
65 std::string
GetServiceType() const override
;
67 void OnRecordUpdate(net::MDnsListener::UpdateType update
,
68 const net::RecordParsed
* record
) override
;
70 void OnNsecRecord(const std::string
& name
, unsigned rrtype
) override
;
72 void OnCachePurged() override
;
74 virtual void OnTransactionResponse(
75 scoped_ptr
<net::MDnsTransaction
>* transaction
,
76 net::MDnsTransaction::Result result
,
77 const net::RecordParsed
* record
);
80 struct ServiceListeners
{
81 ServiceListeners(const std::string
& service_name
,
82 ServiceWatcherImpl
* watcher
,
83 net::MDnsClient
* mdns_client
);
86 void SetActiveRefresh(bool auto_update
);
88 void set_update_pending(bool update_pending
) {
89 update_pending_
= update_pending
;
92 bool update_pending() { return update_pending_
; }
94 void set_has_ptr(bool has_ptr
) {
98 void set_has_srv(bool has_srv
);
100 bool has_ptr_or_srv() { return has_ptr_
|| has_srv_
; }
103 void OnSRVRecord(net::MDnsTransaction::Result result
,
104 const net::RecordParsed
* record
);
108 scoped_ptr
<net::MDnsListener
> srv_listener_
;
109 scoped_ptr
<net::MDnsListener
> txt_listener_
;
110 scoped_ptr
<net::MDnsTransaction
> srv_transaction_
;
112 std::string service_name_
;
113 net::MDnsClient
* mdns_client_
;
114 bool update_pending_
;
120 typedef std::map
<std::string
, linked_ptr
<ServiceListeners
> >
123 void ReadCachedServices();
124 void AddService(const std::string
& service
);
125 void RemovePTR(const std::string
& service
);
126 void RemoveSRV(const std::string
& service
);
127 void AddSRV(const std::string
& service
);
128 bool CreateTransaction(bool active
, bool alert_existing_services
,
130 scoped_ptr
<net::MDnsTransaction
>* transaction
);
132 void DeferUpdate(ServiceWatcher::UpdateType update_type
,
133 const std::string
& service_name
);
134 void DeliverDeferredUpdate(ServiceWatcher::UpdateType update_type
,
135 const std::string
& service_name
);
137 void ScheduleQuery(int timeout_seconds
);
139 void SendQuery(int next_timeout_seconds
, bool force_update
);
141 std::string service_type_
;
142 ServiceListenersMap services_
;
143 scoped_ptr
<net::MDnsTransaction
> transaction_network_
;
144 scoped_ptr
<net::MDnsTransaction
> transaction_cache_
;
145 scoped_ptr
<net::MDnsListener
> listener_
;
147 ServiceWatcher::UpdatedCallback callback_
;
149 bool actively_refresh_services_
;
151 net::MDnsClient
* mdns_client_
;
153 DISALLOW_COPY_AND_ASSIGN(ServiceWatcherImpl
);
156 class ServiceResolverImpl
157 : public ServiceResolver
,
158 public base::SupportsWeakPtr
<ServiceResolverImpl
> {
160 ServiceResolverImpl(const std::string
& service_name
,
161 const ServiceResolver::ResolveCompleteCallback
& callback
,
162 net::MDnsClient
* mdns_client
);
164 ~ServiceResolverImpl() override
;
166 // ServiceResolver implementation:
167 void StartResolving() override
;
169 std::string
GetName() const override
;
172 // Respond to transaction finishing for SRV records.
173 void SrvRecordTransactionResponse(net::MDnsTransaction::Result status
,
174 const net::RecordParsed
* record
);
176 // Respond to transaction finishing for TXT records.
177 void TxtRecordTransactionResponse(net::MDnsTransaction::Result status
,
178 const net::RecordParsed
* record
);
180 // Respond to transaction finishing for A records.
181 void ARecordTransactionResponse(net::MDnsTransaction::Result status
,
182 const net::RecordParsed
* record
);
184 void AlertCallbackIfReady();
186 void ServiceNotFound(RequestStatus status
);
188 // Convert a TXT record to a vector of strings (metadata).
189 const std::vector
<std::string
>& RecordToMetadata(
190 const net::RecordParsed
* record
) const;
192 // Convert an SRV record to a host and port pair.
193 net::HostPortPair
RecordToAddress(
194 const net::RecordParsed
* record
) const;
196 // Convert an A record to an IP address.
197 const net::IPAddressNumber
& RecordToIPAddress(
198 const net::RecordParsed
* record
) const;
200 // Convert an MDns status to a service discovery status.
201 RequestStatus
MDnsStatusToRequestStatus(
202 net::MDnsTransaction::Result status
) const;
204 bool CreateTxtTransaction();
205 bool CreateSrvTransaction();
206 void CreateATransaction();
208 std::string service_name_
;
209 ResolveCompleteCallback callback_
;
211 bool metadata_resolved_
;
212 bool address_resolved_
;
214 scoped_ptr
<net::MDnsTransaction
> txt_transaction_
;
215 scoped_ptr
<net::MDnsTransaction
> srv_transaction_
;
216 scoped_ptr
<net::MDnsTransaction
> a_transaction_
;
218 ServiceDescription service_staging_
;
220 net::MDnsClient
* mdns_client_
;
222 DISALLOW_COPY_AND_ASSIGN(ServiceResolverImpl
);
225 class LocalDomainResolverImpl
: public LocalDomainResolver
{
227 LocalDomainResolverImpl(const std::string
& domain
,
228 net::AddressFamily address_family
,
229 const IPAddressCallback
& callback
,
230 net::MDnsClient
* mdns_client
);
231 ~LocalDomainResolverImpl() override
;
233 void Start() override
;
235 const std::string
& domain() { return domain_
; }
238 void OnTransactionComplete(
239 net::MDnsTransaction::Result result
,
240 const net::RecordParsed
* record
);
242 scoped_ptr
<net::MDnsTransaction
> CreateTransaction(uint16 type
);
246 void SendResolvedAddresses();
249 net::AddressFamily address_family_
;
250 IPAddressCallback callback_
;
252 scoped_ptr
<net::MDnsTransaction
> transaction_a_
;
253 scoped_ptr
<net::MDnsTransaction
> transaction_aaaa_
;
255 int transactions_finished_
;
257 net::MDnsClient
* mdns_client_
;
259 net::IPAddressNumber address_ipv4_
;
260 net::IPAddressNumber address_ipv6_
;
262 base::CancelableCallback
<void()> timeout_callback_
;
264 DISALLOW_COPY_AND_ASSIGN(LocalDomainResolverImpl
);
268 } // namespace local_discovery
270 #endif // CHROME_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_