OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/shill_client_helper.h" | 5 #include "chromeos/dbus/shill_client_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "dbus/message.h" | 9 #include "dbus/message.h" |
10 #include "dbus/object_proxy.h" | 10 #include "dbus/object_proxy.h" |
11 #include "dbus/values_util.h" | 11 #include "dbus/values_util.h" |
12 #include "third_party/cros_system_api/dbus/service_constants.h" | 12 #include "third_party/cros_system_api/dbus/service_constants.h" |
13 | 13 |
14 namespace chromeos { | 14 namespace chromeos { |
15 | 15 |
| 16 namespace { |
| 17 |
| 18 const char kInvalidResponseErrorName[] = ""; // No error name. |
| 19 const char kInvalidResponseErrorMessage[] = "Invalid response."; |
| 20 |
| 21 } // namespace |
| 22 |
16 ShillClientHelper::ShillClientHelper(dbus::Bus* bus, | 23 ShillClientHelper::ShillClientHelper(dbus::Bus* bus, |
17 dbus::ObjectProxy* proxy) | 24 dbus::ObjectProxy* proxy) |
18 : blocking_method_caller_(bus, proxy), | 25 : blocking_method_caller_(bus, proxy), |
19 proxy_(proxy), | 26 proxy_(proxy), |
20 weak_ptr_factory_(this) { | 27 weak_ptr_factory_(this) { |
21 } | 28 } |
22 | 29 |
23 ShillClientHelper::~ShillClientHelper() { | 30 ShillClientHelper::~ShillClientHelper() { |
24 } | 31 } |
25 | 32 |
(...skipping 30 matching lines...) Expand all Loading... |
56 | 63 |
57 void ShillClientHelper::CallObjectPathMethod( | 64 void ShillClientHelper::CallObjectPathMethod( |
58 dbus::MethodCall* method_call, | 65 dbus::MethodCall* method_call, |
59 const ObjectPathDBusMethodCallback& callback) { | 66 const ObjectPathDBusMethodCallback& callback) { |
60 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 67 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
61 base::Bind(&ShillClientHelper::OnObjectPathMethod, | 68 base::Bind(&ShillClientHelper::OnObjectPathMethod, |
62 weak_ptr_factory_.GetWeakPtr(), | 69 weak_ptr_factory_.GetWeakPtr(), |
63 callback)); | 70 callback)); |
64 } | 71 } |
65 | 72 |
| 73 void ShillClientHelper::CallObjectPathMethodWithErrorCallback( |
| 74 dbus::MethodCall* method_call, |
| 75 const ObjectPathCallback& callback, |
| 76 const ErrorCallback& error_callback) { |
| 77 proxy_->CallMethodWithErrorCallback( |
| 78 method_call, |
| 79 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 80 base::Bind(&ShillClientHelper::OnObjectPathMethodWithoutStatus, |
| 81 weak_ptr_factory_.GetWeakPtr(), |
| 82 callback, |
| 83 error_callback), |
| 84 base::Bind(&ShillClientHelper::OnError, |
| 85 weak_ptr_factory_.GetWeakPtr(), |
| 86 error_callback)); |
| 87 } |
| 88 |
66 void ShillClientHelper::CallDictionaryValueMethod( | 89 void ShillClientHelper::CallDictionaryValueMethod( |
67 dbus::MethodCall* method_call, | 90 dbus::MethodCall* method_call, |
68 const DictionaryValueCallback& callback) { | 91 const DictionaryValueCallback& callback) { |
69 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 92 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
70 base::Bind(&ShillClientHelper::OnDictionaryValueMethod, | 93 base::Bind(&ShillClientHelper::OnDictionaryValueMethod, |
71 weak_ptr_factory_.GetWeakPtr(), | 94 weak_ptr_factory_.GetWeakPtr(), |
72 callback)); | 95 callback)); |
73 } | 96 } |
74 | 97 |
75 void ShillClientHelper::CallVoidMethodWithErrorCallback( | 98 void ShillClientHelper::CallVoidMethodWithErrorCallback( |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 } | 249 } |
227 dbus::MessageReader reader(response); | 250 dbus::MessageReader reader(response); |
228 dbus::ObjectPath result; | 251 dbus::ObjectPath result; |
229 if (!reader.PopObjectPath(&result)) { | 252 if (!reader.PopObjectPath(&result)) { |
230 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); | 253 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); |
231 return; | 254 return; |
232 } | 255 } |
233 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); | 256 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); |
234 } | 257 } |
235 | 258 |
| 259 void ShillClientHelper::OnObjectPathMethodWithoutStatus( |
| 260 const ObjectPathCallback& callback, |
| 261 const ErrorCallback& error_callback, |
| 262 dbus::Response* response) { |
| 263 if (!response) { |
| 264 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
| 265 return; |
| 266 } |
| 267 dbus::MessageReader reader(response); |
| 268 dbus::ObjectPath result; |
| 269 if (!reader.PopObjectPath(&result)) { |
| 270 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
| 271 return; |
| 272 } |
| 273 callback.Run(result); |
| 274 } |
| 275 |
236 void ShillClientHelper::OnDictionaryValueMethod( | 276 void ShillClientHelper::OnDictionaryValueMethod( |
237 const DictionaryValueCallback& callback, | 277 const DictionaryValueCallback& callback, |
238 dbus::Response* response) { | 278 dbus::Response* response) { |
239 if (!response) { | 279 if (!response) { |
240 base::DictionaryValue result; | 280 base::DictionaryValue result; |
241 callback.Run(DBUS_METHOD_CALL_FAILURE, result); | 281 callback.Run(DBUS_METHOD_CALL_FAILURE, result); |
242 return; | 282 return; |
243 } | 283 } |
244 dbus::MessageReader reader(response); | 284 dbus::MessageReader reader(response); |
245 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 285 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
(...skipping 13 matching lines...) Expand all Loading... |
259 } | 299 } |
260 | 300 |
261 void ShillClientHelper::OnDictionaryValueMethodWithErrorCallback( | 301 void ShillClientHelper::OnDictionaryValueMethodWithErrorCallback( |
262 const DictionaryValueCallbackWithoutStatus& callback, | 302 const DictionaryValueCallbackWithoutStatus& callback, |
263 const ErrorCallback& error_callback, | 303 const ErrorCallback& error_callback, |
264 dbus::Response* response) { | 304 dbus::Response* response) { |
265 dbus::MessageReader reader(response); | 305 dbus::MessageReader reader(response); |
266 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 306 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
267 base::DictionaryValue* result = NULL; | 307 base::DictionaryValue* result = NULL; |
268 if (!value.get() || !value->GetAsDictionary(&result)) { | 308 if (!value.get() || !value->GetAsDictionary(&result)) { |
269 const std::string error_name; // No error name. | 309 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
270 const std::string error_message = "Invalid response."; | |
271 error_callback.Run(error_name, error_message); | |
272 return; | 310 return; |
273 } | 311 } |
274 callback.Run(*result); | 312 callback.Run(*result); |
275 } | 313 } |
276 | 314 |
277 void ShillClientHelper::OnError(const ErrorCallback& error_callback, | 315 void ShillClientHelper::OnError(const ErrorCallback& error_callback, |
278 dbus::ErrorResponse* response) { | 316 dbus::ErrorResponse* response) { |
279 std::string error_name; | 317 std::string error_name; |
280 std::string error_message; | 318 std::string error_message; |
281 if (response) { | 319 if (response) { |
282 // Error message may contain the error message as string. | 320 // Error message may contain the error message as string. |
283 dbus::MessageReader reader(response); | 321 dbus::MessageReader reader(response); |
284 error_name = response->GetErrorName(); | 322 error_name = response->GetErrorName(); |
285 reader.PopString(&error_message); | 323 reader.PopString(&error_message); |
286 } | 324 } |
287 error_callback.Run(error_name, error_message); | 325 error_callback.Run(error_name, error_message); |
288 } | 326 } |
289 | 327 |
290 } // namespace chromeos | 328 } // namespace chromeos |
OLD | NEW |