Bug 1260672: Use |UniquePtr| for Gonk sensors, r=gsvelto
authorThomas Zimmermann <tdz@users.sourceforge.net>
Wed, 30 Mar 2016 13:18:00 +0200
changeset 291049 90e08b7ccdd35136009da55794590d6a356f6e1a
parent 291048 dc37c0e8f1351a5cd08960a53cfcc9cf753d26dc
child 291050 1fde84b4a3a104907c16073d52383f0c75976341
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgsvelto
bugs1260672
milestone48.0a1
Bug 1260672: Use |UniquePtr| for Gonk sensors, r=gsvelto
hal/gonk/GonkSensorsInterface.cpp
hal/gonk/GonkSensorsInterface.h
hal/gonk/GonkSensorsPollInterface.cpp
hal/gonk/GonkSensorsPollInterface.h
hal/gonk/GonkSensorsRegistryInterface.cpp
hal/gonk/GonkSensorsRegistryInterface.h
--- a/hal/gonk/GonkSensorsInterface.cpp
+++ b/hal/gonk/GonkSensorsInterface.cpp
@@ -254,27 +254,27 @@ GonkSensorsInterface::Connect(GonkSensor
   // here.
   mozilla::hal::StopSystemService("sensorsd");
 
   mNotificationHandler = aNotificationHandler;
 
   mResultHandlerQ.AppendElement(aRes);
 
   if (!mProtocol) {
-    mProtocol = new GonkSensorsProtocol();
+    mProtocol = MakeUnique<GonkSensorsProtocol>();
   }
 
   if (!mListenSocket) {
     mListenSocket = new ListenSocket(this, LISTEN_SOCKET);
   }
 
   // Init, step 1: Listen for data channel... */
 
   if (!mDataSocket) {
-    mDataSocket = new DaemonSocket(mProtocol, this, DATA_SOCKET);
+    mDataSocket = new DaemonSocket(mProtocol.get(), this, DATA_SOCKET);
   } else if (mDataSocket->GetConnectionStatus() == SOCKET_CONNECTED) {
     // Command channel should not be open; let's close it.
     mDataSocket->Close();
   }
 
   // The listen socket's name is generated with a random postfix. This
   // avoids naming collisions if we still have a listen socket from a
   // previously failed cleanup. It also makes it hard for malicious
@@ -329,34 +329,34 @@ GonkSensorsInterface::Disconnect(GonkSen
 
   mResultHandlerQ.AppendElement(aRes);
 }
 
 GonkSensorsRegistryInterface*
 GonkSensorsInterface::GetSensorsRegistryInterface()
 {
   if (mRegistryInterface) {
-    return mRegistryInterface;
+    return mRegistryInterface.get();
   }
 
-  mRegistryInterface = new GonkSensorsRegistryInterface(mProtocol);
+  mRegistryInterface = MakeUnique<GonkSensorsRegistryInterface>(mProtocol.get());
 
-  return mRegistryInterface;
+  return mRegistryInterface.get();
 }
 
 GonkSensorsPollInterface*
 GonkSensorsInterface::GetSensorsPollInterface()
 {
   if (mPollInterface) {
-    return mPollInterface;
+    return mPollInterface.get();
   }
 
-  mPollInterface = new GonkSensorsPollInterface(mProtocol);
+  mPollInterface = MakeUnique<GonkSensorsPollInterface>(mProtocol.get());
 
-  return mPollInterface;
+  return mPollInterface.get();
 }
 
 GonkSensorsInterface::GonkSensorsInterface()
   : mNotificationHandler(nullptr)
 { }
 
 GonkSensorsInterface::~GonkSensorsInterface()
 { }
--- a/hal/gonk/GonkSensorsInterface.h
+++ b/hal/gonk/GonkSensorsInterface.h
@@ -12,16 +12,17 @@
  */
 
 #ifndef hal_gonk_GonkSensorsInterface_h
 #define hal_gonk_GonkSensorsInterface_h
 
 #include <mozilla/ipc/DaemonSocketConsumer.h>
 #include <mozilla/ipc/DaemonSocketMessageHandlers.h>
 #include <mozilla/ipc/ListenSocketConsumer.h>
+#include <mozilla/UniquePtr.h>
 #include "SensorsTypes.h"
 
 namespace mozilla {
 namespace ipc {
 
 class DaemonSocket;
 class ListenSocket;
 
@@ -169,22 +170,22 @@ private:
 
   void OnConnectSuccess(int aIndex) override;
   void OnConnectError(int aIndex) override;
   void OnDisconnect(int aIndex) override;
 
   nsCString mListenSocketName;
   RefPtr<mozilla::ipc::ListenSocket> mListenSocket;
   RefPtr<mozilla::ipc::DaemonSocket> mDataSocket;
-  nsAutoPtr<GonkSensorsProtocol> mProtocol;
+  UniquePtr<GonkSensorsProtocol> mProtocol;
 
   nsTArray<RefPtr<GonkSensorsResultHandler> > mResultHandlerQ;
 
   GonkSensorsNotificationHandler* mNotificationHandler;
 
-  nsAutoPtr<GonkSensorsRegistryInterface> mRegistryInterface;
-  nsAutoPtr<GonkSensorsPollInterface> mPollInterface;
+  UniquePtr<GonkSensorsRegistryInterface> mRegistryInterface;
+  UniquePtr<GonkSensorsPollInterface> mPollInterface;
 };
 
 } // namespace hal
 } // namespace mozilla
 
 #endif // hal_gonk_GonkSensorsInterface_h
