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_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_); |
| 17 } | 18 } |
| 18 | 19 |
| 19 DoodleService::~DoodleService() = default; | 20 DoodleService::~DoodleService() = default; |
| 20 | 21 |
| 21 void DoodleService::AddObserver(Observer* observer) { | 22 void DoodleService::AddObserver(Observer* observer) { |
| 22 observers_.AddObserver(observer); | 23 observers_.AddObserver(observer); |
| 23 } | 24 } |
| 24 | 25 |
| 25 void DoodleService::RemoveObserver(Observer* observer) { | 26 void DoodleService::RemoveObserver(Observer* observer) { |
| 26 observers_.RemoveObserver(observer); | 27 observers_.RemoveObserver(observer); |
| 27 } | 28 } |
| 28 | 29 |
| 29 void DoodleService::Refresh() { | 30 void DoodleService::Refresh() { |
| 30 fetcher_->FetchDoodle( | 31 fetcher_->FetchDoodle( |
| 31 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); | 32 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); |
| 32 } | 33 } |
| 33 | 34 |
| 34 void DoodleService::DoodleFetched( | 35 void DoodleService::DoodleFetched( |
| 35 DoodleState state, | 36 DoodleState state, |
| 36 base::TimeDelta time_to_live, | 37 base::TimeDelta time_to_live, |
| 37 const base::Optional<DoodleConfig>& doodle_config) { | 38 const base::Optional<DoodleConfig>& doodle_config) { |
| 39 // Handle the case where the new config is already expired. | |
| 40 bool expired = time_to_live <= base::TimeDelta(); | |
| 41 const base::Optional<DoodleConfig>& new_config = | |
| 42 expired ? base::nullopt : doodle_config; | |
|
fhorschig
2017/03/02 20:12:59
nit: A reference isn't expensive, so why no go for
Marc Treib
2017/03/03 10:06:57
This won't work; you can't assign to a reference.
fhorschig
2017/03/03 10:45:07
Oh, sorry.
| |
| 43 | |
| 38 // If nothing changed, then there's nothing to do. Note that this checks both | 44 // If nothing changed, then there's nothing to do. Note that this checks both |
| 39 // for existence changes as well as changes of the configs themselves. | 45 // for existence changes as well as changes of the configs themselves. |
| 40 if (cached_config_ == doodle_config) { | 46 if (cached_config_ == new_config) { |
| 41 // TODO(treib): Once we support expiry, update the time to live here. | 47 // Even if the configs are identical, the time-to-live might have changed. |
| 48 UpdateTimeToLive(time_to_live); | |
|
fhorschig
2017/03/02 20:12:59
The reason you do it twice instead of earlier is b
Marc Treib
2017/03/03 10:06:57
No, the reason was so that I could early-out if th
| |
| 42 return; | 49 return; |
| 43 } | 50 } |
| 44 | 51 |
| 45 // Update the cache and notify observers. | 52 // Update the cache and notify observers. |
| 46 cached_config_ = doodle_config; | 53 cached_config_ = new_config; |
| 47 for (auto& observer : observers_) { | 54 for (auto& observer : observers_) { |
| 48 observer.OnDoodleConfigUpdated(cached_config_); | 55 observer.OnDoodleConfigUpdated(cached_config_); |
| 49 } | 56 } |
| 57 | |
| 58 UpdateTimeToLive(time_to_live); | |
| 59 } | |
| 60 | |
| 61 void DoodleService::UpdateTimeToLive(base::TimeDelta time_to_live) { | |
| 62 // (Re-)schedule the cache expiry. | |
| 63 if (cached_config_.has_value()) { | |
| 64 expiry_timer_->Start( | |
| 65 FROM_HERE, time_to_live, | |
| 66 base::Bind(&DoodleService::DoodleExpired, base::Unretained(this))); | |
| 67 } else { | |
| 68 expiry_timer_->Stop(); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void DoodleService::DoodleExpired() { | |
| 73 DCHECK(cached_config_.has_value()); | |
| 74 cached_config_.reset(); | |
| 75 for (auto& observer : observers_) { | |
| 76 observer.OnDoodleConfigUpdated(cached_config_); | |
| 77 } | |
| 50 } | 78 } |
| 51 | 79 |
| 52 } // namespace doodle | 80 } // namespace doodle |
| OLD | NEW |