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