| 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.h" | 5 #include "components/ntp_tiles/popular_sites.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 PopularSites::Site::Site(const Site& other) = default; | 169 PopularSites::Site::Site(const Site& other) = default; |
| 170 | 170 |
| 171 PopularSites::Site::~Site() {} | 171 PopularSites::Site::~Site() {} |
| 172 | 172 |
| 173 PopularSites::PopularSites( | 173 PopularSites::PopularSites( |
| 174 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, | 174 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, |
| 175 PrefService* prefs, | 175 PrefService* prefs, |
| 176 const TemplateURLService* template_url_service, | 176 const TemplateURLService* template_url_service, |
| 177 VariationsService* variations_service, | 177 VariationsService* variations_service, |
| 178 net::URLRequestContextGetter* download_context, | 178 net::URLRequestContextGetter* download_context, |
| 179 const base::FilePath& directory, | 179 const base::FilePath& directory) |
| 180 bool force_download, | 180 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( |
| 181 const FinishedCallback& callback) | 181 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), |
| 182 : callback_(callback), | 182 prefs_(prefs), |
| 183 is_fallback_(false), | 183 template_url_service_(template_url_service), |
| 184 variations_(variations_service), |
| 185 download_context_(download_context), |
| 184 local_path_(directory.empty() | 186 local_path_(directory.empty() |
| 185 ? base::FilePath() | 187 ? base::FilePath() |
| 186 : directory.AppendASCII(kPopularSitesLocalFilename)), | 188 : directory.AppendASCII(kPopularSitesLocalFilename)), |
| 187 prefs_(prefs), | 189 is_fallback_(false), |
| 188 download_context_(download_context), | 190 weak_ptr_factory_(this) {} |
| 189 blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( | 191 |
| 190 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), | 192 PopularSites::~PopularSites() {} |
| 191 weak_ptr_factory_(this) { | 193 |
| 194 void PopularSites::StartFetch(bool force_download, |
| 195 const FinishedCallback& callback) { |
| 196 DCHECK(!callback_); |
| 197 callback_ = callback; |
| 198 |
| 192 const base::Time last_download_time = base::Time::FromInternalValue( | 199 const base::Time last_download_time = base::Time::FromInternalValue( |
| 193 prefs_->GetInt64(kPopularSitesLastDownloadPref)); | 200 prefs_->GetInt64(kPopularSitesLastDownloadPref)); |
| 194 const base::TimeDelta time_since_last_download = | 201 const base::TimeDelta time_since_last_download = |
| 195 base::Time::Now() - last_download_time; | 202 base::Time::Now() - last_download_time; |
| 196 const base::TimeDelta redownload_interval = | 203 const base::TimeDelta redownload_interval = |
| 197 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); | 204 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); |
| 198 const bool download_time_is_future = base::Time::Now() < last_download_time; | 205 const bool download_time_is_future = base::Time::Now() < last_download_time; |
| 199 | 206 |
| 200 const std::string country = | 207 const std::string country = |
| 201 GetCountryToUse(prefs, template_url_service, variations_service); | 208 GetCountryToUse(prefs_, template_url_service_, variations_); |
| 202 const std::string version = GetVersionToUse(prefs); | 209 const std::string version = GetVersionToUse(prefs_); |
| 203 | 210 |
| 204 const GURL override_url = | 211 const GURL override_url = |
| 205 GURL(prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); | 212 GURL(prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); |
| 206 pending_url_ = override_url.is_valid() ? override_url | 213 pending_url_ = override_url.is_valid() ? override_url |
| 207 : GetPopularSitesURL(country, version); | 214 : GetPopularSitesURL(country, version); |
| 208 const bool url_changed = | 215 const bool url_changed = |
| 209 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); | 216 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); |
| 210 | 217 |
| 211 // No valid path to save to. Immediately post failure. | 218 // No valid path to save to. Immediately post failure. |
| 212 if (local_path_.empty()) { | 219 if (local_path_.empty()) { |
| 213 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 220 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 214 base::Bind(callback_, false)); | 221 base::Bind(callback_, false)); |
| 215 return; | 222 return; |
| 216 } | 223 } |
| 217 | 224 |
| 218 // Download forced, or we need to download a new file. | 225 // Download forced, or we need to download a new file. |
| 219 if (force_download || download_time_is_future || | 226 if (force_download || download_time_is_future || |
| 220 (time_since_last_download > redownload_interval) || url_changed) { | 227 (time_since_last_download > redownload_interval) || url_changed) { |
| 221 FetchPopularSites(); | 228 FetchPopularSites(); |
| 222 return; | 229 return; |
| 223 } | 230 } |
| 224 | 231 |
| 225 std::unique_ptr<std::string> file_data(new std::string); | 232 std::unique_ptr<std::string> file_data(new std::string); |
| 226 std::string* file_data_ptr = file_data.get(); | 233 std::string* file_data_ptr = file_data.get(); |
| 227 base::PostTaskAndReplyWithResult( | 234 base::PostTaskAndReplyWithResult( |
| 228 blocking_runner_.get(), FROM_HERE, | 235 blocking_runner_.get(), FROM_HERE, |
| 229 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), | 236 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), |
| 230 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), | 237 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), |
| 231 base::Passed(std::move(file_data)))); | 238 base::Passed(std::move(file_data)))); |
| 232 } | 239 } |
| 233 | 240 |
| 234 PopularSites::~PopularSites() {} | |
| 235 | |
| 236 GURL PopularSites::LastURL() const { | 241 GURL PopularSites::LastURL() const { |
| 237 return GURL(prefs_->GetString(kPopularSitesURLPref)); | 242 return GURL(prefs_->GetString(kPopularSitesURLPref)); |
| 238 } | 243 } |
| 239 | 244 |
| 240 // static | 245 // static |
| 241 void PopularSites::RegisterProfilePrefs( | 246 void PopularSites::RegisterProfilePrefs( |
| 242 user_prefs::PrefRegistrySyncable* user_prefs) { | 247 user_prefs::PrefRegistrySyncable* user_prefs) { |
| 243 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, | 248 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| 244 std::string()); | 249 std::string()); |
| 245 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, | 250 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, | 372 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, |
| 368 kPopularSitesDefaultVersion); | 373 kPopularSitesDefaultVersion); |
| 369 FetchPopularSites(); | 374 FetchPopularSites(); |
| 370 } else { | 375 } else { |
| 371 DLOG(WARNING) << "Download fallback site list failed"; | 376 DLOG(WARNING) << "Download fallback site list failed"; |
| 372 callback_.Run(false); | 377 callback_.Run(false); |
| 373 } | 378 } |
| 374 } | 379 } |
| 375 | 380 |
| 376 } // namespace ntp_tiles | 381 } // namespace ntp_tiles |
| OLD | NEW |