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

Unified Diff: components/doodle/doodle_service.h

Issue 2710003006: [Doodle] Introduce a DoodleService (Closed)
Patch Set: 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
Index: components/doodle/doodle_service.h
diff --git a/components/doodle/doodle_service.h b/components/doodle/doodle_service.h
new file mode 100644
index 0000000000000000000000000000000000000000..2c16d0df3dd91bddb0f503e405b911c84fa99b48
--- /dev/null
+++ b/components/doodle/doodle_service.h
@@ -0,0 +1,60 @@
+// 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.
+
+#ifndef COMPONENTS_DOODLE_DOODLE_SERVICE_H_
+#define COMPONENTS_DOODLE_DOODLE_SERVICE_H_
+
+#include <memory>
+
+#include "base/macros.h"
+#include "base/observer_list.h"
+#include "base/optional.h"
+#include "components/doodle/doodle_fetcher.h"
+#include "components/doodle/doodle_types.h"
+
+namespace doodle {
+
+class DoodleService {
+ public:
+ class Observer {
fhorschig 2017/02/24 15:33:14 Is there a special reason that we use observers in
Marc Treib 2017/02/24 16:18:51 The current NTP integration is based on an observe
fhorschig 2017/02/24 16:38:20 Consideration of caching is a good argument.
Marc Treib 2017/02/27 13:55:41 We're also keeping the status quo. I'm open for di
+ public:
+ virtual void OnDoodleConfigUpdated(const base::Optional<DoodleConfig>&) = 0;
+ };
+
+ DoodleService(std::unique_ptr<DoodleFetcher> fetcher);
+ ~DoodleService();
+
+ // Adds a new observer to the service. It'll be called immediately with the
+ // current DoodleConfig, or nullopt if there is no doodle, or the config
+ // hasn't been downloaded yet.
Marc Treib 2017/02/24 14:04:11 This (immediately notifying the just-added observe
fhorschig 2017/02/24 15:33:14 This side-effect can quickly slip one's attention
Marc Treib 2017/02/24 16:18:51 How about if we name it AddObserverAndNotify or so
fhorschig 2017/02/24 16:38:20 Definitely an improvement but I still prefer very
Marc Treib 2017/02/27 13:55:41 Alright, auto-notifications on AddObserver are gon
+ void AddObserver(Observer* observer);
+
+ // Prevents |observer| from receiving future updates. This is safe to call
+ // even when the observer is being notified of an update.
+ void RemoveObserver(Observer* observer);
+
+ // Requests an asynchronous refresh of the DoodleConfig from the network.
+ // After the update completes, the observers will be notified only if the
+ // config changed.
+ void Refresh();
+
+ private:
+ void DoodleFetched(DoodleState state,
+ const base::Optional<DoodleConfig>& doodle_config);
+
+ // The fetcher for getting fresh DoodleConfigs from the network.
+ std::unique_ptr<DoodleFetcher> fetcher_;
+
+ // The result of the last network fetch.
+ base::Optional<DoodleConfig> cached_config_;
+
+ // The list of observers to be notified when the DoodleConfig changes.
+ base::ObserverList<Observer> observers_;
+
+ DISALLOW_COPY_AND_ASSIGN(DoodleService);
+};
+
+} // namespace doodle
+
+#endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_

Powered by Google App Engine
This is Rietveld 408576698