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

Side by Side 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 unified diff | Download patch
OLDNEW
(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 #ifndef COMPONENTS_DOODLE_DOODLE_SERVICE_H_
6 #define COMPONENTS_DOODLE_DOODLE_SERVICE_H_
7
8 #include <memory>
9
10 #include "base/macros.h"
11 #include "base/observer_list.h"
12 #include "base/optional.h"
13 #include "components/doodle/doodle_fetcher.h"
14 #include "components/doodle/doodle_types.h"
15
16 namespace doodle {
17
18 class DoodleService {
19 public:
20 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
21 public:
22 virtual void OnDoodleConfigUpdated(const base::Optional<DoodleConfig>&) = 0;
23 };
24
25 DoodleService(std::unique_ptr<DoodleFetcher> fetcher);
26 ~DoodleService();
27
28 // Adds a new observer to the service. It'll be called immediately with the
29 // current DoodleConfig, or nullopt if there is no doodle, or the config
30 // 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
31 void AddObserver(Observer* observer);
32
33 // Prevents |observer| from receiving future updates. This is safe to call
34 // even when the observer is being notified of an update.
35 void RemoveObserver(Observer* observer);
36
37 // Requests an asynchronous refresh of the DoodleConfig from the network.
38 // After the update completes, the observers will be notified only if the
39 // config changed.
40 void Refresh();
41
42 private:
43 void DoodleFetched(DoodleState state,
44 const base::Optional<DoodleConfig>& doodle_config);
45
46 // The fetcher for getting fresh DoodleConfigs from the network.
47 std::unique_ptr<DoodleFetcher> fetcher_;
48
49 // The result of the last network fetch.
50 base::Optional<DoodleConfig> cached_config_;
51
52 // The list of observers to be notified when the DoodleConfig changes.
53 base::ObserverList<Observer> observers_;
54
55 DISALLOW_COPY_AND_ASSIGN(DoodleService);
56 };
57
58 } // namespace doodle
59
60 #endif // COMPONENTS_DOODLE_DOODLE_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698