OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 <map> |
| 9 #include <set> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "chrome/browser/extensions/api/mdns/nsd_registry.h" |
| 16 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" |
| 17 #include "chrome/browser/extensions/event_router.h" |
| 18 |
| 19 namespace extensions { |
| 20 |
| 21 class NsdRegistry; |
| 22 |
| 23 // MDnsAPI is instantiated with the profile and will listen for extensions that |
| 24 // register listeners for the chrome.mdns extension API. It will use an Network |
| 25 // Service Discovery type registry class to start the mDNS listener process (if |
| 26 // necessary) and observe new service events to dispatch them to registered |
| 27 // extensions. The Network Service Discovery registry is a location for |
| 28 // managing discovery protocols. |
| 29 class MDnsAPI : public ProfileKeyedAPI, |
| 30 public EventRouter::Observer, |
| 31 public NsdRegistry::NsdObserver { |
| 32 public: |
| 33 explicit MDnsAPI(Profile* profile); |
| 34 virtual ~MDnsAPI(); |
| 35 |
| 36 static MDnsAPI* Get(Profile* profile); |
| 37 |
| 38 // ProfileKeyedAPI implementation. |
| 39 static ProfileKeyedAPIFactory<MDnsAPI>* GetFactoryInstance(); |
| 40 |
| 41 // Used to mock out the NsdRegistry for testing. |
| 42 void SetNsdRegistryForTesting(scoped_ptr<NsdRegistry> registry); |
| 43 |
| 44 protected: |
| 45 // Retrieve an instance to the registry. Lazily create it if needed. |
| 46 virtual NsdRegistry* nsd_registry(); |
| 47 |
| 48 private: |
| 49 friend class ProfileKeyedAPIFactory<MDnsAPI>; |
| 50 |
| 51 // Mapping from MDNS service types to currently listening extensions. |
| 52 typedef std::map<std::string, std::set<std::string> > |
| 53 ServiceTypeToExtensionsMap; |
| 54 |
| 55 // EventRouter::Observer: |
| 56 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE; |
| 57 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE; |
| 58 |
| 59 // NsdRegistry::Observer |
| 60 virtual void OnMDnsEvent( |
| 61 const std::string& service_type, |
| 62 const NsdRegistry::NsdServiceList& services) OVERRIDE; |
| 63 |
| 64 // ProfileKeyedAPI implementation. |
| 65 static const char* service_name() { |
| 66 return "MDnsAPI"; |
| 67 } |
| 68 |
| 69 static const bool kServiceIsCreatedWithBrowserContext = true; |
| 70 static const bool kServiceIsNULLWhileTesting = true; |
| 71 |
| 72 // Ensure methods are only called on UI thread. |
| 73 base::ThreadChecker thread_checker_; |
| 74 Profile* const profile_; |
| 75 // Lazily created on first access and destroyed with this API class. |
| 76 scoped_ptr<NsdRegistry> nsd_registry_; |
| 77 ServiceTypeToExtensionsMap service_type_to_extensions_map_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(MDnsAPI); |
| 80 }; |
| 81 |
| 82 } // namespace extensions |
| 83 |
| 84 #endif // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_ |
OLD | NEW |