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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
// 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.
#include "components/site_engagement/content/site_engagement_score.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <utility>
#include "base/metrics/field_trial_params.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/content_settings_utils.h"
#include "components/site_engagement/content/engagement_type.h"
#include "components/site_engagement/content/site_engagement_metrics.h"
#include "third_party/blink/public/mojom/site_engagement/site_engagement.mojom.h"
namespace site_engagement {
namespace {
// Delta within which to consider scores equal.
constexpr double kScoreDelta = 0.001;
// Delta within which to consider internal time values equal. Internal time
// values are in microseconds, so this delta comes out at one second.
constexpr double kTimeDelta = 1000000;
// Number of days after the last launch of an origin from an installed shortcut
// for which WEB_APP_INSTALLED_POINTS will be added to the engagement score.
constexpr int kMaxDaysSinceShortcutLaunch = 10;
bool DoublesConsideredDifferent(double value1, double value2, double delta) {
double abs_difference = fabs(value1 - value2);
return abs_difference > delta;
}
base::Value::Dict GetSiteEngagementScoreDictForSettings(
const HostContentSettingsMap* settings,
const GURL& origin_url) {
if (!settings)
return base::Value::Dict();
base::Value value = settings->GetWebsiteSetting(
origin_url, origin_url, ContentSettingsType::SITE_ENGAGEMENT, nullptr);
if (!value.is_dict())
return base::Value::Dict();
return std::move(value).TakeDict();
}
} // namespace
const double SiteEngagementScore::kMaxPoints = 100;
const char SiteEngagementScore::kRawScoreKey[] = "rawScore";
const char SiteEngagementScore::kPointsAddedTodayKey[] = "pointsAddedToday";
const char SiteEngagementScore::kLastEngagementTimeKey[] = "lastEngagementTime";
const char SiteEngagementScore::kLastShortcutLaunchTimeKey[] =
"lastShortcutLaunchTime";
// static
SiteEngagementScore::ParamValues& SiteEngagementScore::GetParamValues() {
static base::NoDestructor<ParamValues> param_values([]() {
SiteEngagementScore::ParamValues param_values;
param_values[MAX_POINTS_PER_DAY] = {"max_points_per_day", 15};
param_values[DECAY_PERIOD_IN_HOURS] = {"decay_period_in_hours", 2};
param_values[DECAY_POINTS] = {"decay_points", 0};
param_values[DECAY_PROPORTION] = {"decay_proportion", 0.984};
param_values[SCORE_CLEANUP_THRESHOLD] = {"score_cleanup_threshold", 0.5};
param_values[NAVIGATION_POINTS] = {"navigation_points", 1.5};
param_values[USER_INPUT_POINTS] = {"user_input_points", 0.6};
param_values[VISIBLE_MEDIA_POINTS] = {"visible_media_playing_points", 0.06};
param_values[HIDDEN_MEDIA_POINTS] = {"hidden_media_playing_points", 0.01};
param_values[WEB_APP_INSTALLED_POINTS] = {"web_app_installed_points", 5};
param_values[FIRST_DAILY_ENGAGEMENT] = {"first_daily_engagement_points",
1.5};
param_values[BOOTSTRAP_POINTS] = {"bootstrap_points", 24};
param_values[MEDIUM_ENGAGEMENT_BOUNDARY] = {"medium_engagement_boundary",
15};
param_values[HIGH_ENGAGEMENT_BOUNDARY] = {"high_engagement_boundary", 50};
param_values[MAX_DECAYS_PER_SCORE] = {"max_decays_per_score", 4};
param_values[LAST_ENGAGEMENT_GRACE_PERIOD_IN_HOURS] = {
"last_engagement_grace_period_in_hours", 1};
param_values[NOTIFICATION_INTERACTION_POINTS] = {
"notification_interaction_points", 1};
return param_values;
}());
return *param_values;
}
double SiteEngagementScore::GetMaxPointsPerDay() {
return GetParamValues()[MAX_POINTS_PER_DAY].second;
}
double SiteEngagementScore::GetDecayPeriodInHours() {
return GetParamValues()[DECAY_PERIOD_IN_HOURS].second;
}
double SiteEngagementScore::GetDecayPoints() {
return GetParamValues()[DECAY_POINTS].second;
}
double SiteEngagementScore::GetDecayProportion() {
return GetParamValues()[DECAY_PROPORTION].second;
}
double SiteEngagementScore::GetScoreCleanupThreshold() {
return GetParamValues()[SCORE_CLEANUP_THRESHOLD].second;
}
double SiteEngagementScore::GetNavigationPoints() {
return GetParamValues()[NAVIGATION_POINTS].second;
}
double SiteEngagementScore::GetUserInputPoints() {
return GetParamValues()[USER_INPUT_POINTS].second;
}
double SiteEngagementScore::GetVisibleMediaPoints() {
return GetParamValues()[VISIBLE_MEDIA_POINTS].second;
}
double SiteEngagementScore::GetHiddenMediaPoints() {
return GetParamValues()[HIDDEN_MEDIA_POINTS].second;
}
double SiteEngagementScore::GetWebAppInstalledPoints() {
return GetParamValues()[WEB_APP_INSTALLED_POINTS].second;
}
double SiteEngagementScore::GetFirstDailyEngagementPoints() {
return GetParamValues()[FIRST_DAILY_ENGAGEMENT].second;
}
double SiteEngagementScore::GetBootstrapPoints() {
return GetParamValues()[BOOTSTRAP_POINTS].second;
}
double SiteEngagementScore::GetMediumEngagementBoundary() {
return GetParamValues()[MEDIUM_ENGAGEMENT_BOUNDARY].second;
}
double SiteEngagementScore::GetHighEngagementBoundary() {
return GetParamValues()[HIGH_ENGAGEMENT_BOUNDARY].second;
}
double SiteEngagementScore::GetMaxDecaysPerScore() {
return GetParamValues()[MAX_DECAYS_PER_SCORE].second;
}
double SiteEngagementScore::GetLastEngagementGracePeriodInHours() {
return GetParamValues()[LAST_ENGAGEMENT_GRACE_PERIOD_IN_HOURS].second;
}
double SiteEngagementScore::GetNotificationInteractionPoints() {
return GetParamValues()[NOTIFICATION_INTERACTION_POINTS].second;
}
void SiteEngagementScore::SetParamValuesForTesting() {
GetParamValues()[MAX_POINTS_PER_DAY].second = 5;
GetParamValues()[DECAY_PERIOD_IN_HOURS].second = 7 * 24;
GetParamValues()[DECAY_POINTS].second = 5;
GetParamValues()[NAVIGATION_POINTS].second = 0.5;
GetParamValues()[USER_INPUT_POINTS].second = 0.05;
GetParamValues()[VISIBLE_MEDIA_POINTS].second = 0.02;
GetParamValues()[HIDDEN_MEDIA_POINTS].second = 0.01;
GetParamValues()[WEB_APP_INSTALLED_POINTS].second = 5;
GetParamValues()[BOOTSTRAP_POINTS].second = 8;
GetParamValues()[MEDIUM_ENGAGEMENT_BOUNDARY].second = 5;
GetParamValues()[HIGH_ENGAGEMENT_BOUNDARY].second = 50;
GetParamValues()[MAX_DECAYS_PER_SCORE].second = 1;
GetParamValues()[LAST_ENGAGEMENT_GRACE_PERIOD_IN_HOURS].second = 72;
GetParamValues()[NOTIFICATION_INTERACTION_POINTS].second = 1;
// This is set to values that avoid interference with tests and are set when
// testing these features.
GetParamValues()[FIRST_DAILY_ENGAGEMENT].second = 0;
GetParamValues()[DECAY_PROPORTION].second = 1;
GetParamValues()[SCORE_CLEANUP_THRESHOLD].second = 0;
}
// static
void SiteEngagementScore::UpdateFromVariations(const char* param_name) {
std::array<double, MAX_VARIATION> param_vals;
for (int i = 0; i < MAX_VARIATION; ++i) {
std::string param_string =
base::GetFieldTrialParamValue(param_name, GetParamValues()[i].first);
// Bail out if we didn't get a param string for the key, or if we couldn't
// convert the param string to a double, or if we get a negative value.
if (param_string.empty() ||
!base::StringToDouble(param_string, ¶m_vals[i]) ||
param_vals[i] < 0) {
return;
}
}
// Once we're sure everything is valid, assign the variation to the param
// values array.
for (int i = 0; i < MAX_VARIATION; ++i)
SiteEngagementScore::GetParamValues()[i].second = param_vals[i];
}
SiteEngagementScore::SiteEngagementScore(base::Clock* clock,
const GURL& origin,
HostContentSettingsMap* settings)
: SiteEngagementScore(
clock,
origin,
GetSiteEngagementScoreDictForSettings(settings, origin)) {
settings_map_ = settings;
}
SiteEngagementScore::SiteEngagementScore(SiteEngagementScore&& other) = default;
SiteEngagementScore::~SiteEngagementScore() = default;
SiteEngagementScore& SiteEngagementScore::operator=(
SiteEngagementScore&& other) = default;
void SiteEngagementScore::AddPoints(double points) {
DCHECK_NE(0, points);
// As the score is about to be updated, commit any decay that has happened
// since the last update.
raw_score_ = DecayedScore();
base::Time now = clock_->Now();
if (!last_engagement_time_.is_null() &&
now.LocalMidnight() != last_engagement_time_.LocalMidnight()) {
points_added_today_ = 0;
}
if (points_added_today_ == 0) {
// Award bonus engagement for the first engagement of the day for a site.
points += GetFirstDailyEngagementPoints();
SiteEngagementMetrics::RecordEngagement(
EngagementType::kFirstDailyEngagement);
}
double to_add = std::min(kMaxPoints - raw_score_,
GetMaxPointsPerDay() - points_added_today_);
to_add = std::min(to_add, points);
points_added_today_ += to_add;
raw_score_ += to_add;
last_engagement_time_ = now;
}
double SiteEngagementScore::GetTotalScore() const {
return std::min(DecayedScore() + BonusIfShortcutLaunched(), kMaxPoints);
}
mojom::SiteEngagementDetails SiteEngagementScore::GetDetails() const {
mojom::SiteEngagementDetails engagement;
engagement.origin = origin_;
engagement.base_score = DecayedScore();
engagement.installed_bonus = BonusIfShortcutLaunched();
engagement.total_score = GetTotalScore();
return engagement;
}
void SiteEngagementScore::Commit() {
DCHECK(settings_map_);
DCHECK(score_dict_);
if (!UpdateScoreDict(*score_dict_))
return;
settings_map_->SetWebsiteSettingDefaultScope(
origin_, GURL(), ContentSettingsType::SITE_ENGAGEMENT,
base::Value(std::move(*score_dict_)));
}
blink::mojom::EngagementLevel SiteEngagementScore::GetEngagementLevel() const {
DCHECK_LT(GetMediumEngagementBoundary(), GetHighEngagementBoundary());
double score = GetTotalScore();
if (score == 0)
return blink::mojom::EngagementLevel::NONE;
if (score < 1)
return blink::mojom::EngagementLevel::MINIMAL;
if (score < GetMediumEngagementBoundary())
return blink::mojom::EngagementLevel::LOW;
if (score < GetHighEngagementBoundary())
return blink::mojom::EngagementLevel::MEDIUM;
if (score < SiteEngagementScore::kMaxPoints)
return blink::mojom::EngagementLevel::HIGH;
return blink::mojom::EngagementLevel::MAX;
}
bool SiteEngagementScore::MaxPointsPerDayAdded() const {
if (!last_engagement_time_.is_null() &&
clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) {
return false;
}
return points_added_today_ == GetMaxPointsPerDay();
}
void SiteEngagementScore::Reset(double points,
const base::Time last_engagement_time) {
raw_score_ = points;
points_added_today_ = 0;
// This must be set in order to prevent the score from decaying when read.
last_engagement_time_ = last_engagement_time;
}
void SiteEngagementScore::SetLastEngagementTime(const base::Time& time) {
if (!last_engagement_time_.is_null() &&
time.LocalMidnight() != last_engagement_time_.LocalMidnight()) {
points_added_today_ = 0;
}
last_engagement_time_ = time;
}
bool SiteEngagementScore::UpdateScoreDict(base::Value::Dict& score_dict) {
double raw_score_orig = score_dict.FindDouble(kRawScoreKey).value_or(0);
double points_added_today_orig =
score_dict.FindDouble(kPointsAddedTodayKey).value_or(0);
double last_engagement_time_internal_orig =
score_dict.FindDouble(kLastEngagementTimeKey).value_or(0);
double last_shortcut_launch_time_internal_orig =
score_dict.FindDouble(kLastShortcutLaunchTimeKey).value_or(0);
bool changed =
DoublesConsideredDifferent(raw_score_orig, raw_score_, kScoreDelta) ||
DoublesConsideredDifferent(points_added_today_orig, points_added_today_,
kScoreDelta) ||
DoublesConsideredDifferent(last_engagement_time_internal_orig,
last_engagement_time_.ToInternalValue(),
kTimeDelta) ||
DoublesConsideredDifferent(last_shortcut_launch_time_internal_orig,
last_shortcut_launch_time_.ToInternalValue(),
kTimeDelta);
if (!changed)
return false;
score_dict.Set(kRawScoreKey, raw_score_);
score_dict.Set(kPointsAddedTodayKey, points_added_today_);
score_dict.Set(kLastEngagementTimeKey,
static_cast<double>(last_engagement_time_.ToInternalValue()));
score_dict.Set(
kLastShortcutLaunchTimeKey,
static_cast<double>(last_shortcut_launch_time_.ToInternalValue()));
return true;
}
SiteEngagementScore::SiteEngagementScore(
base::Clock* clock,
const GURL& origin,
std::optional<base::Value::Dict> score_dict)
: clock_(clock),
raw_score_(0),
points_added_today_(0),
last_engagement_time_(),
last_shortcut_launch_time_(),
score_dict_(std::move(score_dict)),
origin_(origin),
settings_map_(nullptr) {
if (!score_dict_)
return;
raw_score_ = score_dict_->FindDouble(kRawScoreKey).value_or(0);
points_added_today_ =
score_dict_->FindDouble(kPointsAddedTodayKey).value_or(0);
std::optional<double> maybe_last_engagement_time =
score_dict_->FindDouble(kLastEngagementTimeKey);
if (maybe_last_engagement_time.has_value())
last_engagement_time_ =
base::Time::FromInternalValue(maybe_last_engagement_time.value());
std::optional<double> maybe_last_shortcut_launch_time =
score_dict_->FindDouble(kLastShortcutLaunchTimeKey);
if (maybe_last_shortcut_launch_time.has_value())
last_shortcut_launch_time_ =
base::Time::FromInternalValue(maybe_last_shortcut_launch_time.value());
}
double SiteEngagementScore::DecayedScore() const {
// Note that users can change their clock, so from this system's perspective
// time can go backwards. If that does happen and the system detects that the
// current day is earlier than the last engagement, no decay (or growth) is
// applied.
int hours_since_engagement =
(clock_->Now() - last_engagement_time_).InHours();
if (hours_since_engagement < 0)
return raw_score_;
int periods = hours_since_engagement / GetDecayPeriodInHours();
return std::max(0.0, raw_score_ * pow(GetDecayProportion(), periods) -
periods * GetDecayPoints());
}
double SiteEngagementScore::BonusIfShortcutLaunched() const {
int days_since_shortcut_launch =
(clock_->Now() - last_shortcut_launch_time_).InDays();
if (days_since_shortcut_launch <= kMaxDaysSinceShortcutLaunch)
return GetWebAppInstalledPoints();
return 0;
}
} // namespace site_engagement
|