Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: chrome/browser/local_discovery/service_discovery_client_impl.h

Issue 16272006: In-browser DNS-based service discovery system (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mdns_implementation
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 {
gene 2013/06/19 22:48:03 not sure what is the correct indentation here
Vitaly Buka (NO REVIEWS) 2013/06/19 22:55:21 all publics must be aligned
Noam Samuel 2013/06/19 23:53:29 Done.
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>>
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
107 class ServiceResolverImpl
108 : public ServiceResolver,
109 public base::SupportsWeakPtr<ServiceResolverImpl> {
110 public:
111 ServiceResolverImpl(const std::string& service_name,
112 const ServiceResolver::ResolveCompleteCallback& callback);
113
114 virtual ~ServiceResolverImpl();
115
116 // ServiceResolver implementation:
117 virtual bool StartResolving() OVERRIDE;
118
119 virtual bool IsResolving() const OVERRIDE;
120
121 virtual bool HasResolved() const OVERRIDE;
122
123 virtual std::string GetName() const OVERRIDE;
124
125 virtual const ServiceDescription& GetServiceDescription() const OVERRIDE;
126
127 private:
128 ServiceDescription* staging_service() {
129 return &services_[(current_service_index_ + 1) % 2];
130 }
131
132 // Respond to transaction finishing for SRV records.
133 void SrvRecordTransactionResponse(net::MDnsTransaction::Result status,
134 const net::RecordParsed* record);
135
136 // Respond to transaction finishing for TXT records.
137 void TxtRecordTransactionResponse(net::MDnsTransaction::Result status,
138 const net::RecordParsed* record);
139
140 // Respond to transaction finishing for A records.
141 void ARecordTransactionResponse(net::MDnsTransaction::Result status,
142 const net::RecordParsed* record);
143
144 void AlertCallbackIfReady();
145
146 // Convert a TXT record to a vector of strings (metadata).
147 const std::vector<std::string>& RecordToMetadata(
148 const net::RecordParsed* record) const;
149
150 // Convert an SRV record to a host and port pair.
151 net::HostPortPair RecordToAddress(
152 const net::RecordParsed* record) const;
153
154 // Convert an A record to an IP address.
155 const net::IPAddressNumber& RecordToIPAddress(
156 const net::RecordParsed* record) const;
157
158 // Convert an MDns status to a service discovery status.
159 RequestStatus MDnsStatusToRequestStatus(
160 net::MDnsTransaction::Result status) const;
161
162 bool CreateTxtTransaction();
163 bool CreateSrvTransaction();
164 void CreateATransaction();
165
166 std::string service_name_;
167 ResolveCompleteCallback callback_;
168
169 bool is_resolving_;
170 bool has_resolved_;
171
172 bool metadata_resolved_;
173 bool address_resolved_;
174
175 scoped_ptr<net::MDnsTransaction> txt_transaction_;
176 scoped_ptr<net::MDnsTransaction> srv_transaction_;
177 scoped_ptr<net::MDnsTransaction> a_transaction_;
178
179 ServiceDescription services_[2];
gene 2013/06/19 22:48:03 This is a bit confusing, a few alternatives are: 1
Noam Samuel 2013/06/19 23:53:29 Done.
180 int current_service_index_;
181
182 DISALLOW_COPY_AND_ASSIGN(ServiceResolverImpl);
183 };
184
185 } // namespace local_discovery
186
187 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_SERVICE_DISCOVERY_CLIENT_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698