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.
5 #ifndef CHROME_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_
6 #define CHROME_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_
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"
22 namespace local_discovery
{
24 struct ServiceDescription
{
27 ~ServiceDescription();
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;
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.
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
{
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
;
61 // Listening will automatically stop when the destructor is called.
62 virtual ~ServiceWatcher() {}
64 // Start the service type watcher.
65 virtual void Start() = 0;
67 // Probe for services of this type.
68 virtual void DiscoverNewServices(bool force_update
) = 0;
70 virtual std::string
GetServiceType() const = 0;
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
{
79 STATUS_REQUEST_TIMEOUT
,
80 STATUS_KNOWN_NONEXISTENT
83 // A callback called once the service has been resolved.
84 typedef base::Callback
<void(RequestStatus
, const ServiceDescription
&)>
85 ResolveCompleteCallback
;
87 // Listening will automatically stop when the destructor is called.
88 virtual ~ServiceResolver() {}
90 // Start the service reader.
91 virtual void StartResolving() = 0;
93 virtual std::string
GetName() const = 0;
96 class LocalDomainResolver
{
98 typedef base::Callback
<void(bool /*success*/,
99 const net::IPAddressNumber
& /*address_ipv4*/,
100 const net::IPAddressNumber
& /*address_ipv6*/)>
103 virtual ~LocalDomainResolver() {}
105 virtual void Start() = 0;
108 class ServiceDiscoveryClient
{
110 virtual ~ServiceDiscoveryClient() {}
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;
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;
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;
131 } // namespace local_discovery
133 #endif // CHROME_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_