OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/leadership_daemon_manager_client.h" | 5 #include "chromeos/dbus/leadership_daemon_manager_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 public dbus::ObjectManager::Interface { | 46 public dbus::ObjectManager::Interface { |
47 public: | 47 public: |
48 LeadershipDaemonManagerClientImpl(); | 48 LeadershipDaemonManagerClientImpl(); |
49 ~LeadershipDaemonManagerClientImpl() override; | 49 ~LeadershipDaemonManagerClientImpl() override; |
50 | 50 |
51 // LeadershipDaemonManagerClient overrides. | 51 // LeadershipDaemonManagerClient overrides. |
52 void AddObserver(Observer* observer) override; | 52 void AddObserver(Observer* observer) override; |
53 void RemoveObserver(Observer* observer) override; | 53 void RemoveObserver(Observer* observer) override; |
54 void JoinGroup(const std::string& group, | 54 void JoinGroup(const std::string& group, |
55 const base::DictionaryValue& options, | 55 const base::DictionaryValue& options, |
56 const StringDBusMethodCallback& callback) override; | 56 const ObjectPathDBusMethodCallback& callback) override; |
57 void LeaveGroup(const std::string& object_path, | 57 void LeaveGroup(const std::string& object_path, |
58 const VoidDBusMethodCallback& callback) override; | 58 const VoidDBusMethodCallback& callback) override; |
59 void SetScore(const std::string& object_path, | 59 void SetScore(const std::string& object_path, |
60 int score, | 60 int score, |
61 const VoidDBusMethodCallback& callback) override; | 61 const VoidDBusMethodCallback& callback) override; |
62 void PokeLeader(const std::string& object_path, | 62 void PokeLeader(const std::string& object_path, |
63 const VoidDBusMethodCallback& callback) override; | 63 const VoidDBusMethodCallback& callback) override; |
64 void Ping(const StringDBusMethodCallback& callback) override; | 64 void Ping(const StringDBusMethodCallback& callback) override; |
65 const GroupProperties* GetGroupProperties( | 65 const GroupProperties* GetGroupProperties( |
66 const dbus::ObjectPath& object_path) override; | 66 const dbus::ObjectPath& object_path) override; |
(...skipping 11 matching lines...) Expand all Loading... |
78 void ObjectRemoved(const dbus::ObjectPath& object_path, | 78 void ObjectRemoved(const dbus::ObjectPath& object_path, |
79 const std::string& interface_name) override; | 79 const std::string& interface_name) override; |
80 | 80 |
81 private: | 81 private: |
82 // Called by dbus::PropertySet when a property value is changed, | 82 // Called by dbus::PropertySet when a property value is changed, |
83 // either by result of a signal or response to a GetAll() or Get() | 83 // either by result of a signal or response to a GetAll() or Get() |
84 // call. Informs observers. | 84 // call. Informs observers. |
85 void OnGroupPropertyChanged(const dbus::ObjectPath& object_path, | 85 void OnGroupPropertyChanged(const dbus::ObjectPath& object_path, |
86 const std::string& property_name); | 86 const std::string& property_name); |
87 | 87 |
| 88 void OnObjectPathDBusMethod(const ObjectPathDBusMethodCallback& callback, |
| 89 dbus::Response* response); |
88 void OnStringDBusMethod(const StringDBusMethodCallback& callback, | 90 void OnStringDBusMethod(const StringDBusMethodCallback& callback, |
89 dbus::Response* response); | 91 dbus::Response* response); |
90 void OnVoidDBusMethod(const VoidDBusMethodCallback& callback, | 92 void OnVoidDBusMethod(const VoidDBusMethodCallback& callback, |
91 dbus::Response* response); | 93 dbus::Response* response); |
92 | 94 |
93 // List of observers interested in event notifications from us. | 95 // List of observers interested in event notifications from us. |
94 ObserverList<Observer> observers_; | 96 ObserverList<Observer> observers_; |
95 dbus::ObjectManager* object_manager_; | 97 dbus::ObjectManager* object_manager_; |
96 base::WeakPtrFactory<LeadershipDaemonManagerClientImpl> weak_ptr_factory_; | 98 base::WeakPtrFactory<LeadershipDaemonManagerClientImpl> weak_ptr_factory_; |
97 | 99 |
(...skipping 17 matching lines...) Expand all Loading... |
115 } | 117 } |
116 | 118 |
117 void LeadershipDaemonManagerClientImpl::RemoveObserver(Observer* observer) { | 119 void LeadershipDaemonManagerClientImpl::RemoveObserver(Observer* observer) { |
118 DCHECK(observer); | 120 DCHECK(observer); |
119 observers_.RemoveObserver(observer); | 121 observers_.RemoveObserver(observer); |
120 } | 122 } |
121 | 123 |
122 void LeadershipDaemonManagerClientImpl::JoinGroup( | 124 void LeadershipDaemonManagerClientImpl::JoinGroup( |
123 const std::string& group, | 125 const std::string& group, |
124 const base::DictionaryValue& options, | 126 const base::DictionaryValue& options, |
125 const StringDBusMethodCallback& callback) { | 127 const ObjectPathDBusMethodCallback& callback) { |
126 dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy( | 128 dbus::ObjectProxy* object_proxy = object_manager_->GetObjectProxy( |
127 dbus::ObjectPath(leaderd::kLeaderdManagerPath)); | 129 dbus::ObjectPath(leaderd::kLeaderdManagerPath)); |
128 if (!object_proxy) { | 130 if (!object_proxy) { |
129 base::MessageLoop::current()->PostTask( | 131 base::MessageLoop::current()->PostTask( |
130 FROM_HERE, | 132 FROM_HERE, |
131 base::Bind(&LeadershipDaemonManagerClientImpl::OnStringDBusMethod, | 133 base::Bind(&LeadershipDaemonManagerClientImpl::OnObjectPathDBusMethod, |
132 weak_ptr_factory_.GetWeakPtr(), callback, nullptr)); | 134 weak_ptr_factory_.GetWeakPtr(), callback, nullptr)); |
133 return; | 135 return; |
134 } | 136 } |
135 | 137 |
136 dbus::MethodCall method_call(leaderd::kManagerInterface, | 138 dbus::MethodCall method_call(leaderd::kManagerInterface, |
137 leaderd::kJoinGroupMethod); | 139 leaderd::kJoinGroupMethod); |
138 dbus::MessageWriter writer(&method_call); | 140 dbus::MessageWriter writer(&method_call); |
139 writer.AppendString(group); | 141 writer.AppendString(group); |
140 dbus::AppendValueData(&writer, options); | 142 dbus::AppendValueData(&writer, options); |
141 object_proxy->CallMethod( | 143 object_proxy->CallMethod( |
142 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 144 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
143 base::Bind(&LeadershipDaemonManagerClientImpl::OnStringDBusMethod, | 145 base::Bind(&LeadershipDaemonManagerClientImpl::OnObjectPathDBusMethod, |
144 weak_ptr_factory_.GetWeakPtr(), callback)); | 146 weak_ptr_factory_.GetWeakPtr(), callback)); |
145 } | 147 } |
146 | 148 |
147 void LeadershipDaemonManagerClientImpl::LeaveGroup( | 149 void LeadershipDaemonManagerClientImpl::LeaveGroup( |
148 const std::string& object_path, | 150 const std::string& object_path, |
149 const VoidDBusMethodCallback& callback) { | 151 const VoidDBusMethodCallback& callback) { |
150 dbus::ObjectProxy* object_proxy = | 152 dbus::ObjectProxy* object_proxy = |
151 object_manager_->GetObjectProxy(dbus::ObjectPath(object_path)); | 153 object_manager_->GetObjectProxy(dbus::ObjectPath(object_path)); |
152 if (!object_proxy) { | 154 if (!object_proxy) { |
153 base::MessageLoop::current()->PostTask( | 155 base::MessageLoop::current()->PostTask( |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 } | 293 } |
292 } | 294 } |
293 | 295 |
294 void LeadershipDaemonManagerClientImpl::OnGroupPropertyChanged( | 296 void LeadershipDaemonManagerClientImpl::OnGroupPropertyChanged( |
295 const dbus::ObjectPath& object_path, | 297 const dbus::ObjectPath& object_path, |
296 const std::string& property_name) { | 298 const std::string& property_name) { |
297 FOR_EACH_OBSERVER(Observer, observers_, | 299 FOR_EACH_OBSERVER(Observer, observers_, |
298 GroupPropertyChanged(object_path, property_name)); | 300 GroupPropertyChanged(object_path, property_name)); |
299 } | 301 } |
300 | 302 |
| 303 void LeadershipDaemonManagerClientImpl::OnObjectPathDBusMethod( |
| 304 const ObjectPathDBusMethodCallback& callback, |
| 305 dbus::Response* response) { |
| 306 if (!response) { |
| 307 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); |
| 308 return; |
| 309 } |
| 310 |
| 311 dbus::MessageReader reader(response); |
| 312 dbus::ObjectPath result; |
| 313 if (!reader.PopObjectPath(&result)) { |
| 314 callback.Run(DBUS_METHOD_CALL_FAILURE, result); |
| 315 return; |
| 316 } |
| 317 |
| 318 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); |
| 319 } |
| 320 |
301 void LeadershipDaemonManagerClientImpl::OnStringDBusMethod( | 321 void LeadershipDaemonManagerClientImpl::OnStringDBusMethod( |
302 const StringDBusMethodCallback& callback, | 322 const StringDBusMethodCallback& callback, |
303 dbus::Response* response) { | 323 dbus::Response* response) { |
304 if (!response) { | 324 if (!response) { |
305 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string()); | 325 callback.Run(DBUS_METHOD_CALL_FAILURE, std::string()); |
306 return; | 326 return; |
307 } | 327 } |
308 | 328 |
309 dbus::MessageReader reader(response); | 329 dbus::MessageReader reader(response); |
310 std::string result; | 330 std::string result; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 | 383 |
364 LeadershipDaemonManagerClient::~LeadershipDaemonManagerClient() { | 384 LeadershipDaemonManagerClient::~LeadershipDaemonManagerClient() { |
365 } | 385 } |
366 | 386 |
367 // static | 387 // static |
368 LeadershipDaemonManagerClient* LeadershipDaemonManagerClient::Create() { | 388 LeadershipDaemonManagerClient* LeadershipDaemonManagerClient::Create() { |
369 return new LeadershipDaemonManagerClientImpl(); | 389 return new LeadershipDaemonManagerClientImpl(); |
370 } | 390 } |
371 | 391 |
372 } // namespace chromeos | 392 } // namespace chromeos |
OLD | NEW |