[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / chromeos / dbus / shill_client_helper.h
bloba049b7dcef94383832f7eddb1bcccfe2705503ee
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_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/values.h"
15 #include "chromeos/dbus/blocking_method_caller.h"
16 #include "chromeos/dbus/dbus_method_call_status.h"
17 #include "chromeos/dbus/shill_property_changed_observer.h"
19 namespace base {
21 class Value;
22 class DictionaryValue;
24 } // namespace base
26 namespace dbus {
28 class Bus;
29 class ErrorResponse;
30 class MessageWriter;
31 class MethodCall;
32 class ObjectPath;
33 class ObjectProxy;
34 class Response;
35 class Signal;
37 } // namespace dbus
39 namespace chromeos {
41 // A class to help implement Shill clients.
42 class ShillClientHelper {
43 public:
44 // A callback to handle PropertyChanged signals.
45 typedef base::Callback<void(const std::string& name,
46 const base::Value& value)> PropertyChangedHandler;
48 // A callback to handle responses for methods with DictionaryValue results.
49 typedef base::Callback<void(
50 DBusMethodCallStatus call_status,
51 const base::DictionaryValue& result)> DictionaryValueCallback;
53 // A callback to handle responses for methods with DictionaryValue results.
54 // This is used by CallDictionaryValueMethodWithErrorCallback.
55 typedef base::Callback<void(const base::DictionaryValue& result)>
56 DictionaryValueCallbackWithoutStatus;
58 // A callback to handle responses of methods returning a ListValue.
59 typedef base::Callback<void(const base::ListValue& result)> ListValueCallback;
61 // A callback to handle errors for method call.
62 typedef base::Callback<void(const std::string& error_name,
63 const std::string& error_message)> ErrorCallback;
65 ShillClientHelper(dbus::Bus* bus, dbus::ObjectProxy* proxy);
67 virtual ~ShillClientHelper();
69 // Adds an |observer| of the PropertyChanged signal.
70 void AddPropertyChangedObserver(ShillPropertyChangedObserver* observer);
72 // Removes an |observer| of the PropertyChanged signal.
73 void RemovePropertyChangedObserver(ShillPropertyChangedObserver* observer);
75 // Starts monitoring PropertyChanged signal.
76 void MonitorPropertyChanged(const std::string& interface_name);
78 // Calls a method without results.
79 void CallVoidMethod(dbus::MethodCall* method_call,
80 const VoidDBusMethodCallback& callback);
82 // Calls a method with an object path result.
83 void CallObjectPathMethod(dbus::MethodCall* method_call,
84 const ObjectPathDBusMethodCallback& callback);
86 // Calls a method with an object path result where there is an error callback.
87 void CallObjectPathMethodWithErrorCallback(
88 dbus::MethodCall* method_call,
89 const ObjectPathCallback& callback,
90 const ErrorCallback& error_callback);
92 // Calls a method with a dictionary value result.
93 void CallDictionaryValueMethod(dbus::MethodCall* method_call,
94 const DictionaryValueCallback& callback);
96 // Calls a method without results with error callback.
97 void CallVoidMethodWithErrorCallback(dbus::MethodCall* method_call,
98 const base::Closure& callback,
99 const ErrorCallback& error_callback);
101 // Calls a method with a dictionary value result with error callback.
102 void CallDictionaryValueMethodWithErrorCallback(
103 dbus::MethodCall* method_call,
104 const DictionaryValueCallbackWithoutStatus& callback,
105 const ErrorCallback& error_callback);
107 // Calls a method with a boolean array result with error callback.
108 void CallListValueMethodWithErrorCallback(
109 dbus::MethodCall* method_call,
110 const ListValueCallback& callback,
111 const ErrorCallback& error_callback);
113 // DEPRECATED DO NOT USE: Calls a method without results.
114 bool CallVoidMethodAndBlock(dbus::MethodCall* method_call);
116 // DEPRECATED DO NOT USE: Calls a method with a dictionary value result.
117 // The caller is responsible to delete the result.
118 // This method returns NULL when method call fails.
119 base::DictionaryValue* CallDictionaryValueMethodAndBlock(
120 dbus::MethodCall* method_call);
122 // Appends the value (basic types and string-to-string dictionary) to the
123 // writer as a variant.
124 static void AppendValueDataAsVariant(dbus::MessageWriter* writer,
125 const base::Value& value);
127 private:
128 // Handles the result of signal connection setup.
129 void OnSignalConnected(const std::string& interface,
130 const std::string& signal,
131 bool success);
133 // Handles PropertyChanged signal.
134 void OnPropertyChanged(dbus::Signal* signal);
136 // Handles responses for methods without results.
137 void OnVoidMethod(const VoidDBusMethodCallback& callback,
138 dbus::Response* response);
140 // Handles responses for methods with ObjectPath results.
141 void OnObjectPathMethod(const ObjectPathDBusMethodCallback& callback,
142 dbus::Response* response);
144 // Handles responses for methods with ObjectPath results.
145 void OnObjectPathMethodWithoutStatus(
146 const ObjectPathCallback& callback,
147 const ErrorCallback& error_callback,
148 dbus::Response* response);
150 // Handles responses for methods with DictionaryValue results.
151 void OnDictionaryValueMethod(const DictionaryValueCallback& callback,
152 dbus::Response* response);
154 // Handles responses for methods without results.
155 // Used by CallVoidMethodWithErrorCallback().
156 void OnVoidMethodWithErrorCallback(const base::Closure& callback,
157 dbus::Response* response);
159 // Handles responses for methods with DictionaryValue results.
160 // Used by CallDictionaryValueMethodWithErrorCallback().
161 void OnDictionaryValueMethodWithErrorCallback(
162 const DictionaryValueCallbackWithoutStatus& callback,
163 const ErrorCallback& error_callback,
164 dbus::Response* response);
166 // Handles responses for methods with Boolean array results.
167 // Used by CallBooleanArrayMethodWithErrorCallback().
168 void OnListValueMethodWithErrorCallback(
169 const ListValueCallback& callback,
170 const ErrorCallback& error_callback,
171 dbus::Response* response);
173 // Handles errors for method calls.
174 void OnError(const ErrorCallback& error_callback,
175 dbus::ErrorResponse* response);
177 // TODO(hashimoto): Remove this when we no longer need to make blocking calls.
178 BlockingMethodCaller blocking_method_caller_;
179 dbus::ObjectProxy* proxy_;
180 PropertyChangedHandler property_changed_handler_;
181 ObserverList<ShillPropertyChangedObserver> observer_list_;
183 // Note: This should remain the last member so it'll be destroyed and
184 // invalidate its weak pointers before any other members are destroyed.
185 base::WeakPtrFactory<ShillClientHelper> weak_ptr_factory_;
187 DISALLOW_COPY_AND_ASSIGN(ShillClientHelper);
190 } // namespace chromeos
192 #endif // CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_