| 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 <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/optional.h" | 16 #include "base/optional.h" |
| 17 #include "base/scoped_observer.h" | 17 #include "base/scoped_observer.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "components/history/core/browser/history_service.h" | 19 #include "components/history/core/browser/history_service.h" |
| 20 #include "components/history/core/browser/history_service_observer.h" | 20 #include "components/history/core/browser/history_service_observer.h" |
| 21 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
| 22 #include "components/ntp_snippets/category_factory.h" | 22 #include "components/ntp_snippets/category_factory.h" |
| 23 #include "components/ntp_snippets/category_status.h" | 23 #include "components/ntp_snippets/category_status.h" |
| 24 #include "components/ntp_snippets/content_suggestions_provider.h" | 24 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 25 #include "components/ntp_snippets/user_classifier.h" | 25 #include "components/ntp_snippets/user_classifier.h" |
| 26 #include "components/prefs/pref_service.h" |
| 26 | 27 |
| 27 class PrefService; | 28 class PrefService; |
| 29 class PrefRegistrySimple; |
| 28 | 30 |
| 29 namespace gfx { | 31 namespace gfx { |
| 30 class Image; | 32 class Image; |
| 31 } // namespace gfx | 33 } // namespace gfx |
| 32 | 34 |
| 33 namespace ntp_snippets { | 35 namespace ntp_snippets { |
| 34 | 36 |
| 35 class NTPSnippetsService; | 37 class NTPSnippetsService; |
| 36 | 38 |
| 37 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves | 39 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 }; | 87 }; |
| 86 | 88 |
| 87 ContentSuggestionsService(State state, | 89 ContentSuggestionsService(State state, |
| 88 history::HistoryService* history_service, | 90 history::HistoryService* history_service, |
| 89 PrefService* pref_service); | 91 PrefService* pref_service); |
| 90 ~ContentSuggestionsService() override; | 92 ~ContentSuggestionsService() override; |
| 91 | 93 |
| 92 // Inherited from KeyedService. | 94 // Inherited from KeyedService. |
| 93 void Shutdown() override; | 95 void Shutdown() override; |
| 94 | 96 |
| 97 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 98 |
| 95 State state() { return state_; } | 99 State state() { return state_; } |
| 96 | 100 |
| 97 // Gets all categories for which a provider is registered. The categories | 101 // Gets all categories for which a provider is registered. The categories |
| 98 // may or may not be available, see |GetCategoryStatus()|. | 102 // may or may not be available, see |GetCategoryStatus()|. |
| 99 const std::vector<Category>& GetCategories() const { return categories_; } | 103 const std::vector<Category>& GetCategories() const { return categories_; } |
| 100 | 104 |
| 101 // Gets the status of a category. | 105 // Gets the status of a category. |
| 102 CategoryStatus GetCategoryStatus(Category category) const; | 106 CategoryStatus GetCategoryStatus(Category category) const; |
| 103 | 107 |
| 104 // Gets the meta information of a category. | 108 // Gets the meta information of a category. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 | 187 |
| 184 UserClassifier* user_classifier() { return &user_classifier_; } | 188 UserClassifier* user_classifier() { return &user_classifier_; } |
| 185 | 189 |
| 186 private: | 190 private: |
| 187 friend class ContentSuggestionsServiceTest; | 191 friend class ContentSuggestionsServiceTest; |
| 188 | 192 |
| 189 // Implementation of ContentSuggestionsProvider::Observer. | 193 // Implementation of ContentSuggestionsProvider::Observer. |
| 190 void OnNewSuggestions(ContentSuggestionsProvider* provider, | 194 void OnNewSuggestions(ContentSuggestionsProvider* provider, |
| 191 Category category, | 195 Category category, |
| 192 std::vector<ContentSuggestion> suggestions) override; | 196 std::vector<ContentSuggestion> suggestions) override; |
| 197 void OnNewSuggestionBatch(ContentSuggestionsProvider* provider, |
| 198 SuggestionBatch suggestion_batch) override; |
| 193 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | 199 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, |
| 194 Category category, | 200 Category category, |
| 195 CategoryStatus new_status) override; | 201 CategoryStatus new_status) override; |
| 196 void OnSuggestionInvalidated( | 202 void OnSuggestionInvalidated( |
| 197 ContentSuggestionsProvider* provider, | 203 ContentSuggestionsProvider* provider, |
| 198 const ContentSuggestion::ID& suggestion_id) override; | 204 const ContentSuggestion::ID& suggestion_id) override; |
| 199 | 205 |
| 200 // history::HistoryServiceObserver implementation. | 206 // history::HistoryServiceObserver implementation. |
| 201 void OnURLsDeleted(history::HistoryService* history_service, | 207 void OnURLsDeleted(history::HistoryService* history_service, |
| 202 bool all_history, | 208 bool all_history, |
| 203 bool expired, | 209 bool expired, |
| 204 const history::URLRows& deleted_rows, | 210 const history::URLRows& deleted_rows, |
| 205 const std::set<GURL>& favicon_urls) override; | 211 const std::set<GURL>& favicon_urls) override; |
| 206 void HistoryServiceBeingDeleted( | 212 void HistoryServiceBeingDeleted( |
| 207 history::HistoryService* history_service) override; | 213 history::HistoryService* history_service) override; |
| 208 | 214 |
| 209 // Registers the given |provider| for the given |category|, unless it is | 215 // Registers the given |provider| for the given |category|, unless it is |
| 210 // already registered. Returns true if the category was newly registered or | 216 // already registered. Returns true if the category was newly registered or |
| 211 // false if it was present before. | 217 // false if it was present before. |
| 212 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider, | 218 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider, |
| 213 Category category); | 219 Category category); |
| 214 | 220 |
| 221 // Notifies the observer of new suggestions if necessary. In particular, |
| 222 // dismissed or unavailable categories will not trigger a notification. |
| 223 void OnNewSuggestionsInternal(ContentSuggestionsProvider* provider, |
| 224 Category category, |
| 225 std::vector<ContentSuggestion> suggestions); |
| 226 |
| 215 // Removes a suggestion from the local store |suggestions_by_category_|, if it | 227 // Removes a suggestion from the local store |suggestions_by_category_|, if it |
| 216 // exists. Returns true if a suggestion was removed. | 228 // exists. Returns true if a suggestion was removed. |
| 217 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); | 229 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); |
| 218 | 230 |
| 219 // Fires the OnCategoryStatusChanged event for the given |category|. | 231 // Fires the OnCategoryStatusChanged event for the given |category|. |
| 220 void NotifyCategoryStatusChanged(Category category); | 232 void NotifyCategoryStatusChanged(Category category); |
| 221 | 233 |
| 234 // Returns whether the provided |category| has been dismissed. |
| 235 bool IsCategoryDismissed(Category category) const; |
| 236 |
| 222 void SortCategories(); | 237 void SortCategories(); |
| 223 | 238 |
| 239 void RestoreDismissedCategoriesFromPrefs(); |
| 240 void StoreDismissedCategoriesToPrefs(); |
| 241 |
| 224 // Whether the content suggestions feature is enabled. | 242 // Whether the content suggestions feature is enabled. |
| 225 State state_; | 243 State state_; |
| 226 | 244 |
| 227 // Provides new and existing categories and an order for them. | 245 // Provides new and existing categories and an order for them. |
| 228 CategoryFactory category_factory_; | 246 CategoryFactory category_factory_; |
| 229 | 247 |
| 230 // All registered providers, owned by the service. | 248 // All registered providers, owned by the service. |
| 231 std::vector<std::unique_ptr<ContentSuggestionsProvider>> providers_; | 249 std::vector<std::unique_ptr<ContentSuggestionsProvider>> providers_; |
| 232 | 250 |
| 233 // All registered categories and their providers. A provider may be contained | 251 // All registered categories and their providers. A provider may be contained |
| 234 // multiple times, if it provides multiple categories. The keys of this map | 252 // multiple times, if it provides multiple categories. The keys of this map |
| 235 // are exactly the entries of |categories_| and the values are a subset of | 253 // are exactly the entries of |categories_| and the values are a subset of |
| 236 // |providers_|. | 254 // |providers_|. |
| 237 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> | 255 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> |
| 238 providers_by_category_; | 256 providers_by_category_; |
| 239 | 257 |
| 240 // All current suggestion categories, in an order determined by the | 258 // All current suggestion categories, in an order determined by the |
| 241 // |category_factory_|. This vector contains exactly the same categories as | 259 // |category_factory_|. This vector contains exactly the same categories as |
| 242 // |providers_by_category_|. | 260 // |providers_by_category_|. |
| 243 std::vector<Category> categories_; | 261 std::vector<Category> categories_; |
| 244 | 262 |
| 245 // All current suggestions grouped by category. This contains an entry for | 263 // All current suggestions grouped by category. This contains an entry for |
| 246 // every category in |categories_| whose status is an available status. It may | 264 // every category in |categories_| whose status is an available status. It may |
| 247 // contain an empty vector if the category is available but empty (or still | 265 // contain an empty vector if the category is available but empty (or still |
| 248 // loading). | 266 // loading). |
| 249 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> | 267 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> |
| 250 suggestions_by_category_; | 268 suggestions_by_category_; |
| 251 | 269 |
| 270 // All the currently dismissed categories. |
| 271 std::set<Category, Category::CompareByID> dismissed_categories_; |
| 272 |
| 252 // Observer for the HistoryService. All providers are notified when history is | 273 // Observer for the HistoryService. All providers are notified when history is |
| 253 // deleted. | 274 // deleted. |
| 254 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 275 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 255 history_service_observer_; | 276 history_service_observer_; |
| 256 | 277 |
| 257 base::ObserverList<Observer> observers_; | 278 base::ObserverList<Observer> observers_; |
| 258 | 279 |
| 259 const std::vector<ContentSuggestion> no_suggestions_; | 280 const std::vector<ContentSuggestion> no_suggestions_; |
| 260 | 281 |
| 261 // Keep a direct reference to this special provider to redirect scheduling, | 282 // Keep a direct reference to this special provider to redirect scheduling, |
| 262 // background fetching and debugging calls to it. If the NTPSnippetsService is | 283 // background fetching and debugging calls to it. If the NTPSnippetsService is |
| 263 // loaded, it is also present in |providers_|, otherwise this is a nullptr. | 284 // loaded, it is also present in |providers_|, otherwise this is a nullptr. |
| 264 NTPSnippetsService* ntp_snippets_service_; | 285 NTPSnippetsService* ntp_snippets_service_; |
| 265 | 286 |
| 287 PrefService* pref_service_; |
| 288 |
| 266 UserClassifier user_classifier_; | 289 UserClassifier user_classifier_; |
| 267 | 290 |
| 268 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 291 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| 269 }; | 292 }; |
| 270 | 293 |
| 271 } // namespace ntp_snippets | 294 } // namespace ntp_snippets |
| 272 | 295 |
| 273 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 296 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |