Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(199)

Unified Diff: components/doodle/doodle_service.cc

Issue 2710003006: [Doodle] Introduce a DoodleService (Closed)
Patch Set: comment Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/doodle/doodle_service.h ('k') | components/doodle/doodle_service_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..fee586c6be958a007011c0d343cd303a16fd74a9
--- /dev/null
+++ b/components/doodle/doodle_service.cc
@@ -0,0 +1,62 @@
+// 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 (!cached_config_.has_value() && !doodle_config.has_value()) {
+ // There was no config before and we didn't get a new one, so there's
+ // nothing to do.
+ 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
« no previous file with comments | « components/doodle/doodle_service.h ('k') | components/doodle/doodle_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698