Bug 1036233 - Implement pairing methods of Bluetooth API v2. r=btian, r=mrbkap
authorJamin Liu <jaliu@mozilla.com>
Tue, 12 Aug 2014 16:30:36 +0800
changeset 213354 a31cf9931d59963cabcd640e40698ecfe8711986
parent 213353 3e159fb268c000d7de524fa4a7877167dada89b8
child 213355 4c8a9727660fd934ac5cdeca38441189c3e395c5
push id6741
push userraliiev@mozilla.com
push dateTue, 02 Sep 2014 16:57:58 +0000
treeherdermozilla-aurora@aed50d3edf33 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbtian, mrbkap
bugs1036233
milestone34.0a1
Bug 1036233 - Implement pairing methods of Bluetooth API v2. r=btian, r=mrbkap - Promise<void> pair(DOMString aAddress) - Promise<void> unpair(DOMString aAddress) - sequence<BluetoothDevice> getPairedDevices()
dom/bluetooth2/BluetoothAdapter.cpp
dom/bluetooth2/BluetoothAdapter.h
dom/webidl/BluetoothAdapter2.webidl
--- a/dom/bluetooth2/BluetoothAdapter.cpp
+++ b/dom/bluetooth2/BluetoothAdapter.cpp
@@ -583,66 +583,80 @@ BluetoothAdapter::GetConnectedDevices(ui
   if (NS_FAILED(rv)) {
     aRv.Throw(rv);
     return nullptr;
   }
 
   return request.forget();
 }
 
-already_AddRefed<DOMRequest>
-BluetoothAdapter::GetPairedDevices(ErrorResult& aRv)
+void
+BluetoothAdapter::GetPairedDevices(nsTArray<nsRefPtr<BluetoothDevice> >& aDevices)
 {
-  // This method will be implemented later in Bug 1036233.
-  return nullptr;
+  for (uint32_t i = 0; i < mDevices.Length(); ++i) {
+    if (mDevices[i]->Paired()) {
+      aDevices.AppendElement(mDevices[i]);
+    }
+  }
 }
 
-already_AddRefed<DOMRequest>
+already_AddRefed<Promise>
 BluetoothAdapter::PairUnpair(bool aPair, const nsAString& aDeviceAddress,
                              ErrorResult& aRv)
 {
-  nsCOMPtr<nsPIDOMWindow> win = GetOwner();
-  if (!win) {
+  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
+  if (!global) {
     aRv.Throw(NS_ERROR_FAILURE);
     return nullptr;
   }
 
-  nsRefPtr<DOMRequest> request = new DOMRequest(win);
-  nsRefPtr<BluetoothVoidReplyRunnable> results =
-    new BluetoothVoidReplyRunnable(request);
+  nsRefPtr<Promise> promise = Promise::Create(global, aRv);
+  NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
 
+  /**
+   * Ensure
+   * - device address is not empty,
+   * - adapter is already enabled, and
+   * - BluetoothService is available.
+   */
+  BT_ENSURE_TRUE_REJECT(!aDeviceAddress.IsEmpty(),
+                        NS_ERROR_DOM_INVALID_STATE_ERR);
+  BT_ENSURE_TRUE_REJECT(mState == BluetoothAdapterState::Enabled,
+                        NS_ERROR_DOM_INVALID_STATE_ERR);
   BluetoothService* bs = BluetoothService::Get();
-  if (!bs) {
-    aRv.Throw(NS_ERROR_FAILURE);
-    return nullptr;
-  }
+  BT_ENSURE_TRUE_REJECT(bs, NS_ERROR_NOT_AVAILABLE);
+
   nsresult rv;
   if (aPair) {
+    nsRefPtr<BluetoothReplyRunnable> result =
+      new BluetoothVoidReplyRunnable(nullptr /* DOMRequest */,
+                                     promise,
+                                     NS_LITERAL_STRING("Pair"));
     rv = bs->CreatePairedDeviceInternal(aDeviceAddress,
                                         kCreatePairedDeviceTimeout,
-                                        results);
+                                        result);
   } else {
-    rv = bs->RemoveDeviceInternal(aDeviceAddress, results);
+    nsRefPtr<BluetoothReplyRunnable> result =
+      new BluetoothVoidReplyRunnable(nullptr /* DOMRequest */,
+                                     promise,
+                                     NS_LITERAL_STRING("Unpair"));
+    rv = bs->RemoveDeviceInternal(aDeviceAddress, result);
   }
-  if (NS_FAILED(rv)) {
-    BT_WARNING("Pair/Unpair failed!");
-    aRv.Throw(rv);
-    return nullptr;
-  }
+  BT_ENSURE_TRUE_REJECT(NS_SUCCEEDED(rv), NS_ERROR_DOM_OPERATION_ERR);
 
-  return request.forget();
+  return promise.forget();
 }
 
