| 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/callbacks.h" | 22 #include "components/ntp_snippets/callbacks.h" |
| 23 #include "components/ntp_snippets/category_factory.h" | 23 #include "components/ntp_snippets/category_factory.h" |
| 24 #include "components/ntp_snippets/category_status.h" | 24 #include "components/ntp_snippets/category_status.h" |
| 25 #include "components/ntp_snippets/content_suggestions_provider.h" | 25 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 26 #include "components/ntp_snippets/user_classifier.h" | 26 #include "components/ntp_snippets/user_classifier.h" |
| 27 #include "components/signin/core/browser/signin_manager.h" |
| 27 | 28 |
| 28 class PrefService; | 29 class PrefService; |
| 29 class PrefRegistrySimple; | 30 class PrefRegistrySimple; |
| 30 | 31 |
| 31 namespace ntp_snippets { | 32 namespace ntp_snippets { |
| 32 | 33 |
| 33 class RemoteSuggestionsProvider; | 34 class RemoteSuggestionsProvider; |
| 34 | 35 |
| 35 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves | 36 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves |
| 36 // them grouped into categories. There can be at most one provider per category. | 37 // them grouped into categories. There can be at most one provider per category. |
| 37 class ContentSuggestionsService : public KeyedService, | 38 class ContentSuggestionsService : public KeyedService, |
| 38 public ContentSuggestionsProvider::Observer, | 39 public ContentSuggestionsProvider::Observer, |
| 40 public SigninManagerBase::Observer, |
| 39 public history::HistoryServiceObserver { | 41 public history::HistoryServiceObserver { |
| 40 public: | 42 public: |
| 41 class Observer { | 43 class Observer { |
| 42 public: | 44 public: |
| 43 // 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 |
| 44 // |category|, replacing any previously available data (though in most cases | 46 // |category|, replacing any previously available data (though in most cases |
| 45 // 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 |
| 46 // data is then available through |GetSuggestionsForCategory(category)|. | 48 // data is then available through |GetSuggestionsForCategory(category)|. |
| 47 virtual void OnNewSuggestions(Category category) = 0; | 49 virtual void OnNewSuggestions(Category category) = 0; |
| 48 | 50 |
| 49 // Fired when the status of a suggestions category changed. When the status | 51 // Fired when the status of a suggestions category changed. When the status |
| 50 // changes to an unavailable status, the suggestions of the respective | 52 // changes to an unavailable status, the suggestions of the respective |
| 51 // category have been invalidated, which means that they must no longer be | 53 // category have been invalidated, which means that they must no longer be |
| 52 // displayed to the user. The UI must immediately clear any suggestions of | 54 // displayed to the user. The UI must immediately clear any suggestions of |
| 53 // that category. | 55 // that category. |
| 54 virtual void OnCategoryStatusChanged(Category category, | 56 virtual void OnCategoryStatusChanged(Category category, |
| 55 CategoryStatus new_status) = 0; | 57 CategoryStatus new_status) = 0; |
| 56 | 58 |
| 57 // Fired when a suggestion has been invalidated. The UI must immediately | 59 // Fired when a suggestion has been invalidated. The UI must immediately |
| 58 // clear the suggestion even from open NTPs. Invalidation happens, for | 60 // clear the suggestion even from open NTPs. Invalidation happens, for |
| 59 // example, when the content that the suggestion refers to is gone. | 61 // example, when the content that the suggestion refers to is gone. |
| 60 // Note that this event may be fired even if the corresponding category is | 62 // Note that this event may be fired even if the corresponding category is |
| 61 // not currently AVAILABLE, because open UIs may still be showing the | 63 // not currently AVAILABLE, because open UIs may still be showing the |
| 62 // suggestion that is to be removed. This event may also be fired for | 64 // suggestion that is to be removed. This event may also be fired for |
| 63 // |suggestion_id|s that never existed and should be ignored in that case. | 65 // |suggestion_id|s that never existed and should be ignored in that case. |
| 64 virtual void OnSuggestionInvalidated( | 66 virtual void OnSuggestionInvalidated( |
| 65 const ContentSuggestion::ID& suggestion_id) = 0; | 67 const ContentSuggestion::ID& suggestion_id) = 0; |
| 66 | 68 |
| 69 // Fired when the previously sent data is not valid anymore and a refresh |
| 70 // of all the suggestions is required. Called for example when the sign in |
| 71 // state changes and personalised suggestions have to be shown or discarded. |
| 72 virtual void OnFullRefreshRequired() = 0; |
| 73 |
| 67 // Sent when the service is shutting down. After the service has shut down, | 74 // Sent when the service is shutting down. After the service has shut down, |
| 68 // it will not provide any data anymore, though calling the getters is still | 75 // it will not provide any data anymore, though calling the getters is still |
| 69 // safe. | 76 // safe. |
| 70 virtual void ContentSuggestionsServiceShutdown() = 0; | 77 virtual void ContentSuggestionsServiceShutdown() = 0; |
| 71 | 78 |
| 72 protected: | 79 protected: |
| 73 virtual ~Observer() = default; | 80 virtual ~Observer() = default; |
| 74 }; | 81 }; |
| 75 | 82 |
| 76 enum State { | 83 enum State { |
| 77 ENABLED, | 84 ENABLED, |
| 78 DISABLED, | 85 DISABLED, |
| 79 }; | 86 }; |
| 80 | 87 |
| 81 ContentSuggestionsService(State state, | 88 ContentSuggestionsService(State state, |
| 89 SigninManagerBase* signin_manager, |
| 82 history::HistoryService* history_service, | 90 history::HistoryService* history_service, |
| 83 PrefService* pref_service); | 91 PrefService* pref_service); |
| 84 ~ContentSuggestionsService() override; | 92 ~ContentSuggestionsService() override; |
| 85 | 93 |
| 86 // Inherited from KeyedService. | 94 // Inherited from KeyedService. |
| 87 void Shutdown() override; | 95 void Shutdown() override; |
| 88 | 96 |
| 89 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 97 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 90 | 98 |
| 91 State state() { return state_; } | 99 State state() { return state_; } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 void OnNewSuggestions(ContentSuggestionsProvider* provider, | 212 void OnNewSuggestions(ContentSuggestionsProvider* provider, |
| 205 Category category, | 213 Category category, |
| 206 std::vector<ContentSuggestion> suggestions) override; | 214 std::vector<ContentSuggestion> suggestions) override; |
| 207 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | 215 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, |
| 208 Category category, | 216 Category category, |
| 209 CategoryStatus new_status) override; | 217 CategoryStatus new_status) override; |
| 210 void OnSuggestionInvalidated( | 218 void OnSuggestionInvalidated( |
| 211 ContentSuggestionsProvider* provider, | 219 ContentSuggestionsProvider* provider, |
| 212 const ContentSuggestion::ID& suggestion_id) override; | 220 const ContentSuggestion::ID& suggestion_id) override; |
| 213 | 221 |
| 222 // SigninManagerBase::Observer implementation |
| 223 void GoogleSigninSucceeded(const std::string& account_id, |
| 224 const std::string& username, |
| 225 const std::string& password) override; |
| 226 void GoogleSignedOut(const std::string& account_id, |
| 227 const std::string& username) override; |
| 228 |
| 214 // history::HistoryServiceObserver implementation. | 229 // history::HistoryServiceObserver implementation. |
| 215 void OnURLsDeleted(history::HistoryService* history_service, | 230 void OnURLsDeleted(history::HistoryService* history_service, |
| 216 bool all_history, | 231 bool all_history, |
| 217 bool expired, | 232 bool expired, |
| 218 const history::URLRows& deleted_rows, | 233 const history::URLRows& deleted_rows, |
| 219 const std::set<GURL>& favicon_urls) override; | 234 const std::set<GURL>& favicon_urls) override; |
| 220 void HistoryServiceBeingDeleted( | 235 void HistoryServiceBeingDeleted( |
| 221 history::HistoryService* history_service) override; | 236 history::HistoryService* history_service) override; |
| 222 | 237 |
| 223 // Registers the given |provider| for the given |category|, unless it is | 238 // Registers the given |provider| for the given |category|, unless it is |
| 224 // already registered. Returns true if the category was newly registered or | 239 // already registered. Returns true if the category was newly registered or |
| 225 // false if it is dismissed or was present before. | 240 // false if it is dismissed or was present before. |
| 226 bool TryRegisterProviderForCategory(ContentSuggestionsProvider* provider, | 241 bool TryRegisterProviderForCategory(ContentSuggestionsProvider* provider, |
| 227 Category category); | 242 Category category); |
| 228 void RegisterCategory(Category category, | 243 void RegisterCategory(Category category, |
| 229 ContentSuggestionsProvider* provider); | 244 ContentSuggestionsProvider* provider); |
| 230 void UnregisterCategory(Category category, | 245 void UnregisterCategory(Category category, |
| 231 ContentSuggestionsProvider* provider); | 246 ContentSuggestionsProvider* provider); |
| 232 | 247 |
| 233 // Removes a suggestion from the local store |suggestions_by_category_|, if it | 248 // Removes a suggestion from the local store |suggestions_by_category_|, if it |
| 234 // exists. Returns true if a suggestion was removed. | 249 // exists. Returns true if a suggestion was removed. |
| 235 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); | 250 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); |
| 236 | 251 |
| 237 // Fires the OnCategoryStatusChanged event for the given |category|. | 252 // Fires the OnCategoryStatusChanged event for the given |category|. |
| 238 void NotifyCategoryStatusChanged(Category category); | 253 void NotifyCategoryStatusChanged(Category category); |
| 239 | 254 |
| 255 void OnSignInStateChanged(); |
| 256 |
| 240 void SortCategories(); | 257 void SortCategories(); |
| 241 | 258 |
| 242 // Re-enables a dismissed category, making querying its provider possible. | 259 // Re-enables a dismissed category, making querying its provider possible. |
| 243 void RestoreDismissedCategory(Category category); | 260 void RestoreDismissedCategory(Category category); |
| 244 | 261 |
| 245 void RestoreDismissedCategoriesFromPrefs(); | 262 void RestoreDismissedCategoriesFromPrefs(); |
| 246 void StoreDismissedCategoriesToPrefs(); | 263 void StoreDismissedCategoriesToPrefs(); |
| 247 | 264 |
| 248 // Whether the content suggestions feature is enabled. | 265 // Whether the content suggestions feature is enabled. |
| 249 State state_; | 266 State state_; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 273 // |providers_by_category_|. | 290 // |providers_by_category_|. |
| 274 std::vector<Category> categories_; | 291 std::vector<Category> categories_; |
| 275 | 292 |
| 276 // All current suggestions grouped by category. This contains an entry for | 293 // All current suggestions grouped by category. This contains an entry for |
| 277 // every category in |categories_| whose status is an available status. It may | 294 // every category in |categories_| whose status is an available status. It may |
| 278 // contain an empty vector if the category is available but empty (or still | 295 // contain an empty vector if the category is available but empty (or still |
| 279 // loading). | 296 // loading). |
| 280 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> | 297 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> |
| 281 suggestions_by_category_; | 298 suggestions_by_category_; |
| 282 | 299 |
| 300 // Observer for the SigninManager. All observers are notified when the signin |
| 301 // state changes so that they can refresh their list of suggestions. |
| 302 ScopedObserver<SigninManagerBase, SigninManagerBase::Observer> |
| 303 signin_observer_; |
| 304 |
| 283 // Observer for the HistoryService. All providers are notified when history is | 305 // Observer for the HistoryService. All providers are notified when history is |
| 284 // deleted. | 306 // deleted. |
| 285 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 307 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 286 history_service_observer_; | 308 history_service_observer_; |
| 287 | 309 |
| 288 base::ObserverList<Observer> observers_; | 310 base::ObserverList<Observer> observers_; |
| 289 | 311 |
| 290 const std::vector<ContentSuggestion> no_suggestions_; | 312 const std::vector<ContentSuggestion> no_suggestions_; |
| 291 | 313 |
| 292 // Keep a direct reference to this special provider to redirect scheduling, | 314 // Keep a direct reference to this special provider to redirect scheduling, |
| 293 // background fetching and debugging calls to it. If the | 315 // background fetching and debugging calls to it. If the |
| 294 // RemoteSuggestionsProvider is loaded, it is also present in |providers_|, | 316 // RemoteSuggestionsProvider is loaded, it is also present in |providers_|, |
| 295 // otherwise this is a nullptr. | 317 // otherwise this is a nullptr. |
| 296 RemoteSuggestionsProvider* ntp_snippets_service_; | 318 RemoteSuggestionsProvider* ntp_snippets_service_; |
| 297 | 319 |
| 298 PrefService* pref_service_; | 320 PrefService* pref_service_; |
| 299 | 321 |
| 300 UserClassifier user_classifier_; | 322 UserClassifier user_classifier_; |
| 301 | 323 |
| 302 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 324 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| 303 }; | 325 }; |
| 304 | 326 |
| 305 } // namespace ntp_snippets | 327 } // namespace ntp_snippets |
| 306 | 328 |
| 307 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 329 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |