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 kPopularSitesDefaultDirectory[] = "chrome/ntp/"; |
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& directory, |
| 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, directory.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 directory = GetDirectoryToFetch(); |
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() |
268 : GetPopularSitesURL(country, version); | 271 ? override_url |
| 272 : GetPopularSitesURL(directory, country, version); |
| 273 } |
| 274 |
| 275 std::string PopularSitesImpl::GetDirectoryToFetch() { |
| 276 std::string directory = |
| 277 prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideDirectory); |
| 278 |
| 279 if (directory.empty()) |
| 280 directory = kPopularSitesDefaultDirectory; |
| 281 |
| 282 return directory; |
269 } | 283 } |
270 | 284 |
271 // Determine the country code to use. In order of precedence: | 285 // Determine the country code to use. In order of precedence: |
272 // - The explicit "override country" pref set by the user. | 286 // - The explicit "override country" pref set by the user. |
273 // - The country code from the field trial config (variation parameter). | 287 // - The country code from the field trial config (variation parameter). |
274 // - The Google country code if Google is the default search engine (and the | 288 // - The Google country code if Google is the default search engine (and the |
275 // "--enable-ntp-search-engine-country-detection" switch is present). | 289 // "--enable-ntp-search-engine-country-detection" switch is present). |
276 // - The country provided by the VariationsService. | 290 // - The country provided by the VariationsService. |
277 // - A default fallback. | 291 // - A default fallback. |
278 std::string PopularSitesImpl::GetCountryToFetch() { | 292 std::string PopularSitesImpl::GetCountryToFetch() { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 | 332 |
319 const base::ListValue* PopularSitesImpl::GetCachedJson() { | 333 const base::ListValue* PopularSitesImpl::GetCachedJson() { |
320 return prefs_->GetList(kPopularSitesJsonPref); | 334 return prefs_->GetList(kPopularSitesJsonPref); |
321 } | 335 } |
322 | 336 |
323 // static | 337 // static |
324 void PopularSitesImpl::RegisterProfilePrefs( | 338 void PopularSitesImpl::RegisterProfilePrefs( |
325 user_prefs::PrefRegistrySyncable* user_prefs) { | 339 user_prefs::PrefRegistrySyncable* user_prefs) { |
326 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, | 340 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, |
327 std::string()); | 341 std::string()); |
| 342 user_prefs->RegisterStringPref( |
| 343 ntp_tiles::prefs::kPopularSitesOverrideDirectory, std::string()); |
328 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, | 344 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, |
329 std::string()); | 345 std::string()); |
330 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, | 346 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, |
331 std::string()); | 347 std::string()); |
332 | 348 |
333 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 349 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
334 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); | 350 user_prefs->RegisterStringPref(kPopularSitesURLPref, std::string()); |
335 user_prefs->RegisterListPref(kPopularSitesJsonPref, DefaultPopularSites()); | 351 user_prefs->RegisterListPref(kPopularSitesJsonPref, DefaultPopularSites()); |
336 } | 352 } |
337 | 353 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 | 422 |
407 void PopularSitesImpl::OnJsonParseFailed(const std::string& error_message) { | 423 void PopularSitesImpl::OnJsonParseFailed(const std::string& error_message) { |
408 DLOG(WARNING) << "JSON parsing failed: " << error_message; | 424 DLOG(WARNING) << "JSON parsing failed: " << error_message; |
409 OnDownloadFailed(); | 425 OnDownloadFailed(); |
410 } | 426 } |
411 | 427 |
412 void PopularSitesImpl::OnDownloadFailed() { | 428 void PopularSitesImpl::OnDownloadFailed() { |
413 if (!is_fallback_) { | 429 if (!is_fallback_) { |
414 DLOG(WARNING) << "Download country site list failed"; | 430 DLOG(WARNING) << "Download country site list failed"; |
415 is_fallback_ = true; | 431 is_fallback_ = true; |
416 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, | 432 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultDirectory, |
| 433 kPopularSitesDefaultCountryCode, |
417 kPopularSitesDefaultVersion); | 434 kPopularSitesDefaultVersion); |
418 FetchPopularSites(); | 435 FetchPopularSites(); |
419 } else { | 436 } else { |
420 DLOG(WARNING) << "Download fallback site list failed"; | 437 DLOG(WARNING) << "Download fallback site list failed"; |
421 callback_.Run(false); | 438 callback_.Run(false); |
422 } | 439 } |
423 } | 440 } |
424 | 441 |
425 } // namespace ntp_tiles | 442 } // namespace ntp_tiles |
OLD | NEW |