#ifndef DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_
#define DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "base/win/scoped_handle.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_export.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
namespace base {
class SequencedTaskRunner;
}
namespace device {
namespace win {
class BluetoothClassicWrapper;
}
class DEVICE_BLUETOOTH_EXPORT BluetoothTaskManagerWin
: public base::RefCountedThreadSafe<BluetoothTaskManagerWin> {
public:
struct DEVICE_BLUETOOTH_EXPORT AdapterState {
AdapterState();
~AdapterState();
std::string name;
std::string address;
bool powered;
};
struct DEVICE_BLUETOOTH_EXPORT ServiceRecordState {
ServiceRecordState();
ServiceRecordState(const ServiceRecordState&) = delete;
ServiceRecordState& operator=(const ServiceRecordState&) = delete;
~ServiceRecordState();
std::string name;
std::vector<uint8_t> sdp_bytes;
BluetoothUUID gatt_uuid;
uint16_t attribute_handle;
base::FilePath path;
};
struct DEVICE_BLUETOOTH_EXPORT DeviceState {
DeviceState();
DeviceState(const DeviceState&) = delete;
DeviceState& operator=(const DeviceState&) = delete;
~DeviceState();
std::string address;
absl::optional<std::string> name;
bool visible;
bool connected;
bool authenticated;
std::vector<std::unique_ptr<ServiceRecordState>> service_record_states;
uint32_t bluetooth_class;
};
class DEVICE_BLUETOOTH_EXPORT Observer {
public:
virtual ~Observer() {}
virtual void AdapterStateChanged(const AdapterState& state) {}
virtual void DiscoveryStarted(bool success) {}
virtual void DiscoveryStopped() {}
virtual void DevicesPolled(
const std::vector<std::unique_ptr<DeviceState>>& devices) {}
};
explicit BluetoothTaskManagerWin(
scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
BluetoothTaskManagerWin(const BluetoothTaskManagerWin&) = delete;
BluetoothTaskManagerWin& operator=(const BluetoothTaskManagerWin&) = delete;
static scoped_refptr<BluetoothTaskManagerWin> CreateForTesting(
std::unique_ptr<win::BluetoothClassicWrapper> classic_wrapper,
scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
void Initialize();
void InitializeWithBluetoothTaskRunner(
scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner);
void PostSetPoweredBluetoothTask(
bool powered,
base::OnceClosure callback,
BluetoothAdapter::ErrorCallback error_callback);
void PostStartDiscoveryTask();
void PostStopDiscoveryTask();
private:
friend class base::RefCountedThreadSafe<BluetoothTaskManagerWin>;
friend class BluetoothTaskManagerWinTest;
static const int kPollIntervalMs;
BluetoothTaskManagerWin(
std::unique_ptr<win::BluetoothClassicWrapper> classic_wrapper,
scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
virtual ~BluetoothTaskManagerWin();
void LogPollingError(const char* message, int win32_error);
void OnAdapterStateChanged(const AdapterState* state);
void OnDiscoveryStarted(bool success);
void OnDiscoveryStopped();
void OnDevicesPolled(std::vector<std::unique_ptr<DeviceState>> devices);
void StartPolling();
void PollAdapter();
void PostAdapterStateToUi();
void SetPowered(bool powered,
base::OnceClosure callback,
BluetoothAdapter::ErrorCallback error_callback);
void StartDiscovery();
void StopDiscovery();
void DiscoverDevices(int timeout_multiplier);
void GetKnownDevices();
bool SearchDevices(int timeout_multiplier,
bool search_cached_devices_only,
std::vector<std::unique_ptr<DeviceState>>* device_list);
bool SearchClassicDevices(
int timeout_multiplier,
bool search_cached_devices_only,
std::vector<std::unique_ptr<DeviceState>>* device_list);
bool DiscoverServices(std::vector<std::unique_ptr<DeviceState>>* device_list,
bool search_cached_services_only);
bool DiscoverClassicDeviceServices(
const std::string& device_address,
const GUID& protocol_uuid,
bool search_cached_services_only,
std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states);
int DiscoverClassicDeviceServicesWorker(
const std::string& device_address,
const GUID& protocol_uuid,
bool search_cached_services_only,
std::vector<std::unique_ptr<ServiceRecordState>>* service_record_states);
scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner_;
base::ObserverList<Observer>::Unchecked observers_;
bool discovering_ = false;
base::TimeTicks current_logging_batch_ticks_;
int current_logging_batch_count_ = 0;
std::unique_ptr<win::BluetoothClassicWrapper> classic_wrapper_;
};
}
#endif