| 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" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "base/threading/sequenced_worker_pool.h" | |
| 19 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 20 #include "base/values.h" | 19 #include "base/values.h" |
| 21 #include "components/data_use_measurement/core/data_use_user_data.h" | 20 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 22 #include "components/google/core/browser/google_util.h" | 21 #include "components/google/core/browser/google_util.h" |
| 23 #include "components/ntp_tiles/constants.h" | 22 #include "components/ntp_tiles/constants.h" |
| 24 #include "components/ntp_tiles/field_trial.h" | 23 #include "components/ntp_tiles/field_trial.h" |
| 25 #include "components/ntp_tiles/pref_names.h" | 24 #include "components/ntp_tiles/pref_names.h" |
| 26 #include "components/ntp_tiles/switches.h" | 25 #include "components/ntp_tiles/switches.h" |
| 27 #include "components/pref_registry/pref_registry_syncable.h" | 26 #include "components/pref_registry/pref_registry_syncable.h" |
| 28 #include "components/prefs/pref_service.h" | 27 #include "components/prefs/pref_service.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 55 "https://www.gstatic.com/%ssuggested_sites_%s_%s.json"; | 54 "https://www.gstatic.com/%ssuggested_sites_%s_%s.json"; |
| 56 const char kPopularSitesDefaultDirectory[] = "chrome/ntp/"; | 55 const char kPopularSitesDefaultDirectory[] = "chrome/ntp/"; |
| 57 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; | 56 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; |
| 58 const char kPopularSitesDefaultVersion[] = "5"; | 57 const char kPopularSitesDefaultVersion[] = "5"; |
| 59 const int kPopularSitesRedownloadIntervalHours = 24; | 58 const int kPopularSitesRedownloadIntervalHours = 24; |
| 60 | 59 |
| 61 const char kPopularSitesLastDownloadPref[] = "popular_sites_last_download"; | 60 const char kPopularSitesLastDownloadPref[] = "popular_sites_last_download"; |
| 62 const char kPopularSitesURLPref[] = "popular_sites_url"; | 61 const char kPopularSitesURLPref[] = "popular_sites_url"; |
| 63 const char kPopularSitesJsonPref[] = "suggested_sites_json"; | 62 const char kPopularSitesJsonPref[] = "suggested_sites_json"; |
| 64 | 63 |
| 65 // TODO(crbug.com/683890): This refers to a local cache stored by older | |
| 66 // versions of Chrome, no longer used. Remove after M61. | |
| 67 const char kPopularSitesLocalFilenameToCleanup[] = "suggested_sites.json"; | |
| 68 | |
| 69 GURL GetPopularSitesURL(const std::string& directory, | 64 GURL GetPopularSitesURL(const std::string& directory, |
| 70 const std::string& country, | 65 const std::string& country, |
| 71 const std::string& version) { | 66 const std::string& version) { |
| 72 return GURL(base::StringPrintf(kPopularSitesURLFormat, directory.c_str(), | 67 return GURL(base::StringPrintf(kPopularSitesURLFormat, directory.c_str(), |
| 73 country.c_str(), version.c_str())); | 68 country.c_str(), version.c_str())); |
| 74 } | 69 } |
| 75 | 70 |
| 76 // Extract the country from the default search engine if the default search | 71 // Extract the country from the default search engine if the default search |
| 77 // engine is Google. | 72 // engine is Google. |
| 78 std::string GetDefaultSearchEngineCountryCode( | 73 std::string GetDefaultSearchEngineCountryCode( |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 favicon_url(favicon_url), | 185 favicon_url(favicon_url), |
| 191 large_icon_url(large_icon_url), | 186 large_icon_url(large_icon_url), |
| 192 thumbnail_url(thumbnail_url), | 187 thumbnail_url(thumbnail_url), |
| 193 default_icon_resource(-1) {} | 188 default_icon_resource(-1) {} |
| 194 | 189 |
| 195 PopularSites::Site::Site(const Site& other) = default; | 190 PopularSites::Site::Site(const Site& other) = default; |
| 196 | 191 |
| 197 PopularSites::Site::~Site() {} | 192 PopularSites::Site::~Site() {} |
| 198 | 193 |
| 199 PopularSitesImpl::PopularSitesImpl( | 194 PopularSitesImpl::PopularSitesImpl( |
| 200 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, | |
| 201 PrefService* prefs, | 195 PrefService* prefs, |
| 202 const TemplateURLService* template_url_service, | 196 const TemplateURLService* template_url_service, |
| 203 VariationsService* variations_service, | 197 VariationsService* variations_service, |
| 204 net::URLRequestContextGetter* download_context, | 198 net::URLRequestContextGetter* download_context, |
| 205 const base::FilePath& directory, | |
| 206 ParseJSONCallback parse_json) | 199 ParseJSONCallback parse_json) |
| 207 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( | 200 : prefs_(prefs), |
| 208 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), | |
| 209 prefs_(prefs), | |
| 210 template_url_service_(template_url_service), | 201 template_url_service_(template_url_service), |
| 211 variations_(variations_service), | 202 variations_(variations_service), |
| 212 download_context_(download_context), | 203 download_context_(download_context), |
| 213 parse_json_(std::move(parse_json)), | 204 parse_json_(std::move(parse_json)), |
| 214 is_fallback_(false), | 205 is_fallback_(false), |
| 215 sites_(ParseSiteList(*prefs->GetList(kPopularSitesJsonPref))), | 206 sites_(ParseSiteList(*prefs->GetList(kPopularSitesJsonPref))), |
| 216 weak_ptr_factory_(this) { | 207 weak_ptr_factory_(this) {} |
| 217 // If valid path provided, remove local files created by older versions. | |
| 218 if (!directory.empty() && blocking_runner_) { | |
| 219 blocking_runner_->PostTask( | |
| 220 FROM_HERE, | |
| 221 base::Bind(base::IgnoreResult(&base::DeleteFile), | |
| 222 directory.AppendASCII(kPopularSitesLocalFilenameToCleanup), | |
| 223 /*recursive=*/false)); | |
| 224 } | |
| 225 } | |
| 226 | 208 |
| 227 PopularSitesImpl::~PopularSitesImpl() {} | 209 PopularSitesImpl::~PopularSitesImpl() {} |
| 228 | 210 |
| 229 bool PopularSitesImpl::MaybeStartFetch(bool force_download, | 211 bool PopularSitesImpl::MaybeStartFetch(bool force_download, |
| 230 const FinishedCallback& callback) { | 212 const FinishedCallback& callback) { |
| 231 DCHECK(!callback_); | 213 DCHECK(!callback_); |
| 232 callback_ = callback; | 214 callback_ = callback; |
| 233 | 215 |
| 234 const base::Time last_download_time = base::Time::FromInternalValue( | 216 const base::Time last_download_time = base::Time::FromInternalValue( |
| 235 prefs_->GetInt64(kPopularSitesLastDownloadPref)); | 217 prefs_->GetInt64(kPopularSitesLastDownloadPref)); |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 kPopularSitesDefaultCountryCode, | 415 kPopularSitesDefaultCountryCode, |
| 434 kPopularSitesDefaultVersion); | 416 kPopularSitesDefaultVersion); |
| 435 FetchPopularSites(); | 417 FetchPopularSites(); |
| 436 } else { | 418 } else { |
| 437 DLOG(WARNING) << "Download fallback site list failed"; | 419 DLOG(WARNING) << "Download fallback site list failed"; |
| 438 callback_.Run(false); | 420 callback_.Run(false); |
| 439 } | 421 } |
| 440 } | 422 } |
| 441 | 423 |
| 442 } // namespace ntp_tiles | 424 } // namespace ntp_tiles |
| OLD | NEW |