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 }; 56 57 // Called when a service has been added or removed for a certain service name. 58 typedef base::Callback<void(UpdateType, const std::string&)> UpdatedCallback; 59 60 // Listening will automatically stop when the destructor is called. 61 virtual ~ServiceWatcher() {} 62 63 // Start the service type watcher. 64 virtual void Start() = 0; 65 66 // Probe for services of this type. 67 virtual void DiscoverNewServices(bool force_update) = 0; 68 69 virtual std::string GetServiceType() const = 0; 70 }; 71 72 // Represents a service on the network and allows users to access the service's 73 // address and metadata. See |ServiceDiscoveryClient::CreateServiceResolver|. 74 class ServiceResolver { 75 public: 76 enum RequestStatus { 77 STATUS_SUCCESS, 78 STATUS_REQUEST_TIMEOUT, 79 STATUS_KNOWN_NONEXISTENT 80 }; 81 82 // A callback called once the service has been resolved. 83 typedef base::Callback<void(RequestStatus, const ServiceDescription&)> 84 ResolveCompleteCallback; 85 86 // Listening will automatically stop when the destructor is called. 87 virtual ~ServiceResolver() {} 88 89 // Start the service reader. 90 virtual void StartResolving() = 0; 91 92 virtual std::string GetName() const = 0; 93 }; 94 95 class LocalDomainResolver { 96 public: 97 typedef base::Callback<void(bool, const net::IPAddressNumber&)> 98 IPAddressCallback; 99 100 virtual ~LocalDomainResolver() {} 101 102 virtual void Start() = 0; 103 }; 104 105 class ServiceDiscoveryClient { 106 public: 107 virtual ~ServiceDiscoveryClient() {} 108 109 // Create a service watcher object listening for DNS-SD service announcements 110 // on service type |service_type|. 111 virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher( 112 const std::string& service_type, 113 const ServiceWatcher::UpdatedCallback& callback) = 0; 114 115 // Create a service resolver object for getting detailed service information 116 // for the service called |service_name|. 117 virtual scoped_ptr<ServiceResolver> CreateServiceResolver( 118 const std::string& service_name, 119 const ServiceResolver::ResolveCompleteCallback& callback) = 0; 120 121 // Create a resolver for local domain, both ipv4 or ipv6. 122 virtual scoped_ptr<LocalDomainResolver> CreateLocalDomainResolver( 123 const std::string& domain, 124 net::AddressFamily address_family, 125 const LocalDomainResolver::IPAddressCallback& callback) = 0; 126 }; 127 128 } // namespace local_discovery 129 130 #endif // CHROME_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_ 131