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 "chrome/browser/extensions/api/profile_keyed_api_factory.h"
15 #include "chrome/browser/extensions/event_router.h"
17 namespace extensions {
21 // MDnsAPI is instantiated with the profile and will listen for extensions that
22 // register listeners for the chrome.mdns extension API. It will use a registry
23 // class to start the mDNS listener process (if necessary) and observe new
24 // service events to dispatch them to registered extensions.
25 class MDnsAPI : public ProfileKeyedAPI,
26 public EventRouter::Observer,
27 public DnsSdRegistry::DnsSdObserver {
29 explicit MDnsAPI(Profile* profile);
32 static MDnsAPI* Get(Profile* profile);
34 // ProfileKeyedAPI implementation.
35 static ProfileKeyedAPIFactory<MDnsAPI>* GetFactoryInstance();
37 // Used to mock out the DnsSdRegistry for testing.
38 void SetDnsSdRegistryForTesting(scoped_ptr<DnsSdRegistry> registry);
41 // Retrieve an instance of the registry. Lazily created when needed.
42 virtual DnsSdRegistry* dns_sd_registry();
45 friend class ProfileKeyedAPIFactory<MDnsAPI>;
47 // EventRouter::Observer:
48 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
49 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
51 // DnsSdRegistry::Observer
52 virtual void OnDnsSdEvent(
53 const std::string& service_type,
54 const DnsSdRegistry::DnsSdServiceList& services) OVERRIDE;
56 // ProfileKeyedAPI implementation.
57 static const char* service_name() {
61 static const bool kServiceIsCreatedWithBrowserContext = true;
62 static const bool kServiceIsNULLWhileTesting = true;
64 // Update the current list of service types and update the registry.
65 void UpdateMDnsListeners(const EventListenerInfo& details);
67 // Ensure methods are only called on UI thread.
68 base::ThreadChecker thread_checker_;
69 Profile* const profile_;
70 // Lazily created on first access and destroyed with this API class.
71 scoped_ptr<DnsSdRegistry> dns_sd_registry_;
72 // Current set of service types registered with the registry.
73 std::set<std::string> service_types_;
75 DISALLOW_COPY_AND_ASSIGN(MDnsAPI);
78 } // namespace extensions
80 #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_