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..f803af6cecb17639c1d27071372260a2bfcf59b9 |
--- /dev/null |
+++ b/components/ntp_snippets/content_suggestions_service.h |
@@ -0,0 +1,134 @@ |
+// 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 { |
+ |
+class ContentSuggestionsServiceObserver; |
+ |
+// Retrieves suggestions from a number of ContentSuggestionsProviders and serves |
+// them grouped into categories. |
+class ContentSuggestionsService : public KeyedService, |
+ public ContentSuggestionsProvider::Delegate { |
+ public: |
+ using ImageFetchedCallback = |
+ base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; |
+ |
+ ContentSuggestionsService(bool enabled); |
+ ~ContentSuggestionsService() override; |
+ |
+ // Inherited from KeyedService. |
+ void Shutdown() override; |
+ |
+ // Available suggestions |
+ const std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>>& |
+ suggestions() const { |
+ return suggestions_by_category_; |
+ } |
+ const std::vector<ContentSuggestion>& suggestions( |
+ ContentSuggestionCategory category) { |
+ return suggestions_by_category_[category]; |
+ } |
+ |
+ // 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 FetchImage(ContentSuggestionsProviderType provider_type, |
+ 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. |
+ void Discard(ContentSuggestionsProviderType provider_type, |
+ const std::string& suggestion_id); |
+ |
+ // Observer accessors. |
+ void AddObserver(ContentSuggestionsServiceObserver* observer); |
+ void RemoveObserver(ContentSuggestionsServiceObserver* observer); |
+ |
+ // Registers a new ContentSuggestionsProvider. This method will fail when a |
Marc Treib
2016/06/28 15:04:55
nit: s/when/if
Philipp Keck
2016/06/30 09:35:35
Done.
|
+ // 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: |
+ // When |enabled_| is true the service will retrieve suggestions from the |
+ // providers. |
+ bool enabled_; |
+ |
+ // All current suggestions. |
+ std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>> |
+ suggestions_by_category_; |
+ |
+ std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*> |
+ providers_; |
+ |
+ // The observers. |
+ base::ObserverList<ContentSuggestionsServiceObserver> observers_; |
+ |
+ // Implementation of ContentSuggestionsProvider::Delegate. |
+ void OnSuggestionsChanged( |
+ const ContentSuggestionsProvider& source, |
+ ContentSuggestionCategory changed_category, |
+ std::vector<ContentSuggestion> suggestions) override; |
+ |
+ void OnProviderShutdown(const ContentSuggestionsProvider& source) override; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); |
+}; |
+ |
+class ContentSuggestionsServiceObserver { |
+ public: |
+ // Fired every time the service loads a new set of data. This event is fired |
+ // per |changed_category|. The new data is then available through |
+ // |suggestions()[changed_category]|. |
+ virtual void OnSuggestionsChanged( |
+ ContentSuggestionCategory changed_category) = 0; |
Marc Treib
2016/06/28 15:04:55
Hm, does this even need to include the category? A
Philipp Keck
2016/06/30 09:35:35
Done.
|
+ |
+ // Sent when the service is shutting down. |
+ virtual void ContentSuggestionsServiceShutdown() = 0; |
+ |
+ protected: |
+ virtual ~ContentSuggestionsServiceObserver() {} |
+}; |
+ |
+} // namespace ntp_snippets |
+ |
+#endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ |