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 | 11 |
| 11 namespace doodle { | 12 namespace doodle { |
| 12 | 13 |
| 13 DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher) | 14 DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher) |
| 14 : fetcher_(std::move(fetcher)) { | 15 : fetcher_(std::move(fetcher)) { |
| 15 DCHECK(fetcher_); | 16 DCHECK(fetcher_); |
| 16 } | 17 } |
| 17 | 18 |
| 18 DoodleService::~DoodleService() = default; | 19 DoodleService::~DoodleService() = default; |
| 19 | 20 |
| 20 void DoodleService::AddObserver(Observer* observer) { | 21 void DoodleService::AddObserver(Observer* observer) { |
| 21 observers_.AddObserver(observer); | 22 observers_.AddObserver(observer); |
| 22 } | 23 } |
| 23 | 24 |
| 24 void DoodleService::RemoveObserver(Observer* observer) { | 25 void DoodleService::RemoveObserver(Observer* observer) { |
| 25 observers_.RemoveObserver(observer); | 26 observers_.RemoveObserver(observer); |
| 26 } | 27 } |
| 27 | 28 |
| 28 void DoodleService::Refresh() { | 29 void DoodleService::Refresh() { |
| 29 fetcher_->FetchDoodle( | 30 fetcher_->FetchDoodle( |
| 30 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); | 31 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); |
| 31 } | 32 } |
| 32 | 33 |
| 33 void DoodleService::DoodleFetched( | 34 void DoodleService::DoodleFetched( |
| 34 DoodleState state, | 35 DoodleState state, |
| 36 base::TimeDelta time_to_live, | |
| 35 const base::Optional<DoodleConfig>& doodle_config) { | 37 const base::Optional<DoodleConfig>& doodle_config) { |
| 36 if (!cached_config_.has_value() && !doodle_config.has_value()) { | 38 // If nothing changed, then there's nothing to do. Note that this checks both |
| 37 // There was no config before and we didn't get a new one, so there's | 39 // for existence changes as well as changes of the configs themselves. |
| 38 // nothing to do. | 40 if (cached_config_ == doodle_config) { |
| 41 // TODO(treib): Once we support expiry, update the time to live here. | |
| 39 return; | 42 return; |
| 40 } | 43 } |
| 41 | 44 |
| 42 bool notify = false; | 45 // Update the cache and notify observers. |
| 43 if (cached_config_.has_value() != doodle_config.has_value()) { | |
| 44 // We got a new config, or an existing one went away. | |
| 45 notify = true; | |
| 46 } else { | |
| 47 // There was a config both before and after the update. Notify observers | |
| 48 // only if something relevant changed. | |
| 49 notify = !cached_config_.value().IsEquivalent(doodle_config.value()); | |
| 50 } | |
| 51 | |
| 52 // In any case, update the cache. | |
| 53 cached_config_ = doodle_config; | 46 cached_config_ = doodle_config; |
| 54 | 47 for (auto& observer : observers_) { |
|
fhorschig
2017/03/02 13:18:08
Is non-const auto& something we are allowed to use
Marc Treib
2017/03/02 14:30:08
Yes (for locally-scoped stuff like this).
This is
| |
| 55 if (notify) { | 48 observer.OnDoodleConfigUpdated(cached_config_); |
| 56 for (auto& observer : observers_) { | |
| 57 observer.OnDoodleConfigUpdated(cached_config_); | |
| 58 } | |
| 59 } | 49 } |
| 60 } | 50 } |
| 61 | 51 |
| 62 } // namespace doodle | 52 } // namespace doodle |
| OLD | NEW |