| 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 | 26 |
| 27 class PrefService; | 27 class PrefService; |
| 28 class PrefRegistrySimple; |
| 28 | 29 |
| 29 namespace gfx { | 30 namespace gfx { |
| 30 class Image; | 31 class Image; |
| 31 } // namespace gfx | 32 } // namespace gfx |
| 32 | 33 |
| 33 namespace ntp_snippets { | 34 namespace ntp_snippets { |
| 34 | 35 |
| 35 class NTPSnippetsService; | 36 class NTPSnippetsService; |
| 36 | 37 |
| 37 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves | 38 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 }; | 86 }; |
| 86 | 87 |
| 87 ContentSuggestionsService(State state, | 88 ContentSuggestionsService(State state, |
| 88 history::HistoryService* history_service, | 89 history::HistoryService* history_service, |
| 89 PrefService* pref_service); | 90 PrefService* pref_service); |
| 90 ~ContentSuggestionsService() override; | 91 ~ContentSuggestionsService() override; |
| 91 | 92 |
| 92 // Inherited from KeyedService. | 93 // Inherited from KeyedService. |
| 93 void Shutdown() override; | 94 void Shutdown() override; |
| 94 | 95 |
| 96 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 97 |
| 95 State state() { return state_; } | 98 State state() { return state_; } |
| 96 | 99 |
| 97 // Gets all categories for which a provider is registered. The categories | 100 // Gets all categories for which a provider is registered. The categories |
| 98 // may or may not be available, see |GetCategoryStatus()|. | 101 // may or may not be available, see |GetCategoryStatus()|. |
| 99 const std::vector<Category>& GetCategories() const { return categories_; } | 102 const std::vector<Category>& GetCategories() const { return categories_; } |
| 100 | 103 |
| 101 // Gets the status of a category. | 104 // Gets the status of a category. |
| 102 CategoryStatus GetCategoryStatus(Category category) const; | 105 CategoryStatus GetCategoryStatus(Category category) const; |
| 103 | 106 |
| 104 // Gets the meta information of a category. | 107 // Gets the meta information of a category. |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 void OnURLsDeleted(history::HistoryService* history_service, | 208 void OnURLsDeleted(history::HistoryService* history_service, |
| 206 bool all_history, | 209 bool all_history, |
| 207 bool expired, | 210 bool expired, |
| 208 const history::URLRows& deleted_rows, | 211 const history::URLRows& deleted_rows, |
| 209 const std::set<GURL>& favicon_urls) override; | 212 const std::set<GURL>& favicon_urls) override; |
| 210 void HistoryServiceBeingDeleted( | 213 void HistoryServiceBeingDeleted( |
| 211 history::HistoryService* history_service) override; | 214 history::HistoryService* history_service) override; |
| 212 | 215 |
| 213 // Registers the given |provider| for the given |category|, unless it is | 216 // Registers the given |provider| for the given |category|, unless it is |
| 214 // already registered. Returns true if the category was newly registered or | 217 // already registered. Returns true if the category was newly registered or |
| 215 // false if it was present before. | 218 // false if it is dismissed or was present before. |
| 216 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider, | 219 bool TryRegisterProviderForCategory(ContentSuggestionsProvider* provider, |
| 217 Category category); | 220 Category category); |
| 221 void RegisterCategory(Category category, |
| 222 ContentSuggestionsProvider* provider); |
| 223 void UnregisterCategory(Category category, |
| 224 ContentSuggestionsProvider* provider); |
| 218 | 225 |
| 219 // Removes a suggestion from the local store |suggestions_by_category_|, if it | 226 // Removes a suggestion from the local store |suggestions_by_category_|, if it |
| 220 // exists. Returns true if a suggestion was removed. | 227 // exists. Returns true if a suggestion was removed. |
| 221 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); | 228 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); |
| 222 | 229 |
| 223 // Fires the OnCategoryStatusChanged event for the given |category|. | 230 // Fires the OnCategoryStatusChanged event for the given |category|. |
| 224 void NotifyCategoryStatusChanged(Category category); | 231 void NotifyCategoryStatusChanged(Category category); |
| 225 | 232 |
| 226 void SortCategories(); | 233 void SortCategories(); |
| 227 | 234 |
| 235 bool IsCategoryDismissed(Category category) const; |
| 236 |
| 237 // Re-enables a dismissed category, making querying its provider possible. |
| 238 void RestoreDismissedCategory(Category category); |
| 239 |
| 240 void RestoreDismissedCategoriesFromPrefs(); |
| 241 void StoreDismissedCategoriesToPrefs(); |
| 242 |
| 228 // Whether the content suggestions feature is enabled. | 243 // Whether the content suggestions feature is enabled. |
| 229 State state_; | 244 State state_; |
| 230 | 245 |
| 231 // Provides new and existing categories and an order for them. | 246 // Provides new and existing categories and an order for them. |
| 232 CategoryFactory category_factory_; | 247 CategoryFactory category_factory_; |
| 233 | 248 |
| 234 // All registered providers, owned by the service. | 249 // All registered providers, owned by the service. |
| 235 std::vector<std::unique_ptr<ContentSuggestionsProvider>> providers_; | 250 std::vector<std::unique_ptr<ContentSuggestionsProvider>> providers_; |
| 236 | 251 |
| 237 // All registered categories and their providers. A provider may be contained | 252 // All registered categories and their providers. A provider may be contained |
| 238 // multiple times, if it provides multiple categories. The keys of this map | 253 // multiple times, if it provides multiple categories. The keys of this map |
| 239 // are exactly the entries of |categories_| and the values are a subset of | 254 // are exactly the entries of |categories_| and the values are a subset of |
| 240 // |providers_|. | 255 // |providers_|. |
| 241 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> | 256 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> |
| 242 providers_by_category_; | 257 providers_by_category_; |
| 243 | 258 |
| 244 // All dismissed categories and their providers. These may be restored by | 259 // All dismissed categories and their providers. These may be restored by |
| 245 // RestoreDismissedCategories(). | 260 // RestoreDismissedCategories(). The provider can be null if the dismissed |
| 261 // category has received no updates since initialisation. |
| 262 // (see RestoreDismissedCategoriesFromPrefs()) |
| 246 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> | 263 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> |
| 247 dismissed_providers_by_category_; | 264 dismissed_providers_by_category_; |
| 248 | 265 |
| 249 // All current suggestion categories, in an order determined by the | 266 // All current suggestion categories, in an order determined by the |
| 250 // |category_factory_|. This vector contains exactly the same categories as | 267 // |category_factory_|. This vector contains exactly the same categories as |
| 251 // |providers_by_category_|. | 268 // |providers_by_category_|. |
| 252 std::vector<Category> categories_; | 269 std::vector<Category> categories_; |
| 253 | 270 |
| 254 // All current suggestions grouped by category. This contains an entry for | 271 // All current suggestions grouped by category. This contains an entry for |
| 255 // every category in |categories_| whose status is an available status. It may | 272 // every category in |categories_| whose status is an available status. It may |
| 256 // contain an empty vector if the category is available but empty (or still | 273 // contain an empty vector if the category is available but empty (or still |
| 257 // loading). | 274 // loading). |
| 258 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> | 275 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> |
| 259 suggestions_by_category_; | 276 suggestions_by_category_; |
| 260 | 277 |
| 261 // Observer for the HistoryService. All providers are notified when history is | 278 // Observer for the HistoryService. All providers are notified when history is |
| 262 // deleted. | 279 // deleted. |
| 263 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 280 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 264 history_service_observer_; | 281 history_service_observer_; |
| 265 | 282 |
| 266 base::ObserverList<Observer> observers_; | 283 base::ObserverList<Observer> observers_; |
| 267 | 284 |
| 268 const std::vector<ContentSuggestion> no_suggestions_; | 285 const std::vector<ContentSuggestion> no_suggestions_; |
| 269 | 286 |
| 270 // Keep a direct reference to this special provider to redirect scheduling, | 287 // Keep a direct reference to this special provider to redirect scheduling, |
| 271 // background fetching and debugging calls to it. If the NTPSnippetsService is | 288 // background fetching and debugging calls to it. If the NTPSnippetsService is |
| 272 // loaded, it is also present in |providers_|, otherwise this is a nullptr. | 289 // loaded, it is also present in |providers_|, otherwise this is a nullptr. |
| 273 NTPSnippetsService* ntp_snippets_service_; | 290 NTPSnippetsService* ntp_snippets_service_; |
| 274 | 291 |
| 292 PrefService* pref_service_; |
| 293 |
| 275 UserClassifier user_classifier_; | 294 UserClassifier user_classifier_; |
| 276 | 295 |
| 277 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 296 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| 278 }; | 297 }; |
| 279 | 298 |
| 280 } // namespace ntp_snippets | 299 } // namespace ntp_snippets |
| 281 | 300 |
| 282 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 301 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |