File: desktop_session_duration_tracker.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 (125 lines) | stat: -rw-r--r-- 4,423 bytes parent folder | download | duplicates (6)
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
// Copyright 2016 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_METRICS_DESKTOP_SESSION_DURATION_DESKTOP_SESSION_DURATION_TRACKER_H_
#define CHROME_BROWSER_METRICS_DESKTOP_SESSION_DURATION_DESKTOP_SESSION_DURATION_TRACKER_H_

#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/browser/metrics/desktop_session_duration/audible_contents_tracker.h"
#include "chrome/browser/metrics/desktop_session_duration/chrome_visibility_observer.h"

namespace metrics {

// Class for tracking and recording session length on desktop based on browser
// visibility, audio and user interaction.
class DesktopSessionDurationTracker : public AudibleContentsTracker::Observer {
 public:
  // The methods for the observer will be called on the UI thread.
  class Observer {
   public:
    virtual ~Observer() = default;
    virtual void OnSessionStarted(base::TimeTicks session_start) {}
    virtual void OnSessionEnded(base::TimeDelta session_length,
                                base::TimeTicks session_end) {}
  };

  // Creates the |DesktopSessionDurationTracker| instance and initializes the
  // observers that notify to it.
  static void Initialize();

  // Returns true if the |DesktopSessionDurationTracker| instance has been
  // created.
  static bool IsInitialized();

  // Returns the |DesktopSessionDurationTracker| instance.
  static DesktopSessionDurationTracker* Get();

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

  // Called when user interaction with the browser is caught.
  void OnUserEvent();

  // Called when visibility of the browser changes. These events can be delayed
  // due to timeout logic, the extent of which can be communicated via
  // |time_ago|. This time is used to correct the session duration.
  void OnVisibilityChanged(bool visible, base::TimeDelta time_ago);

  bool is_visible() const { return is_visible_; }
  bool in_session() const { return in_session_; }
  bool is_audio_playing() const { return is_audio_playing_; }

  void SetInactivityTimeoutForTesting(base::TimeDelta inactivity_timeout) {
    inactivity_timeout_ = inactivity_timeout;
  }

  // For observing the status of the session tracker.
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Ends the session and saves session information into histograms.
  void EndSessionForTesting();

  // Cleans up any global state for testing.
  static void CleanupForTesting();

 protected:
  DesktopSessionDurationTracker();
  ~DesktopSessionDurationTracker() override;

  // AudibleContentsTracker::Observer
  void OnAudioStart() override;
  void OnAudioEnd() override;

  // Decides whether session should be ended. Called when timer for inactivity
  // timeout was fired. Overridden by tests.
  virtual void OnTimerFired();

 private:
  // Starts timer based on |inactivity_timeout_|.
  void StartTimer(base::TimeDelta duration);

  // Marks the start of the session.
  void StartSession();

  // Ends the session and saves session information into histograms.
  // |time_to_discount| contains the amount of time that should be removed from
  // the apparent session length due to timeout logic.
  void EndSession(base::TimeDelta time_to_discount);

  // Sets |inactivity_timeout_| based on variation params.
  void InitInactivityTimeout();

  // Used for marking start if the session.
  base::TimeTicks session_start_;

  // Used for marking last user interaction.
  base::TimeTicks last_user_event_;

  // Used for marking current state of the user engagement.
  bool is_visible_ = false;
  bool in_session_ = false;
  bool is_audio_playing_ = false;
  bool is_first_session_ = true;

  // Timeout for waiting for user interaction.
  base::TimeDelta inactivity_timeout_;

  base::OneShotTimer timer_;

  base::ObserverList<Observer>::UncheckedAndDanglingUntriaged observer_list_;

  ChromeVisibilityObserver visibility_observer_;
  AudibleContentsTracker audio_tracker_;

  base::WeakPtrFactory<DesktopSessionDurationTracker> weak_factory_{this};
};

}  // namespace metrics

#endif  // CHROME_BROWSER_METRICS_DESKTOP_SESSION_DURATION_DESKTOP_SESSION_DURATION_TRACKER_H_