Bug 1266054: GTK+: Hold references to |DBusConnection| in |RefPtr|, r=karlt
This patch changes |WakeLockListener| to store its connection to
the DBus session bus in an instance of |RefPtr|. The reference will
be released automatically from the class' destructor.
MozReview-Commit-ID: Hv6MgpJMNLI
--- a/widget/gtk/WakeLockListener.cpp
+++ b/widget/gtk/WakeLockListener.cpp
@@ -59,17 +59,17 @@ private:
bool SendGNOMEInhibitMessage();
bool SendMessage(DBusMessage* aMessage);
static void ReceiveInhibitReply(DBusPendingCall* aPending, void* aUserData);
void InhibitFailed();
void InhibitSucceeded(uint32_t aInhibitRequest);
nsCString mTopic;
- DBusConnection* mConnection;
+ RefPtr<DBusConnection> mConnection;
DesktopEnvironment mDesktopEnvironment;
uint32_t mInhibitRequest;
bool mShouldInhibit;
bool mWaitingForReply;
};
@@ -307,31 +307,25 @@ WakeLockTopic::ReceiveInhibitReply(DBusP
self->InhibitFailed();
}
dbus_message_unref(msg);
}
WakeLockListener::WakeLockListener()
- : mConnection(dbus_bus_get(DBUS_BUS_SESSION, nullptr))
+ : mConnection(already_AddRefed<DBusConnection>(
+ dbus_bus_get(DBUS_BUS_SESSION, nullptr)))
{
if (mConnection) {
dbus_connection_set_exit_on_disconnect(mConnection, false);
dbus_connection_setup_with_g_main(mConnection, nullptr);
}
}
-WakeLockListener::~WakeLockListener()
-{
- if (mConnection) {
- dbus_connection_unref(mConnection);
- }
-}
-
/* static */ WakeLockListener*
WakeLockListener::GetSingleton(bool aCreate)
{
if (!sSingleton && aCreate) {
sSingleton = new WakeLockListener();
sSingleton->AddRef();
}
--- a/widget/gtk/WakeLockListener.h
+++ b/widget/gtk/WakeLockListener.h
@@ -9,18 +9,18 @@
#ifndef __WakeLockListener_h__
#define __WakeLockListener_h__
#include "nsHashKeys.h"
#include "nsClassHashtable.h"
#include "nsIDOMWakeLockListener.h"
+#include "mozilla/ipc/DBusConnectionRefPtr.h"
-struct DBusConnection;
class WakeLockTopic;
/**
* Receives WakeLock events and simply passes it on to the right WakeLockTopic
* to inhibit the screensaver.
*/
class WakeLockListener final : public nsIDOMMozWakeLockListener
{
@@ -30,19 +30,19 @@ public:
static WakeLockListener* GetSingleton(bool aCreate = true);
static void Shutdown();
virtual nsresult Callback(const nsAString& topic,
const nsAString& state) override;
private:
WakeLockListener();
- ~WakeLockListener();
+ ~WakeLockListener() = default;
static WakeLockListener* sSingleton;
- DBusConnection* mConnection;
+ RefPtr<DBusConnection> mConnection;
// Map of topic names to |WakeLockTopic|s.
// We assume a small, finite-sized set of topics.
nsClassHashtable<nsStringHashKey, WakeLockTopic> mTopics;
};
#endif // __WakeLockListener_h__