| 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_IMPL_H_ |
| 6 #define CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_ |
| 7 |
| 8 #include <set> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/callback.h" |
| 13 #include "base/cancelable_callback.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/message_loop.h" |
| 16 #include "chrome/browser/local_discovery/service_discovery_client.h" |
| 17 #include "net/dns/mdns_client.h" |
| 18 |
| 19 namespace local_discovery { |
| 20 |
| 21 class ServiceDiscoveryClientImpl : public ServiceDiscoveryClient { |
| 22 public: |
| 23 ServiceDiscoveryClientImpl(); |
| 24 virtual ~ServiceDiscoveryClientImpl(); |
| 25 |
| 26 // Create a service object listening for DNS-SD service announcements on |
| 27 // service type |service_type|. |
| 28 virtual scoped_ptr<ServiceTypeWatcher> CreateServiceTypeWatcher( |
| 29 const std::string& service_type, |
| 30 bool active, |
| 31 bool alert_existing_services, |
| 32 ServiceTypeWatcher::Delegate* delegate) OVERRIDE; |
| 33 |
| 34 // Create a service object listening for DNS-SD service announcements on |
| 35 // service |service_name|. |delegate| may be null in which case the |
| 36 // object will not recieve continuous announcements but may still query |
| 37 // about the relevant service. |
| 38 virtual scoped_ptr<ServiceReader> CreateServiceReader( |
| 39 const std::string& service_name, |
| 40 ServiceReader::Delegate* delegate) OVERRIDE; |
| 41 |
| 42 private: |
| 43 DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryClientImpl); |
| 44 }; |
| 45 |
| 46 class ServiceTypeWatcherImpl : public ServiceTypeWatcher, |
| 47 public net::MDnsListener::Delegate { |
| 48 public: |
| 49 ServiceTypeWatcherImpl(const std::string& service_type, |
| 50 bool active, |
| 51 bool alert_existing_services, |
| 52 ServiceTypeWatcher::Delegate* delegate); |
| 53 // Listening will automatically stop when the destructor is called. |
| 54 virtual ~ServiceTypeWatcherImpl(); |
| 55 |
| 56 virtual bool Start() OVERRIDE; |
| 57 |
| 58 // Get all known services names of this watcher's type. Return them in |
| 59 // |services|. |
| 60 virtual void GetAvailableServices( |
| 61 std::vector<std::string>* services) const OVERRIDE; |
| 62 |
| 63 // Probe for services of this type. |
| 64 virtual void DiscoverNewServices() OVERRIDE; |
| 65 |
| 66 // Probe for services of this type and also force early invalidation |
| 67 // of existing services, having the effect of throwing away invalidated |
| 68 // services. This method should NOT be called periodically, but only |
| 69 // in response to user action. |
| 70 virtual void ForceUpdateServices() OVERRIDE; |
| 71 |
| 72 virtual std::string GetServiceType() const OVERRIDE; |
| 73 |
| 74 virtual bool IsActive() const OVERRIDE; |
| 75 |
| 76 virtual void OnRecordUpdate(net::MDnsListener::UpdateType update, |
| 77 const net::RecordParsed* record) OVERRIDE; |
| 78 |
| 79 virtual void OnNsecRecord(const std::string& name, unsigned rrtype) OVERRIDE; |
| 80 |
| 81 virtual void OnCachePurged() OVERRIDE; |
| 82 |
| 83 virtual void OnTransactionResponse(net::MDnsTransaction::Result result, |
| 84 const net::RecordParsed* record); |
| 85 |
| 86 private: |
| 87 void AddService(const std::string& service); |
| 88 void RemoveService(const std::string& service); |
| 89 bool CreateTransaction(bool active, bool alert_existing_services, |
| 90 bool force_refresh); |
| 91 |
| 92 std::string service_type_; |
| 93 bool active_; |
| 94 bool alert_existing_services_; |
| 95 std::set<std::string> services_; |
| 96 scoped_ptr<net::MDnsTransaction> transaction_; |
| 97 scoped_ptr<net::MDnsListener> listener_; |
| 98 |
| 99 ServiceTypeWatcher::Delegate* delegate_; |
| 100 bool started_; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(ServiceTypeWatcherImpl); |
| 103 }; |
| 104 |
| 105 class ServiceReaderImpl |
| 106 : public ServiceReader, |
| 107 public net::MDnsListener::Delegate, |
| 108 public base::SupportsWeakPtr<ServiceReaderImpl> { |
| 109 public: |
| 110 ServiceReaderImpl(const std::string& service_name, |
| 111 ServiceReader::Delegate* delegate); |
| 112 |
| 113 // Listening will automatically stop when the destructor is called. |
| 114 virtual ~ServiceReaderImpl(); |
| 115 |
| 116 virtual bool Start() OVERRIDE; |
| 117 |
| 118 // Get the cached TXT record for a service. Returns true and fills |metadata| |
| 119 // if the record exists. Use NULL for |metadata| to simply check if the value |
| 120 // is present. |
| 121 virtual bool GetMetadata(std::vector<std::string>* metadata) const OVERRIDE; |
| 122 |
| 123 // Read the TXT record for a service. If |force_refresh| is |
| 124 // true, force a refresh of the record even if it is cached, otherwise |
| 125 // defer to the cache when it is available. |
| 126 virtual void ReadMetadata( |
| 127 const ServiceReader::MetadataCallback& callback, |
| 128 bool force_refresh) OVERRIDE; |
| 129 |
| 130 // Get the cached SRV record for a service. Returns true and fills |address| |
| 131 // if the record exists. Use NULL for |address| to simply check if the value |
| 132 // is present. |
| 133 virtual bool GetAddress(net::HostPortPair* address) const OVERRIDE; |
| 134 |
| 135 // Read the SRV record for a service. If |force_refresh| is |
| 136 // true, force a refresh of the record even if it is cached, otherwise |
| 137 // defer to the cache when it is available. |
| 138 virtual void ReadAddress(const ServiceReader::AddressCallback& callback, |
| 139 bool force_refresh) OVERRIDE; |
| 140 |
| 141 virtual std::string GetHumanReadableName() const OVERRIDE; |
| 142 virtual std::string GetType() const OVERRIDE; |
| 143 virtual std::string GetName() const OVERRIDE; |
| 144 |
| 145 // Check whether the service is available. |
| 146 virtual bool IsAvailable() const OVERRIDE; |
| 147 |
| 148 // Return the last time the SRV record has been seen. Return base::Time for |
| 149 // unavailable records. |
| 150 virtual void ReadLastSeen( |
| 151 const ServiceReader::LastSeenCallback& callback) OVERRIDE; |
| 152 |
| 153 virtual void OnRecordUpdate(net::MDnsListener::UpdateType update, |
| 154 const net::RecordParsed* record) OVERRIDE; |
| 155 |
| 156 virtual void OnNsecRecord(const std::string& name, unsigned type) OVERRIDE; |
| 157 |
| 158 virtual void OnCachePurged() OVERRIDE; |
| 159 |
| 160 private: |
| 161 // Respond to transaction finishing for SRV records. |
| 162 void SrvRecordTransactionResponse(net::MDnsTransaction::Result status, |
| 163 const net::RecordParsed* record); |
| 164 |
| 165 // Respond to transaction finishing for TXT records. |
| 166 void TxtRecordTransactionResponse(net::MDnsTransaction::Result status, |
| 167 const net::RecordParsed* record); |
| 168 |
| 169 void LastSeenTransactionResponse( |
| 170 const ServiceReader::LastSeenCallback& callback, |
| 171 net::MDnsTransaction::Result status, |
| 172 const net::RecordParsed* record); |
| 173 |
| 174 // Convert a TXT record to a vector of strings (metadata). |
| 175 const std::vector<std::string>& RecordToMetadata( |
| 176 const net::RecordParsed* record) const; |
| 177 |
| 178 // Convert an SRV record to a host and port pair. |
| 179 net::HostPortPair RecordToAddress( |
| 180 const net::RecordParsed* record) const; |
| 181 |
| 182 // Convert an MDns status to a service discovery status. |
| 183 ServiceReader::RequestStatus MDnsStatusToRequestStatus( |
| 184 net::MDnsTransaction::Result status) const; |
| 185 |
| 186 bool CreateTxtTransaction(); |
| 187 bool CreateSrvTransaction(); |
| 188 |
| 189 std::string service_name_; |
| 190 ServiceReader::Delegate* delegate_; |
| 191 |
| 192 scoped_ptr<net::MDnsListener> txt_listener_; |
| 193 scoped_ptr<net::MDnsListener> srv_listener_; |
| 194 |
| 195 scoped_ptr<net::MDnsTransaction> txt_transaction_; |
| 196 scoped_ptr<net::MDnsTransaction> srv_transaction_; |
| 197 scoped_ptr<net::MDnsTransaction> last_seen_transaction_; |
| 198 |
| 199 std::vector<ServiceReader::AddressCallback> address_callbacks_; |
| 200 std::vector<ServiceReader::MetadataCallback> metadata_callbacks_; |
| 201 |
| 202 bool has_address_; |
| 203 bool has_metadata_; |
| 204 net::HostPortPair address_; |
| 205 std::vector<std::string> metadata_; |
| 206 bool started_; |
| 207 |
| 208 DISALLOW_COPY_AND_ASSIGN(ServiceReaderImpl); |
| 209 }; |
| 210 |
| 211 } // namespace local_discovery |
| 212 |
| 213 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_ |
| OLD | NEW |