| 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/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 observers_.RemoveObserver(observer); | 80 observers_.RemoveObserver(observer); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void DoodleService::Refresh() { | 83 void DoodleService::Refresh() { |
| 84 base::TimeTicks now_ticks = tick_clock_->NowTicks(); | 84 base::TimeTicks now_ticks = tick_clock_->NowTicks(); |
| 85 // Check if we have passed the minimum refresh interval. | 85 // Check if we have passed the minimum refresh interval. |
| 86 base::TimeDelta time_since_fetch = now_ticks - last_successful_fetch_; | 86 base::TimeDelta time_since_fetch = now_ticks - last_successful_fetch_; |
| 87 if (time_since_fetch < min_refresh_interval_) { | 87 if (time_since_fetch < min_refresh_interval_) { |
| 88 RecordDownloadMetrics(OUTCOME_REFRESH_INTERVAL_NOT_PASSED, | 88 RecordDownloadMetrics(OUTCOME_REFRESH_INTERVAL_NOT_PASSED, |
| 89 base::TimeDelta()); | 89 base::TimeDelta()); |
| 90 for (auto& observer : observers_) { |
| 91 observer.OnDoodleConfigRevalidated(/*from_cache=*/true); |
| 92 } |
| 90 return; | 93 return; |
| 91 } | 94 } |
| 92 fetcher_->FetchDoodle(base::BindOnce(&DoodleService::DoodleFetched, | 95 fetcher_->FetchDoodle(base::BindOnce(&DoodleService::DoodleFetched, |
| 93 base::Unretained(this), now_ticks)); | 96 base::Unretained(this), now_ticks)); |
| 94 } | 97 } |
| 95 | 98 |
| 96 // static | 99 // static |
| 97 bool DoodleService::DownloadOutcomeIsSuccess(DownloadOutcome outcome) { | 100 bool DoodleService::DownloadOutcomeIsSuccess(DownloadOutcome outcome) { |
| 98 switch (outcome) { | 101 switch (outcome) { |
| 99 case OUTCOME_NEW_DOODLE: | 102 case OUTCOME_NEW_DOODLE: |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 188 |
| 186 // Handle the case where the new config is already expired. | 189 // Handle the case where the new config is already expired. |
| 187 bool expired = time_to_live <= base::TimeDelta(); | 190 bool expired = time_to_live <= base::TimeDelta(); |
| 188 const base::Optional<DoodleConfig>& new_config = | 191 const base::Optional<DoodleConfig>& new_config = |
| 189 expired ? base::nullopt : doodle_config; | 192 expired ? base::nullopt : doodle_config; |
| 190 | 193 |
| 191 // Determine the download outcome *before* updating the cached config. | 194 // Determine the download outcome *before* updating the cached config. |
| 192 DownloadOutcome outcome = | 195 DownloadOutcome outcome = |
| 193 DetermineDownloadOutcome(cached_config_, new_config, state, expired); | 196 DetermineDownloadOutcome(cached_config_, new_config, state, expired); |
| 194 | 197 |
| 195 // If the config changed, update our cache and notify observers. | |
| 196 // Note that this checks both for existence changes as well as changes of the | 198 // Note that this checks both for existence changes as well as changes of the |
| 197 // configs themselves. | 199 // configs themselves. |
| 198 if (cached_config_ != new_config) { | 200 if (cached_config_ != new_config) { |
| 199 UpdateCachedConfig(time_to_live, new_config); | 201 UpdateCachedConfig(time_to_live, new_config); |
| 200 | 202 |
| 201 for (auto& observer : observers_) { | 203 for (auto& observer : observers_) { |
| 202 observer.OnDoodleConfigUpdated(cached_config_); | 204 observer.OnDoodleConfigUpdated(cached_config_); |
| 203 } | 205 } |
| 206 } else { |
| 207 for (auto& observer : observers_) { |
| 208 observer.OnDoodleConfigRevalidated(/*from_cache=*/false); |
| 209 } |
| 204 } | 210 } |
| 205 | 211 |
| 206 // Even if the configs are identical, the time-to-live might have changed. | 212 // Even if the configs are identical, the time-to-live might have changed. |
| 207 // (Re-)schedule the cache expiry. | 213 // (Re-)schedule the cache expiry. |
| 208 if (cached_config_.has_value()) { | 214 if (cached_config_.has_value()) { |
| 209 expiry_timer_->Start( | 215 expiry_timer_->Start( |
| 210 FROM_HERE, time_to_live, | 216 FROM_HERE, time_to_live, |
| 211 base::Bind(&DoodleService::DoodleExpired, base::Unretained(this))); | 217 base::Bind(&DoodleService::DoodleExpired, base::Unretained(this))); |
| 212 } else { | 218 } else { |
| 213 expiry_timer_->Stop(); | 219 expiry_timer_->Stop(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 233 pref_service_->ClearPref(prefs::kCachedConfigExpiry); | 239 pref_service_->ClearPref(prefs::kCachedConfigExpiry); |
| 234 } | 240 } |
| 235 } | 241 } |
| 236 | 242 |
| 237 void DoodleService::DoodleExpired() { | 243 void DoodleService::DoodleExpired() { |
| 238 DCHECK(cached_config_.has_value()); | 244 DCHECK(cached_config_.has_value()); |
| 239 HandleNewConfig(DoodleState::NO_DOODLE, base::TimeDelta(), base::nullopt); | 245 HandleNewConfig(DoodleState::NO_DOODLE, base::TimeDelta(), base::nullopt); |
| 240 } | 246 } |
| 241 | 247 |
| 242 } // namespace doodle | 248 } // namespace doodle |
| OLD | NEW |