hid_detection_manager_impl.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_IMPL_H_
  5. #define ASH_COMPONENTS_HID_DETECTION_HID_DETECTION_MANAGER_IMPL_H_
  6. #include "ash/components/hid_detection/hid_detection_manager.h"
  7. #include "ash/components/hid_detection/bluetooth_hid_detector.h"
  8. #include "base/callback.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "mojo/public/cpp/bindings/associated_receiver.h"
  11. #include "mojo/public/cpp/bindings/receiver.h"
  12. #include "mojo/public/cpp/bindings/remote.h"
  13. #include "services/device/public/mojom/device_service.mojom.h"
  14. #include "services/device/public/mojom/input_service.mojom.h"
  15. namespace ash::hid_detection {
  16. // Concrete HidDetectionManager implementation that uses InputDeviceManager and
  17. // BluetoothHidDetectorImpl to detect and connect with devices.
  18. class HidDetectionManagerImpl : public HidDetectionManager,
  19. public device::mojom::InputDeviceManagerClient,
  20. public BluetoothHidDetector::Delegate {
  21. public:
  22. using InputDeviceManagerBinder = base::RepeatingCallback<void(
  23. mojo::PendingReceiver<device::mojom::InputDeviceManager>)>;
  24. // Allows tests to override how this class binds InputDeviceManager receivers.
  25. static void SetInputDeviceManagerBinderForTest(
  26. InputDeviceManagerBinder binder);
  27. explicit HidDetectionManagerImpl(
  28. device::mojom::DeviceService* device_service);
  29. ~HidDetectionManagerImpl() override;
  30. private:
  31. friend class HidDetectionManagerImplTest;
  32. // HidDetectionManager:
  33. void GetIsHidDetectionRequired(
  34. base::OnceCallback<void(bool)> callback) override;
  35. void PerformStartHidDetection() override;
  36. void PerformStopHidDetection() override;
  37. HidDetectionManager::HidDetectionStatus ComputeHidDetectionStatus()
  38. const override;
  39. // device::mojom::InputDeviceManagerClient:
  40. void InputDeviceAdded(device::mojom::InputDeviceInfoPtr info) override;
  41. void InputDeviceRemoved(const std::string& id) override;
  42. // BluetoothHidDetector::Delegate:
  43. void OnBluetoothHidStatusChanged() override;
  44. void BindToInputDeviceManagerIfNeeded();
  45. // Processes the list of input devices fetched by GetIsHidDetectionRequired().
  46. // Invokes |callback| with whether HID detection is required or not. The
  47. // returned device list is not saved.
  48. void OnGetDevicesForIsRequired(
  49. base::OnceCallback<void(bool)> callback,
  50. std::vector<device::mojom::InputDeviceInfoPtr> devices);
  51. // Populates |device_id_to_device_map_| with the initial input devices
  52. // connected when HID starts and sets the connected HIDs.
  53. void OnGetDevicesAndSetClient(
  54. std::vector<device::mojom::InputDeviceInfoPtr> devices);
  55. // Iterates through |device_id_to_device_map_| and attempts to set each
  56. // entry as a connected HID. Returns true if any of the entries were set as
  57. // connected and false otherwise.
  58. bool SetConnectedHids();
  59. // Sets |device| as a connected HID if device is an applicable HID and no
  60. // device of the same type is already connected. Returns true if the device
  61. // was set as connected and false otherwise.
  62. bool AttemptSetDeviceAsConnectedHid(
  63. const device::mojom::InputDeviceInfo& device);
  64. // Computes an InputMetadata based on if an input device is connected or if a
  65. // Bluetooth device of the same type as |input_type| is pairing. A null
  66. // |connected_device_id| means no HID for that input is connected. If
  67. // |connected_device_id| is not null, it must be a key for an entry in
  68. // |device_id_to_device_map_|. A null |current_pairing_device| means no
  69. // Bluetooth device is pairing.
  70. InputMetadata GetInputMetadata(
  71. const absl::optional<std::string>& connected_device_id,
  72. BluetoothHidDetector::BluetoothHidType input_type,
  73. const absl::optional<BluetoothHidDetector::BluetoothHidMetadata>&
  74. current_pairing_device) const;
  75. // Informs |bluetooth_hid_detector_| what devices are missing.
  76. void SetInputDevicesStatus();
  77. // Allows tests to override the BluetoothHidDetector implementation used.
  78. void SetBluetoothHidDetectorForTest(
  79. std::unique_ptr<BluetoothHidDetector> bluetooth_hid_detector);
  80. std::map<std::string, device::mojom::InputDeviceInfoPtr>
  81. device_id_to_device_map_;
  82. absl::optional<std::string> connected_touchscreen_id_;
  83. absl::optional<std::string> connected_pointer_id_;
  84. absl::optional<std::string> connected_keyboard_id_;
  85. device::mojom::DeviceService* device_service_ = nullptr;
  86. std::unique_ptr<BluetoothHidDetector> bluetooth_hid_detector_;
  87. mojo::Remote<device::mojom::InputDeviceManager> input_device_manager_;
  88. mojo::AssociatedReceiver<device::mojom::InputDeviceManagerClient>
  89. input_device_manager_receiver_{this};
  90. base::WeakPtrFactory<HidDetectionManagerImpl> weak_ptr_factory_{this};
  91. };
  92. } // namespace ash::hid_detection
  93. #endif // ASH_COMPONENTS_HID_DETECTION_HID_DETECTION_MANAGER_IMPL_H_