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_IMPL_H_ | |
| 6 #define CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_ | |
| 7 | |
| 8 #include <map> | |
| 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<ServiceWatcher> CreateServiceWatcher( | |
| 29 const std::string& service_type, | |
| 30 ServiceWatcher::Delegate* delegate) OVERRIDE; | |
| 31 | |
| 32 // Create a service object listening for DNS-SD service announcements on | |
|
gene
2013/06/18 23:46:53
Comment is for a listener here.
Noam Samuel
2013/06/19 18:46:22
Removed comments on implementation and added refer
| |
| 33 // service |service_name|. |delegate| may be null in which case the | |
| 34 // object will not recieve continuous announcements but may still query | |
| 35 // about the relevant service. | |
| 36 virtual scoped_ptr<ServiceResolver> CreateServiceResolver( | |
| 37 const std::string& service_name, | |
| 38 const ServiceResolver::ResolveCompleteCallback& callback) OVERRIDE; | |
| 39 | |
| 40 static ServiceDiscoveryClientImpl* GetInstance(); | |
| 41 private: | |
| 42 DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryClientImpl); | |
| 43 }; | |
| 44 | |
| 45 class ServiceWatcherImpl : public ServiceWatcher, | |
| 46 public net::MDnsListener::Delegate { | |
| 47 public: | |
| 48 ServiceWatcherImpl(const std::string& service_type, | |
| 49 ServiceWatcher::Delegate* delegate); | |
| 50 // Listening will automatically stop when the destructor is called. | |
| 51 virtual ~ServiceWatcherImpl(); | |
| 52 | |
| 53 virtual bool Start() OVERRIDE; | |
| 54 | |
| 55 // Get all known services names of this watcher's type. Return them in | |
| 56 // |services|. | |
| 57 virtual void GetAvailableServices( | |
| 58 std::vector<std::string>* services) const OVERRIDE; | |
| 59 | |
| 60 // Probe for services of this type. | |
| 61 virtual void DiscoverNewServices(bool force_update) OVERRIDE; | |
| 62 | |
| 63 virtual std::string GetServiceType() const OVERRIDE; | |
| 64 | |
| 65 virtual void ReadCachedServices() OVERRIDE; | |
| 66 | |
| 67 virtual void OnRecordUpdate(net::MDnsListener::UpdateType update, | |
| 68 const net::RecordParsed* record) OVERRIDE; | |
| 69 | |
| 70 virtual void OnNsecRecord(const std::string& name, unsigned rrtype) OVERRIDE; | |
| 71 | |
| 72 virtual void OnCachePurged() OVERRIDE; | |
| 73 | |
| 74 virtual void OnTransactionResponse( | |
| 75 scoped_ptr<net::MDnsTransaction>* transaction, | |
| 76 net::MDnsTransaction::Result result, | |
| 77 const net::RecordParsed* record); | |
| 78 | |
| 79 private: | |
| 80 struct ServiceListeners { | |
| 81 ServiceListeners(const std::string& service_name, | |
| 82 ServiceWatcherImpl* watcher); | |
| 83 ~ServiceListeners(); | |
| 84 bool Start(); | |
| 85 | |
| 86 private: | |
| 87 scoped_ptr<net::MDnsListener> srv_listener_; | |
| 88 scoped_ptr<net::MDnsListener> txt_listener_; | |
| 89 }; | |
| 90 | |
| 91 void AddService(const std::string& service); | |
| 92 void RemoveService(const std::string& service); | |
| 93 bool CreateTransaction(bool active, bool alert_existing_services, | |
| 94 bool force_refresh, | |
| 95 scoped_ptr<net::MDnsTransaction>* transaction); | |
| 96 | |
| 97 std::string service_type_; | |
| 98 std::map<std::string, ServiceListeners*> services_; | |
|
Vitaly Buka (NO REVIEWS)
2013/06/18 23:01:46
Please define typedef for td::map<std::string, Ser
Noam Samuel
2013/06/19 18:46:22
Done.
| |
| 99 scoped_ptr<net::MDnsTransaction> transaction_network_; | |
| 100 scoped_ptr<net::MDnsTransaction> transaction_cache_; | |
| 101 scoped_ptr<net::MDnsListener> listener_; | |
| 102 | |
| 103 ServiceWatcher::Delegate* delegate_; | |
| 104 bool started_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(ServiceWatcherImpl); | |
| 107 }; | |
| 108 | |
| 109 | |
| 110 class ServiceResolverImpl | |
| 111 : public ServiceResolver, | |
| 112 public base::SupportsWeakPtr<ServiceResolverImpl> { | |
| 113 public: | |
| 114 ServiceResolverImpl(const std::string& service_name, | |
| 115 const ServiceResolver::ResolveCompleteCallback& callback); | |
| 116 | |
| 117 virtual ~ServiceResolverImpl(); | |
| 118 | |
| 119 virtual bool StartResolving() OVERRIDE; | |
| 120 | |
| 121 virtual bool IsResolving() const OVERRIDE; | |
| 122 | |
| 123 virtual bool HasResolved() const OVERRIDE; | |
| 124 | |
| 125 virtual std::string GetHumanReadableName() const OVERRIDE; | |
| 126 virtual std::string GetType() const OVERRIDE; | |
| 127 virtual std::string GetName() const OVERRIDE; | |
| 128 | |
| 129 virtual const Service& GetService() const OVERRIDE; | |
| 130 | |
| 131 private: | |
| 132 Service* staging_service() { | |
| 133 return &services_[(current_service_index_ + 1) % 2]; | |
| 134 } | |
| 135 | |
| 136 // Respond to transaction finishing for SRV records. | |
| 137 void SrvRecordTransactionResponse(net::MDnsTransaction::Result status, | |
| 138 const net::RecordParsed* record); | |
| 139 | |
| 140 // Respond to transaction finishing for TXT records. | |
| 141 void TxtRecordTransactionResponse(net::MDnsTransaction::Result status, | |
| 142 const net::RecordParsed* record); | |
| 143 | |
| 144 // Respond to transaction finishing for A records. | |
| 145 void ARecordTransactionResponse(net::MDnsTransaction::Result status, | |
| 146 const net::RecordParsed* record); | |
| 147 | |
| 148 void AlertCallbackIfReady(); | |
| 149 | |
| 150 // Convert a TXT record to a vector of strings (metadata). | |
| 151 const std::vector<std::string>& RecordToMetadata( | |
| 152 const net::RecordParsed* record) const; | |
| 153 | |
| 154 // Convert an SRV record to a host and port pair. | |
| 155 net::HostPortPair RecordToAddress( | |
| 156 const net::RecordParsed* record) const; | |
| 157 | |
| 158 // Convert an A record to an IP address. | |
| 159 const net::IPAddressNumber& RecordToIPAddress( | |
| 160 const net::RecordParsed* record) const; | |
| 161 | |
| 162 // Convert an MDns status to a service discovery status. | |
| 163 RequestStatus MDnsStatusToRequestStatus( | |
| 164 net::MDnsTransaction::Result status) const; | |
| 165 | |
| 166 bool CreateTxtTransaction(); | |
| 167 bool CreateSrvTransaction(); | |
| 168 void CreateATransaction(); | |
| 169 | |
| 170 std::string service_name_; | |
| 171 ResolveCompleteCallback callback_; | |
| 172 | |
| 173 bool is_resolving_; | |
| 174 bool has_resolved_; | |
| 175 | |
| 176 bool metadata_resolved_; | |
| 177 bool address_resolved_; | |
| 178 | |
| 179 scoped_ptr<net::MDnsTransaction> txt_transaction_; | |
| 180 scoped_ptr<net::MDnsTransaction> srv_transaction_; | |
| 181 scoped_ptr<net::MDnsTransaction> a_transaction_; | |
| 182 | |
| 183 Service services_[2]; | |
| 184 int current_service_index_; | |
| 185 | |
| 186 DISALLOW_COPY_AND_ASSIGN(ServiceResolverImpl); | |
| 187 }; | |
| 188 | |
| 189 } // namespace local_discovery | |
| 190 | |
| 191 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_ | |
| OLD | NEW |