Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/doodle/doodle_service.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 | |
| 11 namespace doodle { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 bool DoodleConfigsAreEquivalent(const DoodleConfig& lhs, | |
| 16 const DoodleConfig& rhs) { | |
| 17 // TODO! | |
| 18 return false; | |
| 19 } | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher) | |
| 24 : fetcher_(std::move(fetcher)) { | |
| 25 DCHECK(fetcher_); | |
| 26 } | |
| 27 | |
| 28 DoodleService::~DoodleService() = default; | |
| 29 | |
| 30 void DoodleService::AddObserver(Observer* observer) { | |
| 31 observers_.AddObserver(observer); | |
| 32 observer->OnDoodleConfigUpdated(cached_config_); | |
|
fhorschig
2017/02/24 15:33:14
If you decide to (re)move this line, could you DCH
Marc Treib
2017/02/24 16:18:51
...sorry, I don't follow?
fhorschig
2017/02/24 16:38:20
I mean a "DCHECK(observer);".
(Currently, it would
Marc Treib
2017/02/27 13:55:40
ObserverList already checks that the object isn't
| |
| 33 } | |
| 34 | |
| 35 void DoodleService::RemoveObserver(Observer* observer) { | |
| 36 observers_.RemoveObserver(observer); | |
| 37 } | |
| 38 | |
| 39 void DoodleService::Refresh() { | |
| 40 fetcher_->FetchDoodle( | |
| 41 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); | |
| 42 } | |
| 43 | |
| 44 void DoodleService::DoodleFetched( | |
| 45 DoodleState state, | |
| 46 const base::Optional<DoodleConfig>& doodle_config) { | |
| 47 // If there is no config before or after the update, there's nothing to do. | |
| 48 if (!cached_config_.has_value() && !doodle_config.has_value()) { | |
| 49 return; | |
| 50 } | |
| 51 | |
| 52 bool notify = false; | |
| 53 if (cached_config_.has_value() != doodle_config.has_value()) { | |
| 54 // We got a new config, or an existing one went away. | |
| 55 notify = true; | |
| 56 } else { | |
| 57 // There was a config both before and after the update. Notify observers | |
| 58 // only if something relevant changed. | |
| 59 notify = !DoodleConfigsAreEquivalent(cached_config_.value(), | |
| 60 doodle_config.value()); | |
| 61 } | |
| 62 | |
| 63 // In any case, update the cache. | |
| 64 cached_config_ = doodle_config; | |
| 65 | |
| 66 if (notify) { | |
| 67 for (auto& observer : observers_) { | |
| 68 observer.OnDoodleConfigUpdated(cached_config_); | |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 } // namespace doodle | |
| OLD | NEW |