| 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/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "components/data_use_measurement/core/data_use_user_data.h" | 13 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 14 #include "components/google/core/browser/google_url_tracker.h" | 14 #include "components/google/core/browser/google_url_tracker.h" |
| 15 #include "components/google/core/browser/google_util.h" | 15 #include "components/google/core/browser/google_util.h" |
| 16 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 17 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 18 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
| 19 | 19 |
| 20 using net::URLFetcher; | 20 using net::URLFetcher; |
| 21 | 21 |
| 22 namespace doodle { | 22 namespace doodle { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const double kMaxTimeToLiveMS = 30.0 * 24 * 60 * 60 * 1000; // 30 days | |
| 27 | |
| 28 // "/async/ddljson" is the base API path. "ntp:1" identifies this request as | 26 // "/async/ddljson" is the base API path. "ntp:1" identifies this request as |
| 29 // being for a New Tab page. The "graybg:" param specifies whether the doodle | 27 // being for a New Tab page. The "graybg:" param specifies whether the doodle |
| 30 // will be displayed on a gray background. | 28 // will be displayed on a gray background. |
| 31 const char kDoodleConfigPathFormat[] = "/async/ddljson?async=ntp:1,graybg:%d"; | 29 const char kDoodleConfigPathFormat[] = "/async/ddljson?async=ntp:1,graybg:%d"; |
| 32 | 30 |
| 33 std::string StripSafetyPreamble(const std::string& json) { | 31 std::string StripSafetyPreamble(const std::string& json) { |
| 34 // The response may start with )]}'. Ignore this. | 32 // The response may start with )]}'. Ignore this. |
| 35 const char kResponsePreamble[] = ")]}'"; | 33 const char kResponsePreamble[] = ")]}'"; |
| 36 | 34 |
| 37 base::StringPiece json_sp(json); | 35 base::StringPiece json_sp(json); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 base::nullopt); | 139 base::nullopt); |
| 142 } | 140 } |
| 143 | 141 |
| 144 base::Optional<DoodleConfig> DoodleFetcherImpl::ParseDoodleConfigAndTimeToLive( | 142 base::Optional<DoodleConfig> DoodleFetcherImpl::ParseDoodleConfigAndTimeToLive( |
| 145 const base::DictionaryValue& ddljson, | 143 const base::DictionaryValue& ddljson, |
| 146 base::TimeDelta* time_to_live) const { | 144 base::TimeDelta* time_to_live) const { |
| 147 base::Optional<DoodleConfig> doodle = | 145 base::Optional<DoodleConfig> doodle = |
| 148 DoodleConfig::FromDictionary(ddljson, GetGoogleBaseUrl()); | 146 DoodleConfig::FromDictionary(ddljson, GetGoogleBaseUrl()); |
| 149 | 147 |
| 150 // The JSON doesn't guarantee the number to fit into an int. | 148 // The JSON doesn't guarantee the number to fit into an int. |
| 151 double ttl = 0; // Expires immediately if the parameter is missing. | 149 double ttl_ms = 0; // Expires immediately if the parameter is missing. |
| 152 if (!ddljson.GetDouble("time_to_live_ms", &ttl) || ttl < 0) { | 150 if (!ddljson.GetDouble("time_to_live_ms", &ttl_ms) || ttl_ms < 0) { |
| 153 DLOG(WARNING) << "No valid Doodle TTL present in ddljson!"; | 151 DLOG(WARNING) << "No valid Doodle TTL present in ddljson!"; |
| 154 ttl = 0; | 152 ttl_ms = 0; |
| 155 } | 153 } |
| 156 // TODO(treib,fhorschig): Move this logic into the service. | 154 *time_to_live = base::TimeDelta::FromMillisecondsD(ttl_ms); |
| 157 if (ttl > kMaxTimeToLiveMS) { | |
| 158 ttl = kMaxTimeToLiveMS; | |
| 159 DLOG(WARNING) << "Clamping Doodle TTL to 30 days!"; | |
| 160 } | |
| 161 *time_to_live = base::TimeDelta::FromMillisecondsD(ttl); | |
| 162 | 155 |
| 163 return doodle; | 156 return doodle; |
| 164 } | 157 } |
| 165 | 158 |
| 166 void DoodleFetcherImpl::RespondToAllCallbacks( | 159 void DoodleFetcherImpl::RespondToAllCallbacks( |
| 167 DoodleState state, | 160 DoodleState state, |
| 168 base::TimeDelta time_to_live, | 161 base::TimeDelta time_to_live, |
| 169 const base::Optional<DoodleConfig>& config) { | 162 const base::Optional<DoodleConfig>& config) { |
| 170 for (auto& callback : callbacks_) { | 163 for (auto& callback : callbacks_) { |
| 171 std::move(callback).Run(state, time_to_live, config); | 164 std::move(callback).Run(state, time_to_live, config); |
| 172 } | 165 } |
| 173 callbacks_.clear(); | 166 callbacks_.clear(); |
| 174 } | 167 } |
| 175 | 168 |
| 176 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { | 169 GURL DoodleFetcherImpl::GetGoogleBaseUrl() const { |
| 177 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); | 170 GURL cmd_line_url = google_util::CommandLineGoogleBaseURL(); |
| 178 if (cmd_line_url.is_valid()) { | 171 if (cmd_line_url.is_valid()) { |
| 179 return cmd_line_url; | 172 return cmd_line_url; |
| 180 } | 173 } |
| 181 return google_url_tracker_->google_url(); | 174 return google_url_tracker_->google_url(); |
| 182 } | 175 } |
| 183 | 176 |
| 184 } // namespace doodle | 177 } // namespace doodle |
| OLD | NEW |