Chromium Code Reviews| 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 class Delegate { | |
| 47 public: | |
| 48 virtual ~Delegate() {} | |
| 49 | |
| 50 // A service has been added or removed for a certain service name. | |
| 51 virtual void OnServiceStatusChanged(bool available, | |
|
gene
2013/06/19 22:48:03
May be join those two functions to something like
Noam Samuel
2013/06/19 23:53:29
Done.
| |
| 52 const std::string& service_name) = 0; | |
| 53 | |
| 54 virtual void OnServiceChanged(const std::string& service_name) = 0; | |
| 55 }; | |
| 56 | |
| 57 // Listening will automatically stop when the destructor is called. | |
| 58 virtual ~ServiceWatcher() {} | |
| 59 | |
| 60 // Start the service type watcher. | |
| 61 virtual bool Start() = 0; | |
| 62 | |
| 63 // Get all known services names of this watcher's type. Return them in | |
| 64 // |services|. | |
| 65 virtual void GetAvailableServices( | |
| 66 std::vector<std::string>* services) const = 0; | |
| 67 | |
| 68 // Read services from the cache, alerting the delegate to any service that | |
| 69 // is not yet known. | |
| 70 virtual void ReadCachedServices() = 0; | |
| 71 | |
| 72 // Probe for services of this type. | |
| 73 virtual void DiscoverNewServices(bool force_update) = 0; | |
| 74 | |
| 75 virtual std::string GetServiceType() const = 0; | |
| 76 }; | |
| 77 | |
| 78 // Represents a service on the network and allows users to access the service's | |
| 79 // address and metadata. See |ServiceDiscoveryClient::CreateServiceResolver|. | |
| 80 class ServiceResolver { | |
| 81 public: | |
| 82 enum RequestStatus { | |
| 83 STATUS_SUCCESS = 0, | |
| 84 STATUS_TIMEOUT = 1, | |
|
Vitaly Buka (NO REVIEWS)
2013/06/19 21:02:02
STATUS_TIMEOUT conflicts with define in WinNT.h
Noam Samuel
2013/06/19 23:53:29
Done.
| |
| 85 STATUS_KNOWN_NONEXISTENT = 2 | |
| 86 }; | |
| 87 | |
| 88 // A callback called once the service has been resolved. | |
| 89 typedef base::Callback<void(RequestStatus, const ServiceDescription&)> | |
| 90 ResolveCompleteCallback; | |
|
gene
2013/06/19 22:48:03
indentation
Noam Samuel
2013/06/19 23:53:29
Done.
| |
| 91 | |
| 92 // Listening will automatically stop when the destructor is called. | |
| 93 virtual ~ServiceResolver() {} | |
| 94 | |
| 95 // Start the service reader. | |
| 96 virtual bool StartResolving() = 0; | |
| 97 | |
| 98 // Check whether the resolver is currently resolving. Can be called multiple | |
| 99 // times. | |
| 100 virtual bool IsResolving() const = 0; | |
| 101 | |
| 102 // Check wheteher the resolver has resolved the service already. | |
| 103 virtual bool HasResolved() const = 0; | |
| 104 | |
| 105 virtual std::string GetName() const = 0; | |
| 106 | |
| 107 virtual const ServiceDescription& GetServiceDescription() const = 0; | |
| 108 }; | |
| 109 | |
| 110 class ServiceDiscoveryClient { | |
| 111 public: | |
| 112 virtual ~ServiceDiscoveryClient() {} | |
| 113 | |
| 114 // Create a service watcher object listening for DNS-SD service announcements | |
| 115 // on service type |service_type|. | |
| 116 virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher( | |
| 117 const std::string& service_type, | |
| 118 ServiceWatcher::Delegate* delegate) = 0; | |
| 119 | |
| 120 // Create a service resolver object for getting detailed service information | |
| 121 // for the service called |service_name|. | |
| 122 virtual scoped_ptr<ServiceResolver> CreateServiceResolver( | |
| 123 const std::string& service_name, | |
| 124 const ServiceResolver::ResolveCompleteCallback& callback) = 0; | |
| 125 | |
| 126 // Lazily create and return static instance for ServiceDiscoveryClient. | |
| 127 static ServiceDiscoveryClient* GetInstance(); | |
| 128 | |
| 129 // Set the global instance (for testing). MUST be called before the first call | |
| 130 // to GetInstance. | |
| 131 static void SetInstance(ServiceDiscoveryClient* instance); | |
| 132 }; | |
| 133 | |
| 134 } // namespace local_discovery | |
| 135 | |
| 136 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_ | |
| OLD | NEW |