| 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" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/important_file_writer.h" |
| 13 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 14 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 17 #include "base/task_runner_util.h" | 18 #include "base/task_runner_util.h" |
| 18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 19 #include "base/values.h" | 20 #include "base/values.h" |
| 20 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
| 21 #include "components/google/core/browser/google_util.h" | 22 #include "components/google/core/browser/google_util.h" |
| 22 #include "components/ntp_tiles/pref_names.h" | 23 #include "components/ntp_tiles/pref_names.h" |
| 23 #include "components/ntp_tiles/switches.h" | 24 #include "components/ntp_tiles/switches.h" |
| 24 #include "components/pref_registry/pref_registry_syncable.h" | 25 #include "components/pref_registry/pref_registry_syncable.h" |
| 25 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
| 27 #include "components/safe_json/json_sanitizer.h" |
| 26 #include "components/search_engines/search_engine_type.h" | 28 #include "components/search_engines/search_engine_type.h" |
| 27 #include "components/search_engines/template_url_prepopulate_data.h" | 29 #include "components/search_engines/template_url_prepopulate_data.h" |
| 28 #include "components/search_engines/template_url_service.h" | 30 #include "components/search_engines/template_url_service.h" |
| 29 #include "components/variations/service/variations_service.h" | 31 #include "components/variations/service/variations_service.h" |
| 30 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 33 #include "net/base/load_flags.h" |
| 34 #include "net/http/http_status_code.h" |
| 31 | 35 |
| 32 using content::BrowserThread; | 36 using content::BrowserThread; |
| 37 using net::URLFetcher; |
| 33 using variations::VariationsService; | 38 using variations::VariationsService; |
| 34 | 39 |
| 35 namespace { | 40 namespace { |
| 36 | 41 |
| 37 const char kPopularSitesURLFormat[] = | 42 const char kPopularSitesURLFormat[] = |
| 38 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; | 43 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; |
| 39 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; | 44 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; |
| 40 const char kPopularSitesDefaultVersion[] = "5"; | 45 const char kPopularSitesDefaultVersion[] = "5"; |
| 41 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; | 46 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; |
| 42 const int kPopularSitesRedownloadIntervalHours = 24; | 47 const int kPopularSitesRedownloadIntervalHours = 24; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 121 |
| 117 if (version.empty()) | 122 if (version.empty()) |
| 118 version = variation_param_version; | 123 version = variation_param_version; |
| 119 | 124 |
| 120 if (version.empty()) | 125 if (version.empty()) |
| 121 version = kPopularSitesDefaultVersion; | 126 version = kPopularSitesDefaultVersion; |
| 122 | 127 |
| 123 return version; | 128 return version; |
| 124 } | 129 } |
| 125 | 130 |
| 126 base::FilePath GetPopularSitesPath() { | 131 std::unique_ptr<std::vector<PopularSites::Site>> ParseJson( |
| 127 base::FilePath dir; | 132 const std::string& json) { |
| 128 PathService::Get(chrome::DIR_USER_DATA, &dir); | |
| 129 return dir.AppendASCII(kPopularSitesLocalFilename); | |
| 130 } | |
| 131 | |
| 132 std::unique_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( | |
| 133 const base::FilePath& path) { | |
| 134 std::string json; | |
| 135 if (!base::ReadFileToString(path, &json)) { | |
| 136 DLOG(WARNING) << "Failed reading file"; | |
| 137 return nullptr; | |
| 138 } | |
| 139 | |
| 140 std::unique_ptr<base::Value> value = | 133 std::unique_ptr<base::Value> value = |
| 141 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS); | 134 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS); |
| 142 base::ListValue* list; | 135 base::ListValue* list; |
| 143 if (!value || !value->GetAsList(&list)) { | 136 if (!value || !value->GetAsList(&list)) { |
| 144 DLOG(WARNING) << "Failed parsing json"; | 137 DLOG(WARNING) << "Failed parsing json"; |
| 145 return nullptr; | 138 return nullptr; |
| 146 } | 139 } |
| 147 | 140 |
| 148 std::unique_ptr<std::vector<PopularSites::Site>> sites( | 141 std::unique_ptr<std::vector<PopularSites::Site>> sites( |
| 149 new std::vector<PopularSites::Site>); | 142 new std::vector<PopularSites::Site>); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 165 sites->push_back(PopularSites::Site(title, GURL(url), GURL(favicon_url), | 158 sites->push_back(PopularSites::Site(title, GURL(url), GURL(favicon_url), |
| 166 GURL(large_icon_url), | 159 GURL(large_icon_url), |
| 167 GURL(thumbnail_url))); | 160 GURL(thumbnail_url))); |
| 168 } | 161 } |
| 169 | 162 |
| 170 return sites; | 163 return sites; |
| 171 } | 164 } |
| 172 | 165 |
| 173 } // namespace | 166 } // namespace |
| 174 | 167 |
| 168 base::FilePath ChromePopularSites::GetDirectory() { |
| 169 base::FilePath dir; |
| 170 PathService::Get(chrome::DIR_USER_DATA, &dir); |
| 171 return dir; // empty if PathService::Get() failed. |
| 172 } |
| 173 |
| 175 PopularSites::Site::Site(const base::string16& title, | 174 PopularSites::Site::Site(const base::string16& title, |
| 176 const GURL& url, | 175 const GURL& url, |
| 177 const GURL& favicon_url, | 176 const GURL& favicon_url, |
| 178 const GURL& large_icon_url, | 177 const GURL& large_icon_url, |
| 179 const GURL& thumbnail_url) | 178 const GURL& thumbnail_url) |
| 180 : title(title), | 179 : title(title), |
| 181 url(url), | 180 url(url), |
| 182 favicon_url(favicon_url), | 181 favicon_url(favicon_url), |
| 183 large_icon_url(large_icon_url), | 182 large_icon_url(large_icon_url), |
| 184 thumbnail_url(thumbnail_url) {} | 183 thumbnail_url(thumbnail_url) {} |
| 185 | 184 |
| 186 PopularSites::Site::Site(const Site& other) = default; | 185 PopularSites::Site::Site(const Site& other) = default; |
| 187 | 186 |
| 188 PopularSites::Site::~Site() {} | 187 PopularSites::Site::~Site() {} |
| 189 | 188 |
| 190 PopularSites::PopularSites(PrefService* prefs, | 189 PopularSites::PopularSites(PrefService* prefs, |
| 191 const TemplateURLService* template_url_service, | 190 const TemplateURLService* template_url_service, |
| 192 VariationsService* variations_service, | 191 VariationsService* variations_service, |
| 193 net::URLRequestContextGetter* download_context, | 192 net::URLRequestContextGetter* download_context, |
| 193 const base::FilePath& directory, |
| 194 const std::string& variation_param_country, | 194 const std::string& variation_param_country, |
| 195 const std::string& variation_param_version, | 195 const std::string& variation_param_version, |
| 196 bool force_download, | 196 bool force_download, |
| 197 const FinishedCallback& callback) | 197 const FinishedCallback& callback) |
| 198 : PopularSites(prefs, | 198 : PopularSites(prefs, |
| 199 template_url_service, | 199 template_url_service, |
| 200 download_context, | 200 download_context, |
| 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(PrefService* prefs, |
| 211 const TemplateURLService* template_url_service, | 212 const TemplateURLService* template_url_service, |
| 212 net::URLRequestContextGetter* download_context, | 213 net::URLRequestContextGetter* download_context, |
| 214 const base::FilePath& directory, |
| 213 const GURL& url, | 215 const GURL& url, |
| 214 const FinishedCallback& callback) | 216 const FinishedCallback& callback) |
| 215 : PopularSites(prefs, | 217 : PopularSites(prefs, |
| 216 template_url_service, | 218 template_url_service, |
| 217 download_context, | 219 download_context, |
| 220 directory, |
| 218 std::string(), | 221 std::string(), |
| 219 std::string(), | 222 std::string(), |
| 220 url, | 223 url, |
| 221 true, | 224 true, |
| 222 callback) {} | 225 callback) {} |
| 223 | 226 |
| 224 PopularSites::~PopularSites() {} | 227 PopularSites::~PopularSites() {} |
| 225 | 228 |
| 226 std::string PopularSites::GetCountry() const { | 229 std::string PopularSites::GetCountry() const { |
| 227 return prefs_->GetString(kPopularSitesCountryPref); | 230 return prefs_->GetString(kPopularSitesCountryPref); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 240 std::string()); | 243 std::string()); |
| 241 | 244 |
| 242 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); | 245 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0); |
| 243 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); | 246 user_prefs->RegisterStringPref(kPopularSitesCountryPref, std::string()); |
| 244 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); | 247 user_prefs->RegisterStringPref(kPopularSitesVersionPref, std::string()); |
| 245 } | 248 } |
| 246 | 249 |
| 247 PopularSites::PopularSites(PrefService* prefs, | 250 PopularSites::PopularSites(PrefService* prefs, |
| 248 const TemplateURLService* template_url_service, | 251 const TemplateURLService* template_url_service, |
| 249 net::URLRequestContextGetter* download_context, | 252 net::URLRequestContextGetter* download_context, |
| 253 const base::FilePath& directory, |
| 250 const std::string& country, | 254 const std::string& country, |
| 251 const std::string& version, | 255 const std::string& version, |
| 252 const GURL& override_url, | 256 const GURL& override_url, |
| 253 bool force_download, | 257 bool force_download, |
| 254 const FinishedCallback& callback) | 258 const FinishedCallback& callback) |
| 255 : callback_(callback), | 259 : callback_(callback), |
| 260 is_fallback_(false), |
| 256 pending_country_(country), | 261 pending_country_(country), |
| 257 pending_version_(version), | 262 pending_version_(version), |
| 258 local_path_(GetPopularSitesPath()), | 263 local_path_(directory.empty() |
| 264 ? base::FilePath() |
| 265 : directory.AppendASCII(kPopularSitesLocalFilename)), |
| 259 prefs_(prefs), | 266 prefs_(prefs), |
| 260 template_url_service_(template_url_service), | 267 template_url_service_(template_url_service), |
| 261 download_context_(download_context), | 268 download_context_(download_context), |
| 269 runner_( |
| 270 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| 271 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), |
| 262 weak_ptr_factory_(this) { | 272 weak_ptr_factory_(this) { |
| 273 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 263 const base::Time last_download_time = base::Time::FromInternalValue( | 274 const base::Time last_download_time = base::Time::FromInternalValue( |
| 264 prefs_->GetInt64(kPopularSitesLastDownloadPref)); | 275 prefs_->GetInt64(kPopularSitesLastDownloadPref)); |
| 265 const base::TimeDelta time_since_last_download = | 276 const base::TimeDelta time_since_last_download = |
| 266 base::Time::Now() - last_download_time; | 277 base::Time::Now() - last_download_time; |
| 267 const base::TimeDelta redownload_interval = | 278 const base::TimeDelta redownload_interval = |
| 268 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); | 279 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours); |
| 269 const bool download_time_is_future = base::Time::Now() < last_download_time; | 280 const bool download_time_is_future = base::Time::Now() < last_download_time; |
| 270 const bool country_changed = GetCountry() != pending_country_; | 281 const bool country_changed = GetCountry() != pending_country_; |
| 271 const bool version_changed = GetVersion() != pending_version_; | 282 const bool version_changed = GetVersion() != pending_version_; |
| 272 | 283 |
| 273 const bool should_redownload_if_exists = | 284 const GURL url = |
| 274 force_download || download_time_is_future || | 285 override_url.is_valid() ? override_url : GetPopularSitesURL(); |
| 286 |
| 287 // No valid path to save to. Immediately post failure. |
| 288 if (local_path_.empty()) { |
| 289 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 290 base::Bind(callback_, false)); |
| 291 return; |
| 292 } |
| 293 |
| 294 // Download forced, or we need to download a new file. |
| 295 if (force_download || download_time_is_future || |
| 275 (time_since_last_download > redownload_interval) || country_changed || | 296 (time_since_last_download > redownload_interval) || country_changed || |
| 276 version_changed; | 297 version_changed) { |
| 298 FetchPopularSites(url); |
| 299 return; |
| 300 } |
| 277 | 301 |
| 278 FetchPopularSites( | 302 std::unique_ptr<std::string> file_data(new std::string); |
| 279 override_url.is_valid() ? override_url : GetPopularSitesURL(), | 303 std::string* file_data_ptr = file_data.get(); |
| 280 should_redownload_if_exists, false /* is_fallback */); | 304 base::PostTaskAndReplyWithResult( |
| 305 runner_.get(), FROM_HERE, |
| 306 base::Bind(&base::ReadFileToString, local_path_, file_data_ptr), |
| 307 base::Bind(&PopularSites::OnReadFileDone, weak_ptr_factory_.GetWeakPtr(), |
| 308 url, base::Passed(std::move(file_data)))); |
| 281 } | 309 } |
| 282 | 310 |
| 283 GURL PopularSites::GetPopularSitesURL() const { | 311 GURL PopularSites::GetPopularSitesURL() const { |
| 284 return GURL(base::StringPrintf(kPopularSitesURLFormat, | 312 return GURL(base::StringPrintf(kPopularSitesURLFormat, |
| 285 pending_country_.c_str(), | 313 pending_country_.c_str(), |
| 286 pending_version_.c_str())); | 314 pending_version_.c_str())); |
| 287 } | 315 } |
| 288 | 316 |
| 289 void PopularSites::FetchPopularSites(const GURL& url, | 317 void PopularSites::OnReadFileDone(const GURL& url, |
| 290 bool force_download, | 318 std::unique_ptr<std::string> data, |
| 291 bool is_fallback) { | 319 bool success) { |
| 292 downloader_.reset(new FileDownloader( | 320 if (success) { |
| 293 url, local_path_, force_download, download_context_, | 321 ParseSiteList(*data); |
| 294 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), | 322 } else { |
| 295 is_fallback))); | 323 // File didn't exist, or couldn't be read for some other reason. |
| 296 } | 324 FetchPopularSites(url); |
| 297 | |
| 298 void PopularSites::OnDownloadDone(bool is_fallback, | |
| 299 FileDownloader::Result result) { | |
| 300 downloader_.reset(); | |
| 301 switch (result) { | |
| 302 case FileDownloader::DOWNLOADED: | |
| 303 prefs_->SetInt64(kPopularSitesLastDownloadPref, | |
| 304 base::Time::Now().ToInternalValue()); | |
| 305 prefs_->SetString(kPopularSitesCountryPref, pending_country_); | |
| 306 prefs_->SetString(kPopularSitesVersionPref, pending_version_); | |
| 307 ParseSiteList(local_path_); | |
| 308 break; | |
| 309 case FileDownloader::EXISTS: | |
| 310 ParseSiteList(local_path_); | |
| 311 break; | |
| 312 case FileDownloader::FAILED: | |
| 313 if (!is_fallback) { | |
| 314 DLOG(WARNING) << "Download country site list failed"; | |
| 315 pending_country_ = kPopularSitesDefaultCountryCode; | |
| 316 pending_version_ = kPopularSitesDefaultVersion; | |
| 317 // It is fine to force the download as Fallback is only triggered after | |
| 318 // a failed download. | |
| 319 FetchPopularSites(GetPopularSitesURL(), true /* force_download */, | |
| 320 true /* is_fallback */); | |
| 321 } else { | |
| 322 DLOG(WARNING) << "Download fallback site list failed"; | |
| 323 callback_.Run(false); | |
| 324 } | |
| 325 break; | |
| 326 } | 325 } |
| 327 } | 326 } |
| 328 | 327 |
| 329 void PopularSites::ParseSiteList(const base::FilePath& path) { | 328 void PopularSites::FetchPopularSites(const GURL& url) { |
| 329 fetcher_ = URLFetcher::Create(url, URLFetcher::GET, this); |
| 330 fetcher_->SetRequestContext(download_context_); |
| 331 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 332 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 333 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); |
| 334 fetcher_->Start(); |
| 335 } |
| 336 |
| 337 void PopularSites::OnURLFetchComplete(const net::URLFetcher* source) { |
| 338 DCHECK_EQ(fetcher_.get(), source); |
| 339 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); |
| 340 |
| 341 std::string sketchy_json; |
| 342 if (!(source->GetStatus().is_success() && |
| 343 source->GetResponseCode() == net::HTTP_OK && |
| 344 source->GetResponseAsString(&sketchy_json))) { |
| 345 OnDownloadFailed(); |
| 346 return; |
| 347 } |
| 348 |
| 349 safe_json::JsonSanitizer::Sanitize( |
| 350 sketchy_json, base::Bind(&PopularSites::OnJsonSanitized, |
| 351 weak_ptr_factory_.GetWeakPtr()), |
| 352 base::Bind(&PopularSites::OnJsonSanitizationFailed, |
| 353 weak_ptr_factory_.GetWeakPtr())); |
| 354 } |
| 355 |
| 356 void PopularSites::OnJsonSanitized(const std::string& valid_minified_json) { |
| 330 base::PostTaskAndReplyWithResult( | 357 base::PostTaskAndReplyWithResult( |
| 331 BrowserThread::GetBlockingPool() | 358 runner_.get(), FROM_HERE, |
| 332 ->GetTaskRunnerWithShutdownBehavior( | 359 base::Bind(&base::ImportantFileWriter::WriteFileAtomically, local_path_, |
| 333 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) | 360 valid_minified_json), |
| 334 .get(), | 361 base::Bind(&PopularSites::OnFileWriteDone, weak_ptr_factory_.GetWeakPtr(), |
| 335 FROM_HERE, base::Bind(&ReadAndParseJsonFile, path), | 362 valid_minified_json)); |
| 363 } |
| 364 |
| 365 void PopularSites::OnJsonSanitizationFailed(const std::string& error_message) { |
| 366 DLOG(WARNING) << "JSON sanitization failed: " << error_message; |
| 367 OnDownloadFailed(); |
| 368 } |
| 369 |
| 370 void PopularSites::OnFileWriteDone(const std::string& json, bool success) { |
| 371 if (success) { |
| 372 prefs_->SetInt64(kPopularSitesLastDownloadPref, |
| 373 base::Time::Now().ToInternalValue()); |
| 374 prefs_->SetString(kPopularSitesCountryPref, pending_country_); |
| 375 prefs_->SetString(kPopularSitesVersionPref, pending_version_); |
| 376 ParseSiteList(json); |
| 377 } else { |
| 378 DLOG(WARNING) << "Could not write file to " |
| 379 << local_path_.LossyDisplayName(); |
| 380 OnDownloadFailed(); |
| 381 } |
| 382 } |
| 383 |
| 384 void PopularSites::ParseSiteList(const std::string& json) { |
| 385 base::PostTaskAndReplyWithResult( |
| 386 runner_.get(), FROM_HERE, base::Bind(&ParseJson, json), |
| 336 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); | 387 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr())); |
| 337 } | 388 } |
| 338 | 389 |
| 339 void PopularSites::OnJsonParsed(std::unique_ptr<std::vector<Site>> sites) { | 390 void PopularSites::OnJsonParsed(std::unique_ptr<std::vector<Site>> sites) { |
| 340 if (sites) | 391 if (sites) |
| 341 sites_.swap(*sites); | 392 sites_.swap(*sites); |
| 342 else | 393 else |
| 343 sites_.clear(); | 394 sites_.clear(); |
| 344 callback_.Run(!!sites); | 395 callback_.Run(!!sites); |
| 345 } | 396 } |
| 397 |
| 398 void PopularSites::OnDownloadFailed() { |
| 399 if (!is_fallback_) { |
| 400 DLOG(WARNING) << "Download country site list failed"; |
| 401 is_fallback_ = true; |
| 402 pending_country_ = kPopularSitesDefaultCountryCode; |
| 403 pending_version_ = kPopularSitesDefaultVersion; |
| 404 FetchPopularSites(GetPopularSitesURL()); |
| 405 } else { |
| 406 DLOG(WARNING) << "Download fallback site list failed"; |
| 407 callback_.Run(false); |
| 408 } |
| 409 } |
| OLD | NEW |