blob: 9ac1a1f5088f353259e4fa5e1c1a95521de6d91d [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)
Yusuke Sato35371c82010-06-23 19:04:02 +090037 : object_(x.object_) {
38 if (object_)
39 ::dbus_g_connection_ref(object_);
rspangler@google.com570c65b2009-10-09 20:56:14 +000040 }
41
42 ~BusConnection() {
Yusuke Sato35371c82010-06-23 19:04:02 +090043 if (object_)
44 ::dbus_g_connection_unref(object_);
rspangler@google.com570c65b2009-10-09 20:56:14 +000045 }
46
47 BusConnection& operator=(BusConnection x) {
48 swap(*this, x);
49 return *this;
50 }
51
Yusuke Satof8059812010-01-09 20:13:07 +090052 const value_type& g_connection() const {
53 DCHECK(object_) << "referencing an empty connection";
54 return object_;
55 }
56
Yusuke Sato35371c82010-06-23 19:04:02 +090057 operator bool() const {
58 return object_;
59 }
60
61 bool HasConnection() const {
62 return object_;
63 }
64
rspangler@google.com570c65b2009-10-09 20:56:14 +000065 private:
66 friend void swap(BusConnection& x, BusConnection& y);
67
68 friend class Proxy;
69 friend BusConnection GetSystemBusConnection();
Yusuke Sato71da5ce2009-12-15 10:29:43 +090070 friend BusConnection GetPrivateBusConnection(const char* address);
rspangler@google.com570c65b2009-10-09 20:56:14 +000071
72 // Constructor takes ownership
73 explicit BusConnection(::DBusGConnection* x)
74 : object_(x) {
rspangler@google.com570c65b2009-10-09 20:56:14 +000075 }
76
77 value_type object_;
78};
79
80inline void swap(BusConnection& x, BusConnection& y) {
81 std::swap(x.object_, y.object_);
82}
83
84// \brief Proxy manages the ref-count for a ::DBusGProxy*.
85//
86// Proxy has reference semantics and represents a connection to on object on
87// the bus. A proxy object is constructed with a connection to a bus, a name
88// to an entity on the bus, a path to an object owned by the entity, and an
89// interface protocol name used to communicate with the object.
90
91class Proxy {
92 public:
93 typedef ::DBusGProxy* value_type;
94
95 Proxy()
96 : object_(NULL) {
97 }
98
Yusuke Sato71da5ce2009-12-15 10:29:43 +090099 // Set |connect_to_name_owner| true if you'd like to use
100 // dbus_g_proxy_new_for_name_owner() rather than dbus_g_proxy_new_for_name().
101 Proxy(const BusConnection& connection,
102 const char* name,
103 const char* path,
104 const char* interface,
105 bool connect_to_name_owner)
106 : object_(GetGProxy(
107 connection, name, path, interface, connect_to_name_owner)) {
108 }
109
110 // Equivalent to Proxy(connection, name, path, interface, false).
rspangler@google.com570c65b2009-10-09 20:56:14 +0000111 Proxy(const BusConnection& connection,
112 const char* name,
113 const char* path,
114 const char* interface)
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900115 : object_(GetGProxy(connection, name, path, interface, false)) {
rspangler@google.com570c65b2009-10-09 20:56:14 +0000116 }
117
Mitsuru Oshima93b37832010-05-18 14:17:57 -0700118 // Creates a peer proxy using dbus_g_proxy_new_for_peer.
119 Proxy(const BusConnection& connection,
120 const char* path,
121 const char* interface)
122 : object_(GetGPeerProxy(connection, path, interface)) {
123 }
124
rspangler@google.com570c65b2009-10-09 20:56:14 +0000125 Proxy(const Proxy& x)
126 : object_(x.object_) {
127 if (object_)
128 ::g_object_ref(object_);
129 }
130
131 ~Proxy() {
132 if (object_)
133 ::g_object_unref(object_);
134 }
135
136 Proxy& operator=(Proxy x) {
137 swap(*this, x);
138 return *this;
139 }
140
141 const char* path() const {
142 DCHECK(object_) << "referencing an empty proxy";
143 return ::dbus_g_proxy_get_path(object_);
144 }
145
146 // gproxy() returns a reference to the underlying ::DBusGProxy*. As this
147 // library evolves, the gproxy() will be moved to be private.
148
149 const value_type& gproxy() const {
150 DCHECK(object_) << "referencing an empty proxy";
151 return object_;
152 }
153
154 operator bool() const {
155 return object_;
156 }
157
158 private:
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900159 static value_type GetGProxy(const BusConnection& connection,
160 const char* name,
161 const char* path,
162 const char* interface,
163 bool connect_to_name_owner);
164
Mitsuru Oshima93b37832010-05-18 14:17:57 -0700165 static value_type GetGPeerProxy(const BusConnection& connection,
166 const char* path,
167 const char* interface);
168
rspangler@google.com570c65b2009-10-09 20:56:14 +0000169 operator int() const; // for safe bool cast
170 friend void swap(Proxy& x, Proxy& y);
171
172 value_type object_;
173};
174
175inline void swap(Proxy& x, Proxy& y) {
176 std::swap(x.object_, y.object_);
177}
178
Chris Masonec8150f12010-01-12 12:23:22 -0800179// \brief RegisterExclusiveService configures a GObject to run as a service on
180// a supplied ::BusConnection.
181//
182// RegisterExclusiveService encapsulates the process of configuring the
183// supplied \param object at \param service_path on the \param connection.
184// Exclusivity is ensured by replacing any existing services at that named
185// location and confirming that the connection is the primary owner.
186//
187// Type information for the \param object must be installed with
188// dbus_g_object_type_install_info prior to use.
189
190bool RegisterExclusiveService(const BusConnection& connection,
191 const char* interface_name,
192 const char* service_name,
193 const char* service_path,
194 GObject* object);
195
rspangler@google.com570c65b2009-10-09 20:56:14 +0000196template <typename F> // F is a function signature
197class MonitorConnection;
198
199template <typename A1>
200class MonitorConnection<void (A1)> {
201 public:
202 MonitorConnection(const Proxy& proxy, const char* name,
203 void (*monitor)(void*, A1), void* object)
204 : proxy_(proxy), name_(name), monitor_(monitor), object_(object) {
205 }
206
207 static void Run(::DBusGProxy*, A1 x, MonitorConnection* self) {
208 self->monitor_(self->object_, x);
209 }
210 const Proxy& proxy() const {
211 return proxy_;
212 }
213 const std::string& name() const {
214 return name_;
215 }
216
217 private:
218 Proxy proxy_;
219 std::string name_;
220 void (*monitor_)(void*, A1);
221 void* object_;
222};
223
224template <typename A1, typename A2>
225class MonitorConnection<void (A1, A2)> {
226 public:
227 MonitorConnection(const Proxy& proxy, const char* name,
228 void (*monitor)(void*, A1, A2), void* object)
229 : proxy_(proxy), name_(name), monitor_(monitor), object_(object) {
230 }
231
232 static void Run(::DBusGProxy*, A1 x, A2 y, MonitorConnection* self) {
233 self->monitor_(self->object_, x, y);
234 }
235 const Proxy& proxy() const {
236 return proxy_;
237 }
238 const std::string& name() const {
239 return name_;
240 }
241
242 private:
243 Proxy proxy_;
244 std::string name_;
245 void (*monitor_)(void*, A1, A2);
246 void* object_;
247
248};
249
250template <typename A1>
251MonitorConnection<void (A1)>* Monitor(const Proxy& proxy, const char* name,
252 void (*monitor)(void*, A1),
253 void* object) {
254 typedef MonitorConnection<void (A1)> ConnectionType;
255
256 ConnectionType* result = new ConnectionType(proxy, name, monitor, object);
257
258 ::dbus_g_proxy_add_signal(proxy.gproxy(), name,
259 glib::type_to_gtypeid<A1>(), G_TYPE_INVALID);
260 ::dbus_g_proxy_connect_signal(proxy.gproxy(), name,
261 G_CALLBACK(&ConnectionType::Run),
262 result, NULL);
263 return result;
264}
265
266template <typename A1, typename A2>
267MonitorConnection<void (A1, A2)>* Monitor(const Proxy& proxy, const char* name,
268 void (*monitor)(void*, A1, A2),
269 void* object) {
270 typedef MonitorConnection<void (A1, A2)> ConnectionType;
271
272 ConnectionType* result = new ConnectionType(proxy, name, monitor, object);
273
274 ::dbus_g_proxy_add_signal(proxy.gproxy(), name,
275 glib::type_to_gtypeid<A1>(),
276 glib::type_to_gtypeid<A2>(),
277 G_TYPE_INVALID);
278 ::dbus_g_proxy_connect_signal(proxy.gproxy(), name,
279 G_CALLBACK(&ConnectionType::Run),
280 result, NULL);
281 return result;
282}
283
284template <typename F>
285void Disconnect(MonitorConnection<F>* connection) {
286 typedef MonitorConnection<F> ConnectionType;
287
288 ::dbus_g_proxy_disconnect_signal(connection->proxy().gproxy(),
289 connection->name().c_str(),
290 G_CALLBACK(&ConnectionType::Run),
291 connection);
292 delete connection;
293}
294
295// \brief call_PtrArray() invokes a method on a proxy returning a
296// glib::PtrArray.
297//
298// CallPtrArray is the first instance of what is likely to be a general
299// way to make method calls to a proxy. It will likely be replaced with
300// something like Call(proxy, method, arg1, arg2, ..., ResultType*) in the
301// future. However, I don't yet have enough cases to generalize from.
302
303bool CallPtrArray(const Proxy& proxy,
304 const char* method,
305 glib::ScopedPtrArray<const char*>* result);
306
307// \brief RetrieveProperty() retrieves a property of an object associated with a
308// proxy.
309//
310// Given a proxy to an object supporting the org.freedesktop.DBus.Properties
311// interface, the RetrieveProperty() call will retrieve a property of the
312// specified interface on the object storing it in \param result and returning
313// \true. If the dbus call fails or the object returned is not of type \param T,
314// then \false is returned and \param result is unchanged.
315//
316// \example
317// Proxy proxy(GetSystemBusConnection(),
318// "org.freedesktop.DeviceKit.Power", // A named entity on the bus
319// battery_name, // Path to a battery on the bus
320// "org.freedesktop.DBus.Properties") // Properties interface
321//
322// double x;
323// if (RetrieveProperty(proxy,
324// "org.freedesktop.DeviceKit.Power.Device",
325// "percentage")
326// std::cout << "Battery charge is " << x << "% of capacity.";
327// \end_example
328
329template <typename T>
330bool RetrieveProperty(const Proxy& proxy,
331 const char* interface,
332 const char* property,
333 T* result) {
334 glib::ScopedError error;
335 glib::Value value;
336
337 if (!::dbus_g_proxy_call(proxy.gproxy(), "Get", &Resetter(&error).lvalue(),
338 G_TYPE_STRING, interface,
339 G_TYPE_STRING, property,
340 G_TYPE_INVALID,
341 G_TYPE_VALUE, &value,
dhg@google.com7ec90e92009-10-27 16:44:35 +0000342 G_TYPE_INVALID)){
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900343 LOG(ERROR) << "Getting property failed: "
344 << (error->message ? error->message : "Unknown Error.");
rspangler@google.com570c65b2009-10-09 20:56:14 +0000345 return false;
346
dhg@google.com7ec90e92009-10-27 16:44:35 +0000347 }
348 return glib::Retrieve(value, result);
rspangler@google.com570c65b2009-10-09 20:56:14 +0000349}
350
351// \brief RetrieveProperties returns a HashTable of all properties for the
352// specified interface.
353
354bool RetrieveProperties(const Proxy& proxy,
355 const char* interface,
356 glib::ScopedHashTable* result);
357
358// \brief Returns a connection to the system bus.
359
360BusConnection GetSystemBusConnection();
361
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900362// \brief Returns a private connection to a bus at |address|.
363
364BusConnection GetPrivateBusConnection(const char* address);
365
rspangler@google.com570c65b2009-10-09 20:56:14 +0000366} // namespace dbus
367} // namespace chromeos
368
369#endif // CHROMEOS_DBUS_H_