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
3 // found in the LICENSE file.
5 #include "chromeos/dbus/shill_client_helper.h"
8 #include "base/callback_helpers.h"
9 #include "base/values.h"
10 #include "dbus/message.h"
11 #include "dbus/object_proxy.h"
12 #include "dbus/values_util.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
17 // Class to hold onto a reference to a ShillClientHelper. This calss
18 // is owned by callbacks and released once the callback completes.
19 // Note: Only success callbacks hold the reference. If an error callback is
20 // invoked instead, the success callback will still be destroyed and the
21 // RefHolder with it, once the callback chain completes.
22 class ShillClientHelper::RefHolder
{
24 explicit RefHolder(base::WeakPtr
<ShillClientHelper
> helper
)
34 base::WeakPtr
<ShillClientHelper
> helper_
;
39 const char kInvalidResponseErrorName
[] = ""; // No error name.
40 const char kInvalidResponseErrorMessage
[] = "Invalid response.";
42 // Note: here and below, |ref_holder| is unused in the function body. It only
43 // exists so that it will be destroyed (and the reference released) with the
44 // Callback object once completed.
45 void OnBooleanMethodWithErrorCallback(
46 ShillClientHelper::RefHolder
* ref_holder
,
47 const ShillClientHelper::BooleanCallback
& callback
,
48 const ShillClientHelper::ErrorCallback
& error_callback
,
49 dbus::Response
* response
) {
51 error_callback
.Run(kInvalidResponseErrorName
, kInvalidResponseErrorMessage
);
54 dbus::MessageReader
reader(response
);
56 if (!reader
.PopBool(&result
)) {
57 error_callback
.Run(kInvalidResponseErrorName
, kInvalidResponseErrorMessage
);
63 void OnStringMethodWithErrorCallback(
64 ShillClientHelper::RefHolder
* ref_holder
,
65 const ShillClientHelper::StringCallback
& callback
,
66 const ShillClientHelper::ErrorCallback
& error_callback
,
67 dbus::Response
* response
) {
69 error_callback
.Run(kInvalidResponseErrorName
, kInvalidResponseErrorMessage
);
72 dbus::MessageReader
reader(response
);
74 if (!reader
.PopString(&result
)) {
75 error_callback
.Run(kInvalidResponseErrorName
, kInvalidResponseErrorMessage
);
81 // Handles responses for methods without results.
82 void OnVoidMethod(ShillClientHelper::RefHolder
* ref_holder
,
83 const VoidDBusMethodCallback
& callback
,
84 dbus::Response
* response
) {
86 callback
.Run(DBUS_METHOD_CALL_FAILURE
);
89 callback
.Run(DBUS_METHOD_CALL_SUCCESS
);
92 // Handles responses for methods with ObjectPath results.
93 void OnObjectPathMethod(
94 ShillClientHelper::RefHolder
* ref_holder
,
95 const ObjectPathDBusMethodCallback
& callback
,
96 dbus::Response
* response
) {
98 callback
.Run(DBUS_METHOD_CALL_FAILURE
, dbus::ObjectPath());
101 dbus::MessageReader
reader(response
);
102 dbus::ObjectPath result
;
103 if (!reader
.PopObjectPath(&result
)) {
104 callback
.Run(DBUS_METHOD_CALL_FAILURE
, dbus::ObjectPath());
107 callback
.Run(DBUS_METHOD_CALL_SUCCESS
, result
);
110 // Handles responses for methods with ObjectPath results and no status.
111 void OnObjectPathMethodWithoutStatus(
112 ShillClientHelper::RefHolder
* ref_holder
,
113 const ObjectPathCallback
& callback
,
114 const ShillClientHelper::ErrorCallback
& error_callback
,
115 dbus::Response
* response
) {
117 error_callback
.Run(kInvalidResponseErrorName
, kInvalidResponseErrorMessage
);
120 dbus::MessageReader
reader(response
);
121 dbus::ObjectPath result
;
122 if (!reader
.PopObjectPath(&result
)) {
123 error_callback
.Run(kInvalidResponseErrorName
, kInvalidResponseErrorMessage
);
126 callback
.Run(result
);
129 // Handles responses for methods with DictionaryValue results.
130 void OnDictionaryValueMethod(
131 ShillClientHelper::RefHolder
* ref_holder
,
132 const ShillClientHelper::DictionaryValueCallback
& callback
,
133 dbus::Response
* response
) {
135 base::DictionaryValue result
;
136 callback
.Run(DBUS_METHOD_CALL_FAILURE
, result
);
139 dbus::MessageReader
reader(response
);
140 scoped_ptr
<base::Value
> value(dbus::PopDataAsValue(&reader
));
141 base::DictionaryValue
* result
= NULL
;
142 if (!value
.get() || !value
->GetAsDictionary(&result
)) {
143 base::DictionaryValue result
;
144 callback
.Run(DBUS_METHOD_CALL_FAILURE
, result
);
147 callback
.Run(DBUS_METHOD_CALL_SUCCESS
, *result
);
150 // Handles responses for methods without results.
151 void OnVoidMethodWithErrorCallback(
152 ShillClientHelper::RefHolder
* ref_holder
,
153 const base::Closure
& callback
,
154 dbus::Response
* response
) {
158 // Handles responses for methods with DictionaryValue results.
159 // Used by CallDictionaryValueMethodWithErrorCallback().
160 void OnDictionaryValueMethodWithErrorCallback(
161 ShillClientHelper::RefHolder
* ref_holder
,
162 const ShillClientHelper::DictionaryValueCallbackWithoutStatus
& callback
,
163 const ShillClientHelper::ErrorCallback
& error_callback
,
164 dbus::Response
* response
) {
165 dbus::MessageReader
reader(response
);
166 scoped_ptr
<base::Value
> value(dbus::PopDataAsValue(&reader
));
167 base::DictionaryValue
* result
= NULL
;
168 if (!value
.get() || !value
->GetAsDictionary(&result
)) {
169 error_callback
.Run(kInvalidResponseErrorName
, kInvalidResponseErrorMessage
);
172 callback
.Run(*result
);
175 // Handles responses for methods with ListValue results.
176 void OnListValueMethodWithErrorCallback(
177 ShillClientHelper::RefHolder
* ref_holder
,
178 const ShillClientHelper::ListValueCallback
& callback
,
179 const ShillClientHelper::ErrorCallback
& error_callback
,
180 dbus::Response
* response
) {
181 dbus::MessageReader
reader(response
);
182 scoped_ptr
<base::Value
> value(dbus::PopDataAsValue(&reader
));
183 base::ListValue
* result
= NULL
;
184 if (!value
.get() || !value
->GetAsList(&result
)) {
185 error_callback
.Run(kInvalidResponseErrorName
, kInvalidResponseErrorMessage
);
188 callback
.Run(*result
);
191 // Handles running appropriate error callbacks.
192 void OnError(const ShillClientHelper::ErrorCallback
& error_callback
,
193 dbus::ErrorResponse
* response
) {
194 std::string error_name
;
195 std::string error_message
;
197 // Error message may contain the error message as string.
198 dbus::MessageReader
reader(response
);
199 error_name
= response
->GetErrorName();
200 reader
.PopString(&error_message
);
202 error_callback
.Run(error_name
, error_message
);
207 ShillClientHelper::ShillClientHelper(dbus::ObjectProxy
* proxy
)
210 weak_ptr_factory_(this) {
213 ShillClientHelper::~ShillClientHelper() {
214 LOG_IF(ERROR
, observer_list_
.might_have_observers())
215 << "ShillClientHelper destroyed with active observers";
218 void ShillClientHelper::SetReleasedCallback(ReleasedCallback callback
) {
219 CHECK(released_callback_
.is_null());
220 released_callback_
= callback
;
223 void ShillClientHelper::AddPropertyChangedObserver(
224 ShillPropertyChangedObserver
* observer
) {
225 if (observer_list_
.HasObserver(observer
))
228 // Excecute all the pending MonitorPropertyChanged calls.
229 for (size_t i
= 0; i
< interfaces_to_be_monitored_
.size(); ++i
) {
230 MonitorPropertyChangedInternal(interfaces_to_be_monitored_
[i
]);
232 interfaces_to_be_monitored_
.clear();
234 observer_list_
.AddObserver(observer
);
237 void ShillClientHelper::RemovePropertyChangedObserver(
238 ShillPropertyChangedObserver
* observer
) {
239 if (!observer_list_
.HasObserver(observer
))
241 observer_list_
.RemoveObserver(observer
);
245 void ShillClientHelper::MonitorPropertyChanged(
246 const std::string
& interface_name
) {
247 if (observer_list_
.might_have_observers()) {
248 // Effectively monitor the PropertyChanged now.
249 MonitorPropertyChangedInternal(interface_name
);
251 // Delay the ConnectToSignal until an observer is added.
252 interfaces_to_be_monitored_
.push_back(interface_name
);
256 void ShillClientHelper::MonitorPropertyChangedInternal(
257 const std::string
& interface_name
) {
258 // We are not using dbus::PropertySet to monitor PropertyChanged signal
259 // because the interface is not "org.freedesktop.DBus.Properties".
260 proxy_
->ConnectToSignal(interface_name
,
261 shill::kMonitorPropertyChanged
,
262 base::Bind(&ShillClientHelper::OnPropertyChanged
,
263 weak_ptr_factory_
.GetWeakPtr()),
264 base::Bind(&ShillClientHelper::OnSignalConnected
,
265 weak_ptr_factory_
.GetWeakPtr()));
268 void ShillClientHelper::CallVoidMethod(
269 dbus::MethodCall
* method_call
,
270 const VoidDBusMethodCallback
& callback
) {
271 DCHECK(!callback
.is_null());
273 method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
274 base::Bind(&OnVoidMethod
,
275 base::Owned(new RefHolder(weak_ptr_factory_
.GetWeakPtr())),
279 void ShillClientHelper::CallObjectPathMethod(
280 dbus::MethodCall
* method_call
,
281 const ObjectPathDBusMethodCallback
& callback
) {
282 DCHECK(!callback
.is_null());
284 method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
285 base::Bind(&OnObjectPathMethod
,
286 base::Owned(new RefHolder(weak_ptr_factory_
.GetWeakPtr())),
290 void ShillClientHelper::CallObjectPathMethodWithErrorCallback(
291 dbus::MethodCall
* method_call
,
292 const ObjectPathCallback
& callback
,
293 const ErrorCallback
& error_callback
) {
294 DCHECK(!callback
.is_null());
295 DCHECK(!error_callback
.is_null());
296 proxy_
->CallMethodWithErrorCallback(
298 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
299 base::Bind(&OnObjectPathMethodWithoutStatus
,
300 base::Owned(new RefHolder(weak_ptr_factory_
.GetWeakPtr())),
307 void ShillClientHelper::CallDictionaryValueMethod(
308 dbus::MethodCall
* method_call
,
309 const DictionaryValueCallback
& callback
) {
310 DCHECK(!callback
.is_null());
312 method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
313 base::Bind(&OnDictionaryValueMethod
,
314 base::Owned(new RefHolder(weak_ptr_factory_
.GetWeakPtr())),
318 void ShillClientHelper::CallVoidMethodWithErrorCallback(
319 dbus::MethodCall
* method_call
,
320 const base::Closure
& callback
,
321 const ErrorCallback
& error_callback
) {
322 DCHECK(!callback
.is_null());
323 DCHECK(!error_callback
.is_null());
324 proxy_
->CallMethodWithErrorCallback(
325 method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
326 base::Bind(&OnVoidMethodWithErrorCallback
,
327 base::Owned(new RefHolder(weak_ptr_factory_
.GetWeakPtr())),
333 void ShillClientHelper::CallBooleanMethodWithErrorCallback(
334 dbus::MethodCall
* method_call
,
335 const BooleanCallback
& callback
,
336 const ErrorCallback
& error_callback
) {
337 DCHECK(!callback
.is_null());
338 DCHECK(!error_callback
.is_null());
339 proxy_
->CallMethodWithErrorCallback(
340 method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
341 base::Bind(&OnBooleanMethodWithErrorCallback
,
342 base::Owned(new RefHolder(weak_ptr_factory_
.GetWeakPtr())),
349 void ShillClientHelper::CallStringMethodWithErrorCallback(
350 dbus::MethodCall
* method_call
,
351 const StringCallback
& callback
,
352 const ErrorCallback
& error_callback
) {
353 DCHECK(!callback
.is_null());
354 DCHECK(!error_callback
.is_null());
355 proxy_
->CallMethodWithErrorCallback(
356 method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
357 base::Bind(&OnStringMethodWithErrorCallback
,
358 base::Owned(new RefHolder(weak_ptr_factory_
.GetWeakPtr())),
365 void ShillClientHelper::CallDictionaryValueMethodWithErrorCallback(
366 dbus::MethodCall
* method_call
,
367 const DictionaryValueCallbackWithoutStatus
& callback
,
368 const ErrorCallback
& error_callback
) {
369 DCHECK(!callback
.is_null());
370 DCHECK(!error_callback
.is_null());
371 proxy_
->CallMethodWithErrorCallback(
372 method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
373 base::Bind(&OnDictionaryValueMethodWithErrorCallback
,
374 base::Owned(new RefHolder(weak_ptr_factory_
.GetWeakPtr())),
381 void ShillClientHelper::CallListValueMethodWithErrorCallback(
382 dbus::MethodCall
* method_call
,
383 const ListValueCallback
& callback
,
384 const ErrorCallback
& error_callback
) {
385 DCHECK(!callback
.is_null());
386 DCHECK(!error_callback
.is_null());
387 proxy_
->CallMethodWithErrorCallback(
388 method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
389 base::Bind(&OnListValueMethodWithErrorCallback
,
390 base::Owned(new RefHolder(weak_ptr_factory_
.GetWeakPtr())),
398 void ShillClientHelper::AppendValueDataAsVariant(dbus::MessageWriter
* writer
,
399 const base::Value
& value
) {
400 // Support basic types and string-to-string dictionary.
401 switch (value
.GetType()) {
402 case base::Value::TYPE_DICTIONARY
: {
403 const base::DictionaryValue
* dictionary
= NULL
;
404 value
.GetAsDictionary(&dictionary
);
405 dbus::MessageWriter
variant_writer(NULL
);
406 writer
->OpenVariant("a{ss}", &variant_writer
);
407 dbus::MessageWriter
array_writer(NULL
);
408 variant_writer
.OpenArray("{ss}", &array_writer
);
409 for (base::DictionaryValue::Iterator
it(*dictionary
);
412 dbus::MessageWriter
entry_writer(NULL
);
413 array_writer
.OpenDictEntry(&entry_writer
);
414 entry_writer
.AppendString(it
.key());
415 const base::Value
& value
= it
.value();
416 std::string value_string
;
417 DLOG_IF(ERROR
, value
.GetType() != base::Value::TYPE_STRING
)
418 << "Unexpected type " << value
.GetType();
419 value
.GetAsString(&value_string
);
420 entry_writer
.AppendString(value_string
);
421 array_writer
.CloseContainer(&entry_writer
);
423 variant_writer
.CloseContainer(&array_writer
);
424 writer
->CloseContainer(&variant_writer
);
427 case base::Value::TYPE_LIST
: {
428 const base::ListValue
* list
= NULL
;
429 value
.GetAsList(&list
);
430 dbus::MessageWriter
variant_writer(NULL
);
431 writer
->OpenVariant("as", &variant_writer
);
432 dbus::MessageWriter
array_writer(NULL
);
433 variant_writer
.OpenArray("s", &array_writer
);
434 for (base::ListValue::const_iterator it
= list
->begin();
435 it
!= list
->end(); ++it
) {
436 const base::Value
& value
= **it
;
437 LOG_IF(ERROR
, value
.GetType() != base::Value::TYPE_STRING
)
438 << "Unexpected type " << value
.GetType();
439 std::string value_string
;
440 value
.GetAsString(&value_string
);
441 array_writer
.AppendString(value_string
);
443 variant_writer
.CloseContainer(&array_writer
);
444 writer
->CloseContainer(&variant_writer
);
447 case base::Value::TYPE_BOOLEAN
:
448 case base::Value::TYPE_INTEGER
:
449 case base::Value::TYPE_DOUBLE
:
450 case base::Value::TYPE_STRING
:
451 dbus::AppendBasicTypeValueDataAsVariant(writer
, value
);
454 DLOG(ERROR
) << "Unexpected type " << value
.GetType();
460 void ShillClientHelper::AppendServicePropertiesDictionary(
461 dbus::MessageWriter
* writer
,
462 const base::DictionaryValue
& dictionary
) {
463 dbus::MessageWriter
array_writer(NULL
);
464 writer
->OpenArray("{sv}", &array_writer
);
465 for (base::DictionaryValue::Iterator
it(dictionary
);
468 dbus::MessageWriter
entry_writer(NULL
);
469 array_writer
.OpenDictEntry(&entry_writer
);
470 entry_writer
.AppendString(it
.key());
471 ShillClientHelper::AppendValueDataAsVariant(&entry_writer
, it
.value());
472 array_writer
.CloseContainer(&entry_writer
);
474 writer
->CloseContainer(&array_writer
);
477 void ShillClientHelper::AddRef() {
481 void ShillClientHelper::Release() {
483 if (active_refs_
== 0 && !released_callback_
.is_null())
484 base::ResetAndReturn(&released_callback_
).Run(this); // May delete this
487 void ShillClientHelper::OnSignalConnected(const std::string
& interface
,
488 const std::string
& signal
,
490 LOG_IF(ERROR
, !success
) << "Connect to " << interface
<< " " << signal
494 void ShillClientHelper::OnPropertyChanged(dbus::Signal
* signal
) {
495 if (!observer_list_
.might_have_observers())
498 dbus::MessageReader
reader(signal
);
500 if (!reader
.PopString(&name
))
502 scoped_ptr
<base::Value
> value(dbus::PopDataAsValue(&reader
));
506 FOR_EACH_OBSERVER(ShillPropertyChangedObserver
, observer_list_
,
507 OnPropertyChanged(name
, *value
));
510 } // namespace chromeos