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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/engagement/site_engagement_service.h"
#include <algorithm>
#include <cmath>
#include <vector>
#include "base/command_line.h"
#include "base/metrics/field_trial.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/time/clock.h"
#include "base/time/default_clock.h"
#include "base/values.h"
#include "chrome/browser/banners/app_banner_settings_helper.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/engagement/site_engagement_eviction_policy.h"
#include "chrome/browser/engagement/site_engagement_helper.h"
#include "chrome/browser/engagement/site_engagement_service_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/history/core/browser/history_service.h"
#include "components/variations/variations_associated_data.h"
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h"
namespace {
// Global bool to ensure we only update the parameters from variations once.
bool g_updated_from_variations = false;
// Keys used in the variations params. Order matches
// SiteEngagementScore::Variation enum.
const char* kVariationNames[] = {
"max_points_per_day",
"decay_period_in_days",
"decay_points",
"navigation_points",
"user_input_points",
"visible_media_playing_points",
"hidden_media_playing_points",
};
// Length of time between metrics logging.
const int kMetricsIntervalInMinutes = 60;
// Delta within which to consider scores equal.
const 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.
const double kTimeDelta = 1000000;
scoped_ptr<ContentSettingsForOneType> GetEngagementContentSettings(
HostContentSettingsMap* settings_map) {
scoped_ptr<ContentSettingsForOneType> engagement_settings(
new ContentSettingsForOneType);
settings_map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT,
std::string(), engagement_settings.get());
return engagement_settings.Pass();
}
bool DoublesConsideredDifferent(double value1, double value2, double delta) {
double abs_difference = fabs(value1 - value2);
return abs_difference > delta;
}
// Only accept a navigation event for engagement if it is one of:
// a. direct typed navigation
// b. clicking on an omnibox suggestion brought up by typing a keyword
// c. clicking on a bookmark or opening a bookmark app
// d. a custom search engine keyword search (e.g. Wikipedia search box added as
// search engine).
bool IsEngagementNavigation(ui::PageTransition transition) {
return ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_TYPED) ||
ui::PageTransitionCoreTypeIs(transition,
ui::PAGE_TRANSITION_GENERATED) ||
ui::PageTransitionCoreTypeIs(transition,
ui::PAGE_TRANSITION_AUTO_BOOKMARK) ||
ui::PageTransitionCoreTypeIs(transition,
ui::PAGE_TRANSITION_KEYWORD_GENERATED);
}
scoped_ptr<base::DictionaryValue> GetScoreDictForOrigin(
HostContentSettingsMap* settings,
const GURL& origin_url) {
if (!settings)
return scoped_ptr<base::DictionaryValue>();
scoped_ptr<base::Value> value = settings->GetWebsiteSetting(
origin_url, origin_url, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT,
std::string(), NULL);
if (!value.get())
return make_scoped_ptr(new base::DictionaryValue());
if (!value->IsType(base::Value::TYPE_DICTIONARY))
return make_scoped_ptr(new base::DictionaryValue());
return make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release()));
}
} // namespace
const double SiteEngagementScore::kMaxPoints = 100;
double SiteEngagementScore::param_values[] = {
5, // MAX_POINTS_PER_DAY
7, // DECAY_PERIOD_IN_DAYS
5, // DECAY_POINTS
0.5, // NAVIGATION_POINTS
0.05, // USER_INPUT_POINTS
0.02, // VISIBLE_MEDIA_POINTS
0.01, // HIDDEN_MEDIA_POINTS
};
const char* SiteEngagementScore::kRawScoreKey = "rawScore";
const char* SiteEngagementScore::kPointsAddedTodayKey = "pointsAddedToday";
const char* SiteEngagementScore::kLastEngagementTimeKey = "lastEngagementTime";
double SiteEngagementScore::GetMaxPointsPerDay() {
return param_values[MAX_POINTS_PER_DAY];
}
double SiteEngagementScore::GetDecayPeriodInDays() {
return param_values[DECAY_PERIOD_IN_DAYS];
}
double SiteEngagementScore::GetDecayPoints() {
return param_values[DECAY_POINTS];
}
double SiteEngagementScore::GetNavigationPoints() {
return param_values[NAVIGATION_POINTS];
}
double SiteEngagementScore::GetUserInputPoints() {
return param_values[USER_INPUT_POINTS];
}
double SiteEngagementScore::GetVisibleMediaPoints() {
return param_values[VISIBLE_MEDIA_POINTS];
}
double SiteEngagementScore::GetHiddenMediaPoints() {
return param_values[HIDDEN_MEDIA_POINTS];
}
void SiteEngagementScore::UpdateFromVariations() {
double param_vals[MAX_VARIATION];
for (int i = 0; i < MAX_VARIATION; ++i) {
std::string param_string = variations::GetVariationParamValue(
SiteEngagementService::kEngagementParams, kVariationNames[i]);
// 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::param_values[i] = param_vals[i];
}
SiteEngagementScore::SiteEngagementScore(
base::Clock* clock,
const base::DictionaryValue& score_dict)
: SiteEngagementScore(clock) {
score_dict.GetDouble(kRawScoreKey, &raw_score_);
score_dict.GetDouble(kPointsAddedTodayKey, &points_added_today_);
double internal_time;
if (score_dict.GetDouble(kLastEngagementTimeKey, &internal_time))
last_engagement_time_ = base::Time::FromInternalValue(internal_time);
}
SiteEngagementScore::~SiteEngagementScore() {
}
double SiteEngagementScore::Score() const {
return DecayedScore();
}
void SiteEngagementScore::AddPoints(double 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;
}
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;
}
bool SiteEngagementScore::MaxPointsPerDayAdded() {
if (!last_engagement_time_.is_null() &&
clock_->Now().LocalMidnight() != last_engagement_time_.LocalMidnight()) {
return false;
}
return points_added_today_ == GetMaxPointsPerDay();
}
bool SiteEngagementScore::UpdateScoreDict(base::DictionaryValue* score_dict) {
double raw_score_orig = 0;
double points_added_today_orig = 0;
double last_engagement_time_internal_orig = 0;
score_dict->GetDouble(kRawScoreKey, &raw_score_orig);
score_dict->GetDouble(kPointsAddedTodayKey, &points_added_today_orig);
score_dict->GetDouble(kLastEngagementTimeKey,
&last_engagement_time_internal_orig);
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);
if (!changed)
return false;
score_dict->SetDouble(kRawScoreKey, raw_score_);
score_dict->SetDouble(kPointsAddedTodayKey, points_added_today_);
score_dict->SetDouble(kLastEngagementTimeKey,
last_engagement_time_.ToInternalValue());
return true;
}
SiteEngagementScore::SiteEngagementScore(base::Clock* clock)
: clock_(clock),
raw_score_(0),
points_added_today_(0),
last_engagement_time_() {}
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 days_since_engagement = (clock_->Now() - last_engagement_time_).InDays();
if (days_since_engagement < 0)
return raw_score_;
int periods = days_since_engagement / GetDecayPeriodInDays();
double decayed_score = raw_score_ - periods * GetDecayPoints();
return std::max(0.0, decayed_score);
}
const char SiteEngagementService::kEngagementParams[] = "SiteEngagement";
// static
SiteEngagementService* SiteEngagementService::Get(Profile* profile) {
return SiteEngagementServiceFactory::GetForProfile(profile);
}
// static
bool SiteEngagementService::IsEnabled() {
// If the engagement service or any of its dependencies are force-enabled,
// return true immediately.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableSiteEngagementService) ||
SiteEngagementEvictionPolicy::IsEnabled() ||
AppBannerSettingsHelper::ShouldUseSiteEngagementScore()) {
return true;
}
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableSiteEngagementService)) {
return false;
}
const std::string group_name =
base::FieldTrialList::FindFullName(kEngagementParams);
return base::StartsWith(group_name, "Enabled", base::CompareCase::SENSITIVE);
}
SiteEngagementService::SiteEngagementService(Profile* profile)
: SiteEngagementService(profile, make_scoped_ptr(new base::DefaultClock)) {
content::BrowserThread::PostAfterStartupTask(
FROM_HERE, content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::UI),
base::Bind(&SiteEngagementService::AfterStartupTask,
weak_factory_.GetWeakPtr()));
if (!g_updated_from_variations) {
SiteEngagementScore::UpdateFromVariations();
g_updated_from_variations = true;
}
}
SiteEngagementService::~SiteEngagementService() {
history::HistoryService* history = HistoryServiceFactory::GetForProfile(
profile_, ServiceAccessType::IMPLICIT_ACCESS);
if (history)
history->RemoveObserver(this);
}
void SiteEngagementService::HandleNavigation(const GURL& url,
ui::PageTransition transition) {
if (IsEngagementNavigation(transition)) {
SiteEngagementMetrics::RecordEngagement(
SiteEngagementMetrics::ENGAGEMENT_NAVIGATION);
AddPoints(url, SiteEngagementScore::GetNavigationPoints());
RecordMetrics();
}
}
void SiteEngagementService::HandleUserInput(
const GURL& url,
SiteEngagementMetrics::EngagementType type) {
SiteEngagementMetrics::RecordEngagement(type);
AddPoints(url, SiteEngagementScore::GetUserInputPoints());
RecordMetrics();
}
void SiteEngagementService::HandleMediaPlaying(const GURL& url,
bool is_hidden) {
SiteEngagementMetrics::RecordEngagement(
is_hidden ? SiteEngagementMetrics::ENGAGEMENT_MEDIA_HIDDEN
: SiteEngagementMetrics::ENGAGEMENT_MEDIA_VISIBLE);
AddPoints(url, is_hidden ? SiteEngagementScore::GetHiddenMediaPoints()
: SiteEngagementScore::GetVisibleMediaPoints());
RecordMetrics();
}
void SiteEngagementService::OnURLsDeleted(
history::HistoryService* history_service,
bool all_history,
bool expired,
const history::URLRows& deleted_rows,
const std::set<GURL>& favicon_urls) {
std::set<GURL> origins;
for (const history::URLRow& row : deleted_rows)
origins.insert(row.url().GetOrigin());
history::HistoryService* hs = HistoryServiceFactory::GetForProfile(
profile_, ServiceAccessType::EXPLICIT_ACCESS);
hs->GetCountsForOrigins(
origins, base::Bind(&SiteEngagementService::GetCountsForOriginsComplete,
weak_factory_.GetWeakPtr()));
}
double SiteEngagementService::GetScore(const GURL& url) {
HostContentSettingsMap* settings_map =
HostContentSettingsMapFactory::GetForProfile(profile_);
scoped_ptr<base::DictionaryValue> score_dict =
GetScoreDictForOrigin(settings_map, url);
SiteEngagementScore score(clock_.get(), *score_dict);
return score.Score();
}
double SiteEngagementService::GetTotalEngagementPoints() {
std::map<GURL, double> score_map = GetScoreMap();
double total_score = 0;
for (const auto& value : score_map)
total_score += value.second;
return total_score;
}
std::map<GURL, double> SiteEngagementService::GetScoreMap() {
HostContentSettingsMap* settings_map =
HostContentSettingsMapFactory::GetForProfile(profile_);
scoped_ptr<ContentSettingsForOneType> engagement_settings =
GetEngagementContentSettings(settings_map);
std::map<GURL, double> score_map;
for (const auto& site : *engagement_settings) {
GURL origin(site.primary_pattern.ToString());
if (!origin.is_valid())
continue;
scoped_ptr<base::DictionaryValue> score_dict =
GetScoreDictForOrigin(settings_map, origin);
SiteEngagementScore score(clock_.get(), *score_dict);
score_map[origin] = score.Score();
}
return score_map;
}
SiteEngagementService::SiteEngagementService(Profile* profile,
scoped_ptr<base::Clock> clock)
: profile_(profile), clock_(clock.Pass()), weak_factory_(this) {
// May be null in tests.
history::HistoryService* history = HistoryServiceFactory::GetForProfile(
profile, ServiceAccessType::IMPLICIT_ACCESS);
if (history)
history->AddObserver(this);
}
void SiteEngagementService::AddPoints(const GURL& url, double points) {
HostContentSettingsMap* settings_map =
HostContentSettingsMapFactory::GetForProfile(profile_);
scoped_ptr<base::DictionaryValue> score_dict =
GetScoreDictForOrigin(settings_map, url);
SiteEngagementScore score(clock_.get(), *score_dict);
score.AddPoints(points);
if (score.UpdateScoreDict(score_dict.get())) {
settings_map->SetWebsiteSettingDefaultScope(
url, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(),
score_dict.release());
}
}
void SiteEngagementService::AfterStartupTask() {
CleanupEngagementScores();
RecordMetrics();
}
void SiteEngagementService::CleanupEngagementScores() {
HostContentSettingsMap* settings_map =
HostContentSettingsMapFactory::GetForProfile(profile_);
scoped_ptr<ContentSettingsForOneType> engagement_settings =
GetEngagementContentSettings(settings_map);
for (const auto& site : *engagement_settings) {
GURL origin(site.primary_pattern.ToString());
if (origin.is_valid()) {
scoped_ptr<base::DictionaryValue> score_dict =
GetScoreDictForOrigin(settings_map, origin);
SiteEngagementScore score(clock_.get(), *score_dict);
if (score.Score() != 0)
continue;
}
settings_map->SetWebsiteSettingDefaultScope(
origin, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(),
nullptr);
}
}
void SiteEngagementService::RecordMetrics() {
base::Time now = clock_->Now();
if (last_metrics_time_.is_null() ||
(now - last_metrics_time_).InMinutes() >= kMetricsIntervalInMinutes) {
last_metrics_time_ = now;
std::map<GURL, double> score_map = GetScoreMap();
int origins_with_max_engagement = OriginsWithMaxEngagement(score_map);
int total_origins = score_map.size();
int percent_origins_with_max_engagement =
(total_origins == 0 ? 0 : (origins_with_max_engagement * 100) /
total_origins);
double total_engagement = GetTotalEngagementPoints();
double mean_engagement =
(total_origins == 0 ? 0 : total_engagement / total_origins);
SiteEngagementMetrics::RecordTotalOriginsEngaged(total_origins);
SiteEngagementMetrics::RecordTotalSiteEngagement(total_engagement);
SiteEngagementMetrics::RecordMeanEngagement(mean_engagement);
SiteEngagementMetrics::RecordMedianEngagement(
GetMedianEngagement(score_map));
SiteEngagementMetrics::RecordEngagementScores(score_map);
SiteEngagementMetrics::RecordOriginsWithMaxDailyEngagement(
OriginsWithMaxDailyEngagement());
SiteEngagementMetrics::RecordOriginsWithMaxEngagement(
origins_with_max_engagement);
SiteEngagementMetrics::RecordPercentOriginsWithMaxEngagement(
percent_origins_with_max_engagement);
}
}
double SiteEngagementService::GetMedianEngagement(
std::map<GURL, double>& score_map) {
if (score_map.size() == 0)
return 0;
std::vector<double> scores;
scores.reserve(score_map.size());
for (const auto& value : score_map)
scores.push_back(value.second);
// Calculate the median as the middle value of the sorted engagement scores
// if there are an odd number of scores, or the average of the two middle
// scores otherwise.
std::sort(scores.begin(), scores.end());
size_t mid = scores.size() / 2;
if (scores.size() % 2 == 1)
return scores[mid];
else
return (scores[mid - 1] + scores[mid]) / 2;
}
int SiteEngagementService::OriginsWithMaxDailyEngagement() {
HostContentSettingsMap* settings_map =
HostContentSettingsMapFactory::GetForProfile(profile_);
scoped_ptr<ContentSettingsForOneType> engagement_settings =
GetEngagementContentSettings(settings_map);
int total_origins = 0;
// We cannot call GetScoreMap as we need the score objects, not raw scores.
for (const auto& site : *engagement_settings) {
GURL origin(site.primary_pattern.ToString());
if (!origin.is_valid())
continue;
scoped_ptr<base::DictionaryValue> score_dict =
GetScoreDictForOrigin(settings_map, origin);
SiteEngagementScore score(clock_.get(), *score_dict);
if (score.MaxPointsPerDayAdded())
++total_origins;
}
return total_origins;
}
int SiteEngagementService::OriginsWithMaxEngagement(
std::map<GURL, double>& score_map) {
int total_origins = 0;
for (const auto& value : score_map)
if (value.second == SiteEngagementScore::kMaxPoints)
++total_origins;
return total_origins;
}
void SiteEngagementService::GetCountsForOriginsComplete(
const history::OriginCountMap& origin_counts) {
HostContentSettingsMap* settings_map =
HostContentSettingsMapFactory::GetForProfile(profile_);
for (const auto& origin_to_count : origin_counts) {
if (origin_to_count.second != 0)
continue;
settings_map->SetWebsiteSettingDefaultScope(
origin_to_count.first, GURL(), CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT,
std::string(), nullptr);
}
}
|