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

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

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

Powered by Google App Engine
This is Rietveld 408576698