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/clock.h" | |
| 11 #include "base/time/default_clock.h" | 10 #include "base/time/default_clock.h" |
| 12 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 13 #include "base/values.h" | 12 #include "base/values.h" |
| 14 #include "components/data_use_measurement/core/data_use_user_data.h" | 13 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 15 #include "components/google/core/browser/google_url_tracker.h" | 14 #include "components/google/core/browser/google_url_tracker.h" |
| 16 #include "components/google/core/browser/google_util.h" | 15 #include "components/google/core/browser/google_util.h" |
| 17 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 18 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 19 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
| 20 | 19 |
| 21 using net::URLFetcher; | 20 using net::URLFetcher; |
| 22 | 21 |
| 23 namespace doodle { | 22 namespace doodle { |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 const double kMaxTimeToLiveMS = 30.0 * 24 * 60 * 60 * 1000; // 30 days | 26 const double kMaxTimeToLiveMS = 30.0 * 24 * 60 * 60 * 1000; // 30 days |
|
vitaliii
2017/02/28 11:37:17
base::TimeDelta with base::TimeDelta::FromMillisec
Marc Treib
2017/02/28 13:06:49
We can't have constants of non-trivial types, beca
| |
| 28 | 27 |
| 29 const char kDoodleConfigPath[] = "/async/ddljson"; | 28 const char kDoodleConfigPath[] = "/async/ddljson"; |
| 30 | 29 |
| 31 std::string StripSafetyPreamble(const std::string& json) { | 30 std::string StripSafetyPreamble(const std::string& json) { |
| 32 // The response may start with )]}'. Ignore this. | 31 // The response may start with )]}'. Ignore this. |
| 33 const char kResponsePreamble[] = ")]}'"; | 32 const char kResponsePreamble[] = ")]}'"; |
| 34 | 33 |
| 35 base::StringPiece json_sp(json); | 34 base::StringPiece json_sp(json); |
| 36 if (json_sp.starts_with(kResponsePreamble)) { | 35 if (json_sp.starts_with(kResponsePreamble)) { |
| 37 json_sp.remove_prefix(strlen(kResponsePreamble)); | 36 json_sp.remove_prefix(strlen(kResponsePreamble)); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 base::Bind(&DoodleFetcherImpl::OnJsonParsed, | 122 base::Bind(&DoodleFetcherImpl::OnJsonParsed, |
| 124 weak_ptr_factory_.GetWeakPtr()), | 123 weak_ptr_factory_.GetWeakPtr()), |
| 125 base::Bind(&DoodleFetcherImpl::OnJsonParseFailed, | 124 base::Bind(&DoodleFetcherImpl::OnJsonParseFailed, |
| 126 weak_ptr_factory_.GetWeakPtr())); | 125 weak_ptr_factory_.GetWeakPtr())); |
| 127 } | 126 } |
| 128 | 127 |
| 129 void DoodleFetcherImpl::OnJsonParsed(std::unique_ptr<base::Value> json) { | 128 void DoodleFetcherImpl::OnJsonParsed(std::unique_ptr<base::Value> json) { |
| 130 std::unique_ptr<base::DictionaryValue> config = | 129 std::unique_ptr<base::DictionaryValue> config = |
| 131 base::DictionaryValue::From(std::move(json)); | 130 base::DictionaryValue::From(std::move(json)); |
| 132 if (!config.get()) { | 131 if (!config.get()) { |
| 133 DLOG(WARNING) << "Doodle JSON is not valid"; | 132 DLOG(WARNING) << "Doodle JSON is not valid"; |
|
vitaliii
2017/02/28 11:37:17
s/valid"/valid."
Marc Treib
2017/02/28 13:06:49
Done.
| |
| 134 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); | 133 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); |
| 135 return; | 134 return; |
| 136 } | 135 } |
| 137 | 136 |
| 138 const base::DictionaryValue* ddljson = nullptr; | 137 const base::DictionaryValue* ddljson = nullptr; |
| 139 if (!config->GetDictionary("ddljson", &ddljson)) { | 138 if (!config->GetDictionary("ddljson", &ddljson)) { |
| 140 DLOG(WARNING) << "Doodle JSON reponse did not contain 'ddljson' key."; | 139 DLOG(WARNING) << "Doodle JSON reponse did not contain 'ddljson' key."; |
|
vitaliii
2017/02/28 11:37:17
reSponse
Marc Treib
2017/02/28 13:06:48
Done.
| |
| 141 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); | 140 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); |
| 142 return; | 141 return; |
| 143 } | 142 } |
| 144 | 143 |
| 145 base::Optional<DoodleConfig> doodle = ParseDoodle(*ddljson); | 144 base::Optional<DoodleConfig> doodle = ParseDoodle(*ddljson); |
| 146 if (!doodle.has_value()) { | 145 if (!doodle.has_value()) { |
| 147 RespondToAllCallbacks(DoodleState::NO_DOODLE, base::nullopt); | 146 RespondToAllCallbacks(DoodleState::NO_DOODLE, base::nullopt); |
| 148 return; | 147 return; |
| 149 } | 148 } |
| 150 | 149 |
| 151 RespondToAllCallbacks(DoodleState::AVAILABLE, std::move(doodle)); | 150 RespondToAllCallbacks(DoodleState::AVAILABLE, std::move(doodle)); |
| 152 } | 151 } |
| 153 | 152 |
| 154 void DoodleFetcherImpl::OnJsonParseFailed(const std::string& error_message) { | 153 void DoodleFetcherImpl::OnJsonParseFailed(const std::string& error_message) { |
| 155 DLOG(WARNING) << "JSON parsing failed: " << error_message; | 154 DLOG(WARNING) << "JSON parsing failed: " << error_message; |
| 156 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); | 155 RespondToAllCallbacks(DoodleState::PARSING_ERROR, base::nullopt); |
| 157 } | 156 } |
| 158 | 157 |
| 159 base::Optional<DoodleConfig> DoodleFetcherImpl::ParseDoodle( | 158 base::Optional<DoodleConfig> DoodleFetcherImpl::ParseDoodle( |
| 160 const base::DictionaryValue& ddljson) const { | 159 const base::DictionaryValue& ddljson) const { |
| 161 DoodleConfig doodle; | 160 DoodleConfig doodle; |
| 162 if (!ParseImage(ddljson, "large_image", &doodle.large_image)) { | 161 if (!ParseImage(ddljson, "large_image", &doodle.large_image)) { |
|
vitaliii
2017/02/28 11:37:17
It is not clear why not having a |large_image| mea
Marc Treib
2017/02/28 13:06:48
Per info from the Doodle folks, this is basically
| |
| 163 return base::nullopt; | 162 return base::nullopt; |
| 164 } | 163 } |
| 165 ParseImage(ddljson, "transparent_large_image", | 164 ParseImage(ddljson, "transparent_large_image", |
| 166 &doodle.transparent_large_image); | 165 &doodle.transparent_large_image); |
| 167 ParseImage(ddljson, "large_cta_image", &doodle.large_cta_image); | 166 ParseImage(ddljson, "large_cta_image", &doodle.large_cta_image); |
| 168 ParseBaseInformation(ddljson, &doodle); | 167 ParseBaseInformation(ddljson, &doodle); |
| 169 return doodle; | 168 return doodle; |
| 170 } | 169 } |
| 171 | 170 |
| 172 bool DoodleFetcherImpl::ParseImage(const base::DictionaryValue& image_parent, | 171 bool DoodleFetcherImpl::ParseImage(const base::DictionaryValue& image_parent, |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 | 235 |
| 237 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { | 236 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { |
| 238 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); | 237 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); |
| 239 if (cmd_line_url.is_valid()) { | 238 if (cmd_line_url.is_valid()) { |
| 240 return cmd_line_url; | 239 return cmd_line_url; |
| 241 } | 240 } |
| 242 return google_url_tracker_->google_url(); | 241 return google_url_tracker_->google_url(); |
| 243 } | 242 } |
| 244 | 243 |
| 245 } // namespace doodle | 244 } // namespace doodle |
| OLD | NEW |