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