Bug 851046: Patch 8 - Add BluetoothSocket* as an argument of callback functions in BluetoothSocketObserver; r=mrbkap
authorEric Chou <echou@mozilla.com>
Thu, 04 Apr 2013 17:55:55 -0700
changeset 127747 1246e3291a04bd98739022ec0daf333e3233ed03
parent 127746 26f388aeced914f847ec06858156fc20e7085960
child 127748 b5f2678e1cc8c5f55bb51ef7e59514f00009afb8
push id24512
push userryanvm@gmail.com
push dateFri, 05 Apr 2013 20:13:49 +0000
treeherdermozilla-central@139b6ba547fa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs851046
milestone23.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 851046: Patch 8 - Add BluetoothSocket* as an argument of callback functions in BluetoothSocketObserver; r=mrbkap
dom/bluetooth/BluetoothHfpManager.cpp
dom/bluetooth/BluetoothHfpManager.h
dom/bluetooth/BluetoothOppManager.cpp
dom/bluetooth/BluetoothOppManager.h
dom/bluetooth/BluetoothScoManager.cpp
dom/bluetooth/BluetoothScoManager.h
dom/bluetooth/BluetoothSocket.cpp
dom/bluetooth/BluetoothSocketObserver.h
--- a/dom/bluetooth/BluetoothHfpManager.cpp
+++ b/dom/bluetooth/BluetoothHfpManager.cpp
@@ -668,17 +668,18 @@ BluetoothHfpManager::HandleShutdown()
   gInShutdown = true;
   Disconnect();
   gBluetoothHfpManager = nullptr;
   return NS_OK;
 }
 
 // Virtual function of class SocketConsumer
 void
-BluetoothHfpManager::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage)
+BluetoothHfpManager::ReceiveSocketData(BluetoothSocket* aSocket,
+                                       nsAutoPtr<UnixSocketRawData>& aMessage)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   nsAutoCString msg((const char*)aMessage->mData.get(), aMessage->mSize);
   msg.StripWhitespace();
 
   nsTArray<nsCString> atCommandValues;
 
@@ -1300,18 +1301,20 @@ BluetoothHfpManager::HandleCallStateChan
       NS_WARNING("Not handling state changed");
       sCINDItems[CINDType::CALL].value = CallState::NO_CALL;
       sCINDItems[CINDType::CALLSETUP].value = CallSetupState::NO_CALLSETUP;
       sCINDItems[CINDType::CALLHELD].value = CallHeldState::NO_CALLHELD;
   }
 }
 
 void
