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
|
// 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 "chrome/browser/engagement/important_sites_util.h"
#include <algorithm>
#include <map>
#include <memory>
#include <set>
#include <string_view>
#include <unordered_set>
#include <utility>
#include "base/containers/contains.h"
#include "base/metrics/histogram_macros.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/webapps/installable/installable_utils.h"
#include "chrome/common/pref_names.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/url_and_title.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_utils.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/site_engagement/content/site_engagement_score.h"
#include "components/site_engagement/content/site_engagement_service.h"
#include "components/site_engagement/core/mojom/site_engagement_details.mojom.h"
#include "components/webapps/browser/banners/app_banner_settings_helper.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "third_party/blink/public/mojom/site_engagement/site_engagement.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_util.h"
namespace site_engagement {
namespace {
using bookmarks::BookmarkModel;
using bookmarks::UrlAndTitle;
using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo;
using ImportantReason = ImportantSitesUtil::ImportantReason;
// Note: These values are stored on both the per-site content settings
// dictionary and the dialog preference dictionary.
static const char kTimeLastIgnored[] = "TimeLastIgnored";
static const int kSuppressionExpirationTimeDays = 30 * 5;
static const char kNumTimesIgnoredName[] = "NumTimesIgnored";
static const int kTimesIgnoredForSuppression = 3;
// These are the maximum # of bookmarks we can use as signals. If the user has
// <= kMaxBookmarks, then we just use those bookmarks. Otherwise we filter all
// bookmarks on site engagement > 0, sort, and trim to kMaxBookmarks.
static const int kMaxBookmarks = 5;
// We need this to be a macro, as the histogram macros cache their pointers
// after the first call, so when we change the uma name we check fail if we're
// just a method.
#define RECORD_UMA_FOR_IMPORTANT_REASON(uma_name, uma_count_name, \
reason_bitfield) \
do { \
int count = 0; \
int32_t bitfield = (reason_bitfield); \
for (int i = 0; i < ImportantReason::REASON_BOUNDARY; i++) { \
if ((bitfield >> i) & 1) { \
count++; \
UMA_HISTOGRAM_ENUMERATION((uma_name), static_cast<ImportantReason>(i), \
ImportantReason::REASON_BOUNDARY); \
} \
} \
UMA_HISTOGRAM_EXACT_LINEAR( \
(uma_count_name), count, \
static_cast<int>(ImportantReason::REASON_BOUNDARY)); \
} while (0)
void RecordIgnore(base::Value::Dict& dict) {
int times_ignored = dict.FindInt(kNumTimesIgnoredName).value_or(0);
dict.Set(kNumTimesIgnoredName, ++times_ignored);
dict.Set(kTimeLastIgnored, base::Time::Now().InSecondsFSinceUnixEpoch());
}
// If we should suppress the item with the given dictionary ignored record.
bool ShouldSuppressItem(base::Value::Dict& dict) {
std::optional<double> last_ignored_time = dict.FindDouble(kTimeLastIgnored);
if (last_ignored_time) {
base::TimeDelta diff =
base::Time::Now() -
base::Time::FromSecondsSinceUnixEpoch(*last_ignored_time);
if (diff >= base::Days(kSuppressionExpirationTimeDays)) {
dict.Set(kNumTimesIgnoredName, 0);
dict.Remove(kTimeLastIgnored);
return false;
}
}
std::optional<int> times_ignored = dict.FindInt(kNumTimesIgnoredName);
return times_ignored && *times_ignored >= kTimesIgnoredForSuppression;
}
void MaybePopulateImportantInfoForReason(
const GURL& origin,
std::set<GURL>* visited_origins,
ImportantReason reason,
std::optional<std::string> app_name,
std::map<std::string, ImportantDomainInfo>* output) {
if (!origin.is_valid() || !visited_origins->insert(origin).second)
return;
std::string registerable_domain =
ImportantSitesUtil::GetRegisterableDomainOrIP(origin);
if (registerable_domain.empty()) {
return;
}
ImportantDomainInfo& info = (*output)[registerable_domain];
info.reason_bitfield |= 1 << reason;
if (info.example_origin.is_empty()) {
info.registerable_domain = registerable_domain;
info.example_origin = origin;
}
info.app_name = app_name;
}
// Returns the score associated with the given reason. The order of
// ImportantReason does not need to correspond to the score order. The higher
// the score, the more important the reason is.
int GetScoreForReason(ImportantReason reason) {
switch (reason) {
case ImportantReason::ENGAGEMENT:
return 1 << 0;
case ImportantReason::DURABLE:
return 1 << 1;
case ImportantReason::BOOKMARKS:
return 1 << 2;
case ImportantReason::HOME_SCREEN:
return 1 << 3;
case ImportantReason::NOTIFICATIONS:
return 1 << 4;
case ImportantReason::REASON_BOUNDARY:
return 0;
}
return 0;
}
int GetScoreForReasonsBitfield(int32_t reason_bitfield) {
int score = 0;
for (int i = 0; i < ImportantReason::REASON_BOUNDARY; i++) {
if ((reason_bitfield >> i) & 1) {
score += GetScoreForReason(static_cast<ImportantReason>(i));
}
}
return score;
}
// Returns if |a| has a higher score than |b|, so that when we sort the higher
// score is first.
bool CompareDescendingImportantInfo(
const std::pair<std::string, ImportantDomainInfo>& a,
const std::pair<std::string, ImportantDomainInfo>& b) {
int score_a = GetScoreForReasonsBitfield(a.second.reason_bitfield);
int score_b = GetScoreForReasonsBitfield(b.second.reason_bitfield);
int bitfield_diff = score_a - score_b;
if (bitfield_diff != 0)
return bitfield_diff > 0;
return a.second.engagement_score > b.second.engagement_score;
}
std::unordered_set<std::string> GetSuppressedImportantDomains(
Profile* profile) {
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(profile);
std::unordered_set<std::string> ignoring_domains;
for (ContentSettingPatternSource& site :
map->GetSettingsForOneType(ContentSettingsType::IMPORTANT_SITE_INFO)) {
GURL origin(site.primary_pattern.ToString());
if (!origin.is_valid() || base::Contains(ignoring_domains, origin.host())) {
continue;
}
if (!site.setting_value.is_dict())
continue;
if (ShouldSuppressItem(site.setting_value.GetDict())) {
ignoring_domains.insert(origin.host());
}
}
return ignoring_domains;
}
// Inserts origins with some engagement measure into the map, including a site
// engagement cutoff and recent launches from home screen.
void PopulateInfoMapWithEngagement(
Profile* profile,
blink::mojom::EngagementLevel minimum_engagement,
std::map<GURL, double>* engagement_map,
std::map<std::string, ImportantDomainInfo>* output) {
SiteEngagementService* service = SiteEngagementService::Get(profile);
std::vector<mojom::SiteEngagementDetails> engagement_details =
service->GetAllDetails();
std::set<GURL> content_origins;
// We can have multiple origins for a single domain, so we record the one
// with the highest engagement score.
for (const auto& detail : engagement_details) {
if (detail.installed_bonus > 0) {
MaybePopulateImportantInfoForReason(detail.origin, &content_origins,
ImportantReason::HOME_SCREEN,
std::nullopt, output);
}
(*engagement_map)[detail.origin] = detail.total_score;
if (!SiteEngagementService::IsEngagementAtLeast(detail.total_score,
minimum_engagement)) {
continue;
}
std::string registerable_domain =
ImportantSitesUtil::GetRegisterableDomainOrIP(detail.origin);
if (registerable_domain.empty()) {
continue;
}
ImportantDomainInfo& info = (*output)[registerable_domain];
if (detail.total_score > info.engagement_score) {
info.registerable_domain = registerable_domain;
info.engagement_score = detail.total_score;
info.example_origin = detail.origin;
info.reason_bitfield |= 1 << ImportantReason::ENGAGEMENT;
}
}
}
void PopulateInfoMapWithContentTypeAllowed(
Profile* profile,
ContentSettingsType content_type,
ImportantReason reason,
std::map<std::string, ImportantDomainInfo>* output) {
// Extract a set of urls, using the primary pattern. We don't handle
// wildcard patterns.
std::set<GURL> content_origins;
for (const ContentSettingPatternSource& site :
HostContentSettingsMapFactory::GetForProfile(profile)
->GetSettingsForOneType(content_type)) {
if (site.GetContentSetting() != CONTENT_SETTING_ALLOW)
continue;
GURL url(site.primary_pattern.ToString());
MaybePopulateImportantInfoForReason(url, &content_origins, reason,
std::nullopt, output);
}
}
void PopulateInfoMapWithBookmarks(
Profile* profile,
const std::map<GURL, double>& engagement_map,
std::map<std::string, ImportantDomainInfo>* output) {
BookmarkModel* model =
BookmarkModelFactory::GetForBrowserContextIfExists(profile);
if (!model)
return;
std::vector<UrlAndTitle> untrimmed_bookmarks = model->GetUniqueUrls();
// Process the bookmarks and optionally trim them if we have too many.
std::vector<UrlAndTitle> result_bookmarks;
if (untrimmed_bookmarks.size() > kMaxBookmarks) {
std::ranges::copy_if(
untrimmed_bookmarks, std::back_inserter(result_bookmarks),
[&engagement_map](const UrlAndTitle& entry) {
auto it = engagement_map.find(entry.url.DeprecatedGetOriginAsURL());
double score = it == engagement_map.end() ? 0 : it->second;
return SiteEngagementService::IsEngagementAtLeast(
score, blink::mojom::EngagementLevel::LOW);
});
// TODO(dmurph): Simplify this (and probably much more) once
// SiteEngagementService::GetAllDetails lands (crbug/703848), as that will
// allow us to remove most of these lookups and merging of signals.
std::sort(
result_bookmarks.begin(), result_bookmarks.end(),
[&engagement_map](const UrlAndTitle& a, const UrlAndTitle& b) {
auto a_it = engagement_map.find(a.url.DeprecatedGetOriginAsURL());
auto b_it = engagement_map.find(b.url.DeprecatedGetOriginAsURL());
double a_score = a_it == engagement_map.end() ? 0 : a_it->second;
double b_score = b_it == engagement_map.end() ? 0 : b_it->second;
return a_score > b_score;
});
if (result_bookmarks.size() > kMaxBookmarks)
result_bookmarks.resize(kMaxBookmarks);
} else {
result_bookmarks = std::move(untrimmed_bookmarks);
}
std::set<GURL> content_origins;
for (const UrlAndTitle& bookmark : result_bookmarks) {
MaybePopulateImportantInfoForReason(bookmark.url, &content_origins,
ImportantReason::BOOKMARKS,
std::nullopt, output);
}
}
} // namespace
ImportantDomainInfo::ImportantDomainInfo() = default;
ImportantDomainInfo::~ImportantDomainInfo() = default;
ImportantDomainInfo::ImportantDomainInfo(ImportantDomainInfo&&) = default;
ImportantDomainInfo& ImportantDomainInfo::operator=(ImportantDomainInfo&&) =
default;
std::string ImportantSitesUtil::GetRegisterableDomainOrIP(const GURL& url) {
return GetRegisterableDomainOrIPFromHost(url.host_piece());
}
std::string ImportantSitesUtil::GetRegisterableDomainOrIPFromHost(
std::string_view host) {
std::string registerable_domain =
net::registry_controlled_domains::GetDomainAndRegistry(
host, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
if (registerable_domain.empty() && url::HostIsIPAddress(host))
registerable_domain = std::string(host);
return registerable_domain;
}
bool ImportantSitesUtil::IsDialogDisabled(Profile* profile) {
PrefService* service = profile->GetPrefs();
ScopedDictPrefUpdate update(service, prefs::kImportantSitesDialogHistory);
return ShouldSuppressItem(update.Get());
}
void ImportantSitesUtil::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(prefs::kImportantSitesDialogHistory);
}
// static
std::set<std::string> ImportantSitesUtil::GetInstalledRegisterableDomains(
Profile* profile) {
std::set<GURL> installed_origins = GetOriginsWithInstalledWebApps(profile);
std::set<std::string> registerable_domains;
for (auto& origin : installed_origins) {
registerable_domains.emplace(
ImportantSitesUtil::GetRegisterableDomainOrIP(origin));
}
return registerable_domains;
}
std::vector<ImportantDomainInfo>
ImportantSitesUtil::GetImportantRegisterableDomains(Profile* profile,
size_t max_results) {
SCOPED_UMA_HISTOGRAM_TIMER("Storage.ImportantSites.GenerationTime");
std::map<std::string, ImportantDomainInfo> important_info;
std::map<GURL, double> engagement_map;
PopulateInfoMapWithEngagement(profile, blink::mojom::EngagementLevel::MEDIUM,
&engagement_map, &important_info);
PopulateInfoMapWithContentTypeAllowed(
profile, ContentSettingsType::NOTIFICATIONS,
ImportantReason::NOTIFICATIONS, &important_info);
PopulateInfoMapWithContentTypeAllowed(
profile, ContentSettingsType::DURABLE_STORAGE, ImportantReason::DURABLE,
&important_info);
PopulateInfoMapWithBookmarks(profile, engagement_map, &important_info);
std::unordered_set<std::string> suppressed_domains =
GetSuppressedImportantDomains(profile);
std::vector<std::pair<std::string, ImportantDomainInfo>> items;
for (auto& item : important_info)
items.emplace_back(std::move(item));
std::sort(items.begin(), items.end(), &CompareDescendingImportantInfo);
std::vector<ImportantDomainInfo> final_list;
for (std::pair<std::string, ImportantDomainInfo>& domain_info : items) {
if (final_list.size() >= max_results)
return final_list;
if (suppressed_domains.find(domain_info.first) != suppressed_domains.end())
continue;
final_list.push_back(std::move(domain_info.second));
RECORD_UMA_FOR_IMPORTANT_REASON(
"Storage.ImportantSites.GeneratedReason",
"Storage.ImportantSites.GeneratedReasonCount",
domain_info.second.reason_bitfield);
}
return final_list;
}
void ImportantSitesUtil::RecordExcludedAndIgnoredImportantSites(
Profile* profile,
const std::vector<std::string>& excluded_sites,
const std::vector<int32_t>& excluded_sites_reason_bitfield,
const std::vector<std::string>& ignored_sites,
const std::vector<int32_t>& ignored_sites_reason_bitfield) {
// First, record the metrics for excluded and ignored sites.
for (int32_t reason_bitfield : excluded_sites_reason_bitfield) {
RECORD_UMA_FOR_IMPORTANT_REASON(
"Storage.ImportantSites.CBDChosenReason",
"Storage.ImportantSites.CBDChosenReasonCount", reason_bitfield);
}
for (int32_t reason_bitfield : ignored_sites_reason_bitfield) {
RECORD_UMA_FOR_IMPORTANT_REASON(
"Storage.ImportantSites.CBDIgnoredReason",
"Storage.ImportantSites.CBDIgnoredReasonCount", reason_bitfield);
}
HostContentSettingsMap* map =
HostContentSettingsMapFactory::GetForProfile(profile);
// We use the ignored sites to update our ignore counter only if the user
// chose to exclude a site.
if (!excluded_sites.empty()) {
for (const std::string& ignored_site : ignored_sites) {
GURL origin("http://" + ignored_site);
base::Value dict = map->GetWebsiteSetting(
origin, origin, ContentSettingsType::IMPORTANT_SITE_INFO);
if (!dict.is_dict())
dict = base::Value(base::Value::Type::DICT);
RecordIgnore(dict.GetDict());
map->SetWebsiteSettingDefaultScope(
origin, origin, ContentSettingsType::IMPORTANT_SITE_INFO,
std::move(dict));
}
} else {
// Record that the user did not interact with the dialog.
PrefService* service = profile->GetPrefs();
ScopedDictPrefUpdate update(service, prefs::kImportantSitesDialogHistory);
RecordIgnore(update.Get());
}
// We clear our ignore counter for sites that the user chose.
for (const std::string& excluded_site : excluded_sites) {
GURL origin("http://" + excluded_site);
base::Value::Dict dict;
dict.Set(kNumTimesIgnoredName, 0);
dict.Remove(kTimeLastIgnored);
map->SetWebsiteSettingDefaultScope(origin, origin,
ContentSettingsType::IMPORTANT_SITE_INFO,
base::Value(std::move(dict)));
}
}
void ImportantSitesUtil::MarkOriginAsImportantForTesting(Profile* profile,
const GURL& origin) {
SiteEngagementScore::SetParamValuesForTesting();
// First get data from site engagement.
SiteEngagementService* site_engagement_service =
SiteEngagementService::Get(profile);
site_engagement_service->ResetBaseScoreForURL(
origin, SiteEngagementScore::GetMediumEngagementBoundary());
double score = site_engagement_service->GetScore(origin);
DCHECK(SiteEngagementService::IsEngagementAtLeast(
score, blink::mojom::EngagementLevel::MEDIUM));
}
} // namespace site_engagement
|