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/google/core/browser/google_url_tracker.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 kDoodleConfigPath[] = "/async/ddljson"; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 std::string StripSafetyPreamble(const std::string& json) { | |
| 29 // The response may start with )]}'. Ignore this. | |
| 30 const char kResponsePreamble[] = ")]}'"; | |
| 31 | |
| 32 base::StringPiece json_sp(json); | |
| 33 if (json_sp.starts_with(kResponsePreamble)) { | |
| 34 json_sp.remove_prefix(strlen(kResponsePreamble)); | |
| 35 } | |
| 36 | |
| 37 return json_sp.as_string(); | |
| 38 } | |
| 39 | |
| 40 DoodleType ParseDoodleType(const base::DictionaryValue& ddljson) { | |
| 41 std::string type_str; | |
| 42 ddljson.GetString("doodle_type", &type_str); | |
| 43 if (type_str == "SIMPLE") { | |
| 44 return DoodleType::SIMPLE; | |
| 45 } | |
| 46 if (type_str == "RANDOM") { | |
| 47 return DoodleType::RANDOM; | |
| 48 } | |
| 49 if (type_str == "VIDEO") { | |
| 50 return DoodleType::VIDEO; | |
| 51 } | |
| 52 if (type_str == "INTERACTIVE") { | |
| 53 return DoodleType::INTERACTIVE; | |
| 54 } | |
| 55 if (type_str == "INLINE_INTERACTIVE") { | |
| 56 return DoodleType::INLINE_INTERACTIVE; | |
| 57 } | |
| 58 if (type_str == "SLIDESHOW") { | |
| 59 return DoodleType::SLIDESHOW; | |
| 60 } | |
| 61 return DoodleType::UNKNOWN; | |
| 62 } | |
| 63 | |
| 64 base::Optional<DoodleConfig> NoConfig() { | |
|
Marc Treib
2017/02/01 11:53:14
I think you can just write base::nullopt instead o
fhorschig
2017/02/03 13:21:04
Nice.
| |
| 65 return base::Optional<DoodleConfig>(); | |
| 66 } | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 DoodleImage::DoodleImage() | |
| 71 : height(0), width(0), is_animated_gif(false), is_cta(false) {} | |
| 72 DoodleImage::~DoodleImage() = default; | |
| 73 | |
| 74 DoodleConfig::DoodleConfig() | |
| 75 : doodle_type(DoodleType::UNKNOWN), expiry_date(base::Time::Now()) {} | |
|
Marc Treib
2017/02/01 11:53:13
nit: I'd leave expiry_date without explicit initia
fhorschig
2017/02/03 13:21:04
Done.
| |
| 76 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default; | |
| 77 DoodleConfig::~DoodleConfig() = default; | |
| 78 | |
| 79 DoodleFetcher::DoodleFetcher(net::URLRequestContextGetter* download_context, | |
| 80 GoogleURLTracker* google_url_tracker, | |
| 81 ParseJSONCallback json_parsing_callback) | |
| 82 : download_context_(download_context), | |
| 83 json_parsing_callback_(json_parsing_callback), | |
| 84 google_url_tracker_(google_url_tracker), | |
| 85 clock_(new base::DefaultClock()), | |
| 86 weak_ptr_factory_(this) {} | |
| 87 DoodleFetcher::~DoodleFetcher() {} | |
|
Marc Treib
2017/02/01 11:53:14
nit: empty line before
fhorschig
2017/02/03 13:21:04
Done.
| |
| 88 | |
| 89 void DoodleFetcher::FetchDoodle(FinishedCallback callback) { | |
| 90 callbacks_.push_back(std::move(callback)); | |
| 91 if (callbacks_.size() > 1) { | |
| 92 return; // With 2 or more callbacks, a fetcher is already running. | |
|
Marc Treib
2017/02/01 11:53:14
nit: I'd formulate this as "If there already was a
fhorschig
2017/02/03 13:21:04
Done.
| |
| 93 } | |
| 94 fetcher_ = URLFetcher::Create(GetGoogleBaseUrl().Resolve(kDoodleConfigPath), | |
|
Marc Treib
2017/02/01 11:53:13
Should we DCHECK here that fetcher_ was null?
fhorschig
2017/02/03 13:21:04
Okay.
| |
| 95 URLFetcher::GET, this); | |
| 96 fetcher_->SetRequestContext(download_context_); | |
| 97 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 98 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 99 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); | |
| 100 fetcher_->Start(); | |
| 101 } | |
| 102 | |
| 103 // net::URLFetcherDelegate implementation. | |
| 104 void DoodleFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 105 DCHECK_EQ(fetcher_.get(), source); | |
| 106 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); | |
| 107 | |
| 108 std::string json_string; | |
| 109 if (!(source->GetStatus().is_success() && | |
| 110 source->GetResponseCode() == net::HTTP_OK && | |
| 111 source->GetResponseAsString(&json_string))) { | |
| 112 RespondToAllCallbacks(DoodleState::DOWNLOAD_ERROR, NoConfig()); | |
| 113 return; | |
| 114 } | |
| 115 | |
| 116 json_parsing_callback_.Run( | |
| 117 StripSafetyPreamble(std::move(json_string)), | |
| 118 base::Bind(&DoodleFetcher::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()), | |
| 119 base::Bind(&DoodleFetcher::OnJsonParseFailed, | |
| 120 weak_ptr_factory_.GetWeakPtr())); | |
| 121 } | |
| 122 | |
| 123 void DoodleFetcher::OnJsonParsed(std::unique_ptr<base::Value> json) { | |
| 124 std::unique_ptr<base::DictionaryValue> dict = | |
| 125 base::DictionaryValue::From(std::move(json)); | |
| 126 if (!dict.get()) { | |
| 127 LOG(ERROR) << "Doodle JSON is not valid"; | |
| 128 RespondToAllCallbacks(DoodleState::PARSING_ERROR, NoConfig()); | |
| 129 return; | |
| 130 } | |
| 131 | |
| 132 ParseDoodle(std::move(dict)); | |
| 133 } | |
| 134 | |
| 135 void DoodleFetcher::OnJsonParseFailed(const std::string& error_message) { | |
| 136 LOG(ERROR) << "JSON parsing failed: " << error_message; | |
| 137 RespondToAllCallbacks(DoodleState::PARSING_ERROR, NoConfig()); | |
| 138 } | |
| 139 | |
| 140 void DoodleFetcher::ParseDoodle(std::unique_ptr<base::DictionaryValue> config) { | |
| 141 const base::DictionaryValue* ddljson = nullptr; | |
| 142 if (!config->GetDictionary("ddljson", &ddljson)) { | |
| 143 LOG(ERROR) << "Doodle JSON reponse did not contain 'ddljson' key."; | |
| 144 RespondToAllCallbacks(DoodleState::PARSING_ERROR, NoConfig()); | |
| 145 return; | |
| 146 } | |
| 147 | |
| 148 DoodleConfig result; | |
| 149 ParseBaseInformation(*ddljson, &result); | |
| 150 | |
| 151 if (!ParseImage(*ddljson, "large_image", &result.large_image)) { | |
| 152 RespondToAllCallbacks(DoodleState::NO_DOODLE, NoConfig()); | |
| 153 return; | |
| 154 } | |
| 155 ParseImage(*ddljson, "transparent_large_image", | |
| 156 &result.transparent_large_image); | |
| 157 ParseImage(*ddljson, "large_cta_image", &result.large_cta_image); | |
| 158 | |
| 159 RespondToAllCallbacks(DoodleState::AVAILABLE, std::move(result)); | |
| 160 } | |
| 161 | |
| 162 bool DoodleFetcher::ParseImage(const base::DictionaryValue& image_parent, | |
| 163 const std::string& image_name, | |
| 164 DoodleImage* image) const { | |
| 165 DCHECK(image); | |
| 166 const base::DictionaryValue* image_dict = nullptr; | |
| 167 if (!(image && image_parent.GetDictionary(image_name, &image_dict))) { | |
|
Marc Treib
2017/02/01 11:53:13
You have DCHECKed image before, no reason to null-
fhorschig
2017/02/03 13:21:04
True.
| |
| 168 return false; | |
| 169 } | |
| 170 image->url = ParseRelativeUrl(*image_dict, "url"); | |
|
Marc Treib
2017/02/01 11:53:14
Should this return false if the url is missing or
fhorschig
2017/02/03 13:21:04
Done.
| |
| 171 image_dict->GetInteger("height", &image->height); | |
| 172 image_dict->GetInteger("width", &image->width); | |
| 173 image_dict->GetBoolean("is_animated_gif", &image->is_animated_gif); | |
| 174 image_dict->GetBoolean("is_cta", &image->is_cta); | |
| 175 return true; | |
| 176 } | |
| 177 | |
| 178 void DoodleFetcher::ParseBaseInformation(const base::DictionaryValue& ddljson, | |
| 179 DoodleConfig* config) const { | |
| 180 config->search_url = ParseRelativeUrl(ddljson, "search_url"); | |
| 181 config->target_url = ParseRelativeUrl(ddljson, "target_url"); | |
| 182 config->fullpage_interactive_url = | |
| 183 ParseRelativeUrl(ddljson, "fullpage_interactive_url"); | |
| 184 | |
| 185 config->doodle_type = ParseDoodleType(ddljson); | |
| 186 ddljson.GetString("alt_text", &config->alt_text); | |
| 187 | |
| 188 // TODO(fhorschig): Inject TickClock and test that. | |
| 189 // The JSON doesn't garantuee the number to fit into an int. | |
|
Marc Treib
2017/02/01 11:53:14
s/garantuee/guarantee/
fhorschig
2017/02/03 13:21:04
Thank you
| |
| 190 double ttl; | |
|
Marc Treib
2017/02/01 11:53:13
initialize, and/or check if parsing actually worke
fhorschig
2017/02/03 13:21:04
Done.
| |
| 191 ddljson.GetDouble("time_to_live_ms", &ttl); | |
| 192 config->expiry_date = clock_->Now() + base::TimeDelta::FromMillisecondsD(ttl); | |
| 193 } | |
| 194 | |
| 195 GURL DoodleFetcher::ParseRelativeUrl(const base::DictionaryValue& dict_value, | |
| 196 const std::string& key) const { | |
| 197 std::string str_url; | |
| 198 dict_value.GetString(key, &str_url); | |
| 199 if (str_url.empty()) { | |
| 200 return GURL(); | |
| 201 } | |
| 202 return GetGoogleBaseUrl().Resolve(str_url); | |
| 203 } | |
| 204 | |
| 205 void DoodleFetcher::RespondToAllCallbacks(DoodleState state, | |
| 206 base::Optional<DoodleConfig> config) { | |
| 207 while (!callbacks_.empty()) { | |
| 208 std::move(callbacks_.front()).Run(state, std::move(config)); | |
| 209 callbacks_.pop_front(); | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 GURL DoodleFetcher::GetGoogleBaseUrl() const { | |
| 214 // There might be no tracker without profile. The factory returns nullptr in | |
| 215 // tests, too. | |
|
Marc Treib
2017/02/01 11:53:14
I think this class should assume that the tracker
fhorschig
2017/02/03 13:21:04
Yes, it's a TODO I have in tests.
Adding it here,
Marc Treib
2017/02/03 15:45:48
I'm not sure I follow. They don't "become" nullptr
fhorschig
2017/02/06 11:48:22
Thank you for the clarification.
(As discussed off
| |
| 216 if (google_url_tracker_) { | |
| 217 return google_url_tracker_->google_url(); | |
|
Marc Treib
2017/02/01 15:22:02
Turns out the tracker doesn't respect the command
fhorschig
2017/02/03 13:21:04
Done.
| |
| 218 } | |
| 219 return GURL(GoogleURLTracker::kDefaultGoogleHomepage); | |
| 220 } | |
| 221 | |
| 222 } // namespace doodle | |
| OLD | NEW |