| 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 callback_ = callback; |
| 197 |
| 192 const base::Time last_download_time = base::Time::FromInternalValue( | 198 const base::Time last_download_time = base::Time::FromInternalValue( |
| 193 prefs_->GetInt64(kPopularSitesLastDownloadPref)); | 199 prefs_->GetInt64(kPopularSitesLastDownloadPref)); |
| 194 const base::TimeDelta time_since_last_download = | 200 const base::TimeDelta time_since_last_download = |
| 195 base::Time::Now() - last_download_time; | 201 base::Time::Now() - last_download_time; |
| 196 const base::TimeDelta redownload_interval = | 202 const base::TimeDelta redownload_interval = |
| 197 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); | 203 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); |
| 198 const bool download_time_is_future = base::Time::Now() < last_download_time; | 204 const bool download_time_is_future = base::Time::Now() < last_download_time; |
| 199 | 205 |
| 200 const std::string country = | 206 const std::string country = |
| 201 GetCountryToUse(prefs, template_url_service, variations_service); | 207 GetCountryToUse(prefs_, template_url_service_, variations_); |
| 202 const std::string version = GetVersionToUse(prefs); | 208 const std::string version = GetVersionToUse(prefs_); |
| 203 | 209 |
| 204 const GURL override_url = | 210 const GURL override_url = |
| 205 GURL(prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); | 211 GURL(prefs_->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); |
| 206 pending_url_ = override_url.is_valid() ? override_url | 212 pending_url_ = override_url.is_valid() ? override_url |
| 207 : GetPopularSitesURL(country, version); | 213 : GetPopularSitesURL(country, version); |
| 208 const bool url_changed = | 214 const bool url_changed = |
| 209 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); | 215 pending_url_.spec() != prefs_->GetString(kPopularSitesURLPref); |
| 210 | 216 |
| 211 // No valid path to save to. Immediately post failure. | 217 // No valid path to save to. Immediately post failure. |
| 212 if (local_path_.empty()) { | 218 if (local_path_.empty()) { |
| 213 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 219 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 214 base::Bind(callback_, false)); | 220 base::Bind(callback_, false)); |
| 215 return; | 221 return; |
| 216 } | 222 } |
| 217 | 223 |
| 218 // Download forced, or we need to download a new file. | 224 // Download forced, or we need to download a new file. |
| 219 if (force_download || download_time_is_future || | 225 if (force_download || download_time_is_future || |
| 220 (time_since_last_download > redownload_interval) || url_changed) { | 226 (time_since_last_download > redownload_interval) || url_changed) { |
| 221 FetchPopularSites(); | 227 FetchPopularSites(); |
| 222 return; | 228 return; |
| 223 } | 229 } |
| 224 | 230 |
| 225 std::unique_ptr<std::string> file_data(new std::string); | 231 std::unique_ptr<std::string> file_data(new std::string); |
| 226 std::string* file_data_ptr = file_data.get(); | 232 std::string* file_data_ptr = file_data.get(); |
| 227 base::PostTaskAndReplyWithResult( | 233 base::PostTaskAndReplyWithResult( |
| 228 blocking_runner_.get(), FROM_HERE, | 234 blocking_runner_.get(), FROM_HERE, |
| 229 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), | 235 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), |
| 230 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), | 236 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), |
| 231 base::Passed(std::move(file_data)))); | 237 base::Passed(std::move(file_data)))); |
| 232 } | 238 } |
| 233 | 239 |
| 234 PopularSites::~PopularSites() {} | |
| 235 | |
| 236 GURL PopularSites::LastURL() const { | 240 GURL PopularSites::LastURL() const { |
| 237 return GURL(prefs_->GetString(kPopularSitesURLPref)); | 241 return GURL(prefs_->GetString(kPopularSitesURLPref)); |
| 238 } | 242 } |
| 239 | 243 |
| 240 // static | 244 // static |
| 241 void PopularSites::RegisterProfilePrefs( | 245 void PopularSites::RegisterProfilePrefs( |
| 242 user_prefs::PrefRegistrySyncable* user_prefs) { | 246 user_prefs::PrefRegistrySyncable* user_prefs) { |
| 243 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, | 247 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| 244 std::string()); | 248 std::string()); |
| 245 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, | 249 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, | 371 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, |
| 368 kPopularSitesDefaultVersion); | 372 kPopularSitesDefaultVersion); |
| 369 FetchPopularSites(); | 373 FetchPopularSites(); |
| 370 } else { | 374 } else { |
| 371 DLOG(WARNING) << "Download fallback site list failed"; | 375 DLOG(WARNING) << "Download fallback site list failed"; |
| 372 callback_.Run(false); | 376 callback_.Run(false); |
| 373 } | 377 } |
| 374 } | 378 } |
| 375 | 379 |
| 376 } // namespace ntp_tiles | 380 } // namespace ntp_tiles |
| OLD | NEW |