blob: 60060098783bfc48573656022a47f29602b1b7db [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
Alex Vakulenkofed60b02015-10-27 09:53:05 -07005#ifndef LIBBRILLO_BRILLO_GLIB_DBUS_H_
6#define LIBBRILLO_BRILLO_GLIB_DBUS_H_
rspangler@google.com570c65b2009-10-09 20:56:14 +00007
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"
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070015#include <brillo/brillo_export.h>
16#include <brillo/glib/object.h>
rspangler@google.com570c65b2009-10-09 20:56:14 +000017
Denis Glotov8a93c902011-02-11 22:32:49 +030018struct DBusMessage;
19struct DBusConnection;
20
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070021namespace brillo {
rspangler@google.com570c65b2009-10-09 20:56:14 +000022
23// \precondition No functions in the dbus namespace can be called before
24// ::g_type_init();
25
26namespace dbus {
27
28// \brief BusConnection manages the ref-count for a ::DBusGConnection*.
29//
30// A BusConnection has reference semantics bound to a particular communication
31// bus.
32//
33// \models Copyable, Assignable
34// \related GetSystemBusConnection()
35
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070036class BRILLO_EXPORT BusConnection {
rspangler@google.com570c65b2009-10-09 20:56:14 +000037 public:
38 typedef ::DBusGConnection* value_type;
39
Alex Vakulenko05d29042015-01-13 09:39:25 -080040 BusConnection(const BusConnection& x) : object_(x.object_) {
Yusuke Sato35371c82010-06-23 19:04:02 +090041 if (object_)
42 ::dbus_g_connection_ref(object_);
rspangler@google.com570c65b2009-10-09 20:56:14 +000043 }
44
45 ~BusConnection() {
Yusuke Sato35371c82010-06-23 19:04:02 +090046 if (object_)
47 ::dbus_g_connection_unref(object_);
rspangler@google.com570c65b2009-10-09 20:56:14 +000048 }
49
50 BusConnection& operator=(BusConnection x) {
51 swap(*this, x);
52 return *this;
53 }
54
Yusuke Satof8059812010-01-09 20:13:07 +090055 const value_type& g_connection() const {
56 DCHECK(object_) << "referencing an empty connection";
57 return object_;
58 }
59
Alex Vakulenko05d29042015-01-13 09:39:25 -080060 operator bool() const { return object_; }
Yusuke Sato35371c82010-06-23 19:04:02 +090061
Alex Vakulenko05d29042015-01-13 09:39:25 -080062 bool HasConnection() const { return object_; }
Yusuke Sato35371c82010-06-23 19:04:02 +090063
rspangler@google.com570c65b2009-10-09 20:56:14 +000064 private:
65 friend void swap(BusConnection& x, BusConnection& y);
66
67 friend class Proxy;
68 friend BusConnection GetSystemBusConnection();
Yusuke Sato71da5ce2009-12-15 10:29:43 +090069 friend BusConnection GetPrivateBusConnection(const char* address);
rspangler@google.com570c65b2009-10-09 20:56:14 +000070
71 // Constructor takes ownership
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070072 BRILLO_PRIVATE explicit BusConnection(::DBusGConnection* x) : object_(x) {}
rspangler@google.com570c65b2009-10-09 20:56:14 +000073
74 value_type object_;
75};
76
77inline void swap(BusConnection& x, BusConnection& y) {
78 std::swap(x.object_, y.object_);
79}
80
81// \brief Proxy manages the ref-count for a ::DBusGProxy*.
82//
83// Proxy has reference semantics and represents a connection to on object on
84// the bus. A proxy object is constructed with a connection to a bus, a name
85// to an entity on the bus, a path to an object owned by the entity, and an
86// interface protocol name used to communicate with the object.
87
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -070088class BRILLO_EXPORT Proxy {
rspangler@google.com570c65b2009-10-09 20:56:14 +000089 public:
90 typedef ::DBusGProxy* value_type;
91
Alex Vakulenko847b8712014-08-29 10:43:06 -070092 Proxy();
rspangler@google.com570c65b2009-10-09 20:56:14 +000093
Yusuke Sato71da5ce2009-12-15 10:29:43 +090094 // Set |connect_to_name_owner| true if you'd like to use
95 // dbus_g_proxy_new_for_name_owner() rather than dbus_g_proxy_new_for_name().
96 Proxy(const BusConnection& connection,
97 const char* name,
98 const char* path,
99 const char* interface,
Alex Vakulenko847b8712014-08-29 10:43:06 -0700100 bool connect_to_name_owner);
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900101
102 // Equivalent to Proxy(connection, name, path, interface, false).
rspangler@google.com570c65b2009-10-09 20:56:14 +0000103 Proxy(const BusConnection& connection,
104 const char* name,
105 const char* path,
Alex Vakulenko847b8712014-08-29 10:43:06 -0700106 const char* interface);
rspangler@google.com570c65b2009-10-09 20:56:14 +0000107
Mitsuru Oshima93b37832010-05-18 14:17:57 -0700108 // Creates a peer proxy using dbus_g_proxy_new_for_peer.
109 Proxy(const BusConnection& connection,
110 const char* path,
Alex Vakulenko847b8712014-08-29 10:43:06 -0700111 const char* interface);
Mitsuru Oshima93b37832010-05-18 14:17:57 -0700112
Alex Vakulenko847b8712014-08-29 10:43:06 -0700113 Proxy(const Proxy& x);
rspangler@google.com570c65b2009-10-09 20:56:14 +0000114
Alex Vakulenko847b8712014-08-29 10:43:06 -0700115 ~Proxy();
rspangler@google.com570c65b2009-10-09 20:56:14 +0000116
117 Proxy& operator=(Proxy x) {
118 swap(*this, x);
119 return *this;
120 }
121
122 const char* path() const {
123 DCHECK(object_) << "referencing an empty proxy";
124 return ::dbus_g_proxy_get_path(object_);
125 }
126
127 // gproxy() returns a reference to the underlying ::DBusGProxy*. As this
128 // library evolves, the gproxy() will be moved to be private.
129
130 const value_type& gproxy() const {
131 DCHECK(object_) << "referencing an empty proxy";
132 return object_;
133 }
134
Alex Vakulenko05d29042015-01-13 09:39:25 -0800135 operator bool() const { return object_; }
rspangler@google.com570c65b2009-10-09 20:56:14 +0000136
137 private:
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700138 BRILLO_PRIVATE static value_type GetGProxy(const BusConnection& connection,
139 const char* name,
140 const char* path,
141 const char* interface,
142 bool connect_to_name_owner);
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900143
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700144 BRILLO_PRIVATE static value_type GetGPeerProxy(
Alex Vakulenko847b8712014-08-29 10:43:06 -0700145 const BusConnection& connection,
146 const char* path,
147 const char* interface);
Mitsuru Oshima93b37832010-05-18 14:17:57 -0700148
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700149 BRILLO_PRIVATE operator int() const; // for safe bool cast
rspangler@google.com570c65b2009-10-09 20:56:14 +0000150 friend void swap(Proxy& x, Proxy& y);
151
152 value_type object_;
153};
154
155inline void swap(Proxy& x, Proxy& y) {
156 std::swap(x.object_, y.object_);
157}
158
Chris Masonec8150f12010-01-12 12:23:22 -0800159// \brief RegisterExclusiveService configures a GObject to run as a service on
160// a supplied ::BusConnection.
161//
162// RegisterExclusiveService encapsulates the process of configuring the
163// supplied \param object at \param service_path on the \param connection.
164// Exclusivity is ensured by replacing any existing services at that named
165// location and confirming that the connection is the primary owner.
166//
167// Type information for the \param object must be installed with
168// dbus_g_object_type_install_info prior to use.
169
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700170BRILLO_EXPORT bool RegisterExclusiveService(const BusConnection& connection,
171 const char* interface_name,
172 const char* service_name,
173 const char* service_path,
174 GObject* object);
Chris Masonec8150f12010-01-12 12:23:22 -0800175
Alex Vakulenko05d29042015-01-13 09:39:25 -0800176template<typename F> // F is a function signature
rspangler@google.com570c65b2009-10-09 20:56:14 +0000177class MonitorConnection;
178
Alex Vakulenko05d29042015-01-13 09:39:25 -0800179template<typename A1>
Alex Vakulenkoef47ede2014-07-31 11:44:27 -0700180class MonitorConnection<void(A1)> {
rspangler@google.com570c65b2009-10-09 20:56:14 +0000181 public:
Alex Vakulenko05d29042015-01-13 09:39:25 -0800182 MonitorConnection(const Proxy& proxy,
183 const char* name,
184 void (*monitor)(void*, A1),
185 void* object)
186 : proxy_(proxy), name_(name), monitor_(monitor), object_(object) {}
rspangler@google.com570c65b2009-10-09 20:56:14 +0000187
188 static void Run(::DBusGProxy*, A1 x, MonitorConnection* self) {
189 self->monitor_(self->object_, x);
190 }
Alex Vakulenko05d29042015-01-13 09:39:25 -0800191 const Proxy& proxy() const { return proxy_; }
192 const std::string& name() const { return name_; }
rspangler@google.com570c65b2009-10-09 20:56:14 +0000193
194 private:
195 Proxy proxy_;
196 std::string name_;
197 void (*monitor_)(void*, A1);
198 void* object_;
199};
200
Alex Vakulenko05d29042015-01-13 09:39:25 -0800201template<typename A1, typename A2>
Alex Vakulenkoef47ede2014-07-31 11:44:27 -0700202class MonitorConnection<void(A1, A2)> {
rspangler@google.com570c65b2009-10-09 20:56:14 +0000203 public:
Alex Vakulenko05d29042015-01-13 09:39:25 -0800204 MonitorConnection(const Proxy& proxy,
205 const char* name,
206 void (*monitor)(void*, A1, A2),
207 void* object)
208 : proxy_(proxy), name_(name), monitor_(monitor), object_(object) {}
rspangler@google.com570c65b2009-10-09 20:56:14 +0000209
210 static void Run(::DBusGProxy*, A1 x, A2 y, MonitorConnection* self) {
211 self->monitor_(self->object_, x, y);
212 }
Alex Vakulenko05d29042015-01-13 09:39:25 -0800213 const Proxy& proxy() const { return proxy_; }
214 const std::string& name() const { return name_; }
rspangler@google.com570c65b2009-10-09 20:56:14 +0000215
216 private:
217 Proxy proxy_;
218 std::string name_;
219 void (*monitor_)(void*, A1, A2);
220 void* object_;
rspangler@google.com570c65b2009-10-09 20:56:14 +0000221};
222
Alex Vakulenko05d29042015-01-13 09:39:25 -0800223template<typename A1, typename A2, typename A3>
Alex Vakulenkoef47ede2014-07-31 11:44:27 -0700224class MonitorConnection<void(A1, A2, A3)> {
Toni Barzicfa027112010-08-16 19:21:34 -0700225 public:
Alex Vakulenko05d29042015-01-13 09:39:25 -0800226 MonitorConnection(const Proxy& proxy,
227 const char* name,
228 void (*monitor)(void*, A1, A2, A3),
229 void* object)
230 : proxy_(proxy), name_(name), monitor_(monitor), object_(object) {}
Toni Barzicfa027112010-08-16 19:21:34 -0700231
232 static void Run(::DBusGProxy*, A1 x, A2 y, A3 z, MonitorConnection* self) {
233 self->monitor_(self->object_, x, y, z);
234 }
Alex Vakulenko05d29042015-01-13 09:39:25 -0800235 const Proxy& proxy() const { return proxy_; }
236 const std::string& name() const { return name_; }
Toni Barzicfa027112010-08-16 19:21:34 -0700237
238 private:
239 Proxy proxy_;
240 std::string name_;
241 void (*monitor_)(void*, A1, A2, A3);
242 void* object_;
tbarzic6dd315a2011-07-20 16:53:10 -0700243};
Toni Barzicfa027112010-08-16 19:21:34 -0700244
Alex Vakulenko05d29042015-01-13 09:39:25 -0800245template<typename A1, typename A2, typename A3, typename A4>
Alex Vakulenkoef47ede2014-07-31 11:44:27 -0700246class MonitorConnection<void(A1, A2, A3, A4)> {
tbarzic6dd315a2011-07-20 16:53:10 -0700247 public:
Alex Vakulenko05d29042015-01-13 09:39:25 -0800248 MonitorConnection(const Proxy& proxy,
249 const char* name,
250 void (*monitor)(void*, A1, A2, A3, A4),
251 void* object)
252 : proxy_(proxy), name_(name), monitor_(monitor), object_(object) {}
tbarzic6dd315a2011-07-20 16:53:10 -0700253
Alex Vakulenko05d29042015-01-13 09:39:25 -0800254 static void Run(::DBusGProxy*,
255 A1 x,
256 A2 y,
257 A3 z,
258 A4 w,
tbarzic6dd315a2011-07-20 16:53:10 -0700259 MonitorConnection* self) {
260 self->monitor_(self->object_, x, y, z, w);
261 }
Alex Vakulenko05d29042015-01-13 09:39:25 -0800262 const Proxy& proxy() const { return proxy_; }
263 const std::string& name() const { return name_; }
tbarzic6dd315a2011-07-20 16:53:10 -0700264
265 private:
266 Proxy proxy_;
267 std::string name_;
268 void (*monitor_)(void*, A1, A2, A3, A4);
269 void* object_;
Toni Barzicfa027112010-08-16 19:21:34 -0700270};
271
Alex Vakulenko05d29042015-01-13 09:39:25 -0800272template<typename A1>
273MonitorConnection<void(A1)>* Monitor(const Proxy& proxy,
274 const char* name,
Alex Vakulenkoef47ede2014-07-31 11:44:27 -0700275 void (*monitor)(void*, A1),
276 void* object) {
Alex Vakulenko05d29042015-01-13 09:39:25 -0800277 typedef MonitorConnection<void(A1)> ConnectionType;
rspangler@google.com570c65b2009-10-09 20:56:14 +0000278
279 ConnectionType* result = new ConnectionType(proxy, name, monitor, object);
280
Alex Vakulenko05d29042015-01-13 09:39:25 -0800281 ::dbus_g_proxy_add_signal(
282 proxy.gproxy(), name, glib::type_to_gtypeid<A1>(), G_TYPE_INVALID);
283 ::dbus_g_proxy_connect_signal(
284 proxy.gproxy(), name, G_CALLBACK(&ConnectionType::Run), result, nullptr);
rspangler@google.com570c65b2009-10-09 20:56:14 +0000285 return result;
286}
287
Alex Vakulenko05d29042015-01-13 09:39:25 -0800288template<typename A1, typename A2>
289MonitorConnection<void(A1, A2)>* Monitor(const Proxy& proxy,
290 const char* name,
Alex Vakulenkoef47ede2014-07-31 11:44:27 -0700291 void (*monitor)(void*, A1, A2),
292 void* object) {
Alex Vakulenko05d29042015-01-13 09:39:25 -0800293 typedef MonitorConnection<void(A1, A2)> ConnectionType;
rspangler@google.com570c65b2009-10-09 20:56:14 +0000294
295 ConnectionType* result = new ConnectionType(proxy, name, monitor, object);
296
Alex Vakulenko05d29042015-01-13 09:39:25 -0800297 ::dbus_g_proxy_add_signal(proxy.gproxy(),
298 name,
rspangler@google.com570c65b2009-10-09 20:56:14 +0000299 glib::type_to_gtypeid<A1>(),
300 glib::type_to_gtypeid<A2>(),
301 G_TYPE_INVALID);
Alex Vakulenko05d29042015-01-13 09:39:25 -0800302 ::dbus_g_proxy_connect_signal(
303 proxy.gproxy(), name, G_CALLBACK(&ConnectionType::Run), result, nullptr);
rspangler@google.com570c65b2009-10-09 20:56:14 +0000304 return result;
305}
306
Alex Vakulenko05d29042015-01-13 09:39:25 -0800307template<typename A1, typename A2, typename A3>
Alex Vakulenkoef47ede2014-07-31 11:44:27 -0700308MonitorConnection<void(A1, A2, A3)>* Monitor(const Proxy& proxy,
Alex Vakulenko05d29042015-01-13 09:39:25 -0800309 const char* name,
310 void (*monitor)(void*, A1, A2, A3),
311 void* object) {
312 typedef MonitorConnection<void(A1, A2, A3)> ConnectionType;
Toni Barzicfa027112010-08-16 19:21:34 -0700313
314 ConnectionType* result = new ConnectionType(proxy, name, monitor, object);
315
Alex Vakulenko05d29042015-01-13 09:39:25 -0800316 ::dbus_g_proxy_add_signal(proxy.gproxy(),
317 name,
Toni Barzicfa027112010-08-16 19:21:34 -0700318 glib::type_to_gtypeid<A1>(),
319 glib::type_to_gtypeid<A2>(),
320 glib::type_to_gtypeid<A3>(),
321 G_TYPE_INVALID);
Alex Vakulenko05d29042015-01-13 09:39:25 -0800322 ::dbus_g_proxy_connect_signal(
323 proxy.gproxy(), name, G_CALLBACK(&ConnectionType::Run), result, nullptr);
Toni Barzicfa027112010-08-16 19:21:34 -0700324 return result;
325}
326
Alex Vakulenko05d29042015-01-13 09:39:25 -0800327template<typename A1, typename A2, typename A3, typename A4>
328MonitorConnection<void(A1, A2, A3, A4)>* Monitor(
329 const Proxy& proxy,
tbarzic6dd315a2011-07-20 16:53:10 -0700330 const char* name,
331 void (*monitor)(void*, A1, A2, A3, A4),
332 void* object) {
Alex Vakulenko05d29042015-01-13 09:39:25 -0800333 typedef MonitorConnection<void(A1, A2, A3, A4)> ConnectionType;
tbarzic6dd315a2011-07-20 16:53:10 -0700334
335 ConnectionType* result = new ConnectionType(proxy, name, monitor, object);
336
Alex Vakulenko05d29042015-01-13 09:39:25 -0800337 ::dbus_g_proxy_add_signal(proxy.gproxy(),
338 name,
tbarzic6dd315a2011-07-20 16:53:10 -0700339 glib::type_to_gtypeid<A1>(),
340 glib::type_to_gtypeid<A2>(),
341 glib::type_to_gtypeid<A3>(),
342 glib::type_to_gtypeid<A4>(),
343 G_TYPE_INVALID);
Alex Vakulenko05d29042015-01-13 09:39:25 -0800344 ::dbus_g_proxy_connect_signal(
345 proxy.gproxy(), name, G_CALLBACK(&ConnectionType::Run), result, nullptr);
tbarzic6dd315a2011-07-20 16:53:10 -0700346 return result;
347}
348
Alex Vakulenko05d29042015-01-13 09:39:25 -0800349template<typename F>
rspangler@google.com570c65b2009-10-09 20:56:14 +0000350void Disconnect(MonitorConnection<F>* connection) {
351 typedef MonitorConnection<F> ConnectionType;
352
353 ::dbus_g_proxy_disconnect_signal(connection->proxy().gproxy(),
354 connection->name().c_str(),
355 G_CALLBACK(&ConnectionType::Run),
356 connection);
357 delete connection;
358}
359
360// \brief call_PtrArray() invokes a method on a proxy returning a
361// glib::PtrArray.
362//
363// CallPtrArray is the first instance of what is likely to be a general
364// way to make method calls to a proxy. It will likely be replaced with
365// something like Call(proxy, method, arg1, arg2, ..., ResultType*) in the
366// future. However, I don't yet have enough cases to generalize from.
367
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700368BRILLO_EXPORT bool CallPtrArray(const Proxy& proxy,
Alex Vakulenko847b8712014-08-29 10:43:06 -0700369 const char* method,
370 glib::ScopedPtrArray<const char*>* result);
rspangler@google.com570c65b2009-10-09 20:56:14 +0000371
372// \brief RetrieveProperty() retrieves a property of an object associated with a
373// proxy.
374//
375// Given a proxy to an object supporting the org.freedesktop.DBus.Properties
376// interface, the RetrieveProperty() call will retrieve a property of the
377// specified interface on the object storing it in \param result and returning
378// \true. If the dbus call fails or the object returned is not of type \param T,
379// then \false is returned and \param result is unchanged.
380//
381// \example
382// Proxy proxy(GetSystemBusConnection(),
383// "org.freedesktop.DeviceKit.Power", // A named entity on the bus
384// battery_name, // Path to a battery on the bus
385// "org.freedesktop.DBus.Properties") // Properties interface
386//
387// double x;
388// if (RetrieveProperty(proxy,
389// "org.freedesktop.DeviceKit.Power.Device",
390// "percentage")
391// std::cout << "Battery charge is " << x << "% of capacity.";
392// \end_example
393
Alex Vakulenko05d29042015-01-13 09:39:25 -0800394template<typename T>
Alex Vakulenko847b8712014-08-29 10:43:06 -0700395inline bool RetrieveProperty(const Proxy& proxy,
396 const char* interface,
397 const char* property,
398 T* result) {
rspangler@google.com570c65b2009-10-09 20:56:14 +0000399 glib::ScopedError error;
400 glib::Value value;
401
402 if (!::dbus_g_proxy_call(proxy.gproxy(), "Get", &Resetter(&error).lvalue(),
403 G_TYPE_STRING, interface,
404 G_TYPE_STRING, property,
405 G_TYPE_INVALID,
406 G_TYPE_VALUE, &value,
Bertrand SIMONNETb54b6dc2014-07-02 12:13:56 -0700407 G_TYPE_INVALID)) {
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900408 LOG(ERROR) << "Getting property failed: "
409 << (error->message ? error->message : "Unknown Error.");
rspangler@google.com570c65b2009-10-09 20:56:14 +0000410 return false;
dhg@google.com7ec90e92009-10-27 16:44:35 +0000411 }
412 return glib::Retrieve(value, result);
rspangler@google.com570c65b2009-10-09 20:56:14 +0000413}
414
415// \brief RetrieveProperties returns a HashTable of all properties for the
416// specified interface.
417
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700418BRILLO_EXPORT bool RetrieveProperties(const Proxy& proxy,
419 const char* interface,
420 glib::ScopedHashTable* result);
rspangler@google.com570c65b2009-10-09 20:56:14 +0000421
422// \brief Returns a connection to the system bus.
423
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700424BRILLO_EXPORT BusConnection GetSystemBusConnection();
rspangler@google.com570c65b2009-10-09 20:56:14 +0000425
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900426// \brief Returns a private connection to a bus at |address|.
427
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700428BRILLO_EXPORT BusConnection GetPrivateBusConnection(const char* address);
Yusuke Sato71da5ce2009-12-15 10:29:43 +0900429
Simon Queefb8fe12011-09-21 16:45:31 -0700430// \brief Calls a method |method_name| with no arguments per the given |path|
431// and |interface_name|. Ignores return value.
432
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700433BRILLO_EXPORT void CallMethodWithNoArguments(const char* service_name,
434 const char* path,
435 const char* interface_name,
436 const char* method_name);
Simon Queefb8fe12011-09-21 16:45:31 -0700437
Denis Glotov8a93c902011-02-11 22:32:49 +0300438// \brief Low-level signal monitor base class.
439//
440// Used when there is no definite named signal sender (that Proxy
441// could be used for).
442
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700443class BRILLO_EXPORT SignalWatcher {
Denis Glotov8a93c902011-02-11 22:32:49 +0300444 public:
445 SignalWatcher() {}
446 ~SignalWatcher();
447 void StartMonitoring(const std::string& interface, const std::string& signal);
Denis Glotov8a93c902011-02-11 22:32:49 +0300448
Bertrand SIMONNETb54b6dc2014-07-02 12:13:56 -0700449 private:
Denis Glotov8a93c902011-02-11 22:32:49 +0300450 // Callback invoked on the given signal arrival.
451 virtual void OnSignal(DBusMessage* message) = 0;
452
453 // Returns a string matching the D-Bus messages that we want to listen for.
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700454 BRILLO_PRIVATE std::string GetDBusMatchString() const;
Denis Glotov8a93c902011-02-11 22:32:49 +0300455
456 // A D-Bus message filter to receive signals.
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700457 BRILLO_PRIVATE static DBusHandlerResult FilterDBusMessage(
Alex Vakulenko847b8712014-08-29 10:43:06 -0700458 DBusConnection* dbus_conn,
459 DBusMessage* message,
460 void* data);
Denis Glotov8a93c902011-02-11 22:32:49 +0300461 std::string interface_;
462 std::string signal_;
463};
464
rspangler@google.com570c65b2009-10-09 20:56:14 +0000465} // namespace dbus
Alex Vakulenko9ed0cab2015-10-12 15:21:28 -0700466} // namespace brillo
rspangler@google.com570c65b2009-10-09 20:56:14 +0000467
Alex Vakulenkofed60b02015-10-27 09:53:05 -0700468#endif // LIBBRILLO_BRILLO_GLIB_DBUS_H_