File: guest_os_engagement_metrics.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (147 lines) | stat: -rw-r--r-- 5,656 bytes parent folder | download
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
// 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.

#ifndef COMPONENTS_GUEST_OS_GUEST_OS_ENGAGEMENT_METRICS_H_
#define COMPONENTS_GUEST_OS_GUEST_OS_ENGAGEMENT_METRICS_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "components/session_manager/core/session_manager_observer.h"
#include "ui/wm/public/activation_change_observer.h"

class PrefService;

namespace aura {
class Window;
}  // namespace aura

namespace base {
class Clock;
class TickClock;
}  // namespace base

namespace guest_os {

// A class for recording engagement metrics. Calculates and reports daily
// totals for the following metrics:
// - Foo.EngagementTime.Total: Engaged session time, i.e. excluding when the
// screen is locked or dim due to user idle. To allow comparisons with the
// other metrics, this class should only be instantiated when the relevant
// Guest OS is supported.
// - Foo.EngagementTime.Foreground: Time when the user is engaged and focused
// on a matching window.
// - Foo.EngagementTime.Background: Time when the user is engaged and not
// focused on a matching window, but the Guest OS is otherwise active in the
// background.
// - Foo.Engagement.FooTotal: Total of Foreground and Background.
class GuestOsEngagementMetrics : public wm::ActivationChangeObserver,
                                 public session_manager::SessionManagerObserver,
                                 public chromeos::PowerManagerClient::Observer {
 public:
  using WindowMatcher = base::RepeatingCallback<bool(const aura::Window*)>;

  GuestOsEngagementMetrics(PrefService* pref_service,
                           WindowMatcher window_matcher,
                           const std::string& pref_prefix,
                           const std::string& uma_name);

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

  ~GuestOsEngagementMetrics() override;

  // Instead of using |window_matcher_|, we let consumers define when the Guest
  // OS is active in the background. This function should be called whenever
  // changes.
  void SetBackgroundActive(bool background_active);

  // wm::ActivationChangeObserver:
  void OnWindowActivated(wm::ActivationChangeObserver::ActivationReason reason,
                         aura::Window* gained_active,
                         aura::Window* lost_active) override;

  // session_manager::SessionManagerObserver:
  void OnSessionStateChanged() override;

  // chromeos::PowerManagerClient::Observer:
  void ScreenIdleStateChanged(
      const power_manager::ScreenIdleState& proto) override;

  static std::unique_ptr<GuestOsEngagementMetrics>
  GetEngagementMetricsForTesting(PrefService* pref_service,
                                 WindowMatcher window_matcher,
                                 const std::string& pref_prefix,
                                 const std::string& uma_name,
                                 const base::Clock* clock,
                                 const base::TickClock* tick_clock);

 private:
  // Private, for testing use only
  GuestOsEngagementMetrics(PrefService* pref_service,
                           WindowMatcher window_matcher,
                           const std::string& pref_prefix,
                           const std::string& uma_name,
                           const base::Clock* clock,
                           const base::TickClock* tick_clock);

  // Restores accumulated engagement time in previous sessions from profile
  // preferences.
  void RestoreEngagementTimeFromPrefs();

  // Called periodically to save accumulated results to profile preferences.
  void SaveEngagementTimeToPrefs();

  // Called whenever engagement state is changed. Time spent in last state is
  // accumulated to corresponding metrics.
  void UpdateEngagementTime();

  // Records accumulated engagement time metrics to UMA if necessary (i.e. day
  // has changed).
  void RecordEngagementTimeToUmaIfNeeded();

  // Resets accumulated engagement times to zero, and updates both OS version
  // and day ID.
  void ResetEngagementTimePrefs();

  bool ShouldAccumulateEngagementTotalTime() const;
  bool ShouldAccumulateEngagementForegroundTime() const;
  bool ShouldAccumulateEngagementBackgroundTime() const;

  bool ShouldRecordEngagementTimeToUma() const;

  const raw_ptr<PrefService, ExperimentalAsh> pref_service_;

  WindowMatcher window_matcher_;
  std::string pref_prefix_;
  std::string uma_name_;

  // |clock_| is used for determining when to log to UMA, while |tick_clock_|
  // is used to calculate elapsed time.
  raw_ptr<const base::Clock, ExperimentalAsh> clock_;
  raw_ptr<const base::TickClock, ExperimentalAsh> tick_clock_;
  base::RepeatingTimer update_engagement_time_timer_;
  base::RepeatingTimer save_engagement_time_to_prefs_timer_;
  base::TimeTicks last_update_ticks_;

  // States for determining which engagement metrics should we accumulate to.
  bool session_active_ = false;
  bool screen_dimmed_ = false;
  bool background_active_ = false;
  bool matched_window_active_ = false;

  // Accumulated results and associated state which are saved to profile
  // preferences at fixed interval.
  int day_id_ = 0;
  base::TimeDelta engagement_time_total_;
  base::TimeDelta engagement_time_foreground_;
  base::TimeDelta engagement_time_background_;
};

}  // namespace guest_os

#endif  // COMPONENTS_GUEST_OS_GUEST_OS_ENGAGEMENT_METRICS_H_