Chromium Code Reviews| 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" | |
|
Marc Treib
2016/10/13 15:59:01
Is this needed? Note that there's already a forwar
dgn
2016/10/14 21:20:28
Done.
| |
| 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 void OnURLsDeleted(history::HistoryService* history_service, | 209 void OnURLsDeleted(history::HistoryService* history_service, |
| 206 bool all_history, | 210 bool all_history, |
| 207 bool expired, | 211 bool expired, |
| 208 const history::URLRows& deleted_rows, | 212 const history::URLRows& deleted_rows, |
| 209 const std::set<GURL>& favicon_urls) override; | 213 const std::set<GURL>& favicon_urls) override; |
| 210 void HistoryServiceBeingDeleted( | 214 void HistoryServiceBeingDeleted( |
| 211 history::HistoryService* history_service) override; | 215 history::HistoryService* history_service) override; |
| 212 | 216 |
| 213 // Registers the given |provider| for the given |category|, unless it is | 217 // Registers the given |provider| for the given |category|, unless it is |
| 214 // already registered. Returns true if the category was newly registered or | 218 // already registered. Returns true if the category was newly registered or |
| 215 // false if it was present before. | 219 // false if it is dismissed or was present before. |
| 216 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider, | 220 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider, |
|
Marc Treib
2016/10/13 15:59:01
Would RegisterProviderForCategory be a better name
dgn
2016/10/14 21:20:28
Done.
| |
| 217 Category category); | 221 Category category); |
| 222 void RegisterCategoryInternal(Category category, | |
| 223 ContentSuggestionsProvider* provider); | |
| 224 void UnregisterCategory(Category category, | |
| 225 ContentSuggestionsProvider* provider); | |
|
Marc Treib
2016/10/13 15:59:01
nit: These two have the arguments the other way ar
dgn
2016/10/14 21:20:28
Different names now. The category is the main thin
| |
| 218 | 226 |
| 219 // 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 |
| 220 // exists. Returns true if a suggestion was removed. | 228 // exists. Returns true if a suggestion was removed. |
| 221 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); | 229 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); |
| 222 | 230 |
| 223 // Fires the OnCategoryStatusChanged event for the given |category|. | 231 // Fires the OnCategoryStatusChanged event for the given |category|. |
| 224 void NotifyCategoryStatusChanged(Category category); | 232 void NotifyCategoryStatusChanged(Category category); |
| 225 | 233 |
| 226 void SortCategories(); | 234 void SortCategories(); |
| 227 | 235 |
| 236 bool IsCategoryDismissed(Category category) const; | |
| 237 | |
| 238 // Re-enables a dismissed category, making querying its provider possible. | |
| 239 void RestoreDismissedCategory(Category category); | |
| 240 | |
| 241 void RestoreDismissedCategoriesFromPrefs(); | |
| 242 void StoreDismissedCategoriesToPrefs(); | |
| 243 | |
| 228 // Whether the content suggestions feature is enabled. | 244 // Whether the content suggestions feature is enabled. |
| 229 State state_; | 245 State state_; |
| 230 | 246 |
| 231 // Provides new and existing categories and an order for them. | 247 // Provides new and existing categories and an order for them. |
| 232 CategoryFactory category_factory_; | 248 CategoryFactory category_factory_; |
| 233 | 249 |
| 234 // All registered providers, owned by the service. | 250 // All registered providers, owned by the service. |
| 235 std::vector<std::unique_ptr<ContentSuggestionsProvider>> providers_; | 251 std::vector<std::unique_ptr<ContentSuggestionsProvider>> providers_; |
| 236 | 252 |
| 237 // All registered categories and their providers. A provider may be contained | 253 // All registered categories and their providers. A provider may be contained |
| 238 // multiple times, if it provides multiple categories. The keys of this map | 254 // 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 | 255 // are exactly the entries of |categories_| and the values are a subset of |
| 240 // |providers_|. | 256 // |providers_|. |
| 241 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> | 257 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> |
| 242 providers_by_category_; | 258 providers_by_category_; |
| 243 | 259 |
| 244 // All dismissed categories and their providers. These may be restored by | 260 // All dismissed categories and their providers. These may be restored by |
| 245 // RestoreDismissedCategories(). | 261 // RestoreDismissedCategories(). |
|
Marc Treib
2016/10/13 15:59:01
Add a comment that the providers may be null?
dgn
2016/10/14 21:20:28
Done.
| |
| 246 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> | 262 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> |
| 247 dismissed_providers_by_category_; | 263 dismissed_providers_by_category_; |
| 248 | 264 |
| 249 // All current suggestion categories, in an order determined by the | 265 // All current suggestion categories, in an order determined by the |
| 250 // |category_factory_|. This vector contains exactly the same categories as | 266 // |category_factory_|. This vector contains exactly the same categories as |
| 251 // |providers_by_category_|. | 267 // |providers_by_category_|. |
| 252 std::vector<Category> categories_; | 268 std::vector<Category> categories_; |
| 253 | 269 |
| 254 // All current suggestions grouped by category. This contains an entry for | 270 // All current suggestions grouped by category. This contains an entry for |
| 255 // every category in |categories_| whose status is an available status. It may | 271 // 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 | 272 // contain an empty vector if the category is available but empty (or still |
| 257 // loading). | 273 // loading). |
| 258 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> | 274 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> |
| 259 suggestions_by_category_; | 275 suggestions_by_category_; |
| 260 | 276 |
| 261 // Observer for the HistoryService. All providers are notified when history is | 277 // Observer for the HistoryService. All providers are notified when history is |
| 262 // deleted. | 278 // deleted. |
| 263 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 279 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 264 history_service_observer_; | 280 history_service_observer_; |
| 265 | 281 |
| 266 base::ObserverList<Observer> observers_; | 282 base::ObserverList<Observer> observers_; |
| 267 | 283 |
| 268 const std::vector<ContentSuggestion> no_suggestions_; | 284 const std::vector<ContentSuggestion> no_suggestions_; |
| 269 | 285 |
| 270 // Keep a direct reference to this special provider to redirect scheduling, | 286 // Keep a direct reference to this special provider to redirect scheduling, |
| 271 // background fetching and debugging calls to it. If the NTPSnippetsService is | 287 // background fetching and debugging calls to it. If the NTPSnippetsService is |
| 272 // loaded, it is also present in |providers_|, otherwise this is a nullptr. | 288 // loaded, it is also present in |providers_|, otherwise this is a nullptr. |
| 273 NTPSnippetsService* ntp_snippets_service_; | 289 NTPSnippetsService* ntp_snippets_service_; |
| 274 | 290 |
| 291 PrefService* pref_service_; | |
| 292 | |
| 275 UserClassifier user_classifier_; | 293 UserClassifier user_classifier_; |
| 276 | 294 |
| 277 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 295 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| 278 }; | 296 }; |
| 279 | 297 |
| 280 } // namespace ntp_snippets | 298 } // namespace ntp_snippets |
| 281 | 299 |
| 282 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 300 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |