diff --git a/dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp b/dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp index 628142b148c1..8f2be92e7126 100644 --- a/dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp +++ b/dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp @@ -115,11 +115,8 @@ static bool sIsFirstTimeToggleOffBt(false); #ifndef MOZ_B2G_BT_API_V1 static bool sAdapterEnabled(false); -// Use a static hash table to keep the name of remote device during the pairing -// procedure. In this manner, BT service and adapter can get the name of paired -// device name when bond state changed. -// The hash Key is BD address, the Value is remote BD name. -static nsDataHashtable sPairingNameTable; +// Static hash table to map device name from address +static nsDataHashtable sDeviceNameMap; static nsTArray > sChangeAdapterStateRunnableArray; static nsTArray > sChangeDiscoveryRunnableArray; @@ -1896,7 +1893,7 @@ BluetoothServiceBluedroid::AdapterStateChangedNotification(bool aState) sFetchUuidsRunnableArray.Clear(); sBondingRunnableArray.Clear(); sUnbondingRunnableArray.Clear(); - sPairingNameTable.Clear(); + sDeviceNameMap.Clear(); // Bluetooth scan mode is SCAN_MODE_CONNECTABLE by default, i.e., it should // be connectable and non-discoverable. @@ -2178,7 +2175,8 @@ BluetoothServiceBluedroid::RemoteDevicePropertiesNotification( InfallibleTArray propertiesArray; - BT_APPEND_NAMED_VALUE(propertiesArray, "Address", nsString(aBdAddr)); + nsString bdAddr(aBdAddr); + BT_APPEND_NAMED_VALUE(propertiesArray, "Address", bdAddr); for (int i = 0; i < aNumProperties; ++i) { @@ -2187,6 +2185,9 @@ BluetoothServiceBluedroid::RemoteDevicePropertiesNotification( if (p.mType == PROPERTY_BDNAME) { BT_APPEND_NAMED_VALUE(propertiesArray, "Name", p.mString); + // Update mapping + sDeviceNameMap.Remove(bdAddr); + sDeviceNameMap.Put(bdAddr, p.mString); } else if (p.mType == PROPERTY_CLASS_OF_DEVICE) { uint32_t cod = p.mUint32; BT_APPEND_NAMED_VALUE(propertiesArray, "Cod", cod); @@ -2382,19 +2383,19 @@ BluetoothServiceBluedroid::DeviceFoundNotification( #ifndef MOZ_B2G_BT_API_V1 MOZ_ASSERT(NS_IsMainThread()); - BluetoothValue propertyValue; InfallibleTArray propertiesArray; + nsString bdAddr, bdName; for (int i = 0; i < aNumProperties; i++) { const BluetoothProperty& p = aProperties[i]; if (p.mType == PROPERTY_BDADDR) { BT_APPEND_NAMED_VALUE(propertiesArray, "Address", p.mString); - + bdAddr = p.mString; } else if (p.mType == PROPERTY_BDNAME) { BT_APPEND_NAMED_VALUE(propertiesArray, "Name", p.mString); - + bdName = p.mString; } else if (p.mType == PROPERTY_CLASS_OF_DEVICE) { BT_APPEND_NAMED_VALUE(propertiesArray, "Cod", p.mUint32); @@ -2423,6 +2424,10 @@ BluetoothServiceBluedroid::DeviceFoundNotification( } } + // Update mapping + sDeviceNameMap.Remove(bdAddr); + sDeviceNameMap.Put(bdAddr, bdName); + DistributeSignal(NS_LITERAL_STRING("DeviceFound"), NS_LITERAL_STRING(KEY_ADAPTER), BluetoothValue(propertiesArray)); @@ -2515,14 +2520,23 @@ BluetoothServiceBluedroid::PinRequestNotification(const nsAString& aRemoteBdAddr InfallibleTArray propertiesArray; - BT_APPEND_NAMED_VALUE(propertiesArray, "address", nsString(aRemoteBdAddr)); - BT_APPEND_NAMED_VALUE(propertiesArray, "name", nsString(aBdName)); + // If |aBdName| is empty, get device name from |sDeviceNameMap|; + // Otherwise update mapping with |aBdName| + nsString bdAddr(aRemoteBdAddr); + nsString bdName(aBdName); + if (bdName.IsEmpty()) { + sDeviceNameMap.Get(bdAddr, &bdName); + } else { + sDeviceNameMap.Remove(bdAddr); + sDeviceNameMap.Put(bdAddr, bdName); + } + + BT_APPEND_NAMED_VALUE(propertiesArray, "address", bdAddr); + BT_APPEND_NAMED_VALUE(propertiesArray, "name", bdName); BT_APPEND_NAMED_VALUE(propertiesArray, "passkey", EmptyString()); BT_APPEND_NAMED_VALUE(propertiesArray, "type", NS_LITERAL_STRING(PAIRING_REQ_TYPE_ENTERPINCODE)); - sPairingNameTable.Put(nsString(aRemoteBdAddr), nsString(aBdName)); - DistributeSignal(NS_LITERAL_STRING("PairingRequest"), NS_LITERAL_STRING(KEY_PAIRING_LISTENER), BluetoothValue(propertiesArray)); @@ -2551,8 +2565,17 @@ BluetoothServiceBluedroid::SspRequestNotification( #ifndef MOZ_B2G_BT_API_V1 InfallibleTArray propertiesArray; - nsAutoString passkey; - nsAutoString pairingType; + + // If |aBdName| is empty, get device name from |sDeviceNameMap|; + // Otherwise update mapping with |aBdName| + nsString bdAddr(aRemoteBdAddr); + nsString bdName(aBdName); + if (bdName.IsEmpty()) { + sDeviceNameMap.Get(bdAddr, &bdName); + } else { + sDeviceNameMap.Remove(bdAddr); + sDeviceNameMap.Put(bdAddr, bdName); + } /** * Assign pairing request type and passkey based on the pairing variant. @@ -2562,6 +2585,8 @@ BluetoothServiceBluedroid::SspRequestNotification( * PAIRING_REQ_TYPE_DISPLAYPASSKEY * 2) empty string: PAIRING_REQ_TYPE_CONSENT */ + nsAutoString passkey; + nsAutoString pairingType; switch (aPairingVariant) { case SSP_VARIANT_PASSKEY_CONFIRMATION: pairingType.AssignLiteral(PAIRING_REQ_TYPE_CONFIRMATION); @@ -2579,13 +2604,11 @@ BluetoothServiceBluedroid::SspRequestNotification( return; } - BT_APPEND_NAMED_VALUE(propertiesArray, "address", nsString(aRemoteBdAddr)); - BT_APPEND_NAMED_VALUE(propertiesArray, "name", nsString(aBdName)); + BT_APPEND_NAMED_VALUE(propertiesArray, "address", bdAddr); + BT_APPEND_NAMED_VALUE(propertiesArray, "name", bdName); BT_APPEND_NAMED_VALUE(propertiesArray, "passkey", passkey); BT_APPEND_NAMED_VALUE(propertiesArray, "type", pairingType); - sPairingNameTable.Put(nsString(aRemoteBdAddr), nsString(aBdName)); - DistributeSignal(NS_LITERAL_STRING("PairingRequest"), NS_LITERAL_STRING(KEY_PAIRING_LISTENER), BluetoothValue(propertiesArray)); @@ -2642,16 +2665,13 @@ BluetoothServiceBluedroid::BondStateChangedNotification( return; } - // Retrieve and remove pairing device name from hash table - nsString deviceName; - bool nameExists = sPairingNameTable.Get(aRemoteBdAddr, &deviceName); - if (nameExists) { - sPairingNameTable.Remove(aRemoteBdAddr); - } + // Query pairing device name from hash table + nsString remoteBdAddr(aRemoteBdAddr); + nsString remotebdName; + sDeviceNameMap.Get(remoteBdAddr, &remotebdName); // Update bonded address array and append pairing device name InfallibleTArray propertiesArray; - nsString remoteBdAddr = nsString(aRemoteBdAddr); if (!bonded) { sAdapterBondedAddressArray.RemoveElement(remoteBdAddr); } else { @@ -2659,21 +2679,17 @@ BluetoothServiceBluedroid::BondStateChangedNotification( sAdapterBondedAddressArray.AppendElement(remoteBdAddr); } - // We don't assert |!deviceName.IsEmpty()| here since empty string is - // also a valid name. According to Bluetooth Core Spec. v3.0 - Sec. 6.22, + // We don't assert |!remotebdName.IsEmpty()| since empty string is also + // valid, according to Bluetooth Core Spec. v3.0 - Sec. 6.22: // "a valid Bluetooth name is a UTF-8 encoding string which is up to 248 // bytes in length." - // Furthermore, we don't assert |nameExists| here since it's expected to be - // 'false' if remote device is using "SSP just works without user - // interaction" or "legacy pairing with auto-pairing". - - BT_APPEND_NAMED_VALUE(propertiesArray, "Name", deviceName); + BT_APPEND_NAMED_VALUE(propertiesArray, "Name", remotebdName); } // Notify device of attribute changed BT_APPEND_NAMED_VALUE(propertiesArray, "Paired", bonded); DistributeSignal(NS_LITERAL_STRING("PropertyChanged"), - aRemoteBdAddr, + remoteBdAddr, BluetoothValue(propertiesArray)); // Notify adapter of device paired/unpaired