--- a/hal/gonk/GonkSensorsPollInterface.cpp
+++ b/hal/gonk/GonkSensorsPollInterface.cpp
@@ -1,16 +1,17 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=2 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "GonkSensorsPollInterface.h"
 #include "HalLog.h"
+#include <mozilla/UniquePtr.h>
 
 namespace mozilla {
 namespace hal {
 
 using namespace mozilla::ipc;
 
 //
 // GonkSensorsPollResultHandler
@@ -119,73 +120,73 @@ GonkSensorsPollModule::HandleSvc(const D
 // Commands
 //
 
 nsresult
 GonkSensorsPollModule::EnableSensorCmd(int32_t aId, GonkSensorsPollResultHandler* aRes)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  nsAutoPtr<DaemonSocketPDU> pdu(
-    new DaemonSocketPDU(SERVICE_ID, OPCODE_ENABLE_SENSOR, 0));
+  UniquePtr<DaemonSocketPDU> pdu =
+    MakeUnique<DaemonSocketPDU>(SERVICE_ID, OPCODE_ENABLE_SENSOR, 0);
 
   nsresult rv = PackPDU(aId, *pdu);
   if (NS_FAILED(rv)) {
     return rv;
   }
-  rv = Send(pdu, aRes);
+  rv = Send(pdu.get(), aRes);
   if (NS_FAILED(rv)) {
     return rv;
   }
-  Unused << pdu.forget();
+  Unused << pdu.release();
   return NS_OK;
 }
 
 nsresult
 GonkSensorsPollModule::DisableSensorCmd(int32_t aId, GonkSensorsPollResultHandler* aRes)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  nsAutoPtr<DaemonSocketPDU> pdu(
-    new DaemonSocketPDU(SERVICE_ID, OPCODE_DISABLE_SENSOR, 0));
+  UniquePtr<DaemonSocketPDU> pdu =
+    MakeUnique<DaemonSocketPDU>(SERVICE_ID, OPCODE_DISABLE_SENSOR, 0);
 
   nsresult rv = PackPDU(aId, *pdu);
   if (NS_FAILED(rv)) {
     return rv;
   }
-  rv = Send(pdu, aRes);
+  rv = Send(pdu.get(), aRes);
   if (NS_FAILED(rv)) {
     return rv;
   }
-  Unused << pdu.forget();
+  Unused << pdu.release();
   return NS_OK;
 }
 
 nsresult
 GonkSensorsPollModule::SetPeriodCmd(int32_t aId, uint64_t aPeriod,
                                 GonkSensorsPollResultHandler* aRes)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  nsAutoPtr<DaemonSocketPDU> pdu(
-    new DaemonSocketPDU(SERVICE_ID, OPCODE_SET_PERIOD, 0));
+  UniquePtr<DaemonSocketPDU> pdu =
+    MakeUnique<DaemonSocketPDU>(SERVICE_ID, OPCODE_SET_PERIOD, 0);
 
   nsresult rv = PackPDU(aId, *pdu);
   if (NS_FAILED(rv)) {
     return rv;
   }
   rv = PackPDU(aPeriod, *pdu);
   if (NS_FAILED(rv)) {
     return rv;
   }
-  rv = Send(pdu, aRes);
+  rv = Send(pdu.get(), aRes);
   if (NS_FAILED(rv)) {
     return rv;
   }
-  Unused << pdu.forget();
+  Unused << pdu.release();
   return NS_OK;
 }
 
 // Responses
 //
 
 void
 GonkSensorsPollModule::ErrorRsp(
--- a/hal/gonk/GonkSensorsPollInterface.h
+++ b/hal/gonk/GonkSensorsPollInterface.h
@@ -268,17 +268,18 @@ private:
 /**
  * This class implements the public interface to the Sensors poll
  * component. Use |SensorsInterface::GetPollInterface| to retrieve
  * an instance. All methods run on the main thread.
  */
 class GonkSensorsPollInterface final
 {
 public:
-  friend class GonkSensorsInterface;
+  GonkSensorsPollInterface(GonkSensorsPollModule* aModule);
+  ~GonkSensorsPollInterface();
 
   /**
    * This method sets the notification handler for poll notifications. Call
    * this method immediately after registering the module. Otherwise you won't
    * be able able to receive poll notifications. You may not free the handler
    * class while the poll component is regsitered.
    *
    * @param aNotificationHandler An instance of a poll notification handler.
@@ -321,21 +322,17 @@ public:
    * the sensor's minimum and maximum period.
    *
    * @param aId The sensor's id.
    * @param aPeriod The sensor's new period.
    * @param aRes The result handler.
    */
   void SetPeriod(int32_t aId, uint64_t aPeriod, GonkSensorsPollResultHandler* aRes);
 
-  ~GonkSensorsPollInterface();
-
 private:
-  GonkSensorsPollInterface(GonkSensorsPollModule* aModule);
-
   void DispatchError(GonkSensorsPollResultHandler* aRes, SensorsError aError);
   void DispatchError(GonkSensorsPollResultHandler* aRes, nsresult aRv);
 
   GonkSensorsPollModule* mModule;
 };
 
 } // hal
 } // namespace mozilla
