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

Side by Side 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: comments Created 4 years, 5 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
6 #define COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
7
8 #include <stddef.h>
9
10 #include <map>
11 #include <string>
12 #include <vector>
13
14 #include "base/callback_forward.h"
15 #include "base/observer_list.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "components/ntp_snippets/content_suggestions_provider.h"
18 #include "components/ntp_snippets/content_suggestions_provider_type.h"
19
20 namespace gfx {
21 class Image;
22 }
23
24 namespace ntp_snippets {
25
26 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves
27 // them grouped into categories.
28 // NOTE: This class is not yet in use, please use NTPSnippetService for now
29 // (see ntp_snippet_service.h).
Marc Treib 2016/07/01 09:15:33 nit: NTPSnippetsService and ntp_snippets_service.h
Philipp Keck 2016/07/01 13:00:04 Done.
30 class ContentSuggestionsService : public KeyedService,
31 public ContentSuggestionsProvider::Observer {
32 public:
33 using ImageFetchedCallback =
34 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>;
35
36 class Observer {
37 public:
38 // Fired every time the service loads a new set of data. The new data is
39 // then available through the getters of the service.
40 virtual void OnSuggestionsChanged() = 0;
41
42 // Sent when the service is shutting down. After the service has shut down,
43 // the getters and other methods should not be used anymore.
tschumann 2016/07/01 09:37:11 getters of the service? Is it safe to say, that th
Marc Treib 2016/07/01 09:51:52 Probably not; these are a fixed part of the KeyedS
Philipp Keck 2016/07/01 13:00:04 We probably should not require providers to be Key
44 // TODO(pke) Introduce proper, explicit state of the service and make calls
45 // to methods after shutdown fail.
46 virtual void ContentSuggestionsServiceShutdown() = 0;
47
48 protected:
49 virtual ~Observer() {}
50 };
51
52 enum Enabled : bool {
Marc Treib 2016/07/01 09:15:33 I'd call the enum itself State or something like t
53 Enable = true,
tschumann 2016/07/01 09:37:11 shouldn't these follow the constant naming convent
Marc Treib 2016/07/01 09:51:52 In Chrome, MACRO_STYLE is officially preferred (ht
Philipp Keck 2016/07/01 13:00:04 Done.
54 Disable = false,
55 };
56
57 ContentSuggestionsService(Enabled enabled);
58 ~ContentSuggestionsService() override;
59
60 // Inherited from KeyedService.
61 void Shutdown() override;
62
63 // Getters for available categories and suggestions in these categories
64 const std::vector<ContentSuggestionCategory>& GetCategories() const {
65 return categories_;
66 }
67 const std::vector<ContentSuggestion>& GetSuggestionsForCategory(
68 ContentSuggestionCategory category) const;
69
70 // Fetches the image for the suggestion with the given |suggestion_id| and
71 // runs the |callback|. If that suggestion doesn't exist or the fetch fails,
72 // the callback gets an em// NOTE: This class is not yet in use, please use
73 // NTPSnippet for now
74 // (see ntp_snippet.h).pty image.
Marc Treib 2016/07/01 09:15:33 Copy&paste fail?
Philipp Keck 2016/07/01 13:00:04 Done. I need to turn off middle-click paste..
75 void FetchSuggestionImage(const std::string& suggestion_id,
76 const ImageFetchedCallback& callback);
77
78 // Discards the suggestion with the given |suggestion_id|, if it exists.
79 // This won't necessarily trigger an update through the observers.
80 void DiscardSuggestion(const std::string& suggestion_id);
81
82 // Observer accessors.
83 void AddObserver(Observer* observer);
84 void RemoveObserver(Observer* observer);
85
86 // Registers a new ContentSuggestionsProvider. This method will fail if a
87 // provider of the same type is already registered.
88 void RegisterProvider(ContentSuggestionsProvider* provider);
89
90 // For use by the snippets-internals page only.
91 const std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*>&
92 GetProvidersForDebugging() const {
93 return providers_;
94 }
95
96 // Only for debugging use through the internals page.
97 // Removes all suggestions from all caches or internal stores in all
98 // providers. It does, however, not remove any suggestions from the provider's
99 // sources, so if their configuration hasn't changed, they should return the
100 // same results when they fetch the next time. In particular, calling this
101 // method will not mark any suggestions as discarded.
102 void ClearCachedSuggestionsForDebugging();
103
104 // Only for debugging use through the internals page. Some providers
105 // internally store a list of discarded suggestions to prevent them from
106 // reappearing. This function clears all such lists in all providers, making
107 // discarded suggestions reappear (only for certain providers).
108 void ClearDiscardedSuggestionsForDebugging();
109
110 private:
111 // Implementation of ContentSuggestionsProvider::Observer.
112 void OnSuggestionsChanged(
113 ContentSuggestionsProviderType provider_type,
114 ContentSuggestionCategory changed_category,
115 std::vector<ContentSuggestion> suggestions) override;
116
117 void OnProviderShutdown(
118 ContentSuggestionsProviderType provider_type) override;
119
120 // When |enabled_| is true the service will retrieve suggestions from the
121 // providers.
122 Enabled enabled_;
123
124 // All current suggestion categories, in order.
125 std::vector<ContentSuggestionCategory> categories_;
126 // All current suggestions grouped by category.
127 std::map<ContentSuggestionCategory, std::vector<ContentSuggestion>>
128 suggestions_by_category_;
129
130 std::map<ContentSuggestionsProviderType, ContentSuggestionsProvider*>
131 providers_;
132
133 base::ObserverList<Observer> observers_;
134
135 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService);
136 };
137
138 } // namespace ntp_snippets
139
140 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698