File: glic_metrics.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (349 lines) | stat: -rw-r--r-- 12,179 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// 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_GLIC_GLIC_METRICS_H_
#define CHROME_BROWSER_GLIC_GLIC_METRICS_H_

#include <memory>
#include <set>
#include <vector>

#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/glic/host/context/glic_tab_data.h"
#include "chrome/browser/glic/host/glic.mojom.h"
#include "components/prefs/pref_change_registrar.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "ui/display/display.h"

class Profile;

namespace glic {
class GlicEnabling;

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.

// LINT.IfChange(DisplayPosition)
enum class DisplayPosition {
  kTopLeft = 0,
  kCenterLeft = 1,
  kBottomLeft = 2,
  kTopCenter = 3,
  kCenterCenter = 4,
  kBottomCenter = 5,
  kTopRight = 6,
  kCenterRight = 7,
  kBottomRight = 8,
  kUnknown = 9,
  kMaxValue = kUnknown,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/glic/enums.xml:DisplayPosition)

// LINT.IfChange(Error)
enum class Error {
  kResponseStartWithoutInput = 0,
  kResponseStopWithoutInput = 1,
  kResponseStartWhileHidingOrHidden = 2,
  kWindowCloseWithoutWindowOpen = 3,
  kMaxValue = kWindowCloseWithoutWindowOpen,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/glic/enums.xml:GlicResponseError)

// LINT.IfChange(EntryPointStatus)
enum class EntryPointStatus {
  kBeforeFreNotEligible = 0,
  kBeforeFreAndEligible = 1,
  kIncompleteFreNotEligible = 2,
  kIncompleteFreAndEligible = 3,
  kAfterFreBrowserOnly = 4,
  kAfterFreOsOnly = 5,
  kAfterFreBrowserAndOs = 6,
  kAfterFreThreeDotOnly = 7,
  kAfterFreNotEligible = 8,
  kMaxValue = kAfterFreNotEligible,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/glic/enums.xml:GlicEntryPointStatus)

// LINT.IfChange(ResponseSegmentation)
enum class ResponseSegmentation {
  kUnknown = 0,
  kOsButtonAttachedText = 1,
  kOsButtonAttachedAudio = 2,
  kOsButtonDetachedText = 3,
  kOsButtonDetachedAudio = 4,
  kOsButtonMenuAttachedText = 5,
  kOsButtonMenuAttachedAudio = 6,
  kOsButtonMenuDetachedText = 7,
  kOsButtonMenuDetachedAudio = 8,
  kOsHotkeyAttachedText = 9,
  kOsHotkeyAttachedAudio = 10,
  kOsHotkeyDetachedText = 11,
  kOsHotkeyDetachedAudio = 12,
  kButtonTopChromeAttachedText = 13,
  kButtonTopChromeAttachedAudio = 14,
  kButtonTopChromeDetachedText = 15,
  kButtonTopChromeDetachedAudio = 16,
  kFreAttachedText = 17,
  kFreAttachedAudio = 18,
  kFreDetachedText = 19,
  kFreDetachedAudio = 20,
  kProfilePickerAttachedText = 21,
  kProfilePickerAttachedAudio = 22,
  kProfilePickerDetachedText = 23,
  kProfilePickerDetachedAudio = 24,
  kNudgeAttachedText = 25,
  kNudgeAttachedAudio = 26,
  kNudgeDetachedText = 27,
  kNudgeDetachedAudio = 28,
  kThreeDotsMenuAttachedText = 29,
  kThreeDotsMenuAttachedAudio = 30,
  kThreeDotsMenuDetachedText = 31,
  kThreeDotsMenuDetachedAudio = 32,
  kUnsupportedAttachedText = 33,
  kUnsupportedAttachedAudio = 34,
  kUnsupportedDetachedText = 35,
  kUnsupportedDetachedAudio = 36,
  kWhatsNewAttachedText = 37,
  kWhatsNewAttachedAudio = 38,
  kWhatsNewDetachedText = 39,
  kWhatsNewDetachedAudio = 40,
  kAfterSignInAttachedText = 41,
  kAfterSignInAttachedAudio = 42,
  kAfterSignInDetachedText = 43,
  kAfterSignInDetachedAudio = 44,
  kMaxValue = kAfterSignInDetachedAudio,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/glic/enums.xml:GlicResponseSegmentation)

// LINT.IfChange(GlicInputModesUsed)
enum class InputModesUsed {
  kNone = 0,
  kOnlyText = 1,
  kOnlyAudio = 2,
  kTextAndAudio = 3,

