Bug 876823 - Fire DOMRequest.onerror event when Bluetooth*Manager.Connect() fails. r=gyeh, a=leo+
authorEric Chou <echou@mozilla.com>
Tue, 11 Jun 2013 10:46:00 -0400
changeset 119570 8d0562d2032481301693adfaf643943dd1d15745
parent 119569 756245b5a88279f4dd092d4bfd7329b7dce225d5
child 119571 185f30abad642b1c7d2f6a5b894999ee7c0688d8
push id879
push userryanvm@gmail.com
push dateTue, 11 Jun 2013 14:45:46 +0000
reviewersgyeh, leo
bugs876823
milestone18.0
Bug 876823 - Fire DOMRequest.onerror event when Bluetooth*Manager.Connect() fails. r=gyeh, a=leo+
dom/bluetooth/BluetoothHfpManager.cpp
dom/bluetooth/BluetoothOppManager.cpp
dom/bluetooth/BluetoothProfileManagerBase.h
--- a/dom/bluetooth/BluetoothHfpManager.cpp
+++ b/dom/bluetooth/BluetoothHfpManager.cpp
@@ -993,21 +993,28 @@ respond_with_ok:
 
 void
 BluetoothHfpManager::Connect(const nsAString& aDevicePath,
                              const bool aIsHandsfree,
                              BluetoothReplyRunnable* aRunnable)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  NS_ENSURE_FALSE_VOID(gInShutdown);
-  NS_ENSURE_FALSE_VOID(mSocket);
+  BluetoothService* bs = BluetoothService::Get();
+  if (!bs || gInShutdown) {
+    DispatchBluetoothReply(aRunnable, BluetoothValue(),
+                           NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
+    return;
+  }
 
-  BluetoothService* bs = BluetoothService::Get();
-  NS_ENSURE_TRUE_VOID(bs);
+  if (mSocket) {
+    DispatchBluetoothReply(aRunnable, BluetoothValue(),
+                           NS_LITERAL_STRING(ERR_REACHED_CONNECTION_LIMIT));
+    return;
+  }
 
   nsString uuid;
   if (aIsHandsfree) {
     BluetoothUuidHelper::GetString(BluetoothServiceClass::HANDSFREE, uuid);
   } else {
     BluetoothUuidHelper::GetString(BluetoothServiceClass::HEADSET, uuid);
   }
 
--- a/dom/bluetooth/BluetoothOppManager.cpp
+++ b/dom/bluetooth/BluetoothOppManager.cpp
@@ -252,20 +252,28 @@ BluetoothOppManager::Get()
 }
 
 void
 BluetoothOppManager::Connect(const nsAString& aDeviceObjectPath,
                              BluetoothReplyRunnable* aRunnable)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
-  NS_ENSURE_FALSE_VOID(mSocket);
+  BluetoothService* bs = BluetoothService::Get();
+  if (!bs || sInShutdown) {
+    DispatchBluetoothReply(aRunnable, BluetoothValue(),
+                           NS_LITERAL_STRING(ERR_NO_AVAILABLE_RESOURCE));
+    return;
+  }
 
-  BluetoothService* bs = BluetoothService::Get();
-  NS_ENSURE_TRUE_VOID(bs);
+  if (mSocket) {
+    DispatchBluetoothReply(aRunnable, BluetoothValue(),
+                           NS_LITERAL_STRING(ERR_REACHED_CONNECTION_LIMIT));
+    return;
+  }
 
   nsString uuid;
   BluetoothUuidHelper::GetString(BluetoothServiceClass::OBJECT_PUSH, uuid);
 
   if (NS_FAILED(bs->GetServiceChannel(aDeviceObjectPath, uuid, this))) {
     BluetoothValue v;
     DispatchBluetoothReply(aRunnable, v,
                            NS_LITERAL_STRING("GetServiceChannelError"));
@@ -1345,32 +1353,32 @@ BluetoothOppManager::OnConnectSuccess(Bl
   }
 
   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();
+    mRunnable = nullptr;
   }
 
   // Cache device address since we can't get socket address when a remote
   // device disconnect with us.
   mSocket->GetAddress(mConnectedDeviceAddress);
 }
 
 void
 BluetoothOppManager::OnConnectError(BluetoothSocket* aSocket)
 {
   if (mRunnable) {
     BluetoothValue v;
     DispatchBluetoothReply(mRunnable, v,
                            NS_LITERAL_STRING("OnConnectError:no runnable"));
-    mRunnable.forget();
+    mRunnable = nullptr;
   }
 
   mSocket = nullptr;
   mRfcommSocket = nullptr;
   mL2capSocket = nullptr;
 
   Listen();
 }
--- a/dom/bluetooth/BluetoothProfileManagerBase.h
+++ b/dom/bluetooth/BluetoothProfileManagerBase.h
@@ -2,16 +2,20 @@
 /* vim: set ts=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/. */
 
 #ifndef mozilla_dom_bluetooth_bluetoothprofilemanagerbase_h__
 #define mozilla_dom_bluetooth_bluetoothprofilemanagerbase_h__
 
+#define ERR_SERVICE_CHANNEL_NOT_FOUND "DeviceChannelRetrievalError"
+#define ERR_REACHED_CONNECTION_LIMIT "ReachedConnectionLimitError"
+#define ERR_NO_AVAILABLE_RESOURCE "NoAvailableResourceError"
+
 #include "BluetoothCommon.h"
 
 BEGIN_BLUETOOTH_NAMESPACE
 
 class BluetoothProfileManagerBase
 {
 public:
   virtual void OnGetServiceChannel(const nsAString& aDeviceAddress,