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
|
// 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 "components/user_education/common/new_badge/new_badge_policy.h"
#include <algorithm>
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "components/user_education/common/user_education_data.h"
#include "components/user_education/common/user_education_features.h"
#include "components/user_education/common/user_education_storage_service.h"
namespace user_education {
NewBadgePolicy::NewBadgePolicy()
: NewBadgePolicy(features::GetNewBadgeShowCount(),
features::GetNewBadgeFeatureUsedCount(),
features::GetNewBadgeDisplayWindow(),
features::GetNewProfileGracePeriod()) {}
NewBadgePolicy::NewBadgePolicy(int times_shown_before_dismiss,
int uses_before_dismiss,
base::TimeDelta display_window,
base::TimeDelta new_profile_grace_period)
: times_shown_before_dismiss_(times_shown_before_dismiss),
uses_before_dismiss_(uses_before_dismiss),
display_window_(display_window),
new_profile_grace_period_(new_profile_grace_period) {}
NewBadgePolicy::~NewBadgePolicy() = default;
bool NewBadgePolicy::ShouldShowNewBadge(
const NewBadgeData& data,
const UserEducationStorageService& storage_service) const {
// Check counts and window.
if (data.show_count >= std::max(1, times_shown_before_dismiss_) ||
data.used_count >= std::max(1, uses_before_dismiss_) ||
storage_service.GetCurrentTime() >
data.feature_enabled_time + display_window_) {
return false;
}
// If the feature was rolled out during the grace period, the user has
// basically always had it, so it's not new.
if (data.feature_enabled_time <
storage_service.profile_creation_time() + new_profile_grace_period_) {
return false;
}
return true;
}
void NewBadgePolicy::RecordNewBadgeShown(const base::Feature& feature,
int count) {
base::UmaHistogramBoolean(base::StrCat({"UserEducation.NewBadge.",
feature.name, ".MaxShownReached"}),
count >= times_shown_before_dismiss_);
}
void NewBadgePolicy::RecordFeatureUsed(const base::Feature& feature,
int count) {
// Only record histograms up to the max count. The value of the histogram
// becomes true when the count hits max and the badge becomes disabled.
if (count <= uses_before_dismiss_) {
base::UmaHistogramBoolean(base::StrCat({"UserEducation.NewBadge.",
feature.name, ".MaxUsedReached"}),
count == uses_before_dismiss_);
}
}
int NewBadgePolicy::GetFeatureUsedStorageCap() const {
// Always record more uses than the cap by a significant margin so that if
// parameters change, there is still enough data to interpret the rule.
return std::max(uses_before_dismiss_ * 2, uses_before_dismiss_ + 5);
}
} // namespace user_education
|