| 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/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 "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| 23 #include "components/google/core/browser/google_util.h" | 23 #include "components/google/core/browser/google_util.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" |
| 28 #include "components/search_engines/search_engine_type.h" | 29 #include "components/search_engines/search_engine_type.h" |
| 29 #include "components/search_engines/template_url_prepopulate_data.h" | 30 #include "components/search_engines/template_url_prepopulate_data.h" |
| 30 #include "components/search_engines/template_url_service.h" | 31 #include "components/search_engines/template_url_service.h" |
| 31 #include "components/variations/service/variations_service.h" | 32 #include "components/variations/service/variations_service.h" |
| 32 #include "net/base/load_flags.h" | 33 #include "net/base/load_flags.h" |
| 33 #include "net/http/http_status_code.h" | 34 #include "net/http/http_status_code.h" |
| 34 | 35 |
| 35 #if !defined(OS_IOS) | |
| 36 #include "components/safe_json/safe_json_parser.h" | |
| 37 #endif | |
| 38 | |
| 39 using net::URLFetcher; | 36 using net::URLFetcher; |
| 40 using variations::VariationsService; | 37 using variations::VariationsService; |
| 41 | 38 |
| 42 namespace { | 39 namespace { |
| 43 | 40 |
| 44 const char kPopularSitesURLFormat[] = | 41 const char kPopularSitesURLFormat[] = |
| 45 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; | 42 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; |
| 46 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; | 43 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; |
| 47 const char kPopularSitesDefaultVersion[] = "5"; | 44 const char kPopularSitesDefaultVersion[] = "5"; |
| 48 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; | 45 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 | 129 |
| 133 // Must run on the blocking thread pool. | 130 // Must run on the blocking thread pool. |
| 134 bool WriteJsonToFile(const base::FilePath& local_path, | 131 bool WriteJsonToFile(const base::FilePath& local_path, |
| 135 const base::Value* json) { | 132 const base::Value* json) { |
| 136 std::string json_string; | 133 std::string json_string; |
| 137 return base::JSONWriter::Write(*json, &json_string) && | 134 return base::JSONWriter::Write(*json, &json_string) && |
| 138 base::ImportantFileWriter::WriteFileAtomically(local_path, | 135 base::ImportantFileWriter::WriteFileAtomically(local_path, |
| 139 json_string); | 136 json_string); |
| 140 } | 137 } |
| 141 | 138 |
| 142 #if defined(OS_IOS) | |
| 143 // Mimics SafeJsonParser API, but parses unsafely for iOS. | |
| 144 class JsonUnsafeParser { | |
| 145 public: | |
| 146 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>; | |
| 147 using ErrorCallback = base::Callback<void(const std::string&)>; | |
| 148 | |
| 149 // As with SafeJsonParser, runs either success_callback or error_callback on | |
| 150 // the calling thread, but not before the call returns. | |
| 151 static void Parse(const std::string& unsafe_json, | |
| 152 const SuccessCallback& success_callback, | |
| 153 const ErrorCallback& error_callback) { | |
| 154 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 155 FROM_HERE, | |
| 156 base::Bind(DoParse, unsafe_json, success_callback, error_callback)); | |
| 157 } | |
| 158 | |
| 159 JsonUnsafeParser() = delete; | |
| 160 | |
| 161 private: | |
| 162 static void DoParse(const std::string& unsafe_json, | |
| 163 const SuccessCallback& success_callback, | |
| 164 const ErrorCallback& error_callback) { | |
| 165 std::string error_msg; | |
| 166 int error_line, error_column; | |
| 167 std::unique_ptr<base::Value> value = base::JSONReader::ReadAndReturnError( | |
| 168 unsafe_json, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error_msg, | |
| 169 &error_line, &error_column); | |
| 170 if (value) { | |
| 171 success_callback.Run(std::move(value)); | |
| 172 } else { | |
| 173 error_callback.Run(base::StringPrintf("%s (%d:%d)", error_msg.c_str(), | |
| 174 error_line, error_column)); | |
| 175 } | |
| 176 } | |
| 177 }; | |
| 178 | |
| 179 using UntrustedJsonParser = JsonUnsafeParser; | |
| 180 #else | |
| 181 using UntrustedJsonParser = safe_json::SafeJsonParser; | |
| 182 #endif | |
| 183 | |
| 184 } // namespace | 139 } // namespace |
| 185 | 140 |
| 186 base::FilePath ChromePopularSites::GetDirectory() { | 141 base::FilePath ChromePopularSites::GetDirectory() { |
| 187 base::FilePath dir; | 142 base::FilePath dir; |
| 188 PathService::Get(chrome::DIR_USER_DATA, &dir); | 143 PathService::Get(chrome::DIR_USER_DATA, &dir); |
| 189 return dir; // empty if PathService::Get() failed. | 144 return dir; // empty if PathService::Get() failed. |
| 190 } | 145 } |
| 191 | 146 |
| 192 PopularSites::Site::Site(const base::string16& title, | 147 PopularSites::Site::Site(const base::string16& title, |
| 193 const GURL& url, | 148 const GURL& url, |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); | 318 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); |
| 364 | 319 |
| 365 std::string json_string; | 320 std::string json_string; |
| 366 if (!(source->GetStatus().is_success() && | 321 if (!(source->GetStatus().is_success() && |
| 367 source->GetResponseCode() == net::HTTP_OK && | 322 source->GetResponseCode() == net::HTTP_OK && |
| 368 source->GetResponseAsString(&json_string))) { | 323 source->GetResponseAsString(&json_string))) { |
| 369 OnDownloadFailed(); | 324 OnDownloadFailed(); |
| 370 return; | 325 return; |
| 371 } | 326 } |
| 372 | 327 |
| 373 UntrustedJsonParser::Parse( | 328 safe_json::SafeJsonParser::Parse( |
| 374 json_string, | 329 json_string, |
| 375 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), | 330 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), |
| 376 base::Bind(&PopularSites::OnJsonParseFailed, | 331 base::Bind(&PopularSites::OnJsonParseFailed, |
| 377 weak_ptr_factory_.GetWeakPtr())); | 332 weak_ptr_factory_.GetWeakPtr())); |
| 378 } | 333 } |
| 379 | 334 |
| 380 void PopularSites::OnJsonParsed(std::unique_ptr<base::Value> json) { | 335 void PopularSites::OnJsonParsed(std::unique_ptr<base::Value> json) { |
| 381 const base::Value* json_ptr = json.get(); | 336 const base::Value* json_ptr = json.get(); |
| 382 base::PostTaskAndReplyWithResult( | 337 base::PostTaskAndReplyWithResult( |
| 383 blocking_runner_.get(), FROM_HERE, | 338 blocking_runner_.get(), FROM_HERE, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 DLOG(WARNING) << "Download country site list failed"; | 400 DLOG(WARNING) << "Download country site list failed"; |
| 446 is_fallback_ = true; | 401 is_fallback_ = true; |
| 447 pending_country_ = kPopularSitesDefaultCountryCode; | 402 pending_country_ = kPopularSitesDefaultCountryCode; |
| 448 pending_version_ = kPopularSitesDefaultVersion; | 403 pending_version_ = kPopularSitesDefaultVersion; |
| 449 FetchPopularSites(GetPopularSitesURL()); | 404 FetchPopularSites(GetPopularSitesURL()); |
| 450 } else { | 405 } else { |
| 451 DLOG(WARNING) << "Download fallback site list failed"; | 406 DLOG(WARNING) << "Download fallback site list failed"; |
| 452 callback_.Run(false); | 407 callback_.Run(false); |
| 453 } | 408 } |
| 454 } | 409 } |
| OLD | NEW |