File: user_education_idle_policy.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 (78 lines) | stat: -rw-r--r-- 3,191 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
// Copyright 2024 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_IDLE_POLICY_H_
#define COMPONENTS_USER_EDUCATION_COMMON_SESSION_USER_EDUCATION_IDLE_POLICY_H_

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

namespace user_education {

class UserEducationSessionManager;
class UserEducationSessionProvider;
class UserEducationStorageService;

// Used to determine when the session is active or not based on periods of
// idle and active time. Currently implements the v2 behavior. OVerride
// virtual methods for testing.
class UserEducationIdlePolicy {
 public:
  // Construct an idle policy with values pulled from the v2 flag, or defaults
  // if the flag is not set.
  UserEducationIdlePolicy();
  UserEducationIdlePolicy(const UserEducationIdlePolicy&) = delete;
  void operator=(const UserEducationIdlePolicy&) = delete;
  virtual ~UserEducationIdlePolicy();

  // Called by UserEducationSessionManager to initialize required data members.
  void Init(const UserEducationSessionProvider* session_provider,
            const UserEducationStorageService* storage_service);

  // Determines if a new session should start based on the start of the last
  // session, the last time the application was active, and the new active
  // start time. Only call if `IsActive()` returns true; a session cannot
  // start when the application is inactive.
  virtual bool IsNewSession(base::Time previous_session_start_time,
                            base::Time previous_last_active_time,
                            base::Time most_recent_active_time) const;

 protected:
  // Constructs the idle policy with explicit values for each of the
  // thresholds.
  UserEducationIdlePolicy(base::TimeDelta new_session_idle_time,
                          base::TimeDelta minimum_valid_session_length);

  base::TimeDelta new_session_idle_time() const {
    return new_session_idle_time_;
  }
  base::TimeDelta minimum_valid_session_length() const {
    return minimum_valid_session_length_;
  }

 private:
  friend UserEducationSessionManager;
  friend class UserEducationIdlePolicyTest;

  // The minimum length of time since the last activity before the
  // application is considered idle. Must be nonzero since the sampling of
  // activity is necessarily coarse.
  const base::TimeDelta minimum_idle_time_;

  // The minimum amount of time the application must remain idle before new
  // activity is considered a new session. Must be nonzero.
  const base::TimeDelta new_session_idle_time_;

  // The minimum length of a session; if a previous session lasted for less
  // than this amount of time before the application became idle again then
  // the old session can be discarded and a new one started immediately.
  const base::TimeDelta minimum_valid_session_length_;

  raw_ptr<const UserEducationSessionProvider> session_provider_ = nullptr;
  raw_ptr<const UserEducationStorageService> storage_service_ = nullptr;
};

}  // namespace user_education

#endif  // COMPONENTS_USER_EDUCATION_COMMON_SESSION_USER_EDUCATION_IDLE_POLICY_H_