File: user_education_session_manager.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 (112 lines) | stat: -rw-r--r-- 4,498 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
// Copyright 2023 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_USER_EDUCATION_COMMON_SESSION_USER_EDUCATION_SESSION_MANAGER_H_
#define COMPONENTS_USER_EDUCATION_COMMON_SESSION_USER_EDUCATION_SESSION_MANAGER_H_

#include <memory>

#include "base/callback_list.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "components/user_education/common/session/user_education_idle_observer.h"

namespace user_education {

class UserEducationIdlePolicy;
class UserEducationStorageService;

// Provides session state to clients of the session manager.
class UserEducationSessionProvider {
 public:
  UserEducationSessionProvider() = default;
  virtual ~UserEducationSessionProvider() = default;

  // Registers a callback that will be called whenever a new session happens.
  // To avoid a race condition at startup, you should also check
  // `new_session_since_startup()`.
  virtual base::CallbackListSubscription AddNewSessionCallback(
      base::RepeatingClosure new_session_callback) = 0;

  // Returns whether there has been a new session since application startup.
  virtual bool GetNewSessionSinceStartup() const = 0;
};

// Governs sessions for user education. May use cues such as application open
// and close times as well as active and inactive periods to determine when a
// session should start or end.
class UserEducationSessionManager : public UserEducationSessionProvider {
 public:
  UserEducationSessionManager();
  UserEducationSessionManager(const UserEducationSessionManager&) = delete;
  void operator=(const UserEducationSessionManager&) = delete;
  ~UserEducationSessionManager() override;

  // Initialize the session manager. If overridden, the base class must be
  // called. In derived classes, internal data should be set up or restored
  // either in the constructor or before calling this version of Init.
  virtual void Init(UserEducationStorageService* storage_service,
                    std::unique_ptr<UserEducationIdleObserver> observer,
                    std::unique_ptr<UserEducationIdlePolicy> policy);

  // Possibly updates the current session state, if there might be a new
  // session.
  void MaybeUpdateSessionState();

  // UserEducationSessionProvider:
  base::CallbackListSubscription AddNewSessionCallback(
      base::RepeatingClosure new_session_callback) override;
  bool GetNewSessionSinceStartup() const override;

  // Test-only methods.
  UserEducationStorageService* storage_service_for_testing() {
    return storage_service_;
  }
  template <typename T>
    requires std::derived_from<T, UserEducationIdleObserver>
  T* ReplaceIdleObserverForTesting(std::unique_ptr<T> new_observer) {
    T* const observer_ptr = new_observer.get();
    SetIdleObserver(std::move(new_observer));
    return observer_ptr;
  }

 protected:
  const UserEducationIdlePolicy* idle_policy() const {
    return idle_policy_.get();
  }

  // Called when a new session is started. By default, does nothing. Override to
  // (for example) log metrics, or notify observers.
  virtual void OnNewSession(const base::Time old_start_time,
                            const base::Time old_active_time,
                            const base::Time new_active_time);

  // Called whenever the most recent active time is updated, before the session
  // data is updated in the storage service, so that both the current update and
  // the previous state can be used. By default, does nothing. Override to (for
  // example) log metrics.
  virtual void OnLastActiveTimeUpdating(base::Time new_last_active_time);

 private:
  void SetIdleObserver(std::unique_ptr<UserEducationIdleObserver> new_observer);

  void RecordActivePeriodDuration(base::TimeDelta duration);
  void RecordIdlePeriodDuration(base::TimeDelta duration);

  friend class UserEducationSessionManagerTest;

  void UpdateLastActiveTime(base::Time new_last_active_time);

  raw_ptr<UserEducationStorageService> storage_service_ = nullptr;
  std::unique_ptr<UserEducationIdleObserver> idle_observer_;
  base::CallbackListSubscription idle_observer_subscription_;
  std::unique_ptr<UserEducationIdlePolicy> idle_policy_;
  base::RepeatingCallbackList<void()> new_session_callbacks_;
  bool new_session_since_startup_ = false;
};

}  // namespace user_education

#endif  // COMPONENTS_USER_EDUCATION_COMMON_SESSION_USER_EDUCATION_SESSION_MANAGER_H_