[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 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_MDNS_API_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ |
| 7 | |
kmarshall | 261ed05 | 2015-06-26 18:58:51 | [diff] [blame] | 8 | #include <map> |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
| 12 | |
kmarshall | 261ed05 | 2015-06-26 18:58:51 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 15 | #include "base/threading/thread_checker.h" |
zhaobin | b9946bb2 | 2017-05-16 17:46:49 | [diff] [blame] | 16 | #include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h" |
kmarshall | cbfeea8f2 | 2015-07-09 19:30:11 | [diff] [blame] | 17 | #include "chrome/common/extensions/api/mdns.h" |
| 18 | #include "extensions/browser/api/async_api_function.h" |
[email protected] | 4bf3bed | 2014-03-05 10:21:02 | [diff] [blame] | 19 | #include "extensions/browser/browser_context_keyed_api_factory.h" |
[email protected] | 3442353 | 2013-11-21 18:13:10 | [diff] [blame] | 20 | #include "extensions/browser/event_router.h" |
reddaly | 76db6018 | 2015-04-09 02:39:18 | [diff] [blame] | 21 | #include "extensions/browser/extension_function.h" |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 22 | |
[email protected] | 21eaf354 | 2014-02-25 17:47:52 | [diff] [blame] | 23 | namespace content { |
| 24 | class BrowserContext; |
| 25 | } |
| 26 | |
zhaobin | b9946bb2 | 2017-05-16 17:46:49 | [diff] [blame] | 27 | namespace extensions { |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 28 | // MDnsAPI is instantiated with the profile and will listen for extensions that |
| 29 | // register listeners for the chrome.mdns extension API. It will use a registry |
| 30 | // class to start the mDNS listener process (if necessary) and observe new |
| 31 | // service events to dispatch them to registered extensions. |
[email protected] | 4bf3bed | 2014-03-05 10:21:02 | [diff] [blame] | 32 | class MDnsAPI : public BrowserContextKeyedAPI, |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 33 | public EventRouter::Observer, |
zhaobin | b9946bb2 | 2017-05-16 17:46:49 | [diff] [blame] | 34 | public media_router::DnsSdRegistry::DnsSdObserver { |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 35 | public: |
[email protected] | 21eaf354 | 2014-02-25 17:47:52 | [diff] [blame] | 36 | explicit MDnsAPI(content::BrowserContext* context); |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 37 | ~MDnsAPI() override; |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 38 | |
[email protected] | 21eaf354 | 2014-02-25 17:47:52 | [diff] [blame] | 39 | static MDnsAPI* Get(content::BrowserContext* context); |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 40 | |
[email protected] | 4bf3bed | 2014-03-05 10:21:02 | [diff] [blame] | 41 | // BrowserContextKeyedAPI implementation. |
| 42 | static BrowserContextKeyedAPIFactory<MDnsAPI>* GetFactoryInstance(); |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 43 | |
zhaobin | 7b350ba | 2017-05-16 20:13:02 | [diff] [blame] | 44 | // Used to mock out the DnsSdRegistry for testing. Does not take ownership of |
| 45 | // |registry|. |
| 46 | void SetDnsSdRegistryForTesting(media_router::DnsSdRegistry* registry); |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 47 | |
kmarshall | cbfeea8f2 | 2015-07-09 19:30:11 | [diff] [blame] | 48 | // Immediately issues a multicast DNS query for all service types. |
| 49 | // NOTE: Discovery queries are sent to all event handlers associated with |
| 50 | // |this| service's BrowserContext. |
| 51 | void ForceDiscovery(); |
| 52 | |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 53 | protected: |
| 54 | // Retrieve an instance of the registry. Lazily created when needed. |
zhaobin | b9946bb2 | 2017-05-16 17:46:49 | [diff] [blame] | 55 | virtual media_router::DnsSdRegistry* dns_sd_registry(); |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 56 | |
kmarshall | 261ed05 | 2015-06-26 18:58:51 | [diff] [blame] | 57 | // Gets the list of mDNS event listeners. |
| 58 | virtual const extensions::EventListenerMap::ListenerList& GetEventListeners(); |
| 59 | |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 60 | private: |
kmarshall | 261ed05 | 2015-06-26 18:58:51 | [diff] [blame] | 61 | FRIEND_TEST_ALL_PREFIXES(MDnsAPIDiscoveryTest, |
| 62 | ServiceListenersAddedAndRemoved); |
| 63 | |
| 64 | typedef std::map<std::string, int> ServiceTypeCounts; |
[email protected] | 4bf3bed | 2014-03-05 10:21:02 | [diff] [blame] | 65 | friend class BrowserContextKeyedAPIFactory<MDnsAPI>; |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 66 | |
| 67 | // EventRouter::Observer: |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 68 | void OnListenerAdded(const EventListenerInfo& details) override; |
| 69 | void OnListenerRemoved(const EventListenerInfo& details) override; |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 70 | |
| 71 | // DnsSdRegistry::Observer |
zhaobin | b9946bb2 | 2017-05-16 17:46:49 | [diff] [blame] | 72 | void OnDnsSdEvent( |
| 73 | const std::string& service_type, |
| 74 | const media_router::DnsSdRegistry::DnsSdServiceList& services) override; |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 75 | |
[email protected] | 4bf3bed | 2014-03-05 10:21:02 | [diff] [blame] | 76 | // BrowserContextKeyedAPI implementation. |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 77 | static const char* service_name() { |
| 78 | return "MDnsAPI"; |
| 79 | } |
| 80 | |
| 81 | static const bool kServiceIsCreatedWithBrowserContext = true; |
| 82 | static const bool kServiceIsNULLWhileTesting = true; |
| 83 | |
| 84 | // Update the current list of service types and update the registry. |
kmarshall | 261ed05 | 2015-06-26 18:58:51 | [diff] [blame] | 85 | void UpdateMDnsListeners(); |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 86 | |
reddaly | 76db6018 | 2015-04-09 02:39:18 | [diff] [blame] | 87 | // Write a message to the consoles of extensions listening to a given service |
| 88 | // type. |
| 89 | void WriteToConsole(const std::string& service_type, |
Abhijeet Kandalkar | a5928a0a | 2019-03-13 05:04:42 | [diff] [blame] | 90 | blink::mojom::ConsoleMessageLevel level, |
reddaly | 76db6018 | 2015-04-09 02:39:18 | [diff] [blame] | 91 | const std::string& message); |
| 92 | |
kmarshall | 261ed05 | 2015-06-26 18:58:51 | [diff] [blame] | 93 | // Returns true if an extension or platform app |extension_id| is allowed to |
| 94 | // listen to mDNS events for |service_type|. |
| 95 | virtual bool IsMDnsAllowed(const std::string& extension_id, |
| 96 | const std::string& service_type) const; |
| 97 | |
reddaly | 76db6018 | 2015-04-09 02:39:18 | [diff] [blame] | 98 | // Finds all all the valid listeners of the mdns.onServiceList event and |
kmarshall | 261ed05 | 2015-06-26 18:58:51 | [diff] [blame] | 99 | // filters them by service type if |service_type_filter| is non-empty. |
| 100 | // The list of extensions with active listeners is written to |extension_ids|, |
| 101 | // if non-null. |
| 102 | // The counts for each service type are output to |service_type_counts|, if |
| 103 | // non-null. |
reddaly | 76db6018 | 2015-04-09 02:39:18 | [diff] [blame] | 104 | void GetValidOnServiceListListeners(const std::string& service_type_filter, |
| 105 | std::set<std::string>* extension_ids, |
kmarshall | 261ed05 | 2015-06-26 18:58:51 | [diff] [blame] | 106 | ServiceTypeCounts* service_type_counts); |
reddaly | 76db6018 | 2015-04-09 02:39:18 | [diff] [blame] | 107 | |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 108 | // Ensure methods are only called on UI thread. |
| 109 | base::ThreadChecker thread_checker_; |
[email protected] | 21eaf354 | 2014-02-25 17:47:52 | [diff] [blame] | 110 | content::BrowserContext* const browser_context_; |
zhaobin | 7b350ba | 2017-05-16 20:13:02 | [diff] [blame] | 111 | // Raw pointer to a leaky singleton. Lazily created on first access. Must |
| 112 | // outlive this object. |
| 113 | media_router::DnsSdRegistry* dns_sd_registry_; |
kmarshall | 261ed05 | 2015-06-26 18:58:51 | [diff] [blame] | 114 | // Count of active listeners per service type, saved from the previous |
| 115 | // invocation of UpdateMDnsListeners(). |
| 116 | ServiceTypeCounts prev_service_counts_; |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 117 | |
| 118 | DISALLOW_COPY_AND_ASSIGN(MDnsAPI); |
| 119 | }; |
| 120 | |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 121 | class MdnsForceDiscoveryFunction : public ExtensionFunction { |
kmarshall | cbfeea8f2 | 2015-07-09 19:30:11 | [diff] [blame] | 122 | public: |
| 123 | MdnsForceDiscoveryFunction(); |
| 124 | |
| 125 | protected: |
| 126 | ~MdnsForceDiscoveryFunction() override; |
| 127 | |
| 128 | private: |
Clark DuVall | fd4db3d | 2019-07-30 19:10:43 | [diff] [blame] | 129 | // ExtensionFunction override. |
kmarshall | cbfeea8f2 | 2015-07-09 19:30:11 | [diff] [blame] | 130 | ResponseAction Run() override; |
| 131 | |
Nico Weber | 6e0f7aa | 2019-02-11 03:07:43 | [diff] [blame] | 132 | DECLARE_EXTENSION_FUNCTION("mdns.forceDiscovery", MDNS_FORCEDISCOVERY) |
kmarshall | cbfeea8f2 | 2015-07-09 19:30:11 | [diff] [blame] | 133 | DISALLOW_COPY_AND_ASSIGN(MdnsForceDiscoveryFunction); |
| 134 | }; |
| 135 | |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 136 | } // namespace extensions |
| 137 | |
| 138 | #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ |