hid_connection_impl.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2017 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_IMPL_H_
  5. #define SERVICES_DEVICE_HID_HID_CONNECTION_IMPL_H_
  6. #include "base/memory/ref_counted.h"
  7. #include "mojo/public/cpp/bindings/remote.h"
  8. #include "services/device/hid/hid_connection.h"
  9. #include "services/device/public/mojom/hid.mojom.h"
  10. namespace device {
  11. // HidConnectionImpl is reponsible for handling mojo communications from
  12. // clients. It delegates to HidConnection the real work of creating
  13. // connections in different platforms.
  14. class HidConnectionImpl final : public mojom::HidConnection,
  15. public HidConnection::Client {
  16. public:
  17. // Creates a strongly-bound HidConnectionImpl owned by |receiver| and
  18. // |watcher|. |connection| provides access to the HID device. If
  19. // |connection_client| is bound, it will be notified when input reports are
  20. // received. |watcher|, if bound, will be disconnected when the connection is
  21. // closed.
  22. static void Create(
  23. scoped_refptr<device::HidConnection> connection,
  24. mojo::PendingReceiver<mojom::HidConnection> receiver,
  25. mojo::PendingRemote<mojom::HidConnectionClient> connection_client,
  26. mojo::PendingRemote<mojom::HidConnectionWatcher> watcher);
  27. HidConnectionImpl(const HidConnectionImpl&) = delete;
  28. HidConnectionImpl& operator=(const HidConnectionImpl&) = delete;
  29. // HidConnection::Client implementation:
  30. void OnInputReport(scoped_refptr<base::RefCountedBytes> buffer,
  31. size_t size) override;
  32. // mojom::HidConnection implementation:
  33. void Read(ReadCallback callback) override;
  34. void Write(uint8_t report_id,
  35. const std::vector<uint8_t>& buffer,
  36. WriteCallback callback) override;
  37. void GetFeatureReport(uint8_t report_id,
  38. GetFeatureReportCallback callback) override;
  39. void SendFeatureReport(uint8_t report_id,
  40. const std::vector<uint8_t>& buffer,
  41. SendFeatureReportCallback callback) override;
  42. private:
  43. friend class HidConnectionImplTest;
  44. HidConnectionImpl(
  45. scoped_refptr<device::HidConnection> connection,
  46. mojo::PendingReceiver<mojom::HidConnection> receiver,
  47. mojo::PendingRemote<mojom::HidConnectionClient> connection_client,
  48. mojo::PendingRemote<mojom::HidConnectionWatcher> watcher);
  49. ~HidConnectionImpl() final;
  50. void OnRead(ReadCallback callback,
  51. bool success,
  52. scoped_refptr<base::RefCountedBytes> buffer,
  53. size_t size);
  54. void OnWrite(WriteCallback callback, bool success);
  55. void OnGetFeatureReport(GetFeatureReportCallback callback,
  56. bool success,
  57. scoped_refptr<base::RefCountedBytes> buffer,
  58. size_t size);
  59. void OnSendFeatureReport(SendFeatureReportCallback callback, bool success);
  60. mojo::Receiver<mojom::HidConnection> receiver_;
  61. scoped_refptr<device::HidConnection> hid_connection_;
  62. // Client interfaces.
  63. mojo::Remote<mojom::HidConnectionClient> client_;
  64. mojo::Remote<mojom::HidConnectionWatcher> watcher_;
  65. base::WeakPtrFactory<HidConnectionImpl> weak_factory_{this};
  66. };
  67. } // namespace device
  68. #endif // SERVICES_DEVICE_HID_HID_CONNECTION_IMPL_H_