| 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 17 matching lines...) Expand all Loading... |
| 122 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id); | 125 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id); |
| 123 | 126 |
| 124 // Dismisses the given |category|, if it exists. | 127 // Dismisses the given |category|, if it exists. |
| 125 // This will not trigger an update through the observers. | 128 // This will not trigger an update through the observers. |
| 126 void DismissCategory(Category category); | 129 void DismissCategory(Category category); |
| 127 | 130 |
| 128 // Restores all dismissed categories. | 131 // Restores all dismissed categories. |
| 129 // This will not trigger an update through the observers. | 132 // This will not trigger an update through the observers. |
| 130 void RestoreDismissedCategories(); | 133 void RestoreDismissedCategories(); |
| 131 | 134 |
| 135 // Returns whether |category| is dismissed. |
| 136 bool IsCategoryDismissed(Category category) const; |
| 137 |
| 132 // Observer accessors. | 138 // Observer accessors. |
| 133 void AddObserver(Observer* observer); | 139 void AddObserver(Observer* observer); |
| 134 void RemoveObserver(Observer* observer); | 140 void RemoveObserver(Observer* observer); |
| 135 | 141 |
| 136 // Registers a new ContentSuggestionsProvider. It must be ensured that at most | 142 // Registers a new ContentSuggestionsProvider. It must be ensured that at most |
| 137 // one provider is registered for every category and that this method is | 143 // one provider is registered for every category and that this method is |
| 138 // called only once per provider. | 144 // called only once per provider. |
| 139 void RegisterProvider(std::unique_ptr<ContentSuggestionsProvider> provider); | 145 void RegisterProvider(std::unique_ptr<ContentSuggestionsProvider> provider); |
| 140 | 146 |
| 141 // Removes history from the specified time range where the URL matches the | 147 // Removes history from the specified time range where the URL matches the |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 void OnURLsDeleted(history::HistoryService* history_service, | 211 void OnURLsDeleted(history::HistoryService* history_service, |
| 206 bool all_history, | 212 bool all_history, |
| 207 bool expired, | 213 bool expired, |
| 208 const history::URLRows& deleted_rows, | 214 const history::URLRows& deleted_rows, |
| 209 const std::set<GURL>& favicon_urls) override; | 215 const std::set<GURL>& favicon_urls) override; |
| 210 void HistoryServiceBeingDeleted( | 216 void HistoryServiceBeingDeleted( |
| 211 history::HistoryService* history_service) override; | 217 history::HistoryService* history_service) override; |
| 212 | 218 |
| 213 // Registers the given |provider| for the given |category|, unless it is | 219 // Registers the given |provider| for the given |category|, unless it is |
| 214 // already registered. Returns true if the category was newly registered or | 220 // already registered. Returns true if the category was newly registered or |
| 215 // false if it was present before. | 221 // false if it is dismissed or was present before. |
| 216 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider, | 222 bool TryRegisterProviderForCategory(ContentSuggestionsProvider* provider, |
| 217 Category category); | 223 Category category); |
| 224 void RegisterCategory(Category category, |
| 225 ContentSuggestionsProvider* provider); |
| 226 void UnregisterCategory(Category category, |
| 227 ContentSuggestionsProvider* provider); |
| 218 | 228 |
| 219 // Removes a suggestion from the local store |suggestions_by_category_|, if it | 229 // Removes a suggestion from the local store |suggestions_by_category_|, if it |
| 220 // exists. Returns true if a suggestion was removed. | 230 // exists. Returns true if a suggestion was removed. |
| 221 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); | 231 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); |
| 222 | 232 |
| 223 // Fires the OnCategoryStatusChanged event for the given |category|. | 233 // Fires the OnCategoryStatusChanged event for the given |category|. |
| 224 void NotifyCategoryStatusChanged(Category category); | 234 void NotifyCategoryStatusChanged(Category category); |
| 225 | 235 |
| 226 void SortCategories(); | 236 void SortCategories(); |
| 227 | 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(). The provider can be null if the dismissed |
| 262 // category has received no updates since initialisation. |
| 263 // (see RestoreDismissedCategoriesFromPrefs()) |
| 246 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> | 264 std::map<Category, ContentSuggestionsProvider*, Category::CompareByID> |
| 247 dismissed_providers_by_category_; | 265 dismissed_providers_by_category_; |
| 248 | 266 |
| 249 // All current suggestion categories, in an order determined by the | 267 // All current suggestion categories, in an order determined by the |
| 250 // |category_factory_|. This vector contains exactly the same categories as | 268 // |category_factory_|. This vector contains exactly the same categories as |
| 251 // |providers_by_category_|. | 269 // |providers_by_category_|. |
| 252 std::vector<Category> categories_; | 270 std::vector<Category> categories_; |
| 253 | 271 |
| 254 // All current suggestions grouped by category. This contains an entry for | 272 // All current suggestions grouped by category. This contains an entry for |
| 255 // every category in |categories_| whose status is an available status. It may | 273 // 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 | 274 // contain an empty vector if the category is available but empty (or still |
| 257 // loading). | 275 // loading). |
| 258 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> | 276 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> |
| 259 suggestions_by_category_; | 277 suggestions_by_category_; |
| 260 | 278 |
| 261 // Observer for the HistoryService. All providers are notified when history is | 279 // Observer for the HistoryService. All providers are notified when history is |
| 262 // deleted. | 280 // deleted. |
| 263 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 281 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 264 history_service_observer_; | 282 history_service_observer_; |
| 265 | 283 |
| 266 base::ObserverList<Observer> observers_; | 284 base::ObserverList<Observer> observers_; |
| 267 | 285 |
| 268 const std::vector<ContentSuggestion> no_suggestions_; | 286 const std::vector<ContentSuggestion> no_suggestions_; |
| 269 | 287 |
| 270 // Keep a direct reference to this special provider to redirect scheduling, | 288 // Keep a direct reference to this special provider to redirect scheduling, |
| 271 // background fetching and debugging calls to it. If the NTPSnippetsService is | 289 // background fetching and debugging calls to it. If the NTPSnippetsService is |
| 272 // loaded, it is also present in |providers_|, otherwise this is a nullptr. | 290 // loaded, it is also present in |providers_|, otherwise this is a nullptr. |
| 273 NTPSnippetsService* ntp_snippets_service_; | 291 NTPSnippetsService* ntp_snippets_service_; |
| 274 | 292 |
| 293 PrefService* pref_service_; |
| 294 |
| 275 UserClassifier user_classifier_; | 295 UserClassifier user_classifier_; |
| 276 | 296 |
| 277 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 297 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| 278 }; | 298 }; |
| 279 | 299 |
| 280 } // namespace ntp_snippets | 300 } // namespace ntp_snippets |
| 281 | 301 |
| 282 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 302 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |