Commit 7df710ad authored by mastiz's avatar mastiz Committed by Commit bot

[Popular Sites] Add Variations parameter to override URL path

This new parameter intends to replace or complement the version override
param to distinguish various published versions of Popular Sites.

BUG=715039

Review-Url: https://codereview.chromium.org/2841643005
Cr-Commit-Position: refs/heads/master@{#467294}
parent e850348a
...@@ -66,6 +66,7 @@ class PopularSites { ...@@ -66,6 +66,7 @@ class PopularSites {
// Various internals exposed publicly for diagnostic pages only. // Various internals exposed publicly for diagnostic pages only.
virtual GURL GetLastURLFetched() const = 0; virtual GURL GetLastURLFetched() const = 0;
virtual GURL GetURLToFetch() = 0; virtual GURL GetURLToFetch() = 0;
virtual std::string GetDirectoryToFetch() = 0;
virtual std::string GetCountryToFetch() = 0; virtual std::string GetCountryToFetch() = 0;
virtual std::string GetVersionToFetch() = 0; virtual std::string GetVersionToFetch() = 0;
virtual const base::ListValue* GetCachedJson() = 0; virtual const base::ListValue* GetCachedJson() = 0;
......
...@@ -52,7 +52,8 @@ namespace ntp_tiles { ...@@ -52,7 +52,8 @@ namespace ntp_tiles {
namespace { namespace {
const char kPopularSitesURLFormat[] = const char kPopularSitesURLFormat[] =
"https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; "https://www.gstatic.com/%ssuggested_sites_%s_%s.json";
const char kPopularSitesDefaultDirectory[] = "chrome/ntp/";
const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; const char kPopularSitesDefaultCountryCode[] = "DEFAULT";
const char kPopularSitesDefaultVersion[] = "5"; const char kPopularSitesDefaultVersion[] = "5";
const int kPopularSitesRedownloadIntervalHours = 24; const int kPopularSitesRedownloadIntervalHours = 24;
...@@ -65,10 +66,11 @@ const char kPopularSitesJsonPref[] = "suggested_sites_json"; ...@@ -65,10 +66,11 @@ const char kPopularSitesJsonPref[] = "suggested_sites_json";
// versions of Chrome, no longer used. Remove after M61. // versions of Chrome, no longer used. Remove after M61.
const char kPopularSitesLocalFilenameToCleanup[] = "suggested_sites.json"; const char kPopularSitesLocalFilenameToCleanup[] = "suggested_sites.json";
GURL GetPopularSitesURL(const std::string& country, GURL GetPopularSitesURL(const std::string& directory,
const std::string& country,
const std::string& version) { const std::string& version) {
return GURL(base::StringPrintf(kPopularSitesURLFormat, country.c_str(), return GURL(base::StringPrintf(kPopularSitesURLFormat, directory.c_str(),
version.c_str())); country.c_str(), version.c_str()));
} }
// Extract the country from the default search engine if the default search // Extract the country from the default search engine if the default search
...@@ -259,13 +261,25 @@ GURL PopularSitesImpl::GetLastURLFetched() const { ...@@ -259,13 +261,25 @@ GURL PopularSitesImpl::GetLastURLFetched() const {
} }
GURL PopularSitesImpl::GetURLToFetch() { GURL PopularSitesImpl::GetURLToFetch() {
const std::string directory = GetDirectoryToFetch();
const std::string country = GetCountryToFetch(); const std::string country = GetCountryToFetch();
const std::string version = GetVersionToFetch(); const std::string version = GetVersionToFetch();
const GURL override_url = const GURL override_url =
GURL(prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); GURL(prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL));
return override_url.is_valid() ? override_url return override_url.is_valid()
: GetPopularSitesURL(country, version); ? override_url
: GetPopularSitesURL(directory, country, version);
}
std::string PopularSitesImpl::GetDirectoryToFetch() {
std::string directory =
prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideDirectory);
if (directory.empty())
directory = kPopularSitesDefaultDirectory;
return directory;
} }
// Determine the country code to use. In order of precedence: // Determine the country code to use. In order of precedence:
...@@ -325,6 +339,8 @@ void PopularSitesImpl::RegisterProfilePrefs( ...@@ -325,6 +339,8 @@ void PopularSitesImpl::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* user_prefs) { user_prefs::PrefRegistrySyncable* user_prefs) {
user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL,
std::string()); std::string());
user_prefs->RegisterStringPref(
ntp_tiles::prefs::kPopularSitesOverrideDirectory, std::string());
user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry,
std::string()); std::string());
user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion,
...@@ -413,7 +429,8 @@ void PopularSitesImpl::OnDownloadFailed() { ...@@ -413,7 +429,8 @@ void PopularSitesImpl::OnDownloadFailed() {
if (!is_fallback_) { if (!is_fallback_) {
DLOG(WARNING) << "Download country site list failed"; DLOG(WARNING) << "Download country site list failed";
is_fallback_ = true; is_fallback_ = true;
pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, pending_url_ = GetPopularSitesURL(kPopularSitesDefaultDirectory,
kPopularSitesDefaultCountryCode,
kPopularSitesDefaultVersion); kPopularSitesDefaultVersion);
FetchPopularSites(); FetchPopularSites();
} else { } else {
......
...@@ -67,6 +67,7 @@ class PopularSitesImpl : public PopularSites, public net::URLFetcherDelegate { ...@@ -67,6 +67,7 @@ class PopularSitesImpl : public PopularSites, public net::URLFetcherDelegate {
const SitesVector& sites() const override; const SitesVector& sites() const override;
GURL GetLastURLFetched() const override; GURL GetLastURLFetched() const override;
GURL GetURLToFetch() override; GURL GetURLToFetch() override;
std::string GetDirectoryToFetch() override;
std::string GetCountryToFetch() override; std::string GetCountryToFetch() override;
std::string GetVersionToFetch() override; std::string GetVersionToFetch() override;
const base::ListValue* GetCachedJson() override; const base::ListValue* GetCachedJson() override;
......
...@@ -458,5 +458,18 @@ TEST_F(PopularSitesTest, RefetchesAfterFallback) { ...@@ -458,5 +458,18 @@ TEST_F(PopularSitesTest, RefetchesAfterFallback) {
EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/")); EXPECT_THAT(sites[0].url, URLEq("https://www.chromium.org/"));
} }
TEST_F(PopularSitesTest, ShouldOverrideDirectory) {
SetCountryAndVersion("ZZ", "9");
prefs_->SetString(prefs::kPopularSitesOverrideDirectory, "foo/bar/");
RespondWithJSON("https://www.gstatic.com/foo/bar/suggested_sites_ZZ_9.json",
{kWikipedia});
PopularSites::SitesVector sites;
EXPECT_THAT(FetchPopularSites(/*force_download=*/false, &sites),
Eq(base::Optional<bool>(true)));
EXPECT_THAT(sites.size(), Eq(1u));
}
} // namespace } // namespace
} // namespace ntp_tiles } // namespace ntp_tiles
...@@ -15,6 +15,10 @@ const char kNumPersonalTiles[] = "ntp.num_personal_suggestions"; ...@@ -15,6 +15,10 @@ const char kNumPersonalTiles[] = "ntp.num_personal_suggestions";
// overrides for country and version below. // overrides for country and version below.
const char kPopularSitesOverrideURL[] = "popular_sites.override_url"; const char kPopularSitesOverrideURL[] = "popular_sites.override_url";
// If set, this will override the URL path directory for popular sites.
const char kPopularSitesOverrideDirectory[] =
"popular_sites.override_directory";
// If set, this will override the country detection for popular sites. // If set, this will override the country detection for popular sites.
const char kPopularSitesOverrideCountry[] = "popular_sites.override_country"; const char kPopularSitesOverrideCountry[] = "popular_sites.override_country";
......
...@@ -11,6 +11,7 @@ namespace prefs { ...@@ -11,6 +11,7 @@ namespace prefs {
extern const char kNumPersonalTiles[]; extern const char kNumPersonalTiles[];
extern const char kPopularSitesOverrideURL[]; extern const char kPopularSitesOverrideURL[];
extern const char kPopularSitesOverrideDirectory[];
extern const char kPopularSitesOverrideCountry[]; extern const char kPopularSitesOverrideCountry[];
extern const char kPopularSitesOverrideVersion[]; extern const char kPopularSitesOverrideVersion[];
......
...@@ -110,6 +110,15 @@ void NTPTilesInternalsMessageHandler::HandleUpdate( ...@@ -110,6 +110,15 @@ void NTPTilesInternalsMessageHandler::HandleUpdate(
url_formatter::FixupURL(url, std::string()).spec()); url_formatter::FixupURL(url, std::string()).spec());
} }
std::string directory;
dict->GetString("popular.overrideDirectory", &directory);
if (directory.empty()) {
prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideDirectory);
} else {
prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideDirectory,
directory);
}
std::string country; std::string country;
dict->GetString("popular.overrideCountry", &country); dict->GetString("popular.overrideCountry", &country);
if (country.empty()) { if (country.empty()) {
...@@ -181,12 +190,16 @@ void NTPTilesInternalsMessageHandler::SendSourceInfo() { ...@@ -181,12 +190,16 @@ void NTPTilesInternalsMessageHandler::SendSourceInfo() {
if (most_visited_sites_->DoesSourceExist(TileSource::POPULAR)) { if (most_visited_sites_->DoesSourceExist(TileSource::POPULAR)) {
auto* popular_sites = most_visited_sites_->popular_sites(); auto* popular_sites = most_visited_sites_->popular_sites();
value.SetString("popular.url", popular_sites->GetURLToFetch().spec()); value.SetString("popular.url", popular_sites->GetURLToFetch().spec());
value.SetString("popular.directory", popular_sites->GetDirectoryToFetch());
value.SetString("popular.country", popular_sites->GetCountryToFetch()); value.SetString("popular.country", popular_sites->GetCountryToFetch());
value.SetString("popular.version", popular_sites->GetVersionToFetch()); value.SetString("popular.version", popular_sites->GetVersionToFetch());
value.SetString( value.SetString(
"popular.overrideURL", "popular.overrideURL",
prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL));
value.SetString(
"popular.overrideDirectory",
prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideDirectory));
value.SetString( value.SetString(
"popular.overrideCountry", "popular.overrideCountry",
prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry)); prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry));
......
...@@ -59,7 +59,7 @@ void PopularSitesInternalsMessageHandler::HandleRegisterForEvents( ...@@ -59,7 +59,7 @@ void PopularSitesInternalsMessageHandler::HandleRegisterForEvents(
void PopularSitesInternalsMessageHandler::HandleUpdate( void PopularSitesInternalsMessageHandler::HandleUpdate(
const base::ListValue* args) { const base::ListValue* args) {
DCHECK_EQ(3u, args->GetSize()); DCHECK_EQ(4u, args->GetSize());
PrefService* prefs = web_ui_->GetPrefs(); PrefService* prefs = web_ui_->GetPrefs();
...@@ -71,15 +71,23 @@ void PopularSitesInternalsMessageHandler::HandleUpdate( ...@@ -71,15 +71,23 @@ void PopularSitesInternalsMessageHandler::HandleUpdate(
prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL,
url_formatter::FixupURL(url, std::string()).spec()); url_formatter::FixupURL(url, std::string()).spec());
std::string directory;
args->GetString(1, &directory);
if (directory.empty())
prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideDirectory);
else
prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideDirectory,
directory);
std::string country; std::string country;
args->GetString(1, &country); args->GetString(2, &country);
if (country.empty()) if (country.empty())
prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry);
else else
prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country);
std::string version; std::string version;
args->GetString(2, &version); args->GetString(3, &version);
if (version.empty()) if (version.empty())
prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion);
else else
...@@ -109,13 +117,15 @@ void PopularSitesInternalsMessageHandler::SendOverrides() { ...@@ -109,13 +117,15 @@ void PopularSitesInternalsMessageHandler::SendOverrides() {
PrefService* prefs = web_ui_->GetPrefs(); PrefService* prefs = web_ui_->GetPrefs();
std::string url = std::string url =
prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL); prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL);
std::string directory =
prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideDirectory);
std::string country = std::string country =
prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry);
std::string version = std::string version =
prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion);
web_ui_->CallJavascriptFunction( web_ui_->CallJavascriptFunction(
"chrome.popular_sites_internals.receiveOverrides", base::Value(url), "chrome.popular_sites_internals.receiveOverrides", base::Value(url),
base::Value(country), base::Value(version)); base::Value(directory), base::Value(country), base::Value(version));
} }
void PopularSitesInternalsMessageHandler::SendDownloadResult(bool success) { void PopularSitesInternalsMessageHandler::SendDownloadResult(bool success) {
......
...@@ -63,6 +63,10 @@ found in the LICENSE file. ...@@ -63,6 +63,10 @@ found in the LICENSE file.
<td class="detail">URL</td> <td class="detail">URL</td>
<td class="value"><input id="override-url" type="text" jsvalues="value:overrideURL;placeholder:url"></td> <td class="value"><input id="override-url" type="text" jsvalues="value:overrideURL;placeholder:url"></td>
</tr> </tr>
<tr jsdisplay="$this">
<td class="detail">Country</td>
<td class="value"><input id="override-directory" type="text" jsvalues="value:overrideDirectory;placeholder:directory"></td>
</tr>
<tr jsdisplay="$this"> <tr jsdisplay="$this">
<td class="detail">Country</td> <td class="detail">Country</td>
<td class="value"><input id="override-country" type="text" jsvalues="value:overrideCountry;placeholder:country"></td> <td class="value"><input id="override-country" type="text" jsvalues="value:overrideCountry;placeholder:country"></td>
...@@ -125,4 +129,3 @@ found in the LICENSE file. ...@@ -125,4 +129,3 @@ found in the LICENSE file.
</body> </body>
</html> </html>
...@@ -11,6 +11,7 @@ cr.define('chrome.ntp_tiles_internals', function() { ...@@ -11,6 +11,7 @@ cr.define('chrome.ntp_tiles_internals', function() {
chrome.send('update', [{ chrome.send('update', [{
"popular": { "popular": {
"overrideURL": $('override-url').value, "overrideURL": $('override-url').value,
"overrideDirectory": $('override-directory').value,
"overrideCountry": $('override-country').value, "overrideCountry": $('override-country').value,
"overrideVersion": $('override-version').value, "overrideVersion": $('override-version').value,
}, },
...@@ -52,4 +53,3 @@ cr.define('chrome.ntp_tiles_internals', function() { ...@@ -52,4 +53,3 @@ cr.define('chrome.ntp_tiles_internals', function() {
document.addEventListener('DOMContentLoaded', document.addEventListener('DOMContentLoaded',
chrome.ntp_tiles_internals.initialize); chrome.ntp_tiles_internals.initialize);
...@@ -31,9 +31,13 @@ found in the LICENSE file. ...@@ -31,9 +31,13 @@ found in the LICENSE file.
<h2>Download</h2> <h2>Download</h2>
<table class="section-details"> <table class="section-details">
<tr> <tr>
<td class="detail">URL (takes precedence over Country and Version)</td> <td class="detail">URL (takes precedence over Directory, Country and Version)</td>
<td class="value"><input id="override-url" type="text"></td> <td class="value"><input id="override-url" type="text"></td>
</tr> </tr>
<tr>
<td class="detail">Override Directory</td>
<td class="value"><input id="override-directory" type="text"></td>
</tr>
<tr> <tr>
<td class="detail">Override Country</td> <td class="detail">Override Country</td>
<td class="value"><input id="override-country" type="text"></td> <td class="value"><input id="override-country" type="text"></td>
...@@ -80,4 +84,3 @@ found in the LICENSE file. ...@@ -80,4 +84,3 @@ found in the LICENSE file.
</body> </body>
</html> </html>
...@@ -9,6 +9,7 @@ cr.define('chrome.popular_sites_internals', function() { ...@@ -9,6 +9,7 @@ cr.define('chrome.popular_sites_internals', function() {
function submitUpdate(event) { function submitUpdate(event) {
$('download-result').textContent = ''; $('download-result').textContent = '';
chrome.send('update', [$('override-url').value, chrome.send('update', [$('override-url').value,
$('override-directory').value,
$('override-country').value, $('override-country').value,
$('override-version').value]); $('override-version').value]);
event.preventDefault(); event.preventDefault();
...@@ -27,8 +28,9 @@ cr.define('chrome.popular_sites_internals', function() { ...@@ -27,8 +28,9 @@ cr.define('chrome.popular_sites_internals', function() {
chrome.send('registerForEvents'); chrome.send('registerForEvents');
} }
function receiveOverrides(url, country, version) { function receiveOverrides(url, directory, country, version) {
$('override-url').value = url; $('override-url').value = url;
$('override-directory').value = directory;
$('override-country').value = country; $('override-country').value = country;
$('override-version').value = version; $('override-version').value = version;
} }
...@@ -59,4 +61,3 @@ cr.define('chrome.popular_sites_internals', function() { ...@@ -59,4 +61,3 @@ cr.define('chrome.popular_sites_internals', function() {
document.addEventListener('DOMContentLoaded', document.addEventListener('DOMContentLoaded',
chrome.popular_sites_internals.initialize); chrome.popular_sites_internals.initialize);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment