File: recent_session_policy.cc

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 (214 lines) | stat: -rw-r--r-- 8,821 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
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
// 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.

#include "chrome/browser/ui/user_education/recent_session_policy.h"

#include <optional>

#include "base/dcheck_is_on.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "chrome/browser/user_education/browser_user_education_storage_service.h"
#include "chrome/browser/user_education/user_education_service.h"

namespace {

constexpr int kMaxRecords = RecentSessionTracker::kMaxRecentSessionRecords;

// Gets midnight at the start of the next local day.
// Note: inaccurate if the time is *exactly* midnight, but this will happen so
// rarely that it's not worth worrying about.
base::Time GetEndOfDay(base::Time time) {
  return (time + base::Days(1)).LocalMidnight();
}

// Counts the number of active days in `recent_sessions` going back `num_days`
// from `last_day`. Returns null if session data recording does not go back far
// enough to cover the whole span.
std::optional<int> CountActiveDays(const RecentSessionData& recent_sessions,
                                   base::Time last_day,
                                   int num_days) {
  const base::Time end = GetEndOfDay(last_day);
  const base::Time start = end - base::Days(num_days);

  if (recent_sessions.enabled_time > start) {
    return std::nullopt;
  }

  std::vector<bool> active_days(num_days, false);
  for (const auto& start_time : recent_sessions.recent_session_start_times) {
    if (start_time >= start && start_time < end) {
      const size_t index = (start_time - start) / base::Days(1);
      active_days[index] = true;
    }
  }
  return std::count(active_days.begin(), active_days.end(), true);
}

std::optional<int> ValueOrNull(int value) {
  return value ? std::make_optional(value) : std::nullopt;
}

}  // namespace

bool RecentSessionPolicyImpl::Constraint::ShouldSkipRecording(
    const RecentSessionData& recent_sessions) const {
  return false;
}

bool RecentSessionPolicyImpl::DailyConstraint::ShouldSkipRecording(
    const RecentSessionData& recent_sessions) const {
  // Do not record if there are at least two recent sessions and the most recent
  // session is on the same calendar day as the second-most-recent session; the
  // session would have already been recorded on this day.
  //
  // It is critical that calendar day is used rather than just a 24-hour period,
  // since if the test were simply less than 24 hours, there could be a sequence
  // of, say, 16-hour separations between sessions and only the first one would
  // be recorded, no matter how long the sequence lasted.
  return recent_sessions.recent_session_start_times.size() > 1U &&
         GetEndOfDay(recent_sessions.recent_session_start_times[0]) ==
             GetEndOfDay(recent_sessions.recent_session_start_times[1]);
}

std::optional<int> RecentSessionPolicyImpl::SessionCountConstraint::GetCount(
    const RecentSessionData& recent_sessions) const {
  const base::Time start =
      recent_sessions.recent_session_start_times.front() - base::Days(days_);
  if (recent_sessions.enabled_time > start) {
    return std::nullopt;
  }
  int count = 0;
  for (const auto& start_time : recent_sessions.recent_session_start_times) {
    if (start_time >= start) {
      ++count;
    }
  }
  return count;
}

std::optional<int> RecentSessionPolicyImpl::ActiveDaysConstraint::GetCount(
    const RecentSessionData& recent_sessions) const {
  return CountActiveDays(recent_sessions,
                         recent_sessions.recent_session_start_times.front(),
                         days_);
}

std::optional<int> RecentSessionPolicyImpl::ActiveWeeksConstraint::GetCount(
    const RecentSessionData& recent_sessions) const {
  int count = 0;
  base::Time counting_back_from =
      recent_sessions.recent_session_start_times.front();
  for (int week = 0; week < weeks_; ++week) {
    const auto active_days =
        CountActiveDays(recent_sessions, counting_back_from, 7);
    if (!active_days) {
      return active_days;
    } else if (*active_days >= active_days_) {
      ++count;
    }
    counting_back_from -= base::Days(7);
  }
  return count;
}

RecentSessionPolicyImpl::ConstraintInfo::ConstraintInfo() = default;
RecentSessionPolicyImpl::ConstraintInfo::ConstraintInfo(
    std::unique_ptr<Constraint> constraint_,
    std::string histogram_name_,
    std::optional<int> histogram_max_,
    std::optional<int> low_usage_max_)
    : constraint(std::move(constraint_)),
      histogram_name(std::move(histogram_name_)),
      histogram_max(histogram_max_),
      low_usage_max(low_usage_max_) {}
RecentSessionPolicyImpl::ConstraintInfo::ConstraintInfo(
    ConstraintInfo&&) noexcept = default;
RecentSessionPolicyImpl::ConstraintInfo&
RecentSessionPolicyImpl::ConstraintInfo::operator=(ConstraintInfo&&) noexcept =
    default;
RecentSessionPolicyImpl::ConstraintInfo::~ConstraintInfo() = default;

RecentSessionPolicyImpl::RecentSessionPolicyImpl(ConstraintInfos constraints)
    : constraints_(std::move(constraints)) {
  CHECK(!constraints_.empty());
  for (const auto& constraint : constraints_) {
    CHECK(constraint.constraint);
  }
}

RecentSessionPolicyImpl::~RecentSessionPolicyImpl() = default;

void RecentSessionPolicyImpl::RecordRecentUsageMetrics(
    const RecentSessionData& recent_sessions) {
  for (const auto& constraint : constraints_) {
    if (!constraint.histogram_name.empty() &&
        !constraint.constraint->ShouldSkipRecording(recent_sessions)) {
      if (const auto result =
              constraint.constraint->GetCount(recent_sessions)) {
        base::UmaHistogramExactLinear(
            constraint.histogram_name.c_str(), *result,
            constraint.histogram_max.value_or(kMaxRecords));
      }
    }
  }
}

bool RecentSessionPolicyImpl::ShouldEnableLowUsagePromoMode(
    const RecentSessionData& recent_sessions) const {
  for (const auto& constraint : constraints_) {
    if (const auto limit = constraint.low_usage_max) {
      const auto result = constraint.constraint->GetCount(recent_sessions);
      if (!result || *result > *limit) {
        return false;
      }
    }
  }
  return true;
}

// static
RecentSessionPolicyImpl::ConstraintInfos
RecentSessionPolicyImpl::GetDefaultConstraints() {
  static constexpr int kShortTermDays = 7;
  static constexpr int kLongTermWeeks = 4;
  static constexpr int kLongTermDays = kLongTermWeeks * 7;
  const int max_active_weeks = base::GetFieldTrialParamByFeatureAsInt(
      kAllowRecentSessionTracking, "max_active_weeks", 0);
  const int max_active_days = base::GetFieldTrialParamByFeatureAsInt(
      kAllowRecentSessionTracking, "max_active_days", 0);
  const int super_active_days = base::GetFieldTrialParamByFeatureAsInt(
      kAllowRecentSessionTracking, "super_active_days", 4);
  const int max_monthly_active_days = base::GetFieldTrialParamByFeatureAsInt(
      kAllowRecentSessionTracking, "max_monthly_active_days", 2);
  const int max_super_active_weeks = base::GetFieldTrialParamByFeatureAsInt(
      kAllowRecentSessionTracking, "max_super_active_weeks", 0);
  const int max_weekly_sessions = base::GetFieldTrialParamByFeatureAsInt(
      kAllowRecentSessionTracking, "max_weekly_sessions", 0);
  const int max_monthly_sessions = base::GetFieldTrialParamByFeatureAsInt(
      kAllowRecentSessionTracking, "max_monthly_sessions", 0);
  ConstraintInfos result;
  result.emplace_back(std::make_unique<ActiveDaysConstraint>(kShortTermDays),
                      "UserEducation.Session.RecentActiveDays", kShortTermDays,
                      ValueOrNull(max_active_days));
  result.emplace_back(std::make_unique<ActiveDaysConstraint>(kLongTermDays),
                      "UserEducation.Session.MonthlyActiveDays", kLongTermDays,
                      ValueOrNull(max_monthly_active_days));
  result.emplace_back(
      std::make_unique<ActiveWeeksConstraint>(kLongTermWeeks, 1),
      "UserEducation.Session.RecentActiveWeeks", kLongTermWeeks,
      ValueOrNull(max_active_weeks));
  result.emplace_back(std::make_unique<ActiveWeeksConstraint>(
                          kLongTermWeeks, super_active_days),
                      "UserEducation.Session.RecentSuperActiveWeeks",
                      kLongTermWeeks, ValueOrNull(max_super_active_weeks));
  result.emplace_back(std::make_unique<SessionCountConstraint>(kShortTermDays),
                      "UserEducation.Session.ShortTermCount",
                      kShortTermDays + 1, ValueOrNull(max_weekly_sessions));
  result.emplace_back(std::make_unique<SessionCountConstraint>(kLongTermDays),
                      "UserEducation.Session.LongTermCount", kMaxRecords,
                      ValueOrNull(max_monthly_sessions));
  return result;
}