File: waap_ui_metrics_recorder.cc

package info (click to toggle)
chromium 143.0.7499.109-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 5,786,824 kB
  • sloc: cpp: 35,783,839; ansic: 7,477,365; javascript: 3,962,116; python: 1,480,521; xml: 764,832; asm: 710,816; pascal: 188,028; sh: 88,717; perl: 88,692; objc: 79,984; sql: 57,625; cs: 42,265; fortran: 24,101; makefile: 22,509; tcl: 15,277; php: 14,018; yacc: 9,043; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (165 lines) | stat: -rw-r--r-- 5,360 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/waap/waap_ui_metrics_recorder.h"

#include "base/strings/strcat.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/waap/waap_ui_metrics_service.h"
#include "ui/events/event.h"
#include "ui/events/types/event_type.h"
#include "ui/views/controls/button/button.h"  // For Button::STATE_*

namespace {

// Converts a ui::EventType to a ReloadButtonInputType.
// If the event type is not relevant to the ReloadButton, this will fail
// immediately.
WaapUIMetricsRecorder::ReloadButtonInputType ToReloadButtonInputType(
    const ui::EventType& type) {
  switch (type) {
    case ui::EventType::kMouseReleased:
      return WaapUIMetricsRecorder::ReloadButtonInputType::kMouseRelease;
    case ui::EventType::kKeyPressed:
      return WaapUIMetricsRecorder::ReloadButtonInputType::kKeyPress;
    default:
      NOTREACHED();
  }
}

// Returns true if the event type is relevant to the ReloadButton.
bool IsReloadButtonInputType(const ui::EventType& type) {
  switch (type) {
    case ui::EventType::kMouseReleased:
    case ui::EventType::kKeyPressed:
      return true;
    default:
      return false;
  }
}

}  // namespace

WaapUIMetricsRecorder::WaapUIMetricsRecorder(Profile* profile)
    : waap_service_(WaapUIMetricsService::Get(profile)) {}

WaapUIMetricsRecorder::~WaapUIMetricsRecorder() = default;

void WaapUIMetricsRecorder::OnMouseEntered(base::TimeTicks time) {
  if (mouse_entered_time_.is_null()) {
    mouse_entered_time_ = time;
  }
}

void WaapUIMetricsRecorder::OnMouseExited(base::TimeTicks time) {
  // The next mouseenter will only happen after this mouseexit.
  mouse_entered_time_ = base::TimeTicks();
}

void WaapUIMetricsRecorder::OnMousePressed(base::TimeTicks time) {
  // There can be multiple mousepress events before the mouserelease.
  // Only the timestamp of the last mousepress is remembered.
  mouse_pressed_time_ = time;
}

void WaapUIMetricsRecorder::OnMouseReleased(base::TimeTicks time) {
  mouse_pressed_time_ = base::TimeTicks();
}

void WaapUIMetricsRecorder::OnButtonPressedStart(
    const ui::Event& event,
    ReloadButtonMode current_mode) {
  if (!waap_service_ || !IsReloadButtonInputType(event.type())) {
    return;
  }

  auto input_type = ToReloadButtonInputType(event.type());

  last_input_info_.emplace(LastInputInfo{
      .time = event.time_stamp(),
      .type = input_type,
      .mode_at_input = current_mode,
  });

  waap_service_->OnReloadButtonInput(input_type);
}

void WaapUIMetricsRecorder::DidExecuteStopCommand(base::TimeTicks time) {
  if (!waap_service_ || !last_input_info_.has_value()) {
    return;
  }

  waap_service_->OnReloadButtonInputToStop(last_input_info_->time, time,
                                           last_input_info_->type);
}

// Called after the Reload command has been executed.
void WaapUIMetricsRecorder::DidExecuteReloadCommand(base::TimeTicks time) {
  if (!waap_service_ || !last_input_info_.has_value()) {
    return;
  }

  waap_service_->OnReloadButtonInputToReload(last_input_info_->time, time,
                                             last_input_info_->type);
}

void WaapUIMetricsRecorder::OnChangeVisibleMode(ReloadButtonMode current_mode,
                                                ReloadButtonMode intended_mode,
                                                base::TimeTicks time) {
  if (!waap_service_ || current_mode == intended_mode) {
    return;
  }

  pending_mode_change_.emplace(PendingModeChange{
      .start_time = time,
      .target_mode = intended_mode,
  });
}

void WaapUIMetricsRecorder::OnPaint(ReloadButtonMode visible_mode,
                                    int button_state,
                                    base::TimeTicks now) {
  static bool is_first_paint_recorded = false;
  if (!waap_service_) {
    return;
  }

  // Log first paint metrics.
  if (!is_first_paint_recorded) {
    is_first_paint_recorded = true;
    waap_service_->OnFirstPaint(now);
    waap_service_->OnFirstContentfulPaint(now);
  }

  // Log MousePressToNextPaint.
  if (button_state == views::Button::STATE_PRESSED &&
      !mouse_pressed_time_.is_null()) {
    waap_service_->OnReloadButtonMousePressToNextPaint(mouse_pressed_time_,
                                                       now);
    mouse_pressed_time_ = base::TimeTicks();  // Reset
  }

  // Log MouseHoverToNextPaint.
  if (button_state == views::Button::STATE_HOVERED &&
      !mouse_entered_time_.is_null()) {
    waap_service_->OnReloadButtonMouseHoverToNextPaint(mouse_entered_time_,
                                                       now);
    mouse_entered_time_ = base::TimeTicks();  // Reset
  }

  // Log InputToNextPaint.
  if (last_input_info_.has_value()) {
    waap_service_->OnReloadButtonInputToNextPaint(last_input_info_->time, now,
                                                  last_input_info_->type);
    last_input_info_.reset();
  }

  // Log ChangeVisibleModeToNextPaint.
  if (pending_mode_change_.has_value() &&
      visible_mode == pending_mode_change_->target_mode) {
    waap_service_->OnReloadButtonChangeVisibleModeToNextPaint(
        pending_mode_change_->start_time, now, visible_mode);
    pending_mode_change_.reset();
  }
}