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 <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "base/optional.h" | 15 #include "base/optional.h" |
| 16 #include "base/scoped_observer.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "components/history/core/browser/history_service.h" |
| 19 #include "components/history/core/browser/history_service_observer.h" |
17 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
18 #include "components/ntp_snippets/category_factory.h" | 21 #include "components/ntp_snippets/category_factory.h" |
19 #include "components/ntp_snippets/category_status.h" | 22 #include "components/ntp_snippets/category_status.h" |
20 #include "components/ntp_snippets/content_suggestions_provider.h" | 23 #include "components/ntp_snippets/content_suggestions_provider.h" |
21 | 24 |
22 namespace gfx { | 25 namespace gfx { |
23 class Image; | 26 class Image; |
24 } | 27 } |
25 | 28 |
26 namespace ntp_snippets { | 29 namespace ntp_snippets { |
27 | 30 |
28 class NTPSnippetsService; | 31 class NTPSnippetsService; |
29 | 32 |
30 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves | 33 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves |
31 // them grouped into categories. There can be at most one provider per category. | 34 // them grouped into categories. There can be at most one provider per category. |
32 class ContentSuggestionsService : public KeyedService, | 35 class ContentSuggestionsService : public KeyedService, |
33 public ContentSuggestionsProvider::Observer { | 36 public ContentSuggestionsProvider::Observer, |
| 37 public history::HistoryServiceObserver { |
34 public: | 38 public: |
35 using ImageFetchedCallback = | 39 using ImageFetchedCallback = |
36 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; | 40 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; |
37 using DismissedSuggestionsCallback = base::Callback<void( | 41 using DismissedSuggestionsCallback = base::Callback<void( |
38 std::vector<ContentSuggestion> dismissed_suggestions)>; | 42 std::vector<ContentSuggestion> dismissed_suggestions)>; |
39 | 43 |
40 class Observer { | 44 class Observer { |
41 public: | 45 public: |
42 // Fired every time the service receives a new set of data for the given | 46 // Fired every time the service receives a new set of data for the given |
43 // |category|, replacing any previously available data (though in most cases | 47 // |category|, replacing any previously available data (though in most cases |
(...skipping 26 matching lines...) Expand all Loading... |
70 | 74 |
71 protected: | 75 protected: |
72 virtual ~Observer() {} | 76 virtual ~Observer() {} |
73 }; | 77 }; |
74 | 78 |
75 enum State { | 79 enum State { |
76 ENABLED, | 80 ENABLED, |
77 DISABLED, | 81 DISABLED, |
78 }; | 82 }; |
79 | 83 |
80 ContentSuggestionsService(State state); | 84 ContentSuggestionsService(State state, |
| 85 history::HistoryService* history_service); |
81 ~ContentSuggestionsService() override; | 86 ~ContentSuggestionsService() override; |
82 | 87 |
83 // Inherited from KeyedService. | 88 // Inherited from KeyedService. |
84 void Shutdown() override; | 89 void Shutdown() override; |
85 | 90 |
86 State state() { return state_; } | 91 State state() { return state_; } |
87 | 92 |
88 // Gets all categories for which a provider is registered. The categories | 93 // Gets all categories for which a provider is registered. The categories |
89 // may or may not be available, see |GetCategoryStatus()|. | 94 // may or may not be available, see |GetCategoryStatus()|. |
90 const std::vector<Category>& GetCategories() const { return categories_; } | 95 const std::vector<Category>& GetCategories() const { return categories_; } |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 void OnNewSuggestions(ContentSuggestionsProvider* provider, | 180 void OnNewSuggestions(ContentSuggestionsProvider* provider, |
176 Category category, | 181 Category category, |
177 std::vector<ContentSuggestion> suggestions) override; | 182 std::vector<ContentSuggestion> suggestions) override; |
178 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | 183 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, |
179 Category category, | 184 Category category, |
180 CategoryStatus new_status) override; | 185 CategoryStatus new_status) override; |
181 void OnSuggestionInvalidated(ContentSuggestionsProvider* provider, | 186 void OnSuggestionInvalidated(ContentSuggestionsProvider* provider, |
182 Category category, | 187 Category category, |
183 const std::string& suggestion_id) override; | 188 const std::string& suggestion_id) override; |
184 | 189 |
| 190 // history::HistoryServiceObserver implementation. |
| 191 void OnURLsDeleted(history::HistoryService* history_service, |
| 192 bool all_history, |
| 193 bool expired, |
| 194 const history::URLRows& deleted_rows, |
| 195 const std::set<GURL>& favicon_urls) override; |
| 196 void HistoryServiceBeingDeleted( |
| 197 history::HistoryService* history_service) override; |
| 198 |
185 // Registers the given |provider| for the given |category|, unless it is | 199 // Registers the given |provider| for the given |category|, unless it is |
186 // already registered. Returns true if the category was newly registered or | 200 // already registered. Returns true if the category was newly registered or |
187 // false if it was present before. | 201 // false if it was present before. |
188 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider, | 202 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider, |
189 Category category); | 203 Category category); |
190 | 204 |
191 // Removes a suggestion from the local stores |id_category_map_| and | 205 // Removes a suggestion from the local stores |id_category_map_| and |
192 // |suggestions_by_category_|, if it exists. Returns true if a suggestion was | 206 // |suggestions_by_category_|, if it exists. Returns true if a suggestion was |
193 // removed. | 207 // removed. |
194 bool RemoveSuggestionByID(Category category, | 208 bool RemoveSuggestionByID(Category category, |
(...skipping 30 matching lines...) Expand all Loading... |
225 // contain an empty vector if the category is available but empty (or still | 239 // contain an empty vector if the category is available but empty (or still |
226 // loading). | 240 // loading). |
227 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> | 241 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> |
228 suggestions_by_category_; | 242 suggestions_by_category_; |
229 | 243 |
230 // Map used to determine the category of a suggestion (of which only the ID | 244 // Map used to determine the category of a suggestion (of which only the ID |
231 // is available). This also determines the provider that delivered the | 245 // is available). This also determines the provider that delivered the |
232 // suggestion. | 246 // suggestion. |
233 std::map<std::string, Category> id_category_map_; | 247 std::map<std::string, Category> id_category_map_; |
234 | 248 |
| 249 // Observer for the HistoryService. All providers are notified when history is |
| 250 // deleted. |
| 251 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 252 history_service_observer_; |
| 253 |
235 base::ObserverList<Observer> observers_; | 254 base::ObserverList<Observer> observers_; |
236 | 255 |
237 const std::vector<ContentSuggestion> no_suggestions_; | 256 const std::vector<ContentSuggestion> no_suggestions_; |
238 | 257 |
239 // Keep a direct reference to this special provider to redirect scheduling, | 258 // Keep a direct reference to this special provider to redirect scheduling, |
240 // background fetching and debugging calls to it. If the NTPSnippetsService is | 259 // background fetching and debugging calls to it. If the NTPSnippetsService is |
241 // loaded, it is also present in |providers_|, otherwise this is a nullptr. | 260 // loaded, it is also present in |providers_|, otherwise this is a nullptr. |
242 NTPSnippetsService* ntp_snippets_service_; | 261 NTPSnippetsService* ntp_snippets_service_; |
243 | 262 |
244 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 263 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
245 }; | 264 }; |
246 | 265 |
247 } // namespace ntp_snippets | 266 } // namespace ntp_snippets |
248 | 267 |
249 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 268 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
OLD | NEW |