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 #ifndef CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_
6 #define CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/values.h"
16 #include "chromeos/dbus/dbus_method_call_status.h"
17 #include "chromeos/dbus/shill_property_changed_observer.h"
22 class DictionaryValue
;
41 class BlockingMethodCaller
;
43 // A class to help implement Shill clients.
44 class ShillClientHelper
{
46 // A callback to handle PropertyChanged signals.
47 typedef base::Callback
<void(const std::string
& name
,
48 const base::Value
& value
)> PropertyChangedHandler
;
50 // A callback to handle responses for methods with DictionaryValue results.
51 typedef base::Callback
<void(
52 DBusMethodCallStatus call_status
,
53 const base::DictionaryValue
& result
)> DictionaryValueCallback
;
55 // A callback to handle responses for methods with DictionaryValue results.
56 // This is used by CallDictionaryValueMethodWithErrorCallback.
57 typedef base::Callback
<void(const base::DictionaryValue
& result
)>
58 DictionaryValueCallbackWithoutStatus
;
60 // A callback to handle responses of methods returning a ListValue.
61 typedef base::Callback
<void(const base::ListValue
& result
)> ListValueCallback
;
63 // A callback to handle errors for method call.
64 typedef base::Callback
<void(const std::string
& error_name
,
65 const std::string
& error_message
)> ErrorCallback
;
67 // A callback that handles responses for methods with string results.
68 typedef base::Callback
<void(const std::string
& result
)> StringCallback
;
70 // A callback that handles responses for methods with boolean results.
71 typedef base::Callback
<void(bool result
)> BooleanCallback
;
74 ShillClientHelper(dbus::Bus
* bus
, dbus::ObjectProxy
* proxy
);
76 virtual ~ShillClientHelper();
78 // Adds an |observer| of the PropertyChanged signal.
79 void AddPropertyChangedObserver(ShillPropertyChangedObserver
* observer
);
81 // Removes an |observer| of the PropertyChanged signal.
82 void RemovePropertyChangedObserver(ShillPropertyChangedObserver
* observer
);
84 // Starts monitoring PropertyChanged signal. If there aren't observers for the
85 // PropertyChanged signal, the actual monitoring will be delayed until the
86 // first observer is added.
87 void MonitorPropertyChanged(const std::string
& interface_name
);
89 // Calls a method without results.
90 void CallVoidMethod(dbus::MethodCall
* method_call
,
91 const VoidDBusMethodCallback
& callback
);
93 // Calls a method with an object path result.
94 void CallObjectPathMethod(dbus::MethodCall
* method_call
,
95 const ObjectPathDBusMethodCallback
& callback
);
97 // Calls a method with an object path result where there is an error callback.
98 void CallObjectPathMethodWithErrorCallback(
99 dbus::MethodCall
* method_call
,
100 const ObjectPathCallback
& callback
,
101 const ErrorCallback
& error_callback
);
103 // Calls a method with a dictionary value result.
104 void CallDictionaryValueMethod(dbus::MethodCall
* method_call
,
105 const DictionaryValueCallback
& callback
);
107 // Calls a method without results with error callback.
108 void CallVoidMethodWithErrorCallback(dbus::MethodCall
* method_call
,
109 const base::Closure
& callback
,
110 const ErrorCallback
& error_callback
);
112 // Calls a method with a boolean result with error callback.
113 void CallBooleanMethodWithErrorCallback(
114 dbus::MethodCall
* method_call
,
115 const BooleanCallback
& callback
,
116 const ErrorCallback
& error_callback
);
118 // Calls a method with a string result with error callback.
119 void CallStringMethodWithErrorCallback(dbus::MethodCall
* method_call
,
120 const StringCallback
& callback
,
121 const ErrorCallback
& error_callback
);
124 // Calls a method with a dictionary value result with error callback.
125 void CallDictionaryValueMethodWithErrorCallback(
126 dbus::MethodCall
* method_call
,
127 const DictionaryValueCallbackWithoutStatus
& callback
,
128 const ErrorCallback
& error_callback
);
130 // Calls a method with a boolean array result with error callback.
131 void CallListValueMethodWithErrorCallback(
132 dbus::MethodCall
* method_call
,
133 const ListValueCallback
& callback
,
134 const ErrorCallback
& error_callback
);
136 // DEPRECATED DO NOT USE: Calls a method without results.
137 bool CallVoidMethodAndBlock(dbus::MethodCall
* method_call
);
139 // DEPRECATED DO NOT USE: Calls a method with a dictionary value result.
140 // The caller is responsible to delete the result.
141 // This method returns NULL when method call fails.
142 base::DictionaryValue
* CallDictionaryValueMethodAndBlock(
143 dbus::MethodCall
* method_call
);
145 // Appends the value (basic types and string-to-string dictionary) to the
146 // writer as a variant.
147 static void AppendValueDataAsVariant(dbus::MessageWriter
* writer
,
148 const base::Value
& value
);
151 // Starts monitoring PropertyChanged signal.
152 void MonitorPropertyChangedInternal(const std::string
& interface_name
);
154 // Handles the result of signal connection setup.
155 void OnSignalConnected(const std::string
& interface
,
156 const std::string
& signal
,
159 // Handles PropertyChanged signal.
160 void OnPropertyChanged(dbus::Signal
* signal
);
162 // TODO(hashimoto): Remove this when we no longer need to make blocking calls.
163 scoped_ptr
<BlockingMethodCaller
> blocking_method_caller_
;
164 dbus::ObjectProxy
* proxy_
;
165 PropertyChangedHandler property_changed_handler_
;
166 ObserverList
<ShillPropertyChangedObserver
, true /* check_empty */>
168 std::vector
<std::string
> interfaces_to_be_monitored_
;
170 // Note: This should remain the last member so it'll be destroyed and
171 // invalidate its weak pointers before any other members are destroyed.
172 base::WeakPtrFactory
<ShillClientHelper
> weak_ptr_factory_
;
174 DISALLOW_COPY_AND_ASSIGN(ShillClientHelper
);
177 } // namespace chromeos
179 #endif // CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_