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

Side by Side Diff: components/ntp_snippets/content_suggestions_service.h

Issue 2205233002: Combine all suggestions factories into ContentSuggestionsServiceFactory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Bernhard's comments Created 4 years, 4 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
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 <string> 10 #include <string>
10 #include <vector> 11 #include <vector>
11 12
12 #include "base/callback_forward.h" 13 #include "base/callback_forward.h"
13 #include "base/observer_list.h" 14 #include "base/observer_list.h"
14 #include "components/keyed_service/core/keyed_service.h" 15 #include "components/keyed_service/core/keyed_service.h"
15 #include "components/ntp_snippets/category_factory.h" 16 #include "components/ntp_snippets/category_factory.h"
16 #include "components/ntp_snippets/category_status.h" 17 #include "components/ntp_snippets/category_status.h"
17 #include "components/ntp_snippets/content_suggestions_provider.h" 18 #include "components/ntp_snippets/content_suggestions_provider.h"
18 19
19 namespace gfx { 20 namespace gfx {
20 class Image; 21 class Image;
21 } 22 }
22 23
23 namespace ntp_snippets { 24 namespace ntp_snippets {
24 25
26 class NTPSnippetsService;
27
25 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves 28 // Retrieves suggestions from a number of ContentSuggestionsProviders and serves
26 // them grouped into categories. There can be at most one provider per category. 29 // them grouped into categories. There can be at most one provider per category.
27 class ContentSuggestionsService : public KeyedService, 30 class ContentSuggestionsService : public KeyedService,
28 public ContentSuggestionsProvider::Observer { 31 public ContentSuggestionsProvider::Observer {
29 public: 32 public:
30 using ImageFetchedCallback = 33 using ImageFetchedCallback =
31 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>; 34 base::Callback<void(const std::string& suggestion_id, const gfx::Image&)>;
32 35
33 class Observer { 36 class Observer {
34 public: 37 public:
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 // This will not trigger an update through the observers. 94 // This will not trigger an update through the observers.
92 void DismissSuggestion(const std::string& suggestion_id); 95 void DismissSuggestion(const std::string& suggestion_id);
93 96
94 // Observer accessors. 97 // Observer accessors.
95 void AddObserver(Observer* observer); 98 void AddObserver(Observer* observer);
96 void RemoveObserver(Observer* observer); 99 void RemoveObserver(Observer* observer);
97 100
98 // Registers a new ContentSuggestionsProvider. It must be ensured that at most 101 // Registers a new ContentSuggestionsProvider. It must be ensured that at most
99 // one provider is registered for every category and that this method is 102 // one provider is registered for every category and that this method is
100 // called only once per provider. 103 // called only once per provider.
101 void RegisterProvider(ContentSuggestionsProvider* provider); 104 void RegisterProvider(std::unique_ptr<ContentSuggestionsProvider> provider);
102 105
103 // Only for debugging use through the internals page. 106 // Only for debugging use through the internals page.
104 // Removes all suggestions from all caches or internal stores in all 107 // Removes all suggestions from all caches or internal stores in all
105 // providers. It does, however, not remove any suggestions from the provider's 108 // providers. It does, however, not remove any suggestions from the provider's
106 // sources, so if their configuration hasn't changed, they should return the 109 // sources, so if their configuration hasn't changed, they should return the
107 // same results when they fetch the next time. In particular, calling this 110 // same results when they fetch the next time. In particular, calling this
108 // method will not mark any suggestions as dismissed. 111 // method will not mark any suggestions as dismissed.
109 void ClearCachedSuggestionsForDebugging(); 112 void ClearCachedSuggestionsForDebugging();
110 113
111 // Only for debugging use through the internals page. Some providers 114 // Only for debugging use through the internals page. Some providers
112 // internally store a list of dismissed suggestions to prevent them from 115 // internally store a list of dismissed suggestions to prevent them from
113 // reappearing. This function clears all such lists in all providers, making 116 // reappearing. This function clears all such lists in all providers, making
114 // dismissed suggestions reappear (only for certain providers). 117 // dismissed suggestions reappear (only for certain providers).
115 void ClearDismissedSuggestionsForDebugging(); 118 void ClearDismissedSuggestionsForDebugging();
116 119
117 CategoryFactory* category_factory() { return &category_factory_; } 120 CategoryFactory* category_factory() { return &category_factory_; }
118 121
122 // The reference to the NTPSnippetsService provider should only be set by the
123 // factory and only be used for scheduling, periodic fetching and debugging.
124 NTPSnippetsService* ntp_snippets_service() { return ntp_snippets_service_; }
125 void set_ntp_snippets_service(NTPSnippetsService* ntp_snippets_service) {
126 ntp_snippets_service_ = ntp_snippets_service;
127 }
128
119 private: 129 private:
120 friend class ContentSuggestionsServiceTest; 130 friend class ContentSuggestionsServiceTest;
121 131
122 // This is just an arbitrary ordering by ID, used by the maps in this class, 132 // This is just an arbitrary ordering by ID, used by the maps in this class,
123 // because the ordering needs to be constant for maps. 133 // because the ordering needs to be constant for maps.
124 struct CompareCategoriesByID { 134 struct CompareCategoriesByID {
125 bool operator()(const Category& left, const Category& right) const; 135 bool operator()(const Category& left, const Category& right) const;
126 }; 136 };
127 137
128 // Implementation of ContentSuggestionsProvider::Observer. 138 // Implementation of ContentSuggestionsProvider::Observer.
129 void OnNewSuggestions(ContentSuggestionsProvider* provider, 139 void OnNewSuggestions(ContentSuggestionsProvider* provider,
130 Category category, 140 Category category,
131 std::vector<ContentSuggestion> suggestions) override; 141 std::vector<ContentSuggestion> suggestions) override;
132 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, 142 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider,
133 Category category, 143 Category category,
134 CategoryStatus new_status) override; 144 CategoryStatus new_status) override;
135 void OnProviderShutdown(ContentSuggestionsProvider* provider) override;
136 145
137 // Registers the given |provider| for the given |category|, unless it is 146 // Registers the given |provider| for the given |category|, unless it is
138 // already registered. Returns true if the category was newly registered or 147 // already registered. Returns true if the category was newly registered or
139 // false if it was present before. 148 // false if it was present before.
140 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider, 149 bool RegisterCategoryIfRequired(ContentSuggestionsProvider* provider,
141 Category category); 150 Category category);
142 151
143 // Fires the OnCategoryStatusChanged event for the given |category|. 152 // Fires the OnCategoryStatusChanged event for the given |category|.
144 void NotifyCategoryStatusChanged(Category category); 153 void NotifyCategoryStatusChanged(Category category);
145 154
146 // Whether the content suggestions feature is enabled. 155 // Whether the content suggestions feature is enabled.
147 State state_; 156 State state_;
148 157
149 // Provides new and existing categories and an order for them. 158 // Provides new and existing categories and an order for them.
150 CategoryFactory category_factory_; 159 CategoryFactory category_factory_;
151 160
152 // All registered providers. A provider may be contained multiple times, if it 161 // All registered providers, owned by the service.
153 // provides multiple categories. The keys of this map are exactly the entries 162 std::vector<std::unique_ptr<ContentSuggestionsProvider>> providers_;
154 // of |categories_|. 163
164 // All registered categories and their providers. A provider may be contained
165 // multiple times, if it provides multiple categories. The keys of this map
166 // are exactly the entries of |categories_| and the values are a subset of
167 // |providers_|.
155 std::map<Category, ContentSuggestionsProvider*, CompareCategoriesByID> 168 std::map<Category, ContentSuggestionsProvider*, CompareCategoriesByID>
156 providers_by_category_; 169 providers_by_category_;
157 170
158 // All current suggestion categories, in an order determined by the 171 // All current suggestion categories, in an order determined by the
159 // |category_factory_|. This vector contains exactly the same categories as 172 // |category_factory_|. This vector contains exactly the same categories as
160 // |providers_|. 173 // |providers_by_category_|.
161 std::vector<Category> categories_; 174 std::vector<Category> categories_;
162 175
163 // All current suggestions grouped by category. This contains an entry for 176 // All current suggestions grouped by category. This contains an entry for
164 // every category in |categories_| whose status is an available status. It may 177 // every category in |categories_| whose status is an available status. It may
165 // contain an empty vector if the category is available but empty (or still 178 // contain an empty vector if the category is available but empty (or still
166 // loading). 179 // loading).
167 std::map<Category, std::vector<ContentSuggestion>, CompareCategoriesByID> 180 std::map<Category, std::vector<ContentSuggestion>, CompareCategoriesByID>
168 suggestions_by_category_; 181 suggestions_by_category_;
169 182
170 // Map used to determine the category of a suggestion (of which only the ID 183 // Map used to determine the category of a suggestion (of which only the ID
171 // is available). This also determines the provider that delivered the 184 // is available). This also determines the provider that delivered the
172 // suggestion. 185 // suggestion.
173 std::map<std::string, Category> id_category_map_; 186 std::map<std::string, Category> id_category_map_;
174 187
175 base::ObserverList<Observer> observers_; 188 base::ObserverList<Observer> observers_;
176 189
177 const std::vector<ContentSuggestion> no_suggestions_; 190 const std::vector<ContentSuggestion> no_suggestions_;
178 191
192 // Keep a direct reference to this special provider to redirect scheduling,
193 // background fetching and debugging calls to it. If the NTPSnippetsService is
194 // loaded, it is also present in |providers_|, otherwise this is a nullptr.
195 NTPSnippetsService* ntp_snippets_service_;
196
179 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService); 197 DISALLOW_COPY_AND_ASSIGN(ContentSuggestionsService);
180 }; 198 };
181 199
182 } // namespace ntp_snippets 200 } // namespace ntp_snippets
183 201
184 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_ 202 #endif // COMPONENTS_NTP_SNIPPETS_CONTENT_SUGGESTIONS_SERVICE_H_
OLDNEW
« no previous file with comments | « components/ntp_snippets/content_suggestions_provider.cc ('k') | components/ntp_snippets/content_suggestions_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698