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