Chromium Code Reviews| Index: components/ntp_snippets/content_suggestions_service.h |
| diff --git a/components/ntp_snippets/content_suggestions_service.h b/components/ntp_snippets/content_suggestions_service.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d90f960ae77847c8b52dcab4318706569dcbdfb4 |
| --- /dev/null |
| +++ b/components/ntp_snippets/content_suggestions_service.h |
| @@ -0,0 +1,129 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| +#define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |
| + |
| +#include <stddef.h> |
| + |
| +#include <map> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/callback_forward.h" |
| +#include "base/observer_list.h" |
| +#include "components/keyed_service/core/keyed_service.h" |
| +#include "components/ntp_snippets/content_suggestions_provider.h" |
| +#include "components/ntp_snippets/content_suggestions_provider_type.h" |
| + |
| +namespace gfx { |
| +class Image; |
| +} |
| + |
| +namespace ntp_snippets { |
| + |
| +// Retrieves suggestions from a number of ContentSuggestionsProviders and serves |
| +// them grouped into categories. |
|
Marc Treib
2016/06/30 10:36:54
Add a "not used yet" comment, similar to ContentSu
Philipp Keck
2016/06/30 17:14:08
Done.
|
| +class ContentSuggestionsService : public KeyedService, |
| + public ContentSuggestionsProvider::Observer { |
| + public: |
| + using ImageFetchedCallback = |
| + base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; |
| + |
| + class Observer { |
| + public: |
| + // Fired every time the service loads a new set of data. The new data is |
| + // then available through the getters of the service. |
| + virtual void OnSuggestionsChanged() = 0; |
|
tschumann
2016/06/30 10:55:19
The service interface is taking on a lot of roles.
Marc Treib
2016/06/30 11:34:23
Hm. I'm not convinced that's worth the extra compl
tschumann
2016/06/30 11:58:11
While that's true, I think it would be nice if the
Philipp Keck
2016/06/30 17:14:08
The observer here is the UI, really. The UI needs
Marc Treib
2016/07/01 09:15:32
+1. I see the point of Tim's suggestion, but I thi
Philipp Keck
2016/07/01 13:00:04
Acknowledged.
|
| + |
| + // Sent when the service is shutting down. |
|
tschumann
2016/06/30 10:55:19
what are the post conditions after this function?
Philipp Keck
2016/06/30 17:14:09
Done.
|
| + virtual void ContentSuggestionsServiceShutdown() = 0; |
| + |
| + protected: |
| + virtual ~Observer() {} |
| + }; |
| + |
| + ContentSuggestionsService(bool enabled); |
| + ~ContentSuggestionsService() override; |
| + |
| + // Inherited from KeyedService. |
| + void Shutdown() override; |
| + |
| + // Getters for available categories and suggestions in these categories |
| + const std::vector<ContentSuggestionCategory>& GetCategories() const { |
| + return categories_; |
| + } |
| + const std::vector<ContentSuggestion>& GetSuggestionsForCategory( |
| + ContentSuggestionCategory category) const; |
| + |
| + // Fetches the image for the suggestion with the given |suggestion_id| and |
| + // runs the |callback|. If that suggestion doesn't exist or the fetch fails, |
| + // the callback gets an empty image. |
| + void FetchSuggestionImage(const std::string& suggestion_id, |
| + const ImageFetchedCallback& callback); |
| + |
| + // Only for debugging use through the internals page. |
| + // Removes all suggestions from all caches or internal stores in all |
| + // providers. It does, however, not remove any suggestions from the provider's |
| + // sources, so if their configuration hasn't changed, they should return the |
| + // same results when they fetch the next time. In particular, calling this |
| + // method will not mark any suggestions as discarded. |
| + void ClearCachedSuggestions(); |
| + |
| + // Only for debugging use through the internals page. Some providers |
| + // internally store a list of discarded suggestions to prevent them from |
| + // reappearing. This function clears all such lists in all providers, making |
| + // discarded suggestions reappear (only for certain providers). |
| + void ClearDiscardedSuggestions(); |
| + |
| + // Discards the suggestion with the given |suggestion_id|, if it exists. |
| + // This won't necessarily trigger an update through the observers. |
| + void DiscardSuggestion(const std::string& suggestion_id); |
| + |
| + // Observer accessors. |
| + void AddObserver(Observer* observer); |
| + void RemoveObserver(Observer* observer); |
| + |
| + // Registers a new ContentSuggestionsProvider. This method will fail if a |
| + // provider of the same type is already registered. |
| + void RegisterProvider(ContentSuggestionsProvider* provider); |
| + |
| + // For use by the snippets-internals page only. |
| + const std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*>& |
| + providers() const { |
| + return providers_; |
| + } |
| + |
| + private: |
| + // Implementation of ContentSuggestionsProvider::Observer. |
| + void OnSuggestionsChanged( |
| + ContentSuggestionsProviderType provider_type, |
| + ContentSuggestionCategory changed_category, |
| + std::vector<ContentSuggestion> suggestions) override; |
| + |
| + void OnProviderShutdown( |
|
Marc Treib
2016/06/30 10:36:54
This is one of the cases where there usually isn't
Philipp Keck
2016/06/30 17:14:09
Yep, this empty line has been inserted because of
Marc Treib
2016/07/01 09:15:32
I'd argue that nobody who reads this header should
|
| + ContentSuggestionsProviderType provider_type) override; |
| + |
| + // When |enabled_| is true the service will retrieve suggestions from the |
| + // providers. |
| + bool enabled_; |
| + |
| + // All current suggestion categories, in order. |
|
tschumann
2016/06/30 10:55:19
what's the order?
Philipp Keck
2016/06/30 17:14:09
That's not yet defined. It's a UX question, I gues
|
| + std::vector<ContentSuggestionCategory> categories_; |
| + // All current suggestions grouped by category. |
| + std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>> |
| + suggestions_by_category_; |
|
Marc Treib
2016/06/30 10:36:54
Hm, maybe at this point just a vector of pairs wou
Philipp Keck
2016/06/30 17:14:09
Acknowledged.
|
| + |
| + std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*> |
| + providers_; |
| + |
| + // The observers. |
|
Marc Treib
2016/06/30 10:36:54
This is kinda "add 1 to i" :D
tschumann
2016/06/30 10:55:19
you can drop this comment -- it's not adding any v
Philipp Keck
2016/06/30 17:14:09
Done.
|
| + base::ObserverList<Observer> observers_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
| +}; |
| + |
| +} // namespace ntp_snippets |
| + |
| +#endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |