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

Side by Side Diff: components/doodle/doodle_fetcher_impl.cc

Issue 2710003006: [Doodle] Introduce a DoodleService (Closed)
Patch Set: comment Created 3 years, 9 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 | « components/doodle/doodle_fetcher_impl.h ('k') | components/doodle/doodle_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/doodle/doodle_fetcher_impl.h" 5 #include "components/doodle/doodle_fetcher_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/time/clock.h"
11 #include "base/time/default_clock.h" 10 #include "base/time/default_clock.h"
12 #include "base/time/time.h" 11 #include "base/time/time.h"
13 #include "base/values.h" 12 #include "base/values.h"
14 #include "components/data_use_measurement/core/data_use_user_data.h" 13 #include "components/data_use_measurement/core/data_use_user_data.h"
15 #include "components/google/core/browser/google_url_tracker.h" 14 #include "components/google/core/browser/google_url_tracker.h"
16 #include "components/google/core/browser/google_util.h" 15 #include "components/google/core/browser/google_util.h"
17 #include "net/base/load_flags.h" 16 #include "net/base/load_flags.h"
18 #include "net/http/http_status_code.h" 17 #include "net/http/http_status_code.h"
19 #include "net/url_request/url_fetcher.h" 18 #include "net/url_request/url_fetcher.h"
20 19
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 base::Bind(&DoodleFetcherImpl::OnJsonParsed, 122 base::Bind(&DoodleFetcherImpl::OnJsonParsed,
124 weak_ptr_factory_.GetWeakPtr()), 123 weak_ptr_factory_.GetWeakPtr()),
125 base::Bind(&DoodleFetcherImpl::OnJsonParseFailed, 124 base::Bind(&DoodleFetcherImpl::OnJsonParseFailed,
126 weak_ptr_factory_.GetWeakPtr())); 125 weak_ptr_factory_.GetWeakPtr()));
127 } 126 }
128 127
129 void DoodleFetcherImpl::OnJsonParsed(std::unique_ptr<base::Value> json) { 128 void DoodleFetcherImpl::OnJsonParsed(std::unique_ptr<base::Value> json) {
130 std::unique_ptr<base::DictionaryValue> config = 129 std::unique_ptr<base::DictionaryValue> config =
131 base::DictionaryValue::From(std::move(json)); 130 base::DictionaryValue::From(std::move(json));
132 if (!config.get()) { 131 if (!config.get()) {
133 DLOG(WARNING) << "Doodle JSON is not valid"; 132 DLOG(WARNING) << "Doodle JSON is not valid.";
134 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); 133 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt);
135 return; 134 return;
136 } 135 }
137 136
138 const base::DictionaryValue* ddljson = nullptr; 137 const base::DictionaryValue* ddljson = nullptr;
139 if (!config->GetDictionary("ddljson", &ddljson)) { 138 if (!config->GetDictionary("ddljson", &ddljson)) {
140 DLOG(WARNING) << "Doodle JSON reponse did not contain 'ddljson' key."; 139 DLOG(WARNING) << "Doodle JSON response did not contain 'ddljson' key.";
141 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); 140 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt);
142 return; 141 return;
143 } 142 }
144 143
145 base::Optional<DoodleConfig> doodle = ParseDoodle(*ddljson); 144 base::Optional<DoodleConfig> doodle = ParseDoodle(*ddljson);
146 if (!doodle.has_value()) { 145 if (!doodle.has_value()) {
147 RespondToAllCallbacks(DoodleState::NO_DOODLE, base::nullopt); 146 RespondToAllCallbacks(DoodleState::NO_DOODLE, base::nullopt);
148 return; 147 return;
149 } 148 }
150 149
151 RespondToAllCallbacks(DoodleState::AVAILABLE, std::move(doodle)); 150 RespondToAllCallbacks(DoodleState::AVAILABLE, std::move(doodle));
152 } 151 }
153 152
154 void DoodleFetcherImpl::OnJsonParseFailed(const std::string& error_message) { 153 void DoodleFetcherImpl::OnJsonParseFailed(const std::string& error_message) {
155 DLOG(WARNING) << "JSON parsing failed: " << error_message; 154 DLOG(WARNING) << "JSON parsing failed: " << error_message;
156 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); 155 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt);
157 } 156 }
158 157
159 base::Optional<DoodleConfig> DoodleFetcherImpl::ParseDoodle( 158 base::Optional<DoodleConfig> DoodleFetcherImpl::ParseDoodle(
160 const base::DictionaryValue& ddljson) const { 159 const base::DictionaryValue& ddljson) const {
161 DoodleConfig doodle; 160 DoodleConfig doodle;
161 // The |large_image| field is required (it's the "default" representation for
162 // the doodle).
162 if (!ParseImage(ddljson, "large_image", &doodle.large_image)) { 163 if (!ParseImage(ddljson, "large_image", &doodle.large_image)) {
163 return base::nullopt; 164 return base::nullopt;
164 } 165 }
165 ParseImage(ddljson, "transparent_large_image", 166 ParseImage(ddljson, "transparent_large_image",
166 &doodle.transparent_large_image); 167 &doodle.transparent_large_image);
167 ParseImage(ddljson, "large_cta_image", &doodle.large_cta_image); 168 ParseImage(ddljson, "large_cta_image", &doodle.large_cta_image);
168 ParseBaseInformation(ddljson, &doodle); 169 ParseBaseInformation(ddljson, &doodle);
169 return doodle; 170 return doodle;
170 } 171 }
171 172
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 237
237 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { 238 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const {
238 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); 239 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL();
239 if (cmd_line_url.is_valid()) { 240 if (cmd_line_url.is_valid()) {
240 return cmd_line_url; 241 return cmd_line_url;
241 } 242 }
242 return google_url_tracker_->google_url(); 243 return google_url_tracker_->google_url();
243 } 244 }
244 245
245 } // namespace doodle 246 } // namespace doodle
OLDNEW
« no previous file with comments | « components/doodle/doodle_fetcher_impl.h ('k') | components/doodle/doodle_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698