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
{
56 UPDATE_TYPE_LAST
= UPDATE_INVALIDATED
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
;
62 // Listening will automatically stop when the destructor is called.
63 virtual ~ServiceWatcher() {}
65 // Start the service type watcher.
66 virtual void Start() = 0;
68 // Probe for services of this type.
69 virtual void DiscoverNewServices(bool force_update
) = 0;
71 virtual void SetActivelyRefreshServices(bool actively_refresh_services
) = 0;
73 virtual std::string
GetServiceType() const = 0;
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
{
82 STATUS_REQUEST_TIMEOUT
,
83 STATUS_KNOWN_NONEXISTENT
,
84 REQUEST_STATUS_LAST
= STATUS_KNOWN_NONEXISTENT
87 // A callback called once the service has been resolved.
88 typedef base::Callback
<void(RequestStatus
, const ServiceDescription
&)>
89 ResolveCompleteCallback
;
91 // Listening will automatically stop when the destructor is called.
92 virtual ~ServiceResolver() {}
94 // Start the service reader.
95 virtual void StartResolving() = 0;
97 virtual std::string
GetName() const = 0;
100 class LocalDomainResolver
{
102 typedef base::Callback
<void(bool /*success*/,
103 const net::IPAddressNumber
& /*address_ipv4*/,
104 const net::IPAddressNumber
& /*address_ipv6*/)>
107 virtual ~LocalDomainResolver() {}
109 virtual void Start() = 0;
112 class ServiceDiscoveryClient
{
114 virtual ~ServiceDiscoveryClient() {}
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;
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;
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;
135 } // namespace local_discovery
137 #endif // CHROME_COMMON_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_