Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(565)

Side by Side Diff: chrome/browser/android/ntp/popular_sites.cc

Issue 2045563002: Parse PopularSites JSON on iOS too. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #if !defined(OS_IOS)
Marc Treib 2016/06/07 09:03:07 This should go in a separate block, below the non-
sfiera 2016/06/07 12:43:52 Done.
28 #include "components/safe_json/safe_json_parser.h" 29 #include "components/safe_json/safe_json_parser.h"
30 #endif
29 #include "components/search_engines/search_engine_type.h" 31 #include "components/search_engines/search_engine_type.h"
30 #include "components/search_engines/template_url_prepopulate_data.h" 32 #include "components/search_engines/template_url_prepopulate_data.h"
31 #include "components/search_engines/template_url_service.h" 33 #include "components/search_engines/template_url_service.h"
32 #include "components/variations/service/variations_service.h" 34 #include "components/variations/service/variations_service.h"
33 #include "net/base/load_flags.h" 35 #include "net/base/load_flags.h"
34 #include "net/http/http_status_code.h" 36 #include "net/http/http_status_code.h"
35 37
36 using net::URLFetcher; 38 using net::URLFetcher;
37 using variations::VariationsService; 39 using variations::VariationsService;
38 40
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 131
130 // Must run on the blocking thread pool. 132 // Must run on the blocking thread pool.
131 bool WriteJsonToFile(const base::FilePath& local_path, 133 bool WriteJsonToFile(const base::FilePath& local_path,
132 const base::Value* json) { 134 const base::Value* json) {
133 std::string json_string; 135 std::string json_string;
134 return base::JSONWriter::Write(*json, &json_string) && 136 return base::JSONWriter::Write(*json, &json_string) &&
135 base::ImportantFileWriter::WriteFileAtomically(local_path, 137 base::ImportantFileWriter::WriteFileAtomically(local_path,
136 json_string); 138 json_string);
137 } 139 }
138 140
141 #if defined(OS_IOS)
142 // Mimics SafeJsonParser API, but parses unsafely for iOS.
143 class JsonUnsafeParser {
144 public:
145 using SuccessCallback = base::Callback<void(std::unique_ptr<base::Value>)>;
146 using ErrorCallback = base::Callback<void(const std::string&)>;
147
148 // As with SafeJsonParser, runs either success_callback or error_callback on
149 // the calling thread, but not before the call returns.
150 static void Parse(const std::string& unsafe_json,
151 const SuccessCallback& success_callback,
152 const ErrorCallback& error_callback) {
153 base::ThreadTaskRunnerHandle::Get()->PostTask(
154 FROM_HERE,
155 base::Bind(DoParse, unsafe_json, success_callback, error_callback));
156 }
157
158 JsonUnsafeParser() = delete;
159
160 private:
161 static void DoParse(const std::string& unsafe_json,
162 const SuccessCallback& success_callback,
163 const ErrorCallback& error_callback) {
164 auto value =
Marc Treib 2016/06/07 09:03:07 Use the actual type please
sfiera 2016/06/07 12:43:52 Done.
165 base::JSONReader::Read(unsafe_json, base::JSON_ALLOW_TRAILING_COMMAS);
166 if (value) {
167 success_callback.Run(std::move(value));
168 } else {
169 error_callback.Run("JSON parsing failed");
Marc Treib 2016/06/07 09:03:07 JSONReader has a ReadAndReturnError, maybe use tha
sfiera 2016/06/07 12:43:52 Done.
170 }
171 }
172 };
173
174 using UntrustedJsonParser = JsonUnsafeParser;
175 #else
176 using UntrustedJsonParser = safe_json::SafeJsonParser;
177 #endif
178
139 } // namespace 179 } // namespace
140 180
141 base::FilePath ChromePopularSites::GetDirectory() { 181 base::FilePath ChromePopularSites::GetDirectory() {
142 base::FilePath dir; 182 base::FilePath dir;
143 PathService::Get(chrome::DIR_USER_DATA, &dir); 183 PathService::Get(chrome::DIR_USER_DATA, &dir);
144 return dir; // empty if PathService::Get() failed. 184 return dir; // empty if PathService::Get() failed.
145 } 185 }
146 186
147 PopularSites::Site::Site(const base::string16& title, 187 PopularSites::Site::Site(const base::string16& title,
148 const GURL& url, 188 const GURL& url,
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); 358 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_);
319 359
320 std::string json_string; 360 std::string json_string;
321 if (!(source->GetStatus().is_success() && 361 if (!(source->GetStatus().is_success() &&
322 source->GetResponseCode() == net::HTTP_OK && 362 source->GetResponseCode() == net::HTTP_OK &&
323 source->GetResponseAsString(&json_string))) { 363 source->GetResponseAsString(&json_string))) {
324 OnDownloadFailed(); 364 OnDownloadFailed();
325 return; 365 return;
326 } 366 }
327 367
328 safe_json::SafeJsonParser::Parse( 368 UntrustedJsonParser::Parse(
329 json_string, 369 json_string,
330 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), 370 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()),
331 base::Bind(&PopularSites::OnJsonParseFailed, 371 base::Bind(&PopularSites::OnJsonParseFailed,
332 weak_ptr_factory_.GetWeakPtr())); 372 weak_ptr_factory_.GetWeakPtr()));
333 } 373 }
334 374
335 void PopularSites::OnJsonParsed(std::unique_ptr<base::Value> json) { 375 void PopularSites::OnJsonParsed(std::unique_ptr<base::Value> json) {
336 const base::Value* json_ptr = json.get(); 376 const base::Value* json_ptr = json.get();
337 base::PostTaskAndReplyWithResult( 377 base::PostTaskAndReplyWithResult(
338 blocking_runner_.get(), FROM_HERE, 378 blocking_runner_.get(), FROM_HERE,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 DLOG(WARNING) << "Download country site list failed"; 440 DLOG(WARNING) << "Download country site list failed";
401 is_fallback_ = true; 441 is_fallback_ = true;
402 pending_country_ = kPopularSitesDefaultCountryCode; 442 pending_country_ = kPopularSitesDefaultCountryCode;
403 pending_version_ = kPopularSitesDefaultVersion; 443 pending_version_ = kPopularSitesDefaultVersion;
404 FetchPopularSites(GetPopularSitesURL()); 444 FetchPopularSites(GetPopularSitesURL());
405 } else { 445 } else {
406 DLOG(WARNING) << "Download fallback site list failed"; 446 DLOG(WARNING) << "Download fallback site list failed";
407 callback_.Run(false); 447 callback_.Run(false);
408 } 448 }
409 } 449 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698