| 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 (!cached_config_.has_value() && !doodle_config.has_value()) { |
| 37 // There was no config before and we didn't get a new one, so there's |
| 38 // nothing to do. |
| 39 return; |
| 40 } |
| 41 |
| 42 bool notify = false; |
| 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; |
| 54 |
| 55 if (notify) { |
| 56 for (auto& observer : observers_) { |
| 57 observer.OnDoodleConfigUpdated(cached_config_); |
| 58 } |
| 59 } |
| 60 } |
| 61 |
| 62 } // namespace doodle |
| OLD | NEW |