hid_detection_manager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2022 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 ASH_COMPONENTS_HID_DETECTION_HID_DETECTION_MANAGER_H_
  5. #define ASH_COMPONENTS_HID_DETECTION_HID_DETECTION_MANAGER_H_
  6. #include "base/callback.h"
  7. #include "ash/components/hid_detection/bluetooth_hid_detector.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. namespace ash::hid_detection {
  10. // Manages detecting and automatically connecting to human interface devices.
  11. class HidDetectionManager {
  12. public:
  13. // The connection state of an input.
  14. enum class InputState {
  15. // No device is connected.
  16. kSearching,
  17. // A device is connected via USB.
  18. kConnectedViaUsb,
  19. // A device is being paired with via Bluetooth.
  20. kPairingViaBluetooth,
  21. // A device is connected via Bluetooth.
  22. kPairedViaBluetooth,
  23. // A device is connected, but is not known to be USB (Bluetooth vs USB vs
  24. // serial).
  25. kConnected
  26. };
  27. // Info of an input on the device.
  28. struct InputMetadata {
  29. InputState state = InputState::kSearching;
  30. // The name of the HID currently being interfaced with. Empty if |state| is
  31. // kSearching (no HID is being interfaced with).
  32. std::string detected_hid_name;
  33. };
  34. // Represents the status of inputs on the device.
  35. struct HidDetectionStatus {
  36. HidDetectionStatus(InputMetadata pointer_metadata,
  37. InputMetadata keyboard_metadata,
  38. bool touchscreen_detected,
  39. absl::optional<BluetoothHidPairingState> pairing_state);
  40. HidDetectionStatus(HidDetectionStatus&& other);
  41. HidDetectionStatus& operator=(HidDetectionStatus&& other);
  42. ~HidDetectionStatus();
  43. // Pointer input info of the device.
  44. InputMetadata pointer_metadata;
  45. // Keyboard input info of the device.
  46. InputMetadata keyboard_metadata;
  47. // Indicates the device has a touchscreen connected.
  48. bool touchscreen_detected = false;
  49. // Set if the current pairing requires a code that should be displayed to
  50. // the user to enter.
  51. absl::optional<BluetoothHidPairingState> pairing_state;
  52. };
  53. class Delegate {
  54. public:
  55. virtual ~Delegate() = default;
  56. // Invoked whenever any HID detection status property changes.
  57. virtual void OnHidDetectionStatusChanged(HidDetectionStatus status) = 0;
  58. };
  59. virtual ~HidDetectionManager();
  60. // Invokes |callback| with a result indicating whether HID detection is
  61. // required or not. If both a keyboard and pointer are connected, HID
  62. // detection is not required, otherwise it is.
  63. virtual void GetIsHidDetectionRequired(
  64. base::OnceCallback<void(bool)> callback) = 0;
  65. // Begins scanning for HIDs. Informs |delegate| every time
  66. // the status of HID detection changes. This should only be called once in the
  67. // lifetime of this class.
  68. void StartHidDetection(Delegate* delegate);
  69. // Stops scanning for HIDs. This should only be called while HID detection is
  70. // active.
  71. void StopHidDetection();
  72. protected:
  73. HidDetectionManager();
  74. // Implementation-specific version of StartHidDetection().
  75. virtual void PerformStartHidDetection() = 0;
  76. // Implementation-specific version of StopHidDetection().
  77. virtual void PerformStopHidDetection() = 0;
  78. // Computes the HID detection status sent to |delegate_|.
  79. virtual HidDetectionStatus ComputeHidDetectionStatus() const = 0;
  80. // Notifies |delegate_| of status changes; should be called by derived
  81. // types to notify observers of status changes.
  82. void NotifyHidDetectionStatusChanged();
  83. Delegate* delegate_ = nullptr;
  84. };
  85. } // namespace ash::hid_detection
  86. #endif // ASH_COMPONENTS_HID_DETECTION_HID_DETECTION_MANAGER_H_