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/linked_ptr.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/message_loop.h" | |
| 17 #include "chrome/browser/local_discovery/service_discovery_client.h" | |
| 18 #include "net/dns/mdns_client.h" | |
| 19 | |
| 20 namespace local_discovery { | |
| 21 | |
| 22 class ServiceDiscoveryClientImpl : public ServiceDiscoveryClient { | |
| 23 public: | |
| 24 ServiceDiscoveryClientImpl(); | |
| 25 virtual ~ServiceDiscoveryClientImpl(); | |
| 26 | |
| 27 // ServiceDiscoveryClient implementation: | |
| 28 virtual scoped_ptr<ServiceWatcher> CreateServiceWatcher( | |
| 29 const std::string& service_type, | |
| 30 ServiceWatcher::Delegate* delegate) OVERRIDE; | |
| 31 | |
| 32 virtual scoped_ptr<ServiceResolver> CreateServiceResolver( | |
| 33 const std::string& service_name, | |
| 34 const ServiceResolver::ResolveCompleteCallback& callback) OVERRIDE; | |
| 35 | |
| 36 static ServiceDiscoveryClientImpl* GetInstance(); | |
| 37 private: | |
| 38 DISALLOW_COPY_AND_ASSIGN(ServiceDiscoveryClientImpl); | |
| 39 }; | |
| 40 | |
| 41 class ServiceWatcherImpl : public ServiceWatcher, | |
| 42 public net::MDnsListener::Delegate { | |
| 43 public: | |
| 44 ServiceWatcherImpl(const std::string& service_type, | |
| 45 ServiceWatcher::Delegate* delegate); | |
| 46 // Listening will automatically stop when the destructor is called. | |
| 47 virtual ~ServiceWatcherImpl(); | |
| 48 | |
| 49 // ServiceWatcher implementation: | |
| 50 virtual bool Start() OVERRIDE; | |
| 51 | |
| 52 virtual void GetAvailableServices( | |
| 53 std::vector<std::string>* services) const OVERRIDE; | |
| 54 | |
| 55 virtual void DiscoverNewServices(bool force_update) OVERRIDE; | |
| 56 | |
| 57 virtual std::string GetServiceType() const OVERRIDE; | |
| 58 | |
| 59 virtual void ReadCachedServices() OVERRIDE; | |
| 60 | |
| 61 virtual void OnRecordUpdate(net::MDnsListener::UpdateType update, | |
| 62 const net::RecordParsed* record) OVERRIDE; | |
| 63 | |
| 64 virtual void OnNsecRecord(const std::string& name, unsigned rrtype) OVERRIDE; | |
| 65 | |
| 66 virtual void OnCachePurged() OVERRIDE; | |
| 67 | |
| 68 virtual void OnTransactionResponse( | |
| 69 scoped_ptr<net::MDnsTransaction>* transaction, | |
| 70 net::MDnsTransaction::Result result, | |
| 71 const net::RecordParsed* record); | |
| 72 | |
| 73 private: | |
| 74 struct ServiceListeners { | |
| 75 ServiceListeners(const std::string& service_name, | |
| 76 ServiceWatcherImpl* watcher); | |
| 77 ~ServiceListeners(); | |
| 78 bool Start(); | |
| 79 | |
| 80 private: | |
| 81 scoped_ptr<net::MDnsListener> srv_listener_; | |
| 82 scoped_ptr<net::MDnsListener> txt_listener_; | |
| 83 }; | |
| 84 | |
| 85 typedef std::map<std::string, linked_ptr<ServiceListeners>> | |
|
szym
2013/06/27 22:03:19
Please fix. ">>" is a syntax with our build flags
| |
| 86 ServiceListenersMap; | |
| 87 | |
| 88 void AddService(const std::string& service); | |
| 89 void RemoveService(const std::string& service); | |
| 90 bool CreateTransaction(bool active, bool alert_existing_services, | |
| 91 bool force_refresh, | |
| 92 scoped_ptr<net::MDnsTransaction>* transaction); | |
| 93 | |
| 94 std::string service_type_; | |
| 95 ServiceListenersMap services_; | |
| 96 scoped_ptr<net::MDnsTransaction> transaction_network_; | |
| 97 scoped_ptr<net::MDnsTransaction> transaction_cache_; | |
| 98 scoped_ptr<net::MDnsListener> listener_; | |
| 99 | |
| 100 ServiceWatcher::Delegate* delegate_; | |
| 101 bool started_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(ServiceWatcherImpl); | |
| 104 }; | |
| 105 | |
| 106 class ServiceResolverImpl | |
| 107 : public ServiceResolver, | |
| 108 public base::SupportsWeakPtr<ServiceResolverImpl> { | |
| 109 public: | |
| 110 ServiceResolverImpl(const std::string& service_name, | |
| 111 const ServiceResolver::ResolveCompleteCallback& callback); | |
| 112 | |
| 113 virtual ~ServiceResolverImpl(); | |
| 114 | |
| 115 // ServiceResolver implementation: | |
| 116 virtual bool StartResolving() OVERRIDE; | |
| 117 | |
| 118 virtual bool IsResolving() const OVERRIDE; | |
| 119 | |
| 120 virtual bool HasResolved() const OVERRIDE; | |
| 121 | |
| 122 virtual std::string GetName() const OVERRIDE; | |
| 123 | |
| 124 virtual const ServiceDescription& GetServiceDescription() const OVERRIDE; | |
| 125 | |
| 126 private: | |
| 127 // Respond to transaction finishing for SRV records. | |
| 128 void SrvRecordTransactionResponse(net::MDnsTransaction::Result status, | |
| 129 const net::RecordParsed* record); | |
| 130 | |
| 131 // Respond to transaction finishing for TXT records. | |
| 132 void TxtRecordTransactionResponse(net::MDnsTransaction::Result status, | |
| 133 const net::RecordParsed* record); | |
| 134 | |
| 135 // Respond to transaction finishing for A records. | |
| 136 void ARecordTransactionResponse(net::MDnsTransaction::Result status, | |
| 137 const net::RecordParsed* record); | |
| 138 | |
| 139 void AlertCallbackIfReady(); | |
| 140 | |
| 141 void ServiceNotFound(RequestStatus status); | |
| 142 | |
| 143 // Convert a TXT record to a vector of strings (metadata). | |
| 144 const std::vector<std::string>& RecordToMetadata( | |
| 145 const net::RecordParsed* record) const; | |
| 146 | |
| 147 // Convert an SRV record to a host and port pair. | |
| 148 net::HostPortPair RecordToAddress( | |
| 149 const net::RecordParsed* record) const; | |
| 150 | |
| 151 // Convert an A record to an IP address. | |
| 152 const net::IPAddressNumber& RecordToIPAddress( | |
| 153 const net::RecordParsed* record) const; | |
| 154 | |
| 155 // Convert an MDns status to a service discovery status. | |
| 156 RequestStatus MDnsStatusToRequestStatus( | |
| 157 net::MDnsTransaction::Result status) const; | |
| 158 | |
| 159 bool CreateTxtTransaction(); | |
| 160 bool CreateSrvTransaction(); | |
| 161 void CreateATransaction(); | |
| 162 | |
| 163 std::string service_name_; | |
| 164 ResolveCompleteCallback callback_; | |
| 165 | |
| 166 bool is_resolving_; | |
| 167 bool has_resolved_; | |
| 168 | |
| 169 bool metadata_resolved_; | |
| 170 bool address_resolved_; | |
| 171 | |
| 172 scoped_ptr<net::MDnsTransaction> txt_transaction_; | |
| 173 scoped_ptr<net::MDnsTransaction> srv_transaction_; | |
| 174 scoped_ptr<net::MDnsTransaction> a_transaction_; | |
| 175 | |
| 176 scoped_ptr<ServiceDescription> service_staging_; | |
| 177 scoped_ptr<ServiceDescription> service_final_; | |
| 178 | |
| 179 DISALLOW_COPY_AND_ASSIGN(ServiceResolverImpl); | |
| 180 }; | |
| 181 | |
| 182 } // namespace local_discovery | |
| 183 | |
| 184 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_ | |
| OLD | NEW |