Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_tiles/popular_sites_impl.h" | 5 #include "components/ntp_tiles/popular_sites_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 #endif | 45 #endif |
| 46 | 46 |
| 47 using net::URLFetcher; | 47 using net::URLFetcher; |
| 48 using variations::VariationsService; | 48 using variations::VariationsService; |
| 49 | 49 |
| 50 namespace ntp_tiles { | 50 namespace ntp_tiles { |
| 51 | 51 |
| 52 namespace { | 52 namespace { |
| 53 | 53 |
| 54 const char kPopularSitesURLFormat[] = | 54 const char kPopularSitesURLFormat[] = |
| 55 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; | 55 "https://www.gstatic.com/%ssuggested_sites_%s_%s.json"; |
| 56 const char kPopularSitesDefaultPath[] = "chrome/ntp/"; | |
|
sfiera
2017/04/25 13:35:42
This isn't the [whole] path, it's the directory.
mastiz
2017/04/25 17:54:00
Thanks, renamed path->directory.
| |
| 56 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; | 57 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; |
| 57 const char kPopularSitesDefaultVersion[] = "5"; | 58 const char kPopularSitesDefaultVersion[] = "5"; |
| 58 const int kPopularSitesRedownloadIntervalHours = 24; | 59 const int kPopularSitesRedownloadIntervalHours = 24; |
| 59 | 60 |
| 60 const char kPopularSitesLastDownloadPref[] = "popular_sites_last_download"; | 61 const char kPopularSitesLastDownloadPref[] = "popular_sites_last_download"; |
| 61 const char kPopularSitesURLPref[] = "popular_sites_url"; | 62 const char kPopularSitesURLPref[] = "popular_sites_url"; |
| 62 const char kPopularSitesJsonPref[] = "suggested_sites_json"; | 63 const char kPopularSitesJsonPref[] = "suggested_sites_json"; |
| 63 | 64 |
| 64 // TODO(crbug.com/683890): This refers to a local cache stored by older | 65 // TODO(crbug.com/683890): This refers to a local cache stored by older |
| 65 // versions of Chrome, no longer used. Remove after M61. | 66 // versions of Chrome, no longer used. Remove after M61. |
| 66 const char kPopularSitesLocalFilenameToCleanup[] = "suggested_sites.json"; | 67 const char kPopularSitesLocalFilenameToCleanup[] = "suggested_sites.json"; |
| 67 | 68 |
| 68 GURL GetPopularSitesURL(const std::string& country, | 69 GURL GetPopularSitesURL(const std::string& path, |
| 70 const std::string& country, | |
| 69 const std::string& version) { | 71 const std::string& version) { |
| 70 return GURL(base::StringPrintf(kPopularSitesURLFormat, country.c_str(), | 72 return GURL(base::StringPrintf(kPopularSitesURLFormat, path.c_str(), |
| 71 version.c_str())); | 73 country.c_str(), version.c_str())); |
| 72 } | 74 } |
| 73 | 75 |
| 74 // Extract the country from the default search engine if the default search | 76 // Extract the country from the default search engine if the default search |
| 75 // engine is Google. | 77 // engine is Google. |
| 76 std::string GetDefaultSearchEngineCountryCode( | 78 std::string GetDefaultSearchEngineCountryCode( |
| 77 const TemplateURLService* template_url_service) { | 79 const TemplateURLService* template_url_service) { |
| 78 DCHECK(template_url_service); | 80 DCHECK(template_url_service); |
| 79 | 81 |
| 80 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 82 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 81 if (!cmd_line->HasSwitch( | 83 if (!cmd_line->HasSwitch( |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 | 254 |
| 253 const PopularSites::SitesVector& PopularSitesImpl::sites() const { | 255 const PopularSites::SitesVector& PopularSitesImpl::sites() const { |
| 254 return sites_; | 256 return sites_; |
| 255 } | 257 } |
| 256 | 258 |
| 257 GURL PopularSitesImpl::GetLastURLFetched() const { | 259 GURL PopularSitesImpl::GetLastURLFetched() const { |
| 258 return GURL(prefs_->GetString(kPopularSitesURLPref)); | 260 return GURL(prefs_->GetString(kPopularSitesURLPref)); |
| 259 } | 261 } |
| 260 | 262 |
| 261 GURL PopularSitesImpl::GetURLToFetch() { | 263 GURL PopularSitesImpl::GetURLToFetch() { |
| 264 const std::string path = GetPathToFetch(); | |
| 262 const std::string country = GetCountryToFetch(); | 265 const std::string country = GetCountryToFetch(); |
| 263 const std::string version = GetVersionToFetch(); | 266 const std::string version = GetVersionToFetch(); |
| 264 | 267 |
| 265 const GURL override_url = | 268 const GURL override_url = |
| 266 GURL(prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); | 269 GURL(prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); |
| 267 return override_url.is_valid() ? override_url | 270 return override_url.is_valid() ? override_url |
| 268 : GetPopularSitesURL(country, version); | 271 : GetPopularSitesURL(path, country, version); |
| 272 } | |
| 273 | |
| 274 std::string PopularSitesImpl::GetPathToFetch() { | |
| 275 std::string path = | |
| 276 prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverridePath); | |
| 277 | |
| 278 if (path.empty()) | |
| 279 path = kPopularSitesDefaultPath; | |
| 280 | |
| 281 return path; | |
| 269 } | 282 } |
| 270 | 283 |
| 271 // Determine the country code to use. In order of precedence: | 284 // Determine the country code to use. In order of precedence: |
| 272 // - The explicit "override country" pref set by the user. | 285 // - The explicit "override country" pref set by the user. |
| 273 // - The country code from the field trial config (variation parameter). | 286 // - The country code from the field trial config (variation parameter). |
| 274 // - The Google country code if Google is the default search engine (and the | 287 // - The Google country code if Google is the default search engine (and the |
| 275 // "--enable-ntp-search-engine-country-detection" switch is present). | 288 // "--enable-ntp-search-engine-country-detection" switch is present). |
| 276 // - The country provided by the VariationsService. | 289 // - The country provided by the VariationsService. |
| 277 // - A default fallback. | 290 // - A default fallback. |
| 278 std::string PopularSitesImpl::GetCountryToFetch() { | 291 std::string PopularSitesImpl::GetCountryToFetch() { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 | 331 |
| 319 const base::ListValue* PopularSitesImpl::GetCachedJson() { | 332 const base::ListValue* PopularSitesImpl::GetCachedJson() { |
| 320 return prefs_->GetList(kPopularSitesJsonPref); | 333 return prefs_->GetList(kPopularSitesJsonPref); |
| 321 } | 334 } |
| 322 | 335 |
| 323 // static | 336 // static |
| 324 void PopularSitesImpl::RegisterProfilePrefs( | 337 void PopularSitesImpl::RegisterProfilePrefs( |
| 325 user_prefs::PrefRegistrySyncable* user_prefs) { | 338 user_prefs::PrefRegistrySyncable* user_prefs) { |
| 326 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, | 339 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| 327 std::string()); | 340 std::string()); |
| 341 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverridePath, | |
| 342 std::string()); | |
| 328 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, | 343 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, |
| 329 std::string()); | 344 std::string()); |
| 330 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, | 345 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, |
| 331 std::string()); | 346 std::string()); |
| 332 | 347 |
| 333 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 348 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
| 334 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); | 349 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); |
| 335 user_prefs->RegisterListPref(kPopularSitesJsonPref, DefaultPopularSites()); | 350 user_prefs->RegisterListPref(kPopularSitesJsonPref, DefaultPopularSites()); |
| 336 } | 351 } |
| 337 | 352 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 | 421 |
| 407 void PopularSitesImpl::OnJsonParseFailed(const std::string& error_message) { | 422 void PopularSitesImpl::OnJsonParseFailed(const std::string& error_message) { |
| 408 DLOG(WARNING) << "JSON parsing failed: " << error_message; | 423 DLOG(WARNING) << "JSON parsing failed: " << error_message; |
| 409 OnDownloadFailed(); | 424 OnDownloadFailed(); |
| 410 } | 425 } |
| 411 | 426 |
| 412 void PopularSitesImpl::OnDownloadFailed() { | 427 void PopularSitesImpl::OnDownloadFailed() { |
| 413 if (!is_fallback_) { | 428 if (!is_fallback_) { |
| 414 DLOG(WARNING) << "Download country site list failed"; | 429 DLOG(WARNING) << "Download country site list failed"; |
| 415 is_fallback_ = true; | 430 is_fallback_ = true; |
| 416 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, | 431 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultPath, |
| 432 kPopularSitesDefaultCountryCode, | |
| 417 kPopularSitesDefaultVersion); | 433 kPopularSitesDefaultVersion); |
| 418 FetchPopularSites(); | 434 FetchPopularSites(); |
| 419 } else { | 435 } else { |
| 420 DLOG(WARNING) << "Download fallback site list failed"; | 436 DLOG(WARNING) << "Download fallback site list failed"; |
| 421 callback_.Run(false); | 437 callback_.Run(false); |
| 422 } | 438 } |
| 423 } | 439 } |
| 424 | 440 |
| 425 } // namespace ntp_tiles | 441 } // namespace ntp_tiles |
| OLD | NEW |