hid_connection_win.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef SERVICES_DEVICE_HID_HID_CONNECTION_WIN_H_
  5. #define SERVICES_DEVICE_HID_HID_CONNECTION_WIN_H_
  6. #include <windows.h>
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include <list>
  10. #include "base/containers/flat_set.h"
  11. #include "base/win/scoped_handle.h"
  12. #include "services/device/hid/hid_connection.h"
  13. namespace device {
  14. class PendingHidTransfer;
  15. class HidConnectionWin : public HidConnection {
  16. public:
  17. // On Windows, a single HID interface may be represented by multiple file
  18. // handles where each file handle represents one top-level HID collection.
  19. // Maintain a mapping of report IDs to open file handles so that the correct
  20. // handle is used for each report supported by the device.
  21. struct HidDeviceEntry {
  22. HidDeviceEntry(base::flat_set<uint8_t> report_ids,
  23. base::win::ScopedHandle file_handle);
  24. ~HidDeviceEntry();
  25. // Reports with these IDs will be routed to |file_handle|.
  26. base::flat_set<uint8_t> report_ids;
  27. // An open file handle representing a HID top-level collection.
  28. base::win::ScopedHandle file_handle;
  29. };
  30. static scoped_refptr<HidConnection> Create(
  31. scoped_refptr<HidDeviceInfo> device_info,
  32. std::vector<std::unique_ptr<HidDeviceEntry>> file_handles,
  33. bool allow_protected_reports,
  34. bool allow_fido_reports);
  35. HidConnectionWin(HidConnectionWin&) = delete;
  36. HidConnectionWin& operator=(HidConnectionWin&) = delete;
  37. private:
  38. friend class HidServiceWin;
  39. friend class PendingHidTransfer;
  40. HidConnectionWin(scoped_refptr<HidDeviceInfo> device_info,
  41. std::vector<std::unique_ptr<HidDeviceEntry>> file_handles,
  42. bool allow_protected_reports,
  43. bool allow_fido_reports);
  44. ~HidConnectionWin() override;
  45. // HidConnection implementation.
  46. void PlatformClose() override;
  47. void PlatformWrite(scoped_refptr<base::RefCountedBytes> buffer,
  48. WriteCallback callback) override;
  49. void PlatformGetFeatureReport(uint8_t report_id,
  50. ReadCallback callback) override;
  51. void PlatformSendFeatureReport(scoped_refptr<base::RefCountedBytes> buffer,
  52. WriteCallback callback) override;
  53. // Start listening for input reports from all devices in |file_handles_|.
  54. void SetUpInitialReads();
  55. // Listen for the next input report from |file_handle|.
  56. void ReadNextInputReportOnHandle(HANDLE file_handle);
  57. void OnReadInputReport(HANDLE file_handle,
  58. scoped_refptr<base::RefCountedBytes> buffer,
  59. PendingHidTransfer* transfer,
  60. bool signaled);
  61. void OnReadFeatureComplete(HANDLE file_handle,
  62. scoped_refptr<base::RefCountedBytes> buffer,
  63. ReadCallback callback,
  64. PendingHidTransfer* transfer,
  65. bool signaled);
  66. void OnWriteComplete(HANDLE file_handle,
  67. WriteCallback callback,
  68. PendingHidTransfer* transfer,
  69. bool signaled);
  70. std::unique_ptr<PendingHidTransfer> UnlinkTransfer(
  71. PendingHidTransfer* transfer);
  72. HANDLE GetHandleForReportId(uint8_t report_id) const;
  73. std::vector<std::unique_ptr<HidDeviceEntry>> file_handles_;
  74. std::list<std::unique_ptr<PendingHidTransfer>> transfers_;
  75. };
  76. } // namespace device
  77. #endif // SERVICES_DEVICE_HID_HID_CONNECTION_WIN_H_