File: click_input_tracker.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rw-r--r-- 3,489 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/page_load_metrics/browser/observers/click_input_tracker.h"

#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_macros.h"
#include "components/page_load_metrics/browser/features.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "third_party/blink/public/common/input/web_gesture_event.h"
#include "third_party/blink/public/common/input/web_mouse_event.h"

namespace page_load_metrics {

namespace {

bool IsClickInputTrackerEnabled() {
  static bool enabled = base::FeatureList::IsEnabled(
      page_load_metrics::features::kClickInputTracker);
  return enabled;
}

}  // namespace

const int kMaxCountForUkm = 50;

ClickInputTracker::ClickInputTracker() {
  // Set thresholds per Feature parameters as appropriate.
  time_delta_threshold_ =
      base::Milliseconds(base::GetFieldTrialParamByFeatureAsInt(
          page_load_metrics::features::kClickInputTracker,
          "time_delta_threshold_ms", 500));
  position_delta_threshold_ = base::GetFieldTrialParamByFeatureAsInt(
      page_load_metrics::features::kClickInputTracker,
      "position_delta_threshold", 10);
  burst_count_threshold_ = base::GetFieldTrialParamByFeatureAsInt(
      page_load_metrics::features::kClickInputTracker, "burst_count_threshold",
      3);
}

ClickInputTracker::~ClickInputTracker() = default;

void ClickInputTracker::OnUserInput(const blink::WebInputEvent& event) {
  if (!IsClickInputTrackerEnabled()) {
    return;
  }

  if (event.GetType() != blink::WebInputEvent::Type::kGestureTap &&
      event.GetType() != blink::WebInputEvent::Type::kMouseUp) {
    return;
  }

  gfx::PointF position;
  if (event.GetType() == blink::WebInputEvent::Type::kGestureTap) {
    const blink::WebGestureEvent& gesture =
        static_cast<const blink::WebGestureEvent&>(event);
    position = gesture.PositionInScreen();
  } else if (event.GetType() == blink::WebInputEvent::Type::kMouseUp) {
    const blink::WebMouseEvent& mouse_click =
        static_cast<const blink::WebMouseEvent&>(event);
    position = mouse_click.PositionInScreen();
  } else {
    NOTREACHED();
  }

  if (!last_click_timestamp_.is_null()) {
    base::TimeDelta delta = event.TimeStamp() - last_click_timestamp_;
    if (delta < time_delta_threshold_ &&
        std::abs(position.x() - last_click_position_.x()) <
            position_delta_threshold_ &&
        std::abs(position.y() - last_click_position_.y()) <
            position_delta_threshold_) {
      current_click_input_burst_++;
      max_click_input_burst_ =
          std::max(max_click_input_burst_, current_click_input_burst_);
    } else {
      current_click_input_burst_ = 1;
    }
  } else {
    current_click_input_burst_ = 1;
  }

  last_click_timestamp_ = event.TimeStamp();
  last_click_position_ = position;
}

void ClickInputTracker::RecordClickBurst(ukm::SourceId source_id) {
  if (!IsClickInputTrackerEnabled()) {
    return;
  }

  if (max_click_input_burst_ >= burst_count_threshold_) {
    UMA_HISTOGRAM_COUNTS_100("PageLoad.Experimental.ClickInputBurst",
                             max_click_input_burst_);
    ukm::builders::ClickInput(source_id)
        .SetExperimental_ClickInputBurst(
            std::min(max_click_input_burst_, kMaxCountForUkm))
        .Record(ukm::UkmRecorder::Get());
  }
}

}  // namespace page_load_metrics