Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Unified Diff: components/ntp_snippets/content_suggestions_service.h

Issue 2102023002: Add ContentSuggestionsService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename ContentSuggestionsProvider::Delegate::OnContentChanged to OnSuggestionsChanged Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..72f208177a653e5a72773cec26899e06b8c787f2
--- /dev/null
+++ b/components/ntp_snippets/content_suggestions_service.h
@@ -0,0 +1,139 @@
+// 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 <set>
+#include <string>
+#include <vector>
+
+#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;
+}
tschumann 2016/06/28 11:47:17 necessary?
tschumann 2016/06/28 12:32:40 Done.
+
+namespace ntp_snippets {
+
+class ContentSuggestionsServiceObserver;
+
+// Retrieves suggestions from a number of ContentSuggestionsProviders and serves
+// them to the NTP.
+class ContentSuggestionsService : public KeyedService,
+ public ContentSuggestionsProvider::Delegate {
+ public:
+ using ImageFetchedCallback =
+ base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>;
+
+ ContentSuggestionsService(bool enabled);
tschumann 2016/06/28 12:32:40 nit: usually enums are better to read here. Compar
Philipp Keck 2016/06/28 14:18:57 True, but wouldn't that question pretty much every
tschumann 2016/06/30 10:20:06 It essentially applies to every boolean parameter
Philipp Keck 2016/06/30 10:41:53 Done. Unlike your code example above, I used an ac
+ ~ContentSuggestionsService() override;
+
+ // Inherited from KeyedService.
+ void Shutdown() override;
+
+ // Available suggestions
+ const std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>>&
+ suggestions() const {
+ return suggestions_;
+ }
+ const std::vector<ContentSuggestion>& suggestions(
+ ContentSuggestionCategory category) {
+ return suggestions_[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(const ContentSuggestionsProviderType provider_type,
+ const std::string& suggestion_id,
+ const ImageFetchedCallback& callback);
+
+ // 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 ClearSuggestions();
+
+ // 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(const ContentSuggestionsProviderType provider_type,
+ const std::string& suggestion_id);
+
+ // Observer accessors (outgoing observers, from this service towards UI)
+ void AddObserver(ContentSuggestionsServiceObserver* observer);
+ void RemoveObserver(ContentSuggestionsServiceObserver* observer);
+
+ // Implementation of ContentSuggestionsProvider::Delegate (incoming data)
+ void OnSuggestionsChanged(
+ const ContentSuggestionsProvider& source,
+ ContentSuggestionCategory changed_category,
+ std::vector<ContentSuggestion> suggestions) override;
+ void OnProviderShutdown(const ContentSuggestionsProvider& source) override;
tschumann 2016/06/28 12:32:40 nit: please insert an empty line between methods.
Philipp Keck 2016/06/28 14:18:57 Ok. What are the criteria for doing that? Because
Marc Treib 2016/06/28 15:04:55 AFAIK there aren't any hard rules. Some people pre
tschumann 2016/06/30 10:20:06 Good point! I agree that in the AddObserver/Remove
Philipp Keck 2016/06/30 10:41:53 This is already resolved, see https://codereview.
+
+ // Registers a new ContentSuggestionsProvider. The service will call
+ // ContentSuggestionsProvider::setDelegate with a delegate object, through
+ // which the registered provider should report its new suggestions. This
+ // method will fail when 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:
+ // When |enabled_| is true the service will retrieve suggestions from the
+ // providers.
+ bool enabled_;
+
+ // All current suggestions.
+ std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>>
+ suggestions_;
+
+ std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*>
+ providers_;
+
+ // The observers.
+ base::ObserverList<ContentSuggestionsServiceObserver> observers_;
+
+ // Helper method to remove all suggestions of the given |provider_type| from
+ // the given |list|. Returns true if anything changed.
+ bool RemoveSuggestionsOfProvider(
tschumann 2016/06/28 12:32:40 shouldn't functions go before members? (declaratio
Philipp Keck 2016/06/28 14:18:57 Acknowledged, this member was removed anyway.
+ std::vector<ContentSuggestion>* list,
+ ContentSuggestionsProviderType provider_type);
+
+ 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;
+ // Sent when the service is shutting down.
tschumann 2016/06/28 12:32:40 please insert empty line.
Philipp Keck 2016/06/28 14:18:57 Done.
+ virtual void ContentSuggestionsServiceShutdown() = 0;
+
+ protected:
+ virtual ~ContentSuggestionsServiceObserver() {}
+};
+
+} // namespace ntp_snippets
+
+#endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_

Powered by Google App Engine
This is Rietveld 408576698