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/default_clock.h" | |
| 11 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 12 #include "base/values.h" | 11 #include "base/values.h" |
| 13 #include "components/data_use_measurement/core/data_use_user_data.h" | 12 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 14 #include "components/google/core/browser/google_url_tracker.h" | 13 #include "components/google/core/browser/google_url_tracker.h" |
| 15 #include "components/google/core/browser/google_util.h" | 14 #include "components/google/core/browser/google_util.h" |
| 16 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 17 #include "net/http/http_status_code.h" | 16 #include "net/http/http_status_code.h" |
| 18 #include "net/url_request/url_fetcher.h" | 17 #include "net/url_request/url_fetcher.h" |
| 19 | 18 |
| 20 using net::URLFetcher; | 19 using net::URLFetcher; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 } | 61 } |
| 63 return DoodleType::UNKNOWN; | 62 return DoodleType::UNKNOWN; |
| 64 } | 63 } |
| 65 | 64 |
| 66 } // namespace | 65 } // namespace |
| 67 | 66 |
| 68 DoodleImage::DoodleImage() | 67 DoodleImage::DoodleImage() |
| 69 : height(0), width(0), is_animated_gif(false), is_cta(false) {} | 68 : height(0), width(0), is_animated_gif(false), is_cta(false) {} |
| 70 DoodleImage::~DoodleImage() = default; | 69 DoodleImage::~DoodleImage() = default; |
| 71 | 70 |
| 72 DoodleConfig::DoodleConfig() : doodle_type(DoodleType::UNKNOWN) {} | 71 DoodleConfig::DoodleConfig() : doodle_type(DoodleType::UNKNOWN) {} |
|
fhorschig
2017/03/01 15:40:52
Do we want the default TTL to be 0?
Alternatives w
Marc Treib
2017/03/02 09:28:02
Hm. Max() I don't like, because then we'd have a c
| |
| 73 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default; | 72 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default; |
| 74 DoodleConfig::~DoodleConfig() = default; | 73 DoodleConfig::~DoodleConfig() = default; |
| 75 | 74 |
| 76 DoodleFetcherImpl::DoodleFetcherImpl( | 75 DoodleFetcherImpl::DoodleFetcherImpl( |
| 77 scoped_refptr<net::URLRequestContextGetter> download_context, | 76 scoped_refptr<net::URLRequestContextGetter> download_context, |
| 78 GoogleURLTracker* google_url_tracker, | 77 GoogleURLTracker* google_url_tracker, |
| 79 const ParseJSONCallback& json_parsing_callback) | 78 const ParseJSONCallback& json_parsing_callback) |
| 80 : download_context_(download_context), | 79 : download_context_(download_context), |
| 81 json_parsing_callback_(json_parsing_callback), | 80 json_parsing_callback_(json_parsing_callback), |
| 82 google_url_tracker_(google_url_tracker), | 81 google_url_tracker_(google_url_tracker), |
| 83 clock_(new base::DefaultClock()), | |
| 84 weak_ptr_factory_(this) { | 82 weak_ptr_factory_(this) { |
| 85 DCHECK(google_url_tracker_); | 83 DCHECK(google_url_tracker_); |
| 86 } | 84 } |
| 87 | 85 |
| 88 DoodleFetcherImpl::~DoodleFetcherImpl() = default; | 86 DoodleFetcherImpl::~DoodleFetcherImpl() = default; |
| 89 | 87 |
| 90 void DoodleFetcherImpl::FetchDoodle(FinishedCallback callback) { | 88 void DoodleFetcherImpl::FetchDoodle(FinishedCallback callback) { |
| 91 if (IsFetchInProgress()) { | 89 if (IsFetchInProgress()) { |
| 92 callbacks_.push_back(std::move(callback)); | 90 callbacks_.push_back(std::move(callback)); |
| 93 return; // The callback will be called for the existing request's results. | 91 return; // The callback will be called for the existing request's results. |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 config->fullpage_interactive_url = | 196 config->fullpage_interactive_url = |
| 199 ParseRelativeUrl(ddljson, "fullpage_interactive_url"); | 197 ParseRelativeUrl(ddljson, "fullpage_interactive_url"); |
| 200 | 198 |
| 201 config->doodle_type = ParseDoodleType(ddljson); | 199 config->doodle_type = ParseDoodleType(ddljson); |
| 202 ddljson.GetString("alt_text", &config->alt_text); | 200 ddljson.GetString("alt_text", &config->alt_text); |
| 203 ddljson.GetString("interactive_html", &config->interactive_html); | 201 ddljson.GetString("interactive_html", &config->interactive_html); |
| 204 | 202 |
| 205 // The JSON doesn't guarantee the number to fit into an int. | 203 // The JSON doesn't guarantee the number to fit into an int. |
| 206 double ttl = 0; // Expires immediately if the parameter is missing. | 204 double ttl = 0; // Expires immediately if the parameter is missing. |
| 207 if (!ddljson.GetDouble("time_to_live_ms", &ttl) || ttl < 0) { | 205 if (!ddljson.GetDouble("time_to_live_ms", &ttl) || ttl < 0) { |
| 208 DLOG(WARNING) << "No valid Doodle image TTL present in ddljson!"; | 206 DLOG(WARNING) << "No valid Doodle TTL present in ddljson!"; |
| 209 ttl = 0; | 207 ttl = 0; |
| 210 } | 208 } |
| 209 // TODO(treib,fhorschig): Should this logic live in the service instead? | |
|
fhorschig
2017/03/01 15:40:52
In my opinion absolutely. The clamping was mainly
Marc Treib
2017/03/02 09:28:02
Reformulated without a question :)
| |
| 211 if (ttl > kMaxTimeToLiveMS) { | 210 if (ttl > kMaxTimeToLiveMS) { |
| 212 ttl = kMaxTimeToLiveMS; | 211 ttl = kMaxTimeToLiveMS; |
| 213 DLOG(WARNING) << "Clamping Doodle image TTL to 30 days!"; | 212 DLOG(WARNING) << "Clamping Doodle TTL to 30 days!"; |
| 214 } | 213 } |
| 215 config->expiry_date = clock_->Now() + base::TimeDelta::FromMillisecondsD(ttl); | 214 config->time_to_live = base::TimeDelta::FromMillisecondsD(ttl); |
| 216 } | 215 } |
| 217 | 216 |
| 218 GURL DoodleFetcherImpl::ParseRelativeUrl( | 217 GURL DoodleFetcherImpl::ParseRelativeUrl( |
| 219 const base::DictionaryValue& dict_value, | 218 const base::DictionaryValue& dict_value, |
| 220 const std::string& key) const { | 219 const std::string& key) const { |
| 221 std::string str_url; | 220 std::string str_url; |
| 222 dict_value.GetString(key, &str_url); | 221 dict_value.GetString(key, &str_url); |
| 223 if (str_url.empty()) { | 222 if (str_url.empty()) { |
| 224 return GURL(); | 223 return GURL(); |
| 225 } | 224 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 237 | 236 |
| 238 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { | 237 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { |
| 239 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); | 238 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); |
| 240 if (cmd_line_url.is_valid()) { | 239 if (cmd_line_url.is_valid()) { |
| 241 return cmd_line_url; | 240 return cmd_line_url; |
| 242 } | 241 } |
| 243 return google_url_tracker_->google_url(); | 242 return google_url_tracker_->google_url(); |
| 244 } | 243 } |
| 245 | 244 |
| 246 } // namespace doodle | 245 } // namespace doodle |
| OLD | NEW |