Home | History | Annotate | Download | only in local_discovery
      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_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_
      6 #define CHROME_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/callback.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/time/time.h"
     14 #include "net/base/address_family.h"
     15 #include "net/base/host_port_pair.h"
     16 #include "net/base/net_util.h"
     17 
     18 namespace net {
     19 class MDnsClient;
     20 }
     21 
     22 namespace local_discovery {
     23 
     24 struct ServiceDescription {
     25  public:
     26   ServiceDescription();
     27   ~ServiceDescription();
     28 
     29   // Convenience function to get useful parts of the service name. A service
     30   // name follows the format <instance_name>.<service_type>.
     31   std::string instance_name() const;
     32   std::string service_type() const;
     33 
     34   // The name of the service.
     35   std::string service_name;
     36   // The address (in host/port format) for the service (from SRV record).
     37   net::HostPortPair address;
     38   // The metadata (from TXT record) of the service.
     39   std::vector<std::string> metadata;
     40   // IP address of the service, if available from cache. May be empty.
     41   net::IPAddressNumber ip_address;
     42   // Last time the service was seen.
     43   base::Time last_seen;
     44 };
     45 
     46 // Lets users browse the network for services of interest or listen for changes
     47 // in the services they are interested in. See
     48 // |ServiceDiscoveryClient::CreateServiceWatcher|.
     49 class ServiceWatcher {
     50  public:
     51   enum UpdateType {
     52     UPDATE_ADDED,
     53     UPDATE_CHANGED,
     54     UPDATE_REMOVED,
     55     UPDATE_INVALIDATED
     56   };
     57 
     58   // Called when a service has been added or removed for a certain service name.
     59   typedef base::Callback<void(UpdateType, const std::string&)> UpdatedCallback;
     60 
     61   // Listening will automatically stop when the destructor is called.
     62   virtual ~ServiceWatcher() {}
     63 
     64   // Start the service type watcher.
     65   virtual void Start() = 0;
     66 
     67   // Probe for services of this type.
     68   virtual void DiscoverNewServices(bool force_update) = 0;
     69 
     70   virtual std::string GetServiceType() const = 0;
     71 };
     72 
     73 // Represents a service on the network and allows users to access the service's
     74 // address and metadata. See |ServiceDiscoveryClient::CreateServiceResolver|.
     75 class ServiceResolver {
     76  public:
     77   enum RequestStatus {
     78     STATUS_SUCCESS,
     79     STATUS_REQUEST_TIMEOUT,
     80     STATUS_KNOWN_NONEXISTENT
     81   };
     82 
     83   // A callback called once the service has been resolved.
     84   typedef base::Callback<void(RequestStatus, const ServiceDescription&)>
     85       ResolveCompleteCallback;
     86 
     87   // Listening will automatically stop when the destructor is called.
     88   virtual ~ServiceResolver() {}
     89 
     90   // Start the service reader.
     91   virtual void StartResolving() = 0;
     92 
     93   virtual std::string GetName() const = 0;
     94 };
     95 
     96 class LocalDomainResolver {
     97  public:
     98   typedef base::Callback<void(bool /*success*/,
     99                               const net::IPAddressNumber& /*address_ipv4*/,
    100                               const net::IPAddressNumber& /*address_ipv6*/)>
    101       IPAddressCallback;
    102 
    103   virtual ~LocalDomainResolver() {}
    104 
    105   virtual void Start() = 0;
    106 };
    107 
    108 class ServiceDiscoveryClient {
    109  public:
    110   virtual ~ServiceDiscoveryClient() {}
    111 
    112   // Create a service watcher object listening for DNS-SD service announcements
    113   // on service type |service_type|.
    114   virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher(
    115       const std::string& service_type,
    116       const ServiceWatcher::UpdatedCallback& callback) = 0;
    117 
    118   // Create a service resolver object for getting detailed service information
    119   // for the service called |service_name|.
    120   virtual scoped_ptr<ServiceResolver> CreateServiceResolver(
    121       const std::string& service_name,
    122       const ServiceResolver::ResolveCompleteCallback& callback) = 0;
    123 
    124   // Create a resolver for local domain, both ipv4 or ipv6.
    125   virtual scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver(
    126       const std::string& domain,
    127       net::AddressFamily address_family,
    128       const LocalDomainResolver::IPAddressCallback& callback) = 0;
    129 };
    130 
    131 }  // namespace local_discovery
    132 
    133 #endif  // CHROME_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_
    134