| 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/time/time.h" | 10 #include "base/time/time.h" |
| 11 | 11 |
| 12 namespace doodle { | 12 namespace doodle { |
| 13 | 13 |
| 14 DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher) | 14 DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher, |
| 15 : fetcher_(std::move(fetcher)) { | 15 std::unique_ptr<base::OneShotTimer> expiry_timer) |
| 16 : fetcher_(std::move(fetcher)), expiry_timer_(std::move(expiry_timer)) { |
| 16 DCHECK(fetcher_); | 17 DCHECK(fetcher_); |
| 18 DCHECK(expiry_timer_); |
| 17 } | 19 } |
| 18 | 20 |
| 19 DoodleService::~DoodleService() = default; | 21 DoodleService::~DoodleService() = default; |
| 20 | 22 |
| 21 void DoodleService::AddObserver(Observer* observer) { | 23 void DoodleService::AddObserver(Observer* observer) { |
| 22 observers_.AddObserver(observer); | 24 observers_.AddObserver(observer); |
| 23 } | 25 } |
| 24 | 26 |
| 25 void DoodleService::RemoveObserver(Observer* observer) { | 27 void DoodleService::RemoveObserver(Observer* observer) { |
| 26 observers_.RemoveObserver(observer); | 28 observers_.RemoveObserver(observer); |
| 27 } | 29 } |
| 28 | 30 |
| 29 void DoodleService::Refresh() { | 31 void DoodleService::Refresh() { |
| 30 fetcher_->FetchDoodle( | 32 fetcher_->FetchDoodle( |
| 31 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); | 33 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); |
| 32 } | 34 } |
| 33 | 35 |
| 34 void DoodleService::DoodleFetched( | 36 void DoodleService::DoodleFetched( |
| 35 DoodleState state, | 37 DoodleState state, |
| 36 base::TimeDelta time_to_live, | 38 base::TimeDelta time_to_live, |
| 37 const base::Optional<DoodleConfig>& doodle_config) { | 39 const base::Optional<DoodleConfig>& doodle_config) { |
| 38 // If nothing changed, then there's nothing to do. Note that this checks both | 40 // Handle the case where the new config is already expired. |
| 39 // for existence changes as well as changes of the configs themselves. | 41 bool expired = time_to_live <= base::TimeDelta(); |
| 40 if (cached_config_ == doodle_config) { | 42 const base::Optional<DoodleConfig>& new_config = |
| 41 // TODO(treib): Once we support expiry, update the time to live here. | 43 expired ? base::nullopt : doodle_config; |
| 42 return; | 44 |
| 45 // If the config changed, update our cache and notify observers. |
| 46 // Note that this checks both for existence changes as well as changes of the |
| 47 // configs themselves. |
| 48 if (cached_config_ != new_config) { |
| 49 cached_config_ = new_config; |
| 50 for (auto& observer : observers_) { |
| 51 observer.OnDoodleConfigUpdated(cached_config_); |
| 52 } |
| 43 } | 53 } |
| 44 | 54 |
| 45 // Update the cache and notify observers. | 55 // Even if the configs are identical, the time-to-live might have changed. |
| 46 cached_config_ = doodle_config; | 56 UpdateTimeToLive(time_to_live); |
| 57 } |
| 58 |
| 59 void DoodleService::UpdateTimeToLive(base::TimeDelta time_to_live) { |
| 60 // (Re-)schedule the cache expiry. |
| 61 if (cached_config_.has_value()) { |
| 62 expiry_timer_->Start( |
| 63 FROM_HERE, time_to_live, |
| 64 base::Bind(&DoodleService::DoodleExpired, base::Unretained(this))); |
| 65 } else { |
| 66 expiry_timer_->Stop(); |
| 67 } |
| 68 } |
| 69 |
| 70 void DoodleService::DoodleExpired() { |
| 71 DCHECK(cached_config_.has_value()); |
| 72 cached_config_.reset(); |
| 47 for (auto& observer : observers_) { | 73 for (auto& observer : observers_) { |
| 48 observer.OnDoodleConfigUpdated(cached_config_); | 74 observer.OnDoodleConfigUpdated(cached_config_); |
| 49 } | 75 } |
| 50 } | 76 } |
| 51 | 77 |
| 52 } // namespace doodle | 78 } // namespace doodle |
| OLD | NEW |