#ifndef DEVICE_BLUETOOTH_FLOSS_BLUETOOTH_SOCKET_FLOSS_H_
#define DEVICE_BLUETOOTH_FLOSS_BLUETOOTH_SOCKET_FLOSS_H_
#include <memory>
#include <string>
#include "base/task/cancelable_task_tracker.h"
#include "base/task/sequenced_task_runner.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_export.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "device/bluetooth/bluetooth_socket_net.h"
#include "device/bluetooth/bluetooth_socket_thread.h"
#include "device/bluetooth/floss/floss_dbus_client.h"
#include "device/bluetooth/floss/floss_socket_manager.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
namespace floss {
class BluetoothDeviceFloss;
class DEVICE_BLUETOOTH_EXPORT BluetoothSocketFloss
: public device::BluetoothSocketNet {
public:
static scoped_refptr<BluetoothSocketFloss> CreateBluetoothSocket(
scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
scoped_refptr<device::BluetoothSocketThread> socket_thread);
BluetoothSocketFloss(const BluetoothSocketFloss&) = delete;
BluetoothSocketFloss& operator=(const BluetoothSocketFloss&) = delete;
virtual void Connect(BluetoothDeviceFloss* device,
const FlossSocketManager::Security security,
const device::BluetoothUUID& uuid,
base::OnceClosure success_callback,
ErrorCompletionCallback error_callback);
virtual void Listen(
scoped_refptr<device::BluetoothAdapter> adapter,
FlossSocketManager::SocketType socket_type,
const std::optional<device::BluetoothUUID>& uuid,
const device::BluetoothAdapter::ServiceOptions& service_options,
base::OnceClosure success_callback,
ErrorCompletionCallback error_callback);
void Disconnect(base::OnceClosure callback) override;
void Accept(AcceptCompletionCallback success_callback,
ErrorCompletionCallback error_callback) override;
protected:
void DoConnectionStateChanged(FlossSocketManager::ServerSocketState state,
FlossSocketManager::FlossListeningSocket socket,
FlossDBusClient::BtifStatus status);
void DoConnectionAccepted(FlossSocketManager::FlossSocket&& socket);
void CompleteListen(base::OnceClosure success_callback,
ErrorCompletionCallback error_callback,
DBusResult<FlossDBusClient::BtifStatus> result);
void CompleteConnect(base::OnceClosure success_callback,
ErrorCompletionCallback error_callback,
FlossDBusClient::BtifStatus status,
std::optional<FlossSocketManager::FlossSocket>&& socket);
void CompleteAccept(DBusResult<FlossDBusClient::BtifStatus> result);
void CompleteClose(base::OnceClosure callback,
DBusResult<FlossDBusClient::BtifStatus> result);
void CompleteListeningConnect();
void CompleteConnectionInSocketThread(
base::OnceClosure success_callback,
ErrorCompletionCallback error_callback,
FlossDBusClient::BtifStatus status,
std::optional<FlossSocketManager::FlossSocket>&& socket);
private:
struct AcceptRequest {
AcceptRequest();
~AcceptRequest();
AcceptCompletionCallback success_callback;
ErrorCompletionCallback error_callback;
};
BluetoothSocketFloss(
scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
scoped_refptr<device::BluetoothSocketThread> socket_thread);
~BluetoothSocketFloss() override;
scoped_refptr<device::BluetoothAdapter> adapter_;
std::string device_address_;
std::optional<FlossSocketManager::FlossListeningSocket>
listening_socket_info_;
bool is_accepting_ = false;
base::OnceClosure pending_listen_ready_callback_;
base::OnceClosure pending_listen_close_callback_;
FlossSocketManager::FlossSocket connecting_socket_info_;
std::unique_ptr<AcceptRequest> accept_request_;
scoped_refptr<BluetoothSocketFloss> pending_accept_socket_;
base::CancelableTaskTracker socket_task_tracker_;
base::queue<FlossSocketManager::FlossSocket> connection_request_queue_;
base::WeakPtrFactory<BluetoothSocketFloss> weak_ptr_factory_{this};
};
}
#endif