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 DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher) | |
| 14 : fetcher_(std::move(fetcher)) { | |
| 15 DCHECK(fetcher_); | |
| 16 } | |
| 17 | |
| 18 DoodleService::~DoodleService() = default; | |
| 19 | |
| 20 void DoodleService::AddObserver(Observer* observer) { | |
| 21 observers_.AddObserver(observer); | |
| 22 } | |
| 23 | |
| 24 void DoodleService::RemoveObserver(Observer* observer) { | |
| 25 observers_.RemoveObserver(observer); | |
| 26 } | |
| 27 | |
| 28 void DoodleService::Refresh() { | |
| 29 fetcher_->FetchDoodle( | |
| 30 base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); | |
| 31 } | |
| 32 | |
| 33 void DoodleService::DoodleFetched( | |
| 34 DoodleState state, | |
| 35 const base::Optional<DoodleConfig>& doodle_config) { | |
| 36 // If there is no config before or after the update, there's nothing to do. | |
|
vitaliii
2017/02/28 11:37:17
s/or/and
Marc Treib
2017/02/28 13:06:49
Actually, neither formulation is really unambiguou
vitaliii
2017/02/28 13:13:28
Both messages feel to me like !cached_config_.has_
Marc Treib
2017/02/28 13:34:22
I agree that your formulation is much better. Done
| |
| 37 if (!cached_config_.has_value() && !doodle_config.has_value()) { | |
| 38 return; | |
| 39 } | |
| 40 | |
| 41 bool notify = false; | |
| 42 if (cached_config_.has_value() != doodle_config.has_value()) { | |
| 43 // We got a new config, or an existing one went away. | |
| 44 notify = true; | |
| 45 } else { | |
| 46 // There was a config both before and after the update. Notify observers | |
| 47 // only if something relevant changed. | |
| 48 notify = !cached_config_.value().IsEquivalent(doodle_config.value()); | |
| 49 } | |
| 50 | |
| 51 // In any case, update the cache. | |
| 52 cached_config_ = doodle_config; | |
| 53 | |
| 54 if (notify) { | |
| 55 for (auto& observer : observers_) { | |
| 56 observer.OnDoodleConfigUpdated(cached_config_); | |
| 57 } | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 } // namespace doodle | |
| OLD | NEW |