-BluetoothHfpManager::OnConnectSuccess()
+BluetoothHfpManager::OnConnectSuccess(BluetoothSocket* aSocket)
 {
+  MOZ_ASSERT(aSocket == mSocket);
+
   nsCOMPtr<nsITelephonyProvider> provider =
     do_GetService(NS_RILCONTENTHELPER_CONTRACTID);
   NS_ENSURE_TRUE_VOID(provider);
   provider->EnumerateCalls(mListener->GetListener());
 
   // For active connection request, we need to reply the DOMRequest
   if (mRunnable) {
     BluetoothValue v = true;
@@ -1325,35 +1328,39 @@ BluetoothHfpManager::OnConnectSuccess()
   // when a headset disconnect with us
   mSocket->GetAddress(mDevicePath);
   mSocketStatus = mSocket->GetConnectionStatus();
 
   NotifySettings();
 }
 
 void
-BluetoothHfpManager::OnConnectError()
+BluetoothHfpManager::OnConnectError(BluetoothSocket* aSocket)
 {
+  MOZ_ASSERT(aSocket == mSocket);
+
   // For active connection request, we need to reply the DOMRequest
   if (mRunnable) {
     BluetoothValue v;
     nsString errorStr;
     errorStr.AssignLiteral("Failed to connect with a bluetooth headset!");
     DispatchBluetoothReply(mRunnable, v, errorStr);
 
     mRunnable.forget();
   }
 
   // If connecting for some reason didn't work, restart listening
   Listen();
 }
 
 void
-BluetoothHfpManager::OnDisconnect()
+BluetoothHfpManager::OnDisconnect(BluetoothSocket* aSocket)
 {
+  MOZ_ASSERT(aSocket == mSocket);
+
   // When we close a connected socket, then restart listening again and
   // notify Settings app.
   if (mSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) {
     Listen();
     NotifySettings();
   } else if (mSocketStatus == SocketConnectionStatus::SOCKET_CONNECTING) {
     NS_WARNING("BluetoothHfpManager got unexpected socket status!");
   }
--- a/dom/bluetooth/BluetoothHfpManager.h
+++ b/dom/bluetooth/BluetoothHfpManager.h
@@ -52,20 +52,21 @@ enum BluetoothCmeError {
 
 class BluetoothHfpManager : public BluetoothSocketObserver
 {
 public:
   static BluetoothHfpManager* Get();
   ~BluetoothHfpManager();
 
   virtual void ReceiveSocketData(
+    BluetoothSocket* aSocket,
     nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage) MOZ_OVERRIDE;
-  virtual void OnConnectSuccess() MOZ_OVERRIDE;
-  virtual void OnConnectError() MOZ_OVERRIDE;
-  virtual void OnDisconnect() MOZ_OVERRIDE;
+  virtual void OnConnectSuccess(BluetoothSocket* aSocket) MOZ_OVERRIDE;
+  virtual void OnConnectError(BluetoothSocket* aSocket) MOZ_OVERRIDE;
+  virtual void OnDisconnect(BluetoothSocket* aSocket) MOZ_OVERRIDE;
 
   bool Connect(const nsAString& aDeviceObjectPath,
                const bool aIsHandsfree,
                BluetoothReplyRunnable* aRunnable);
   void Disconnect();
   bool Listen();
 
   /**
--- a/dom/bluetooth/BluetoothOppManager.cpp
+++ b/dom/bluetooth/BluetoothOppManager.cpp
@@ -923,17 +923,18 @@ BluetoothOppManager::ClientDataHandler(U
     }
   } else {
     NS_WARNING("Unhandled ObexRequestCode");
   }
 }
 
 // Virtual function of class SocketConsumer
 void
-BluetoothOppManager::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage)
+BluetoothOppManager::ReceiveSocketData(BluetoothSocket* aSocket,
+                                       nsAutoPtr<UnixSocketRawData>& aMessage)
 {
   if (mLastCommand) {
     ClientDataHandler(aMessage);
     return;
   }
 
   ServerDataHandler(aMessage);
 }
@@ -1297,36 +1298,40 @@ BluetoothOppManager::ReceivingFileConfir
 
   if (!BroadcastSystemMessage(type, parameters)) {
     NS_WARNING("Failed to send [bluetooth-opp-receiving-file-confirmation]");
     return;
   }
 }
 
 void
-BluetoothOppManager::OnConnectSuccess()
+BluetoothOppManager::OnConnectSuccess(BluetoothSocket* aSocket)
 {
+  MOZ_ASSERT(aSocket == mSocket);
+
   if (mRunnable) {
     BluetoothReply* reply = new BluetoothReply(BluetoothReplySuccess(true));
     mRunnable->SetReply(reply);
     if (NS_FAILED(NS_DispatchToMainThread(mRunnable))) {
       NS_WARNING("Failed to dispatch to main thread!");
     }
     mRunnable.forget();
   }
 
   // Cache device address since we can't get socket address when a remote
   // device disconnect with us.
   mSocket->GetAddress(mConnectedDeviceAddress);
   mSocketStatus = mSocket->GetConnectionStatus();
 }
 
 void
-BluetoothOppManager::OnConnectError()
+BluetoothOppManager::OnConnectError(BluetoothSocket* aSocket)
 {
+  MOZ_ASSERT(aSocket == mSocket);
+
   if (mRunnable) {
     nsString errorStr;
     errorStr.AssignLiteral("Failed to connect with a bluetooth opp manager!");
     BluetoothReply* reply = new BluetoothReply(BluetoothReplyError(errorStr));
     mRunnable->SetReply(reply);
     if (NS_FAILED(NS_DispatchToMainThread(mRunnable))) {
       NS_WARNING("Failed to dispatch to main thread!");
     }
@@ -1334,18 +1339,20 @@ BluetoothOppManager::OnConnectError()
   }
 
   mSocket->Disconnect();
   mSocketStatus = mSocket->GetConnectionStatus();
   Listen();
 }
 
 void
-BluetoothOppManager::OnDisconnect()
+BluetoothOppManager::OnDisconnect(BluetoothSocket* aSocket)
 {
+  MOZ_ASSERT(aSocket == mSocket);
+
   /**
    * It is valid for a bluetooth device which is transfering file via OPP
    * closing socket without sending OBEX disconnect request first. So we
    * delete the broken file when we failed to receive a file from the remote,
    * and notify the transfer has been completed (but failed). We also call
    * AfterOppDisconnected here to ensure all variables will be cleaned.
    */
   if (mSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) {
--- a/dom/bluetooth/BluetoothOppManager.h
+++ b/dom/bluetooth/BluetoothOppManager.h
@@ -69,18 +69,23 @@ public:
   bool ExtractBlobHeaders();
   nsresult HandleShutdown();
 
   // Return true if there is an ongoing file-transfer session, please see
   // Bug 827267 for more information.
   bool IsTransferring();
 
   // Implement interface BluetoothSocketObserver
-  void ReceiveSocketData(nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage)
-    MOZ_OVERRIDE;
+  void ReceiveSocketData(
+    BluetoothSocket* aSocket,
+    nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage) MOZ_OVERRIDE;
+
+  virtual void OnConnectSuccess(BluetoothSocket* aSocket) MOZ_OVERRIDE;
+  virtual void OnConnectError(BluetoothSocket* aSocket) MOZ_OVERRIDE;
+  virtual void OnDisconnect(BluetoothSocket* aSocket) MOZ_OVERRIDE;
   void OnConnectSuccess() MOZ_OVERRIDE;
   void OnConnectError() MOZ_OVERRIDE;
   void OnDisconnect() MOZ_OVERRIDE;
 
 private:
   BluetoothOppManager();
   void StartFileTransfer();
   void FileTransferComplete();
--- a/dom/bluetooth/BluetoothScoManager.cpp
+++ b/dom/bluetooth/BluetoothScoManager.cpp
@@ -168,17 +168,18 @@ BluetoothScoManager::Get()
   NS_ENSURE_TRUE(manager->Init(), nullptr);
 
   gBluetoothScoManager = manager;
   return gBluetoothScoManager;
 }
 
 // Virtual function of class SocketConsumer
 void
-BluetoothScoManager::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage)
+BluetoothScoManager::ReceiveSocketData(BluetoothSocket* aSocket,
+                                       nsAutoPtr<UnixSocketRawData>& aMessage)
 {
   // SCO socket do nothing here
   MOZ_NOT_REACHED("This should never be called!");
 }
 
 nsresult
 BluetoothScoManager::HandleShutdown()
 {
@@ -250,35 +251,41 @@ BluetoothScoManager::Listen()
 
 void
 BluetoothScoManager::Disconnect()
 {
   mSocket->Disconnect();
 }
 
 void
-BluetoothScoManager::OnConnectSuccess()
+BluetoothScoManager::OnConnectSuccess(BluetoothSocket* aSocket)
 {
+  MOZ_ASSERT(aSocket == mSocket);
+
   nsString address;
   mSocket->GetAddress(address);
   NotifyAudioManager(address);
 
   mSocketStatus = mSocket->GetConnectionStatus();
 }
 
 void
-BluetoothScoManager::OnConnectError()
+BluetoothScoManager::OnConnectError(BluetoothSocket* aSocket)
 {
+  MOZ_ASSERT(aSocket == mSocket);
+
   mSocket->Disconnect();
   mSocketStatus = mSocket->GetConnectionStatus();
   Listen();
 }
 
 void
-BluetoothScoManager::OnDisconnect()
+BluetoothScoManager::OnDisconnect(BluetoothSocket* aSocket)
 {
+  MOZ_ASSERT(aSocket == mSocket);
+
   if (mSocketStatus == SocketConnectionStatus::SOCKET_CONNECTED) {
     Listen();
 
     nsString address = NS_LITERAL_STRING("");
     NotifyAudioManager(address);
   }
 }
--- a/dom/bluetooth/BluetoothScoManager.h
+++ b/dom/bluetooth/BluetoothScoManager.h
@@ -19,21 +19,21 @@ class BluetoothSocket;
 
 class BluetoothScoManager : public BluetoothSocketObserver
 {
 public:
   static BluetoothScoManager* Get();
   ~BluetoothScoManager();
 
   virtual void ReceiveSocketData(
-                 nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage)
-                 MOZ_OVERRIDE;
-  virtual void OnConnectSuccess() MOZ_OVERRIDE;
-  virtual void OnConnectError() MOZ_OVERRIDE;
-  virtual void OnDisconnect() MOZ_OVERRIDE;
+    BluetoothSocket* aSocket,
+    nsAutoPtr<mozilla::ipc::UnixSocketRawData>& aMessage) MOZ_OVERRIDE;
+  virtual void OnConnectSuccess(BluetoothSocket* aSocket) MOZ_OVERRIDE;
+  virtual void OnConnectError(BluetoothSocket* aSocket) MOZ_OVERRIDE;
+  virtual void OnDisconnect(BluetoothSocket* aSocket) MOZ_OVERRIDE;
 
   bool Connect(const nsAString& aDeviceObjectPath);
   void Disconnect();
   bool Listen();
 
 private:
   friend class BluetoothScoManagerObserver;
   BluetoothScoManager();
--- a/dom/bluetooth/BluetoothSocket.cpp
+++ b/dom/bluetooth/BluetoothSocket.cpp
@@ -64,35 +64,35 @@ BluetoothSocket::Listen(int aChannel)
   return true;
 }
 
 void
 BluetoothSocket::ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage)
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(mObserver);
-  mObserver->ReceiveSocketData(aMessage);
+  mObserver->ReceiveSocketData(this, aMessage);
 }
 
 void
 BluetoothSocket::OnConnectSuccess()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(mObserver);
-  mObserver->OnConnectSuccess();
+  mObserver->OnConnectSuccess(this);
 }
 
 void
 BluetoothSocket::OnConnectError()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(mObserver);
-  mObserver->OnConnectError();
+  mObserver->OnConnectError(this);
 }
 
 void
 BluetoothSocket::OnDisconnect()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(mObserver);
-  mObserver->OnDisconnect();
+  mObserver->OnDisconnect(this);
 }
 
--- a/dom/bluetooth/BluetoothSocketObserver.h
+++ b/dom/bluetooth/BluetoothSocketObserver.h
@@ -9,20 +9,41 @@
 
 #include "BluetoothCommon.h"
 #include <mozilla/ipc/UnixSocket.h>
 
 using namespace mozilla::ipc;
 
 BEGIN_BLUETOOTH_NAMESPACE
 
+class BluetoothSocket;
+
 class BluetoothSocketObserver
 {
 public:
-  virtual void ReceiveSocketData(nsAutoPtr<UnixSocketRawData>& aMessage) = 0;
-  virtual void OnConnectSuccess() = 0;
-  virtual void OnConnectError() = 0;
-  virtual void OnDisconnect() = 0;
+  virtual void ReceiveSocketData(BluetoothSocket* aSocket,
+                                 nsAutoPtr<UnixSocketRawData>& aMessage) = 0;
+ 
+   /**
+    * A callback function which would be called when a socket connection
+    * is established successfully. To be more specific, this would be called
+    * when socket state changes from CONNECTING/LISTENING to CONNECTED.
+    */
+  virtual void OnConnectSuccess(BluetoothSocket* aSocket) = 0;
+ 
+   /**
+    * A callback function which would be called when BluetoothSocket::Connect()
+    * fails.
+    */
+  virtual void OnConnectError(BluetoothSocket* aSocket) = 0;
+ 
+   /**
+    * A callback function which would be called when a socket connection
+    * is dropped. To be more specific, this would be called when socket state
+    * changes from CONNECTED/LISTENING to DISCONNECTED.
+    */
+  virtual void OnDisconnect(BluetoothSocket* aSocket) = 0;
+
 };
 
 END_BLUETOOTH_NAMESPACE
 
 #endif