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 #include "chromeos/dbus/dbus.h" | 5 #include "chromeos/dbus/dbus.h" |
6 | 6 |
| 7 #include <dbus/dbus-glib-lowlevel.h> |
| 8 #include <dbus/dbus-glib-bindings.h> |
7 #include <dbus/dbus.h> | 9 #include <dbus/dbus.h> |
8 #include <dbus/dbus-glib-bindings.h> | 10 |
9 #include <dbus/dbus-glib-lowlevel.h> | 11 #include "base/logging.h" |
10 #include <base/logging.h> | 12 #include "base/stringprintf.h" |
11 | 13 |
12 namespace chromeos { | 14 namespace chromeos { |
13 namespace dbus { | 15 namespace dbus { |
14 | 16 |
15 bool CallPtrArray(const Proxy& proxy, | 17 bool CallPtrArray(const Proxy& proxy, |
16 const char* method, | 18 const char* method, |
17 glib::ScopedPtrArray<const char*>* result) { | 19 glib::ScopedPtrArray<const char*>* result) { |
18 glib::ScopedError error; | 20 glib::ScopedError error; |
19 | 21 |
20 ::GType g_type_array = ::dbus_g_type_get_collection("GPtrArray", | 22 ::GType g_type_array = ::dbus_g_type_get_collection("GPtrArray", |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 LOG(ERROR) << "Failed to create a signal: " | 251 LOG(ERROR) << "Failed to create a signal: " |
250 << "path: " << path << ", " | 252 << "path: " << path << ", " |
251 << "interface_name: " << interface_name << ", " | 253 << "interface_name: " << interface_name << ", " |
252 << "signal_name: " << signal_name; | 254 << "signal_name: " << signal_name; |
253 return; | 255 return; |
254 } | 256 } |
255 ::dbus_g_proxy_send(proxy.gproxy(), signal, NULL); | 257 ::dbus_g_proxy_send(proxy.gproxy(), signal, NULL); |
256 ::dbus_message_unref(signal); | 258 ::dbus_message_unref(signal); |
257 } | 259 } |
258 | 260 |
| 261 void SignalWatcher::StartMonitoring(const string& interface, |
| 262 const string& signal) { |
| 263 DCHECK(!interface_.empty()) << "StartMonitoring() must be called only once"; |
| 264 interface_ = interface; |
| 265 signal_ = signal; |
| 266 |
| 267 // Snoop on D-Bus messages so we can get notified about signals. |
| 268 DBusConnection* dbus_conn = dbus_g_connection_get_connection( |
| 269 GetSystemBusConnection().g_connection()); |
| 270 DCHECK(dbus_conn); |
| 271 |
| 272 DBusError error; |
| 273 dbus_error_init(&error); |
| 274 dbus_bus_add_match(dbus_conn, GetDBusMatchString().c_str(), &error); |
| 275 if (dbus_error_is_set(&error)) { |
| 276 LOG(DFATAL) << "Got error while adding D-Bus match rule: " << error.name |
| 277 << " (" << error.message << ")"; |
| 278 } |
| 279 |
| 280 if (!dbus_connection_add_filter(dbus_conn, |
| 281 &SignalWatcher::FilterDBusMessage, |
| 282 this, // user_data |
| 283 NULL)) { // free_data_function |
| 284 LOG(DFATAL) << "Unable to add D-Bus filter"; |
| 285 } |
| 286 } |
| 287 |
| 288 SignalWatcher::~SignalWatcher() { |
| 289 if (interface_.empty()) |
| 290 return; |
| 291 |
| 292 DBusConnection* dbus_conn = dbus_g_connection_get_connection( |
| 293 dbus::GetSystemBusConnection().g_connection()); |
| 294 DCHECK(dbus_conn); |
| 295 |
| 296 dbus_connection_remove_filter(dbus_conn, |
| 297 &SignalWatcher::FilterDBusMessage, |
| 298 this); |
| 299 |
| 300 DBusError error; |
| 301 dbus_error_init(&error); |
| 302 dbus_bus_remove_match(dbus_conn, GetDBusMatchString().c_str(), &error); |
| 303 if (dbus_error_is_set(&error)) { |
| 304 LOG(DFATAL) << "Got error while removing D-Bus match rule: " << error.name |
| 305 << " (" << error.message << ")"; |
| 306 } |
| 307 } |
| 308 |
| 309 std::string SignalWatcher::GetDBusMatchString() const { |
| 310 return StringPrintf("type='signal', interface='%s', member='%s'", |
| 311 interface_.c_str(), signal_.c_str()); |
| 312 } |
| 313 |
| 314 /* static */ |
| 315 DBusHandlerResult SignalWatcher::FilterDBusMessage(DBusConnection* dbus_conn, |
| 316 DBusMessage* message, |
| 317 void* data) { |
| 318 SignalWatcher* self = static_cast<SignalWatcher*>(data); |
| 319 if (dbus_message_is_signal(message, self->interface_, self->signal_)) { |
| 320 self->OnSignal(message); |
| 321 return DBUS_HANDLER_RESULT_HANDLED; |
| 322 } else { |
| 323 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 324 } |
| 325 } |
| 326 |
259 } // namespace dbus | 327 } // namespace dbus |
260 } // namespace chromeos | 328 } // namespace chromeos |
OLD | NEW |