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

Side by Side Diff: chromeos/dbus/peer_daemon_manager_client.cc

Issue 2295433002: chromeos: Remove unused peerd D-Bus client libraries (Closed)
Patch Set: rebase Created 4 years, 3 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
« no previous file with comments | « chromeos/dbus/peer_daemon_manager_client.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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 #include "chromeos/dbus/peer_daemon_manager_client.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "dbus/bus.h"
17 #include "dbus/message.h"
18 #include "dbus/object_manager.h"
19 #include "dbus/object_proxy.h"
20 #include "dbus/values_util.h"
21
22 namespace chromeos {
23 namespace {
24
25 // TODO(benchan): Move these constants to system_api.
26 namespace peerd {
27 const char kPeerdServiceName[] = "org.chromium.peerd";
28 const char kPeerdObjectManagerServicePath[] = "/org/chromium/peerd";
29 const char kPeerdManagerPath[] = "/org/chromium/peerd/Manager";
30 const char kManagerInterface[] = "org.chromium.peerd.Manager";
31 const char kServiceInterface[] = "org.chromium.peerd.Service";
32 const char kPeerInterface[] = "org.chromium.peerd.Peer";
33 const char kStartMonitoringMethod[] = "StartMonitoring";
34 const char kStopMonitoringMethod[] = "StopMonitoring";
35 const char kExposeServiceMethod[] = "ExposeService";
36 const char kRemoveExposedServiceMethod[] = "RemoveExposedService";
37 const char kPingMethod[] = "Ping";
38 } // namespace peerd
39
40 // The PeerDaemonManagerClient implementation used in production.
41 class PeerDaemonManagerClientImpl : public PeerDaemonManagerClient,
42 public dbus::ObjectManager::Interface {
43 public:
44 PeerDaemonManagerClientImpl();
45 ~PeerDaemonManagerClientImpl() override;
46
47 // DBusClient overrides.
48 void Init(dbus::Bus* bus) override;
49
50 // PeerDaemonManagerClient overrides.
51 void AddObserver(Observer* observer) override;
52 void RemoveObserver(Observer* observer) override;
53 std::vector<dbus::ObjectPath> GetServices() override;
54 std::vector<dbus::ObjectPath> GetPeers() override;
55 ServiceProperties* GetServiceProperties(
56 const dbus::ObjectPath& object_path) override;
57 PeerProperties* GetPeerProperties(
58 const dbus::ObjectPath& object_path) override;
59 void StartMonitoring(
60 const std::vector<std::string>& requested_technologies,
61 const base::DictionaryValue& options,
62 const StringDBusMethodCallback& callback) override;
63 void StopMonitoring(const std::string& monitoring_token,
64 const VoidDBusMethodCallback& callback) override;
65 void ExposeService(
66 const std::string& service_id,
67 const std::map<std::string, std::string>& service_info,
68 const base::DictionaryValue& options,
69 const StringDBusMethodCallback& callback) override;
70 void RemoveExposedService(const std::string& service_token,
71 const VoidDBusMethodCallback& callback) override;
72 void Ping(const StringDBusMethodCallback& callback) override;
73
74 // dbus::ObjectManager::Interface overrides.
75 dbus::PropertySet* CreateProperties(
76 dbus::ObjectProxy* object_proxy,
77 const dbus::ObjectPath& object_path,
78 const std::string& interface_name) override;
79 void ObjectAdded(const dbus::ObjectPath& object_path,
80 const std::string& interface_name) override;
81 void ObjectRemoved(const dbus::ObjectPath& object_path,
82 const std::string& interface_name) override;
83
84 private:
85 void OnStringDBusMethod(const StringDBusMethodCallback& callback,
86 dbus::Response* response);
87 void OnVoidDBusMethod(const VoidDBusMethodCallback& callback,
88 dbus::Response* response);
89 void OnManagerPropertyChanged(const std::string& property_name);
90 void OnServicePropertyChanged(const dbus::ObjectPath& object_path,
91 const std::string& property_name);
92 void OnPeerPropertyChanged(const dbus::ObjectPath& object_path,
93 const std::string& property_name);
94
95 // List of observers interested in event notifications from us.
96 base::ObserverList<Observer> observers_;
97 dbus::ObjectManager* object_manager_;
98
99 base::WeakPtrFactory<PeerDaemonManagerClientImpl> weak_ptr_factory_;
100
101 DISALLOW_COPY_AND_ASSIGN(PeerDaemonManagerClientImpl);
102 };
103
104 PeerDaemonManagerClientImpl::PeerDaemonManagerClientImpl()
105 : object_manager_(nullptr), weak_ptr_factory_(this) {
106 }
107
108 PeerDaemonManagerClientImpl::~PeerDaemonManagerClientImpl() {
109 if (object_manager_) {
110 object_manager_->UnregisterInterface(peerd::kManagerInterface);
111 object_manager_->UnregisterInterface(peerd::kServiceInterface);
112 object_manager_->UnregisterInterface(peerd::kPeerInterface);
113 }
114 }
115
116 void PeerDaemonManagerClientImpl::AddObserver(Observer* observer) {
117 DCHECK(observer);
118 observers_.AddObserver(observer);
119 }
120
121 void PeerDaemonManagerClientImpl::RemoveObserver(Observer* observer) {
122 DCHECK(observer);
123 observers_.RemoveObserver(observer);
124 }
125
126 std::vector<dbus::ObjectPath> PeerDaemonManagerClientImpl::GetServices() {
127 return object_manager_->GetObjectsWithInterface(peerd::kServiceInterface);
128 }
129
130 std::vector<dbus::ObjectPath> PeerDaemonManagerClientImpl::GetPeers() {
131 return object_manager_->GetObjectsWithInterface(peerd::kPeerInterface);
132 }
133
134 PeerDaemonManagerClient::ServiceProperties*
135 PeerDaemonManagerClientImpl::GetServiceProperties(
136 const dbus::ObjectPath& object_path) {
137 return static_cast<ServiceProperties*>(
138 object_manager_->GetProperties(object_path, peerd::kServiceInterface));
139 }
140
141 PeerDaemonManagerClient::PeerProperties*
142 PeerDaemonManagerClientImpl::GetPeerProperties(
143 const dbus::ObjectPath& object_path) {
144 return static_cast<PeerProperties*>(
145 object_manager_->GetProperties(object_path, peerd::kPeerInterface));
146 }
147
148 void PeerDaemonManagerClientImpl::StartMonitoring(
149 const std::vector<std::string>& requested_technologies,
150 const base::DictionaryValue& options,
151 const StringDBusMethodCallback& callback) {
152 dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
153 dbus::ObjectPath(peerd::kPeerdManagerPath));
154 if (!object_proxy) {
155 base::ThreadTaskRunnerHandle::Get()->PostTask(
156 FROM_HERE,
157 base::Bind(&PeerDaemonManagerClientImpl::OnStringDBusMethod,
158 weak_ptr_factory_.GetWeakPtr(), callback, nullptr));
159 return;
160 }
161
162 dbus::MethodCall method_call(peerd::kManagerInterface,
163 peerd::kStartMonitoringMethod);
164 dbus::MessageWriter writer(&method_call);
165 writer.AppendArrayOfStrings(requested_technologies);
166 dbus::AppendValueData(&writer, options);
167 object_proxy->CallMethod(
168 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
169 base::Bind(&PeerDaemonManagerClientImpl::OnStringDBusMethod,
170 weak_ptr_factory_.GetWeakPtr(), callback));
171 }
172
173 void PeerDaemonManagerClientImpl::StopMonitoring(
174 const std::string& monitoring_token,
175 const VoidDBusMethodCallback& callback) {
176 dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
177 dbus::ObjectPath(peerd::kPeerdManagerPath));
178 if (!object_proxy) {
179 base::ThreadTaskRunnerHandle::Get()->PostTask(
180 FROM_HERE,
181 base::Bind(&PeerDaemonManagerClientImpl::OnVoidDBusMethod,
182 weak_ptr_factory_.GetWeakPtr(), callback, nullptr));
183 return;
184 }
185
186 dbus::MethodCall method_call(peerd::kManagerInterface,
187 peerd::kStopMonitoringMethod);
188 dbus::MessageWriter writer(&method_call);
189 writer.AppendString(monitoring_token);
190 object_proxy->CallMethod(
191 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
192 base::Bind(&PeerDaemonManagerClientImpl::OnVoidDBusMethod,
193 weak_ptr_factory_.GetWeakPtr(), callback));
194 }
195
196 void PeerDaemonManagerClientImpl::ExposeService(
197 const std::string& service_id,
198 const std::map<std::string, std::string>& service_info,
199 const base::DictionaryValue& options,
200 const StringDBusMethodCallback& callback) {
201 dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
202 dbus::ObjectPath(peerd::kPeerdManagerPath));
203 if (!object_proxy) {
204 base::ThreadTaskRunnerHandle::Get()->PostTask(
205 FROM_HERE,
206 base::Bind(&PeerDaemonManagerClientImpl::OnStringDBusMethod,
207 weak_ptr_factory_.GetWeakPtr(), callback, nullptr));
208 return;
209 }
210
211 dbus::MethodCall method_call(peerd::kManagerInterface,
212 peerd::kExposeServiceMethod);
213 dbus::MessageWriter writer(&method_call);
214 writer.AppendString(service_id);
215
216 dbus::MessageWriter array_writer(nullptr);
217 writer.OpenArray("{ss}", &array_writer);
218 for (const auto& entry : service_info) {
219 dbus::MessageWriter dict_entry_writer(nullptr);
220 array_writer.OpenDictEntry(&dict_entry_writer);
221 dict_entry_writer.AppendString(entry.first);
222 dict_entry_writer.AppendString(entry.second);
223 array_writer.CloseContainer(&dict_entry_writer);
224 }
225 writer.CloseContainer(&array_writer);
226
227 dbus::AppendValueData(&writer, options);
228 object_proxy->CallMethod(
229 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
230 base::Bind(&PeerDaemonManagerClientImpl::OnStringDBusMethod,
231 weak_ptr_factory_.GetWeakPtr(), callback));
232 }
233
234 void PeerDaemonManagerClientImpl::RemoveExposedService(
235 const std::string& service_token,
236 const VoidDBusMethodCallback& callback) {
237 dbus::MethodCall method_call(peerd::kManagerInterface,
238 peerd::kRemoveExposedServiceMethod);
239 dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
240 dbus::ObjectPath(peerd::kPeerdManagerPath));
241 if (!object_proxy) {
242 base::ThreadTaskRunnerHandle::Get()->PostTask(
243 FROM_HERE,
244 base::Bind(&PeerDaemonManagerClientImpl::OnVoidDBusMethod,
245 weak_ptr_factory_.GetWeakPtr(), callback, nullptr));
246 return;
247 }
248 dbus::MessageWriter writer(&method_call);
249 writer.AppendString(service_token);
250 object_proxy->CallMethod(
251 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
252 base::Bind(&PeerDaemonManagerClientImpl::OnVoidDBusMethod,
253 weak_ptr_factory_.GetWeakPtr(), callback));
254 }
255
256 void PeerDaemonManagerClientImpl::Ping(
257 const StringDBusMethodCallback& callback) {
258 dbus::MethodCall method_call(peerd::kManagerInterface,
259 peerd::kPingMethod);
260 dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
261 dbus::ObjectPath(peerd::kPeerdManagerPath));
262 if (!object_proxy) {
263 base::ThreadTaskRunnerHandle::Get()->PostTask(
264 FROM_HERE,
265 base::Bind(&PeerDaemonManagerClientImpl::OnStringDBusMethod,
266 weak_ptr_factory_.GetWeakPtr(), callback, nullptr));
267 return;
268 }
269 dbus::MessageWriter writer(&method_call);
270 object_proxy->CallMethod(
271 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
272 base::Bind(&PeerDaemonManagerClientImpl::OnStringDBusMethod,
273 weak_ptr_factory_.GetWeakPtr(), callback));
274 }
275
276 dbus::PropertySet* PeerDaemonManagerClientImpl::CreateProperties(
277 dbus::ObjectProxy* object_proxy,
278 const dbus::ObjectPath& object_path,
279 const std::string& interface_name) {
280 dbus::PropertySet* properties = nullptr;
281 if (interface_name == peerd::kManagerInterface) {
282 properties = new ManagerProperties(
283 object_proxy,
284 base::Bind(&PeerDaemonManagerClientImpl::OnManagerPropertyChanged,
285 weak_ptr_factory_.GetWeakPtr()));
286 } else if (interface_name == peerd::kServiceInterface) {
287 properties = new ServiceProperties(
288 object_proxy,
289 base::Bind(&PeerDaemonManagerClientImpl::OnServicePropertyChanged,
290 weak_ptr_factory_.GetWeakPtr(), object_path));
291 } else if (interface_name == peerd::kPeerInterface) {
292 properties = new PeerProperties(
293 object_proxy,
294 base::Bind(&PeerDaemonManagerClientImpl::OnPeerPropertyChanged,
295 weak_ptr_factory_.GetWeakPtr(), object_path));
296 } else {
297 NOTREACHED() << "Unhandled interface name " << interface_name;
298 }
299 return properties;
300 }
301
302 void PeerDaemonManagerClientImpl::ObjectAdded(
303 const dbus::ObjectPath& object_path,
304 const std::string& interface_name) {
305 if (interface_name == peerd::kManagerInterface) {
306 FOR_EACH_OBSERVER(Observer, observers_, ManagerAdded());
307 } else if (interface_name == peerd::kServiceInterface) {
308 FOR_EACH_OBSERVER(Observer, observers_, ServiceAdded(object_path));
309 } else if (interface_name == peerd::kPeerInterface) {
310 FOR_EACH_OBSERVER(Observer, observers_, PeerAdded(object_path));
311 } else {
312 NOTREACHED() << "Unhandled interface name " << interface_name;
313 }
314 }
315
316 void PeerDaemonManagerClientImpl::ObjectRemoved(
317 const dbus::ObjectPath& object_path,
318 const std::string& interface_name) {
319 if (interface_name == peerd::kManagerInterface) {
320 FOR_EACH_OBSERVER(Observer, observers_, ManagerRemoved());
321 } else if (interface_name == peerd::kServiceInterface) {
322 FOR_EACH_OBSERVER(Observer, observers_, ServiceRemoved(object_path));
323 } else if (interface_name == peerd::kPeerInterface) {
324 FOR_EACH_OBSERVER(Observer, observers_, PeerRemoved(object_path));
325 } else {
326 NOTREACHED() << "Unhandled interface name " << interface_name;
327 }
328 }
329
330 void PeerDaemonManagerClientImpl::OnStringDBusMethod(
331 const StringDBusMethodCallback& callback,
332 dbus::Response* response) {
333 if (!response) {
334 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string());
335 return;
336 }
337
338 dbus::MessageReader reader(response);
339 std::string result;
340 if (!reader.PopString(&result)) {
341 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string());
342 return;
343 }
344
345 callback.Run(DBUS_METHOD_CALL_SUCCESS, result);
346 }
347
348 void PeerDaemonManagerClientImpl::OnVoidDBusMethod(
349 const VoidDBusMethodCallback& callback,
350 dbus::Response* response) {
351 callback.Run(response ? DBUS_METHOD_CALL_SUCCESS : DBUS_METHOD_CALL_FAILURE);
352 }
353
354 void PeerDaemonManagerClientImpl::Init(dbus::Bus* bus) {
355 object_manager_ = bus->GetObjectManager(
356 peerd::kPeerdServiceName,
357 dbus::ObjectPath(peerd::kPeerdObjectManagerServicePath));
358 object_manager_->RegisterInterface(peerd::kManagerInterface, this);
359 object_manager_->RegisterInterface(peerd::kServiceInterface, this);
360 object_manager_->RegisterInterface(peerd::kPeerInterface, this);
361 }
362
363 void PeerDaemonManagerClientImpl::OnManagerPropertyChanged(
364 const std::string& property_name) {
365 FOR_EACH_OBSERVER(Observer, observers_,
366 ManagerPropertyChanged(property_name));
367 }
368
369 void PeerDaemonManagerClientImpl::OnServicePropertyChanged(
370 const dbus::ObjectPath& object_path,
371 const std::string& property_name) {
372 FOR_EACH_OBSERVER(Observer, observers_,
373 ServicePropertyChanged(object_path, property_name));
374 }
375
376 void PeerDaemonManagerClientImpl::OnPeerPropertyChanged(
377 const dbus::ObjectPath& object_path,
378 const std::string& property_name) {
379 FOR_EACH_OBSERVER(Observer, observers_,
380 PeerPropertyChanged(object_path, property_name));
381 }
382
383 } // namespace
384
385 PeerDaemonManagerClient::ManagerProperties::ManagerProperties(
386 dbus::ObjectProxy* object_proxy,
387 const PropertyChangedCallback& callback)
388 : dbus::PropertySet{object_proxy, peerd::kManagerInterface, callback} {
389 RegisterProperty("MonitoredTechnologies", &monitored_technologies_);
390 }
391
392 PeerDaemonManagerClient::ManagerProperties::~ManagerProperties() {
393 }
394
395 PeerDaemonManagerClient::ServiceProperties::ServiceProperties(
396 dbus::ObjectProxy* object_proxy,
397 const PropertyChangedCallback& callback)
398 : dbus::PropertySet{object_proxy, peerd::kServiceInterface, callback} {
399 RegisterProperty("ServiceId", &service_id_);
400 RegisterProperty("ServiceInfo", &service_info_);
401 RegisterProperty("IpInfos", &ip_infos_);
402 }
403
404 PeerDaemonManagerClient::ServiceProperties::~ServiceProperties() {
405 }
406
407 PeerDaemonManagerClient::PeerProperties::PeerProperties(
408 dbus::ObjectProxy* object_proxy,
409 const PropertyChangedCallback& callback)
410 : dbus::PropertySet{object_proxy, peerd::kPeerInterface, callback} {
411 RegisterProperty("UUID", &uuid_);
412 RegisterProperty("LastSeen", &last_seen_);
413 }
414
415 PeerDaemonManagerClient::PeerProperties::~PeerProperties() {
416 }
417
418 PeerDaemonManagerClient::PeerDaemonManagerClient() {
419 }
420
421 PeerDaemonManagerClient::~PeerDaemonManagerClient() {
422 }
423
424 // static
425 PeerDaemonManagerClient* PeerDaemonManagerClient::Create() {
426 return new PeerDaemonManagerClientImpl();
427 }
428
429 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/peer_daemon_manager_client.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698