blob: af8b5dc89f338ee153a11b21bd14098ba8315bbd [file] [log] [blame]
rspangler@google.com570c65b2009-10-09 20:56:14 +00001// Copyright (c) 2009 The Chromium OS 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.
4
5#ifndef CHROMEOS_DBUS_H_
6#define CHROMEOS_DBUS_H_
7
8#include <dbus/dbus-glib.h>
9#include <glib-object.h>
rspangler@google.com570c65b2009-10-09 20:56:14 +000010
11#include <algorithm>
12#include <string>
13
seanparent@google.comf2ea0242009-11-04 21:04:51 +000014#include "base/logging.h"
rspangler@google.com570c65b2009-10-09 20:56:14 +000015#include "chromeos/glib/object.h"
16
17namespace chromeos {
18
19// \precondition No functions in the dbus namespace can be called before
20// ::g_type_init();
21
22namespace dbus {
23
24// \brief BusConnection manages the ref-count for a ::DBusGConnection*.
25//
26// A BusConnection has reference semantics bound to a particular communication
27// bus.
28//
29// \models Copyable, Assignable
30// \related GetSystemBusConnection()
31
32class BusConnection {
33 public:
34 typedef ::DBusGConnection* value_type;
35
36 BusConnection(const BusConnection& x)
37 : object_(dbus_g_connection_ref(x.object_)) {
38 }
39
40 ~BusConnection() {
41 ::dbus_g_connection_unref(object_);
42 }
43
44 BusConnection& operator=(BusConnection x) {
45 swap(*this, x);
46 return *this;
47 }
48
Yusuke Satof8059812010-01-09 20:13:07 +090049 const value_type& g_connection() const {
50 DCHECK(object_) << "referencing an empty connection";
51 return object_;
52 }
53
rspangler@google.com570c65b2009-10-09 20:56:14 +000054 private:
55 friend void swap(BusConnection& x, BusConnection& y);
56
57 friend class Proxy;
58 friend BusConnection GetSystemBusConnection();
Yusuke Sato71da5ce2009-12-15 10:29:43 +090059 friend BusConnection GetPrivateBusConnection(const char* address);
rspangler@google.com570c65b2009-10-09 20:56:14 +000060
61 // Constructor takes ownership
62 explicit BusConnection(::DBusGConnection* x)
63 : object_(x) {
64 DCHECK(object_) << "Constructing BusConnection with NULL object.";
65 }
66
67 value_type object_;
68};
69
70inline void swap(BusConnection& x, BusConnection& y) {
71 std::swap(x.object_, y.object_);
72}
73
74// \brief Proxy manages the ref-count for a ::DBusGProxy*.
75//
76// Proxy has reference semantics and represents a connection to on object on
77// the bus. A proxy object is constructed with a connection to a bus, a name
78// to an entity on the bus, a path to an object owned by the entity, and an
79// interface protocol name used to communicate with the object.
80
81class Proxy {
82 public:
83 typedef ::DBusGProxy* value_type;
84
85 Proxy()
86 : object_(NULL) {
87 }
88
Yusuke Sato71da5ce2009-12-15 10:29:43 +090089 // Set |connect_to_name_owner| true if you'd like to use
90 // dbus_g_proxy_new_for_name_owner() rather than dbus_g_proxy_new_for_name().
91 Proxy(const BusConnection& connection,
92 const char* name,
93 const char* path,
94 const char* interface,
95 bool connect_to_name_owner)
96 : object_(GetGProxy(
97 connection, name, path, interface, connect_to_name_owner)) {
98 }
99
100 // Equivalent to Proxy(connection, name, path, interface, false).
rspangler@google.com570c65b2009-10-09 20:56:14 +0000101 Proxy(const BusConnection& connection,
102 const char* name,
103 const char* path,
104 const char* interface)
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900105 : object_(GetGProxy(connection, name, path, interface, false)) {
rspangler@google.com570c65b2009-10-09 20:56:14 +0000106 }
107
108 Proxy(const Proxy& x)
109 : object_(x.object_) {
110 if (object_)
111 ::g_object_ref(object_);
112 }
113
114 ~Proxy() {
115 if (object_)
116 ::g_object_unref(object_);
117 }
118
119 Proxy& operator=(Proxy x) {
120 swap(*this, x);
121 return *this;
122 }
123
124 const char* path() const {
125 DCHECK(object_) << "referencing an empty proxy";
126 return ::dbus_g_proxy_get_path(object_);
127 }
128
129 // gproxy() returns a reference to the underlying ::DBusGProxy*. As this
130 // library evolves, the gproxy() will be moved to be private.
131
132 const value_type& gproxy() const {
133 DCHECK(object_) << "referencing an empty proxy";
134 return object_;
135 }
136
137 operator bool() const {
138 return object_;
139 }
140
141 private:
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900142 static value_type GetGProxy(const BusConnection& connection,
143 const char* name,
144 const char* path,
145 const char* interface,
146 bool connect_to_name_owner);
147
rspangler@google.com570c65b2009-10-09 20:56:14 +0000148 operator int() const; // for safe bool cast
149 friend void swap(Proxy& x, Proxy& y);
150
151 value_type object_;
152};
153
154inline void swap(Proxy& x, Proxy& y) {
155 std::swap(x.object_, y.object_);
156}
157
Chris Masonec8150f12010-01-12 12:23:22 -0800158// \brief RegisterExclusiveService configures a GObject to run as a service on
159// a supplied ::BusConnection.
160//
161// RegisterExclusiveService encapsulates the process of configuring the
162// supplied \param object at \param service_path on the \param connection.
163// Exclusivity is ensured by replacing any existing services at that named
164// location and confirming that the connection is the primary owner.
165//
166// Type information for the \param object must be installed with
167// dbus_g_object_type_install_info prior to use.
168
169bool RegisterExclusiveService(const BusConnection& connection,
170 const char* interface_name,
171 const char* service_name,
172 const char* service_path,
173 GObject* object);
174
rspangler@google.com570c65b2009-10-09 20:56:14 +0000175template <typename F> // F is a function signature
176class MonitorConnection;
177
178template <typename A1>
179class MonitorConnection<void (A1)> {
180 public:
181 MonitorConnection(const Proxy& proxy, const char* name,
182 void (*monitor)(void*, A1), void* object)
183 : proxy_(proxy), name_(name), monitor_(monitor), object_(object) {
184 }
185
186 static void Run(::DBusGProxy*, A1 x, MonitorConnection* self) {
187 self->monitor_(self->object_, x);
188 }
189 const Proxy& proxy() const {
190 return proxy_;
191 }
192 const std::string& name() const {
193 return name_;
194 }
195
196 private:
197 Proxy proxy_;
198 std::string name_;
199 void (*monitor_)(void*, A1);
200 void* object_;
201};
202
203template <typename A1, typename A2>
204class MonitorConnection<void (A1, A2)> {
205 public:
206 MonitorConnection(const Proxy& proxy, const char* name,
207 void (*monitor)(void*, A1, A2), void* object)
208 : proxy_(proxy), name_(name), monitor_(monitor), object_(object) {
209 }
210
211 static void Run(::DBusGProxy*, A1 x, A2 y, MonitorConnection* self) {
212 self->monitor_(self->object_, x, y);
213 }
214 const Proxy& proxy() const {
215 return proxy_;
216 }
217 const std::string& name() const {
218 return name_;
219 }
220
221 private:
222 Proxy proxy_;
223 std::string name_;
224 void (*monitor_)(void*, A1, A2);
225 void* object_;
226
227};
228
229template <typename A1>
230MonitorConnection<void (A1)>* Monitor(const Proxy& proxy, const char* name,
231 void (*monitor)(void*, A1),
232 void* object) {
233 typedef MonitorConnection<void (A1)> ConnectionType;
234
235 ConnectionType* result = new ConnectionType(proxy, name, monitor, object);
236
237 ::dbus_g_proxy_add_signal(proxy.gproxy(), name,
238 glib::type_to_gtypeid<A1>(), G_TYPE_INVALID);
239 ::dbus_g_proxy_connect_signal(proxy.gproxy(), name,
240 G_CALLBACK(&ConnectionType::Run),
241 result, NULL);
242 return result;
243}
244
245template <typename A1, typename A2>
246MonitorConnection<void (A1, A2)>* Monitor(const Proxy& proxy, const char* name,
247 void (*monitor)(void*, A1, A2),
248 void* object) {
249 typedef MonitorConnection<void (A1, A2)> ConnectionType;
250
251 ConnectionType* result = new ConnectionType(proxy, name, monitor, object);
252
253 ::dbus_g_proxy_add_signal(proxy.gproxy(), name,
254 glib::type_to_gtypeid<A1>(),
255 glib::type_to_gtypeid<A2>(),
256 G_TYPE_INVALID);
257 ::dbus_g_proxy_connect_signal(proxy.gproxy(), name,
258 G_CALLBACK(&ConnectionType::Run),
259 result, NULL);
260 return result;
261}
262
263template <typename F>
264void Disconnect(MonitorConnection<F>* connection) {
265 typedef MonitorConnection<F> ConnectionType;
266
267 ::dbus_g_proxy_disconnect_signal(connection->proxy().gproxy(),
268 connection->name().c_str(),
269 G_CALLBACK(&ConnectionType::Run),
270 connection);
271 delete connection;
272}
273
274// \brief call_PtrArray() invokes a method on a proxy returning a
275// glib::PtrArray.
276//
277// CallPtrArray is the first instance of what is likely to be a general
278// way to make method calls to a proxy. It will likely be replaced with
279// something like Call(proxy, method, arg1, arg2, ..., ResultType*) in the
280// future. However, I don't yet have enough cases to generalize from.
281
282bool CallPtrArray(const Proxy& proxy,
283 const char* method,
284 glib::ScopedPtrArray<const char*>* result);
285
286// \brief RetrieveProperty() retrieves a property of an object associated with a
287// proxy.
288//
289// Given a proxy to an object supporting the org.freedesktop.DBus.Properties
290// interface, the RetrieveProperty() call will retrieve a property of the
291// specified interface on the object storing it in \param result and returning
292// \true. If the dbus call fails or the object returned is not of type \param T,
293// then \false is returned and \param result is unchanged.
294//
295// \example
296// Proxy proxy(GetSystemBusConnection(),
297// "org.freedesktop.DeviceKit.Power", // A named entity on the bus
298// battery_name, // Path to a battery on the bus
299// "org.freedesktop.DBus.Properties") // Properties interface
300//
301// double x;
302// if (RetrieveProperty(proxy,
303// "org.freedesktop.DeviceKit.Power.Device",
304// "percentage")
305// std::cout << "Battery charge is " << x << "% of capacity.";
306// \end_example
307
308template <typename T>
309bool RetrieveProperty(const Proxy& proxy,
310 const char* interface,
311 const char* property,
312 T* result) {
313 glib::ScopedError error;
314 glib::Value value;
315
316 if (!::dbus_g_proxy_call(proxy.gproxy(), "Get", &Resetter(&error).lvalue(),
317 G_TYPE_STRING, interface,
318 G_TYPE_STRING, property,
319 G_TYPE_INVALID,
320 G_TYPE_VALUE, &value,
dhg@google.com7ec90e92009-10-27 16:44:35 +0000321 G_TYPE_INVALID)){
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900322 LOG(ERROR) << "Getting property failed: "
323 << (error->message ? error->message : "Unknown Error.");
rspangler@google.com570c65b2009-10-09 20:56:14 +0000324 return false;
325
dhg@google.com7ec90e92009-10-27 16:44:35 +0000326 }
327 return glib::Retrieve(value, result);
rspangler@google.com570c65b2009-10-09 20:56:14 +0000328}
329
330// \brief RetrieveProperties returns a HashTable of all properties for the
331// specified interface.
332
333bool RetrieveProperties(const Proxy& proxy,
334 const char* interface,
335 glib::ScopedHashTable* result);
336
337// \brief Returns a connection to the system bus.
338
339BusConnection GetSystemBusConnection();
340
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900341// \brief Returns a private connection to a bus at |address|.
342
343BusConnection GetPrivateBusConnection(const char* address);
344
rspangler@google.com570c65b2009-10-09 20:56:14 +0000345} // namespace dbus
346} // namespace chromeos
347
348#endif // CHROMEOS_DBUS_H_