Home | History | Annotate | Download | only in mdns
      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"
     15 #include "extensions/browser/event_router.h"
     16 
     17 namespace extensions {
     18 
     19 class DnsSdRegistry;
     20 
     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 {
     28  public:
     29   explicit MDnsAPI(Profile* profile);
     30   virtual ~MDnsAPI();
     31 
     32   static MDnsAPI* Get(Profile* profile);
     33 
     34   // ProfileKeyedAPI implementation.
     35   static ProfileKeyedAPIFactory<MDnsAPI>* GetFactoryInstance();
     36 
     37   // Used to mock out the DnsSdRegistry for testing.
     38   void SetDnsSdRegistryForTesting(scoped_ptr<DnsSdRegistry> registry);
     39 
     40  protected:
     41   // Retrieve an instance of the registry. Lazily created when needed.
     42   virtual DnsSdRegistry* dns_sd_registry();
     43 
     44  private:
     45   friend class ProfileKeyedAPIFactory<MDnsAPI>;
     46 
     47   // EventRouter::Observer:
     48   virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
     49   virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
     50 
     51   // DnsSdRegistry::Observer
     52   virtual void OnDnsSdEvent(
     53       const std::string& service_type,
     54       const DnsSdRegistry::DnsSdServiceList& services) OVERRIDE;
     55 
     56   // ProfileKeyedAPI implementation.
     57   static const char* service_name() {
     58     return "MDnsAPI";
     59   }
     60 
     61   static const bool kServiceIsCreatedWithBrowserContext = true;
     62   static const bool kServiceIsNULLWhileTesting = true;
     63 
     64   // Update the current list of service types and update the registry.
     65   void UpdateMDnsListeners(const EventListenerInfo& details);
     66 
     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_;
     74 
     75   DISALLOW_COPY_AND_ASSIGN(MDnsAPI);
     76 };
     77 
     78 }  // namespace extensions
     79 
     80 #endif  // CHROME_BROWSER_EXTENSIONS_API_MDNS_MDNS_API_H_
     81