fix: take bluetooth serial ports into account in serial.getPorts() (#43170)

* fix: take bluetooth serial ports into account in serial.getPorts()

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: remove port.path.LossyDisplayName()

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2024-08-01 23:10:46 -07:00 committed by GitHub
parent 2ad5d1ff71
commit 1c368f13f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -24,14 +24,15 @@ namespace electron {
constexpr char kPortNameKey[] = "name"; constexpr char kPortNameKey[] = "name";
constexpr char kTokenKey[] = "token"; constexpr char kTokenKey[] = "token";
constexpr char kBluetoothDevicePathKey[] = "bluetooth_device_path";
#if BUILDFLAG(IS_WIN) #if BUILDFLAG(IS_WIN)
const char kDeviceInstanceIdKey[] = "device_instance_id"; constexpr char kDeviceInstanceIdKey[] = "device_instance_id";
#else #else
const char kVendorIdKey[] = "vendor_id"; constexpr char kVendorIdKey[] = "vendor_id";
const char kProductIdKey[] = "product_id"; constexpr char kProductIdKey[] = "product_id";
const char kSerialNumberKey[] = "serial_number"; constexpr char kSerialNumberKey[] = "serial_number";
#if BUILDFLAG(IS_MAC) #if BUILDFLAG(IS_MAC)
const char kUsbDriverKey[] = "usb_driver"; constexpr char kUsbDriverKey[] = "usb_driver";
#endif // BUILDFLAG(IS_MAC) #endif // BUILDFLAG(IS_MAC)
#endif // BUILDFLAG(IS_WIN) #endif // BUILDFLAG(IS_WIN)
@ -44,33 +45,38 @@ std::string EncodeToken(const base::UnguessableToken& token) {
base::Value PortInfoToValue(const device::mojom::SerialPortInfo& port) { base::Value PortInfoToValue(const device::mojom::SerialPortInfo& port) {
base::Value::Dict value; base::Value::Dict value;
if (port.display_name && !port.display_name->empty()) if (port.display_name && !port.display_name->empty()) {
value.Set(kPortNameKey, *port.display_name); value.Set(kPortNameKey, *port.display_name);
else } else {
value.Set(kPortNameKey, port.path.LossyDisplayName()); value.Set(kPortNameKey, port.path.LossyDisplayName());
}
if (!SerialChooserContext::CanStorePersistentEntry(port)) { if (!SerialChooserContext::CanStorePersistentEntry(port)) {
value.Set(kTokenKey, EncodeToken(port.token)); value.Set(kTokenKey, EncodeToken(port.token));
return base::Value(std::move(value)); return base::Value(std::move(value));
} }
if (port.bluetooth_service_class_id &&
port.bluetooth_service_class_id->IsValid()) {
value.Set(kBluetoothDevicePathKey, port.path.LossyDisplayName());
} else {
#if BUILDFLAG(IS_WIN) #if BUILDFLAG(IS_WIN)
// Windows provides a handy device identifier which we can rely on to be // Windows provides a handy device identifier which we can rely on to be
// sufficiently stable for identifying devices across restarts. // sufficiently stable for identifying devices across restarts.
value.Set(kDeviceInstanceIdKey, port.device_instance_id); value.Set(kDeviceInstanceIdKey, port.device_instance_id);
#else #else
DCHECK(port.has_vendor_id); CHECK(port.has_vendor_id);
value.Set(kVendorIdKey, port.vendor_id); value.Set(kVendorIdKey, port.vendor_id);
DCHECK(port.has_product_id); CHECK(port.has_product_id);
value.Set(kProductIdKey, port.product_id); value.Set(kProductIdKey, port.product_id);
DCHECK(port.serial_number); CHECK(port.serial_number);
value.Set(kSerialNumberKey, *port.serial_number); value.Set(kSerialNumberKey, *port.serial_number);
#if BUILDFLAG(IS_MAC) #if BUILDFLAG(IS_MAC)
DCHECK(port.usb_driver_name && !port.usb_driver_name->empty()); CHECK(port.usb_driver_name && !port.usb_driver_name->empty());
value.Set(kUsbDriverKey, *port.usb_driver_name); value.Set(kUsbDriverKey, *port.usb_driver_name);
#endif // BUILDFLAG(IS_MAC) #endif // BUILDFLAG(IS_MAC)
#endif // BUILDFLAG(IS_WIN) #endif // BUILDFLAG(IS_WIN)
}
return base::Value(std::move(value)); return base::Value(std::move(value));
} }
@ -173,11 +179,19 @@ bool SerialChooserContext::CanStorePersistentEntry(
if (!port.display_name || port.display_name->empty()) if (!port.display_name || port.display_name->empty())
return false; return false;
const bool has_bluetooth = port.bluetooth_service_class_id &&
port.bluetooth_service_class_id->IsValid() &&
!port.path.empty();
if (has_bluetooth) {
return true;
}
#if BUILDFLAG(IS_WIN) #if BUILDFLAG(IS_WIN)
return !port.device_instance_id.empty(); return !port.device_instance_id.empty();
#else #else
if (!port.has_vendor_id || !port.has_product_id || !port.serial_number || const bool has_usb = port.has_vendor_id && port.has_product_id &&
port.serial_number->empty()) { port.serial_number && !port.serial_number->empty();
if (!has_usb) {
return false; return false;
} }