File: waap_ui_metrics_recorder.h

package info (click to toggle)
chromium 143.0.7499.109-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (106 lines) | stat: -rw-r--r-- 3,994 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
// 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.

#ifndef CHROME_BROWSER_UI_WAAP_WAAP_UI_METRICS_RECORDER_H_
#define CHROME_BROWSER_UI_WAAP_WAAP_UI_METRICS_RECORDER_H_

#include <optional>

#include "base/memory/raw_ptr.h"
#include "base/time/time.h"

class Profile;

namespace ui {
class Event;
}

class WaapUIMetricsService;

// `WaapUIMetricsRecorder` is responsible for tracking state and timings
// related to user interactions within the WaaP UIs, which for now is limited to
// the TopChrome ReloadButton UI.
// Each ReloadButton instance should own an instance of this recorder.
class WaapUIMetricsRecorder {
 public:
  // Input type to activate the ReloadButton for use in this recorder.
  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  // LINT.IfChange(ReloadButtonInputType)
  enum class ReloadButtonInputType {
    kMouseRelease = 0,
    kKeyPress = 1,
    kMaxValue = kKeyPress
  };
  // LINT.ThenChange(//tools/metrics/histograms/metadata/ui/enums.xml:ReloadButtonInputType)

  // Mode of the ReloadButton for use in this recorder.
  enum class ReloadButtonMode { kReload = 0, kStop = 1, kMaxValue = kStop };

  explicit WaapUIMetricsRecorder(Profile* profile);

  WaapUIMetricsRecorder(const WaapUIMetricsRecorder&) = delete;
  WaapUIMetricsRecorder& operator=(const WaapUIMetricsRecorder&) = delete;

  ~WaapUIMetricsRecorder();

  // Called when the mouse enters the ReloadButton.
  void OnMouseEntered(base::TimeTicks time);
  // Called when the mouse exits the ReloadButton.
  void OnMouseExited(base::TimeTicks time);
  // Called when a mouse button is pressed on the ReloadButton.
  void OnMousePressed(base::TimeTicks time);
  // Called when a mouse button is released on the ReloadButton.
  void OnMouseReleased(base::TimeTicks time);

  // Called at the start of ReloadButton::ButtonPressed.
  void OnButtonPressedStart(const ui::Event& event,
                            ReloadButtonMode current_mode);
  // Called after the Stop command has been executed.
  void DidExecuteStopCommand(base::TimeTicks time);
  // Called after the Reload command has been executed.
  void DidExecuteReloadCommand(base::TimeTicks time);
  // Called when the visible mode of the ReloadButton possibly changes.
  void OnChangeVisibleMode(ReloadButtonMode current_mode,
                           ReloadButtonMode intended_mode,
                           base::TimeTicks time);
  // Called within ReloadButton::PaintButtonContents to record paint-related
  // metrics.
  void OnPaint(ReloadButtonMode visible_mode,
               int button_state,
               base::TimeTicks now);

 private:
  // Information about the last input event that triggered ButtonPressed.
  struct LastInputInfo {
    base::TimeTicks time;
    ReloadButtonInputType type;
    ReloadButtonMode mode_at_input;
  };

  // Information about a pending mode change visual update.
  struct PendingModeChange {
    base::TimeTicks start_time;
    ReloadButtonMode target_mode = ReloadButtonMode::kReload;
  };

  // This may be null if profile is null, e.g. in tests, or if the feature is
  // disabled. Its lifetime is managed by the `WaapUIMetricsServiceFactory` and
  // is guaranteed to outlive this object as long as none null.
  // Not owned.
  const raw_ptr<WaapUIMetricsService> waap_service_;

  // The timestamp of the last mouse enter event.
  base::TimeTicks mouse_entered_time_;
  // The timestamp of the last mouse press event.
  // Both left and right clicks are considered.
  base::TimeTicks mouse_pressed_time_;

  // State related to the last ButtonPressed input event.
  std::optional<const LastInputInfo> last_input_info_ = std::nullopt;
  // State related to a pending mode change.
  std::optional<const PendingModeChange> pending_mode_change_ = std::nullopt;
};

#endif  // CHROME_BROWSER_UI_WAAP_WAAP_UI_METRICS_RECORDER_H_