1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_DBusHelpers_h
8 #define mozilla_DBusHelpers_h
10 #include <dbus/dbus.h>
11 #include "mozilla/UniquePtr.h"
12 #include "mozilla/RefPtr.h"
17 struct RefPtrTraits
<DBusMessage
> {
18 static void AddRef(DBusMessage
* aMessage
) {
20 dbus_message_ref(aMessage
);
22 static void Release(DBusMessage
* aMessage
) {
24 dbus_message_unref(aMessage
);
29 struct RefPtrTraits
<DBusPendingCall
> {
30 static void AddRef(DBusPendingCall
* aPendingCall
) {
31 MOZ_ASSERT(aPendingCall
);
32 dbus_pending_call_ref(aPendingCall
);
34 static void Release(DBusPendingCall
* aPendingCall
) {
35 MOZ_ASSERT(aPendingCall
);
36 dbus_pending_call_unref(aPendingCall
);
41 * |RefPtrTraits<DBusConnection>| specializes |RefPtrTraits<>|
42 * for managing |DBusConnection| with |RefPtr|.
44 * |RefPtrTraits<DBusConnection>| will _not_ close the DBus
45 * connection upon the final unref. The caller is responsible
46 * for closing the connection.
49 struct RefPtrTraits
<DBusConnection
> {
50 static void AddRef(DBusConnection
* aConnection
) {
51 MOZ_ASSERT(aConnection
);
52 dbus_connection_ref(aConnection
);
54 static void Release(DBusConnection
* aConnection
) {
55 MOZ_ASSERT(aConnection
);
56 dbus_connection_unref(aConnection
);
61 * |DBusConnectionDelete| is a deleter for managing instances
62 * of |DBusConnection| in |UniquePtr|. Upon destruction, it
63 * will close an open connection before unref'ing the data
66 * Do not use |UniquePtr| with shared DBus connections. For
67 * shared connections, use |RefPtr|.
69 class DBusConnectionDelete
{
71 constexpr DBusConnectionDelete() {}
73 void operator()(DBusConnection
* aConnection
) const {
74 MOZ_ASSERT(aConnection
);
75 if (dbus_connection_get_is_connected(aConnection
)) {
76 dbus_connection_close(aConnection
);
78 dbus_connection_unref(aConnection
);
82 } // namespace mozilla
84 #endif // mozilla_DBusHelpers_h