Bug 1266054: GTK+: Hold references to |DBusConnection| in |RefPtr|, r=karlt draft
authorThomas Zimmermann <tdz@users.sourceforge.net>
Mon, 09 May 2016 11:36:37 +0200
changeset 364789 e6fe1e3d39bccc97745cb09f2497402bee1f5dab
parent 364682 bae525a694e2dc0aa433885be8751330d4995a49
child 364790 a37acd8d13717bb0c418069173cb6ed169fe2b67
push id17564
push usertdz@users.sourceforge.net
push dateMon, 09 May 2016 10:27:13 +0000
reviewerskarlt
bugs1266054
milestone49.0a1
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
widget/gtk/WakeLockListener.cpp
widget/gtk/WakeLockListener.h
--- 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__