Chromium Code Reviews| 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/safe_json/safe_json_parser.h" | |
| 15 #include "net/base/load_flags.h" | |
| 16 #include "net/http/http_status_code.h" | |
| 17 #include "net/url_request/url_fetcher.h" | |
| 18 #include "url/gurl.h" | |
| 19 | |
| 20 using net::URLFetcher; | |
| 21 | |
| 22 namespace doodle { | |
| 23 | |
| 24 const char kDoodleConfigUrl[] = "/async/ddljson?ogdeb=ct~2000000007"; | |
|
Marc Treib
2017/01/31 14:46:38
It's not actually a URL, but a path
fhorschig
2017/02/01 11:16:13
Done.
| |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 void ParseJson( | |
| 29 const std::string& json, | |
| 30 const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback, | |
| 31 const base::Callback<void(const std::string&)>& error_callback) { | |
| 32 // The response may start with )]}'. Ignore this. | |
| 33 const char kResponsePreamble[] = ")]}'"; | |
| 34 | |
| 35 base::StringPiece json_sp(json); | |
| 36 if (json_sp.starts_with(kResponsePreamble)) { | |
| 37 json_sp.remove_prefix(strlen(kResponsePreamble)); | |
| 38 } | |
| 39 | |
| 40 safe_json::SafeJsonParser::Parse(json_sp.as_string(), success_callback, | |
| 41 error_callback); | |
| 42 } | |
| 43 | |
| 44 DoodleType ParseDoodleType(const base::DictionaryValue* ddljson) { | |
| 45 std::string type_str; | |
| 46 ddljson->GetString("doodle_type", &type_str); | |
| 47 if (type_str == "SIMPLE") { | |
| 48 return DoodleType::SIMPLE; | |
| 49 } | |
| 50 if (type_str == "RANDOM") { | |
| 51 return DoodleType::RANDOM; | |
| 52 } | |
| 53 if (type_str == "VIDEO") { | |
| 54 return DoodleType::VIDEO; | |
| 55 } | |
| 56 if (type_str == "INTERACTIVE") { | |
| 57 return DoodleType::INTERACTIVE; | |
| 58 } | |
| 59 if (type_str == "INLINE_INTERACTIVE") { | |
| 60 return DoodleType::INLINE_INTERACTIVE; | |
| 61 } | |
| 62 if (type_str == "SLIDESHOW") { | |
| 63 return DoodleType::SLIDESHOW; | |
| 64 } | |
| 65 return DoodleType::SIMPLE; | |
|
Marc Treib
2017/01/31 14:46:38
Hm. Should we treat all unknown types as SIMPLE? T
fhorschig
2017/02/01 11:16:13
UNKNOWN Added. This only affects tests and invalid
| |
| 66 } | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 DoodleImage::DoodleImage() | |
| 71 : height(0), | |
| 72 width(0), | |
| 73 background_color("#ffffff"), | |
| 74 is_animated_gif(false), | |
| 75 is_cta(false) {} | |
| 76 DoodleImage::DoodleImage(const DoodleImage& image) = default; | |
| 77 DoodleImage::~DoodleImage() = default; | |
| 78 | |
| 79 DoodleConfig::DoodleConfig(DoodleState doodle_state) | |
| 80 : state(doodle_state), | |
| 81 doodle_type(DoodleType::SIMPLE), | |
| 82 expiry_date(base::Time::Now()) {} | |
| 83 DoodleConfig::~DoodleConfig() = default; | |
| 84 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default; | |
| 85 | |
| 86 DoodleFetcher::DoodleFetcher(net::URLRequestContextGetter* download_context, | |
| 87 GURL base_url) | |
| 88 : DoodleFetcher(download_context, base_url, base::Bind(ParseJson)) {} | |
| 89 | |
| 90 DoodleFetcher::DoodleFetcher(net::URLRequestContextGetter* download_context, | |
| 91 GURL base_url, | |
| 92 ParseJSONCallback json_parsing_callback) | |
| 93 : download_context_(download_context), | |
| 94 json_parsing_callback_(json_parsing_callback), | |
| 95 base_url_(base_url), | |
| 96 clock_(new base::DefaultClock()), | |
| 97 weak_ptr_factory_(this) {} | |
| 98 | |
| 99 DoodleFetcher::~DoodleFetcher() {} | |
| 100 | |
| 101 void DoodleFetcher::FetchDoodle(const FinishedCallback& callback) { | |
| 102 DCHECK(!callback_); | |
| 103 callback_ = callback; | |
| 104 // TODO(fhorschig): Store multiple callbacks. | |
| 105 // TODO(fhorschig): Cache responses? | |
| 106 fetcher_ = URLFetcher::Create(base_url_.Resolve(kDoodleConfigUrl), | |
| 107 URLFetcher::GET, this); | |
| 108 fetcher_->SetRequestContext(download_context_); | |
| 109 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 110 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 111 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); | |
| 112 fetcher_->Start(); | |
| 113 } | |
| 114 | |
| 115 // net::URLFetcherDelegate implementation. | |
| 116 void DoodleFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 117 DCHECK_EQ(fetcher_.get(), source); | |
| 118 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); | |
| 119 | |
| 120 std::string json_string; | |
| 121 if (!(source->GetStatus().is_success() && | |
| 122 source->GetResponseCode() == net::HTTP_OK && | |
| 123 source->GetResponseAsString(&json_string))) { | |
| 124 callback_.Run(DoodleConfig(DoodleState::DOWNLOAD_ERROR)); | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 ParseJson(json_string, base::Bind(&DoodleFetcher::OnJsonParsed, | |
| 129 weak_ptr_factory_.GetWeakPtr()), | |
| 130 base::Bind(&DoodleFetcher::OnJsonParseFailed, | |
| 131 weak_ptr_factory_.GetWeakPtr())); | |
| 132 } | |
| 133 | |
| 134 // ParseJSONCallback Success callback | |
|
Marc Treib
2017/01/31 14:46:38
nit: Comment not needed here (you could put someth
fhorschig
2017/02/01 11:16:13
Done.
| |
| 135 void DoodleFetcher::OnJsonParsed(std::unique_ptr<base::Value> json) { | |
| 136 std::unique_ptr<base::DictionaryValue> dict = | |
| 137 base::DictionaryValue::From(std::move(json)); | |
| 138 if (!dict.get()) { | |
| 139 LOG(ERROR) << "Doodle JSON is not valid"; | |
| 140 callback_.Run(DoodleConfig(DoodleState::PARSING_ERROR)); | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 ParseDoodle(std::move(dict)); | |
| 145 } | |
| 146 | |
| 147 // ParseJSONCallback Failure callback | |
| 148 void DoodleFetcher::OnJsonParseFailed(const std::string& error_message) { | |
| 149 LOG(ERROR) << "JSON parsing failed: " << error_message; | |
| 150 callback_.Run(DoodleConfig(DoodleState::PARSING_ERROR)); | |
| 151 } | |
| 152 | |
| 153 void DoodleFetcher::ParseDoodle(std::unique_ptr<base::DictionaryValue> config) { | |
| 154 const base::DictionaryValue* ddljson = nullptr; | |
| 155 if (!config->GetDictionary("ddljson", &ddljson)) { | |
| 156 LOG(ERROR) << "Doodle JSON reponse did not contain 'ddljson' key."; | |
| 157 callback_.Run(DoodleConfig(DoodleState::PARSING_ERROR)); | |
| 158 return; | |
| 159 } | |
| 160 | |
| 161 DoodleConfig result(DoodleState::AVAILABLE); | |
| 162 ParseBaseInformation(ddljson, &result); | |
| 163 | |
| 164 if (!ParseImage(ddljson, "large_image", &result.large_image)) { | |
| 165 callback_.Run(DoodleConfig(DoodleState::NO_DOODLE)); | |
| 166 return; | |
| 167 } | |
| 168 ParseImage(ddljson, "transparent_large_image", | |
| 169 &result.transparent_large_image); | |
| 170 ParseImage(ddljson, "hires_image", &result.hires_image); | |
| 171 ParseImage(ddljson, "medium_image", &result.medium_image); | |
| 172 ParseImage(ddljson, "small_image", &result.small_image); | |
| 173 | |
| 174 callback_.Run(std::move(result)); | |
| 175 } | |
| 176 | |
| 177 bool DoodleFetcher::ParseImage(const base::DictionaryValue* image_parent, | |
| 178 const std::string& image_name, | |
| 179 DoodleImage* image) const { | |
| 180 DCHECK(image); | |
| 181 const base::DictionaryValue* image_dict = nullptr; | |
| 182 if (!(image && image_parent && | |
| 183 image_parent->GetDictionary(image_name, &image_dict))) { | |
| 184 return false; | |
| 185 } | |
| 186 std::string url; | |
| 187 image_dict->GetString("url", &url); | |
| 188 image->url = base_url_.Resolve(url); | |
| 189 image_dict->GetString("background_color", &image->background_color); | |
| 190 image_dict->GetInteger("height", &image->height); | |
| 191 image_dict->GetInteger("width", &image->width); | |
| 192 image_dict->GetBoolean("is_animated_gif", &image->is_animated_gif); | |
| 193 image_dict->GetBoolean("is_cta", &image->is_cta); | |
| 194 return true; | |
| 195 } | |
| 196 | |
| 197 void DoodleFetcher::ParseBaseInformation(const base::DictionaryValue* ddljson, | |
| 198 DoodleConfig* config) const { | |
| 199 ParseRelativeUrl(ddljson, "search_url", &config->search_url); | |
| 200 ParseRelativeUrl(ddljson, "target_url", &config->target_url); | |
| 201 ParseRelativeUrl(ddljson, "fullpage_interactive_url", | |
| 202 &config->fullpage_interactive_url); | |
| 203 | |
| 204 config->doodle_type = ParseDoodleType(ddljson); | |
| 205 ddljson->GetString("alt_text", &config->alt_text); | |
| 206 | |
| 207 // TODO(fhorschig): Inject TickClock and test that. | |
| 208 // The JSON doesn't garantuee the number to fit into an int. | |
| 209 double ttl; | |
| 210 ddljson->GetDouble("time_to_live_ms", &ttl); | |
| 211 config->expiry_date = clock_->Now() + base::TimeDelta::FromMillisecondsD(ttl); | |
| 212 } | |
| 213 | |
| 214 void DoodleFetcher::ParseRelativeUrl(const base::DictionaryValue* dict_value, | |
|
Marc Treib
2017/01/31 14:46:38
nit: Could this just return the resulting GURL?
fhorschig
2017/02/01 11:16:13
Absolutely.
| |
| 215 const std::string& key, | |
| 216 GURL* url) const { | |
| 217 std::string str_url; | |
| 218 dict_value->GetString(key, &str_url); | |
| 219 if (str_url.empty()) { | |
| 220 *url = GURL(); | |
| 221 } else { | |
| 222 *url = base_url_.Resolve(str_url); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 } // namespace doodle | |
| OLD | NEW |