| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_ |
| 6 #define CHROME_BROWSER_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.h" |
| 14 #include "net/base/host_port_pair.h" |
| 15 #include "net/base/net_util.h" |
| 16 |
| 17 namespace local_discovery { |
| 18 |
| 19 struct ServiceDescription { |
| 20 public: |
| 21 ServiceDescription(); |
| 22 ~ServiceDescription(); |
| 23 |
| 24 // Convenience function to get useful parts of the service name. A service |
| 25 // name follows the format <instance_name>.<service_type>. |
| 26 std::string instance_name() const; |
| 27 std::string service_type() const; |
| 28 |
| 29 // The name of the service. |
| 30 std::string service_name; |
| 31 // The address (in host/port format) for the service (from SRV record). |
| 32 net::HostPortPair address; |
| 33 // The metadata (from TXT record) of the service. |
| 34 std::vector<std::string> metadata; |
| 35 // IP address of the service, if available from cache. May be empty. |
| 36 net::IPAddressNumber ip_address; |
| 37 // Last time the service was seen. |
| 38 base::Time last_seen; |
| 39 }; |
| 40 |
| 41 // Lets users browse the network for services of interest or listen for changes |
| 42 // in the services they are interested in. See |
| 43 // |ServiceDiscoveryClient::CreateServiceWatcher|. |
| 44 class ServiceWatcher { |
| 45 public: |
| 46 enum UpdateType { |
| 47 UPDATE_ADDED, |
| 48 UPDATE_CHANGED, |
| 49 UPDATE_REMOVED |
| 50 }; |
| 51 |
| 52 class Delegate { |
| 53 public: |
| 54 virtual ~Delegate() {} |
| 55 |
| 56 // A service has been added or removed for a certain service name. |
| 57 virtual void OnServiceUpdated(UpdateType update, |
| 58 const std::string& service_name) = 0; |
| 59 }; |
| 60 |
| 61 // Listening will automatically stop when the destructor is called. |
| 62 virtual ~ServiceWatcher() {} |
| 63 |
| 64 // Start the service type watcher. |
| 65 virtual bool Start() = 0; |
| 66 |
| 67 // Get all known services names of this watcher's type. Return them in |
| 68 // |services|. |
| 69 virtual void GetAvailableServices( |
| 70 std::vector<std::string>* services) const = 0; |
| 71 |
| 72 // Read services from the cache, alerting the delegate to any service that |
| 73 // is not yet known. |
| 74 virtual void ReadCachedServices() = 0; |
| 75 |
| 76 // Probe for services of this type. |
| 77 virtual void DiscoverNewServices(bool force_update) = 0; |
| 78 |
| 79 virtual std::string GetServiceType() const = 0; |
| 80 }; |
| 81 |
| 82 // Represents a service on the network and allows users to access the service's |
| 83 // address and metadata. See |ServiceDiscoveryClient::CreateServiceResolver|. |
| 84 class ServiceResolver { |
| 85 public: |
| 86 enum RequestStatus { |
| 87 STATUS_SUCCESS = 0, |
| 88 STATUS_REQUEST_TIMEOUT = 1, |
| 89 STATUS_KNOWN_NONEXISTENT = 2 |
| 90 }; |
| 91 |
| 92 // A callback called once the service has been resolved. |
| 93 typedef base::Callback<void(RequestStatus, const ServiceDescription&)> |
| 94 ResolveCompleteCallback; |
| 95 |
| 96 // Listening will automatically stop when the destructor is called. |
| 97 virtual ~ServiceResolver() {} |
| 98 |
| 99 // Start the service reader. |
| 100 virtual bool StartResolving() = 0; |
| 101 |
| 102 // Check whether the resolver is currently resolving. Can be called multiple |
| 103 // times. |
| 104 virtual bool IsResolving() const = 0; |
| 105 |
| 106 // Check wheteher the resolver has resolved the service already. |
| 107 virtual bool HasResolved() const = 0; |
| 108 |
| 109 virtual std::string GetName() const = 0; |
| 110 |
| 111 virtual const ServiceDescription& GetServiceDescription() const = 0; |
| 112 }; |
| 113 |
| 114 class ServiceDiscoveryClient { |
| 115 public: |
| 116 virtual ~ServiceDiscoveryClient() {} |
| 117 |
| 118 // Create a service watcher object listening for DNS-SD service announcements |
| 119 // on service type |service_type|. |
| 120 virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher( |
| 121 const std::string& service_type, |
| 122 ServiceWatcher::Delegate* delegate) = 0; |
| 123 |
| 124 // Create a service resolver object for getting detailed service information |
| 125 // for the service called |service_name|. |
| 126 virtual scoped_ptr<ServiceResolver> CreateServiceResolver( |
| 127 const std::string& service_name, |
| 128 const ServiceResolver::ResolveCompleteCallback& callback) = 0; |
| 129 |
| 130 // Lazily create and return static instance for ServiceDiscoveryClient. |
| 131 static ServiceDiscoveryClient* GetInstance(); |
| 132 |
| 133 // Set the global instance (for testing). MUST be called before the first call |
| 134 // to GetInstance. |
| 135 static void SetInstance(ServiceDiscoveryClient* instance); |
| 136 }; |
| 137 |
| 138 } // namespace local_discovery |
| 139 |
| 140 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_ |
| OLD | NEW |