1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/dbus/leadership_daemon_manager_client.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/observer_list.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_manager.h"
#include "dbus/object_proxy.h"
#include "dbus/values_util.h"
namespace chromeos {
// TODO(benchan): Move these constants to system_api.
namespace leaderd {
const char kLeaderdServiceName[] = "org.chromium.leaderd";
const char kLeaderdObjectManagerServicePath[] = "/org/chromium/leaderd";
const char kLeaderdManagerPath[] = "/org/chromium/leaderd/Manager";
const char kManagerInterface[] = "org.chromium.leaderd.Manager";
const char kGroupInterface[] = "org.chromium.leaderd.Group";
const char kJoinGroupMethod[] = "JoinGroup";
const char kLeaveGroupMethod[] = "LeaveGroup";
const char kSetScoreMethod[] = "SetScore";
const char kPokeLeaderMethod[] = "PokeLeader";
const char kPingMethod[] = "Ping";
const char kLeaderUUID[] = "LeaderUUID";
const char kGroupMembers[] = "GroupMembers";
} // namespace leaderd
namespace {
// Since there is no property associated with Manager objects, an empty callback
// is used.
void DoNothing(const std::string& property_name) {
}
// The LeadershipDaemonManagerClient implementation used in production.
class LeadershipDaemonManagerClientImpl
: public LeadershipDaemonManagerClient,
public dbus::ObjectManager::Interface {
public:
LeadershipDaemonManagerClientImpl();
~LeadershipDaemonManagerClientImpl() override;
// LeadershipDaemonManagerClient overrides.
void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override;
void JoinGroup(const std::string& group,
const base::DictionaryValue& options,
const ObjectPathDBusMethodCallback& callback) override;
void LeaveGroup(const std::string& object_path,
const VoidDBusMethodCallback& callback) override;
void SetScore(const std::string& object_path,
int score,
const VoidDBusMethodCallback& callback) override;
void PokeLeader(const std::string& object_path,
const VoidDBusMethodCallback& callback) override;
void Ping(const StringDBusMethodCallback& callback) override;
const GroupProperties* GetGroupProperties(
const dbus::ObjectPath& object_path) override;
// DBusClient overrides.
void Init(dbus::Bus* bus) override;
// dbus::ObjectManager::Interface overrides.
dbus::PropertySet* CreateProperties(
dbus::ObjectProxy* object_proxy,
const dbus::ObjectPath& object_path,
const std::string& interface_name) override;
void ObjectAdded(const dbus::ObjectPath& object_path,
const std::string& interface_name) override;
void ObjectRemoved(const dbus::ObjectPath& object_path,
const std::string& interface_name) override;
private:
// Called by dbus::PropertySet when a property value is changed,
// either by result of a signal or response to a GetAll() or Get()
// call. Informs observers.
void OnGroupPropertyChanged(const dbus::ObjectPath& object_path,
const std::string& property_name);
void OnObjectPathDBusMethod(const ObjectPathDBusMethodCallback& callback,
dbus::Response* response);
void OnStringDBusMethod(const StringDBusMethodCallback& callback,
dbus::Response* response);
void OnVoidDBusMethod(const VoidDBusMethodCallback& callback,
dbus::Response* response);
// List of observers interested in event notifications from us.
ObserverList<Observer> observers_;
dbus::ObjectManager* object_manager_;
base::WeakPtrFactory<LeadershipDaemonManagerClientImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(LeadershipDaemonManagerClientImpl);
};
LeadershipDaemonManagerClientImpl::LeadershipDaemonManagerClientImpl()
: object_manager_(nullptr), weak_ptr_factory_(this) {
}
LeadershipDaemonManagerClientImpl::~LeadershipDaemonManagerClientImpl() {
if (object_manager_) {
object_manager_->UnregisterInterface(leaderd::kManagerInterface);
object_manager_->UnregisterInterface(leaderd::kGroupInterface);
}
}
void LeadershipDaemonManagerClientImpl::AddObserver(Observer* observer) {
DCHECK(observer);
observers_.AddObserver(observer);
}
void LeadershipDaemonManagerClientImpl::RemoveObserver(Observer* observer) {
DCHECK(observer);
observers_.RemoveObserver(observer);
}
void LeadershipDaemonManagerClientImpl::JoinGroup(
const std::string& group,
const base::DictionaryValue& options,
const ObjectPathDBusMethodCallback& callback) {
dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
dbus::ObjectPath(leaderd::kLeaderdManagerPath));
if (!object_proxy) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&LeadershipDaemonManagerClientImpl::OnObjectPathDBusMethod,
weak_ptr_factory_.GetWeakPtr(), callback, nullptr));
return;
}
dbus::MethodCall method_call(leaderd::kManagerInterface,
leaderd::kJoinGroupMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(group);
dbus::AppendValueData(&writer, options);
object_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&LeadershipDaemonManagerClientImpl::OnObjectPathDBusMethod,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void LeadershipDaemonManagerClientImpl::LeaveGroup(
const std::string& object_path,
const VoidDBusMethodCallback& callback) {
dbus::ObjectProxy* object_proxy =
object_manager_->GetObjectProxy(dbus::ObjectPath(object_path));
if (!object_proxy) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&LeadershipDaemonManagerClientImpl::OnVoidDBusMethod,
weak_ptr_factory_.GetWeakPtr(), callback, nullptr));
return;
}
dbus::MethodCall method_call(leaderd::kGroupInterface,
leaderd::kLeaveGroupMethod);
dbus::MessageWriter writer(&method_call);
object_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&LeadershipDaemonManagerClientImpl::OnVoidDBusMethod,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void LeadershipDaemonManagerClientImpl::SetScore(
const std::string& object_path,
int score,
const VoidDBusMethodCallback& callback) {
dbus::ObjectProxy* object_proxy =
object_manager_->GetObjectProxy(dbus::ObjectPath(object_path));
if (!object_proxy) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&LeadershipDaemonManagerClientImpl::OnVoidDBusMethod,
weak_ptr_factory_.GetWeakPtr(), callback, nullptr));
return;
}
dbus::MethodCall method_call(leaderd::kGroupInterface,
leaderd::kSetScoreMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendInt32(score);
object_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&LeadershipDaemonManagerClientImpl::OnVoidDBusMethod,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void LeadershipDaemonManagerClientImpl::PokeLeader(
const std::string& object_path,
const VoidDBusMethodCallback& callback) {
dbus::ObjectProxy* object_proxy =
object_manager_->GetObjectProxy(dbus::ObjectPath(object_path));
if (!object_proxy) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&LeadershipDaemonManagerClientImpl::OnVoidDBusMethod,
weak_ptr_factory_.GetWeakPtr(), callback, nullptr));
return;
}
dbus::MethodCall method_call(leaderd::kGroupInterface,
leaderd::kPokeLeaderMethod);
dbus::MessageWriter writer(&method_call);
object_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&LeadershipDaemonManagerClientImpl::OnVoidDBusMethod,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void LeadershipDaemonManagerClientImpl::Ping(
const StringDBusMethodCallback& callback) {
dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
dbus::ObjectPath(leaderd::kLeaderdManagerPath));
if (!object_proxy) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&LeadershipDaemonManagerClientImpl::OnStringDBusMethod,
weak_ptr_factory_.GetWeakPtr(), callback, nullptr));
return;
}
dbus::MethodCall method_call(leaderd::kManagerInterface,
leaderd::kPingMethod);
dbus::MessageWriter writer(&method_call);
object_proxy->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&LeadershipDaemonManagerClientImpl::OnStringDBusMethod,
weak_ptr_factory_.GetWeakPtr(), callback));
}
const LeadershipDaemonManagerClient::GroupProperties*
LeadershipDaemonManagerClientImpl::GetGroupProperties(
const dbus::ObjectPath& object_path) {
return static_cast<GroupProperties*>(
object_manager_->GetProperties(object_path, leaderd::kGroupInterface));
}
void LeadershipDaemonManagerClientImpl::Init(dbus::Bus* bus) {
object_manager_ = bus->GetObjectManager(
leaderd::kLeaderdServiceName,
dbus::ObjectPath(leaderd::kLeaderdObjectManagerServicePath));
object_manager_->RegisterInterface(leaderd::kManagerInterface, this);
object_manager_->RegisterInterface(leaderd::kGroupInterface, this);
}
dbus::PropertySet* LeadershipDaemonManagerClientImpl::CreateProperties(
dbus::ObjectProxy* object_proxy,
const dbus::ObjectPath& object_path,
const std::string& interface_name) {
dbus::PropertySet* properties = nullptr;
if (interface_name == leaderd::kManagerInterface) {
properties = new dbus::PropertySet(object_proxy, interface_name,
base::Bind(&DoNothing));
} else if (interface_name == leaderd::kGroupInterface) {
properties = new GroupProperties(
object_proxy, interface_name,
base::Bind(&LeadershipDaemonManagerClientImpl::OnGroupPropertyChanged,
weak_ptr_factory_.GetWeakPtr(), object_path));
} else {
NOTREACHED() << "Unhandled interface name " << interface_name;
}
return properties;
}
void LeadershipDaemonManagerClientImpl::ObjectAdded(
const dbus::ObjectPath& object_path,
const std::string& interface_name) {
if (interface_name == leaderd::kManagerInterface) {
FOR_EACH_OBSERVER(Observer, observers_, ManagerAdded());
} else if (interface_name == leaderd::kGroupInterface) {
FOR_EACH_OBSERVER(Observer, observers_, GroupAdded(object_path));
} else {
NOTREACHED() << "Unhandled interface name " << interface_name;
}
}
void LeadershipDaemonManagerClientImpl::ObjectRemoved(
const dbus::ObjectPath& object_path,
const std::string& interface_name) {
if (interface_name == leaderd::kManagerInterface) {
FOR_EACH_OBSERVER(Observer, observers_, ManagerRemoved());
} else if (interface_name == leaderd::kGroupInterface) {
FOR_EACH_OBSERVER(Observer, observers_, GroupRemoved(object_path));
} else {
NOTREACHED() << "Unhandled interface name " << interface_name;
}
}
void LeadershipDaemonManagerClientImpl::OnGroupPropertyChanged(
const dbus::ObjectPath& object_path,
const std::string& property_name) {
FOR_EACH_OBSERVER(Observer, observers_,
GroupPropertyChanged(object_path, property_name));
}
void LeadershipDaemonManagerClientImpl::OnObjectPathDBusMethod(
const ObjectPathDBusMethodCallback& callback,
dbus::Response* response) {
if (!response) {
callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath());
return;
}
dbus::MessageReader reader(response);
dbus::ObjectPath result;
if (!reader.PopObjectPath(&result)) {
callback.Run(DBUS_METHOD_CALL_FAILURE, result);
return;
}
callback.Run(DBUS_METHOD_CALL_SUCCESS, result);
}
void LeadershipDaemonManagerClientImpl::OnStringDBusMethod(
const StringDBusMethodCallback& callback,
dbus::Response* response) {
if (!response) {
callback.Run(DBUS_METHOD_CALL_FAILURE, std::string());
return;
}
dbus::MessageReader reader(response);
std::string result;
if (!reader.PopString(&result)) {
callback.Run(DBUS_METHOD_CALL_FAILURE, std::string());
return;
}
callback.Run(DBUS_METHOD_CALL_SUCCESS, result);
}
void LeadershipDaemonManagerClientImpl::OnVoidDBusMethod(
const VoidDBusMethodCallback& callback,
dbus::Response* response) {
callback.Run(response ? DBUS_METHOD_CALL_SUCCESS : DBUS_METHOD_CALL_FAILURE);
}
} // namespace
LeadershipDaemonManagerClient::GroupProperties::GroupProperties(
dbus::ObjectProxy* object_proxy,
const std::string& interface_name,
const PropertyChangedCallback& callback)
: dbus::PropertySet(object_proxy, interface_name, callback) {
RegisterProperty(leaderd::kLeaderUUID, &leader_uuid_);
RegisterProperty(leaderd::kGroupMembers, &group_members_);
}
LeadershipDaemonManagerClient::GroupProperties::~GroupProperties() {
}
LeadershipDaemonManagerClient::Observer::~Observer() {
}
void LeadershipDaemonManagerClient::Observer::ManagerAdded() {
}
void LeadershipDaemonManagerClient::Observer::ManagerRemoved() {
}
void LeadershipDaemonManagerClient::Observer::GroupAdded(
const dbus::ObjectPath& object_path) {
}
void LeadershipDaemonManagerClient::Observer::GroupRemoved(
const dbus::ObjectPath& object_path) {
}
void LeadershipDaemonManagerClient::Observer::GroupPropertyChanged(
const dbus::ObjectPath& object_path,
const std::string& property_name) {
}
LeadershipDaemonManagerClient::LeadershipDaemonManagerClient() {
}
LeadershipDaemonManagerClient::~LeadershipDaemonManagerClient() {
}
// static
LeadershipDaemonManagerClient* LeadershipDaemonManagerClient::Create() {
return new LeadershipDaemonManagerClientImpl();
}
} // namespace chromeos
|