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

Side by Side Diff: components/ntp_snippets/remote/ntp_snippets_service.h

Issue 2473483006: Adds a Fetch() method to the ContentSuggestionService which asks any provider to provide more conte… (Closed)
Patch Set: comments by Markus. Created 4 years, 1 month 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_REMOTE_NTP_SNIPPETS_SERVICE_H_ 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_
6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_ 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_
7 7
8 #include <cstddef> 8 #include <cstddef>
9 #include <map> 9 #include <map>
10 #include <memory> 10 #include <memory>
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 // tasks will be re-scheduled even if they already exist and have the correct 110 // tasks will be re-scheduled even if they already exist and have the correct
111 // periods. 111 // periods.
112 void RescheduleFetching(bool force); 112 void RescheduleFetching(bool force);
113 113
114 // ContentSuggestionsProvider implementation 114 // ContentSuggestionsProvider implementation
115 CategoryStatus GetCategoryStatus(Category category) override; 115 CategoryStatus GetCategoryStatus(Category category) override;
116 CategoryInfo GetCategoryInfo(Category category) override; 116 CategoryInfo GetCategoryInfo(Category category) override;
117 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; 117 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override;
118 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, 118 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id,
119 const ImageFetchedCallback& callback) override; 119 const ImageFetchedCallback& callback) override;
120 void Fetch(const Category& category,
121 const std::set<std::string>& known_suggestion_ids,
122 FetchingCallback callback) override;
120 void ClearHistory( 123 void ClearHistory(
121 base::Time begin, 124 base::Time begin,
122 base::Time end, 125 base::Time end,
123 const base::Callback<bool(const GURL& url)>& filter) override; 126 const base::Callback<bool(const GURL& url)>& filter) override;
124 void ClearCachedSuggestions(Category category) override; 127 void ClearCachedSuggestions(Category category) override;
125 void GetDismissedSuggestionsForDebugging( 128 void GetDismissedSuggestionsForDebugging(
126 Category category, 129 Category category,
127 const DismissedSuggestionsCallback& callback) override; 130 const DismissedSuggestionsCallback& callback) override;
128 void ClearDismissedSuggestionsForDebugging(Category category) override; 131 void ClearDismissedSuggestionsForDebugging(Category category) override;
129 132
(...skipping 13 matching lines...) Expand all
143 } 146 }
144 147
145 // Dismissed snippets, only for unit tests. 148 // Dismissed snippets, only for unit tests.
146 const NTPSnippet::PtrVector& GetDismissedSnippetsForTesting( 149 const NTPSnippet::PtrVector& GetDismissedSnippetsForTesting(
147 Category category) const { 150 Category category) const {
148 return categories_.find(category)->second.dismissed; 151 return categories_.find(category)->second.dismissed;
149 } 152 }
150 153
151 private: 154 private:
152 friend class NTPSnippetsServiceTest; 155 friend class NTPSnippetsServiceTest;
156
153 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, 157 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest,
154 RemoveExpiredDismissedContent); 158 RemoveExpiredDismissedContent);
155 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, RescheduleOnStateChange); 159 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, RescheduleOnStateChange);
156 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); 160 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges);
157 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, 161 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest,
158 SuggestionsFetchedOnSignInAndSignOut); 162 SuggestionsFetchedOnSignInAndSignOut);
159 163
160 // Possible state transitions: 164 // Possible state transitions:
161 // NOT_INITED --------+ 165 // NOT_INITED --------+
162 // / \ | 166 // / \ |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 205
202 // image_fetcher::ImageFetcherDelegate implementation. 206 // image_fetcher::ImageFetcherDelegate implementation.
203 void OnImageDataFetched(const std::string& id_within_category, 207 void OnImageDataFetched(const std::string& id_within_category,
204 const std::string& image_data) override; 208 const std::string& image_data) override;
205 209
206 // Callbacks for the NTPSnippetsDatabase. 210 // Callbacks for the NTPSnippetsDatabase.
207 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); 211 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets);
208 void OnDatabaseError(); 212 void OnDatabaseError();
209 213
210 // Callback for the NTPSnippetsFetcher. 214 // Callback for the NTPSnippetsFetcher.
215 // |fetching_callback| is ignored if |fetched_more| is false. Otherwise, it's
216 // expected to be non null.
211 void OnFetchFinished( 217 void OnFetchFinished(
218 bool fetched_more,
219 base::Optional<FetchingCallback> fetching_callback,
212 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); 220 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories);
213 221
214 // Moves all snippets from |to_archive| into the archive of the |category|. 222 // Moves all snippets from |to_archive| into the archive of the |category|.
215 // It also deletes the snippets from the DB and keeps the archive reasonably 223 // It also deletes the snippets from the DB and keeps the archive reasonably
216 // short. 224 // short.
217 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive); 225 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive);
218 226
219 // Replace old snippets in |category| by newly available snippets. 227 // Adds newly available suggestions in |category| to the available ones.
220 void ReplaceSnippets(Category category, NTPSnippet::PtrVector new_snippets); 228 // If |replace_snippets| is set, archives existing suggestions.
229 void IncludeSnippets(const Category& category,
230 NTPSnippet::PtrVector new_snippets,
231 bool replace_snippets);
221 232
222 // Removes expired dismissed snippets from the service and the database. 233 // Removes expired dismissed snippets from the service and the database.
223 void ClearExpiredDismissedSnippets(); 234 void ClearExpiredDismissedSnippets();
224 235
225 // Removes images from the DB that are not referenced from any known snippet. 236 // Removes images from the DB that are not referenced from any known snippet.
226 // Needs to iterate the whole snippet database -- so do it often enough to 237 // Needs to iterate the whole snippet database -- so do it often enough to
227 // keep it small but not too often as it still iterates over the file system. 238 // keep it small but not too often as it still iterates over the file system.
228 void ClearOrphanedImages(); 239 void ClearOrphanedImages();
229 240
230 // Clears all stored snippets and updates the observer. 241 // Clears all stored snippets and updates the observer.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 void EnterStateDisabled(); 279 void EnterStateDisabled();
269 280
270 // Disables the service permanently because an unrecoverable error occurred. 281 // Disables the service permanently because an unrecoverable error occurred.
271 // Do not call directly, use |EnterState| instead. 282 // Do not call directly, use |EnterState| instead.
272 void EnterStateError(); 283 void EnterStateError();
273 284
274 // Converts the cached snippets in the given |category| to content suggestions 285 // Converts the cached snippets in the given |category| to content suggestions
275 // and notifies the observer. 286 // and notifies the observer.
276 void NotifyNewSuggestions(Category category); 287 void NotifyNewSuggestions(Category category);
277 288
289 // Converts the cached snippets in the given |category| to content suggestions
290 // and passes them to the |callback|.
291 // TODO(tschumann): Make |callback| required.
292 void NotifyMoreSuggestions(Category category,
293 base::Optional<FetchingCallback> callback);
294
278 // Updates the internal status for |category| to |category_status_| and 295 // Updates the internal status for |category| to |category_status_| and
279 // notifies the content suggestions observer if it changed. 296 // notifies the content suggestions observer if it changed.
280 void UpdateCategoryStatus(Category category, CategoryStatus status); 297 void UpdateCategoryStatus(Category category, CategoryStatus status);
281 // Calls UpdateCategoryStatus() for all provided categories. 298 // Calls UpdateCategoryStatus() for all provided categories.
282 void UpdateAllCategoryStatus(CategoryStatus status); 299 void UpdateAllCategoryStatus(CategoryStatus status);
283 300
284 void RestoreCategoriesFromPrefs(); 301 void RestoreCategoriesFromPrefs();
285 void StoreCategoriesToPrefs(); 302 void StoreCategoriesToPrefs();
286 303
304 // Implementation for |FetchSnippets| and |Fetch| that calls the snippet
305 // fetcher and replaces or adds the fetched snippets depending on the
306 // |fetch_more| parameter.
307 void FetchSnippetsFromHostsImpl(
308 const std::set<std::string>& hosts,
309 bool interactive_request,
310 bool fetch_more,
311 const std::set<std::string>& known_suggestion_ids,
312 base::Optional<Category> exclusive_category,
313 base::Optional<FetchingCallback> fetched_more_callback);
314
315 // Returns a set of snippet IDs that should not be fetched. These IDs always
316 // include dismissed snippets. If |fetch_more| is set, they include all known
317 // snippet IDs.
318 std::set<std::string> CollectIdsToExclude(
319 bool fetch_more,
320 const std::set<std::string>& additional_ids) const;
321
322 void MarkEmptyCategoriesAsLoading();
323
287 State state_; 324 State state_;
288 325
289 PrefService* pref_service_; 326 PrefService* pref_service_;
290 327
291 const Category articles_category_; 328 const Category articles_category_;
292 329
293 // TODO(sfiera): Reduce duplication of CategoryContent with CategoryInfo. 330 // TODO(sfiera): Reduce duplication of CategoryContent with CategoryInfo.
294 struct CategoryContent { 331 struct CategoryContent {
295 CategoryStatus status = CategoryStatus::INITIALIZING; 332 CategoryStatus status = CategoryStatus::INITIALIZING;
296 333
(...skipping 20 matching lines...) Expand all
317 354
318 // Returns a non-dismissed snippet with the given |id_within_category|, or 355 // Returns a non-dismissed snippet with the given |id_within_category|, or
319 // null if none exist. 356 // null if none exist.
320 const NTPSnippet* FindSnippet(const std::string& id_within_category) const; 357 const NTPSnippet* FindSnippet(const std::string& id_within_category) const;
321 358
322 CategoryContent(); 359 CategoryContent();
323 CategoryContent(CategoryContent&&); 360 CategoryContent(CategoryContent&&);
324 ~CategoryContent(); 361 ~CategoryContent();
325 CategoryContent& operator=(CategoryContent&&); 362 CategoryContent& operator=(CategoryContent&&);
326 }; 363 };
327 std::map<Category, CategoryContent, Category::CompareByID> categories_; 364 using CategoryContentMap =
365 std::map<Category, CategoryContent, Category::CompareByID>;
366 CategoryContentMap categories_;
Marc Treib 2016/11/07 14:28:23 Why this change? CategoryContentMap isn't used any
tschumann 2016/11/08 16:57:35 Done.
328 367
329 // The ISO 639-1 code of the language used by the application. 368 // The ISO 639-1 code of the language used by the application.
330 const std::string application_language_code_; 369 const std::string application_language_code_;
331 370
332 // Classifier that tells us how active the user is. Not owned. 371 // Classifier that tells us how active the user is. Not owned.
333 const UserClassifier* user_classifier_; 372 const UserClassifier* user_classifier_;
334 373
335 // Scheduler for fetching snippets. Not owned. 374 // Scheduler for fetching snippets. Not owned.
336 NTPSnippetsScheduler* scheduler_; 375 NTPSnippetsScheduler* scheduler_;
337 376
(...skipping 21 matching lines...) Expand all
359 398
360 // Request throttler for limiting requests to thumbnail images. 399 // Request throttler for limiting requests to thumbnail images.
361 RequestThrottler thumbnail_requests_throttler_; 400 RequestThrottler thumbnail_requests_throttler_;
362 401
363 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); 402 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService);
364 }; 403 };
365 404
366 } // namespace ntp_snippets 405 } // namespace ntp_snippets
367 406
368 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_ 407 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698