Chromium Code Reviews| 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 scoped_refptr<base::SingleThreadTaskRunner> ui_thread, |
| 191 VariationsService* variations_service, | 189 scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
| 192 net::URLRequestContextGetter* download_context, | 190 PrefService* prefs, |
| 193 const base::FilePath& directory, | 191 const TemplateURLService* template_url_service, |
| 194 const std::string& variation_param_country, | 192 VariationsService* variations_service, |
| 195 const std::string& variation_param_version, | 193 net::URLRequestContextGetter* download_context, |
| 196 bool force_download, | 194 const base::FilePath& directory, |
| 197 const FinishedCallback& callback) | 195 const std::string& variation_param_country, |
| 198 : PopularSites(prefs, | 196 const std::string& variation_param_version, |
| 197 bool force_download, | |
| 198 const FinishedCallback& callback) | |
| 199 : PopularSites(std::move(ui_thread), | |
| 200 std::move(blocking_pool), | |
| 201 prefs, | |
| 199 download_context, | 202 download_context, |
| 200 directory, | 203 directory, |
| 201 GetCountryToUse(prefs, | 204 GetCountryToUse(prefs, |
| 202 template_url_service, | 205 template_url_service, |
| 203 variations_service, | 206 variations_service, |
| 204 variation_param_country), | 207 variation_param_country), |
| 205 GetVersionToUse(prefs, variation_param_version), | 208 GetVersionToUse(prefs, variation_param_version), |
| 206 GURL(), | 209 GURL(), |
| 207 force_download, | 210 force_download, |
| 208 callback) {} | 211 callback) {} |
| 209 | 212 |
| 210 PopularSites::PopularSites(PrefService* prefs, | 213 PopularSites::PopularSites( |
| 211 net::URLRequestContextGetter* download_context, | 214 scoped_refptr<base::SingleThreadTaskRunner> ui_thread, |
| 212 const base::FilePath& directory, | 215 scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
| 213 const GURL& url, | 216 PrefService* prefs, |
| 214 const FinishedCallback& callback) | 217 net::URLRequestContextGetter* download_context, |
| 215 : PopularSites(prefs, | 218 const base::FilePath& directory, |
| 219 const GURL& url, | |
| 220 const FinishedCallback& callback) | |
| 221 : PopularSites(std::move(ui_thread), | |
| 222 std::move(blocking_pool), | |
| 223 prefs, | |
| 216 download_context, | 224 download_context, |
| 217 directory, | 225 directory, |
| 218 std::string(), | 226 std::string(), |
| 219 std::string(), | 227 std::string(), |
| 220 url, | 228 url, |
| 221 true, | 229 true, |
| 222 callback) {} | 230 callback) {} |
| 223 | 231 |
| 224 PopularSites::~PopularSites() {} | 232 PopularSites::~PopularSites() {} |
| 225 | 233 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 237 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, | 245 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideCountry, |
| 238 std::string()); | 246 std::string()); |
| 239 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, | 247 user_prefs->RegisterStringPref(ntp_tiles::prefs::kPopularSitesOverrideVersion, |
| 240 std::string()); | 248 std::string()); |
| 241 | 249 |
| 242 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 250 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
| 243 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); | 251 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); |
| 244 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); | 252 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); |
| 245 } | 253 } |
| 246 | 254 |
| 247 PopularSites::PopularSites(PrefService* prefs, | 255 PopularSites::PopularSites( |
| 248 net::URLRequestContextGetter* download_context, | 256 scoped_refptr<base::SingleThreadTaskRunner> ui_thread, |
| 249 const base::FilePath& directory, | 257 scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
| 250 const std::string& country, | 258 PrefService* prefs, |
| 251 const std::string& version, | 259 net::URLRequestContextGetter* download_context, |
| 252 const GURL& override_url, | 260 const base::FilePath& directory, |
| 253 bool force_download, | 261 const std::string& country, |
| 254 const FinishedCallback& callback) | 262 const std::string& version, |
| 263 const GURL& override_url, | |
| 264 bool force_download, | |
| 265 const FinishedCallback& callback) | |
| 255 : callback_(callback), | 266 : callback_(callback), |
| 256 is_fallback_(false), | 267 is_fallback_(false), |
| 257 pending_country_(country), | 268 pending_country_(country), |
| 258 pending_version_(version), | 269 pending_version_(version), |
| 259 local_path_(directory.empty() | 270 local_path_(directory.empty() |
| 260 ? base::FilePath() | 271 ? base::FilePath() |
| 261 : directory.AppendASCII(kPopularSitesLocalFilename)), | 272 : directory.AppendASCII(kPopularSitesLocalFilename)), |
| 262 prefs_(prefs), | 273 prefs_(prefs), |
| 263 download_context_(download_context), | 274 download_context_(download_context), |
| 264 runner_( | 275 ui_thread_(std::move(ui_thread)), |
| 265 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 276 blocking_pool_(std::move(blocking_pool)), |
| 266 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), | |
| 267 weak_ptr_factory_(this) { | 277 weak_ptr_factory_(this) { |
| 268 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 278 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 269 const base::Time last_download_time = base::Time::FromInternalValue( | 279 const base::Time last_download_time = base::Time::FromInternalValue( |
| 270 prefs_->GetInt64(kPopularSitesLastDownloadPref)); | 280 prefs_->GetInt64(kPopularSitesLastDownloadPref)); |
| 271 const base::TimeDelta time_since_last_download = | 281 const base::TimeDelta time_since_last_download = |
| 272 base::Time::Now() - last_download_time; | 282 base::Time::Now() - last_download_time; |
| 273 const base::TimeDelta redownload_interval = | 283 const base::TimeDelta redownload_interval = |
| 274 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); | 284 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); |
| 275 const bool download_time_is_future = base::Time::Now() < last_download_time; | 285 const bool download_time_is_future = base::Time::Now() < last_download_time; |
| 276 const bool country_changed = GetCountry() != pending_country_; | 286 const bool country_changed = GetCountry() != pending_country_; |
| 277 const bool version_changed = GetVersion() != pending_version_; | 287 const bool version_changed = GetVersion() != pending_version_; |
| 278 | 288 |
| 279 const GURL url = | 289 const GURL url = |
| 280 override_url.is_valid() ? override_url : GetPopularSitesURL(); | 290 override_url.is_valid() ? override_url : GetPopularSitesURL(); |
| 281 | 291 |
| 282 // No valid path to save to. Immediately post failure. | 292 // No valid path to save to. Immediately post failure. |
| 283 if (local_path_.empty()) { | 293 if (local_path_.empty()) { |
| 284 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 294 ui_thread_->PostTask(FROM_HERE, base::Bind(callback_, false)); |
| 285 base::Bind(callback_, false)); | |
| 286 return; | 295 return; |
| 287 } | 296 } |
| 288 | 297 |
| 289 // Download forced, or we need to download a new file. | 298 // Download forced, or we need to download a new file. |
| 290 if (force_download || download_time_is_future || | 299 if (force_download || download_time_is_future || |
| 291 (time_since_last_download > redownload_interval) || country_changed || | 300 (time_since_last_download > redownload_interval) || country_changed || |
| 292 version_changed) { | 301 version_changed) { |
| 293 FetchPopularSites(url); | 302 FetchPopularSites(url); |
| 294 return; | 303 return; |
| 295 } | 304 } |
| 296 | 305 |
| 297 std::unique_ptr<std::string> file_data(new std::string); | 306 std::unique_ptr<std::string> file_data(new std::string); |
| 298 std::string* file_data_ptr = file_data.get(); | 307 std::string* file_data_ptr = file_data.get(); |
| 299 base::PostTaskAndReplyWithResult( | 308 base::PostTaskAndReplyWithResult( |
| 300 runner_.get(), FROM_HERE, | 309 blocking_pool_ |
| 310 ->GetTaskRunnerWithShutdownBehavior( | |
| 311 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) | |
| 312 .get(), | |
| 313 FROM_HERE, | |
| 301 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), | 314 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), |
| 302 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), | 315 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), |
| 303 url, base::Passed(std::move(file_data)))); | 316 url, base::Passed(std::move(file_data)))); |
| 304 } | 317 } |
| 305 | 318 |
| 306 GURL PopularSites::GetPopularSitesURL() const { | 319 GURL PopularSites::GetPopularSitesURL() const { |
| 307 return GURL(base::StringPrintf(kPopularSitesURLFormat, | 320 return GURL(base::StringPrintf(kPopularSitesURLFormat, |
| 308 pending_country_.c_str(), | 321 pending_country_.c_str(), |
| 309 pending_version_.c_str())); | 322 pending_version_.c_str())); |
| 310 } | 323 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 | 356 |
| 344 safe_json::JsonSanitizer::Sanitize( | 357 safe_json::JsonSanitizer::Sanitize( |
| 345 sketchy_json, base::Bind(&PopularSites::OnJsonSanitized, | 358 sketchy_json, base::Bind(&PopularSites::OnJsonSanitized, |
| 346 weak_ptr_factory_.GetWeakPtr()), | 359 weak_ptr_factory_.GetWeakPtr()), |
| 347 base::Bind(&PopularSites::OnJsonSanitizationFailed, | 360 base::Bind(&PopularSites::OnJsonSanitizationFailed, |
| 348 weak_ptr_factory_.GetWeakPtr())); | 361 weak_ptr_factory_.GetWeakPtr())); |
| 349 } | 362 } |
| 350 | 363 |
| 351 void PopularSites::OnJsonSanitized(const std::string& valid_minified_json) { | 364 void PopularSites::OnJsonSanitized(const std::string& valid_minified_json) { |
| 352 base::PostTaskAndReplyWithResult( | 365 base::PostTaskAndReplyWithResult( |
| 353 runner_.get(), FROM_HERE, | 366 blocking_pool_ |
| 354 base::Bind(&base::ImportantFileWriter::WriteFileAtomically, local_path_, | 367 ->GetTaskRunnerWithShutdownBehavior( |
| 355 valid_minified_json), | 368 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) |
| 369 .get(), | |
| 370 FROM_HERE, base::Bind(&base::ImportantFileWriter::WriteFileAtomically, | |
| 371 local_path_, valid_minified_json), | |
| 356 base::Bind(&PopularSites::OnFileWriteDone, weak_ptr_factory_.GetWeakPtr(), | 372 base::Bind(&PopularSites::OnFileWriteDone, weak_ptr_factory_.GetWeakPtr(), |
| 357 valid_minified_json)); | 373 valid_minified_json)); |
| 358 } | 374 } |
| 359 | 375 |
| 360 void PopularSites::OnJsonSanitizationFailed(const std::string& error_message) { | 376 void PopularSites::OnJsonSanitizationFailed(const std::string& error_message) { |
| 361 DLOG(WARNING) << "JSON sanitization failed: " << error_message; | 377 DLOG(WARNING) << "JSON sanitization failed: " << error_message; |
| 362 OnDownloadFailed(); | 378 OnDownloadFailed(); |
| 363 } | 379 } |
| 364 | 380 |
| 365 void PopularSites::OnFileWriteDone(const std::string& json, bool success) { | 381 void PopularSites::OnFileWriteDone(const std::string& json, bool success) { |
| 366 if (success) { | 382 if (success) { |
| 367 prefs_->SetInt64(kPopularSitesLastDownloadPref, | 383 prefs_->SetInt64(kPopularSitesLastDownloadPref, |
| 368 base::Time::Now().ToInternalValue()); | 384 base::Time::Now().ToInternalValue()); |
| 369 prefs_->SetString(kPopularSitesCountryPref, pending_country_); | 385 prefs_->SetString(kPopularSitesCountryPref, pending_country_); |
| 370 prefs_->SetString(kPopularSitesVersionPref, pending_version_); | 386 prefs_->SetString(kPopularSitesVersionPref, pending_version_); |
| 371 ParseSiteList(json); | 387 ParseSiteList(json); |
| 372 } else { | 388 } else { |
| 373 DLOG(WARNING) << "Could not write file to " | 389 DLOG(WARNING) << "Could not write file to " |
| 374 << local_path_.LossyDisplayName(); | 390 << local_path_.LossyDisplayName(); |
| 375 OnDownloadFailed(); | 391 OnDownloadFailed(); |
| 376 } | 392 } |
| 377 } | 393 } |
| 378 | 394 |
| 379 void PopularSites::ParseSiteList(const std::string& json) { | 395 void PopularSites::ParseSiteList(const std::string& json) { |
| 380 base::PostTaskAndReplyWithResult( | 396 base::PostTaskAndReplyWithResult( |
| 381 runner_.get(), FROM_HERE, base::Bind(&ParseJson, json), | 397 blocking_pool_ |
| 398 ->GetTaskRunnerWithShutdownBehavior( | |
|
blundell
2016/05/24 18:01:45
This is a behavioral change, as you're no longer s
sfiera
2016/05/24 18:18:59
Done.
(In practice I think it should end up the s
| |
| 399 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) | |
| 400 .get(), | |
| 401 FROM_HERE, base::Bind(&ParseJson, json), | |
| 382 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); | 402 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); |
| 383 } | 403 } |
| 384 | 404 |
| 385 void PopularSites::OnJsonParsed(std::unique_ptr<std::vector<Site>> sites) { | 405 void PopularSites::OnJsonParsed(std::unique_ptr<std::vector<Site>> sites) { |
| 386 if (sites) | 406 if (sites) |
| 387 sites_.swap(*sites); | 407 sites_.swap(*sites); |
| 388 else | 408 else |
| 389 sites_.clear(); | 409 sites_.clear(); |
| 390 callback_.Run(!!sites); | 410 callback_.Run(!!sites); |
| 391 } | 411 } |
| 392 | 412 |
| 393 void PopularSites::OnDownloadFailed() { | 413 void PopularSites::OnDownloadFailed() { |
| 394 if (!is_fallback_) { | 414 if (!is_fallback_) { |
| 395 DLOG(WARNING) << "Download country site list failed"; | 415 DLOG(WARNING) << "Download country site list failed"; |
| 396 is_fallback_ = true; | 416 is_fallback_ = true; |
| 397 pending_country_ = kPopularSitesDefaultCountryCode; | 417 pending_country_ = kPopularSitesDefaultCountryCode; |
| 398 pending_version_ = kPopularSitesDefaultVersion; | 418 pending_version_ = kPopularSitesDefaultVersion; |
| 399 FetchPopularSites(GetPopularSitesURL()); | 419 FetchPopularSites(GetPopularSitesURL()); |
| 400 } else { | 420 } else { |
| 401 DLOG(WARNING) << "Download fallback site list failed"; | 421 DLOG(WARNING) << "Download fallback site list failed"; |
| 402 callback_.Run(false); | 422 callback_.Run(false); |
| 403 } | 423 } |
| 404 } | 424 } |
| OLD | NEW |