pointer_metrics_recorder.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2016 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/metrics/pointer_metrics_recorder.h"
  5. #include "ash/constants/app_types.h"
  6. #include "ash/display/screen_orientation_controller.h"
  7. #include "ash/shell.h"
  8. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  9. #include "base/metrics/histogram_macros.h"
  10. #include "ui/aura/client/aura_constants.h"
  11. #include "ui/aura/window.h"
  12. #include "ui/events/event_constants.h"
  13. #include "ui/events/types/event_type.h"
  14. #include "ui/views/widget/widget.h"
  15. namespace ash {
  16. namespace {
  17. int GetDestination(views::Widget* target) {
  18. if (!target)
  19. return static_cast<int>(AppType::NON_APP);
  20. aura::Window* window = target->GetNativeWindow();
  21. DCHECK(window);
  22. int app_type = window->GetProperty(aura::client::kAppType);
  23. // Use "BROWSER" for Lacros Chrome's pointer metrics.
  24. if (app_type == static_cast<int>(AppType::LACROS))
  25. return static_cast<int>(AppType::BROWSER);
  26. return app_type;
  27. }
  28. DownEventMetric2 FindCombination(int destination,
  29. DownEventSource input_type,
  30. DownEventFormFactor form_factor) {
  31. constexpr int kNumCombinationPerDestination =
  32. static_cast<int>(DownEventSource::kSourceCount) *
  33. static_cast<int>(DownEventFormFactor::kFormFactorCount);
  34. int result = destination * kNumCombinationPerDestination +
  35. static_cast<int>(DownEventFormFactor::kFormFactorCount) *
  36. static_cast<int>(input_type) +
  37. static_cast<int>(form_factor);
  38. DCHECK(result >= 0 &&
  39. result <= static_cast<int>(DownEventMetric2::kMaxValue));
  40. return static_cast<DownEventMetric2>(result);
  41. }
  42. void RecordUMA(ui::EventPointerType type, ui::EventTarget* event_target) {
  43. DCHECK_NE(type, ui::EventPointerType::kUnknown);
  44. views::Widget* target = views::Widget::GetTopLevelWidgetForNativeView(
  45. static_cast<aura::Window*>(event_target));
  46. DownEventFormFactor form_factor = DownEventFormFactor::kClamshell;
  47. if (Shell::Get()->tablet_mode_controller()->InTabletMode()) {
  48. chromeos::OrientationType screen_orientation =
  49. Shell::Get()->screen_orientation_controller()->GetCurrentOrientation();
  50. if (screen_orientation == chromeos::OrientationType::kLandscapePrimary ||
  51. screen_orientation == chromeos::OrientationType::kLandscapeSecondary) {
  52. form_factor = DownEventFormFactor::kTabletModeLandscape;
  53. } else {
  54. form_factor = DownEventFormFactor::kTabletModePortrait;
  55. }
  56. }
  57. DownEventSource input_type = DownEventSource::kUnknown;
  58. switch (type) {
  59. case ui::EventPointerType::kUnknown:
  60. return;
  61. case ui::EventPointerType::kMouse:
  62. input_type = DownEventSource::kMouse;
  63. break;
  64. case ui::EventPointerType::kPen:
  65. input_type = DownEventSource::kStylus;
  66. break;
  67. case ui::EventPointerType::kTouch:
  68. input_type = DownEventSource::kTouch;
  69. break;
  70. case ui::EventPointerType::kEraser:
  71. input_type = DownEventSource::kStylus;
  72. break;
  73. }
  74. UMA_HISTOGRAM_ENUMERATION(
  75. "Event.DownEventCount.PerInputFormFactorDestinationCombination2",
  76. FindCombination(GetDestination(target), input_type, form_factor));
  77. }
  78. } // namespace
  79. PointerMetricsRecorder::PointerMetricsRecorder() {
  80. Shell::Get()->AddPreTargetHandler(this);
  81. }
  82. PointerMetricsRecorder::~PointerMetricsRecorder() {
  83. Shell::Get()->RemovePreTargetHandler(this);
  84. }
  85. void PointerMetricsRecorder::OnMouseEvent(ui::MouseEvent* event) {
  86. if (event->type() == ui::ET_MOUSE_PRESSED)
  87. RecordUMA(event->pointer_details().pointer_type, event->target());
  88. }
  89. void PointerMetricsRecorder::OnTouchEvent(ui::TouchEvent* event) {
  90. if (event->type() == ui::ET_TOUCH_PRESSED)
  91. RecordUMA(event->pointer_details().pointer_type, event->target());
  92. }
  93. } // namespace ash