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 | |
| 16 namespace local_discovery { | |
| 17 | |
| 18 // Lets users browse the network for services of interest or listen for changes | |
| 19 // in the services they are interested in. See | |
| 20 // |ServiceDiscoveryClient::CreateServiceTypeWatcher|. | |
| 21 class ServiceTypeWatcher { | |
| 22 public: | |
| 23 class Delegate { | |
| 24 public: | |
| 25 virtual ~Delegate() {} | |
| 26 | |
| 27 // A service has been added or removed for a certain service name. | |
| 28 virtual void OnServiceStatusChanged(bool available, | |
| 29 const std::string& service_name) = 0; | |
| 30 }; | |
| 31 | |
| 32 // Listening will automatically stop when the destructor is called. | |
| 33 virtual ~ServiceTypeWatcher() {} | |
| 34 | |
| 35 // Start the service type watcher. | |
| 36 virtual bool Start() = 0; | |
| 37 | |
| 38 // Get all known services names of this watcher's type. Return them in | |
| 39 // |services|. | |
| 40 virtual void GetAvailableServices( | |
| 41 std::vector<std::string>* services) const = 0; | |
|
Vitaly Buka (NO REVIEWS)
2013/06/15 00:22:30
one line?
Noam Samuel
2013/06/18 22:46:03
Doesn't fit.
| |
| 42 | |
| 43 // Probe for services of this type. | |
| 44 virtual void DiscoverNewServices() = 0; | |
| 45 | |
| 46 // Probe for services of this type and also force early invalidation | |
| 47 // of existing services, having the effect of throwing away invalidated | |
| 48 // services. This method should NOT be called periodically, but only | |
| 49 // in response to user action. | |
| 50 virtual void ForceUpdateServices() = 0; | |
| 51 | |
| 52 virtual std::string GetServiceType() const = 0; | |
| 53 | |
| 54 virtual bool IsActive() const = 0; | |
| 55 }; | |
| 56 | |
| 57 // Represents a service on the network and allows users to access the service's | |
| 58 // address and metadata. See |ServiceDiscoveryClient::CreateServiceReader|. | |
| 59 class ServiceReader { | |
| 60 public: | |
| 61 enum RequestStatus { | |
| 62 STATUS_SUCCESS = 0, | |
| 63 STATUS_TIMEOUT = 1, | |
| 64 STATUS_KNOWN_NONEXISTENT = 2 | |
| 65 }; | |
| 66 | |
| 67 class Delegate { | |
| 68 public: | |
| 69 virtual ~Delegate() {} | |
| 70 | |
| 71 virtual void OnAddressChanged(const std::string& service_name, | |
| 72 const net::HostPortPair& address) = 0; | |
| 73 | |
| 74 virtual void OnMetadataChanged( | |
| 75 const std::string& service_name, | |
| 76 const std::vector<std::string>& metadata) = 0; | |
| 77 }; | |
| 78 | |
| 79 // A callback for recieving metadata. The first argument is the request status | |
| 80 // and the second argument is the metadata. | |
| 81 typedef base::Callback<void(ServiceReader::RequestStatus, | |
| 82 const std::vector<std::string>&)> | |
| 83 MetadataCallback; | |
|
Vitaly Buka (NO REVIEWS)
2013/06/15 00:22:30
Maybe move all callback into delegate?
Noam Samuel
2013/06/15 00:42:06
Hm. It would definitely provide some simplicity. T
Noam Samuel
2013/06/18 22:46:03
Made irrelevant by refactor.
On 2013/06/15 00:42:
| |
| 84 | |
| 85 // A callback for recieving the address. The first argument is the request | |
| 86 // status and the second argument is the address. | |
| 87 typedef base::Callback<void(ServiceReader::RequestStatus, | |
| 88 const net::HostPortPair&)> | |
| 89 AddressCallback; | |
| 90 | |
| 91 // A callback for recieving the last time the PTR record has been seen. | |
| 92 typedef base::Callback<void(base::Time)> LastSeenCallback; | |
| 93 | |
| 94 // Listening will automatically stop when the destructor is called. | |
| 95 virtual ~ServiceReader() {} | |
| 96 | |
| 97 // Start the service reader. | |
| 98 virtual bool Start() = 0; | |
| 99 | |
| 100 // Get the cached TXT record for a service. Returns true and fills |metadata| | |
| 101 // if the record exists. Use NULL for |metadata| to simply check if the value | |
| 102 // is present. | |
| 103 virtual bool GetMetadata(std::vector<std::string>* metadata) const = 0; | |
| 104 | |
| 105 // Read the TXT record for a service. If |force_refresh| is | |
| 106 // true, force a refresh of the record even if it is cached, otherwise | |
| 107 // defer to the cache when it is available. | |
| 108 virtual void ReadMetadata(const MetadataCallback& callback, | |
| 109 bool force_refresh) = 0; | |
| 110 | |
| 111 // Get the cached SRV record for a service. Returns true and fills |address| | |
| 112 // if the record exists. Use NULL for |address| to simply check if the value | |
| 113 // is present. | |
| 114 virtual bool GetAddress(net::HostPortPair* address) const = 0; | |
| 115 | |
| 116 // Read the SRV record for a service. If |force_refresh| is | |
| 117 // true, force a refresh of the record even if it is cached, otherwise | |
| 118 // defer to the cache when it is available. | |
| 119 virtual void ReadAddress(const AddressCallback& callback, | |
| 120 bool force_refresh) = 0; | |
|
Vitaly Buka (NO REVIEWS)
2013/06/15 00:22:30
missaligned
Noam Samuel
2013/06/18 22:46:03
Code removed.
| |
| 121 | |
| 122 virtual std::string GetHumanReadableName() const = 0; | |
| 123 virtual std::string GetType() const = 0; | |
| 124 virtual std::string GetName() const = 0; | |
| 125 | |
| 126 // Check whether the service is available, measured by the existence of an SRV | |
| 127 // record. This does not determine whether a service is still being | |
| 128 // advertised. | |
| 129 virtual bool IsAvailable() const = 0; | |
| 130 | |
| 131 // Call |callback| with the last time the SRV record has been seen. Uses the | |
| 132 // default value of base::Time to signify that it is unavailable. | |
| 133 virtual void ReadLastSeen(const LastSeenCallback& callback) = 0; | |
| 134 }; | |
| 135 | |
| 136 class ServiceDiscoveryClient { | |
| 137 public: | |
| 138 virtual ~ServiceDiscoveryClient() {} | |
| 139 | |
| 140 // Create a service object listening for DNS-SD service announcements on | |
| 141 // service type |service_type|. If a service type watcher is |active|, | |
| 142 // it will query the network for new services. If |alert_existing_services| is | |
| 143 // true, the delegate will be alerted for services already in the cache. | |
| 144 virtual scoped_ptr<ServiceTypeWatcher> CreateServiceTypeWatcher( | |
| 145 const std::string& service_type, | |
| 146 bool active, | |
| 147 bool alert_existing_services, | |
| 148 ServiceTypeWatcher::Delegate* delegate) = 0; | |
| 149 | |
| 150 // Create a service object listening for DNS-SD service announcements on | |
| 151 // service |service_name|. |delegate| may be null in which case the | |
| 152 // object will not recieve continuous announcements but may still query | |
| 153 // about the relevant service. | |
| 154 virtual scoped_ptr<ServiceReader> CreateServiceReader( | |
| 155 const std::string& service_name, | |
| 156 ServiceReader::Delegate* delegate) = 0; | |
| 157 | |
| 158 // Lazily create and return static instance for ServiceDiscoveryClient. | |
| 159 static ServiceDiscoveryClient* GetInstance(); | |
| 160 | |
| 161 // Set the global instance (for testing). MUST be called before the first call | |
| 162 // to GetInstance. | |
| 163 static void SetInstance(ServiceDiscoveryClient* instance); | |
| 164 }; | |
| 165 | |
| 166 } // namespace local_discovery | |
| 167 | |
| 168 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_H_ | |
| OLD | NEW |