bluetooth_hid_detector_impl.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_BLUETOOTH_HID_DETECTOR_IMPL_H_
  5. #define ASH_COMPONENTS_HID_DETECTION_BLUETOOTH_HID_DETECTOR_IMPL_H_
  6. #include "ash/components/hid_detection/bluetooth_hid_detector.h"
  7. #include "base/containers/flat_set.h"
  8. #include "base/containers/queue.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/timer/elapsed_timer.h"
  11. #include "chromeos/services/bluetooth_config/public/mojom/cros_bluetooth_config.mojom.h"
  12. #include "mojo/public/cpp/bindings/receiver.h"
  13. #include "mojo/public/cpp/bindings/remote.h"
  14. namespace ash::hid_detection {
  15. // Concrete BluetoothHidDetector implementation that uses CrosBluetoothConfig.
  16. class BluetoothHidDetectorImpl
  17. : public BluetoothHidDetector,
  18. public chromeos::bluetooth_config::mojom::SystemPropertiesObserver,
  19. public chromeos::bluetooth_config::mojom::BluetoothDiscoveryDelegate,
  20. public chromeos::bluetooth_config::mojom::DevicePairingDelegate,
  21. public chromeos::bluetooth_config::mojom::KeyEnteredHandler {
  22. public:
  23. BluetoothHidDetectorImpl();
  24. ~BluetoothHidDetectorImpl() override;
  25. // BluetoothHidDetector:
  26. void SetInputDevicesStatus(InputDevicesStatus input_devices_status) override;
  27. const BluetoothHidDetectionStatus GetBluetoothHidDetectionStatus() override;
  28. private:
  29. // States used for internal state machine.
  30. enum State {
  31. // HID detection is currently not active.
  32. kNotStarted,
  33. // HID detection has began activating.
  34. kStarting,
  35. // HID detection has began activating and is waiting for the Bluetooth
  36. // adapter to be enabled.
  37. kEnablingAdapter,
  38. // HID detection is fully active and is now searching for devices.
  39. kDetecting,
  40. // HID detection is paused due to the Bluetooth adapter becoming unenabled
  41. // for external reasons.
  42. kStoppedExternally,
  43. };
  44. // BluetoothHidDetector:
  45. void PerformStartBluetoothHidDetection(
  46. InputDevicesStatus input_devices_status) override;
  47. void PerformStopBluetoothHidDetection(bool is_using_bluetooth) override;
  48. // chromeos::bluetooth_config::mojom::SystemPropertiesObserver
  49. void OnPropertiesUpdated(
  50. chromeos::bluetooth_config::mojom::BluetoothSystemPropertiesPtr
  51. properties) override;
  52. // chromeos::bluetooth_config::mojom::BluetoothDiscoveryDelegate
  53. void OnBluetoothDiscoveryStarted(
  54. mojo::PendingRemote<
  55. chromeos::bluetooth_config::mojom::DevicePairingHandler> handler)
  56. override;
  57. void OnBluetoothDiscoveryStopped() override;
  58. void OnDiscoveredDevicesListChanged(
  59. std::vector<
  60. chromeos::bluetooth_config::mojom::BluetoothDevicePropertiesPtr>
  61. discovered_devices) override;
  62. // chromeos::bluetooth_config::mojom::DevicePairingDelegate
  63. void RequestPinCode(RequestPinCodeCallback callback) override;
  64. void RequestPasskey(RequestPasskeyCallback callback) override;
  65. void DisplayPinCode(const std::string& pin_code,
  66. mojo::PendingReceiver<
  67. chromeos::bluetooth_config::mojom::KeyEnteredHandler>
  68. handler) override;
  69. void DisplayPasskey(const std::string& passkey,
  70. mojo::PendingReceiver<
  71. chromeos::bluetooth_config::mojom::KeyEnteredHandler>
  72. handler) override;
  73. void ConfirmPasskey(const std::string& passkey,
  74. ConfirmPasskeyCallback callback) override;
  75. void AuthorizePairing(AuthorizePairingCallback callback) override;
  76. // chromeos::bluetooth_config::mojom::KeyEnteredHandler
  77. void HandleKeyEntered(uint8_t num_keys_entered) override;
  78. bool IsHidTypeMissing(BluetoothHidDetector::BluetoothHidType hid_type);
  79. bool ShouldAttemptToPairWithDevice(
  80. const chromeos::bluetooth_config::mojom::BluetoothDevicePropertiesPtr&
  81. device);
  82. void ProcessQueue();
  83. void OnPairDevice(
  84. std::unique_ptr<base::ElapsedTimer> pairing_timer,
  85. chromeos::bluetooth_config::mojom::PairingResult pairing_result);
  86. // Removes any state related to the current pairing device. This will cancel
  87. // pairing with the device if there is an ongoing pairing.
  88. void ClearCurrentPairingState();
  89. // Resets properties related to discovery, pairing handlers and queueing.
  90. void ResetDiscoveryState();
  91. // Informs the client "DisplayPasskey" or "DisplayPinCode" pairing
  92. // authorization is required.
  93. void RequirePairingCode(
  94. const std::string& code,
  95. mojo::PendingReceiver<
  96. chromeos::bluetooth_config::mojom::KeyEnteredHandler> handler);
  97. // Map that contains the ids of the devices in |queue_|.
  98. base::flat_set<std::string> queued_device_ids_;
  99. // The queue of devices that will be attempted to be paired with.
  100. std::unique_ptr<base::queue<
  101. chromeos::bluetooth_config::mojom::BluetoothDevicePropertiesPtr>>
  102. queue_ = std::make_unique<base::queue<
  103. chromeos::bluetooth_config::mojom::BluetoothDevicePropertiesPtr>>();
  104. // The device currently being paired with.
  105. absl::optional<
  106. chromeos::bluetooth_config::mojom::BluetoothDevicePropertiesPtr>
  107. current_pairing_device_;
  108. // If defined, indicates that the current pairing requires an authorization
  109. // code that should be displayed to the user for them to enter into the HID.
  110. absl::optional<BluetoothHidPairingState> current_pairing_state_;
  111. InputDevicesStatus input_devices_status_;
  112. State state_ = kNotStarted;
  113. // This is a counter used to emit a count of the number of pairing attempts
  114. // that occur while HID detection is active. The count is reset to zero each
  115. // time a HID detection session is started.
  116. size_t num_pairing_attempts_ = 0;
  117. mojo::Remote<chromeos::bluetooth_config::mojom::CrosBluetoothConfig>
  118. cros_bluetooth_config_remote_;
  119. mojo::Receiver<chromeos::bluetooth_config::mojom::SystemPropertiesObserver>
  120. system_properties_observer_receiver_{this};
  121. mojo::Receiver<chromeos::bluetooth_config::mojom::BluetoothDiscoveryDelegate>
  122. bluetooth_discovery_delegate_receiver_{this};
  123. mojo::Remote<chromeos::bluetooth_config::mojom::DevicePairingHandler>
  124. device_pairing_handler_remote_;
  125. mojo::Receiver<chromeos::bluetooth_config::mojom::DevicePairingDelegate>
  126. device_pairing_delegate_receiver_{this};
  127. mojo::Receiver<chromeos::bluetooth_config::mojom::KeyEnteredHandler>
  128. key_entered_handler_receiver_{this};
  129. base::WeakPtrFactory<BluetoothHidDetectorImpl> weak_ptr_factory_{this};
  130. };
  131. } // namespace ash::hid_detection
  132. #endif // ASH_COMPONENTS_HID_DETECTION_BLUETOOTH_HID_DETECTOR_IMPL_H_