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_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/threading/thread_checker.h"
13 #include "chrome/browser/extensions/api/mdns/dns_sd_registry.h"
14 #include "extensions/browser/browser_context_keyed_api_factory.h"
15 #include "extensions/browser/event_router.h"
21 namespace extensions
{
25 // MDnsAPI is instantiated with the profile and will listen for extensions that
26 // register listeners for the chrome.mdns extension API. It will use a registry
27 // class to start the mDNS listener process (if necessary) and observe new
28 // service events to dispatch them to registered extensions.
29 class MDnsAPI
: public BrowserContextKeyedAPI
,
30 public EventRouter::Observer
,
31 public DnsSdRegistry::DnsSdObserver
{
33 explicit MDnsAPI(content::BrowserContext
* context
);
36 static MDnsAPI
* Get(content::BrowserContext
* context
);
38 // BrowserContextKeyedAPI implementation.
39 static BrowserContextKeyedAPIFactory
<MDnsAPI
>* GetFactoryInstance();
41 // Used to mock out the DnsSdRegistry for testing.
42 void SetDnsSdRegistryForTesting(scoped_ptr
<DnsSdRegistry
> registry
);
45 // Retrieve an instance of the registry. Lazily created when needed.
46 virtual DnsSdRegistry
* dns_sd_registry();
49 friend class BrowserContextKeyedAPIFactory
<MDnsAPI
>;
51 // EventRouter::Observer:
52 void OnListenerAdded(const EventListenerInfo
& details
) override
;
53 void OnListenerRemoved(const EventListenerInfo
& details
) override
;
55 // DnsSdRegistry::Observer
56 void OnDnsSdEvent(const std::string
& service_type
,
57 const DnsSdRegistry::DnsSdServiceList
& services
) override
;
59 // BrowserContextKeyedAPI implementation.
60 static const char* service_name() {
64 static const bool kServiceIsCreatedWithBrowserContext
= true;
65 static const bool kServiceIsNULLWhileTesting
= true;
67 // Update the current list of service types and update the registry.
68 void UpdateMDnsListeners(const EventListenerInfo
& details
);
70 // Ensure methods are only called on UI thread.
71 base::ThreadChecker thread_checker_
;
72 content::BrowserContext
* const browser_context_
;
73 // Lazily created on first access and destroyed with this API class.
74 scoped_ptr
<DnsSdRegistry
> dns_sd_registry_
;
75 // Current set of service types registered with the registry.
76 std::set
<std::string
> service_types_
;
78 DISALLOW_COPY_AND_ASSIGN(MDnsAPI
);
81 } // namespace extensions
83 #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_