Chromium Code Reviews| Index: components/doodle/doodle_service.cc |
| diff --git a/components/doodle/doodle_service.cc b/components/doodle/doodle_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..de0ae2bde38f3468b685b153a19bf1cd60f8ef92 |
| --- /dev/null |
| +++ b/components/doodle/doodle_service.cc |
| @@ -0,0 +1,61 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/doodle/doodle_service.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/bind.h" |
| + |
| +namespace doodle { |
| + |
| +DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher) |
| + : fetcher_(std::move(fetcher)) { |
| + DCHECK(fetcher_); |
| +} |
| + |
| +DoodleService::~DoodleService() = default; |
| + |
| +void DoodleService::AddObserver(Observer* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void DoodleService::RemoveObserver(Observer* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| +void DoodleService::Refresh() { |
| + fetcher_->FetchDoodle( |
| + base::BindOnce(&DoodleService::DoodleFetched, base::Unretained(this))); |
| +} |
| + |
| +void DoodleService::DoodleFetched( |
| + DoodleState state, |
| + const base::Optional<DoodleConfig>& doodle_config) { |
| + // 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
|
| + if (!cached_config_.has_value() && !doodle_config.has_value()) { |
| + return; |
| + } |
| + |
| + bool notify = false; |
| + if (cached_config_.has_value() != doodle_config.has_value()) { |
| + // We got a new config, or an existing one went away. |
| + notify = true; |
| + } else { |
| + // There was a config both before and after the update. Notify observers |
| + // only if something relevant changed. |
| + notify = !cached_config_.value().IsEquivalent(doodle_config.value()); |
| + } |
| + |
| + // In any case, update the cache. |
| + cached_config_ = doodle_config; |
| + |
| + if (notify) { |
| + for (auto& observer : observers_) { |
| + observer.OnDoodleConfigUpdated(cached_config_); |
| + } |
| + } |
| +} |
| + |
| +} // namespace doodle |