--- a/hal/gonk/GonkSensorsRegistryInterface.cpp
+++ b/hal/gonk/GonkSensorsRegistryInterface.cpp
@@ -2,16 +2,17 @@
 /* vim: set ts=2 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "GonkSensorsRegistryInterface.h"
 #include "GonkSensorsHelpers.h"
 #include "HalLog.h"
+#include <mozilla/UniquePtr.h>
 
 namespace mozilla {
 namespace hal {
 
 using namespace mozilla::ipc;
 
 //
 // GonkSensorsRegistryResultHandler
@@ -75,49 +76,49 @@ GonkSensorsRegistryModule::HandleSvc(con
 //
 
 nsresult
 GonkSensorsRegistryModule::RegisterModuleCmd(
   uint8_t aId, GonkSensorsRegistryResultHandler* aRes)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  nsAutoPtr<DaemonSocketPDU> pdu(
-    new DaemonSocketPDU(SERVICE_ID, OPCODE_REGISTER_MODULE, 0));
+  UniquePtr<DaemonSocketPDU> pdu =
+    MakeUnique<DaemonSocketPDU>(SERVICE_ID, OPCODE_REGISTER_MODULE, 0);
 
   nsresult rv = PackPDU(aId, *pdu);
   if (NS_FAILED(rv)) {
     return rv;
   }
-  rv = Send(pdu, aRes);
+  rv = Send(pdu.get(), aRes);
   if (NS_FAILED(rv)) {
     return rv;
   }
-  Unused << pdu.forget();
+  Unused << pdu.release();
   return NS_OK;
 }
 
 nsresult
 GonkSensorsRegistryModule::UnregisterModuleCmd(
   uint8_t aId, GonkSensorsRegistryResultHandler* aRes)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  nsAutoPtr<DaemonSocketPDU> pdu(
-    new DaemonSocketPDU(SERVICE_ID, OPCODE_UNREGISTER_MODULE, 0));
+  UniquePtr<DaemonSocketPDU> pdu =
+    MakeUnique<DaemonSocketPDU>(SERVICE_ID, OPCODE_UNREGISTER_MODULE, 0);
 
   nsresult rv = PackPDU(aId, *pdu);
   if (NS_FAILED(rv)) {
     return rv;
   }
-  rv = Send(pdu, aRes);
+  rv = Send(pdu.get(), aRes);
   if (NS_FAILED(rv)) {
     return rv;
   }
-  Unused << pdu.forget();
+  Unused << pdu.release();
   return NS_OK;
 }
 
 // Responses
 //
 
 void
 GonkSensorsRegistryModule::ErrorRsp(
--- a/hal/gonk/GonkSensorsRegistryInterface.h
+++ b/hal/gonk/GonkSensorsRegistryInterface.h
@@ -140,17 +140,18 @@ protected:
 /**
  * This class implements the public interface to the Sensors Registry
  * component. Use |SensorsInterface::GetRegistryInterface| to retrieve
  * an instance. All methods run on the main thread.
  */
 class GonkSensorsRegistryInterface final
 {
 public:
-  friend class GonkSensorsInterface;
+  GonkSensorsRegistryInterface(GonkSensorsRegistryModule* aModule);
+  ~GonkSensorsRegistryInterface();
 
   /**
    * Sends a RegisterModule command to the Sensors daemon. When the
    * result handler's |RegisterModule| method gets called, the service
    * has been registered successfully and can be used.
    *
    * @param aId The id of the service that is to be registered.
    * @param aRes The result handler.
@@ -161,21 +162,17 @@ public:
    * Sends an UnregisterModule command to the Sensors daemon. The service
    * should not be used afterwards until it has been registered again.
    *
    * @param aId The id of the service that is to be unregistered.
    * @param aRes The result handler.
    */
   void UnregisterModule(uint8_t aId, GonkSensorsRegistryResultHandler* aRes);
 
-  ~GonkSensorsRegistryInterface();
-
 private:
-  GonkSensorsRegistryInterface(GonkSensorsRegistryModule* aModule);
-
   void DispatchError(GonkSensorsRegistryResultHandler* aRes,
                      SensorsError aError);
   void DispatchError(GonkSensorsRegistryResultHandler* aRes,
                      nsresult aRv);
 
   GonkSensorsRegistryModule* mModule;
 };