| 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" |
| 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/files/important_file_writer.h" |
| 14 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 15 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "components/google/core/browser/google_util.h" | 22 #include "components/google/core/browser/google_util.h" |
| 23 #include "components/ntp_tiles/constants.h" | 23 #include "components/ntp_tiles/constants.h" |
| 24 #include "components/ntp_tiles/pref_names.h" | 24 #include "components/ntp_tiles/pref_names.h" |
| 25 #include "components/ntp_tiles/switches.h" | 25 #include "components/ntp_tiles/switches.h" |
| 26 #include "components/pref_registry/pref_registry_syncable.h" | 26 #include "components/pref_registry/pref_registry_syncable.h" |
| 27 #include "components/prefs/pref_service.h" | 27 #include "components/prefs/pref_service.h" |
| 28 #include "components/safe_json/safe_json_parser.h" | |
| 29 #include "components/search_engines/search_engine_type.h" | 28 #include "components/search_engines/search_engine_type.h" |
| 30 #include "components/search_engines/template_url_service.h" | 29 #include "components/search_engines/template_url_service.h" |
| 31 #include "components/variations/service/variations_service.h" | 30 #include "components/variations/service/variations_service.h" |
| 32 #include "components/variations/variations_associated_data.h" | 31 #include "components/variations/variations_associated_data.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 net::URLFetcher; | 35 using net::URLFetcher; |
| 37 using variations::VariationsService; | 36 using variations::VariationsService; |
| 38 | 37 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 PopularSites::Site::Site(const Site& other) = default; | 168 PopularSites::Site::Site(const Site& other) = default; |
| 170 | 169 |
| 171 PopularSites::Site::~Site() {} | 170 PopularSites::Site::~Site() {} |
| 172 | 171 |
| 173 PopularSites::PopularSites( | 172 PopularSites::PopularSites( |
| 174 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, | 173 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, |
| 175 PrefService* prefs, | 174 PrefService* prefs, |
| 176 const TemplateURLService* template_url_service, | 175 const TemplateURLService* template_url_service, |
| 177 VariationsService* variations_service, | 176 VariationsService* variations_service, |
| 178 net::URLRequestContextGetter* download_context, | 177 net::URLRequestContextGetter* download_context, |
| 179 const base::FilePath& directory) | 178 const base::FilePath& directory, |
| 179 ParseJSONCallback parse_json) |
| 180 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( | 180 : blocking_runner_(blocking_pool->GetTaskRunnerWithShutdownBehavior( |
| 181 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), | 181 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)), |
| 182 prefs_(prefs), | 182 prefs_(prefs), |
| 183 template_url_service_(template_url_service), | 183 template_url_service_(template_url_service), |
| 184 variations_(variations_service), | 184 variations_(variations_service), |
| 185 download_context_(download_context), | 185 download_context_(download_context), |
| 186 local_path_(directory.empty() | 186 local_path_(directory.empty() |
| 187 ? base::FilePath() | 187 ? base::FilePath() |
| 188 : directory.AppendASCII(kPopularSitesLocalFilename)), | 188 : directory.AppendASCII(kPopularSitesLocalFilename)), |
| 189 parse_json_(std::move(parse_json)), |
| 189 is_fallback_(false), | 190 is_fallback_(false), |
| 190 weak_ptr_factory_(this) {} | 191 weak_ptr_factory_(this) {} |
| 191 | 192 |
| 192 PopularSites::~PopularSites() {} | 193 PopularSites::~PopularSites() {} |
| 193 | 194 |
| 194 void PopularSites::StartFetch(bool force_download, | 195 void PopularSites::StartFetch(bool force_download, |
| 195 const FinishedCallback& callback) { | 196 const FinishedCallback& callback) { |
| 196 DCHECK(!callback_); | 197 DCHECK(!callback_); |
| 197 callback_ = callback; | 198 callback_ = callback; |
| 198 | 199 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); | 290 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); |
| 290 | 291 |
| 291 std::string json_string; | 292 std::string json_string; |
| 292 if (!(source->GetStatus().is_success() && | 293 if (!(source->GetStatus().is_success() && |
| 293 source->GetResponseCode() == net::HTTP_OK && | 294 source->GetResponseCode() == net::HTTP_OK && |
| 294 source->GetResponseAsString(&json_string))) { | 295 source->GetResponseAsString(&json_string))) { |
| 295 OnDownloadFailed(); | 296 OnDownloadFailed(); |
| 296 return; | 297 return; |
| 297 } | 298 } |
| 298 | 299 |
| 299 safe_json::SafeJsonParser::Parse( | 300 parse_json_.Run( |
| 300 json_string, | 301 json_string, |
| 301 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), | 302 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), |
| 302 base::Bind(&PopularSites::OnJsonParseFailed, | 303 base::Bind(&PopularSites::OnJsonParseFailed, |
| 303 weak_ptr_factory_.GetWeakPtr())); | 304 weak_ptr_factory_.GetWeakPtr())); |
| 304 } | 305 } |
| 305 | 306 |
| 306 void PopularSites::OnJsonParsed(std::unique_ptr<base::Value> json) { | 307 void PopularSites::OnJsonParsed(std::unique_ptr<base::Value> json) { |
| 307 const base::Value* json_ptr = json.get(); | 308 const base::Value* json_ptr = json.get(); |
| 308 base::PostTaskAndReplyWithResult( | 309 base::PostTaskAndReplyWithResult( |
| 309 blocking_runner_.get(), FROM_HERE, | 310 blocking_runner_.get(), FROM_HERE, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, | 373 pending_url_ = GetPopularSitesURL(kPopularSitesDefaultCountryCode, |
| 373 kPopularSitesDefaultVersion); | 374 kPopularSitesDefaultVersion); |
| 374 FetchPopularSites(); | 375 FetchPopularSites(); |
| 375 } else { | 376 } else { |
| 376 DLOG(WARNING) << "Download fallback site list failed"; | 377 DLOG(WARNING) << "Download fallback site list failed"; |
| 377 callback_.Run(false); | 378 callback_.Run(false); |
| 378 } | 379 } |
| 379 } | 380 } |
| 380 | 381 |
| 381 } // namespace ntp_tiles | 382 } // namespace ntp_tiles |
| OLD | NEW |