OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMEOS_DBUS_H_ | 5 #ifndef CHROMEOS_DBUS_H_ |
6 #define CHROMEOS_DBUS_H_ | 6 #define CHROMEOS_DBUS_H_ |
7 | 7 |
8 #include <dbus/dbus-glib.h> | 8 #include <dbus/dbus-glib.h> |
9 #include <glib-object.h> | 9 #include <glib-object.h> |
10 | 10 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 BusConnection& operator=(BusConnection x) { | 44 BusConnection& operator=(BusConnection x) { |
45 swap(*this, x); | 45 swap(*this, x); |
46 return *this; | 46 return *this; |
47 } | 47 } |
48 | 48 |
49 private: | 49 private: |
50 friend void swap(BusConnection& x, BusConnection& y); | 50 friend void swap(BusConnection& x, BusConnection& y); |
51 | 51 |
52 friend class Proxy; | 52 friend class Proxy; |
53 friend BusConnection GetSystemBusConnection(); | 53 friend BusConnection GetSystemBusConnection(); |
| 54 friend BusConnection GetPrivateBusConnection(const char* address); |
54 | 55 |
55 // Constructor takes ownership | 56 // Constructor takes ownership |
56 explicit BusConnection(::DBusGConnection* x) | 57 explicit BusConnection(::DBusGConnection* x) |
57 : object_(x) { | 58 : object_(x) { |
58 DCHECK(object_) << "Constructing BusConnection with NULL object."; | 59 DCHECK(object_) << "Constructing BusConnection with NULL object."; |
59 } | 60 } |
60 | 61 |
61 value_type object_; | 62 value_type object_; |
62 }; | 63 }; |
63 | 64 |
64 inline void swap(BusConnection& x, BusConnection& y) { | 65 inline void swap(BusConnection& x, BusConnection& y) { |
65 std::swap(x.object_, y.object_); | 66 std::swap(x.object_, y.object_); |
66 } | 67 } |
67 | 68 |
68 // \brief Proxy manages the ref-count for a ::DBusGProxy*. | 69 // \brief Proxy manages the ref-count for a ::DBusGProxy*. |
69 // | 70 // |
70 // Proxy has reference semantics and represents a connection to on object on | 71 // Proxy has reference semantics and represents a connection to on object on |
71 // the bus. A proxy object is constructed with a connection to a bus, a name | 72 // the bus. A proxy object is constructed with a connection to a bus, a name |
72 // to an entity on the bus, a path to an object owned by the entity, and an | 73 // to an entity on the bus, a path to an object owned by the entity, and an |
73 // interface protocol name used to communicate with the object. | 74 // interface protocol name used to communicate with the object. |
74 | 75 |
75 class Proxy { | 76 class Proxy { |
76 public: | 77 public: |
77 typedef ::DBusGProxy* value_type; | 78 typedef ::DBusGProxy* value_type; |
78 | 79 |
79 Proxy() | 80 Proxy() |
80 : object_(NULL) { | 81 : object_(NULL) { |
81 } | 82 } |
82 | 83 |
| 84 // Set |connect_to_name_owner| true if you'd like to use |
| 85 // dbus_g_proxy_new_for_name_owner() rather than dbus_g_proxy_new_for_name(). |
| 86 Proxy(const BusConnection& connection, |
| 87 const char* name, |
| 88 const char* path, |
| 89 const char* interface, |
| 90 bool connect_to_name_owner) |
| 91 : object_(GetGProxy( |
| 92 connection, name, path, interface, connect_to_name_owner)) { |
| 93 } |
| 94 |
| 95 // Equivalent to Proxy(connection, name, path, interface, false). |
83 Proxy(const BusConnection& connection, | 96 Proxy(const BusConnection& connection, |
84 const char* name, | 97 const char* name, |
85 const char* path, | 98 const char* path, |
86 const char* interface) | 99 const char* interface) |
87 : object_(::dbus_g_proxy_new_for_name(connection.object_, name, path, | 100 : object_(GetGProxy(connection, name, path, interface, false)) { |
88 interface)) { | |
89 DCHECK(object_) << "Failed to construct proxy."; | |
90 } | 101 } |
91 | 102 |
92 Proxy(const Proxy& x) | 103 Proxy(const Proxy& x) |
93 : object_(x.object_) { | 104 : object_(x.object_) { |
94 if (object_) | 105 if (object_) |
95 ::g_object_ref(object_); | 106 ::g_object_ref(object_); |
96 } | 107 } |
97 | 108 |
98 ~Proxy() { | 109 ~Proxy() { |
99 if (object_) | 110 if (object_) |
(...skipping 16 matching lines...) Expand all Loading... |
116 const value_type& gproxy() const { | 127 const value_type& gproxy() const { |
117 DCHECK(object_) << "referencing an empty proxy"; | 128 DCHECK(object_) << "referencing an empty proxy"; |
118 return object_; | 129 return object_; |
119 } | 130 } |
120 | 131 |
121 operator bool() const { | 132 operator bool() const { |
122 return object_; | 133 return object_; |
123 } | 134 } |
124 | 135 |
125 private: | 136 private: |
| 137 static value_type GetGProxy(const BusConnection& connection, |
| 138 const char* name, |
| 139 const char* path, |
| 140 const char* interface, |
| 141 bool connect_to_name_owner); |
| 142 |
126 operator int() const; // for safe bool cast | 143 operator int() const; // for safe bool cast |
127 friend void swap(Proxy& x, Proxy& y); | 144 friend void swap(Proxy& x, Proxy& y); |
128 | 145 |
129 value_type object_; | 146 value_type object_; |
130 }; | 147 }; |
131 | 148 |
132 inline void swap(Proxy& x, Proxy& y) { | 149 inline void swap(Proxy& x, Proxy& y) { |
133 std::swap(x.object_, y.object_); | 150 std::swap(x.object_, y.object_); |
134 } | 151 } |
135 | 152 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 T* result) { | 290 T* result) { |
274 glib::ScopedError error; | 291 glib::ScopedError error; |
275 glib::Value value; | 292 glib::Value value; |
276 | 293 |
277 if (!::dbus_g_proxy_call(proxy.gproxy(), "Get", &Resetter(&error).lvalue(), | 294 if (!::dbus_g_proxy_call(proxy.gproxy(), "Get", &Resetter(&error).lvalue(), |
278 G_TYPE_STRING, interface, | 295 G_TYPE_STRING, interface, |
279 G_TYPE_STRING, property, | 296 G_TYPE_STRING, property, |
280 G_TYPE_INVALID, | 297 G_TYPE_INVALID, |
281 G_TYPE_VALUE, &value, | 298 G_TYPE_VALUE, &value, |
282 G_TYPE_INVALID)){ | 299 G_TYPE_INVALID)){ |
283 LOG(ERROR) << "Getting property failed: " << (error->message ? error->messag
e : "Unknown Error."); | 300 LOG(ERROR) << "Getting property failed: " |
| 301 << (error->message ? error->message : "Unknown Error."); |
284 return false; | 302 return false; |
285 | 303 |
286 } | 304 } |
287 return glib::Retrieve(value, result); | 305 return glib::Retrieve(value, result); |
288 } | 306 } |
289 | 307 |
290 // \brief RetrieveProperties returns a HashTable of all properties for the | 308 // \brief RetrieveProperties returns a HashTable of all properties for the |
291 // specified interface. | 309 // specified interface. |
292 | 310 |
293 bool RetrieveProperties(const Proxy& proxy, | 311 bool RetrieveProperties(const Proxy& proxy, |
294 const char* interface, | 312 const char* interface, |
295 glib::ScopedHashTable* result); | 313 glib::ScopedHashTable* result); |
296 | 314 |
297 // \brief Returns a connection to the system bus. | 315 // \brief Returns a connection to the system bus. |
298 | 316 |
299 BusConnection GetSystemBusConnection(); | 317 BusConnection GetSystemBusConnection(); |
300 | 318 |
| 319 // \brief Returns a private connection to a bus at |address|. |
| 320 |
| 321 BusConnection GetPrivateBusConnection(const char* address); |
| 322 |
301 } // namespace dbus | 323 } // namespace dbus |
302 } // namespace chromeos | 324 } // namespace chromeos |
303 | 325 |
304 #endif // CHROMEOS_DBUS_H_ | 326 #endif // CHROMEOS_DBUS_H_ |
OLD | NEW |