| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 16 #include "chrome/browser/extensions/api/mdns/dns_sd_registry.h" | 16 #include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h" |
| 17 #include "chrome/common/extensions/api/mdns.h" | 17 #include "chrome/common/extensions/api/mdns.h" |
| 18 #include "extensions/browser/api/async_api_function.h" | 18 #include "extensions/browser/api/async_api_function.h" |
| 19 #include "extensions/browser/browser_context_keyed_api_factory.h" | 19 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 20 #include "extensions/browser/event_router.h" | 20 #include "extensions/browser/event_router.h" |
| 21 #include "extensions/browser/extension_function.h" | 21 #include "extensions/browser/extension_function.h" |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 class BrowserContext; | 24 class BrowserContext; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace media_router { |
| 28 class DnsSdRegistry; |
| 29 } |
| 30 |
| 27 namespace extensions { | 31 namespace extensions { |
| 28 | |
| 29 class DnsSdRegistry; | |
| 30 | |
| 31 // MDnsAPI is instantiated with the profile and will listen for extensions that | 32 // MDnsAPI is instantiated with the profile and will listen for extensions that |
| 32 // register listeners for the chrome.mdns extension API. It will use a registry | 33 // register listeners for the chrome.mdns extension API. It will use a registry |
| 33 // class to start the mDNS listener process (if necessary) and observe new | 34 // class to start the mDNS listener process (if necessary) and observe new |
| 34 // service events to dispatch them to registered extensions. | 35 // service events to dispatch them to registered extensions. |
| 35 class MDnsAPI : public BrowserContextKeyedAPI, | 36 class MDnsAPI : public BrowserContextKeyedAPI, |
| 36 public EventRouter::Observer, | 37 public EventRouter::Observer, |
| 37 public DnsSdRegistry::DnsSdObserver { | 38 public media_router::DnsSdRegistry::DnsSdObserver { |
| 38 public: | 39 public: |
| 39 explicit MDnsAPI(content::BrowserContext* context); | 40 explicit MDnsAPI(content::BrowserContext* context); |
| 40 ~MDnsAPI() override; | 41 ~MDnsAPI() override; |
| 41 | 42 |
| 42 static MDnsAPI* Get(content::BrowserContext* context); | 43 static MDnsAPI* Get(content::BrowserContext* context); |
| 43 | 44 |
| 44 // BrowserContextKeyedAPI implementation. | 45 // BrowserContextKeyedAPI implementation. |
| 45 static BrowserContextKeyedAPIFactory<MDnsAPI>* GetFactoryInstance(); | 46 static BrowserContextKeyedAPIFactory<MDnsAPI>* GetFactoryInstance(); |
| 46 | 47 |
| 47 // Used to mock out the DnsSdRegistry for testing. | 48 // Used to mock out the DnsSdRegistry for testing. |
| 48 void SetDnsSdRegistryForTesting(std::unique_ptr<DnsSdRegistry> registry); | 49 void SetDnsSdRegistryForTesting( |
| 50 std::unique_ptr<media_router::DnsSdRegistry> registry); |
| 49 | 51 |
| 50 // Immediately issues a multicast DNS query for all service types. | 52 // Immediately issues a multicast DNS query for all service types. |
| 51 // NOTE: Discovery queries are sent to all event handlers associated with | 53 // NOTE: Discovery queries are sent to all event handlers associated with |
| 52 // |this| service's BrowserContext. | 54 // |this| service's BrowserContext. |
| 53 void ForceDiscovery(); | 55 void ForceDiscovery(); |
| 54 | 56 |
| 55 protected: | 57 protected: |
| 56 // Retrieve an instance of the registry. Lazily created when needed. | 58 // Retrieve an instance of the registry. Lazily created when needed. |
| 57 virtual DnsSdRegistry* dns_sd_registry(); | 59 virtual media_router::DnsSdRegistry* dns_sd_registry(); |
| 58 | 60 |
| 59 // Gets the list of mDNS event listeners. | 61 // Gets the list of mDNS event listeners. |
| 60 virtual const extensions::EventListenerMap::ListenerList& GetEventListeners(); | 62 virtual const extensions::EventListenerMap::ListenerList& GetEventListeners(); |
| 61 | 63 |
| 62 private: | 64 private: |
| 63 FRIEND_TEST_ALL_PREFIXES(MDnsAPIDiscoveryTest, | 65 FRIEND_TEST_ALL_PREFIXES(MDnsAPIDiscoveryTest, |
| 64 ServiceListenersAddedAndRemoved); | 66 ServiceListenersAddedAndRemoved); |
| 65 | 67 |
| 66 typedef std::map<std::string, int> ServiceTypeCounts; | 68 typedef std::map<std::string, int> ServiceTypeCounts; |
| 67 friend class BrowserContextKeyedAPIFactory<MDnsAPI>; | 69 friend class BrowserContextKeyedAPIFactory<MDnsAPI>; |
| 68 | 70 |
| 69 // EventRouter::Observer: | 71 // EventRouter::Observer: |
| 70 void OnListenerAdded(const EventListenerInfo& details) override; | 72 void OnListenerAdded(const EventListenerInfo& details) override; |
| 71 void OnListenerRemoved(const EventListenerInfo& details) override; | 73 void OnListenerRemoved(const EventListenerInfo& details) override; |
| 72 | 74 |
| 73 // DnsSdRegistry::Observer | 75 // DnsSdRegistry::Observer |
| 74 void OnDnsSdEvent(const std::string& service_type, | 76 void OnDnsSdEvent( |
| 75 const DnsSdRegistry::DnsSdServiceList& services) override; | 77 const std::string& service_type, |
| 78 const media_router::DnsSdRegistry::DnsSdServiceList& services) override; |
| 76 | 79 |
| 77 // BrowserContextKeyedAPI implementation. | 80 // BrowserContextKeyedAPI implementation. |
| 78 static const char* service_name() { | 81 static const char* service_name() { |
| 79 return "MDnsAPI"; | 82 return "MDnsAPI"; |
| 80 } | 83 } |
| 81 | 84 |
| 82 static const bool kServiceIsCreatedWithBrowserContext = true; | 85 static const bool kServiceIsCreatedWithBrowserContext = true; |
| 83 static const bool kServiceIsNULLWhileTesting = true; | 86 static const bool kServiceIsNULLWhileTesting = true; |
| 84 | 87 |
| 85 // Update the current list of service types and update the registry. | 88 // Update the current list of service types and update the registry. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 103 // The counts for each service type are output to |service_type_counts|, if | 106 // The counts for each service type are output to |service_type_counts|, if |
| 104 // non-null. | 107 // non-null. |
| 105 void GetValidOnServiceListListeners(const std::string& service_type_filter, | 108 void GetValidOnServiceListListeners(const std::string& service_type_filter, |
| 106 std::set<std::string>* extension_ids, | 109 std::set<std::string>* extension_ids, |
| 107 ServiceTypeCounts* service_type_counts); | 110 ServiceTypeCounts* service_type_counts); |
| 108 | 111 |
| 109 // Ensure methods are only called on UI thread. | 112 // Ensure methods are only called on UI thread. |
| 110 base::ThreadChecker thread_checker_; | 113 base::ThreadChecker thread_checker_; |
| 111 content::BrowserContext* const browser_context_; | 114 content::BrowserContext* const browser_context_; |
| 112 // Lazily created on first access and destroyed with this API class. | 115 // Lazily created on first access and destroyed with this API class. |
| 113 std::unique_ptr<DnsSdRegistry> dns_sd_registry_; | 116 std::unique_ptr<media_router::DnsSdRegistry> dns_sd_registry_; |
| 114 // Count of active listeners per service type, saved from the previous | 117 // Count of active listeners per service type, saved from the previous |
| 115 // invocation of UpdateMDnsListeners(). | 118 // invocation of UpdateMDnsListeners(). |
| 116 ServiceTypeCounts prev_service_counts_; | 119 ServiceTypeCounts prev_service_counts_; |
| 117 | 120 |
| 118 DISALLOW_COPY_AND_ASSIGN(MDnsAPI); | 121 DISALLOW_COPY_AND_ASSIGN(MDnsAPI); |
| 119 }; | 122 }; |
| 120 | 123 |
| 121 class MdnsForceDiscoveryFunction : public UIThreadExtensionFunction { | 124 class MdnsForceDiscoveryFunction : public UIThreadExtensionFunction { |
| 122 public: | 125 public: |
| 123 MdnsForceDiscoveryFunction(); | 126 MdnsForceDiscoveryFunction(); |
| 124 | 127 |
| 125 protected: | 128 protected: |
| 126 ~MdnsForceDiscoveryFunction() override; | 129 ~MdnsForceDiscoveryFunction() override; |
| 127 | 130 |
| 128 private: | 131 private: |
| 129 // UIThreadExtensionFunction override. | 132 // UIThreadExtensionFunction override. |
| 130 ResponseAction Run() override; | 133 ResponseAction Run() override; |
| 131 | 134 |
| 132 DECLARE_EXTENSION_FUNCTION("mdns.forceDiscovery", MDNS_FORCEDISCOVERY); | 135 DECLARE_EXTENSION_FUNCTION("mdns.forceDiscovery", MDNS_FORCEDISCOVERY); |
| 133 DISALLOW_COPY_AND_ASSIGN(MdnsForceDiscoveryFunction); | 136 DISALLOW_COPY_AND_ASSIGN(MdnsForceDiscoveryFunction); |
| 134 }; | 137 }; |
| 135 | 138 |
| 136 } // namespace extensions | 139 } // namespace extensions |
| 137 | 140 |
| 138 #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ | 141 #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ |
| OLD | NEW |