| 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 = |
| 147 ParseDoodleConfigAndTimeToLive(*ddljson, &time_to_live); |
| 143 if (!doodle.has_value()) { | 148 if (!doodle.has_value()) { |
| 144 RespondToAllCallbacks(DoodleState::NO_DOODLE, base::nullopt); | 149 RespondToAllCallbacks(DoodleState::NO_DOODLE, base::TimeDelta(), |
| 150 base::nullopt); |
| 145 return; | 151 return; |
| 146 } | 152 } |
| 147 | 153 |
| 148 RespondToAllCallbacks(DoodleState::AVAILABLE, std::move(doodle)); | 154 RespondToAllCallbacks(DoodleState::AVAILABLE, time_to_live, |
| 155 std::move(doodle)); |
| 149 } | 156 } |
| 150 | 157 |
| 151 void DoodleFetcherImpl::OnJsonParseFailed(const std::string& error_message) { | 158 void DoodleFetcherImpl::OnJsonParseFailed(const std::string& error_message) { |
| 152 DLOG(WARNING) << "JSON parsing failed: " << error_message; | 159 DLOG(WARNING) << "JSON parsing failed: " << error_message; |
| 153 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); | 160 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::TimeDelta(), |
| 161 base::nullopt); |
| 154 } | 162 } |
| 155 | 163 |
| 156 base::Optional<DoodleConfig> DoodleFetcherImpl::ParseDoodle( | 164 base::Optional<DoodleConfig> DoodleFetcherImpl::ParseDoodleConfigAndTimeToLive( |
| 157 const base::DictionaryValue& ddljson) const { | 165 const base::DictionaryValue& ddljson, |
| 166 base::TimeDelta* time_to_live) const { |
| 158 DoodleConfig doodle; | 167 DoodleConfig doodle; |
| 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 } |
| 173 ParseBaseInformation(ddljson, &doodle, time_to_live); |
| 164 ParseImage(ddljson, "transparent_large_image", | 174 ParseImage(ddljson, "transparent_large_image", |
| 165 &doodle.transparent_large_image); | 175 &doodle.transparent_large_image); |
| 166 ParseImage(ddljson, "large_cta_image", &doodle.large_cta_image); | 176 ParseImage(ddljson, "large_cta_image", &doodle.large_cta_image); |
| 167 ParseBaseInformation(ddljson, &doodle); | |
| 168 return doodle; | 177 return doodle; |
| 169 } | 178 } |
| 170 | 179 |
| 171 bool DoodleFetcherImpl::ParseImage(const base::DictionaryValue& image_parent, | 180 bool DoodleFetcherImpl::ParseImage(const base::DictionaryValue& image_parent, |
| 172 const std::string& image_name, | 181 const std::string& image_name, |
| 173 DoodleImage* image) const { | 182 DoodleImage* image) const { |
| 174 DCHECK(image); | 183 DCHECK(image); |
| 175 const base::DictionaryValue* image_dict = nullptr; | 184 const base::DictionaryValue* image_dict = nullptr; |
| 176 if (!image_parent.GetDictionary(image_name, &image_dict)) { | 185 if (!image_parent.GetDictionary(image_name, &image_dict)) { |
| 177 return false; | 186 return false; |
| 178 } | 187 } |
| 179 image->url = ParseRelativeUrl(*image_dict, "url"); | 188 image->url = ParseRelativeUrl(*image_dict, "url"); |
| 180 if (!image->url.is_valid()) { | 189 if (!image->url.is_valid()) { |
| 181 DLOG(WARNING) << "Image URL for \"" << image_name << "\" is invalid."; | 190 DLOG(WARNING) << "Image URL for \"" << image_name << "\" is invalid."; |
| 182 return false; | 191 return false; |
| 183 } | 192 } |
| 184 image_dict->GetInteger("height", &image->height); | 193 image_dict->GetInteger("height", &image->height); |
| 185 image_dict->GetInteger("width", &image->width); | 194 image_dict->GetInteger("width", &image->width); |
| 186 image_dict->GetBoolean("is_animated_gif", &image->is_animated_gif); | 195 image_dict->GetBoolean("is_animated_gif", &image->is_animated_gif); |
| 187 image_dict->GetBoolean("is_cta", &image->is_cta); | 196 image_dict->GetBoolean("is_cta", &image->is_cta); |
| 188 return true; | 197 return true; |
| 189 } | 198 } |
| 190 | 199 |
| 191 void DoodleFetcherImpl::ParseBaseInformation( | 200 void DoodleFetcherImpl::ParseBaseInformation( |
| 192 const base::DictionaryValue& ddljson, | 201 const base::DictionaryValue& ddljson, |
| 193 DoodleConfig* config) const { | 202 DoodleConfig* config, |
| 203 base::TimeDelta* time_to_live) const { |
| 194 config->search_url = ParseRelativeUrl(ddljson, "search_url"); | 204 config->search_url = ParseRelativeUrl(ddljson, "search_url"); |
| 195 config->target_url = ParseRelativeUrl(ddljson, "target_url"); | 205 config->target_url = ParseRelativeUrl(ddljson, "target_url"); |
| 196 config->fullpage_interactive_url = | 206 config->fullpage_interactive_url = |
| 197 ParseRelativeUrl(ddljson, "fullpage_interactive_url"); | 207 ParseRelativeUrl(ddljson, "fullpage_interactive_url"); |
| 198 | 208 |
| 199 config->doodle_type = ParseDoodleType(ddljson); | 209 config->doodle_type = ParseDoodleType(ddljson); |
| 200 ddljson.GetString("alt_text", &config->alt_text); | 210 ddljson.GetString("alt_text", &config->alt_text); |
| 201 ddljson.GetString("interactive_html", &config->interactive_html); | 211 ddljson.GetString("interactive_html", &config->interactive_html); |
| 202 | 212 |
| 203 // The JSON doesn't guarantee the number to fit into an int. | 213 // The JSON doesn't guarantee the number to fit into an int. |
| 204 double ttl = 0; // Expires immediately if the parameter is missing. | 214 double ttl = 0; // Expires immediately if the parameter is missing. |
| 205 if (!ddljson.GetDouble("time_to_live_ms", &ttl) || ttl < 0) { | 215 if (!ddljson.GetDouble("time_to_live_ms", &ttl) || ttl < 0) { |
| 206 DLOG(WARNING) << "No valid Doodle TTL present in ddljson!"; | 216 DLOG(WARNING) << "No valid Doodle TTL present in ddljson!"; |
| 207 ttl = 0; | 217 ttl = 0; |
| 208 } | 218 } |
| 209 // TODO(treib,fhorschig): Move this logic into the service. | 219 // TODO(treib,fhorschig): Move this logic into the service. |
| 210 if (ttl > kMaxTimeToLiveMS) { | 220 if (ttl > kMaxTimeToLiveMS) { |
| 211 ttl = kMaxTimeToLiveMS; | 221 ttl = kMaxTimeToLiveMS; |
| 212 DLOG(WARNING) << "Clamping Doodle TTL to 30 days!"; | 222 DLOG(WARNING) << "Clamping Doodle TTL to 30 days!"; |
| 213 } | 223 } |
| 214 config->time_to_live = base::TimeDelta::FromMillisecondsD(ttl); | 224 *time_to_live = base::TimeDelta::FromMillisecondsD(ttl); |
| 215 } | 225 } |
| 216 | 226 |
| 217 GURL DoodleFetcherImpl::ParseRelativeUrl( | 227 GURL DoodleFetcherImpl::ParseRelativeUrl( |
| 218 const base::DictionaryValue& dict_value, | 228 const base::DictionaryValue& dict_value, |
| 219 const std::string& key) const { | 229 const std::string& key) const { |
| 220 std::string str_url; | 230 std::string str_url; |
| 221 dict_value.GetString(key, &str_url); | 231 dict_value.GetString(key, &str_url); |
| 222 if (str_url.empty()) { | 232 if (str_url.empty()) { |
| 223 return GURL(); | 233 return GURL(); |
| 224 } | 234 } |
| 225 return GetGoogleBaseUrl().Resolve(str_url); | 235 return GetGoogleBaseUrl().Resolve(str_url); |
| 226 } | 236 } |
| 227 | 237 |
| 228 void DoodleFetcherImpl::RespondToAllCallbacks( | 238 void DoodleFetcherImpl::RespondToAllCallbacks( |
| 229 DoodleState state, | 239 DoodleState state, |
| 240 base::TimeDelta time_to_live, |
| 230 const base::Optional<DoodleConfig>& config) { | 241 const base::Optional<DoodleConfig>& config) { |
| 231 for (auto& callback : callbacks_) { | 242 for (auto& callback : callbacks_) { |
| 232 std::move(callback).Run(state, config); | 243 std::move(callback).Run(state, time_to_live, config); |
| 233 } | 244 } |
| 234 callbacks_.clear(); | 245 callbacks_.clear(); |
| 235 } | 246 } |
| 236 | 247 |
| 237 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { | 248 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { |
| 238 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); | 249 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); |
| 239 if (cmd_line_url.is_valid()) { | 250 if (cmd_line_url.is_valid()) { |
| 240 return cmd_line_url; | 251 return cmd_line_url; |
| 241 } | 252 } |
| 242 return google_url_tracker_->google_url(); | 253 return google_url_tracker_->google_url(); |
| 243 } | 254 } |
| 244 | 255 |
| 245 } // namespace doodle | 256 } // namespace doodle |
| OLD | NEW |