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

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: Rename ContentSuggestionsState to ContentSuggestionsCategoryStatus; Add s to ContentSuggestionsCate… 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_category_status.h"
18 #include "components/ntp_snippets/content_suggestions_provider.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 status of a suggestions category changed. When the status
45 // changes to an unavailable status, 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 OnCategoryStatusChanged(
50 ContentSuggestionsCategory category,
51 ContentSuggestionsCategoryStatus new_status) = 0;
52
53 // Sent when the service is shutting down. After the service has shut down,
54 // it will not provide any data anymore, though calling the getters is still
55 // safe.
56 virtual void ContentSuggestionsServiceShutdown() = 0;
57
58 protected:
59 virtual ~Observer() {}
60 };
61
62 enum State : bool {
tschumann 2016/07/06 06:36:12 drop ': bool' and the value assignments?
Philipp Keck 2016/07/06 08:34:23 Done.
63 ENABLED = true,
64 DISABLED = false,
65 };
66
67 ContentSuggestionsService(State enabled);
68 ~ContentSuggestionsService() override;
69
70 // Inherited from KeyedService.
71 void Shutdown() override;
72
73 // Gets all categories for which a provider is registered. The categories
74 // may or may not be available, see |GetCategoryStatus()|.
75 const std::vector<ContentSuggestionsCategory>& GetCategories() const {
76 return categories_;
77 }
78
79 // Gets the status of a category.
80 ContentSuggestionsCategoryStatus GetCategoryStatus(
81 ContentSuggestionsCategory category) const;
82
83 // Gets the available suggestions for a category. The result is empty if the
84 // category is available and empty, but also if the category is unavailable
85 // for any reason, see |GetCategoryStatus()|.
86 const std::vector<ContentSuggestion>& GetSuggestionsForCategory(
87 ContentSuggestionsCategory category) const;
88
89 // Fetches the image for the suggestion with the given |suggestion_id| and
90 // runs the |callback|. If that suggestion doesn't exist or the fetch fails,
91 // the callback gets an empty image.
92 void FetchSuggestionImage(const std::string& suggestion_id,
93 const ImageFetchedCallback& callback);
94
95 // Discards the suggestion with the given |suggestion_id|, if it exists.
96 // This will not trigger an update through the observers.
97 void DiscardSuggestion(const std::string& suggestion_id);
98
99 // Observer accessors.
100 void AddObserver(Observer* observer);
101 void RemoveObserver(Observer* observer);
102
103 // Registers a new ContentSuggestionsProvider. It must be ensured that at most
104 // one provider is registered for every category and that this method is
105 // called only once per provider.
106 void RegisterProvider(ContentSuggestionsProvider* provider);
107
108 // Only for debugging use through the internals page.
109 // Removes all suggestions from all caches or internal stores in all
110 // providers. It does, however, not remove any suggestions from the provider's
111 // sources, so if their configuration hasn't changed, they should return the
112 // same results when they fetch the next time. In particular, calling this
113 // method will not mark any suggestions as discarded.
114 void ClearCachedSuggestionsForDebugging();
115
116 // Only for debugging use through the internals page. Some providers
117 // internally store a list of discarded suggestions to prevent them from
118 // reappearing. This function clears all such lists in all providers, making
119 // discarded suggestions reappear (only for certain providers).
120 void ClearDiscardedSuggestionsForDebugging();
121
122 private:
123 // Implementation of ContentSuggestionsProvider::Observer.
124 void OnNewSuggestions(ContentSuggestionsCategory changed_category,
125 std::vector<ContentSuggestion> suggestions) override;
126 void OnCategoryStatusChanged(
127 ContentSuggestionsCategory changed_category,
128 ContentSuggestionsCategoryStatus new_status) override;
129 void OnProviderShutdown(ContentSuggestionsProvider* provider) override;
130
131 // Checks whether a provider for the given |category| is registered.
132 bool IsCategoryRegistered(ContentSuggestionsCategory category) const;
133
134 // Whether the content suggestions feature is enabled.
135 State enabled_;
136
137 // All current suggestion categories, in order. This contains exactly the same
tschumann 2016/07/06 06:36:12 what is this order? The order they were registered
Philipp Keck 2016/07/06 08:34:23 Done.
138 // categories as |providers_|.
139 std::vector<ContentSuggestionsCategory> categories_;
140
141 // All current suggestions grouped by category. This contains an entry for
142 // every category in |categories_| whose status is an available status. It may
143 // contain an empty vector if the category is available but empty (or still
144 // loading).
145 std::map<ContentSuggestionsCategory, std::vector<ContentSuggestion>>
146 suggestions_by_category_;
147
148 // All registered providers. A provider may be contained multiple times, if it
149 // provides multiple categories. The keys of this map are exactly the entries
150 // of |categories_|.
151 std::map<ContentSuggestionsCategory, ContentSuggestionsProvider*> providers_;
152
153 // Map used to determine the category of a suggestion (of which only the ID
154 // is available). This also determines the provider that delivered the
155 // suggestion.
156 std::map<std::string, ContentSuggestionsCategory> id_category_map_;
157
158 base::ObserverList<Observer> observers_;
159
160 const std::vector<ContentSuggestion> no_suggestions_;
161
162 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService);
163 };
164
165 } // namespace ntp_snippets
166
167 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698