hid_detection_manager_impl.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. #include "ash/components/hid_detection/hid_detection_manager_impl.h"
  5. #include "ash/components/hid_detection/bluetooth_hid_detector_impl.h"
  6. #include "ash/components/hid_detection/hid_detection_utils.h"
  7. #include "base/containers/contains.h"
  8. #include "base/no_destructor.h"
  9. #include "components/device_event_log/device_event_log.h"
  10. namespace ash::hid_detection {
  11. namespace {
  12. using BluetoothHidType = BluetoothHidDetector::BluetoothHidType;
  13. using InputState = HidDetectionManager::InputState;
  14. // Global InputDeviceManagerBinder instance that can be overridden in tests.
  15. base::NoDestructor<HidDetectionManagerImpl::InputDeviceManagerBinder>
  16. g_input_device_manager_binder;
  17. } // namespace
  18. // static
  19. void HidDetectionManagerImpl::SetInputDeviceManagerBinderForTest(
  20. InputDeviceManagerBinder binder) {
  21. *g_input_device_manager_binder = std::move(binder);
  22. }
  23. HidDetectionManagerImpl::HidDetectionManagerImpl(
  24. device::mojom::DeviceService* device_service)
  25. : device_service_{device_service},
  26. bluetooth_hid_detector_{std::make_unique<BluetoothHidDetectorImpl>()} {}
  27. HidDetectionManagerImpl::~HidDetectionManagerImpl() = default;
  28. void HidDetectionManagerImpl::GetIsHidDetectionRequired(
  29. base::OnceCallback<void(bool)> callback) {
  30. BindToInputDeviceManagerIfNeeded();
  31. HID_LOG(EVENT) << "Fetching input devices for GetIsHidDetectionRequired().";
  32. input_device_manager_->GetDevices(
  33. base::BindOnce(&HidDetectionManagerImpl::OnGetDevicesForIsRequired,
  34. weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
  35. }
  36. void HidDetectionManagerImpl::PerformStartHidDetection() {
  37. BindToInputDeviceManagerIfNeeded();
  38. HID_LOG(EVENT) << "Starting HID detection by fetching input devices.";
  39. input_device_manager_->GetDevicesAndSetClient(
  40. input_device_manager_receiver_.BindNewEndpointAndPassRemote(),
  41. base::BindOnce(&HidDetectionManagerImpl::OnGetDevicesAndSetClient,
  42. weak_ptr_factory_.GetWeakPtr()));
  43. }
  44. void HidDetectionManagerImpl::PerformStopHidDetection() {
  45. HID_LOG(EVENT) << "Stopping HID detection.";
  46. input_device_manager_receiver_.reset();
  47. // Check if any of the connected input devices are connected via Bluetooth.
  48. bool is_using_bluetooth = false;
  49. for (const auto& [device_id, device] : device_id_to_device_map_) {
  50. if (device->type == device::mojom::InputDeviceType::TYPE_BLUETOOTH) {
  51. is_using_bluetooth = true;
  52. break;
  53. }
  54. }
  55. bluetooth_hid_detector_->StopBluetoothHidDetection(is_using_bluetooth);
  56. }
  57. HidDetectionManager::HidDetectionStatus
  58. HidDetectionManagerImpl::ComputeHidDetectionStatus() const {
  59. BluetoothHidDetector::BluetoothHidDetectionStatus bluetooth_status =
  60. bluetooth_hid_detector_->GetBluetoothHidDetectionStatus();
  61. return HidDetectionManager::HidDetectionStatus(
  62. GetInputMetadata(connected_pointer_id_, BluetoothHidType::kPointer,
  63. bluetooth_status.current_pairing_device),
  64. GetInputMetadata(connected_keyboard_id_, BluetoothHidType::kKeyboard,
  65. bluetooth_status.current_pairing_device),
  66. connected_touchscreen_id_.has_value(),
  67. std::move(bluetooth_status.pairing_state));
  68. }
  69. void HidDetectionManagerImpl::InputDeviceAdded(
  70. device::mojom::InputDeviceInfoPtr info) {
  71. HID_LOG(EVENT) << "Input device added, id: " << info->id
  72. << ", name: " << info->name;
  73. const std::string& device_id = info->id;
  74. device_id_to_device_map_[device_id] = std::move(info);
  75. hid_detection::RecordHidConnected(*device_id_to_device_map_[device_id]);
  76. if (AttemptSetDeviceAsConnectedHid(*device_id_to_device_map_[device_id])) {
  77. NotifyHidDetectionStatusChanged();
  78. SetInputDevicesStatus();
  79. }
  80. }
  81. void HidDetectionManagerImpl::InputDeviceRemoved(const std::string& id) {
  82. if (!base::Contains(device_id_to_device_map_, id)) {
  83. // Some devices may be removed that were not registered in
  84. // InputDeviceAdded() or OnGetDevicesAndSetClient().
  85. HID_LOG(EVENT)
  86. << "Input device with id: " << id
  87. << " was removed that was not in |device_id_to_device_map_|.";
  88. return;
  89. }
  90. HID_LOG(EVENT) << "Input device removed, id: " << id
  91. << ", name: " << device_id_to_device_map_[id]->name;
  92. hid_detection::RecordHidDisconnected(*device_id_to_device_map_[id]);
  93. device_id_to_device_map_.erase(id);
  94. bool was_connected_hid_disconnected_ = false;
  95. if (id == connected_touchscreen_id_) {
  96. HID_LOG(EVENT) << "Removing connected touchscreen: " << id;
  97. connected_touchscreen_id_.reset();
  98. was_connected_hid_disconnected_ = true;
  99. }
  100. if (id == connected_pointer_id_) {
  101. HID_LOG(EVENT) << "Removing connected pointer: " << id;
  102. connected_pointer_id_.reset();
  103. was_connected_hid_disconnected_ = true;
  104. }
  105. if (id == connected_keyboard_id_) {
  106. HID_LOG(EVENT) << "Removing connected keyboard: " << id;
  107. connected_keyboard_id_.reset();
  108. was_connected_hid_disconnected_ = true;
  109. }
  110. if (was_connected_hid_disconnected_) {
  111. SetConnectedHids();
  112. NotifyHidDetectionStatusChanged();
  113. SetInputDevicesStatus();
  114. }
  115. }
  116. void HidDetectionManagerImpl::OnBluetoothHidStatusChanged() {
  117. NotifyHidDetectionStatusChanged();
  118. }
  119. void HidDetectionManagerImpl::BindToInputDeviceManagerIfNeeded() {
  120. if (input_device_manager_.is_bound())
  121. return;
  122. mojo::PendingReceiver<device::mojom::InputDeviceManager> receiver =
  123. input_device_manager_.BindNewPipeAndPassReceiver();
  124. if (*g_input_device_manager_binder) {
  125. g_input_device_manager_binder->Run(std::move(receiver));
  126. return;
  127. }
  128. DCHECK(device_service_);
  129. device_service_->BindInputDeviceManager(std::move(receiver));
  130. }
  131. void HidDetectionManagerImpl::OnGetDevicesForIsRequired(
  132. base::OnceCallback<void(bool)> callback,
  133. std::vector<device::mojom::InputDeviceInfoPtr> devices) {
  134. bool has_pointer = false;
  135. bool has_keyboard = false;
  136. for (const auto& device : devices) {
  137. if (hid_detection::IsDevicePointer(*device))
  138. has_pointer = true;
  139. if (device->is_keyboard)
  140. has_keyboard = true;
  141. if (has_pointer && has_keyboard)
  142. break;
  143. }
  144. hid_detection::HidsMissing hids_missing = hid_detection::HidsMissing::kNone;
  145. if (!has_pointer) {
  146. if (!has_keyboard) {
  147. hids_missing = hid_detection::HidsMissing::kPointerAndKeyboard;
  148. } else {
  149. hids_missing = hid_detection::HidsMissing::kPointer;
  150. }
  151. } else if (!has_keyboard) {
  152. hids_missing = hid_detection::HidsMissing::kKeyboard;
  153. }
  154. hid_detection::RecordInitialHidsMissing(hids_missing);
  155. HID_LOG(EVENT)
  156. << "Fetched " << devices.size()
  157. << " input devices for GetIsHidDetectionRequired(). Pointer detected: "
  158. << has_pointer << ", keyboard detected: " << has_keyboard;
  159. // HID detection is not required if both devices are present.
  160. std::move(callback).Run(!(has_pointer && has_keyboard));
  161. }
  162. void HidDetectionManagerImpl::OnGetDevicesAndSetClient(
  163. std::vector<device::mojom::InputDeviceInfoPtr> devices) {
  164. DCHECK(device_id_to_device_map_.empty())
  165. << " |devices_| should be empty when fetching initial devices.";
  166. for (auto& device : devices) {
  167. device_id_to_device_map_[device->id] = std::move(device);
  168. }
  169. SetConnectedHids();
  170. NotifyHidDetectionStatusChanged();
  171. bluetooth_hid_detector_->StartBluetoothHidDetection(
  172. this, {.pointer_is_missing = !connected_pointer_id_.has_value(),
  173. .keyboard_is_missing = !connected_keyboard_id_.has_value()});
  174. }
  175. bool HidDetectionManagerImpl::SetConnectedHids() {
  176. HID_LOG(EVENT) << "Setting connected HIDs";
  177. bool is_any_device_newly_connected_hid = false;
  178. for (const auto& [device_id, device] : device_id_to_device_map_) {
  179. is_any_device_newly_connected_hid |=
  180. AttemptSetDeviceAsConnectedHid(*device);
  181. }
  182. return is_any_device_newly_connected_hid;
  183. }
  184. bool HidDetectionManagerImpl::AttemptSetDeviceAsConnectedHid(
  185. const device::mojom::InputDeviceInfo& device) {
  186. bool is_device_newly_connected_hid = false;
  187. if (!connected_touchscreen_id_.has_value() &&
  188. hid_detection::IsDeviceTouchscreen(device)) {
  189. HID_LOG(EVENT) << "Touchscreen detected: " << device.id;
  190. connected_touchscreen_id_ = device.id;
  191. is_device_newly_connected_hid = true;
  192. }
  193. if (!connected_pointer_id_.has_value() &&
  194. hid_detection::IsDevicePointer(device)) {
  195. HID_LOG(EVENT) << "Pointer detected: " << device.id;
  196. connected_pointer_id_ = device.id;
  197. is_device_newly_connected_hid = true;
  198. }
  199. if (!connected_keyboard_id_.has_value() && device.is_keyboard) {
  200. HID_LOG(EVENT) << "Keyboard detected: " << device.id;
  201. connected_keyboard_id_ = device.id;
  202. is_device_newly_connected_hid = true;
  203. }
  204. return is_device_newly_connected_hid;
  205. }
  206. HidDetectionManager::InputMetadata HidDetectionManagerImpl::GetInputMetadata(
  207. const absl::optional<std::string>& connected_device_id,
  208. BluetoothHidType input_type,
  209. const absl::optional<BluetoothHidDetector::BluetoothHidMetadata>&
  210. current_pairing_device) const {
  211. if (connected_device_id.has_value()) {
  212. const device::mojom::InputDeviceInfoPtr& device =
  213. device_id_to_device_map_.find(connected_device_id.value())->second;
  214. DCHECK(device)
  215. << " |connected_device_id| not found in |device_id_to_device_map_|";
  216. InputState state;
  217. switch (device->type) {
  218. case device::mojom::InputDeviceType::TYPE_BLUETOOTH:
  219. state = InputState::kPairedViaBluetooth;
  220. break;
  221. case device::mojom::InputDeviceType::TYPE_USB:
  222. state = InputState::kConnectedViaUsb;
  223. break;
  224. case device::mojom::InputDeviceType::TYPE_SERIO:
  225. [[fallthrough]];
  226. case device::mojom::InputDeviceType::TYPE_UNKNOWN:
  227. state = InputState::kConnected;
  228. break;
  229. }
  230. return InputMetadata{state, device->name};
  231. }
  232. if (current_pairing_device.has_value() &&
  233. (current_pairing_device.value().type == input_type ||
  234. current_pairing_device.value().type ==
  235. BluetoothHidType::kKeyboardPointerCombo)) {
  236. return InputMetadata{InputState::kPairingViaBluetooth,
  237. current_pairing_device.value().name};
  238. }
  239. return InputMetadata();
  240. }
  241. void HidDetectionManagerImpl::SetInputDevicesStatus() {
  242. bluetooth_hid_detector_->SetInputDevicesStatus(
  243. {.pointer_is_missing = !connected_pointer_id_.has_value(),
  244. .keyboard_is_missing = !connected_keyboard_id_.has_value()});
  245. }
  246. void HidDetectionManagerImpl::SetBluetoothHidDetectorForTest(
  247. std::unique_ptr<BluetoothHidDetector> bluetooth_hid_detector) {
  248. bluetooth_hid_detector_ = std::move(bluetooth_hid_detector);
  249. }
  250. } // namespace ash::hid_detection