rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 1 | // 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.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 10 | |
| 11 | #include <algorithm> |
| 12 | #include <string> |
| 13 | |
seanparent@google.com | f2ea024 | 2009-11-04 21:04:51 +0000 | [diff] [blame] | 14 | #include "base/logging.h" |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 15 | #include "chromeos/glib/object.h" |
| 16 | |
| 17 | namespace chromeos { |
| 18 | |
| 19 | // \precondition No functions in the dbus namespace can be called before |
| 20 | // ::g_type_init(); |
| 21 | |
| 22 | namespace 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 | |
| 32 | class BusConnection { |
| 33 | public: |
| 34 | typedef ::DBusGConnection* value_type; |
| 35 | |
| 36 | BusConnection(const BusConnection& x) |
Yusuke Sato | 35371c8 | 2010-06-23 19:04:02 +0900 | [diff] [blame^] | 37 | : object_(x.object_) { |
| 38 | if (object_) |
| 39 | ::dbus_g_connection_ref(object_); |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | ~BusConnection() { |
Yusuke Sato | 35371c8 | 2010-06-23 19:04:02 +0900 | [diff] [blame^] | 43 | if (object_) |
| 44 | ::dbus_g_connection_unref(object_); |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | BusConnection& operator=(BusConnection x) { |
| 48 | swap(*this, x); |
| 49 | return *this; |
| 50 | } |
| 51 | |
Yusuke Sato | f805981 | 2010-01-09 20:13:07 +0900 | [diff] [blame] | 52 | const value_type& g_connection() const { |
| 53 | DCHECK(object_) << "referencing an empty connection"; |
| 54 | return object_; |
| 55 | } |
| 56 | |
Yusuke Sato | 35371c8 | 2010-06-23 19:04:02 +0900 | [diff] [blame^] | 57 | operator bool() const { |
| 58 | return object_; |
| 59 | } |
| 60 | |
| 61 | bool HasConnection() const { |
| 62 | return object_; |
| 63 | } |
| 64 | |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 65 | private: |
| 66 | friend void swap(BusConnection& x, BusConnection& y); |
| 67 | |
| 68 | friend class Proxy; |
| 69 | friend BusConnection GetSystemBusConnection(); |
Yusuke Sato | 71da5ce | 2009-12-15 10:29:43 +0900 | [diff] [blame] | 70 | friend BusConnection GetPrivateBusConnection(const char* address); |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 71 | |
| 72 | // Constructor takes ownership |
| 73 | explicit BusConnection(::DBusGConnection* x) |
| 74 | : object_(x) { |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | value_type object_; |
| 78 | }; |
| 79 | |
| 80 | inline 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 | |
| 91 | class Proxy { |
| 92 | public: |
| 93 | typedef ::DBusGProxy* value_type; |
| 94 | |
| 95 | Proxy() |
| 96 | : object_(NULL) { |
| 97 | } |
| 98 | |
Yusuke Sato | 71da5ce | 2009-12-15 10:29:43 +0900 | [diff] [blame] | 99 | // 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.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 111 | Proxy(const BusConnection& connection, |
| 112 | const char* name, |
| 113 | const char* path, |
| 114 | const char* interface) |
Yusuke Sato | 71da5ce | 2009-12-15 10:29:43 +0900 | [diff] [blame] | 115 | : object_(GetGProxy(connection, name, path, interface, false)) { |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Mitsuru Oshima | 93b3783 | 2010-05-18 14:17:57 -0700 | [diff] [blame] | 118 | // 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.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 125 | 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 Sato | 71da5ce | 2009-12-15 10:29:43 +0900 | [diff] [blame] | 159 | 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 Oshima | 93b3783 | 2010-05-18 14:17:57 -0700 | [diff] [blame] | 165 | static value_type GetGPeerProxy(const BusConnection& connection, |
| 166 | const char* path, |
| 167 | const char* interface); |
| 168 | |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 169 | operator int() const; // for safe bool cast |
| 170 | friend void swap(Proxy& x, Proxy& y); |
| 171 | |
| 172 | value_type object_; |
| 173 | }; |
| 174 | |
| 175 | inline void swap(Proxy& x, Proxy& y) { |
| 176 | std::swap(x.object_, y.object_); |
| 177 | } |
| 178 | |
Chris Masone | c8150f1 | 2010-01-12 12:23:22 -0800 | [diff] [blame] | 179 | // \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 | |
| 190 | bool 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.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 196 | template <typename F> // F is a function signature |
| 197 | class MonitorConnection; |
| 198 | |
| 199 | template <typename A1> |
| 200 | class 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 | |
| 224 | template <typename A1, typename A2> |
| 225 | class 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 | |
| 250 | template <typename A1> |
| 251 | MonitorConnection<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 | |
| 266 | template <typename A1, typename A2> |
| 267 | MonitorConnection<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 | |
| 284 | template <typename F> |
| 285 | void 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 | |
| 303 | bool 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 | |
| 329 | template <typename T> |
| 330 | bool 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.com | 7ec90e9 | 2009-10-27 16:44:35 +0000 | [diff] [blame] | 342 | G_TYPE_INVALID)){ |
Yusuke Sato | 71da5ce | 2009-12-15 10:29:43 +0900 | [diff] [blame] | 343 | LOG(ERROR) << "Getting property failed: " |
| 344 | << (error->message ? error->message : "Unknown Error."); |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 345 | return false; |
| 346 | |
dhg@google.com | 7ec90e9 | 2009-10-27 16:44:35 +0000 | [diff] [blame] | 347 | } |
| 348 | return glib::Retrieve(value, result); |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // \brief RetrieveProperties returns a HashTable of all properties for the |
| 352 | // specified interface. |
| 353 | |
| 354 | bool RetrieveProperties(const Proxy& proxy, |
| 355 | const char* interface, |
| 356 | glib::ScopedHashTable* result); |
| 357 | |
| 358 | // \brief Returns a connection to the system bus. |
| 359 | |
| 360 | BusConnection GetSystemBusConnection(); |
| 361 | |
Yusuke Sato | 71da5ce | 2009-12-15 10:29:43 +0900 | [diff] [blame] | 362 | // \brief Returns a private connection to a bus at |address|. |
| 363 | |
| 364 | BusConnection GetPrivateBusConnection(const char* address); |
| 365 | |
rspangler@google.com | 570c65b | 2009-10-09 20:56:14 +0000 | [diff] [blame] | 366 | } // namespace dbus |
| 367 | } // namespace chromeos |
| 368 | |
| 369 | #endif // CHROMEOS_DBUS_H_ |