| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/doodle/doodle_fetcher.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/time/clock.h" |
| 11 #include "base/time/default_clock.h" |
| 12 #include "base/time/time.h" |
| 13 #include "base/values.h" |
| 14 #include "components/google/core/browser/google_url_tracker.h" |
| 15 #include "components/google/core/browser/google_util.h" |
| 16 #include "net/base/load_flags.h" |
| 17 #include "net/http/http_status_code.h" |
| 18 #include "net/url_request/url_fetcher.h" |
| 19 |
| 20 using net::URLFetcher; |
| 21 |
| 22 namespace doodle { |
| 23 |
| 24 namespace { |
| 25 |
| 26 const double kMaxTimeToLiveMS = 30.0 * 24 * 60 * 60 * 1000; // 30 days |
| 27 |
| 28 const char kDoodleConfigPath[] = "/async/ddljson"; |
| 29 |
| 30 std::string StripSafetyPreamble(const std::string& json) { |
| 31 // The response may start with )]}'. Ignore this. |
| 32 const char kResponsePreamble[] = ")]}'"; |
| 33 |
| 34 base::StringPiece json_sp(json); |
| 35 if (json_sp.starts_with(kResponsePreamble)) { |
| 36 json_sp.remove_prefix(strlen(kResponsePreamble)); |
| 37 } |
| 38 |
| 39 return json_sp.as_string(); |
| 40 } |
| 41 |
| 42 DoodleType ParseDoodleType(const base::DictionaryValue& ddljson) { |
| 43 std::string type_str; |
| 44 ddljson.GetString("doodle_type", &type_str); |
| 45 if (type_str == "SIMPLE") { |
| 46 return DoodleType::SIMPLE; |
| 47 } |
| 48 if (type_str == "RANDOM") { |
| 49 return DoodleType::RANDOM; |
| 50 } |
| 51 if (type_str == "VIDEO") { |
| 52 return DoodleType::VIDEO; |
| 53 } |
| 54 if (type_str == "INTERACTIVE") { |
| 55 return DoodleType::INTERACTIVE; |
| 56 } |
| 57 if (type_str == "INLINE_INTERACTIVE") { |
| 58 return DoodleType::INLINE_INTERACTIVE; |
| 59 } |
| 60 if (type_str == "SLIDESHOW") { |
| 61 return DoodleType::SLIDESHOW; |
| 62 } |
| 63 return DoodleType::UNKNOWN; |
| 64 } |
| 65 |
| 66 } // namespace |
| 67 |
| 68 DoodleImage::DoodleImage() |
| 69 : height(0), width(0), is_animated_gif(false), is_cta(false) {} |
| 70 DoodleImage::~DoodleImage() = default; |
| 71 |
| 72 DoodleConfig::DoodleConfig() : doodle_type(DoodleType::UNKNOWN) {} |
| 73 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default; |
| 74 DoodleConfig::~DoodleConfig() = default; |
| 75 |
| 76 DoodleFetcher::DoodleFetcher( |
| 77 scoped_refptr<net::URLRequestContextGetter> download_context, |
| 78 GoogleURLTracker* google_url_tracker, |
| 79 const ParseJSONCallback& json_parsing_callback) |
| 80 : download_context_(download_context), |
| 81 json_parsing_callback_(json_parsing_callback), |
| 82 google_url_tracker_(google_url_tracker), |
| 83 clock_(new base::DefaultClock()), |
| 84 weak_ptr_factory_(this) { |
| 85 DCHECK(google_url_tracker_); |
| 86 } |
| 87 |
| 88 DoodleFetcher::~DoodleFetcher() = default; |
| 89 |
| 90 void DoodleFetcher::FetchDoodle(FinishedCallback callback) { |
| 91 if (IsFetchInProgress()) { |
| 92 callbacks_.push_back(std::move(callback)); |
| 93 return; // The callback will be called for the existing request's results. |
| 94 } |
| 95 DCHECK(!fetcher_.get()); |
| 96 callbacks_.push_back(std::move(callback)); |
| 97 fetcher_ = URLFetcher::Create(GetGoogleBaseUrl().Resolve(kDoodleConfigPath), |
| 98 URLFetcher::GET, this); |
| 99 fetcher_->SetRequestContext(download_context_.get()); |
| 100 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 101 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 102 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 103 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); |
| 104 fetcher_->Start(); |
| 105 } |
| 106 |
| 107 void DoodleFetcher::OnURLFetchComplete(const URLFetcher* source) { |
| 108 DCHECK_EQ(fetcher_.get(), source); |
| 109 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); |
| 110 |
| 111 std::string json_string; |
| 112 if (!(source->GetStatus().is_success() && |
| 113 source->GetResponseCode() == net::HTTP_OK && |
| 114 source->GetResponseAsString(&json_string))) { |
| 115 RespondToAllCallbacks(DoodleState::DOWNLOAD_ERROR, base::nullopt); |
| 116 return; |
| 117 } |
| 118 |
| 119 json_parsing_callback_.Run( |
| 120 StripSafetyPreamble(std::move(json_string)), |
| 121 base::Bind(&DoodleFetcher::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), |
| 122 base::Bind(&DoodleFetcher::OnJsonParseFailed, |
| 123 weak_ptr_factory_.GetWeakPtr())); |
| 124 } |
| 125 |
| 126 void DoodleFetcher::OnJsonParsed(std::unique_ptr<base::Value> json) { |
| 127 std::unique_ptr<base::DictionaryValue> config = |
| 128 base::DictionaryValue::From(std::move(json)); |
| 129 if (!config.get()) { |
| 130 DLOG(WARNING) << "Doodle JSON is not valid"; |
| 131 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); |
| 132 return; |
| 133 } |
| 134 |
| 135 const base::DictionaryValue* ddljson = nullptr; |
| 136 if (!config->GetDictionary("ddljson", &ddljson)) { |
| 137 DLOG(WARNING) << "Doodle JSON reponse did not contain 'ddljson' key."; |
| 138 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); |
| 139 return; |
| 140 } |
| 141 |
| 142 base::Optional<DoodleConfig> doodle = ParseDoodle(*ddljson); |
| 143 if (!doodle.has_value()) { |
| 144 RespondToAllCallbacks(DoodleState::NO_DOODLE, base::nullopt); |
| 145 return; |
| 146 } |
| 147 |
| 148 RespondToAllCallbacks(DoodleState::AVAILABLE, std::move(doodle)); |
| 149 } |
| 150 |
| 151 void DoodleFetcher::OnJsonParseFailed(const std::string& error_message) { |
| 152 DLOG(WARNING) << "JSON parsing failed: " << error_message; |
| 153 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); |
| 154 } |
| 155 |
| 156 base::Optional<DoodleConfig> DoodleFetcher::ParseDoodle( |
| 157 const base::DictionaryValue& ddljson) const { |
| 158 DoodleConfig doodle; |
| 159 if (!ParseImage(ddljson, "large_image", &doodle.large_image)) { |
| 160 return base::nullopt; |
| 161 } |
| 162 ParseImage(ddljson, "transparent_large_image", |
| 163 &doodle.transparent_large_image); |
| 164 ParseImage(ddljson, "large_cta_image", &doodle.large_cta_image); |
| 165 ParseBaseInformation(ddljson, &doodle); |
| 166 return doodle; |
| 167 } |
| 168 |
| 169 bool DoodleFetcher::ParseImage(const base::DictionaryValue& image_parent, |
| 170 const std::string& image_name, |
| 171 DoodleImage* image) const { |
| 172 DCHECK(image); |
| 173 const base::DictionaryValue* image_dict = nullptr; |
| 174 if (!image_parent.GetDictionary(image_name, &image_dict)) { |
| 175 return false; |
| 176 } |
| 177 image->url = ParseRelativeUrl(*image_dict, "url"); |
| 178 if (!image->url.is_valid()) { |
| 179 DLOG(WARNING) << "Image URL for \"" << image_name << "\" is invalid."; |
| 180 return false; |
| 181 } |
| 182 image_dict->GetInteger("height", &image->height); |
| 183 image_dict->GetInteger("width", &image->width); |
| 184 image_dict->GetBoolean("is_animated_gif", &image->is_animated_gif); |
| 185 image_dict->GetBoolean("is_cta", &image->is_cta); |
| 186 return true; |
| 187 } |
| 188 |
| 189 void DoodleFetcher::ParseBaseInformation(const base::DictionaryValue& ddljson, |
| 190 DoodleConfig* config) const { |
| 191 config->search_url = ParseRelativeUrl(ddljson, "search_url"); |
| 192 config->target_url = ParseRelativeUrl(ddljson, "target_url"); |
| 193 config->fullpage_interactive_url = |
| 194 ParseRelativeUrl(ddljson, "fullpage_interactive_url"); |
| 195 |
| 196 config->doodle_type = ParseDoodleType(ddljson); |
| 197 ddljson.GetString("alt_text", &config->alt_text); |
| 198 ddljson.GetString("interactive_html", &config->interactive_html); |
| 199 |
| 200 // The JSON doesn't guarantee the number to fit into an int. |
| 201 double ttl = 0; // Expires immediately if the parameter is missing. |
| 202 if (!ddljson.GetDouble("time_to_live_ms", &ttl) || ttl < 0) { |
| 203 DLOG(WARNING) << "No valid Doodle image TTL present in ddljson!"; |
| 204 ttl = 0; |
| 205 } |
| 206 if (ttl > kMaxTimeToLiveMS) { |
| 207 ttl = kMaxTimeToLiveMS; |
| 208 DLOG(WARNING) << "Clamping Doodle image TTL to 30 days!"; |
| 209 } |
| 210 config->expiry_date = clock_->Now() + base::TimeDelta::FromMillisecondsD(ttl); |
| 211 } |
| 212 |
| 213 GURL DoodleFetcher::ParseRelativeUrl(const base::DictionaryValue& dict_value, |
| 214 const std::string& key) const { |
| 215 std::string str_url; |
| 216 dict_value.GetString(key, &str_url); |
| 217 if (str_url.empty()) { |
| 218 return GURL(); |
| 219 } |
| 220 return GetGoogleBaseUrl().Resolve(str_url); |
| 221 } |
| 222 |
| 223 void DoodleFetcher::RespondToAllCallbacks( |
| 224 DoodleState state, |
| 225 const base::Optional<DoodleConfig>& config) { |
| 226 for (auto& callback : callbacks_) { |
| 227 std::move(callback).Run(state, config); |
| 228 } |
| 229 callbacks_.clear(); |
| 230 } |
| 231 |
| 232 GURL DoodleFetcher::GetGoogleBaseUrl() const { |
| 233 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); |
| 234 if (cmd_line_url.is_valid()) { |
| 235 return cmd_line_url; |
| 236 } |
| 237 return google_url_tracker_->google_url(); |
| 238 } |
| 239 |
| 240 } // namespace doodle |
| OLD | NEW |