Bug 1261307: Convert NFC IPC code to |UniquePtr|, r=nfroyd
authorThomas Zimmermann <tdz@users.sourceforge.net>
Tue, 05 Apr 2016 13:40:11 +0200
changeset 347769 26d2542166766085c978adb3bf6130441cbe13c2
parent 347768 6d72a998142a67abdf81082e0b4803d2fa52a503
child 347770 7e2855721acc1389cf1e15ff3110a38b649f4387
push id14654
push userbmo:rail@mozilla.com
push dateTue, 05 Apr 2016 19:30:33 +0000
reviewersnfroyd
bugs1261307
milestone48.0a1
Bug 1261307: Convert NFC IPC code to |UniquePtr|, r=nfroyd
dom/nfc/gonk/NfcService.cpp
dom/nfc/gonk/NfcService.h
--- a/dom/nfc/gonk/NfcService.cpp
+++ b/dom/nfc/gonk/NfcService.cpp
@@ -13,20 +13,20 @@
 #include "mozilla/dom/RootedDictionary.h"
 #include "mozilla/Endian.h"
 #include "mozilla/ipc/ListenSocket.h"
 #include "mozilla/ipc/ListenSocketConsumer.h"
 #include "mozilla/ipc/NfcConnector.h"
 #include "mozilla/ipc/StreamSocket.h"
 #include "mozilla/ipc/StreamSocketConsumer.h"
 #include "mozilla/ModuleUtils.h"
+#include "mozilla/UniquePtr.h"
 #include "mozilla/unused.h"
 #include "NfcMessageHandler.h"
 #include "NfcOptions.h"
-#include "nsAutoPtr.h"
 #include "nsString.h"
 #include "nsXULAppAPI.h"
 
 #define NS_NFCSERVICE_CID \
   { 0x584c9a21, 0x4e17, 0x43b7, {0xb1, 0x6a, 0x87, 0xa0, 0x42, 0xef, 0xd4, 0x64} }
 #define NS_NFCSERVICE_CONTRACTID "@mozilla.org/nfc/service;1"
 
 using namespace android;
@@ -80,17 +80,17 @@ private:
   };
 
   bool IsNfcServiceThread() const;
 
   RefPtr<NfcService> mNfcService;
   nsCOMPtr<nsIThread> mThread;
   RefPtr<mozilla::ipc::ListenSocket> mListenSocket;
   RefPtr<mozilla::ipc::StreamSocket> mStreamSocket;
-  nsAutoPtr<NfcMessageHandler> mHandler;
+  UniquePtr<NfcMessageHandler> mHandler;
   nsCString mListenSocketName;
 };
 
 NfcConsumer::NfcConsumer(NfcService* aNfcService)
   : mNfcService(aNfcService)
 {
   MOZ_ASSERT(mNfcService);
 }
@@ -106,17 +106,17 @@ NfcConsumer::Start()
   // use with |IsNfcServiceThread|.
   mThread = do_GetCurrentThread();
 
   // If we could not cleanup properly before and an old
   // instance of the daemon is still running, we kill it
   // here.
   Unused << NS_WARN_IF(property_set("ctl.stop", "nfcd") < 0);
 
-  mHandler = new NfcMessageHandler();
+  mHandler = MakeUnique<NfcMessageHandler>();
 
   mStreamSocket = new StreamSocket(this, STREAM_SOCKET);
 
   mListenSocketName = BASE_SOCKET_NAME;
 
   mListenSocket = new ListenSocket(this, LISTEN_SOCKET);
   nsresult rv = mListenSocket->Listen(new NfcConnector(mListenSocketName),
                                       mStreamSocket);
@@ -485,32 +485,32 @@ private:
 NS_IMETHODIMP
 NfcService::Start(nsINfcGonkEventListener* aListener)
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(aListener);
   MOZ_ASSERT(!mThread);
   MOZ_ASSERT(!mNfcConsumer);
 
