Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/time.h" | 10 #include "base/time/time.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 } | 105 } |
| 106 | 106 |
| 107 void DoodleFetcherImpl::OnURLFetchComplete(const URLFetcher* source) { | 107 void DoodleFetcherImpl::OnURLFetchComplete(const URLFetcher* source) { |
| 108 DCHECK_EQ(fetcher_.get(), source); | 108 DCHECK_EQ(fetcher_.get(), source); |
| 109 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); | 109 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); |
| 110 | 110 |
| 111 std::string json_string; | 111 std::string json_string; |
| 112 if (!(source->GetStatus().is_success() && | 112 if (!(source->GetStatus().is_success() && |
| 113 source->GetResponseCode() == net::HTTP_OK && | 113 source->GetResponseCode() == net::HTTP_OK && |
| 114 source->GetResponseAsString(&json_string))) { | 114 source->GetResponseAsString(&json_string))) { |
| 115 RespondToAllCallbacks(DoodleState::DOWNLOAD_ERROR, base::nullopt); | 115 RespondToAllCallbacks(DoodleState::DOWNLOAD_ERROR, base::TimeDelta(), |
| 116 base::nullopt); | |
| 116 return; | 117 return; |
| 117 } | 118 } |
| 118 | 119 |
| 119 json_parsing_callback_.Run(StripSafetyPreamble(std::move(json_string)), | 120 json_parsing_callback_.Run(StripSafetyPreamble(std::move(json_string)), |
| 120 base::Bind(&DoodleFetcherImpl::OnJsonParsed, | 121 base::Bind(&DoodleFetcherImpl::OnJsonParsed, |
| 121 weak_ptr_factory_.GetWeakPtr()), | 122 weak_ptr_factory_.GetWeakPtr()), |
| 122 base::Bind(&DoodleFetcherImpl::OnJsonParseFailed, | 123 base::Bind(&DoodleFetcherImpl::OnJsonParseFailed, |
| 123 weak_ptr_factory_.GetWeakPtr())); | 124 weak_ptr_factory_.GetWeakPtr())); |
| 124 } | 125 } |
| 125 | 126 |
| 126 void DoodleFetcherImpl::OnJsonParsed(std::unique_ptr<base::Value> json) { | 127 void DoodleFetcherImpl::OnJsonParsed(std::unique_ptr<base::Value> json) { |
| 127 std::unique_ptr<base::DictionaryValue> config = | 128 std::unique_ptr<base::DictionaryValue> config = |
| 128 base::DictionaryValue::From(std::move(json)); | 129 base::DictionaryValue::From(std::move(json)); |
| 129 if (!config.get()) { | 130 if (!config.get()) { |
| 130 DLOG(WARNING) << "Doodle JSON is not valid."; | 131 DLOG(WARNING) << "Doodle JSON is not valid."; |
| 131 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); | 132 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::TimeDelta(), |
| 133 base::nullopt); | |
| 132 return; | 134 return; |
| 133 } | 135 } |
| 134 | 136 |
| 135 const base::DictionaryValue* ddljson = nullptr; | 137 const base::DictionaryValue* ddljson = nullptr; |
| 136 if (!config->GetDictionary("ddljson", &ddljson)) { | 138 if (!config->GetDictionary("ddljson", &ddljson)) { |
| 137 DLOG(WARNING) << "Doodle JSON response did not contain 'ddljson' key."; | 139 DLOG(WARNING) << "Doodle JSON response did not contain 'ddljson' key."; |
| 138 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); | 140 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::TimeDelta(), |
| 141 base::nullopt); | |
| 139 return; | 142 return; |
| 140 } | 143 } |
| 141 | 144 |
| 142 base::Optional<DoodleConfig> doodle = ParseDoodle(*ddljson); | 145 base::TimeDelta time_to_live; |
| 146 base::Optional<DoodleConfig> doodle = ParseDoodle(*ddljson, &time_to_live); | |
| 143 if (!doodle.has_value()) { | 147 if (!doodle.has_value()) { |
| 144 RespondToAllCallbacks(DoodleState::NO_DOODLE, base::nullopt); | 148 RespondToAllCallbacks(DoodleState::NO_DOODLE, base::TimeDelta(), |
| 149 base::nullopt); | |
| 145 return; | 150 return; |
| 146 } | 151 } |
| 147 | 152 |
| 148 RespondToAllCallbacks(DoodleState::AVAILABLE, std::move(doodle)); | 153 RespondToAllCallbacks(DoodleState::AVAILABLE, time_to_live, |
| 154 std::move(doodle)); | |
| 149 } | 155 } |
| 150 | 156 |
| 151 void DoodleFetcherImpl::OnJsonParseFailed(const std::string& error_message) { | 157 void DoodleFetcherImpl::OnJsonParseFailed(const std::string& error_message) { |
| 152 DLOG(WARNING) << "JSON parsing failed: " << error_message; | 158 DLOG(WARNING) << "JSON parsing failed: " << error_message; |
| 153 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); | 159 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::TimeDelta(), |
| 160 base::nullopt); | |
| 154 } | 161 } |
| 155 | 162 |
| 156 base::Optional<DoodleConfig> DoodleFetcherImpl::ParseDoodle( | 163 base::Optional<DoodleConfig> DoodleFetcherImpl::ParseDoodle( |
| 157 const base::DictionaryValue& ddljson) const { | 164 const base::DictionaryValue& ddljson, |
| 165 base::TimeDelta* time_to_live) const { | |
| 158 DoodleConfig doodle; | 166 DoodleConfig doodle; |
| 167 ParseBaseInformation(ddljson, &doodle, time_to_live); | |
|
fhorschig
2017/03/02 13:18:08
Is there a good reason to pull this up?
If ParseIm
Marc Treib
2017/03/02 14:30:08
Not really, it just seemed a bit weird to me that
| |
| 159 // The |large_image| field is required (it's the "default" representation for | 168 // The |large_image| field is required (it's the "default" representation for |
| 160 // the doodle). | 169 // the doodle). |
| 161 if (!ParseImage(ddljson, "large_image", &doodle.large_image)) { | 170 if (!ParseImage(ddljson, "large_image", &doodle.large_image)) { |
| 162 return base::nullopt; | 171 return base::nullopt; |
| 163 } | 172 } |
| 164 ParseImage(ddljson, "transparent_large_image", | 173 ParseImage(ddljson, "transparent_large_image", |
| 165 &doodle.transparent_large_image); | 174 &doodle.transparent_large_image); |
| 166 ParseImage(ddljson, "large_cta_image", &doodle.large_cta_image); | 175 ParseImage(ddljson, "large_cta_image", &doodle.large_cta_image); |
| 167 ParseBaseInformation(ddljson, &doodle); | |
| 168 return doodle; | 176 return doodle; |
| 169 } | 177 } |
| 170 | 178 |
| 171 bool DoodleFetcherImpl::ParseImage(const base::DictionaryValue& image_parent, | 179 bool DoodleFetcherImpl::ParseImage(const base::DictionaryValue& image_parent, |
| 172 const std::string& image_name, | 180 const std::string& image_name, |
| 173 DoodleImage* image) const { | 181 DoodleImage* image) const { |
| 174 DCHECK(image); | 182 DCHECK(image); |
| 175 const base::DictionaryValue* image_dict = nullptr; | 183 const base::DictionaryValue* image_dict = nullptr; |
| 176 if (!image_parent.GetDictionary(image_name, &image_dict)) { | 184 if (!image_parent.GetDictionary(image_name, &image_dict)) { |
| 177 return false; | 185 return false; |
| 178 } | 186 } |
| 179 image->url = ParseRelativeUrl(*image_dict, "url"); | 187 image->url = ParseRelativeUrl(*image_dict, "url"); |
| 180 if (!image->url.is_valid()) { | 188 if (!image->url.is_valid()) { |
| 181 DLOG(WARNING) << "Image URL for \"" << image_name << "\" is invalid."; | 189 DLOG(WARNING) << "Image URL for \"" << image_name << "\" is invalid."; |
| 182 return false; | 190 return false; |
| 183 } | 191 } |
| 184 image_dict->GetInteger("height", &image->height); | 192 image_dict->GetInteger("height", &image->height); |
| 185 image_dict->GetInteger("width", &image->width); | 193 image_dict->GetInteger("width", &image->width); |
| 186 image_dict->GetBoolean("is_animated_gif", &image->is_animated_gif); | 194 image_dict->GetBoolean("is_animated_gif", &image->is_animated_gif); |
| 187 image_dict->GetBoolean("is_cta", &image->is_cta); | 195 image_dict->GetBoolean("is_cta", &image->is_cta); |
| 188 return true; | 196 return true; |
| 189 } | 197 } |
| 190 | 198 |
| 191 void DoodleFetcherImpl::ParseBaseInformation( | 199 void DoodleFetcherImpl::ParseBaseInformation( |
| 192 const base::DictionaryValue& ddljson, | 200 const base::DictionaryValue& ddljson, |
| 193 DoodleConfig* config) const { | 201 DoodleConfig* config, |
| 202 base::TimeDelta* time_to_live) const { | |
| 194 config->search_url = ParseRelativeUrl(ddljson, "search_url"); | 203 config->search_url = ParseRelativeUrl(ddljson, "search_url"); |
| 195 config->target_url = ParseRelativeUrl(ddljson, "target_url"); | 204 config->target_url = ParseRelativeUrl(ddljson, "target_url"); |
| 196 config->fullpage_interactive_url = | 205 config->fullpage_interactive_url = |
| 197 ParseRelativeUrl(ddljson, "fullpage_interactive_url"); | 206 ParseRelativeUrl(ddljson, "fullpage_interactive_url"); |
| 198 | 207 |
| 199 config->doodle_type = ParseDoodleType(ddljson); | 208 config->doodle_type = ParseDoodleType(ddljson); |
| 200 ddljson.GetString("alt_text", &config->alt_text); | 209 ddljson.GetString("alt_text", &config->alt_text); |
| 201 ddljson.GetString("interactive_html", &config->interactive_html); | 210 ddljson.GetString("interactive_html", &config->interactive_html); |
| 202 | 211 |
| 203 // The JSON doesn't guarantee the number to fit into an int. | 212 // The JSON doesn't guarantee the number to fit into an int. |
| 204 double ttl = 0; // Expires immediately if the parameter is missing. | 213 double ttl = 0; // Expires immediately if the parameter is missing. |
| 205 if (!ddljson.GetDouble("time_to_live_ms", &ttl) || ttl < 0) { | 214 if (!ddljson.GetDouble("time_to_live_ms", &ttl) || ttl < 0) { |
| 206 DLOG(WARNING) << "No valid Doodle TTL present in ddljson!"; | 215 DLOG(WARNING) << "No valid Doodle TTL present in ddljson!"; |
| 207 ttl = 0; | 216 ttl = 0; |
| 208 } | 217 } |
| 209 // TODO(treib,fhorschig): Move this logic into the service. | 218 // TODO(treib,fhorschig): Move this logic into the service. |
| 210 if (ttl > kMaxTimeToLiveMS) { | 219 if (ttl > kMaxTimeToLiveMS) { |
| 211 ttl = kMaxTimeToLiveMS; | 220 ttl = kMaxTimeToLiveMS; |
| 212 DLOG(WARNING) << "Clamping Doodle TTL to 30 days!"; | 221 DLOG(WARNING) << "Clamping Doodle TTL to 30 days!"; |
| 213 } | 222 } |
| 214 config->time_to_live = base::TimeDelta::FromMillisecondsD(ttl); | 223 *time_to_live = base::TimeDelta::FromMillisecondsD(ttl); |
| 215 } | 224 } |
| 216 | 225 |
| 217 GURL DoodleFetcherImpl::ParseRelativeUrl( | 226 GURL DoodleFetcherImpl::ParseRelativeUrl( |
| 218 const base::DictionaryValue& dict_value, | 227 const base::DictionaryValue& dict_value, |
| 219 const std::string& key) const { | 228 const std::string& key) const { |
| 220 std::string str_url; | 229 std::string str_url; |
| 221 dict_value.GetString(key, &str_url); | 230 dict_value.GetString(key, &str_url); |
| 222 if (str_url.empty()) { | 231 if (str_url.empty()) { |
| 223 return GURL(); | 232 return GURL(); |
| 224 } | 233 } |
| 225 return GetGoogleBaseUrl().Resolve(str_url); | 234 return GetGoogleBaseUrl().Resolve(str_url); |
| 226 } | 235 } |
| 227 | 236 |
| 228 void DoodleFetcherImpl::RespondToAllCallbacks( | 237 void DoodleFetcherImpl::RespondToAllCallbacks( |
| 229 DoodleState state, | 238 DoodleState state, |
| 239 base::TimeDelta time_to_live, | |
| 230 const base::Optional<DoodleConfig>& config) { | 240 const base::Optional<DoodleConfig>& config) { |
| 231 for (auto& callback : callbacks_) { | 241 for (auto& callback : callbacks_) { |
| 232 std::move(callback).Run(state, config); | 242 std::move(callback).Run(state, time_to_live, config); |
| 233 } | 243 } |
| 234 callbacks_.clear(); | 244 callbacks_.clear(); |
| 235 } | 245 } |
| 236 | 246 |
| 237 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { | 247 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { |
| 238 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); | 248 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); |
| 239 if (cmd_line_url.is_valid()) { | 249 if (cmd_line_url.is_valid()) { |
| 240 return cmd_line_url; | 250 return cmd_line_url; |
| 241 } | 251 } |
| 242 return google_url_tracker_->google_url(); | 252 return google_url_tracker_->google_url(); |
| 243 } | 253 } |
| 244 | 254 |
| 245 } // namespace doodle | 255 } // namespace doodle |
| OLD | NEW |