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..78483b4d3c9984a089900211f73b0a05a43be80e |
| --- /dev/null |
| +++ b/components/doodle/doodle_service.cc |
| @@ -0,0 +1,73 @@ |
| +// 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 { |
| + |
| +namespace { |
| + |
| +bool DoodleConfigsAreEquivalent(const DoodleConfig& lhs, |
| + const DoodleConfig& rhs) { |
| + // TODO! |
| + return false; |
| +} |
| + |
| +} // namespace |
| + |
| +DoodleService::DoodleService(std::unique_ptr<DoodleFetcher> fetcher) |
| + : fetcher_(std::move(fetcher)) { |
| + DCHECK(fetcher_); |
| +} |
| + |
| +DoodleService::~DoodleService() = default; |
| + |
| +void DoodleService::AddObserver(Observer* observer) { |
| + observers_.AddObserver(observer); |
| + 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
|
| +} |
| + |
| +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. |
| + 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 = !DoodleConfigsAreEquivalent(cached_config_.value(), |
| + 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 |