OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MDNS_DNS_SD_REGISTRY_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_MDNS_DNS_SD_REGISTRY_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <string> | |
11 #include <utility> | |
12 #include <vector> | |
13 | |
14 #include "base/macros.h" | |
15 #include "base/observer_list.h" | |
16 #include "chrome/browser/extensions/api/mdns/dns_sd_delegate.h" | |
17 | |
18 namespace local_discovery { | |
19 class ServiceDiscoverySharedClient; | |
20 } | |
21 | |
22 namespace extensions { | |
23 | |
24 class DnsSdDeviceLister; | |
25 class ServiceTypeData; | |
26 | |
27 // Registry class for keeping track of discovered network services over DNS-SD. | |
28 class DnsSdRegistry : public DnsSdDelegate { | |
29 public: | |
30 typedef std::vector<DnsSdService> DnsSdServiceList; | |
31 | |
32 class DnsSdObserver { | |
33 public: | |
34 virtual void OnDnsSdEvent(const std::string& service_type, | |
35 const DnsSdServiceList& services) = 0; | |
36 | |
37 protected: | |
38 virtual ~DnsSdObserver() {} | |
39 }; | |
40 | |
41 DnsSdRegistry(); | |
42 explicit DnsSdRegistry(local_discovery::ServiceDiscoverySharedClient* client); | |
43 virtual ~DnsSdRegistry(); | |
44 | |
45 // Publishes the current device list for |service_type| to event listeners | |
46 // whose event filter matches the service type. | |
47 virtual void Publish(const std::string& service_type); | |
48 | |
49 // Immediately issues a multicast DNS query for all service types of the | |
50 // calling extension. | |
51 virtual void ForceDiscovery(); | |
52 | |
53 // Observer registration for parties interested in discovery events. | |
54 virtual void AddObserver(DnsSdObserver* observer); | |
55 virtual void RemoveObserver(DnsSdObserver* observer); | |
56 | |
57 // DNS-SD-related discovery functionality. | |
58 virtual void RegisterDnsSdListener(const std::string& service_type); | |
59 virtual void UnregisterDnsSdListener(const std::string& service_type); | |
60 | |
61 protected: | |
62 // Data class for managing all the resources and information related to a | |
63 // particular service type. | |
64 class ServiceTypeData { | |
65 public: | |
66 explicit ServiceTypeData(std::unique_ptr<DnsSdDeviceLister> lister); | |
67 virtual ~ServiceTypeData(); | |
68 | |
69 // Notify the data class of listeners so that it can be reference counted. | |
70 void ListenerAdded(); | |
71 // Returns true if the last listener was removed. | |
72 bool ListenerRemoved(); | |
73 int GetListenerCount(); | |
74 | |
75 // Immediately issues a multicast DNS query for the service type owned by | |
76 // |this|. | |
77 void ForceDiscovery(); | |
78 | |
79 // Methods for adding, updating or removing services for this service type. | |
80 bool UpdateService(bool added, const DnsSdService& service); | |
81 bool RemoveService(const std::string& service_name); | |
82 // Called when the discovery service was restarted. | |
83 // Clear the local cache and initiate rediscovery. | |
84 bool ClearServices(); | |
85 | |
86 const DnsSdRegistry::DnsSdServiceList& GetServiceList(); | |
87 | |
88 private: | |
89 int ref_count; | |
90 std::unique_ptr<DnsSdDeviceLister> lister_; | |
91 DnsSdRegistry::DnsSdServiceList service_list_; | |
92 DISALLOW_COPY_AND_ASSIGN(ServiceTypeData); | |
93 }; | |
94 | |
95 virtual DnsSdDeviceLister* CreateDnsSdDeviceLister( | |
96 DnsSdDelegate* delegate, | |
97 const std::string& service_type, | |
98 local_discovery::ServiceDiscoverySharedClient* discovery_client); | |
99 | |
100 // DnsSdDelegate implementation: | |
101 void ServiceChanged(const std::string& service_type, | |
102 bool added, | |
103 const DnsSdService& service) override; | |
104 void ServiceRemoved(const std::string& service_type, | |
105 const std::string& service_name) override; | |
106 void ServicesFlushed(const std::string& service_type) override; | |
107 | |
108 std::map<std::string, std::unique_ptr<ServiceTypeData>> service_data_map_; | |
109 | |
110 private: | |
111 void DispatchApiEvent(const std::string& service_type); | |
112 bool IsRegistered(const std::string& service_type); | |
113 | |
114 scoped_refptr<local_discovery::ServiceDiscoverySharedClient> | |
115 service_discovery_client_; | |
116 base::ObserverList<DnsSdObserver> observers_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(DnsSdRegistry); | |
119 }; | |
120 | |
121 } // namespace extensions | |
122 | |
123 #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_DNS_SD_REGISTRY_H_ | |
OLD | NEW |