blob: dbfe3b46e414ccd18fe2087b3ab1a6c995f46a93 [file] [log] [blame]
[email protected]267864e2013-09-06 18:25:571// 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
kmarshall261ed052015-06-26 18:58:518#include <map>
dchengc963c7142016-04-08 03:55:229#include <memory>
[email protected]267864e2013-09-06 18:25:5710#include <set>
11#include <string>
12
kmarshall261ed052015-06-26 18:58:5113#include "base/gtest_prod_util.h"
avia2f4804a2015-12-24 23:11:1314#include "base/macros.h"
[email protected]267864e2013-09-06 18:25:5715#include "base/threading/thread_checker.h"
zhaobinb9946bb22017-05-16 17:46:4916#include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h"
kmarshallcbfeea8f22015-07-09 19:30:1117#include "chrome/common/extensions/api/mdns.h"
18#include "extensions/browser/api/async_api_function.h"
[email protected]4bf3bed2014-03-05 10:21:0219#include "extensions/browser/browser_context_keyed_api_factory.h"
[email protected]34423532013-11-21 18:13:1020#include "extensions/browser/event_router.h"
reddaly76db60182015-04-09 02:39:1821#include "extensions/browser/extension_function.h"
[email protected]267864e2013-09-06 18:25:5722
[email protected]21eaf3542014-02-25 17:47:5223namespace content {
24class BrowserContext;
25}
26
zhaobinb9946bb22017-05-16 17:46:4927namespace extensions {
[email protected]267864e2013-09-06 18:25:5728// 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]4bf3bed2014-03-05 10:21:0232class MDnsAPI : public BrowserContextKeyedAPI,
[email protected]267864e2013-09-06 18:25:5733 public EventRouter::Observer,
zhaobinb9946bb22017-05-16 17:46:4934 public media_router::DnsSdRegistry::DnsSdObserver {
[email protected]267864e2013-09-06 18:25:5735 public:
[email protected]21eaf3542014-02-25 17:47:5236 explicit MDnsAPI(content::BrowserContext* context);
dchengae36a4a2014-10-21 12:36:3637 ~MDnsAPI() override;
[email protected]267864e2013-09-06 18:25:5738
[email protected]21eaf3542014-02-25 17:47:5239 static MDnsAPI* Get(content::BrowserContext* context);
[email protected]267864e2013-09-06 18:25:5740
[email protected]4bf3bed2014-03-05 10:21:0241 // BrowserContextKeyedAPI implementation.
42 static BrowserContextKeyedAPIFactory<MDnsAPI>* GetFactoryInstance();
[email protected]267864e2013-09-06 18:25:5743
zhaobin7b350ba2017-05-16 20:13:0244 // Used to mock out the DnsSdRegistry for testing. Does not take ownership of
45 // |registry|.
46 void SetDnsSdRegistryForTesting(media_router::DnsSdRegistry* registry);
[email protected]267864e2013-09-06 18:25:5747
kmarshallcbfeea8f22015-07-09 19:30:1148 // 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]267864e2013-09-06 18:25:5753 protected:
54 // Retrieve an instance of the registry. Lazily created when needed.
zhaobinb9946bb22017-05-16 17:46:4955 virtual media_router::DnsSdRegistry* dns_sd_registry();
[email protected]267864e2013-09-06 18:25:5756
kmarshall261ed052015-06-26 18:58:5157 // Gets the list of mDNS event listeners.
58 virtual const extensions::EventListenerMap::ListenerList& GetEventListeners();
59
[email protected]267864e2013-09-06 18:25:5760 private:
kmarshall261ed052015-06-26 18:58:5161 FRIEND_TEST_ALL_PREFIXES(MDnsAPIDiscoveryTest,
62 ServiceListenersAddedAndRemoved);
63
64 typedef std::map<std::string, int> ServiceTypeCounts;
[email protected]4bf3bed2014-03-05 10:21:0265 friend class BrowserContextKeyedAPIFactory<MDnsAPI>;
[email protected]267864e2013-09-06 18:25:5766
67 // EventRouter::Observer:
dchengae36a4a2014-10-21 12:36:3668 void OnListenerAdded(const EventListenerInfo& details) override;
69 void OnListenerRemoved(const EventListenerInfo& details) override;
[email protected]267864e2013-09-06 18:25:5770
71 // DnsSdRegistry::Observer
zhaobinb9946bb22017-05-16 17:46:4972 void OnDnsSdEvent(
73 const std::string& service_type,
74 const media_router::DnsSdRegistry::DnsSdServiceList& services) override;
[email protected]267864e2013-09-06 18:25:5775
[email protected]4bf3bed2014-03-05 10:21:0276 // BrowserContextKeyedAPI implementation.
[email protected]267864e2013-09-06 18:25:5777 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.
kmarshall261ed052015-06-26 18:58:5185 void UpdateMDnsListeners();
[email protected]267864e2013-09-06 18:25:5786
reddaly76db60182015-04-09 02:39:1887 // Write a message to the consoles of extensions listening to a given service
88 // type.
89 void WriteToConsole(const std::string& service_type,
Abhijeet Kandalkara5928a0a2019-03-13 05:04:4290 blink::mojom::ConsoleMessageLevel level,
reddaly76db60182015-04-09 02:39:1891 const std::string& message);
92
kmarshall261ed052015-06-26 18:58:5193 // 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
reddaly76db60182015-04-09 02:39:1898 // Finds all all the valid listeners of the mdns.onServiceList event and
kmarshall261ed052015-06-26 18:58:5199 // 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.
reddaly76db60182015-04-09 02:39:18104 void GetValidOnServiceListListeners(const std::string& service_type_filter,
105 std::set<std::string>* extension_ids,
kmarshall261ed052015-06-26 18:58:51106 ServiceTypeCounts* service_type_counts);
reddaly76db60182015-04-09 02:39:18107
[email protected]267864e2013-09-06 18:25:57108 // Ensure methods are only called on UI thread.
109 base::ThreadChecker thread_checker_;
[email protected]21eaf3542014-02-25 17:47:52110 content::BrowserContext* const browser_context_;
zhaobin7b350ba2017-05-16 20:13:02111 // Raw pointer to a leaky singleton. Lazily created on first access. Must
112 // outlive this object.
113 media_router::DnsSdRegistry* dns_sd_registry_;
kmarshall261ed052015-06-26 18:58:51114 // Count of active listeners per service type, saved from the previous
115 // invocation of UpdateMDnsListeners().
116 ServiceTypeCounts prev_service_counts_;
[email protected]267864e2013-09-06 18:25:57117
118 DISALLOW_COPY_AND_ASSIGN(MDnsAPI);
119};
120
Clark DuVallfd4db3d2019-07-30 19:10:43121class MdnsForceDiscoveryFunction : public ExtensionFunction {
kmarshallcbfeea8f22015-07-09 19:30:11122 public:
123 MdnsForceDiscoveryFunction();
124
125 protected:
126 ~MdnsForceDiscoveryFunction() override;
127
128 private:
Clark DuVallfd4db3d2019-07-30 19:10:43129 // ExtensionFunction override.
kmarshallcbfeea8f22015-07-09 19:30:11130 ResponseAction Run() override;
131
Nico Weber6e0f7aa2019-02-11 03:07:43132 DECLARE_EXTENSION_FUNCTION("mdns.forceDiscovery", MDNS_FORCEDISCOVERY)
kmarshallcbfeea8f22015-07-09 19:30:11133 DISALLOW_COPY_AND_ASSIGN(MdnsForceDiscoveryFunction);
134};
135
[email protected]267864e2013-09-06 18:25:57136} // namespace extensions
137
138#endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_