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_DNS_SD_REGISTRY_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_MDNS_DNS_SD_REGISTRY_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <utility>
     11 #include <vector>
     12 
     13 #include "base/memory/linked_ptr.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/observer_list.h"
     16 #include "chrome/browser/extensions/api/mdns/dns_sd_delegate.h"
     17 
     18 namespace local_discovery {
     19 class ServiceDiscoverySharedClient;
     20 class ServiceDiscoveryClient;
     21 }
     22 
     23 namespace extensions {
     24 
     25 class DnsSdDeviceLister;
     26 class ServiceTypeData;
     27 
     28 // Registry class for keeping track of discovered network services over DNS-SD.
     29 class DnsSdRegistry : public DnsSdDelegate {
     30  public:
     31   typedef std::vector<DnsSdService> DnsSdServiceList;
     32 
     33   class DnsSdObserver {
     34    public:
     35     virtual void OnDnsSdEvent(const std::string& service_type,
     36                               const DnsSdServiceList& services) = 0;
     37 
     38    protected:
     39     virtual ~DnsSdObserver() {}
     40   };
     41 
     42   DnsSdRegistry();
     43   explicit DnsSdRegistry(local_discovery::ServiceDiscoverySharedClient* client);
     44   virtual ~DnsSdRegistry();
     45 
     46   // Observer registration for parties interested in discovery events.
     47   virtual void AddObserver(DnsSdObserver* observer);
     48   virtual void RemoveObserver(DnsSdObserver* observer);
     49 
     50   // DNS-SD-related discovery functionality.
     51   virtual void RegisterDnsSdListener(std::string service_type);
     52   virtual void UnregisterDnsSdListener(std::string service_type);
     53 
     54  protected:
     55   // Data class for managing all the resources and information related to a
     56   // particular service type.
     57   class ServiceTypeData {
     58    public:
     59     explicit ServiceTypeData(scoped_ptr<DnsSdDeviceLister> lister);
     60     virtual ~ServiceTypeData();
     61 
     62     // Notify the data class of listeners so that it can be reference counted.
     63     void ListenerAdded();
     64     // Returns true if the last listener was removed.
     65     bool ListenerRemoved();
     66     int GetListenerCount();
     67 
     68     // Methods for adding, updating or removing services for this service type.
     69     bool UpdateService(bool added, const DnsSdService& service);
     70     bool RemoveService(const std::string& service_name);
     71     bool ClearServices();
     72 
     73     const DnsSdRegistry::DnsSdServiceList& GetServiceList();
     74 
     75    private:
     76     int ref_count;
     77     scoped_ptr<DnsSdDeviceLister> lister_;
     78     DnsSdRegistry::DnsSdServiceList service_list_;
     79     DISALLOW_COPY_AND_ASSIGN(ServiceTypeData);
     80   };
     81 
     82   // Maps service types to associated data such as listers and service lists.
     83   typedef std::map<std::string, linked_ptr<ServiceTypeData> >
     84       DnsSdServiceTypeDataMap;
     85 
     86   virtual DnsSdDeviceLister* CreateDnsSdDeviceLister(
     87       DnsSdDelegate* delegate,
     88       const std::string& service_type,
     89       local_discovery::ServiceDiscoverySharedClient* discovery_client);
     90 
     91   // DnsSdDelegate implementation:
     92   virtual void ServiceChanged(const std::string& service_type,
     93                               bool added,
     94                               const DnsSdService& service) OVERRIDE;
     95   virtual void ServiceRemoved(const std::string& service_type,
     96                               const std::string& service_name) OVERRIDE;
     97   virtual void ServicesFlushed(const std::string& service_type) OVERRIDE;
     98 
     99   DnsSdServiceTypeDataMap service_data_map_;
    100 
    101  private:
    102   void DispatchApiEvent(const std::string& service_type);
    103   bool IsRegistered(const std::string& service_type);
    104 
    105   scoped_refptr<local_discovery::ServiceDiscoverySharedClient>
    106       service_discovery_client_;
    107   ObserverList<DnsSdObserver> observers_;
    108 
    109   DISALLOW_COPY_AND_ASSIGN(DnsSdRegistry);
    110 };
    111 
    112 }  // namespace extensions
    113 
    114 #endif  // CHROME_BROWSER_EXTENSIONS_API_MDNS_DNS_SD_REGISTRY_H_
    115