-  nsAutoPtr<NfcConsumer> nfcConsumer(new NfcConsumer(this));
+  auto nfcConsumer = MakeUnique<NfcConsumer>(this);
 
   nsresult rv = NS_NewNamedThread("NfcThread", getter_AddRefs(mThread));
   if (NS_FAILED(rv)) {
     NS_WARNING("Can't create Nfc worker thread.");
     return rv;
   }
 
-  rv = mThread->Dispatch(new StartConsumerRunnable(nfcConsumer),
+  rv = mThread->Dispatch(new StartConsumerRunnable(nfcConsumer.get()),
                          nsIEventTarget::DISPATCH_NORMAL);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   mListener = aListener;
-  mNfcConsumer = nfcConsumer.forget();
+  mNfcConsumer = Move(nfcConsumer);
 
   return NS_OK;
 }
 
 /**
  * |CleanupRunnable| deletes instances of the NFC consumer and
  * thread on the main thread. This has to be down after shutting
  * down the NFC consumer on the NFC thread.
@@ -535,17 +535,17 @@ public:
 
     mThread->Shutdown();
     mThread = nullptr; // deletes NFC worker thread
 
     return NS_OK;
   }
 
 private:
-  nsAutoPtr<NfcConsumer> mNfcConsumer;
+  UniquePtr<NfcConsumer> mNfcConsumer;
   nsCOMPtr<nsIThread> mThread;
 };
 
 /**
  * |ShutdownConsumerRunnable| calls |NfcConsumer::Shutdown| on the
  * NFC thread. Optionally, it can dispatch a |CleanupRunnable| to
  * the main thread for cleaning up the NFC resources.
  */
@@ -578,25 +578,26 @@ private:
 
 NS_IMETHODIMP
 NfcService::Shutdown()
 {
   if (!mNfcConsumer) {
     return NS_OK; // NFC was shut down meanwhile; not an error
   }
 
-  nsresult rv = mThread->Dispatch(new ShutdownConsumerRunnable(mNfcConsumer,
-                                                               true),
-                                  nsIEventTarget::DISPATCH_NORMAL);
+  nsresult rv = mThread->Dispatch(
+    new ShutdownConsumerRunnable(mNfcConsumer.get(), true),
+    nsIEventTarget::DISPATCH_NORMAL);
+
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   // |CleanupRunnable| will take care of these pointers
-  Unused << mNfcConsumer.forget();
+  Unused << mNfcConsumer.release();
   Unused << mThread.forget();
 
   return NS_OK;
 }
 
 /**
  * |SendRunnable| calls |NfcConsumer::Send| on the NFC thread.
  */
@@ -630,19 +631,20 @@ NfcService::SendCommand(JS::HandleValue 
   NfcCommandOptions options;
 
   if (!options.Init(aCx, aOptions)) {
     NS_WARNING("Bad dictionary passed to NfcService::SendCommand");
     return NS_ERROR_FAILURE;
   }
 
   // Dispatch the command to the NFC thread.
-  nsresult rv = mThread->Dispatch(new SendRunnable(mNfcConsumer,
-                                                   CommandOptions(options)),
-                                  nsIEventTarget::DISPATCH_NORMAL);
+  nsresult rv = mThread->Dispatch(
+    new SendRunnable(mNfcConsumer.get(), CommandOptions(options)),
+    nsIEventTarget::DISPATCH_NORMAL);
+
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   return NS_OK;
 }
 
 void
--- a/dom/nfc/gonk/NfcService.h
+++ b/dom/nfc/gonk/NfcService.h
@@ -35,14 +35,14 @@ private:
   class ShutdownConsumerRunnable;
   class StartConsumerRunnable;
 
   NfcService();
   ~NfcService();
 
   nsCOMPtr<nsIThread> mThread;
   nsCOMPtr<nsINfcGonkEventListener> mListener;
-  nsAutoPtr<NfcConsumer> mNfcConsumer;
+  UniquePtr<NfcConsumer> mNfcConsumer;
 };
 
 } // namespace mozilla
 
 #endif // NfcService_h