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

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

Powered by Google App Engine
This is Rietveld 408576698