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/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 that personalised suggestions have to be shown or | |
|
Marc Treib
2016/11/21 15:48:53
Remove "that"?
dgn
2016/11/21 16:22:08
Done.
| |
| 72 // discarded. | |
| 73 virtual void OnFullRefreshRequired() = 0; | |
| 74 | |
| 67 // Sent when the service is shutting down. After the service has shut down, | 75 // 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 | 76 // it will not provide any data anymore, though calling the getters is still |
| 69 // safe. | 77 // safe. |
| 70 virtual void ContentSuggestionsServiceShutdown() = 0; | 78 virtual void ContentSuggestionsServiceShutdown() = 0; |
| 71 | 79 |
| 72 protected: | 80 protected: |
| 73 virtual ~Observer() = default; | 81 virtual ~Observer() = default; |
| 74 }; | 82 }; |
| 75 | 83 |
| 76 enum State { | 84 enum State { |
| 77 ENABLED, | 85 ENABLED, |
| 78 DISABLED, | 86 DISABLED, |
| 79 }; | 87 }; |
| 80 | 88 |
| 81 ContentSuggestionsService(State state, | 89 ContentSuggestionsService(State state, |
| 90 SigninManagerBase* signin_manager, | |
| 82 history::HistoryService* history_service, | 91 history::HistoryService* history_service, |
| 83 PrefService* pref_service); | 92 PrefService* pref_service); |
| 84 ~ContentSuggestionsService() override; | 93 ~ContentSuggestionsService() override; |
| 85 | 94 |
| 86 // Inherited from KeyedService. | 95 // Inherited from KeyedService. |
| 87 void Shutdown() override; | 96 void Shutdown() override; |
| 88 | 97 |
| 89 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 98 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 90 | 99 |
| 91 State state() { return state_; } | 100 State state() { return state_; } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 void OnNewSuggestions(ContentSuggestionsProvider* provider, | 213 void OnNewSuggestions(ContentSuggestionsProvider* provider, |
| 205 Category category, | 214 Category category, |
| 206 std::vector<ContentSuggestion> suggestions) override; | 215 std::vector<ContentSuggestion> suggestions) override; |
| 207 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | 216 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, |
| 208 Category category, | 217 Category category, |
| 209 CategoryStatus new_status) override; | 218 CategoryStatus new_status) override; |
| 210 void OnSuggestionInvalidated( | 219 void OnSuggestionInvalidated( |
| 211 ContentSuggestionsProvider* provider, | 220 ContentSuggestionsProvider* provider, |
| 212 const ContentSuggestion::ID& suggestion_id) override; | 221 const ContentSuggestion::ID& suggestion_id) override; |
| 213 | 222 |
| 223 // SigninManagerBase::Observer implementation | |
| 224 void GoogleSigninSucceeded(const std::string& account_id, | |
| 225 const std::string& username, | |
| 226 const std::string& password) override; | |
| 227 void GoogleSignedOut(const std::string& account_id, | |
| 228 const std::string& username) override; | |
| 229 | |
| 214 // history::HistoryServiceObserver implementation. | 230 // history::HistoryServiceObserver implementation. |
| 215 void OnURLsDeleted(history::HistoryService* history_service, | 231 void OnURLsDeleted(history::HistoryService* history_service, |
| 216 bool all_history, | 232 bool all_history, |
| 217 bool expired, | 233 bool expired, |
| 218 const history::URLRows& deleted_rows, | 234 const history::URLRows& deleted_rows, |
| 219 const std::set<GURL>& favicon_urls) override; | 235 const std::set<GURL>& favicon_urls) override; |
| 220 void HistoryServiceBeingDeleted( | 236 void HistoryServiceBeingDeleted( |
| 221 history::HistoryService* history_service) override; | 237 history::HistoryService* history_service) override; |
| 222 | 238 |
| 223 // Registers the given |provider| for the given |category|, unless it is | 239 // Registers the given |provider| for the given |category|, unless it is |
| 224 // already registered. Returns true if the category was newly registered or | 240 // already registered. Returns true if the category was newly registered or |
| 225 // false if it is dismissed or was present before. | 241 // false if it is dismissed or was present before. |
| 226 bool TryRegisterProviderForCategory(ContentSuggestionsProvider* provider, | 242 bool TryRegisterProviderForCategory(ContentSuggestionsProvider* provider, |
| 227 Category category); | 243 Category category); |
| 228 void RegisterCategory(Category category, | 244 void RegisterCategory(Category category, |
| 229 ContentSuggestionsProvider* provider); | 245 ContentSuggestionsProvider* provider); |
| 230 void UnregisterCategory(Category category, | 246 void UnregisterCategory(Category category, |
| 231 ContentSuggestionsProvider* provider); | 247 ContentSuggestionsProvider* provider); |
| 232 | 248 |
| 233 // Removes a suggestion from the local store |suggestions_by_category_|, if it | 249 // Removes a suggestion from the local store |suggestions_by_category_|, if it |
| 234 // exists. Returns true if a suggestion was removed. | 250 // exists. Returns true if a suggestion was removed. |
| 235 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); | 251 bool RemoveSuggestionByID(const ContentSuggestion::ID& suggestion_id); |
| 236 | 252 |
| 237 // Fires the OnCategoryStatusChanged event for the given |category|. | 253 // Fires the OnCategoryStatusChanged event for the given |category|. |
| 238 void NotifyCategoryStatusChanged(Category category); | 254 void NotifyCategoryStatusChanged(Category category); |
| 239 | 255 |
| 256 void OnSignInStateChanged(); | |
| 257 | |
| 240 void SortCategories(); | 258 void SortCategories(); |
| 241 | 259 |
| 242 // Re-enables a dismissed category, making querying its provider possible. | 260 // Re-enables a dismissed category, making querying its provider possible. |
| 243 void RestoreDismissedCategory(Category category); | 261 void RestoreDismissedCategory(Category category); |
| 244 | 262 |
| 245 void RestoreDismissedCategoriesFromPrefs(); | 263 void RestoreDismissedCategoriesFromPrefs(); |
| 246 void StoreDismissedCategoriesToPrefs(); | 264 void StoreDismissedCategoriesToPrefs(); |
| 247 | 265 |
| 248 // Whether the content suggestions feature is enabled. | 266 // Whether the content suggestions feature is enabled. |
| 249 State state_; | 267 State state_; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 273 // |providers_by_category_|. | 291 // |providers_by_category_|. |
| 274 std::vector<Category> categories_; | 292 std::vector<Category> categories_; |
| 275 | 293 |
| 276 // All current suggestions grouped by category. This contains an entry for | 294 // All current suggestions grouped by category. This contains an entry for |
| 277 // every category in |categories_| whose status is an available status. It may | 295 // 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 | 296 // contain an empty vector if the category is available but empty (or still |
| 279 // loading). | 297 // loading). |
| 280 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> | 298 std::map<Category, std::vector<ContentSuggestion>, Category::CompareByID> |
| 281 suggestions_by_category_; | 299 suggestions_by_category_; |
| 282 | 300 |
| 301 // Observer for the SigninManager. All observers are notified when the signin | |
| 302 // state changes so that they can refresh their list of suggestions. | |
| 303 ScopedObserver<SigninManagerBase, SigninManagerBase::Observer> | |
| 304 signin_observer_; | |
| 305 | |
| 283 // Observer for the HistoryService. All providers are notified when history is | 306 // Observer for the HistoryService. All providers are notified when history is |
| 284 // deleted. | 307 // deleted. |
| 285 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 308 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 286 history_service_observer_; | 309 history_service_observer_; |
| 287 | 310 |
| 288 base::ObserverList<Observer> observers_; | 311 base::ObserverList<Observer> observers_; |
| 289 | 312 |
| 290 const std::vector<ContentSuggestion> no_suggestions_; | 313 const std::vector<ContentSuggestion> no_suggestions_; |
| 291 | 314 |
| 292 // Keep a direct reference to this special provider to redirect scheduling, | 315 // Keep a direct reference to this special provider to redirect scheduling, |
| 293 // background fetching and debugging calls to it. If the | 316 // background fetching and debugging calls to it. If the |
| 294 // RemoteSuggestionsProvider is loaded, it is also present in |providers_|, | 317 // RemoteSuggestionsProvider is loaded, it is also present in |providers_|, |
| 295 // otherwise this is a nullptr. | 318 // otherwise this is a nullptr. |
| 296 RemoteSuggestionsProvider* ntp_snippets_service_; | 319 RemoteSuggestionsProvider* ntp_snippets_service_; |
| 297 | 320 |
| 298 PrefService* pref_service_; | 321 PrefService* pref_service_; |
| 299 | 322 |
| 300 UserClassifier user_classifier_; | 323 UserClassifier user_classifier_; |
| 301 | 324 |
| 302 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); | 325 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| 303 }; | 326 }; |
| 304 | 327 |
| 305 } // namespace ntp_snippets | 328 } // namespace ntp_snippets |
| 306 | 329 |
| 307 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ | 330 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| OLD | NEW |