hid_detection_utils.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_utils.h"
  5. #include "base/metrics/histogram_functions.h"
  6. #include "base/strings/strcat.h"
  7. #include "components/device_event_log/device_event_log.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. namespace ash::hid_detection {
  10. namespace {
  11. using InputDeviceType = device::mojom::InputDeviceType;
  12. absl::optional<HidType> GetHidType(
  13. const device::mojom::InputDeviceInfo& device) {
  14. if (device.is_touchscreen || device.is_tablet)
  15. return HidType::kTouchscreen;
  16. if (IsDevicePointer(device)) {
  17. switch (device.type) {
  18. case InputDeviceType::TYPE_BLUETOOTH:
  19. return HidType::kBluetoothPointer;
  20. case InputDeviceType::TYPE_USB:
  21. return HidType::kUsbPointer;
  22. case InputDeviceType::TYPE_SERIO:
  23. return HidType::kSerialPointer;
  24. case InputDeviceType::TYPE_UNKNOWN:
  25. return HidType::kUnknownPointer;
  26. }
  27. }
  28. if (device.is_keyboard) {
  29. switch (device.type) {
  30. case InputDeviceType::TYPE_BLUETOOTH:
  31. return HidType::kBluetoothKeyboard;
  32. case InputDeviceType::TYPE_USB:
  33. return HidType::kUsbKeyboard;
  34. case InputDeviceType::TYPE_SERIO:
  35. return HidType::kSerialKeyboard;
  36. case InputDeviceType::TYPE_UNKNOWN:
  37. return HidType::kUnknownKeyboard;
  38. }
  39. }
  40. return absl::nullopt;
  41. }
  42. } // namespace
  43. bool IsDevicePointer(const device::mojom::InputDeviceInfo& device) {
  44. return device.is_mouse || device.is_touchpad;
  45. }
  46. bool IsDeviceTouchscreen(const device::mojom::InputDeviceInfo& device) {
  47. return device.is_touchscreen || device.is_tablet;
  48. }
  49. void RecordHidConnected(const device::mojom::InputDeviceInfo& device) {
  50. absl::optional<HidType> hid_type = GetHidType(device);
  51. // If |device| is not relevant (i.e. an accelerometer, joystick, etc), don't
  52. // emit metric.
  53. if (!hid_type.has_value()) {
  54. HID_LOG(DEBUG) << "HidConnected not logged for device " << device.id
  55. << " because it doesn't have a relevant device type.";
  56. return;
  57. }
  58. base::UmaHistogramEnumeration("OOBE.HidDetectionScreen.HidConnected",
  59. hid_type.value());
  60. }
  61. void RecordHidDisconnected(const device::mojom::InputDeviceInfo& device) {
  62. absl::optional<HidType> hid_type = GetHidType(device);
  63. // If |device| is not relevant (i.e. an accelerometer, joystick, etc), don't
  64. // emit metric.
  65. if (!hid_type.has_value()) {
  66. HID_LOG(DEBUG) << "HidDisconnected not logged for device " << device.id
  67. << " because it doesn't have a relevant device type.";
  68. return;
  69. }
  70. base::UmaHistogramEnumeration("OOBE.HidDetectionScreen.HidDisconnected",
  71. hid_type.value());
  72. }
  73. void RecordBluetoothPairingAttempts(size_t attempts) {
  74. base::UmaHistogramCounts100(
  75. "OOBE.HidDetectionScreen.BluetoothPairingAttempts", attempts);
  76. }
  77. void RecordBluetoothPairingResult(bool success,
  78. base::TimeDelta pairing_duration) {
  79. base::UmaHistogramCustomTimes(
  80. base::StrCat({"OOBE.HidDetectionScreen.BluetoothPairing.Duration.",
  81. success ? "Success" : "Failure"}),
  82. pairing_duration,
  83. /*min=*/base::Milliseconds(1),
  84. /*max=*/base::Seconds(30), /*buckets=*/50);
  85. // Also record the pairing result metric.
  86. base::UmaHistogramBoolean("OOBE.HidDetectionScreen.BluetoothPairing.Result",
  87. success);
  88. }
  89. void RecordInitialHidsMissing(const HidsMissing& hids_missing) {
  90. base::UmaHistogramEnumeration("OOBE.HidDetectionScreen.InitialHidsMissing",
  91. hids_missing);
  92. }
  93. } // namespace ash::hid_detection