-already_AddRefed<DOMRequest>
+already_AddRefed<Promise>
 BluetoothAdapter::Pair(const nsAString& aDeviceAddress, ErrorResult& aRv)
 {
   return PairUnpair(true, aDeviceAddress, aRv);
 }
 
-already_AddRefed<DOMRequest>
+already_AddRefed<Promise>
 BluetoothAdapter::Unpair(const nsAString& aDeviceAddress, ErrorResult& aRv)
 {
   return PairUnpair(false, aDeviceAddress, aRv);
 }
 
 already_AddRefed<Promise>
 BluetoothAdapter::EnableDisable(bool aEnable, ErrorResult& aRv)
 {
--- a/dom/bluetooth2/BluetoothAdapter.h
+++ b/dom/bluetooth2/BluetoothAdapter.h
@@ -94,22 +94,27 @@ public:
   void SetDiscoveryHandleInUse(BluetoothDiscoveryHandle* aDiscoveryHandle);
 
   already_AddRefed<Promise> SetName(const nsAString& aName, ErrorResult& aRv);
   already_AddRefed<Promise>
     SetDiscoverable(bool aDiscoverable, ErrorResult& aRv);
   already_AddRefed<Promise> StartDiscovery(ErrorResult& aRv);
   already_AddRefed<Promise> StopDiscovery(ErrorResult& aRv);
 
-  already_AddRefed<DOMRequest>
+  already_AddRefed<Promise>
     Pair(const nsAString& aDeviceAddress, ErrorResult& aRv);
-  already_AddRefed<DOMRequest>
+  already_AddRefed<Promise>
     Unpair(const nsAString& aDeviceAddress, ErrorResult& aRv);
-  already_AddRefed<DOMRequest>
-    GetPairedDevices(ErrorResult& aRv);
+
+  /**
+   * Get a list of paired bluetooth devices.
+   *
+   * @param aDevices [out] Devices array to return
+   */
+  void GetPairedDevices(nsTArray<nsRefPtr<BluetoothDevice> >& aDevices);
 
   already_AddRefed<Promise> EnableDisable(bool aEnable, ErrorResult& aRv);
   already_AddRefed<Promise> Enable(ErrorResult& aRv);
   already_AddRefed<Promise> Disable(ErrorResult& aRv);
 
   already_AddRefed<DOMRequest>
     Connect(BluetoothDevice& aDevice,
             const Optional<short unsigned int>& aServiceUuid, ErrorResult& aRv);
@@ -156,17 +161,17 @@ public:
 
   virtual JSObject*
     WrapObject(JSContext* aCx) MOZ_OVERRIDE;
 
 private:
   BluetoothAdapter(nsPIDOMWindow* aOwner, const BluetoothValue& aValue);
   ~BluetoothAdapter();
 
-  already_AddRefed<mozilla::dom::DOMRequest>
+  already_AddRefed<Promise>
     PairUnpair(bool aPair, const nsAString& aDeviceAddress, ErrorResult& aRv);
 
   bool IsAdapterAttributeChanged(BluetoothAdapterAttribute aType,
                                  const BluetoothValue& aValue);
   void HandleAdapterStateChanged();
   void HandlePropertyChanged(const BluetoothValue& aValue);
   void DispatchAttributeEvent(const nsTArray<nsString>& aTypes);
   BluetoothAdapterAttribute
--- a/dom/webidl/BluetoothAdapter2.webidl
+++ b/dom/webidl/BluetoothAdapter2.webidl
@@ -98,21 +98,22 @@ interface BluetoothAdapter : EventTarget
   Promise<void> setDiscoverable(boolean aDiscoverable);
 
   [NewObject, Throws]
   Promise<BluetoothDiscoveryHandle> startDiscovery();
   [NewObject, Throws]
   Promise<void> stopDiscovery();
 
   [NewObject, Throws]
-  DOMRequest pair(DOMString deviceAddress);
+  Promise<void> pair(DOMString deviceAddress);
   [NewObject, Throws]
-  DOMRequest unpair(DOMString deviceAddress);
-  [NewObject, Throws]
-  DOMRequest getPairedDevices();
+  Promise<void> unpair(DOMString deviceAddress);
+
+  sequence<BluetoothDevice> getPairedDevices();
+
   [NewObject, Throws]
   DOMRequest getConnectedDevices(unsigned short serviceUuid);
 
   /**
    * Connect/Disconnect to a specific service of a target remote device.
    * To check the value of service UUIDs, please check "Bluetooth Assigned
    * Numbers" / "Service Discovery Protocol" for more information.
    *