  kMaxValue = kTextAndAudio,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/glic/enums.xml:GlicInputModesUsed)

// LINT.IfChange(AttachChangeReason)
enum class AttachChangeReason {
  // Attach state changed because of a drag gesture.
  kDrag = 0,
  // Attach state changed because of a menu click.
  kMenu = 1,
  // Attachment state initialized.
  kInit = 2,

  kMaxValue = kInit,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/glic/enums.xml:GlicAttachChangeReason)

// Events related to requests to the Glic API from the web client.
// LINT.IfChange(GlicRequestEvent)
enum class GlicRequestEvent {
  kRequestReceived = 0,
  kRequestSent = 1,
  kRequestHandlerException = 2,
  kRequestReceivedWhileHidden = 3,
  kMaxValue = kRequestReceivedWhileHidden,
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/glic/enums.xml:GlicRequestEvent)

// The different states of active tab sharing.
// LINT.IfChange(ActiveTabSharingState)
enum class ActiveTabSharingState {
  kActiveTabIsShared = 0,
  kCannotShareActiveTab = 1,
  kNoTabCanBeShared = 2,
  kTabContextPermissionNotGranted = 3,
  kMaxValue = kTabContextPermissionNotGranted
};
// LINT.ThenChange(//tools/metrics/histograms/metadata/glic/enums.xml:ActiveTabSharingState)

class GlicEnabling;
class GlicSharingManager;
class GlicWindowController;

namespace internal {
class BrowserActivityObserver;
}

// Responsible for all glic web-client metrics, and all stateful glic metrics.
// Some stateless glic metrics are logged inline in the relevant code for
// convenience.
class GlicMetrics {
 public:
  class Delegate {
   public:
    virtual ~Delegate() {}
    virtual gfx::Size GetWindowSize() const = 0;
    virtual bool IsWindowShowing() const = 0;
    virtual bool IsWindowAttached() const = 0;
    virtual content::WebContents* GetContents() = 0;
    virtual ActiveTabSharingState GetActiveTabSharingState() = 0;
  };

  GlicMetrics(Profile* profile, GlicEnabling* enabling);
  GlicMetrics(const GlicMetrics&) = delete;
  GlicMetrics& operator=(const GlicMetrics&) = delete;
  ~GlicMetrics();

  // See glic.mojom for details. These are events from the web client. The
  // lifetime of the web client is scoped to that of the window, so if these
  // methods are called then controller_ is guaranteed to exist.
  void OnUserInputSubmitted(mojom::WebClientMode mode);
  void OnResponseStarted();
  void OnResponseStopped();
  void OnSessionTerminated();
  void OnResponseRated(bool positive);
  void OnAttachedToBrowser(AttachChangeReason reason);
  void OnDetachedFromBrowser(AttachChangeReason reason);

  // ----Public API called by other glic classes-----
  // Called when the user clicks Accept in the FRE.
  void OnFreAccepted();
  // Called when the glic window starts to open.
  void OnGlicWindowOpen(bool attached, mojom::InvocationSource source);
  // Called just after the the glic window has been loaded into the UI.
  void OnGlicWindowShown(std::optional<display::Display> display,
                         const gfx::Point& glic_center_point);
  // Called when the glic window has been opened and is ready.
  void OnGlicWindowOpenAndReady();
  // Called when the glic window is resized.
  void OnGlicWindowResize();
  // Called when the glic window starts being resized by the user.
  void OnWidgetUserResizeStarted();
  // Called when the glic window stops being resized by the user.
  void OnWidgetUserResizeEnded();
  // Called when the glic window finishes closing.
  void OnGlicWindowClose(std::optional<display::Display> display,
                         const gfx::Point& glic_center_point);
  // Called when glic requests a scroll.
  void OnGlicScrollAttempt();
  // Called when scrolling starts (after glic requests to scroll) or if
  // the operation fails. `success` is true if a scroll was successfully
  // triggered.
  void OnGlicScrollComplete(bool success);

  // Called when a response is received with closed captions showing.
  void LogClosedCaptionsShown();

  // Must be called immediately after constructor before any calls from
  // glic.mojom.
  void SetControllers(GlicWindowController* window_controller,
                      GlicSharingManager* sharing_manager);
  void SetDelegateForTesting(std::unique_ptr<Delegate> delegate);

