#ifndef SERVICES_DEVICE_SERIAL_BLUETOOTH_SERIAL_DEVICE_ENUMERATOR_H_
#define SERVICES_DEVICE_SERIAL_BLUETOOTH_SERIAL_DEVICE_ENUMERATOR_H_
#include <string>
#include <string_view>
#include <utility>
#include "base/containers/flat_map.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/sequence_bound.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "services/device/public/mojom/serial.mojom-forward.h"
#include "services/device/serial/bluetooth_serial_port_impl.h"
#include "services/device/serial/serial_device_enumerator.h"
namespace device {
class BluetoothUUID;
class BluetoothSerialDeviceEnumerator : public SerialDeviceEnumerator {
public:
explicit BluetoothSerialDeviceEnumerator(
scoped_refptr<base::SingleThreadTaskRunner> adapter_runner);
BluetoothSerialDeviceEnumerator(const BluetoothSerialDeviceEnumerator&) =
delete;
BluetoothSerialDeviceEnumerator& operator=(
const BluetoothSerialDeviceEnumerator&) = delete;
~BluetoothSerialDeviceEnumerator() override;
void GetDevicesAfterInitialEnumeration(
mojom::SerialPortManager::GetDevicesCallback callback);
void DeviceAddedOrChanged(std::string_view device_address,
std::u16string_view device_name,
BluetoothDevice::UUIDSet service_class_ids,
bool is_connected);
void DeviceRemoved(const std::string& device_address);
void OpenPort(const std::string& address,
const BluetoothUUID& service_class_id,
mojom::SerialConnectionOptionsPtr options,
mojo::PendingRemote<mojom::SerialPortClient> client,
mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher,
BluetoothSerialPortImpl::OpenCallback callback);
std::optional<std::string> GetAddressFromToken(
const base::UnguessableToken& token) const;
BluetoothUUID GetServiceClassIdFromToken(
const base::UnguessableToken& token) const;
void DeviceAddedForTesting(BluetoothAdapter* adapter,
BluetoothDevice* device);
void DeviceChangedForTesting(BluetoothAdapter* adapter,
BluetoothDevice* device);
void SynchronouslyResetHelperForTesting();
private:
class AdapterHelper;
using DeviceServiceInfo = std::pair<std::string, BluetoothUUID>;
using DevicePortsMap =
base::flat_map<DeviceServiceInfo, base::UnguessableToken>;
void AddOrUpdateService(std::string_view device_address,
std::u16string_view device_name,
const BluetoothUUID& service_class_id,
bool is_connected);
void OnInitialEnumerationComplete();
bool initial_enumeration_completed_ = false;
std::vector<mojom::SerialPortManager::GetDevicesCallback>
pending_get_devices_;
DevicePortsMap device_ports_;
base::SequenceBound<AdapterHelper> helper_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<BluetoothSerialDeviceEnumerator> weak_ptr_factory_{this};
};
}
#endif