| 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" | |
| 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_prepopulate_data.h" | 29 #include "components/search_engines/template_url_prepopulate_data.h" |
| 31 #include "components/search_engines/template_url_service.h" | 30 #include "components/search_engines/template_url_service.h" |
| 32 #include "components/variations/service/variations_service.h" | 31 #include "components/variations/service/variations_service.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 |
| 35 #if !defined(OS_IOS) |
| 36 #include "components/safe_json/safe_json_parser.h" |
| 37 #endif |
| 38 |
| 36 using net::URLFetcher; | 39 using net::URLFetcher; |
| 37 using variations::VariationsService; | 40 using variations::VariationsService; |
| 38 | 41 |
| 39 namespace { | 42 namespace { |
| 40 | 43 |
| 41 const char kPopularSitesURLFormat[] = | 44 const char kPopularSitesURLFormat[] = |
| 42 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; | 45 "https://www.gstatic.com/chrome/ntp/suggested_sites_%s_%s.json"; |
| 43 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; | 46 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; |
| 44 const char kPopularSitesDefaultVersion[] = "5"; | 47 const char kPopularSitesDefaultVersion[] = "5"; |
| 45 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; | 48 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 132 |
| 130 // Must run on the blocking thread pool. | 133 // Must run on the blocking thread pool. |
| 131 bool WriteJsonToFile(const base::FilePath& local_path, | 134 bool WriteJsonToFile(const base::FilePath& local_path, |
| 132 const base::Value* json) { | 135 const base::Value* json) { |
| 133 std::string json_string; | 136 std::string json_string; |
| 134 return base::JSONWriter::Write(*json, &json_string) && | 137 return base::JSONWriter::Write(*json, &json_string) && |
| 135 base::ImportantFileWriter::WriteFileAtomically(local_path, | 138 base::ImportantFileWriter::WriteFileAtomically(local_path, |
| 136 json_string); | 139 json_string); |
| 137 } | 140 } |
| 138 | 141 |
| 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 |
| 139 } // namespace | 184 } // namespace |
| 140 | 185 |
| 141 base::FilePath ChromePopularSites::GetDirectory() { | 186 base::FilePath ChromePopularSites::GetDirectory() { |
| 142 base::FilePath dir; | 187 base::FilePath dir; |
| 143 PathService::Get(chrome::DIR_USER_DATA, &dir); | 188 PathService::Get(chrome::DIR_USER_DATA, &dir); |
| 144 return dir; // empty if PathService::Get() failed. | 189 return dir; // empty if PathService::Get() failed. |
| 145 } | 190 } |
| 146 | 191 |
| 147 PopularSites::Site::Site(const base::string16& title, | 192 PopularSites::Site::Site(const base::string16& title, |
| 148 const GURL& url, | 193 const GURL& url, |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); | 363 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); |
| 319 | 364 |
| 320 std::string json_string; | 365 std::string json_string; |
| 321 if (!(source->GetStatus().is_success() && | 366 if (!(source->GetStatus().is_success() && |
| 322 source->GetResponseCode() == net::HTTP_OK && | 367 source->GetResponseCode() == net::HTTP_OK && |
| 323 source->GetResponseAsString(&json_string))) { | 368 source->GetResponseAsString(&json_string))) { |
| 324 OnDownloadFailed(); | 369 OnDownloadFailed(); |
| 325 return; | 370 return; |
| 326 } | 371 } |
| 327 | 372 |
| 328 safe_json::SafeJsonParser::Parse( | 373 UntrustedJsonParser::Parse( |
| 329 json_string, | 374 json_string, |
| 330 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), | 375 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), |
| 331 base::Bind(&PopularSites::OnJsonParseFailed, | 376 base::Bind(&PopularSites::OnJsonParseFailed, |
| 332 weak_ptr_factory_.GetWeakPtr())); | 377 weak_ptr_factory_.GetWeakPtr())); |
| 333 } | 378 } |
| 334 | 379 |
| 335 void PopularSites::OnJsonParsed(std::unique_ptr<base::Value> json) { | 380 void PopularSites::OnJsonParsed(std::unique_ptr<base::Value> json) { |
| 336 const base::Value* json_ptr = json.get(); | 381 const base::Value* json_ptr = json.get(); |
| 337 base::PostTaskAndReplyWithResult( | 382 base::PostTaskAndReplyWithResult( |
| 338 blocking_runner_.get(), FROM_HERE, | 383 blocking_runner_.get(), FROM_HERE, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 DLOG(WARNING) << "Download country site list failed"; | 445 DLOG(WARNING) << "Download country site list failed"; |
| 401 is_fallback_ = true; | 446 is_fallback_ = true; |
| 402 pending_country_ = kPopularSitesDefaultCountryCode; | 447 pending_country_ = kPopularSitesDefaultCountryCode; |
| 403 pending_version_ = kPopularSitesDefaultVersion; | 448 pending_version_ = kPopularSitesDefaultVersion; |
| 404 FetchPopularSites(GetPopularSitesURL()); | 449 FetchPopularSites(GetPopularSitesURL()); |
| 405 } else { | 450 } else { |
| 406 DLOG(WARNING) << "Download fallback site list failed"; | 451 DLOG(WARNING) << "Download fallback site list failed"; |
| 407 callback_.Run(false); | 452 callback_.Run(false); |
| 408 } | 453 } |
| 409 } | 454 } |
| OLD | NEW |