Bug 796184 - Revert UnixSocketImpl to a bare pointer, make Bluetooth*Managers use CloseSocket; r=cjones r=gyeh
authorKyle Machulis <kyle@nonpolynomial.com>
Fri, 05 Oct 2012 16:05:35 -0700
changeset 109510 c9e91088418a2de397e458e63e71fff4e7887bbd
parent 109509 04870d2657db8115444cfa7293b6f803e4c8e639
child 109511 6544d4ed8b80b2962d9675bb8f92270f591243cc
push idunknown
push userunknown
push dateunknown
reviewerscjones, gyeh
bugs796184
milestone18.0a1
Bug 796184 - Revert UnixSocketImpl to a bare pointer, make Bluetooth*Managers use CloseSocket; r=cjones r=gyeh
dom/bluetooth/BluetoothHfpManager.cpp
dom/bluetooth/BluetoothScoManager.cpp
ipc/unixsocket/UnixSocket.cpp
ipc/unixsocket/UnixSocket.h
--- a/dom/bluetooth/BluetoothHfpManager.cpp
+++ b/dom/bluetooth/BluetoothHfpManager.cpp
@@ -197,17 +197,16 @@ BluetoothHfpManager::Get()
     return nullptr;
   }
 
   // Create new instance, register, return
   nsRefPtr<BluetoothHfpManager> manager = new BluetoothHfpManager();
   NS_ENSURE_TRUE(manager, nullptr);
 
   if (!manager->Init()) {
-    manager->Cleanup();
     return nullptr;
   }
 
   gBluetoothHfpManager = manager;
   return gBluetoothHfpManager;
 }
 
 void
@@ -329,17 +328,17 @@ BluetoothHfpManager::HandleVolumeChanged
   return NS_OK;
 }
 
 nsresult
 BluetoothHfpManager::HandleShutdown()
 {
   MOZ_ASSERT(NS_IsMainThread());
   gInShutdown = true;
-  Cleanup();
+  CloseSocket();
   gBluetoothHfpManager = nullptr;
   return NS_OK;
 }
 
 nsresult
 BluetoothHfpManager::Observe(nsISupports* aSubject,
                              const char* aTopic,
                              const PRUnichar* aData)
--- a/dom/bluetooth/BluetoothScoManager.cpp
+++ b/dom/bluetooth/BluetoothScoManager.cpp
@@ -103,17 +103,16 @@ BluetoothScoManager::Get()
     return nullptr;
   }
 
   // Create new instance, register, return
   nsRefPtr<BluetoothScoManager> manager = new BluetoothScoManager();
   NS_ENSURE_TRUE(manager, nullptr);
 
   if (!manager->Init()) {
-    manager->Cleanup();
     return nullptr;
   }
 
   gBluetoothScoManager = manager;
   return gBluetoothScoManager;
 }
 
 nsresult
@@ -137,17 +136,17 @@ BluetoothScoManager::ReceiveSocketData(m
   MOZ_NOT_REACHED("This should never be called!");
 }
 
 nsresult
 BluetoothScoManager::HandleShutdown()
 {
   MOZ_ASSERT(NS_IsMainThread());
   gInShutdown = true;
-  Cleanup();
+  CloseSocket();
   gBluetoothScoManager = nullptr;
   return NS_OK;
 }
 
 bool
 BluetoothScoManager::Connect(const nsAString& aDeviceObjectPath)
 {
   MOZ_ASSERT(NS_IsMainThread());
--- a/ipc/unixsocket/UnixSocket.cpp
+++ b/ipc/unixsocket/UnixSocket.cpp
@@ -113,17 +113,18 @@ public:
                                  true,
                                  MessageLoopForIO::WATCH_READ,
                                  &mReadWatcher,
                                  this);
   }
 
   void PrepareRemoval()
   {
-    mConsumer.forget();
+    mTask = nullptr;
+    mCurrentTaskIsCanceled = true;
   }
 
   /** 
    * Connect to a socket
    */
   void Connect();
 
   /** 
@@ -224,16 +225,17 @@ private:
    * Address we are connecting to, assuming we are creating a client connection.
    */
   nsCString mAddress;
 };
 
 static void
 DestroyImpl(UnixSocketImpl* impl)
 {
+  MOZ_ASSERT(impl);
   delete impl;
 }
 
 class SocketReceiveTask : public nsRunnable
 {
 public:
   SocketReceiveTask(UnixSocketImpl* aImpl, UnixSocketRawData* aData) :
     mImpl(aImpl),
@@ -493,23 +495,25 @@ UnixSocketConsumer::SendSocketData(const
 }
 
 void
 UnixSocketConsumer::CloseSocket()
 {
   if (!mImpl) {
     return;
   }
+  UnixSocketImpl* impl = mImpl;
+  mImpl->mConsumer.forget();
+  mImpl = nullptr;
   // To make sure the owner doesn't die on the IOThread, remove pointer here
-  mImpl->PrepareRemoval();
   // Line it up to be destructed on the IO Thread
   // Kill our pointer to it
   XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
                                    NewRunnableFunction(DestroyImpl,
-                                                       mImpl.forget()));
+                                                       impl));
 }
 
 void
 UnixSocketImpl::OnFileCanReadWithoutBlocking(int aFd)
 {
   // Keep reading data until either
   //
   //   - mIncoming is completely read
--- a/ipc/unixsocket/UnixSocket.h
+++ b/ipc/unixsocket/UnixSocket.h
@@ -168,15 +168,15 @@ public:
    */
   void CloseSocket();
 
   /** 
    * Cancels connect/accept task loop, if one is currently running.
    */
   void CancelSocketTask();
 private:
-  nsAutoPtr<UnixSocketImpl> mImpl;
+  UnixSocketImpl* mImpl;
 };
 
 } // namespace ipc
 } // namepsace mozilla
 
 #endif // mozilla_ipc_Socket_h