1 // Copyright 2013 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_BROWSER_EXTENSIONS_API_MDNS_DNS_SD_REGISTRY_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_MDNS_DNS_SD_REGISTRY_H_
13 #include "base/memory/linked_ptr.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "chrome/browser/extensions/api/mdns/dns_sd_delegate.h"
18 namespace local_discovery
{
19 class ServiceDiscoverySharedClient
;
20 class ServiceDiscoveryClient
;
23 namespace extensions
{
25 class DnsSdDeviceLister
;
26 class ServiceTypeData
;
28 // Registry class for keeping track of discovered network services over DNS-SD.
29 class DnsSdRegistry
: public DnsSdDelegate
{
31 typedef std::vector
<DnsSdService
> DnsSdServiceList
;
35 virtual void OnDnsSdEvent(const std::string
& service_type
,
36 const DnsSdServiceList
& services
) = 0;
39 virtual ~DnsSdObserver() {}
43 explicit DnsSdRegistry(local_discovery::ServiceDiscoverySharedClient
* client
);
44 virtual ~DnsSdRegistry();
46 // Publishes the current device list for |service_type| to event listeners
47 // whose event filter matches the service type.
48 virtual void Publish(const std::string
& service_type
);
50 // Immediately issues a multicast DNS query for all service types of the
52 virtual void ForceDiscovery();
54 // Observer registration for parties interested in discovery events.
55 virtual void AddObserver(DnsSdObserver
* observer
);
56 virtual void RemoveObserver(DnsSdObserver
* observer
);
58 // DNS-SD-related discovery functionality.
59 virtual void RegisterDnsSdListener(const std::string
& service_type
);
60 virtual void UnregisterDnsSdListener(const std::string
& service_type
);
63 // Data class for managing all the resources and information related to a
64 // particular service type.
65 class ServiceTypeData
{
67 explicit ServiceTypeData(scoped_ptr
<DnsSdDeviceLister
> lister
);
68 virtual ~ServiceTypeData();
70 // Notify the data class of listeners so that it can be reference counted.
72 // Returns true if the last listener was removed.
73 bool ListenerRemoved();
74 int GetListenerCount();
76 // Immediately issues a multicast DNS query for the service type owned by
78 void ForceDiscovery();
80 // Methods for adding, updating or removing services for this service type.
81 bool UpdateService(bool added
, const DnsSdService
& service
);
82 bool RemoveService(const std::string
& service_name
);
83 // Called when the discovery service was restarted.
84 // Clear the local cache and initiate rediscovery.
87 const DnsSdRegistry::DnsSdServiceList
& GetServiceList();
91 scoped_ptr
<DnsSdDeviceLister
> lister_
;
92 DnsSdRegistry::DnsSdServiceList service_list_
;
93 DISALLOW_COPY_AND_ASSIGN(ServiceTypeData
);
96 // Maps service types to associated data such as listers and service lists.
97 typedef std::map
<std::string
, linked_ptr
<ServiceTypeData
> >
98 DnsSdServiceTypeDataMap
;
100 virtual DnsSdDeviceLister
* CreateDnsSdDeviceLister(
101 DnsSdDelegate
* delegate
,
102 const std::string
& service_type
,
103 local_discovery::ServiceDiscoverySharedClient
* discovery_client
);
105 // DnsSdDelegate implementation:
106 void ServiceChanged(const std::string
& service_type
,
108 const DnsSdService
& service
) override
;
109 void ServiceRemoved(const std::string
& service_type
,
110 const std::string
& service_name
) override
;
111 void ServicesFlushed(const std::string
& service_type
) override
;
113 DnsSdServiceTypeDataMap service_data_map_
;
116 void DispatchApiEvent(const std::string
& service_type
);
117 bool IsRegistered(const std::string
& service_type
);
119 scoped_refptr
<local_discovery::ServiceDiscoverySharedClient
>
120 service_discovery_client_
;
121 base::ObserverList
<DnsSdObserver
> observers_
;
123 DISALLOW_COPY_AND_ASSIGN(DnsSdRegistry
);
126 } // namespace extensions
128 #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_DNS_SD_REGISTRY_H_