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_MDNS_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/thread_checker.h"
15 #include "chrome/browser/extensions/api/mdns/dns_sd_registry.h"
16 #include "chrome/common/extensions/api/mdns.h"
17 #include "extensions/browser/api/async_api_function.h"
18 #include "extensions/browser/browser_context_keyed_api_factory.h"
19 #include "extensions/browser/event_router.h"
20 #include "extensions/browser/extension_function.h"
26 namespace extensions
{
30 // MDnsAPI is instantiated with the profile and will listen for extensions that
31 // register listeners for the chrome.mdns extension API. It will use a registry
32 // class to start the mDNS listener process (if necessary) and observe new
33 // service events to dispatch them to registered extensions.
34 class MDnsAPI
: public BrowserContextKeyedAPI
,
35 public EventRouter::Observer
,
36 public DnsSdRegistry::DnsSdObserver
{
38 explicit MDnsAPI(content::BrowserContext
* context
);
41 static MDnsAPI
* Get(content::BrowserContext
* context
);
43 // BrowserContextKeyedAPI implementation.
44 static BrowserContextKeyedAPIFactory
<MDnsAPI
>* GetFactoryInstance();
46 // Used to mock out the DnsSdRegistry for testing.
47 void SetDnsSdRegistryForTesting(scoped_ptr
<DnsSdRegistry
> registry
);
49 // Immediately issues a multicast DNS query for all service types.
50 // NOTE: Discovery queries are sent to all event handlers associated with
51 // |this| service's BrowserContext.
52 void ForceDiscovery();
55 // Retrieve an instance of the registry. Lazily created when needed.
56 virtual DnsSdRegistry
* dns_sd_registry();
58 // Gets the list of mDNS event listeners.
59 virtual const extensions::EventListenerMap::ListenerList
& GetEventListeners();
62 FRIEND_TEST_ALL_PREFIXES(MDnsAPIDiscoveryTest
,
63 ServiceListenersAddedAndRemoved
);
65 typedef std::map
<std::string
, int> ServiceTypeCounts
;
66 friend class BrowserContextKeyedAPIFactory
<MDnsAPI
>;
68 // EventRouter::Observer:
69 void OnListenerAdded(const EventListenerInfo
& details
) override
;
70 void OnListenerRemoved(const EventListenerInfo
& details
) override
;
72 // DnsSdRegistry::Observer
73 void OnDnsSdEvent(const std::string
& service_type
,
74 const DnsSdRegistry::DnsSdServiceList
& services
) override
;
76 // BrowserContextKeyedAPI implementation.
77 static const char* service_name() {
81 static const bool kServiceIsCreatedWithBrowserContext
= true;
82 static const bool kServiceIsNULLWhileTesting
= true;
84 // Update the current list of service types and update the registry.
85 void UpdateMDnsListeners();
87 // Write a message to the consoles of extensions listening to a given service
89 void WriteToConsole(const std::string
& service_type
,
90 content::ConsoleMessageLevel level
,
91 const std::string
& message
);
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;
98 // Finds all all the valid listeners of the mdns.onServiceList event and
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|,
102 // The counts for each service type are output to |service_type_counts|, if
104 void GetValidOnServiceListListeners(const std::string
& service_type_filter
,
105 std::set
<std::string
>* extension_ids
,
106 ServiceTypeCounts
* service_type_counts
);
108 // Ensure methods are only called on UI thread.
109 base::ThreadChecker thread_checker_
;
110 content::BrowserContext
* const browser_context_
;
111 // Lazily created on first access and destroyed with this API class.
112 scoped_ptr
<DnsSdRegistry
> dns_sd_registry_
;
113 // Count of active listeners per service type, saved from the previous
114 // invocation of UpdateMDnsListeners().
115 ServiceTypeCounts prev_service_counts_
;
117 DISALLOW_COPY_AND_ASSIGN(MDnsAPI
);
120 class MdnsForceDiscoveryFunction
: public UIThreadExtensionFunction
{
122 MdnsForceDiscoveryFunction();
125 ~MdnsForceDiscoveryFunction() override
;
128 // UIThreadExtensionFunction override.
129 ResponseAction
Run() override
;
131 DECLARE_EXTENSION_FUNCTION("mdns.forceDiscovery", MDNS_FORCEDISCOVERY
);
132 DISALLOW_COPY_AND_ASSIGN(MdnsForceDiscoveryFunction
);
135 } // namespace extensions
137 #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_