#ifndef SERVICES_DEVICE_HID_HID_CONNECTION_H_
#define SERVICES_DEVICE_HID_HID_CONNECTION_H_
#include <stddef.h>
#include <stdint.h>
#include <tuple>
#include "base/containers/queue.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/sequence_checker.h"
#include "services/device/hid/hid_device_info.h"
namespace base {
class RefCountedBytes;
}
namespace device {
class HidConnection : public base::RefCountedThreadSafe<HidConnection> {
public:
enum SpecialReportIds {
kNullReportId = 0x00,
kAnyReportId = 0xFF,
};
using ReadCallback =
base::OnceCallback<void(bool success,
scoped_refptr<base::RefCountedBytes> buffer,
size_t size)>;
using WriteCallback = base::OnceCallback<void(bool success)>;
class Client {
public:
virtual void OnInputReport(scoped_refptr<base::RefCountedBytes> buffer,
size_t size) = 0;
};
HidConnection(HidConnection&) = delete;
HidConnection& operator=(HidConnection&) = delete;
void SetClient(Client* client);
scoped_refptr<HidDeviceInfo> device_info() const { return device_info_; }
bool has_always_protected_collection() const {
return has_always_protected_collection_;
}
bool closed() const { return closed_; }
void Close();
void Read(ReadCallback callback);
void Write(scoped_refptr<base::RefCountedBytes> buffer,
WriteCallback callback);
void GetFeatureReport(uint8_t report_id, ReadCallback callback);
void SendFeatureReport(scoped_refptr<base::RefCountedBytes> buffer,
WriteCallback callback);
protected:
friend class base::RefCountedThreadSafe<HidConnection>;
HidConnection(scoped_refptr<HidDeviceInfo> device_info,
bool allow_protected_reports,
bool allow_fido_reports);
virtual ~HidConnection();
virtual void PlatformClose() = 0;
virtual void PlatformWrite(scoped_refptr<base::RefCountedBytes> buffer,
WriteCallback callback) = 0;
virtual void PlatformGetFeatureReport(uint8_t report_id,
ReadCallback callback) = 0;
virtual void PlatformSendFeatureReport(
scoped_refptr<base::RefCountedBytes> buffer,
WriteCallback callback) = 0;
bool IsReportIdProtected(uint8_t report_id, HidReportType report_type);
void ProcessInputReport(scoped_refptr<base::RefCountedBytes> buffer,
size_t size);
void ProcessReadQueue();
private:
scoped_refptr<HidDeviceInfo> device_info_;
const bool allow_protected_reports_;
const bool allow_fido_reports_;
raw_ptr<Client> client_ = nullptr;
bool has_always_protected_collection_;
bool closed_;
base::queue<std::tuple<scoped_refptr<base::RefCountedBytes>, size_t>>
pending_reports_;
base::queue<ReadCallback> pending_reads_;
SEQUENCE_CHECKER(sequence_checker_);
};
}
#endif