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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
|
// Copyright 2015 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/ntp_tiles/popular_sites_impl.h"
#include <stddef.h>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/types/expected_macros.h"
#include "base/values.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "components/google/core/common/google_util.h"
#include "components/ntp_tiles/features.h"
#include "components/ntp_tiles/pref_names.h"
#include "components/ntp_tiles/switches.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/search_engine_type.h"
#include "components/search_engines/template_url_service.h"
#include "components/variations/service/variations_service.h"
#include "components/variations/variations_associated_data.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
#include "base/json/json_reader.h"
#include "components/grit/components_resources.h"
#include "ui/base/resource/resource_bundle.h"
#endif
#if BUILDFLAG(IS_IOS)
#include "components/ntp_tiles/country_code_ios.h"
#endif
using variations::VariationsService;
namespace ntp_tiles {
namespace {
const char kPopularSitesURLFormat[] =
"https://www.gstatic.com/%ssuggested_sites_%s_%s.json";
const char kPopularSitesDefaultDirectory[] = "chrome/ntp/";
const char kPopularSitesDefaultCountryCode[] = "DEFAULT";
const char kPopularSitesDefaultVersion[] = "5";
const int kSitesExplorationStartVersion = 6;
const int kPopularSitesRedownloadIntervalHours = 24;
#if BUILDFLAG(IS_IOS)
const char kIOSDefaultPopularSitesLocaleUS[] =
"https://www.gstatic.com/chrome/ntp/ios/"
"suggested_sites_US_2023q1_mvt_experiment_with_popular_sites.json";
#endif
GURL GetPopularSitesURL(const std::string& directory,
const std::string& country,
const std::string& version) {
// US-based Chrome iOS users have a curated set of default Most Visited Tiles,
// based on Chrome iOS history in the US.
#if BUILDFLAG(IS_IOS)
if (country == "US") {
return GURL(kIOSDefaultPopularSitesLocaleUS);
}
#endif
return GURL(base::StringPrintf(kPopularSitesURLFormat, directory.c_str(),
country.c_str(), version.c_str()));
}
// Extract the country from the default search engine if the default search
// engine is Google.
std::string GetDefaultSearchEngineCountryCode(
const TemplateURLService* template_url_service) {
DCHECK(template_url_service);
base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
if (!cmd_line->HasSwitch(switches::kEnableNTPSearchEngineCountryDetection)) {
return std::string();
}
const TemplateURL* default_provider =
template_url_service->GetDefaultSearchProvider();
// It's possible to not have a default provider in the case that the default
// search engine is defined by policy.
if (default_provider) {
bool is_google_search_engine =
default_provider->GetEngineType(
template_url_service->search_terms_data()) ==
SearchEngineType::SEARCH_ENGINE_GOOGLE;
if (is_google_search_engine) {
GURL search_url = default_provider->GenerateSearchURL(
template_url_service->search_terms_data());
return google_util::GetGoogleCountryCode(search_url);
}
}
return std::string();
}
std::string GetVariationCountry() {
return base::GetFieldTrialParamValue(kPopularSitesFieldTrialName, "country");
}
std::string GetVariationVersion() {
return base::GetFieldTrialParamValue(kPopularSitesFieldTrialName, "version");
}
std::string GetVariationDirectory() {
return base::GetFieldTrialParamValue(kPopularSitesFieldTrialName,
"directory");
}
PopularSites::SitesVector ParseSiteList(const base::Value::List& list) {
PopularSites::SitesVector sites;
for (const base::Value& item_value : list) {
if (!item_value.is_dict()) {
continue;
}
const base::Value::Dict& item = item_value.GetDict();
std::u16string title;
if (const std::string* ptr = item.FindString("title")) {
title = base::UTF8ToUTF16(*ptr);
} else {
continue;
}
std::string url;
if (const std::string* ptr = item.FindString("url")) {
url = *ptr;
} else {
continue;
}
std::string favicon_url;
if (const std::string* ptr = item.FindString("favicon_url")) {
favicon_url = *ptr;
}
std::string large_icon_url;
if (const std::string* ptr = item.FindString("large_icon_url")) {
large_icon_url = *ptr;
}
TileTitleSource title_source = TileTitleSource::UNKNOWN;
std::optional<int> title_source_int = item.FindInt("title_source");
if (!title_source_int) {
// Only v6 and later have "title_source". Earlier versions use title tags.
title_source = TileTitleSource::TITLE_TAG;
} else if (*title_source_int <= static_cast<int>(TileTitleSource::LAST) &&
*title_source_int >= 0) {
title_source = static_cast<TileTitleSource>(*title_source_int);
}
sites.emplace_back(title, GURL(url), GURL(favicon_url),
GURL(large_icon_url), title_source);
std::optional<int> default_icon_resource =
item.FindInt("default_icon_resource");
if (default_icon_resource) {
sites.back().default_icon_resource = *default_icon_resource;
}
std::optional<bool> baked_in = item.FindBool("baked_in");
if (baked_in.has_value()) {
sites.back().baked_in = baked_in.value();
}
}
return sites;
}
std::map<SectionType, PopularSites::SitesVector> ParseVersion5(
const base::Value::List& list) {
return {{SectionType::PERSONALIZED, ParseSiteList(list)}};
}
std::map<SectionType, PopularSites::SitesVector> ParseVersion6OrAbove(
const base::Value::List& list) {
// Valid lists would have contained at least the PERSONALIZED section.
std::map<SectionType, PopularSites::SitesVector> sections = {
std::make_pair(SectionType::PERSONALIZED, PopularSites::SitesVector{})};
for (size_t i = 0; i < list.size(); i++) {
const base::Value::Dict* item_dict = list[i].GetIfDict();
if (!item_dict) {
LOG(WARNING) << "Parsed SitesExploration list contained an invalid "
<< "section at position " << i << ".";
continue;
}
int section = item_dict->FindInt("section").value_or(-1);
if (section < 0 || section > static_cast<int>(SectionType::LAST)) {
LOG(WARNING) << "Parsed SitesExploration list contained a section with "
<< "invalid ID (" << section << ")";
continue;
}
// Non-personalized site exploration tiles are no longer supported, so
// ignore all other section types.
SectionType section_type = static_cast<SectionType>(section);
if (section_type != SectionType::PERSONALIZED) {
continue;
}
const base::Value::List* sites_list = item_dict->FindList("sites");
if (!sites_list) {
continue;
}
sections[section_type] = ParseSiteList(*sites_list);
}
return sections;
}
std::map<SectionType, PopularSites::SitesVector> ParseSites(
const base::Value::List& list,
int version) {
if (version >= kSitesExplorationStartVersion) {
return ParseVersion6OrAbove(list);
}
return ParseVersion5(list);
}
#if BUILDFLAG(GOOGLE_CHROME_BRANDING) && \
(BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS))
void SetDefaultResourceForSite(size_t index,
int resource_id,
base::Value::List& sites) {
if (index >= sites.size() || !sites[index].is_dict()) {
return;
}
sites[index].GetDict().Set("default_icon_resource", resource_id);
}
#endif
// Creates the list of popular sites based on a snapshot available for mobile.
base::Value::List DefaultPopularSites(std::optional<std::string> country) {
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
return base::Value::List();
#else
if (!base::FeatureList::IsEnabled(kPopularSitesBakedInContentFeature)) {
return base::Value::List();
}
int popular_sites_json = IDR_DEFAULT_POPULAR_SITES_JSON;
#if BUILDFLAG(IS_IOS)
if (country.has_value() && *country == "US") {
popular_sites_json = IDR_DEFAULT_POPULAR_SITES_WITH_POPULAR_APPS_JSON;
}
#endif
std::optional<base::Value> sites = base::JSONReader::Read(
ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
popular_sites_json));
base::Value::List& sites_list = sites->GetList();
for (base::Value& site : sites_list) {
site.GetDict().Set("baked_in", true);
}
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
static constexpr int default_popular_sites_icons[] = {
IDR_DEFAULT_POPULAR_SITES_ICON0, IDR_DEFAULT_POPULAR_SITES_ICON1,
IDR_DEFAULT_POPULAR_SITES_ICON2, IDR_DEFAULT_POPULAR_SITES_ICON3,
IDR_DEFAULT_POPULAR_SITES_ICON4, IDR_DEFAULT_POPULAR_SITES_ICON5,
IDR_DEFAULT_POPULAR_SITES_ICON6, IDR_DEFAULT_POPULAR_SITES_ICON7};
base::span<const int> icon_list = default_popular_sites_icons;
#if BUILDFLAG(IS_IOS)
// US-based Chrome iOS users have a curated set of default Most Visited Tiles,
// based on Chrome iOS history in the US.
if (country.has_value() && *country == "US") {
static constexpr int popular_sites_icons_with_popular_ios_apps[] = {
IDR_DEFAULT_POPULAR_SITES_WITH_POPULAR_APPS_ICON0,
IDR_DEFAULT_POPULAR_SITES_WITH_POPULAR_APPS_ICON1,
IDR_DEFAULT_POPULAR_SITES_WITH_POPULAR_APPS_ICON2,
IDR_DEFAULT_POPULAR_SITES_WITH_POPULAR_APPS_ICON3,
IDR_DEFAULT_POPULAR_SITES_WITH_POPULAR_APPS_ICON4,
IDR_DEFAULT_POPULAR_SITES_WITH_POPULAR_APPS_ICON5,
IDR_DEFAULT_POPULAR_SITES_WITH_POPULAR_APPS_ICON6,
IDR_DEFAULT_POPULAR_SITES_WITH_POPULAR_APPS_ICON7};
icon_list = popular_sites_icons_with_popular_ios_apps;
}
#endif
size_t index = 0;
for (int icon_resource : icon_list) {
SetDefaultResourceForSite(index++, icon_resource, sites_list);
}
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
return std::move(sites_list);
#endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
}
} // namespace
PopularSites::Site::Site(const std::u16string& title,
const GURL& url,
const GURL& favicon_url,
const GURL& large_icon_url,
TileTitleSource title_source)
: title(title),
url(url),
favicon_url(favicon_url),
large_icon_url(large_icon_url),
title_source(title_source),
baked_in(false),
default_icon_resource(-1) {}
PopularSites::Site::Site(const Site& other) = default;
PopularSites::Site::~Site() = default;
PopularSitesImpl::PopularSitesImpl(
PrefService* prefs,
const TemplateURLService* template_url_service,
VariationsService* variations_service,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: prefs_(prefs),
template_url_service_(template_url_service),
variations_(variations_service),
url_loader_factory_(std::move(url_loader_factory)),
is_fallback_(false),
sections_(
ParseSites(prefs->GetList(prefs::kPopularSitesJsonPref),
prefs_->GetInteger(prefs::kPopularSitesVersionPref))) {}
PopularSitesImpl::~PopularSitesImpl() = default;
bool PopularSitesImpl::MaybeStartFetch(bool force_download,
FinishedCallback callback) {
DCHECK(!callback_);
callback_ = std::move(callback);
const base::Time last_download_time = base::Time::FromInternalValue(
prefs_->GetInt64(prefs::kPopularSitesLastDownloadPref));
const base::TimeDelta time_since_last_download =
base::Time::Now() - last_download_time;
const base::TimeDelta redownload_interval =
base::Hours(kPopularSitesRedownloadIntervalHours);
const bool download_time_is_future = base::Time::Now() < last_download_time;
pending_url_ = GetURLToFetch();
const bool url_changed =
pending_url_.spec() != prefs_->GetString(prefs::kPopularSitesURLPref);
// Download forced, or we need to download a new file.
if (force_download || download_time_is_future ||
(time_since_last_download > redownload_interval) || url_changed) {
FetchPopularSites();
return true;
}
return false;
}
const std::map<SectionType, PopularSitesImpl::SitesVector>&
PopularSitesImpl::sections() const {
return sections_;
}
GURL PopularSitesImpl::GetURLToFetch() {
const std::string directory = GetDirectoryToFetch();
const std::string country = GetCountryToFetch();
const std::string version = GetVersionToFetch();
if (!base::StringToInt(version, &version_in_pending_url_)) {
// Parses the leading digits as version. Defaults to 0 if that failed.
if (version_in_pending_url_ <= 0) {
bool success = base::StringToInt(kPopularSitesDefaultVersion,
&version_in_pending_url_);
DLOG(WARNING) << "The set version \"" << version << "\" does not start "
<< "with a valid version number. Default version was used "
<< "instead (" << kPopularSitesDefaultVersion << ").";
DCHECK(success);
}
}
const GURL override_url =
GURL(prefs_->GetString(prefs::kPopularSitesOverrideURL));
return override_url.is_valid()
? override_url
: GetPopularSitesURL(directory, country, version);
}
std::string PopularSitesImpl::GetDirectoryToFetch() {
std::string directory =
prefs_->GetString(prefs::kPopularSitesOverrideDirectory);
if (directory.empty()) {
directory = GetVariationDirectory();
}
if (directory.empty()) {
directory = kPopularSitesDefaultDirectory;
}
return directory;
}
// Determine the country code to use. In order of precedence:
// - The explicit "override country" pref set by the user.
// - The country code from the field trial config (variation parameter).
// - The Google country code if Google is the default search engine (and the
// "--enable-ntp-search-engine-country-detection" switch is present).
// - The country provided by the VariationsService.
// - A default fallback.
std::string PopularSitesImpl::GetCountryToFetch() {
std::string country_code =
prefs_->GetString(prefs::kPopularSitesOverrideCountry);
if (country_code.empty()) {
country_code = GetVariationCountry();
}
if (country_code.empty()) {
country_code = GetDefaultSearchEngineCountryCode(template_url_service_);
}
if (country_code.empty() && variations_) {
country_code = variations_->GetStoredPermanentCountry();
}
#if BUILDFLAG(IS_IOS)
if (country_code.empty()) {
country_code = GetDeviceCountryCode();
}
#endif
if (country_code.empty()) {
country_code = kPopularSitesDefaultCountryCode;
}
return base::ToUpperASCII(country_code);
}
// Determine the version to use. In order of precedence:
// - The explicit "override version" pref set by the user.
// - The version from the field trial config (variation parameter).
// - A default fallback.
std::string PopularSitesImpl::GetVersionToFetch() {
std::string version = prefs_->GetString(prefs::kPopularSitesOverrideVersion);
if (version.empty()) {
version = GetVariationVersion();
}
if (version.empty()) {
version = kPopularSitesDefaultVersion;
}
return version;
}
const base::Value::List& PopularSitesImpl::GetCachedJson() {
return prefs_->GetList(prefs::kPopularSitesJsonPref);
}
// static
void PopularSitesImpl::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* user_prefs) {
user_prefs->RegisterStringPref(prefs::kPopularSitesOverrideURL,
std::string());
user_prefs->RegisterStringPref(prefs::kPopularSitesOverrideDirectory,
std::string());
user_prefs->RegisterStringPref(prefs::kPopularSitesOverrideCountry,
std::string());
user_prefs->RegisterStringPref(prefs::kPopularSitesOverrideVersion,
std::string());
user_prefs->RegisterInt64Pref(prefs::kPopularSitesLastDownloadPref, 0);
user_prefs->RegisterStringPref(prefs::kPopularSitesURLPref, std::string());
#if BUILDFLAG(IS_IOS)
// Estimate the country code; `GetVariationCountry()` cannot be called because
// `RegisterProfilePrefs()` is static.
std::string country_code_estimate = GetVariationCountry();
if (country_code_estimate.empty()) {
country_code_estimate = GetDeviceCountryCode();
}
std::optional<std::string> country(country_code_estimate);
user_prefs->RegisterListPref(prefs::kPopularSitesJsonPref,
DefaultPopularSites(country));
#else
user_prefs->RegisterListPref(prefs::kPopularSitesJsonPref,
DefaultPopularSites(std::nullopt));
#endif
int version;
base::StringToInt(kPopularSitesDefaultVersion, &version);
user_prefs->RegisterIntegerPref(prefs::kPopularSitesVersionPref, version);
}
void PopularSitesImpl::FetchPopularSites() {
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("popular_sites_fetch", R"(
semantics {
sender: "Popular Sites New Tab Fetch"
description:
"Google Chrome may display a list of regionally-popular web sites "
"on the New Tab Page. This service fetches the list of these sites."
trigger:
"Once per day, unless no popular web sites are required because "
"the New Tab Page is filled with suggestions based on the user's "
"browsing history."
data: "A two letter country code based on the user's location."
destination: GOOGLE_OWNED_SERVICE
}
policy {
cookies_allowed: NO
setting: "This feature cannot be disabled in settings."
policy_exception_justification:
"Not implemented, considered not useful."
})");
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = pending_url_;
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
simple_url_loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), traffic_annotation);
simple_url_loader_->SetRetryOptions(
1, network::SimpleURLLoader::RETRY_ON_NETWORK_CHANGE);
simple_url_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
url_loader_factory_.get(),
base::BindOnce(&PopularSitesImpl::OnSimpleLoaderComplete,
base::Unretained(this)));
}
void PopularSitesImpl::OnSimpleLoaderComplete(
std::unique_ptr<std::string> response_body) {
simple_url_loader_.reset();
if (!response_body) {
OnDownloadFailed();
return;
}
data_decoder::DataDecoder::ParseJsonIsolated(
*response_body, base::BindOnce(&PopularSitesImpl::OnJsonParsed,
weak_ptr_factory_.GetWeakPtr()));
}
void PopularSitesImpl::OnJsonParsed(
data_decoder::DataDecoder::ValueOrError result) {
ASSIGN_OR_RETURN(base::Value list, std::move(result), [&](std::string error) {
DLOG(WARNING) << "JSON parsing failed: " << std::move(error);
OnDownloadFailed();
});
if (!list.is_list()) {
DLOG(WARNING) << "JSON is not a list";
OnDownloadFailed();
return;
}
sections_ = ParseSites(list.GetList(), version_in_pending_url_);
prefs_->SetList(prefs::kPopularSitesJsonPref, std::move(list).TakeList());
prefs_->SetInt64(prefs::kPopularSitesLastDownloadPref,
base::Time::Now().ToInternalValue());
prefs_->SetInteger(prefs::kPopularSitesVersionPref, version_in_pending_url_);
prefs_->SetString(prefs::kPopularSitesURLPref, pending_url_.spec());
std::move(callback_).Run(true);
}
void PopularSitesImpl::OnDownloadFailed() {
if (!is_fallback_) {
DLOG(WARNING) << "Download country site list failed";
is_fallback_ = true;
pending_url_ = GetPopularSitesURL(kPopularSitesDefaultDirectory,
kPopularSitesDefaultCountryCode,
kPopularSitesDefaultVersion);
FetchPopularSites();
} else {
DLOG(WARNING) << "Download fallback site list failed";
std::move(callback_).Run(false);
}
}
} // namespace ntp_tiles
|