OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
7 | 7 |
8 #include <stddef.h> | |
9 | |
10 #include <map> | 8 #include <map> |
11 #include <string> | 9 #include <string> |
12 #include <vector> | 10 #include <vector> |
13 | 11 |
14 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
15 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
16 #include "components/keyed_service/core/keyed_service.h" | 14 #include "components/keyed_service/core/keyed_service.h" |
| 15 #include "components/ntp_snippets/content_suggestions_category_factory.h" |
17 #include "components/ntp_snippets/content_suggestions_category_status.h" | 16 #include "components/ntp_snippets/content_suggestions_category_status.h" |
18 #include "components/ntp_snippets/content_suggestions_provider.h" | 17 #include "components/ntp_snippets/content_suggestions_provider.h" |
19 | 18 |
20 namespace gfx { | 19 namespace gfx { |
21 class Image; | 20 class Image; |
22 } | 21 } |
23 | 22 |
24 namespace ntp_snippets { | 23 namespace ntp_snippets { |
25 | 24 |
26 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves | 25 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // same results when they fetch the next time. In particular, calling this | 111 // same results when they fetch the next time. In particular, calling this |
113 // method will not mark any suggestions as dismissed. | 112 // method will not mark any suggestions as dismissed. |
114 void ClearCachedSuggestionsForDebugging(); | 113 void ClearCachedSuggestionsForDebugging(); |
115 | 114 |
116 // Only for debugging use through the internals page. Some providers | 115 // Only for debugging use through the internals page. Some providers |
117 // internally store a list of dismissed suggestions to prevent them from | 116 // internally store a list of dismissed suggestions to prevent them from |
118 // reappearing. This function clears all such lists in all providers, making | 117 // reappearing. This function clears all such lists in all providers, making |
119 // dismissed suggestions reappear (only for certain providers). | 118 // dismissed suggestions reappear (only for certain providers). |
120 void ClearDismissedSuggestionsForDebugging(); | 119 void ClearDismissedSuggestionsForDebugging(); |
121 | 120 |
| 121 ContentSuggestionsCategoryFactory* category_factory() { |
| 122 return &category_factory_; |
| 123 } |
| 124 |
122 private: | 125 private: |
123 friend class ContentSuggestionsServiceTest; | 126 friend class ContentSuggestionsServiceTest; |
124 | 127 |
| 128 // This is just an arbitrary ordering by ID, used by the maps in this class, |
| 129 // because the ordering needs to be constant for maps. |
| 130 struct CompareCategoriesByID { |
| 131 bool operator()(const ContentSuggestionsCategory& left, |
| 132 const ContentSuggestionsCategory& right) const; |
| 133 }; |
| 134 |
125 // Implementation of ContentSuggestionsProvider::Observer. | 135 // Implementation of ContentSuggestionsProvider::Observer. |
126 void OnNewSuggestions(ContentSuggestionsCategory changed_category, | 136 void OnNewSuggestions(ContentSuggestionsCategory changed_category, |
127 std::vector<ContentSuggestion> suggestions) override; | 137 std::vector<ContentSuggestion> suggestions) override; |
128 void OnCategoryStatusChanged( | 138 void OnCategoryStatusChanged( |
129 ContentSuggestionsCategory changed_category, | 139 ContentSuggestionsCategory changed_category, |
130 ContentSuggestionsCategoryStatus new_status) override; | 140 ContentSuggestionsCategoryStatus new_status) override; |
131 void OnProviderShutdown(ContentSuggestionsProvider* provider) override; | 141 void OnProviderShutdown(ContentSuggestionsProvider* provider) override; |
132 | 142 |
133 // Checks whether a provider for the given |category| is registered. | 143 // Checks whether a provider for the given |category| is registered. |
134 bool IsCategoryRegistered(ContentSuggestionsCategory category) const; | 144 bool IsCategoryRegistered(ContentSuggestionsCategory category) const; |
135 | 145 |
136 // Fires the OnCategoryStatusChanged event for the given |category|. | 146 // Fires the OnCategoryStatusChanged event for the given |category|. |
137 void NotifyCategoryStatusChanged(ContentSuggestionsCategory category); | 147 void NotifyCategoryStatusChanged(ContentSuggestionsCategory category); |
138 | 148 |
139 // Whether the content suggestions feature is enabled. | 149 // Whether the content suggestions feature is enabled. |
140 State state_; | 150 State state_; |
141 | 151 |
| 152 // Provides new and existing categories and an order for them. |
| 153 ContentSuggestionsCategoryFactory category_factory_; |
| 154 |
142 // All registered providers. A provider may be contained multiple times, if it | 155 // All registered providers. A provider may be contained multiple times, if it |
143 // provides multiple categories. The keys of this map are exactly the entries | 156 // provides multiple categories. The keys of this map are exactly the entries |
144 // of |categories_|. | 157 // of |categories_|. |
145 std::map<ContentSuggestionsCategory, ContentSuggestionsProvider*> providers_; | 158 std::map<ContentSuggestionsCategory, |
| 159 ContentSuggestionsProvider*, |
| 160 CompareCategoriesByID> |
| 161 providers_; |
146 | 162 |
147 // All current suggestion categories, in an order determined by the service. | 163 // All current suggestion categories, in an order determined by the |
148 // Currently, this is simply the order in which the providers were registered. | 164 // |category_factory_|. This vector contains exactly the same categories as |
149 // This vector contains exactly the same categories as |providers_|. | 165 // |providers_|. |
150 // TODO(pke): Implement a useful and consistent ordering for categories. | |
151 std::vector<ContentSuggestionsCategory> categories_; | 166 std::vector<ContentSuggestionsCategory> categories_; |
152 | 167 |
153 // All current suggestions grouped by category. This contains an entry for | 168 // All current suggestions grouped by category. This contains an entry for |
154 // every category in |categories_| whose status is an available status. It may | 169 // every category in |categories_| whose status is an available status. It may |
155 // contain an empty vector if the category is available but empty (or still | 170 // contain an empty vector if the category is available but empty (or still |
156 // loading). | 171 // loading). |
157 std::map<ContentSuggestionsCategory, std::vector<ContentSuggestion>> | 172 std::map<ContentSuggestionsCategory, |
| 173 std::vector<ContentSuggestion>, |
| 174 CompareCategoriesByID> |
158 suggestions_by_category_; | 175 suggestions_by_category_; |
159 | 176 |
160 // Map used to determine the category of a suggestion (of which only the ID | 177 // Map used to determine the category of a suggestion (of which only the ID |
161 // is available). This also determines the provider that delivered the | 178 // is available). This also determines the provider that delivered the |
162 // suggestion. | 179 // suggestion. |
163 std::map<std::string, ContentSuggestionsCategory> id_category_map_; | 180 std::map<std::string, ContentSuggestionsCategory> id_category_map_; |
164 | 181 |
165 base::ObserverList<Observer> observers_; | 182 base::ObserverList<Observer> observers_; |
166 | 183 |
167 const std::vector<ContentSuggestion> no_suggestions_; | 184 const std::vector<ContentSuggestion> no_suggestions_; |
168 | 185 |
169 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 186 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
170 }; | 187 }; |
171 | 188 |
172 } // namespace ntp_snippets | 189 } // namespace ntp_snippets |
173 | 190 |
174 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 191 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
OLD | NEW |