  // Must be called when context is requested.
  void DidRequestContextFromFocusedTab();

  void set_show_start_time(base::TimeTicks time) { show_start_time_ = time; }

  void set_starting_mode(mojom::WebClientMode mode) { starting_mode_ = mode; }

 private:
  // Called when `impression_timer_` fires.
  void OnImpressionTimerFired();

  // Called when `glic_window_size_timer_` fires.
  void OnGlicWindowSizeTimerFired();

  // Stores the source id at the time that context is requested.
  void StoreSourceId();

  // Called when kGlicCompletedFre or GlicEnabling::IsAllowed() changes.
  void OnMaybeEnabledAndConsentForProfileChanged();

  // Called when kGlicPinnedToTabstrip changes.
  void OnPinningPrefChanged();

  // Called when kGlicTabContextEnabled changes.
  void OnTabContextEnabledPrefChanged();

  // Resets the window timing state variables.
  void ResetGlicWindowPresentationTimingState();

  // Returns the area in the display a given center point is.
  DisplayPosition GetDisplayPositionOfPoint(
      std::optional<display::Display> display,
      const gfx::Point& glic_center_point);

  base::TimeTicks fre_accepted_time_;

  // These members are cleared in OnResponseStopped.
  base::TimeTicks input_submitted_time_;
  mojom::WebClientMode input_mode_;
  bool did_request_context_ = false;
  std::set<mojom::WebClientMode> inputs_modes_used_;
  int attach_change_count_ = 0;

  // Session state. `session_start_time_` is a sentinel that is cleared in
  // OnGlicWindowClose() and is used to determine whether OnGlicWindowOpen was
  // called.
  int session_responses_ = 0;
  base::TimeTicks session_start_time_;
  mojom::InvocationSource invocation_source_ =
      mojom::InvocationSource::kOsButton;

  // Used to record impressions of glic entry points.
  base::RepeatingTimer impression_timer_;

  // Used to record metrics about the glic window size.
  base::RepeatingTimer glic_window_size_timer_;

  // A context-free source id used when no web contents is targeted.
  ukm::SourceId no_url_source_id_ = ukm::NoURLSourceId();
  // The source id at the time context is requested. If context was not
  // requested then this is `no_url_source_id_`.
  ukm::SourceId source_id_ = ukm::NoURLSourceId();

  // The owner of this class is responsible for maintaining appropriate lifetime
  // for controller_.
  std::unique_ptr<Delegate> delegate_;
  raw_ptr<Profile> profile_;
  raw_ptr<GlicEnabling> enabling_;

  // Whether Glic is enabled and FRE has been completed. Tracked to trigger
  // metric(s) on change.
  bool is_enabled_ = false;

  // Set to true in OnResponseStarted() and set to false in OnResponseStopped().
  // This is a workaround and should be removed, see crbug.com/399151164.
  bool response_started_ = false;

  std::vector<base::CallbackListSubscription> subscriptions_;

  // Cache the last value of the kGlicPinnedToTabstrip pref so that we only emit
  // metrics for changes to the last value.
  bool is_pinned_ = false;
  PrefChangeRegistrar pref_registrar_;

  // The following two variables are used together for recording metrics and are
  // reset together after the metric is recorded.
  // The timestamp when the glic window starts to be shown.
  base::TimeTicks show_start_time_;
  // Web client's operation modes.
  mojom::WebClientMode starting_mode_ = mojom::WebClientMode::kUnknown;

  // The following variables are used for recording scroll related metrics.
  // The number of scroll attempts  (tracked per session and reset when the
  // session ends).
  int scroll_attempt_count_ = 0;
  // These two variables mirror `input_submitted_time_` and
  // `input_mode_`, but are only set when `OnGlicScrollAttempt()` is
  // called. They are reset in `OnGlicScrollComplete()`. They are separately
  // tracked because `OnGlicScrollComplete()` could potentially be called after
  // `OnResponseStopped()`, which resets `input_submitted_time_` and
  // `input_mode_`.
  base::TimeTicks scroll_input_submitted_time_;
  mojom::WebClientMode scroll_input_mode_;
  std::unique_ptr<internal::BrowserActivityObserver> browser_activity_observer_;
};

}  // namespace glic
#endif  // CHROME_BROWSER_GLIC_GLIC_METRICS_H_