Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: chromeos/dbus/dbus.cc

Issue 6485006: MonitorSignal low-level wrapper introduced (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/common.git@master
Patch Set: forgotten comments Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« chromeos/dbus/dbus.h ('K') | « chromeos/dbus/dbus.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <base/stringprintf.h>
Daniel Erat 2011/02/10 23:07:24 nit: alphabetize these (technically, i think that
glotov 2011/02/11 16:36:52 Done.
8 #include <base/logging.h>
9 #include <dbus/dbus-glib-bindings.h>
7 #include <dbus/dbus.h> 10 #include <dbus/dbus.h>
8 #include <dbus/dbus-glib-bindings.h>
9 #include <dbus/dbus-glib-lowlevel.h>
10 #include <base/logging.h>
11 11
12 namespace chromeos { 12 namespace chromeos {
13 namespace dbus { 13 namespace dbus {
14 14
15 bool CallPtrArray(const Proxy& proxy, 15 bool CallPtrArray(const Proxy& proxy,
16 const char* method, 16 const char* method,
17 glib::ScopedPtrArray<const char*>* result) { 17 glib::ScopedPtrArray<const char*>* result) {
18 glib::ScopedError error; 18 glib::ScopedError error;
19 19
20 ::GType g_type_array = ::dbus_g_type_get_collection("GPtrArray", 20 ::GType g_type_array = ::dbus_g_type_get_collection("GPtrArray",
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 LOG(ERROR) << "Failed to create a signal: " 249 LOG(ERROR) << "Failed to create a signal: "
250 << "path: " << path << ", " 250 << "path: " << path << ", "
251 << "interface_name: " << interface_name << ", " 251 << "interface_name: " << interface_name << ", "
252 << "signal_name: " << signal_name; 252 << "signal_name: " << signal_name;
253 return; 253 return;
254 } 254 }
255 ::dbus_g_proxy_send(proxy.gproxy(), signal, NULL); 255 ::dbus_g_proxy_send(proxy.gproxy(), signal, NULL);
256 ::dbus_message_unref(signal); 256 ::dbus_message_unref(signal);
257 } 257 }
258 258
259 void MonitorSignal::StartMonitoring(const char* interface,
260 const char* signal) {
261 DCHECK(!interface_) << "StartMonitoring() must be called only once";
262 interface_ = interface;
263 signal_ = signal;
264
265 // Snoop on D-Bus messages so we can get notified about brightness changes.
Daniel Erat 2011/02/10 23:07:24 remove the part about brightness here
glotov 2011/02/11 16:36:52 Done.
266 DBusConnection* dbus_conn = dbus_g_connection_get_connection(
267 GetSystemBusConnection().g_connection());
268 DCHECK(dbus_conn);
269
270 DBusError error;
271 dbus_error_init(&error);
272 dbus_bus_add_match(dbus_conn, GetDBusMatchString().c_str(), &error);
273 if (dbus_error_is_set(&error)) {
274 LOG(DFATAL) << "Got error while adding D-Bus match rule: " << error.name
275 << " (" << error.message << ")";
276 }
277
278 if (!dbus_connection_add_filter(dbus_conn,
279 &MonitorSignal::FilterDBusMessage,
280 this, // user_data
281 NULL)) { // free_data_function
282 LOG(DFATAL) << "Unable to add D-Bus filter";
283 }
284 }
285
286 MonitorSignal::~MonitorSignal() {
287 DBusConnection* dbus_conn = dbus_g_connection_get_connection(
288 dbus::GetSystemBusConnection().g_connection());
289 DCHECK(dbus_conn);
290
291 dbus_connection_remove_filter(dbus_conn,
292 &MonitorSignal::FilterDBusMessage,
293 this);
294
295 DBusError error;
296 dbus_error_init(&error);
297 dbus_bus_remove_match(dbus_conn, GetDBusMatchString().c_str(), &error);
Daniel Erat 2011/02/10 23:07:24 i think that you'll segfault right now if someone
glotov 2011/02/11 16:36:52 Done.
298 if (dbus_error_is_set(&error)) {
299 LOG(DFATAL) << "Got error while removing D-Bus match rule: " << error.name
300 << " (" << error.message << ")";
301 }
302 }
303
304 std::string MonitorSignal::GetDBusMatchString() const {
305 return StringPrintf("type='signal', interface='%s', member='%s'",
306 interface_, signal_);
307 }
308
309 /* static */
310 DBusHandlerResult MonitorSignal::FilterDBusMessage(DBusConnection* dbus_conn,
311 DBusMessage* message,
312 void* data) {
313 MonitorSignal* self = static_cast<MonitorSignal*>(data);
314 if (dbus_message_is_signal(message, self->interface_, self->signal_)) {
315 self->OnSignal(message);
316 return DBUS_HANDLER_RESULT_HANDLED;
317 } else {
318 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
319 }
320 }
321
259 } // namespace dbus 322 } // namespace dbus
260 } // namespace chromeos 323 } // namespace chromeos
OLDNEW
« chromeos/dbus/dbus.h ('K') | « chromeos/dbus/dbus.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698