| 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_service.h" | 5 #include "components/doodle/doodle_service.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.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/doodle/pref_names.h" | 13 #include "components/doodle/pref_names.h" |
| 14 #include "components/prefs/pref_registry.h" | 14 #include "components/prefs/pref_registry.h" |
| 15 #include "components/prefs/pref_registry_simple.h" | 15 #include "components/prefs/pref_registry_simple.h" |
| 16 #include "components/prefs/pref_service.h" | 16 #include "components/prefs/pref_service.h" |
| 17 | 17 |
| 18 namespace doodle { | 18 namespace doodle { |
| 19 | 19 |
| 20 namespace { |
| 21 |
| 22 const int64_t kMaxTimeToLiveSecs = 30 * 24 * 60 * 60; // 30 days |
| 23 |
| 24 } // namespace |
| 25 |
| 20 // static | 26 // static |
| 21 void DoodleService::RegisterProfilePrefs(PrefRegistrySimple* pref_registry) { | 27 void DoodleService::RegisterProfilePrefs(PrefRegistrySimple* pref_registry) { |
| 22 pref_registry->RegisterDictionaryPref(prefs::kCachedConfig, | 28 pref_registry->RegisterDictionaryPref(prefs::kCachedConfig, |
| 23 new base::DictionaryValue(), | 29 new base::DictionaryValue(), |
| 24 PrefRegistry::LOSSY_PREF); | 30 PrefRegistry::LOSSY_PREF); |
| 25 pref_registry->RegisterInt64Pref(prefs::kCachedConfigExpiry, 0, | 31 pref_registry->RegisterInt64Pref(prefs::kCachedConfigExpiry, 0, |
| 26 PrefRegistry::LOSSY_PREF); | 32 PrefRegistry::LOSSY_PREF); |
| 27 } | 33 } |
| 28 | 34 |
| 29 DoodleService::DoodleService(PrefService* pref_service, | 35 DoodleService::DoodleService(PrefService* pref_service, |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 const base::Optional<DoodleConfig>& doodle_config) { | 147 const base::Optional<DoodleConfig>& doodle_config) { |
| 142 base::TimeDelta download_time = tick_clock_->NowTicks() - start_time; | 148 base::TimeDelta download_time = tick_clock_->NowTicks() - start_time; |
| 143 DownloadOutcome outcome = HandleNewConfig(state, time_to_live, doodle_config); | 149 DownloadOutcome outcome = HandleNewConfig(state, time_to_live, doodle_config); |
| 144 RecordDownloadMetrics(outcome, download_time); | 150 RecordDownloadMetrics(outcome, download_time); |
| 145 } | 151 } |
| 146 | 152 |
| 147 DoodleService::DownloadOutcome DoodleService::HandleNewConfig( | 153 DoodleService::DownloadOutcome DoodleService::HandleNewConfig( |
| 148 DoodleState state, | 154 DoodleState state, |
| 149 base::TimeDelta time_to_live, | 155 base::TimeDelta time_to_live, |
| 150 const base::Optional<DoodleConfig>& doodle_config) { | 156 const base::Optional<DoodleConfig>& doodle_config) { |
| 157 // Clamp the time-to-live to some reasonable maximum. |
| 158 if (time_to_live.InSeconds() > kMaxTimeToLiveSecs) { |
| 159 time_to_live = base::TimeDelta::FromSeconds(kMaxTimeToLiveSecs); |
| 160 DLOG(WARNING) << "Clamping TTL to " << kMaxTimeToLiveSecs << " seconds!"; |
| 161 } |
| 162 |
| 151 // Handle the case where the new config is already expired. | 163 // Handle the case where the new config is already expired. |
| 152 bool expired = time_to_live <= base::TimeDelta(); | 164 bool expired = time_to_live <= base::TimeDelta(); |
| 153 const base::Optional<DoodleConfig>& new_config = | 165 const base::Optional<DoodleConfig>& new_config = |
| 154 expired ? base::nullopt : doodle_config; | 166 expired ? base::nullopt : doodle_config; |
| 155 | 167 |
| 156 // Determine the download outcome *before* updating the cached config. | 168 // Determine the download outcome *before* updating the cached config. |
| 157 DownloadOutcome outcome = | 169 DownloadOutcome outcome = |
| 158 DetermineDownloadOutcome(cached_config_, new_config, state, expired); | 170 DetermineDownloadOutcome(cached_config_, new_config, state, expired); |
| 159 | 171 |
| 160 // If the config changed, update our cache and notify observers. | 172 // If the config changed, update our cache and notify observers. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 pref_service_->ClearPref(prefs::kCachedConfigExpiry); | 210 pref_service_->ClearPref(prefs::kCachedConfigExpiry); |
| 199 } | 211 } |
| 200 } | 212 } |
| 201 | 213 |
| 202 void DoodleService::DoodleExpired() { | 214 void DoodleService::DoodleExpired() { |
| 203 DCHECK(cached_config_.has_value()); | 215 DCHECK(cached_config_.has_value()); |
| 204 HandleNewConfig(DoodleState::NO_DOODLE, base::TimeDelta(), base::nullopt); | 216 HandleNewConfig(DoodleState::NO_DOODLE, base::TimeDelta(), base::nullopt); |
| 205 } | 217 } |
| 206 | 218 |
| 207 } // namespace doodle | 219 } // namespace doodle |
| OLD | NEW |