Bug 1141944 - [bluetooth2] Revise device paired/unpaired handlers in BluetoothAdapter, f=jaliu, r=shuang
authorBen Tian <btian@mozilla.com>
Mon, 16 Mar 2015 16:01:59 +0800
changeset 233764 889c932508bce082a0d49e066d3a7306097ae6a1
parent 233763 816f3a3a72ae36368c4c5f8ded98f65e0b0b9906
child 233765 cb812b993bac3a05fca03f59913af138be44beb4
push id28424
push usercbook@mozilla.com
push dateTue, 17 Mar 2015 10:13:09 +0000
treeherdermozilla-central@c3dc0e5f8b5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersshuang
bugs1141944
milestone39.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 1141944 - [bluetooth2] Revise device paired/unpaired handlers in BluetoothAdapter, f=jaliu, r=shuang
dom/bluetooth2/BluetoothAdapter.cpp
dom/bluetooth2/BluetoothAdapter.h
--- a/dom/bluetooth2/BluetoothAdapter.cpp
+++ b/dom/bluetooth2/BluetoothAdapter.cpp
@@ -848,82 +848,72 @@ BluetoothAdapter::HandleDeviceFound(cons
 
   // Notify application of discovered device via discovery handle
   mDiscoveryHandleInUse->DispatchDeviceEvent(discoveredDevice);
 }
 
 void
 BluetoothAdapter::HandleDevicePaired(const BluetoothValue& aValue)
 {
-  MOZ_ASSERT(aValue.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
-
-  if (mState != BluetoothAdapterState::Enabled) {
-    BT_WARNING("HandleDevicePaired() is called when adapter isn't enabled.");
+  if (NS_WARN_IF(mState != BluetoothAdapterState::Enabled)) {
     return;
   }
 
+  MOZ_ASSERT(aValue.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
+
   const InfallibleTArray<BluetoothNamedValue>& arr =
     aValue.get_ArrayOfBluetoothNamedValue();
 
   MOZ_ASSERT(arr.Length() == 3 &&
              arr[0].value().type() == BluetoothValue::TnsString && // Address
-             arr[1].value().type() == BluetoothValue::Tbool &&     // Paired
-             arr[2].value().type() == BluetoothValue::TnsString);  // Name
+             arr[1].value().type() == BluetoothValue::TnsString && // Name
+             arr[2].value().type() == BluetoothValue::Tbool);      // Paired
   MOZ_ASSERT(!arr[0].value().get_nsString().IsEmpty() &&
-             arr[1].value().get_bool());
-
-  nsString deviceAddress = arr[0].value().get_nsString();
-
-  nsRefPtr<BluetoothDevice> pairedDevice = nullptr;
+             arr[2].value().get_bool());
 
-  // Check whether or not the address exists in mDevices.
-  size_t index = mDevices.IndexOf(deviceAddress);
+  // Append the paired device if it doesn't exist in adapter's devices array
+  size_t index = mDevices.IndexOf(arr[0].value().get_nsString());
   if (index == mDevices.NoIndex) {
-    // Create a new device and append it to adapter's device array
-    pairedDevice = BluetoothDevice::Create(GetOwner(), aValue);
-    mDevices.AppendElement(pairedDevice);
-  } else {
-    // Use existing device
-    pairedDevice = mDevices[index];
+    index = mDevices.Length(); // the new device's index
+    mDevices.AppendElement(
+      BluetoothDevice::Create(GetOwner(), aValue));
   }
 
   // Notify application of paired device
   BluetoothDeviceEventInit init;
-  init.mDevice = pairedDevice;
-  DispatchDeviceEvent(NS_LITERAL_STRING("devicepaired"), init);
+  init.mDevice = mDevices[index];
+  DispatchDeviceEvent(NS_LITERAL_STRING(DEVICE_PAIRED_ID), init);
 }
 
 void
 BluetoothAdapter::HandleDeviceUnpaired(const BluetoothValue& aValue)
 {
-  MOZ_ASSERT(aValue.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
-
-  if (mState != BluetoothAdapterState::Enabled) {
-    BT_WARNING("HandleDeviceUnpaired() is called when adapter isn't enabled.");
+  if (NS_WARN_IF(mState != BluetoothAdapterState::Enabled)) {
     return;
   }
 
+  MOZ_ASSERT(aValue.type() == BluetoothValue::TArrayOfBluetoothNamedValue);
+
   const InfallibleTArray<BluetoothNamedValue>& arr =
     aValue.get_ArrayOfBluetoothNamedValue();
 
   MOZ_ASSERT(arr.Length() == 2 &&
              arr[0].value().type() == BluetoothValue::TnsString && // Address
              arr[1].value().type() == BluetoothValue::Tbool);      // Paired
   MOZ_ASSERT(!arr[0].value().get_nsString().IsEmpty() &&
              !arr[1].value().get_bool());
 
+  // Remove the device with the same address
   nsString deviceAddress = arr[0].value().get_nsString();
-
-  // Remove the device with the same address
   mDevices.RemoveElement(deviceAddress);
 
   // Notify application of unpaired device
   BluetoothDeviceEventInit init;
   init.mAddress = deviceAddress;
-  DispatchDeviceEvent(NS_LITERAL_STRING("deviceunpaired"), init);
+  DispatchDeviceEvent(NS_LITERAL_STRING(DEVICE_UNPAIRED_ID), init);
 }
 
 void
 BluetoothAdapter::DispatchAttributeEvent(const nsTArray<nsString>& aTypes)
 {
   NS_ENSURE_TRUE_VOID(aTypes.Length());
 
   AutoJSAPI jsapi;
--- a/dom/bluetooth2/BluetoothAdapter.h
+++ b/dom/bluetooth2/BluetoothAdapter.h
@@ -333,28 +333,29 @@ private:
    *
    * This variable is set to the latest discovery handle when adapter just
    * starts discovery, and is reset to nullptr when discovery is stopped by
    * some adapter.
    */
   nsRefPtr<BluetoothDiscoveryHandle> mDiscoveryHandleInUse;
 
   /**
-   * Arrays of references to BluetoothDevices created by this adapter.
-   * This array is empty when adapter state is Disabled.
+   * nsRefPtr array of BluetoothDevices created by this adapter. The array is
+   * empty when adapter state is Disabled.
    *
    * Devices will be appended when
-   * 1) Enabling BT: Paired devices reported by stack.
-   * 2) Discovering: Discovered devices during discovery operation.
-   * A device won't be appended if a device object with the same
-   * address already exists.
+   *   1) adapter is enabling: Paired devices reported by stack.
+   *   2) adapter is discovering: Discovered devices during discovery operation.
+   *   3) adapter paired with a device: The paired device reported by stack.
+   * Note devices with identical address won't be appended.
    *
    * Devices will be removed when
-   * 1) Starting discovery: All unpaired devices will be removed before this
-   *    adapter starts a new discovery.
-   * 2) Disabling BT: All devices will be removed.
+   *   1) adapter is disabling: All devices will be removed.
+   *   2) adapter starts discovery: All unpaired devices will be removed before
+   *      this new discovery starts.
+   *   3) adapter unpaired with a device: The unpaired device will be removed.
    */
   nsTArray<nsRefPtr<BluetoothDevice> > mDevices;
 };
 
 END_BLUETOOTH_NAMESPACE
 
 #endif