| 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 "chrome/browser/android/ntp/popular_sites.h" | 5 #include "chrome/browser/android/ntp/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 11 matching lines...) Expand all Loading... |
| 22 #include "components/google/core/browser/google_util.h" | 22 #include "components/google/core/browser/google_util.h" |
| 23 #include "components/ntp_tiles/pref_names.h" | 23 #include "components/ntp_tiles/pref_names.h" |
| 24 #include "components/ntp_tiles/switches.h" | 24 #include "components/ntp_tiles/switches.h" |
| 25 #include "components/pref_registry/pref_registry_syncable.h" | 25 #include "components/pref_registry/pref_registry_syncable.h" |
| 26 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
| 27 #include "components/safe_json/json_sanitizer.h" | 27 #include "components/safe_json/json_sanitizer.h" |
| 28 #include "components/search_engines/search_engine_type.h" | 28 #include "components/search_engines/search_engine_type.h" |
| 29 #include "components/search_engines/template_url_prepopulate_data.h" | 29 #include "components/search_engines/template_url_prepopulate_data.h" |
| 30 #include "components/search_engines/template_url_service.h" | 30 #include "components/search_engines/template_url_service.h" |
| 31 #include "components/variations/service/variations_service.h" | 31 #include "components/variations/service/variations_service.h" |
| 32 #include "content/public/browser/browser_thread.h" | |
| 33 #include "net/base/load_flags.h" | 32 #include "net/base/load_flags.h" |
| 34 #include "net/http/http_status_code.h" | 33 #include "net/http/http_status_code.h" |
| 35 | 34 |
| 36 using content::BrowserThread; | |
| 37 using net::URLFetcher; | 35 using net::URLFetcher; |
| 38 using variations::VariationsService; | 36 using variations::VariationsService; |
| 39 | 37 |
| 40 namespace { | 38 namespace { |
| 41 | 39 |
| 42 const char kPopularSitesURLFormat[] = | 40 const char kPopularSitesURLFormat[] = |
| 43 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; | 41 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; |
| 44 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; | 42 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; |
| 45 const char kPopularSitesDefaultVersion[] = "5"; | 43 const char kPopularSitesDefaultVersion[] = "5"; |
| 46 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; | 44 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 : title(title), | 177 : title(title), |
| 180 url(url), | 178 url(url), |
| 181 favicon_url(favicon_url), | 179 favicon_url(favicon_url), |
| 182 large_icon_url(large_icon_url), | 180 large_icon_url(large_icon_url), |
| 183 thumbnail_url(thumbnail_url) {} | 181 thumbnail_url(thumbnail_url) {} |
| 184 | 182 |
| 185 PopularSites::Site::Site(const Site& other) = default; | 183 PopularSites::Site::Site(const Site& other) = default; |
| 186 | 184 |
| 187 PopularSites::Site::~Site() {} | 185 PopularSites::Site::~Site() {} |
| 188 | 186 |
| 189 PopularSites::PopularSites(PrefService* prefs, | 187 PopularSites::PopularSites( |
| 190 const TemplateURLService* template_url_service, | 188 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, |
| 191 VariationsService* variations_service, | 189 PrefService* prefs, |
| 192 net::URLRequestContextGetter* download_context, | 190 const TemplateURLService* template_url_service, |
| 193 const base::FilePath& directory, | 191 VariationsService* variations_service, |
| 194 const std::string& variation_param_country, | 192 net::URLRequestContextGetter* download_context, |
| 195 const std::string& variation_param_version, | 193 const base::FilePath& directory, |
| 196 bool force_download, | 194 const std::string& variation_param_country, |
| 197 const FinishedCallback& callback) | 195 const std::string& variation_param_version, |
| 198 : PopularSites(prefs, | 196 bool force_download, |
| 197 const FinishedCallback& callback) |
| 198 : PopularSites(blocking_pool, |
| 199 prefs, |
| 199 download_context, | 200 download_context, |
| 200 directory, | 201 directory, |
| 201 GetCountryToUse(prefs, | 202 GetCountryToUse(prefs, |
| 202 template_url_service, | 203 template_url_service, |
| 203 variations_service, | 204 variations_service, |
| 204 variation_param_country), | 205 variation_param_country), |
| 205 GetVersionToUse(prefs, variation_param_version), | 206 GetVersionToUse(prefs, variation_param_version), |
| 206 GURL(), | 207 GURL(), |
| 207 force_download, | 208 force_download, |
| 208 callback) {} | 209 callback) {} |
| 209 | 210 |
| 210 PopularSites::PopularSites(PrefService* prefs, | 211 PopularSites::PopularSites( |
| 211 net::URLRequestContextGetter* download_context, | 212 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, |
| 212 const base::FilePath& directory, | 213 PrefService* prefs, |
| 213 const GURL& url, | 214 net::URLRequestContextGetter* download_context, |
| 214 const FinishedCallback& callback) | 215 const base::FilePath& directory, |
| 215 : PopularSites(prefs, | 216 const GURL& url, |
| 217 const FinishedCallback& callback) |
| 218 : PopularSites(blocking_pool, |
| 219 prefs, |
| 216 download_context, | 220 download_context, |
| 217 directory, | 221 directory, |
| 218 std::string(), | 222 std::string(), |
| 219 std::string(), | 223 std::string(), |
| 220 url, | 224 url, |
| 221 true, | 225 true, |
| 222 callback) {} | 226 callback) {} |
| 223 | 227 |
| 224 PopularSites::~PopularSites() {} | 228 PopularSites::~PopularSites() {} |
| 225 | 229 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 237 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, | 241 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, |
| 238 std::string()); | 242 std::string()); |
| 239 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, | 243 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, |
| 240 std::string()); | 244 std::string()); |
| 241 | 245 |
| 242 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 246 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
| 243 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); | 247 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); |
| 244 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); | 248 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); |
| 245 } | 249 } |
| 246 | 250 |
| 247 PopularSites::PopularSites(PrefService* prefs, | 251 PopularSites::PopularSites( |
| 248 net::URLRequestContextGetter* download_context, | 252 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, |
| 249 const base::FilePath& directory, | 253 PrefService* prefs, |
| 250 const std::string& country, | 254 net::URLRequestContextGetter* download_context, |
| 251 const std::string& version, | 255 const base::FilePath& directory, |
| 252 const GURL& override_url, | 256 const std::string& country, |
| 253 bool force_download, | 257 const std::string& version, |
| 254 const FinishedCallback& callback) | 258 const GURL& override_url, |
| 259 bool force_download, |
| 260 const FinishedCallback& callback) |
| 255 : callback_(callback), | 261 : callback_(callback), |
| 256 is_fallback_(false), | 262 is_fallback_(false), |
| 257 pending_country_(country), | 263 pending_country_(country), |
| 258 pending_version_(version), | 264 pending_version_(version), |
| 259 local_path_(directory.empty() | 265 local_path_(directory.empty() |
| 260 ? base::FilePath() | 266 ? base::FilePath() |
| 261 : directory.AppendASCII(kPopularSitesLocalFilename)), | 267 : directory.AppendASCII(kPopularSitesLocalFilename)), |
| 262 prefs_(prefs), | 268 prefs_(prefs), |
| 263 download_context_(download_context), | 269 download_context_(download_context), |
| 264 runner_( | 270 blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( |
| 265 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 271 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), |
| 266 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), | |
| 267 weak_ptr_factory_(this) { | 272 weak_ptr_factory_(this) { |
| 268 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 269 const base::Time last_download_time = base::Time::FromInternalValue( | 273 const base::Time last_download_time = base::Time::FromInternalValue( |
| 270 prefs_->GetInt64(kPopularSitesLastDownloadPref)); | 274 prefs_->GetInt64(kPopularSitesLastDownloadPref)); |
| 271 const base::TimeDelta time_since_last_download = | 275 const base::TimeDelta time_since_last_download = |
| 272 base::Time::Now() - last_download_time; | 276 base::Time::Now() - last_download_time; |
| 273 const base::TimeDelta redownload_interval = | 277 const base::TimeDelta redownload_interval = |
| 274 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); | 278 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); |
| 275 const bool download_time_is_future = base::Time::Now() < last_download_time; | 279 const bool download_time_is_future = base::Time::Now() < last_download_time; |
| 276 const bool country_changed = GetCountry() != pending_country_; | 280 const bool country_changed = GetCountry() != pending_country_; |
| 277 const bool version_changed = GetVersion() != pending_version_; | 281 const bool version_changed = GetVersion() != pending_version_; |
| 278 | 282 |
| 279 const GURL url = | 283 const GURL url = |
| 280 override_url.is_valid() ? override_url : GetPopularSitesURL(); | 284 override_url.is_valid() ? override_url : GetPopularSitesURL(); |
| 281 | 285 |
| 282 // No valid path to save to. Immediately post failure. | 286 // No valid path to save to. Immediately post failure. |
| 283 if (local_path_.empty()) { | 287 if (local_path_.empty()) { |
| 284 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 288 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 285 base::Bind(callback_, false)); | 289 base::Bind(callback_, false)); |
| 286 return; | 290 return; |
| 287 } | 291 } |
| 288 | 292 |
| 289 // Download forced, or we need to download a new file. | 293 // Download forced, or we need to download a new file. |
| 290 if (force_download || download_time_is_future || | 294 if (force_download || download_time_is_future || |
| 291 (time_since_last_download > redownload_interval) || country_changed || | 295 (time_since_last_download > redownload_interval) || country_changed || |
| 292 version_changed) { | 296 version_changed) { |
| 293 FetchPopularSites(url); | 297 FetchPopularSites(url); |
| 294 return; | 298 return; |
| 295 } | 299 } |
| 296 | 300 |
| 297 std::unique_ptr<std::string> file_data(new std::string); | 301 std::unique_ptr<std::string> file_data(new std::string); |
| 298 std::string* file_data_ptr = file_data.get(); | 302 std::string* file_data_ptr = file_data.get(); |
| 299 base::PostTaskAndReplyWithResult( | 303 base::PostTaskAndReplyWithResult( |
| 300 runner_.get(), FROM_HERE, | 304 blocking_runner_.get(), FROM_HERE, |
| 301 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), | 305 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), |
| 302 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), | 306 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), |
| 303 url, base::Passed(std::move(file_data)))); | 307 url, base::Passed(std::move(file_data)))); |
| 304 } | 308 } |
| 305 | 309 |
| 306 GURL PopularSites::GetPopularSitesURL() const { | 310 GURL PopularSites::GetPopularSitesURL() const { |
| 307 return GURL(base::StringPrintf(kPopularSitesURLFormat, | 311 return GURL(base::StringPrintf(kPopularSitesURLFormat, |
| 308 pending_country_.c_str(), | 312 pending_country_.c_str(), |
| 309 pending_version_.c_str())); | 313 pending_version_.c_str())); |
| 310 } | 314 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 | 347 |
| 344 safe_json::JsonSanitizer::Sanitize( | 348 safe_json::JsonSanitizer::Sanitize( |
| 345 sketchy_json, base::Bind(&PopularSites::OnJsonSanitized, | 349 sketchy_json, base::Bind(&PopularSites::OnJsonSanitized, |
| 346 weak_ptr_factory_.GetWeakPtr()), | 350 weak_ptr_factory_.GetWeakPtr()), |
| 347 base::Bind(&PopularSites::OnJsonSanitizationFailed, | 351 base::Bind(&PopularSites::OnJsonSanitizationFailed, |
| 348 weak_ptr_factory_.GetWeakPtr())); | 352 weak_ptr_factory_.GetWeakPtr())); |
| 349 } | 353 } |
| 350 | 354 |
| 351 void PopularSites::OnJsonSanitized(const std::string& valid_minified_json) { | 355 void PopularSites::OnJsonSanitized(const std::string& valid_minified_json) { |
| 352 base::PostTaskAndReplyWithResult( | 356 base::PostTaskAndReplyWithResult( |
| 353 runner_.get(), FROM_HERE, | 357 blocking_runner_.get(), FROM_HERE, |
| 354 base::Bind(&base::ImportantFileWriter::WriteFileAtomically, local_path_, | 358 base::Bind(&base::ImportantFileWriter::WriteFileAtomically, local_path_, |
| 355 valid_minified_json), | 359 valid_minified_json), |
| 356 base::Bind(&PopularSites::OnFileWriteDone, weak_ptr_factory_.GetWeakPtr(), | 360 base::Bind(&PopularSites::OnFileWriteDone, weak_ptr_factory_.GetWeakPtr(), |
| 357 valid_minified_json)); | 361 valid_minified_json)); |
| 358 } | 362 } |
| 359 | 363 |
| 360 void PopularSites::OnJsonSanitizationFailed(const std::string& error_message) { | 364 void PopularSites::OnJsonSanitizationFailed(const std::string& error_message) { |
| 361 DLOG(WARNING) << "JSON sanitization failed: " << error_message; | 365 DLOG(WARNING) << "JSON sanitization failed: " << error_message; |
| 362 OnDownloadFailed(); | 366 OnDownloadFailed(); |
| 363 } | 367 } |
| 364 | 368 |
| 365 void PopularSites::OnFileWriteDone(const std::string& json, bool success) { | 369 void PopularSites::OnFileWriteDone(const std::string& json, bool success) { |
| 366 if (success) { | 370 if (success) { |
| 367 prefs_->SetInt64(kPopularSitesLastDownloadPref, | 371 prefs_->SetInt64(kPopularSitesLastDownloadPref, |
| 368 base::Time::Now().ToInternalValue()); | 372 base::Time::Now().ToInternalValue()); |
| 369 prefs_->SetString(kPopularSitesCountryPref, pending_country_); | 373 prefs_->SetString(kPopularSitesCountryPref, pending_country_); |
| 370 prefs_->SetString(kPopularSitesVersionPref, pending_version_); | 374 prefs_->SetString(kPopularSitesVersionPref, pending_version_); |
| 371 ParseSiteList(json); | 375 ParseSiteList(json); |
| 372 } else { | 376 } else { |
| 373 DLOG(WARNING) << "Could not write file to " | 377 DLOG(WARNING) << "Could not write file to " |
| 374 << local_path_.LossyDisplayName(); | 378 << local_path_.LossyDisplayName(); |
| 375 OnDownloadFailed(); | 379 OnDownloadFailed(); |
| 376 } | 380 } |
| 377 } | 381 } |
| 378 | 382 |
| 379 void PopularSites::ParseSiteList(const std::string& json) { | 383 void PopularSites::ParseSiteList(const std::string& json) { |
| 380 base::PostTaskAndReplyWithResult( | 384 base::PostTaskAndReplyWithResult( |
| 381 runner_.get(), FROM_HERE, base::Bind(&ParseJson, json), | 385 blocking_runner_.get(), FROM_HERE, base::Bind(&ParseJson, json), |
| 382 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); | 386 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); |
| 383 } | 387 } |
| 384 | 388 |
| 385 void PopularSites::OnJsonParsed(std::unique_ptr<std::vector<Site>> sites) { | 389 void PopularSites::OnJsonParsed(std::unique_ptr<std::vector<Site>> sites) { |
| 386 if (sites) | 390 if (sites) |
| 387 sites_.swap(*sites); | 391 sites_.swap(*sites); |
| 388 else | 392 else |
| 389 sites_.clear(); | 393 sites_.clear(); |
| 390 callback_.Run(!!sites); | 394 callback_.Run(!!sites); |
| 391 } | 395 } |
| 392 | 396 |
| 393 void PopularSites::OnDownloadFailed() { | 397 void PopularSites::OnDownloadFailed() { |
| 394 if (!is_fallback_) { | 398 if (!is_fallback_) { |
| 395 DLOG(WARNING) << "Download country site list failed"; | 399 DLOG(WARNING) << "Download country site list failed"; |
| 396 is_fallback_ = true; | 400 is_fallback_ = true; |
| 397 pending_country_ = kPopularSitesDefaultCountryCode; | 401 pending_country_ = kPopularSitesDefaultCountryCode; |
| 398 pending_version_ = kPopularSitesDefaultVersion; | 402 pending_version_ = kPopularSitesDefaultVersion; |
| 399 FetchPopularSites(GetPopularSitesURL()); | 403 FetchPopularSites(GetPopularSitesURL()); |
| 400 } else { | 404 } else { |
| 401 DLOG(WARNING) << "Download fallback site list failed"; | 405 DLOG(WARNING) << "Download fallback site list failed"; |
| 402 callback_.Run(false); | 406 callback_.Run(false); |
| 403 } | 407 } |
| 404 } | 408 } |
| OLD | NEW |