From 4de46e8113001f2251d5a251c6b748510c77ff4d Mon Sep 17 00:00:00 2001 From: Thomas Zimmermann Date: Tue, 9 Jul 2013 09:55:08 +0200 Subject: [PATCH] Bug 884840: Added DBusReplyHandler, r=qdot Handler functions for DBus replies sometimes need several data fields or need to keep state over replies for multiple messages. The DBus API itself only allows for a single pointer to user data. The class DBusReplyHandler is a base class for implementing DBus reply- message handlers. Users of DBus can inherit from this class to implement message-specific handlers. --HG-- extra : rebase_source : 91c5f6e8d365922fd3b52fbaf502052f86274f16 --- ipc/dbus/DBusUtils.cpp | 10 ++++++++++ ipc/dbus/DBusUtils.h | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/ipc/dbus/DBusUtils.cpp b/ipc/dbus/DBusUtils.cpp index 80701010b3ab..0f119234c14c 100644 --- a/ipc/dbus/DBusUtils.cpp +++ b/ipc/dbus/DBusUtils.cpp @@ -647,5 +647,15 @@ int dbus_returns_uint32(DBusMessage *reply) return ret; } +void DBusReplyHandler::Callback(DBusMessage* aReply, void* aData) +{ + MOZ_ASSERT(aData); + + nsRefPtr handler = + already_AddRefed(static_cast(aData)); + + handler->Handle(aReply); +} + } } diff --git a/ipc/dbus/DBusUtils.h b/ipc/dbus/DBusUtils.h index a1ec0ebadb1a..b3f0f018ce68 100644 --- a/ipc/dbus/DBusUtils.h +++ b/ipc/dbus/DBusUtils.h @@ -20,6 +20,7 @@ #define mozilla_ipc_dbus_dbusutils_h__ #include +#include "mozilla/RefPtr.h" #include "mozilla/Scoped.h" // LOGE and free a D-Bus error @@ -50,8 +51,45 @@ private: DBusMessage* mMsg; }; -typedef void (*DBusCallback)(DBusMessage *, void *); +/** + * DBusReplyHandler represents a handler for DBus reply messages. Inherit + * from this class and implement the Handle method. The method Callback + * should be passed to the DBus send function, with the class instance as + * user-data argument. + */ +class DBusReplyHandler : public mozilla::RefCounted +{ +public: + virtual ~DBusReplyHandler() { + } + /** + * Implements a call-back function for DBus. The supplied value for + * aData must be a pointer to an instance of DBusReplyHandler. + */ + static void Callback(DBusMessage* aReply, void* aData); + + /** + * Call-back method for handling the reply message from DBus. + */ + virtual void Handle(DBusMessage* aReply) = 0; + +protected: + DBusReplyHandler() + { + } + + DBusReplyHandler(const DBusReplyHandler& aHandler) + { + } + + DBusReplyHandler& operator = (const DBusReplyHandler& aRhs) + { + return *this; + } +}; + +typedef void (*DBusCallback)(DBusMessage *, void *); void log_and_free_dbus_error(DBusError* err, const char* function,