Repository: electron/electron
File: shell/browser/lib/bluetooth_chooser.cc (C++)

Code before:
  if (!prevent_default && idle_state) {
    if (device_id_to_name_map_.empty()) {
      event_handler_.Run(content::BluetoothChooserEvent::CANCELLED, "");
    } else {
      auto it = device_id_to_name_map_.begin();
      auto device_id = it->first;
      event_handler_.Run(content::BluetoothChooserEvent::SELECTED, device_id);
    }
  }
}

void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
                                         bool should_update_name,
                                         const std::u16string& device_name,
                                         bool is_gatt_connected,
                                         bool is_paired,
                                         int signal_strength_level) {
  // Don't fire an event during refresh.
  if (refreshing_)
    return;

  // Emit a select-bluetooth-device handler to allow for user to listen for
  // bluetooth device found. If there's no listener in place, then select the
  // first device that matches the filters provided.
  auto [iter, changed] =
      device_id_to_name_map_.try_emplace(device_id, device_name);
  if (changed) {
    bool prevent_default = api_web_contents_->Emit(
        "select-bluetooth-device", GetDeviceList(),
        base::BindOnce(&BluetoothChooser::OnDeviceChosen,
                       weak_ptr_factory_.GetWeakPtr()));

    if (!prevent_default)
      event_handler_.Run(content::BluetoothChooserEvent::SELECTED, device_id);
  }
}

void BluetoothChooser::OnDeviceChosen(const std::string& device_id) {
  if (event_handler_.is_null())
    return;

  if (device_id.empty()) {
    event_handler_.Run(content::BluetoothChooserEvent::CANCELLED, device_id);
  } else {
    event_handler_.Run(content::BluetoothChooserEvent::SELECTED, device_id);
  }
}

std::vector<electron::BluetoothChooser::DeviceInfo>
BluetoothChooser::GetDeviceList() {
  std::vector<electron::BluetoothChooser::DeviceInfo> vec;

Review comment (bug):
Looks like the `if (!changed && should_update_name)` got lost here, which seems like a bug?

Code after:
  if (!prevent_default && idle_state) {
    if (device_id_to_name_map_.empty()) {
      event_handler_.Run(content::BluetoothChooserEvent::CANCELLED, "");
    } else {
      auto it = device_id_to_name_map_.begin();
      auto device_id = it->first;
      event_handler_.Run(content::BluetoothChooserEvent::SELECTED, device_id);
    }
  }
}

void BluetoothChooser::AddOrUpdateDevice(const std::string& device_id,
                                         bool should_update_name,
                                         const std::u16string& device_name,
                                         bool is_gatt_connected,
                                         bool is_paired,
                                         int signal_strength_level) {
  // Don't fire an event during refresh.
  if (refreshing_)
    return;

  // Emit a select-bluetooth-device handler to allow for user to listen for
  // bluetooth device found. If there's no listener in place, then select the
  // first device that matches the filters provided.
  auto [iter, changed] =
      device_id_to_name_map_.try_emplace(device_id, device_name);
  if (!changed && should_update_name) {
    iter->second = device_name;
    changed = true;
  }

  if (changed) {
    bool prevent_default = api_web_contents_->Emit(
        "select-bluetooth-device", GetDeviceList(),
        base::BindOnce(&BluetoothChooser::OnDeviceChosen,
                       weak_ptr_factory_.GetWeakPtr()));

    if (!prevent_default)
      event_handler_.Run(content::BluetoothChooserEvent::SELECTED, device_id);
  }
}

void BluetoothChooser::OnDeviceChosen(const std::string& device_id) {
  if (event_handler_.is_null())
    return;

  if (device_id.empty()) {
    event_handler_.Run(content::BluetoothChooserEvent::CANCELLED, device_id);
  } else {
    event_handler_.Run(content::BluetoothChooserEvent::SELECTED, device_id);
  }