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

Side by Side Diff: components/ntp_snippets/content_suggestions_service.h

Issue 2102023002: Add ContentSuggestionsService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Marks comments Created 4 years, 5 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 2016 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_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
7
8 #include <stddef.h>
9
10 #include <map>
11 #include <string>
12 #include <vector>
13
14 #include "base/callback_forward.h"
15 #include "base/observer_list.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "components/ntp_snippets/content_suggestions_provider.h"
18 #include "components/ntp_snippets/content_suggestions_state.h"
19
20 namespace gfx {
21 class Image;
22 }
23
24 namespace ntp_snippets {
25
26 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves
27 // them grouped into categories. There can be at most one provider per category.
28 // NOTE: This class is not yet in use, please use NTPSnippetsService for now
29 // (see ntp_snippets_service.h).
30 class ContentSuggestionsService : public KeyedService,
31 public ContentSuggestionsProvider::Observer {
32 public:
33 using ImageFetchedCallback =
34 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>;
35
36 class Observer {
37 public:
38 // Fired every time the service receives a new set of data, replacing any
39 // previously available data (though in most cases there will be an overlap
40 // and only a few changes within the data). The new data is then available
41 // through the getters of the service.
42 virtual void OnNewSuggestions() = 0;
43
44 // Fired when the state of a suggestions category changed. If the state
45 // changes to an unavailable state, the suggestions of the respective
46 // category have been invalidated, which means that they must no longer be
47 // displayed to the user. The UI must immediately clear any suggestions of
48 // that category.
49 virtual void OnCategoryStateChanged(ContentSuggestionCategory category,
50 ContentSuggestionsState new_state) = 0;
51
52 // Sent when the service is shutting down. After the service has shut down,
53 // it will not provide any data anymore, though calling the getters is still
54 // safe.
55 virtual void ContentSuggestionsServiceShutdown() = 0;
56
57 protected:
58 virtual ~Observer() {}
59 };
60
61 enum State : bool {
62 ENABLED = true,
63 DISABLED = false,
64 };
65
66 ContentSuggestionsService(State enabled);
67 ~ContentSuggestionsService() override;
68
69 // Inherited from KeyedService.
70 void Shutdown() override;
71
72 // Gets all categories for which a provider is registered. The categories
73 // may or may not be available, see |GetCategoryState()|.
74 const std::vector<ContentSuggestionCategory>& GetCategories() const {
75 return categories_;
76 }
77
78 // Gets the status of a category.
79 ContentSuggestionsState GetCategoryState(
80 ContentSuggestionCategory category) const;
81
82 // Gets the available suggestions for a category. The result is empty if the
83 // category is available and empty, but also if the category is unavailable
84 // for any reason, see |GetCategoryState()|.
85 const std::vector<ContentSuggestion>& GetSuggestionsForCategory(
86 ContentSuggestionCategory category) const;
87
88 // Fetches the image for the suggestion with the given |suggestion_id| and
89 // runs the |callback|. If that suggestion doesn't exist or the fetch fails,
90 // the callback gets an empty image.
91 void FetchSuggestionImage(const std::string& suggestion_id,
92 const ImageFetchedCallback& callback);
93
94 // Discards the suggestion with the given |suggestion_id|, if it exists.
95 // This will not trigger an update through the observers.
96 void DiscardSuggestion(const std::string& suggestion_id);
97
98 // Observer accessors.
99 void AddObserver(Observer* observer);
100 void RemoveObserver(Observer* observer);
101
102 // Registers a new ContentSuggestionsProvider. It must be ensured that at most
103 // one provider is registered for every category and that this method is
104 // called only once per provider.
105 void RegisterProvider(ContentSuggestionsProvider* provider);
106
107 // Only for debugging use through the internals page.
108 // Removes all suggestions from all caches or internal stores in all
109 // providers. It does, however, not remove any suggestions from the provider's
110 // sources, so if their configuration hasn't changed, they should return the
111 // same results when they fetch the next time. In particular, calling this
112 // method will not mark any suggestions as discarded.
113 void ClearCachedSuggestionsForDebugging();
114
115 // Only for debugging use through the internals page. Some providers
116 // internally store a list of discarded suggestions to prevent them from
117 // reappearing. This function clears all such lists in all providers, making
118 // discarded suggestions reappear (only for certain providers).
119 void ClearDiscardedSuggestionsForDebugging();
120
121 private:
122 // Implementation of ContentSuggestionsProvider::Observer.
123 void OnNewSuggestions(ContentSuggestionCategory changed_category,
124 std::vector<ContentSuggestion> suggestions) override;
125 void OnCategoryStateChanged(ContentSuggestionCategory changed_category,
126 ContentSuggestionsState new_state) override;
127 void OnProviderShutdown(ContentSuggestionsProvider* provider) override;
128
129 // Checks whether a provider for the given |category| is registered.
130 bool IsCategoryRegistered(ContentSuggestionCategory category) const;
131
132 // Whether the content suggestions feature is enabled.
133 State enabled_;
134
135 // All current suggestion categories, in order. This contains exactly the same
136 // categories as |providers_|.
137 std::vector<ContentSuggestionCategory> categories_;
138
139 // All current suggestions grouped by category. This contains an entry for
140 // every category in |categories_| whose state is an available state. It may
141 // contain an empty vector if the category is available but empty (or still
142 // loading).
143 std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>>
144 suggestions_by_category_;
145
146 // All registered providers. A provider may be contained multiple times, if it
147 // provides multiple categories. The keys of this map are exactly the entries
148 // of |categories_|.
149 std::map<ContentSuggestionCategory, ContentSuggestionsProvider*> providers_;
150
151 // Map used to determine the category of a suggestion (of which only the ID
152 // is available). This also determines the provider that delivered the
153 // suggestion.
154 std::map<std::string, ContentSuggestionCategory> id_category_map_;
155
156 base::ObserverList<Observer> observers_;
157
158 const std::vector<ContentSuggestion> empty_categories_list_;
159
160 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService);
161 };
162
163 } // namespace ntp_snippets
164
165 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698