[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 | |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | |
| 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" |
[email protected] | 3442353 | 2013-11-21 18:13:10 | [diff] [blame] | 15 | #include "extensions/browser/event_router.h" |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 16 | |
[email protected] | 21eaf354 | 2014-02-25 17:47:52 | [diff] [blame^] | 17 | namespace content { |
| 18 | class BrowserContext; |
| 19 | } |
| 20 | |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 21 | namespace extensions { |
| 22 | |
| 23 | class DnsSdRegistry; |
| 24 | |
| 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 ProfileKeyedAPI, |
| 30 | public EventRouter::Observer, |
| 31 | public DnsSdRegistry::DnsSdObserver { |
| 32 | public: |
[email protected] | 21eaf354 | 2014-02-25 17:47:52 | [diff] [blame^] | 33 | explicit MDnsAPI(content::BrowserContext* context); |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 34 | virtual ~MDnsAPI(); |
| 35 | |
[email protected] | 21eaf354 | 2014-02-25 17:47:52 | [diff] [blame^] | 36 | static MDnsAPI* Get(content::BrowserContext* context); |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 37 | |
| 38 | // ProfileKeyedAPI implementation. |
| 39 | static ProfileKeyedAPIFactory<MDnsAPI>* GetFactoryInstance(); |
| 40 | |
| 41 | // Used to mock out the DnsSdRegistry for testing. |
| 42 | void SetDnsSdRegistryForTesting(scoped_ptr<DnsSdRegistry> registry); |
| 43 | |
| 44 | protected: |
| 45 | // Retrieve an instance of the registry. Lazily created when needed. |
| 46 | virtual DnsSdRegistry* dns_sd_registry(); |
| 47 | |
| 48 | private: |
| 49 | friend class ProfileKeyedAPIFactory<MDnsAPI>; |
| 50 | |
| 51 | // EventRouter::Observer: |
| 52 | virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE; |
| 53 | virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE; |
| 54 | |
| 55 | // DnsSdRegistry::Observer |
| 56 | virtual void OnDnsSdEvent( |
| 57 | const std::string& service_type, |
| 58 | const DnsSdRegistry::DnsSdServiceList& services) OVERRIDE; |
| 59 | |
| 60 | // ProfileKeyedAPI implementation. |
| 61 | static const char* service_name() { |
| 62 | return "MDnsAPI"; |
| 63 | } |
| 64 | |
| 65 | static const bool kServiceIsCreatedWithBrowserContext = true; |
| 66 | static const bool kServiceIsNULLWhileTesting = true; |
| 67 | |
| 68 | // Update the current list of service types and update the registry. |
| 69 | void UpdateMDnsListeners(const EventListenerInfo& details); |
| 70 | |
| 71 | // Ensure methods are only called on UI thread. |
| 72 | base::ThreadChecker thread_checker_; |
[email protected] | 21eaf354 | 2014-02-25 17:47:52 | [diff] [blame^] | 73 | content::BrowserContext* const browser_context_; |
[email protected] | 267864e | 2013-09-06 18:25:57 | [diff] [blame] | 74 | // Lazily created on first access and destroyed with this API class. |
| 75 | scoped_ptr<DnsSdRegistry> dns_sd_registry_; |
| 76 | // Current set of service types registered with the registry. |
| 77 | std::set<std::string> service_types_; |
| 78 | |
| 79 | DISALLOW_COPY_AND_ASSIGN(MDnsAPI); |
| 80 | }; |
| 81 | |
| 82 | } // namespace extensions |
| 83 | |
| 84 | #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ |