Bug 796176 - Patch 2: Make Bluetooth*Managers listen on startup/disconnect, stop listening to stop/connect. r=echou, a=blocking-basecamp
authorKyle Machulis <kyle@nonpolynomial.com>
Wed, 10 Oct 2012 22:49:11 -0700
changeset 116051 fa62fe4387dde61c5fa25aee78807da9861b9b55
parent 116050 e47353b30f18e46b660158753be885b2526410c5
child 116052 a24b525c7f93cc1b7f7cae429d4c941711eb6234
push id1708
push userakeybl@mozilla.com
push dateMon, 19 Nov 2012 21:10:21 +0000
treeherdermozilla-beta@27b14fe50103 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersechou, blocking-basecamp
bugs796176
milestone18.0a2
Bug 796176 - Patch 2: Make Bluetooth*Managers listen on startup/disconnect, stop listening to stop/connect. r=echou, a=blocking-basecamp
dom/bluetooth/BluetoothHfpManager.cpp
dom/bluetooth/BluetoothHfpManager.h
dom/bluetooth/BluetoothOppManager.cpp
dom/bluetooth/BluetoothOppManager.h
dom/bluetooth/BluetoothReplyRunnable.cpp
dom/bluetooth/BluetoothScoManager.cpp
dom/bluetooth/BluetoothScoManager.h
dom/bluetooth/BluetoothUnixSocketConnector.cpp
dom/bluetooth/BluetoothUnixSocketConnector.h
dom/bluetooth/linux/BluetoothDBusService.cpp
--- a/dom/bluetooth/BluetoothHfpManager.cpp
+++ b/dom/bluetooth/BluetoothHfpManager.cpp
@@ -535,16 +535,18 @@ BluetoothHfpManager::Connect(const nsASt
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   if (gInShutdown) {
     MOZ_ASSERT(false, "Connect called while in shutdown!");
     return false;
   }
 
+  CloseSocket();
+
   BluetoothService* bs = BluetoothService::Get();
   if (!bs) {
     NS_WARNING("BluetoothService not available!");
     return false;
   }
   mDevicePath = aDeviceObjectPath;
 
   nsString serviceUuidStr;
@@ -578,16 +580,18 @@ BluetoothHfpManager::Listen()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   if (gInShutdown) {
     MOZ_ASSERT(false, "Listen called while in shutdown!");
     return false;
   }
 
+  CloseSocket();
+
   BluetoothService* bs = BluetoothService::Get();
   if (!bs) {
     NS_WARNING("BluetoothService not available!");
     return false;
   }
 
   nsresult rv = bs->ListenSocketViaService(BluetoothReservedChannels::HANDSFREE_AG,
                                            BluetoothSocketType::RFCOMM,
@@ -600,16 +604,17 @@ BluetoothHfpManager::Listen()
 void
 BluetoothHfpManager::Disconnect()
 {
   NotifySettings(false);
   CloseSocket();
   mCall = 0;
   mCallSetup = 0;
   mCallHeld = 0;
+  Listen();
 }
 
 bool
 BluetoothHfpManager::SendLine(const char* aMessage)
 {
   const char* kHfpCrlf = "\xd\xa";
   nsAutoCString msg;
 
@@ -760,8 +765,21 @@ BluetoothHfpManager::CallStateChanged(in
       NS_WARNING("Not handling state changed");
 #endif
       break;
   }
 
   mCurrentCallIndex = aCallIndex;
   mCurrentCallState = aCallState;
 }
+
+void
+BluetoothHfpManager::OnConnectSuccess()
+{
+}
+
+void
+BluetoothHfpManager::OnConnectError()
+{
+  CloseSocket();
+  // If connecting for some reason didn't work, restart listening
+  Listen();
+}
--- a/dom/bluetooth/BluetoothHfpManager.h
+++ b/dom/bluetooth/BluetoothHfpManager.h
@@ -17,17 +17,18 @@ BEGIN_BLUETOOTH_NAMESPACE
 class BluetoothReplyRunnable;
 class BluetoothHfpManagerObserver;
 
 class BluetoothHfpManager : public mozilla::ipc::UnixSocketConsumer
 {
 public:
   ~BluetoothHfpManager();
   static BluetoothHfpManager* Get();
-  void ReceiveSocketData(mozilla::ipc::UnixSocketRawData* aMessage);
+  virtual void ReceiveSocketData(mozilla::ipc::UnixSocketRawData* aMessage)
+    MOZ_OVERRIDE;
   bool Connect(const nsAString& aDeviceObjectPath,
                const bool aIsHandsfree,
                BluetoothReplyRunnable* aRunnable);
   void Disconnect();
   bool SendLine(const char* aMessage);
   void CallStateChanged(int aCallIndex, int aCallState,
                         const char* aNumber, bool aIsActive);
   void EnumerateCallState(int aCallIndex, int aCallState,
@@ -38,16 +39,18 @@ private:
   friend class BluetoothHfpManagerObserver;
   BluetoothHfpManager();
   nsresult HandleVolumeChanged(const nsAString& aData);
   nsresult HandleShutdown();
   bool Init();
   void Cleanup();
   void NotifyDialer(const nsAString& aCommand);
   void NotifySettings(const bool aConnected);
+  virtual void OnConnectSuccess() MOZ_OVERRIDE;
+  virtual void OnConnectError() MOZ_OVERRIDE;
 
   int mCurrentVgs;
   int mCurrentCallIndex;
   int mCurrentCallState;
   int mCall;
   int mCallSetup;
   int mCallHeld;
   nsAutoPtr<BluetoothRilListener> mListener;
--- a/dom/bluetooth/BluetoothOppManager.cpp
+++ b/dom/bluetooth/BluetoothOppManager.cpp
@@ -565,8 +565,18 @@ BluetoothOppManager::UpdateProgress(uint
   v = aFileLength;
   parameters.AppendElement(BluetoothNamedValue(name, v));
 
   if (!BroadcastSystemMessage(type, parameters)) {
     NS_WARNING("Failed to broadcast [bluetooth-opp-update-progress]");
     return;
   }
 }
+
+void
+BluetoothOppManager::OnConnectSuccess()
+{
+}
+
+void
+BluetoothOppManager::OnConnectError()
+{
+}
--- a/dom/bluetooth/BluetoothOppManager.h
+++ b/dom/bluetooth/BluetoothOppManager.h
@@ -24,17 +24,18 @@ public:
    * function add_reserved_service_records() in
    * external/bluetooth/bluez/src/adapter.c for more information.
    */
   static const int DEFAULT_OPP_CHANNEL = 10;
   static const int MAX_PACKET_LENGTH = 0xFFFE;
 
   ~BluetoothOppManager();
   static BluetoothOppManager* Get();
-  void ReceiveSocketData(mozilla::ipc::UnixSocketRawData* aMessage);
+  void ReceiveSocketData(mozilla::ipc::UnixSocketRawData* aMessage)
+    MOZ_OVERRIDE;
 
   /*
    * If a application wnats to send a file, first, it needs to
    * call Connect() to create a valid RFCOMM connection. After
    * that, call SendFile()/StopSendingFile() to control file-sharing
    * process. During the file transfering process, the application
    * will receive several system messages which contain the processed
    * percentage of file. At the end, the application will get another
@@ -60,16 +61,18 @@ public:
 private:
   BluetoothOppManager();
   void FileTransferComplete(bool aSuccess, bool aReceived,
                             const nsString& aFileName, uint32_t aFileLength);
   void UpdateProgress(uint32_t aProcessed, uint32_t aFileLength);
   void ReplyToConnect();
   void ReplyToDisconnect();
   void ReplyToPut(bool aFinal);
+  virtual void OnConnectSuccess() MOZ_OVERRIDE;
+  virtual void OnConnectError() MOZ_OVERRIDE;
 
   bool mConnected;
   int mConnectionId;
   int mLastCommand;
   uint8_t mRemoteObexVersion;
   uint8_t mRemoteConnectionFlags;
   int mRemoteMaxPacketLength;
   bool mAbortFlag;
--- a/dom/bluetooth/BluetoothReplyRunnable.cpp
+++ b/dom/bluetooth/BluetoothReplyRunnable.cpp
@@ -38,32 +38,32 @@ BluetoothReplyRunnable::FireReply(const 
   
   if (!rs) {
     NS_WARNING("No DOMRequest Service!");
     return NS_ERROR_FAILURE;
   }
   
   
   return mReply->type() == BluetoothReply::TBluetoothReplySuccess ?
-    rs->FireSuccess(mDOMRequest, aVal) :
-    rs->FireError(mDOMRequest, mReply->get_BluetoothReplyError().error());
+    rs->FireSuccessAsync(mDOMRequest, aVal) :
+    rs->FireErrorAsync(mDOMRequest, mReply->get_BluetoothReplyError().error());
 }
 
 nsresult
 BluetoothReplyRunnable::FireErrorString()
 {
   nsCOMPtr<nsIDOMRequestService> rs =
     do_GetService("@mozilla.org/dom/dom-request-service;1");
   
   if (!rs) {
     NS_WARNING("No DOMRequest Service!");
     return NS_ERROR_FAILURE;
   }
   
-  return rs->FireError(mDOMRequest, mErrorString);
+  return rs->FireErrorAsync(mDOMRequest, mErrorString);
 }
 
 NS_IMETHODIMP
 BluetoothReplyRunnable::Run()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(mDOMRequest);
   MOZ_ASSERT(mReply);
--- a/dom/bluetooth/BluetoothScoManager.cpp
+++ b/dom/bluetooth/BluetoothScoManager.cpp
@@ -203,8 +203,18 @@ BluetoothScoManager::GetConnected()
   return mConnected;
 }
 
 void
 BluetoothScoManager::SetConnected(bool aConnected)
 {
   mConnected = aConnected;
 }
+
+void
+BluetoothScoManager::OnConnectSuccess()
+{
+}
+
+void
+BluetoothScoManager::OnConnectError()
+{
+}
--- a/dom/bluetooth/BluetoothScoManager.h
+++ b/dom/bluetooth/BluetoothScoManager.h
@@ -17,28 +17,31 @@ class BluetoothReplyRunnable;
 class BluetoothScoManagerObserver;
 
 class BluetoothScoManager : public mozilla::ipc::UnixSocketConsumer
 {
 public:
   ~BluetoothScoManager();
 
   static BluetoothScoManager* Get();
-  void ReceiveSocketData(mozilla::ipc::UnixSocketRawData* aMessage);
+  void ReceiveSocketData(mozilla::ipc::UnixSocketRawData* aMessage)
+    MOZ_OVERRIDE;
 
   bool Connect(const nsAString& aDeviceObjectPath);
   void Disconnect();
   void SetConnected(bool aConnected);
   bool GetConnected();
 
 private:
   friend class BluetoothScoManagerObserver;
   BluetoothScoManager();
   bool Init();
   void Cleanup();
   nsresult HandleShutdown();
   void CreateScoSocket(const nsAString& aDevicePath);
+  virtual void OnConnectSuccess() MOZ_OVERRIDE;
+  virtual void OnConnectError() MOZ_OVERRIDE;
   bool mConnected;
 };
 
 END_BLUETOOTH_NAMESPACE
 
 #endif
--- a/dom/bluetooth/BluetoothUnixSocketConnector.cpp
+++ b/dom/bluetooth/BluetoothUnixSocketConnector.cpp
@@ -67,17 +67,17 @@ BluetoothUnixSocketConnector::BluetoothU
   bool aEncrypt) : mType(aType)
                  , mChannel(aChannel)
                  , mAuth(aAuth)
                  , mEncrypt(aEncrypt)
 {
 }
 
 bool
-BluetoothUnixSocketConnector::Setup(int aFd)
+BluetoothUnixSocketConnector::SetUp(int aFd)
 {
   int lm = 0;
   int sndbuf;
   /* kernel does not yet support LM for SCO */
   switch (mType) {
   case BluetoothSocketType::RFCOMM:
     lm |= mAuth ? RFCOMM_LM_AUTH : 0;
     lm |= mEncrypt ? RFCOMM_LM_ENCRYPT : 0;
@@ -128,17 +128,17 @@ BluetoothUnixSocketConnector::Create()
     MOZ_NOT_REACHED();
   }
 
   if (fd < 0) {
     NS_WARNING("Could not open bluetooth socket!");
     return -1;
   }
 
-  if (!Setup(fd)) {
+  if (!SetUp(fd)) {
     NS_WARNING("Could not set up socket!");
   }
   return fd;
 }
 
 void
 BluetoothUnixSocketConnector::CreateAddr(bool aIsServer,
                                          socklen_t& aAddrSize,
--- a/dom/bluetooth/BluetoothUnixSocketConnector.h
+++ b/dom/bluetooth/BluetoothUnixSocketConnector.h
@@ -20,17 +20,17 @@ public:
                                bool aAuth, bool aEncrypt);
   virtual ~BluetoothUnixSocketConnector()
   {}
   virtual int Create() MOZ_OVERRIDE;
   virtual void CreateAddr(bool aIsServer,
                           socklen_t& aAddrSize,
                           struct sockaddr* aAddr,
                           const char* aAddress) MOZ_OVERRIDE;
-  virtual bool Setup(int aFd) MOZ_OVERRIDE;
+  virtual bool SetUp(int aFd) MOZ_OVERRIDE;
 private:
   BluetoothSocketType mType;
   int mChannel;
   bool mAuth;
   bool mEncrypt;
 };
 
 END_BLUETOOTH_NAMESPACE
--- a/dom/bluetooth/linux/BluetoothDBusService.cpp
+++ b/dom/bluetooth/linux/BluetoothDBusService.cpp
@@ -742,16 +742,44 @@ BluetoothDBusService::AddReservedService
     LOG("Null DBus message. Couldn't extract handles.");
     return false;
   }
 
   ExtractHandles(reply, aServiceHandlesContainer);
   return true;
 }
 
+class PrepareProfileManagersRunnable : public nsRunnable
+{
+public:
+  NS_IMETHOD
+  Run()
+  {
+    BluetoothHfpManager* h = BluetoothHfpManager::Get();
+    if (h) {
+      h->Listen();
+    }
+    return NS_OK;
+  }
+};
+
+class ShutdownProfileManagersRunnable : public nsRunnable
+{
+public:
+  NS_IMETHOD
+  Run()
+  {
+    BluetoothHfpManager* h = BluetoothHfpManager::Get();
+    if (h) {
+      h->CloseSocket();
+    }
+    return NS_OK;
+  }
+};
+
 class PrepareAdapterRunnable : public nsRunnable
 {
 public:
   PrepareAdapterRunnable(const nsAString& aPath) :
     mPath(aPath)
   {
     MOZ_ASSERT(NS_IsMainThread());
   }
@@ -777,16 +805,17 @@ public:
 #endif
     }
 
     if(!RegisterAgent(mPath)) {
       NS_WARNING("Failed to register agent");
       return NS_ERROR_FAILURE;
     }
 
+    NS_DispatchToMainThread(new PrepareProfileManagersRunnable());
     return NS_OK;
   }
 
 private:
   nsString mPath;
 };
 
 void
@@ -1417,16 +1446,18 @@ UnrefDBusMessages(const nsAString& key, 
 }
 
 nsresult
 BluetoothDBusService::StopInternal()
 {
   // This could block. It should never be run on the main thread.
   MOZ_ASSERT(!NS_IsMainThread());
 
+  NS_DispatchToMainThread(new ShutdownProfileManagersRunnable());
+
   if (!mConnection) {
     StopDBus();
     return NS_OK;
   }
 
   DBusError err;
   dbus_error_init(&err);
   for (uint32_t i = 0; i < ArrayLength(sBluetoothDBusSignals); ++i) {
@@ -2312,38 +2343,39 @@ class ConnectBluetoothSocketRunnable : p
 {
 public:
   ConnectBluetoothSocketRunnable(BluetoothReplyRunnable* aRunnable,
                                  UnixSocketConsumer* aConsumer,
                                  const nsAString& aObjectPath,
                                  const nsAString& aServiceUUID,
                                  BluetoothSocketType aType,
                                  bool aAuth,
-                                 bool aEncrypt)
-    : mRunnable(dont_AddRef(aRunnable)),
-      mConsumer(aConsumer),
-      mObjectPath(aObjectPath),
-      mServiceUUID(aServiceUUID),
-      mType(aType),
-      mAuth(aAuth),
-      mEncrypt(aEncrypt)
+                                 bool aEncrypt,
+                                 int aChannel)
+    : mRunnable(dont_AddRef(aRunnable))
+    , mConsumer(aConsumer)
+    , mObjectPath(aObjectPath)
+    , mServiceUUID(aServiceUUID)
+    , mType(aType)
+    , mAuth(aAuth)
+    , mEncrypt(aEncrypt)
+    , mChannel(aChannel)
   {
   }
 
   nsresult
   Run()
   {
-    MOZ_ASSERT(!NS_IsMainThread());
+    MOZ_ASSERT(NS_IsMainThread());
 
     nsString address = GetAddressFromObjectPath(mObjectPath);
-    int channel = GetDeviceServiceChannel(mObjectPath, mServiceUUID, 0x0004);
     BluetoothValue v;
     nsString replyError;
     BluetoothUnixSocketConnector* c =
-      new BluetoothUnixSocketConnector(mType, channel, mAuth, mEncrypt);
+      new BluetoothUnixSocketConnector(mType, mChannel, mAuth, mEncrypt);
     if (!mConsumer->ConnectSocket(c, NS_ConvertUTF16toUTF8(address).get())) {
       replyError.AssignLiteral("SocketConnectionError");
       DispatchBluetoothReply(mRunnable, v, replyError);
       return NS_ERROR_FAILURE;
     }
     // Bluetooth value needs to be set to something to succeed.
     v = true;
     DispatchBluetoothReply(mRunnable, v, replyError);
@@ -2354,18 +2386,109 @@ public:
 private:
   nsRefPtr<BluetoothReplyRunnable> mRunnable;
   nsRefPtr<UnixSocketConsumer> mConsumer;
   nsString mObjectPath;
   nsString mServiceUUID;
   BluetoothSocketType mType;
   bool mAuth;
   bool mEncrypt;
+  int mChannel;
 };
 
+class GetDeviceChannelForConnectRunnable : public nsRunnable
+{
+public:
+  GetDeviceChannelForConnectRunnable(BluetoothReplyRunnable* aRunnable,
+                                     UnixSocketConsumer* aConsumer,
+                                     const nsAString& aObjectPath,
+                                     const nsAString& aServiceUUID,
+                                     BluetoothSocketType aType,
+                                     bool aAuth,
+                                     bool aEncrypt)
+    : mRunnable(dont_AddRef(aRunnable)),
+      mConsumer(aConsumer),
+      mObjectPath(aObjectPath),
+      mServiceUUID(aServiceUUID),
+      mType(aType),
+      mAuth(aAuth),
+      mEncrypt(aEncrypt)
+  {
+  }
+
+  nsresult
+  Run()
+  {
+    MOZ_ASSERT(!NS_IsMainThread());
+
+    int channel = GetDeviceServiceChannel(mObjectPath, mServiceUUID, 0x0004);
+    BluetoothValue v;
+    nsString replyError;
+    if(channel < 0) {
+      replyError.AssignLiteral("DeviceChannelRetrievalError");
+      DispatchBluetoothReply(mRunnable, v, replyError);
+      return NS_OK;
+    }
+    nsRefPtr<nsRunnable> func(new ConnectBluetoothSocketRunnable(mRunnable,
+                                                                 mConsumer,
+                                                                 mObjectPath,
+                                                                 mServiceUUID,
+                                                                 mType, mAuth,
+                                                                 mEncrypt,
+                                                                 channel));
+    if (NS_FAILED(NS_DispatchToMainThread(func, NS_DISPATCH_NORMAL))) {
+      NS_WARNING("Cannot dispatch connection task!");
+      return NS_ERROR_FAILURE;
+    }
+
+    return NS_OK;
+  }
+
+private:
+  nsRefPtr<BluetoothReplyRunnable> mRunnable;
+  nsRefPtr<UnixSocketConsumer> mConsumer;
+  nsString mObjectPath;
+  nsString mServiceUUID;
+  BluetoothSocketType mType;
+  bool mAuth;
+  bool mEncrypt;
+};
+
+nsresult
+BluetoothDBusService::GetSocketViaService(const nsAString& aObjectPath,
+                                          const nsAString& aService,
+                                          BluetoothSocketType aType,
+                                          bool aAuth,
+                                          bool aEncrypt,
+                                          mozilla::ipc::UnixSocketConsumer* aConsumer,
+                                          BluetoothReplyRunnable* aRunnable)
+{
+  NS_ASSERTION(NS_IsMainThread(), "Must be called from main thread!");
+  if (!mConnection || !gThreadConnection) {
+    NS_ERROR("Bluetooth service not started yet!");
+    return NS_ERROR_FAILURE;
+  }
+
+  nsRefPtr<BluetoothReplyRunnable> runnable = aRunnable;
+
+  nsRefPtr<nsRunnable> func(new GetDeviceChannelForConnectRunnable(
+                              runnable,
+                              aConsumer,
+                              aObjectPath,
+                              aService, aType,
+                              aAuth, aEncrypt));
+  if (NS_FAILED(mBluetoothCommandThread->Dispatch(func, NS_DISPATCH_NORMAL))) {
+    NS_WARNING("Cannot dispatch firmware loading task!");
+    return NS_ERROR_FAILURE;
+  }
+
+  runnable.forget();
+  return NS_OK;
+}
+
 nsresult
 BluetoothDBusService::GetScoSocket(const nsAString& aObjectPath,
                                    bool aAuth,
                                    bool aEncrypt,
                                    mozilla::ipc::UnixSocketConsumer* aConsumer)
 {
   NS_ASSERTION(NS_IsMainThread(), "Must be called from main thread!");
   if (!mConnection || !gThreadConnection) {
@@ -2377,46 +2500,16 @@ BluetoothDBusService::GetScoSocket(const
                                                          aObjectPath,
                                                          aAuth,
                                                          aEncrypt));
   if (NS_FAILED(mBluetoothCommandThread->Dispatch(func, NS_DISPATCH_NORMAL))) {
     NS_WARNING("Cannot dispatch firmware loading task!");
     return NS_ERROR_FAILURE;
   }
 
-  return NS_OK; 
-}
-
-nsresult
-BluetoothDBusService::GetSocketViaService(const nsAString& aObjectPath,
-                                          const nsAString& aService,
-                                          BluetoothSocketType aType,
-                                          bool aAuth,
-                                          bool aEncrypt,
-                                          mozilla::ipc::UnixSocketConsumer* aConsumer,
-                                          BluetoothReplyRunnable* aRunnable)
-{
-  NS_ASSERTION(NS_IsMainThread(), "Must be called from main thread!");
-  if (!mConnection || !gThreadConnection) {
-    NS_ERROR("Bluetooth service not started yet!");
-    return NS_ERROR_FAILURE;
-  }
-  nsRefPtr<BluetoothReplyRunnable> runnable = aRunnable;
-
-  nsRefPtr<nsRunnable> func(new ConnectBluetoothSocketRunnable(runnable,
-                                                              aConsumer,
-                                                              aObjectPath,
-                                                              aService, aType,
-                                                              aAuth, aEncrypt));
-  if (NS_FAILED(mBluetoothCommandThread->Dispatch(func, NS_DISPATCH_NORMAL))) {
-    NS_WARNING("Cannot dispatch firmware loading task!");
-    return NS_ERROR_FAILURE;
-  }
-
-  runnable.forget();
   return NS_OK;
 }
 
 bool
 BluetoothDBusService::SendFile(const nsAString& aDeviceAddress,
                                BlobParent* aBlobParent,
                                BlobChild* aBlobChild,
                                BluetoothReplyRunnable* aRunnable)
@@ -2440,68 +2533,26 @@ BluetoothDBusService::StopSendingFile(co
   // has been determined when calling 'Connect()'. Nevertheless, keep
   // it for future use.
   BluetoothOppManager* opp = BluetoothOppManager::Get();
   opp->StopSendingFile(aRunnable);
 
   return true;
 }
 
-class ListenBluetoothSocketRunnable : public nsRunnable
-{
-public:
-  ListenBluetoothSocketRunnable(UnixSocketConsumer* aConsumer,
-                                int aChannel,
-                                BluetoothSocketType aType,
-                                bool aAuth,
-                                bool aEncrypt)
-    : mConsumer(aConsumer)
-    , mChannel(aChannel)
-    , mType(aType)
-    , mAuth(aAuth)
-    , mEncrypt(aEncrypt)
-  {
-  }
-
-  nsresult
-  Run()
-  {
-    MOZ_ASSERT(!NS_IsMainThread());
-    BluetoothUnixSocketConnector* c =
-      new BluetoothUnixSocketConnector(mType, mChannel, mAuth, mEncrypt);
-    if (!mConsumer->ListenSocket(c)) {
-      NS_WARNING("Can't listen on socket!");
-      return NS_ERROR_FAILURE;
-    }
-    return NS_OK;
-  }
-
-private:
-  nsRefPtr<UnixSocketConsumer> mConsumer;
-  int mChannel;
-  BluetoothSocketType mType;
-  bool mAuth;
-  bool mEncrypt;
-};
-
 nsresult
 BluetoothDBusService::ListenSocketViaService(int aChannel,
                                              BluetoothSocketType aType,
                                              bool aAuth,
                                              bool aEncrypt,
                                              mozilla::ipc::UnixSocketConsumer* aConsumer)
 {
   NS_ASSERTION(NS_IsMainThread(), "Must be called from main thread!");
-  if (!mConnection || !gThreadConnection) {
-    NS_ERROR("Bluetooth service not started yet!");
-    return NS_ERROR_FAILURE;
-  }
-  nsRefPtr<nsRunnable> func(new ListenBluetoothSocketRunnable(aConsumer,
-                                                              aChannel, aType,
-                                                              aAuth,
-                                                              aEncrypt));
-  if (NS_FAILED(mBluetoothCommandThread->Dispatch(func, NS_DISPATCH_NORMAL))) {
-    NS_WARNING("Cannot dispatch firmware loading task!");
+
+  BluetoothUnixSocketConnector* c =
+    new BluetoothUnixSocketConnector(aType, aChannel, aAuth, aEncrypt);
+  if (!aConsumer->ListenSocket(c)) {
+    NS_WARNING("Can't listen on socket!");
     return NS_ERROR_FAILURE;
   }
 
   return NS_OK;
 }