OLD | NEW |
---|---|
(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 Enabled : bool { | |
Marc Treib
2016/07/05 13:17:12
This should still be called State or something lik
Philipp Keck
2016/07/05 13:52:00
Done. Maybe should really rename ContentSuggestion
| |
62 ENABLE = true, | |
63 DISABLE = false, | |
Marc Treib
2016/07/05 13:17:12
And these should be ENABLED and DISABLED - they de
Philipp Keck
2016/07/05 13:52:00
Done.
| |
64 }; | |
65 | |
66 ContentSuggestionsService(Enabled 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. This method will fail if there | |
Marc Treib
2016/07/05 13:17:12
"will fail" sounds like some runtime error, like "
Philipp Keck
2016/07/05 13:52:00
Done.
| |
103 // is an overlap of the provided categories with another provider. | |
104 void RegisterProvider(ContentSuggestionsProvider* provider); | |
105 | |
106 // Only for debugging use through the internals page. | |
107 // Removes all suggestions from all caches or internal stores in all | |
108 // providers. It does, however, not remove any suggestions from the provider's | |
109 // sources, so if their configuration hasn't changed, they should return the | |
110 // same results when they fetch the next time. In particular, calling this | |
111 // method will not mark any suggestions as discarded. | |
112 void ClearCachedSuggestionsForDebugging(); | |
113 | |
114 // Only for debugging use through the internals page. Some providers | |
115 // internally store a list of discarded suggestions to prevent them from | |
116 // reappearing. This function clears all such lists in all providers, making | |
117 // discarded suggestions reappear (only for certain providers). | |
118 void ClearDiscardedSuggestionsForDebugging(); | |
119 | |
120 private: | |
121 // Implementation of ContentSuggestionsProvider::Observer. | |
122 void OnNewSuggestions(ContentSuggestionCategory changed_category, | |
123 std::vector<ContentSuggestion> suggestions) override; | |
124 void OnCategoryStateChanged(ContentSuggestionCategory changed_category, | |
125 ContentSuggestionsState new_state) override; | |
126 void OnProviderShutdown(ContentSuggestionsProvider* provider) override; | |
127 | |
128 bool CategoryExists(ContentSuggestionCategory category) const; | |
Marc Treib
2016/07/05 13:17:12
CategoryIsRegistered?
Philipp Keck
2016/07/05 13:52:00
Done. IsCategoryRegistered.
| |
129 | |
130 // Whether the content suggestions feature is enabled. | |
131 Enabled enabled_; | |
132 | |
133 // All current suggestion categories, in order. This contains exactly the same | |
134 // categories as |providers_|. | |
135 std::vector<ContentSuggestionCategory> categories_; | |
136 | |
137 // All current suggestions grouped by category. This contains an entry for | |
138 // every category in |categories_| whose state is an available state. It may | |
139 // contain an empty vector if the category is available but empty (or still | |
140 // loading). | |
Marc Treib
2016/07/05 13:17:12
It might be easier now if it always contained a (p
Philipp Keck
2016/07/05 13:52:00
empty_categories_list_ will need to stay because t
Marc Treib
2016/07/05 14:50:50
It's not a list of categories, but a list of sugge
Philipp Keck
2016/07/05 16:53:34
Ah ok, I see what you mean. I named it after its u
| |
141 std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>> | |
142 suggestions_by_category_; | |
143 | |
144 // All registered providers. A provider may be contained multiple times, if it | |
145 // provides multiple categories. The keys of this map are exactly the entries | |
146 // of |categories_|. | |
147 std::map<ContentSuggestionCategory, ContentSuggestionsProvider*> providers_; | |
148 | |
149 // Map used to determine the category of a suggestion (of which only the ID | |
150 // is available). This also determines the provider that delivered the | |
151 // suggestion. | |
152 std::map<std::string, ContentSuggestionCategory> id_category_map_; | |
Marc Treib
2016/07/05 13:17:12
Hm. So you need this because you don't want to par
Philipp Keck
2016/07/05 13:52:00
Tim's suggestion :-) Tim, wdyt?
I was already in
Philipp Keck
2016/07/05 14:28:34
Tim still prefers more data structures over parsin
Marc Treib
2016/07/05 14:50:50
Alright.
Removing category() from ContentSuggesti
Philipp Keck
2016/07/05 16:53:34
Done.
| |
153 | |
154 base::ObserverList<Observer> observers_; | |
155 | |
156 const std::vector<ContentSuggestion> empty_categories_list_; | |
157 | |
158 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | |
159 }; | |
160 | |
161 } // namespace ntp_snippets | |
162 | |
163 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | |
OLD | NEW |