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

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

Issue 2421463002: FetchMore functionality backend (Closed)
Patch Set: ID set reference, Optional callback, ... (2466863003 comments). 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
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.
211 void OnFetchFinished( 215 void OnFetchFinished(
216 bool fetched_more,
217 base::Optional<FetchingCallback> fetching_callback,
212 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); 218 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories);
213 219
214 // Moves all snippets from |to_archive| into the archive of the |category|. 220 // 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 221 // It also deletes the snippets from the DB and keeps the archive reasonably
216 // short. 222 // short.
217 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive); 223 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive);
218 224
219 // Replace old snippets in |category| by newly available snippets. 225 // Adds newly available suggestions in |category| to the available ones.
220 void ReplaceSnippets(Category category, NTPSnippet::PtrVector new_snippets); 226 // If |replace_snippets| is set, archives existing suggestions.
227 void IncludeSnippets(const Category& category,
228 NTPSnippet::PtrVector new_snippets,
229 bool replace_snippets);
221 230
222 // Removes expired dismissed snippets from the service and the database. 231 // Removes expired dismissed snippets from the service and the database.
223 void ClearExpiredDismissedSnippets(); 232 void ClearExpiredDismissedSnippets();
224 233
225 // Removes images from the DB that are not referenced from any known snippet. 234 // 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 235 // 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. 236 // keep it small but not too often as it still iterates over the file system.
228 void ClearOrphanedImages(); 237 void ClearOrphanedImages();
229 238
230 // Clears all stored snippets and updates the observer. 239 // Clears all stored snippets and updates the observer.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 void EnterStateDisabled(); 277 void EnterStateDisabled();
269 278
270 // Disables the service permanently because an unrecoverable error occurred. 279 // Disables the service permanently because an unrecoverable error occurred.
271 // Do not call directly, use |EnterState| instead. 280 // Do not call directly, use |EnterState| instead.
272 void EnterStateError(); 281 void EnterStateError();
273 282
274 // Converts the cached snippets in the given |category| to content suggestions 283 // Converts the cached snippets in the given |category| to content suggestions
275 // and notifies the observer. 284 // and notifies the observer.
276 void NotifyNewSuggestions(Category category); 285 void NotifyNewSuggestions(Category category);
277 286
287 // Converts the cached snippets in the given |category| to content suggestions
288 // and passes them to the |callback|.
289 void NotifyMoreSuggestions(Category category,
290 base::Optional<FetchingCallback> callback);
291
278 // Updates the internal status for |category| to |category_status_| and 292 // Updates the internal status for |category| to |category_status_| and
279 // notifies the content suggestions observer if it changed. 293 // notifies the content suggestions observer if it changed.
280 void UpdateCategoryStatus(Category category, CategoryStatus status); 294 void UpdateCategoryStatus(Category category, CategoryStatus status);
281 // Calls UpdateCategoryStatus() for all provided categories. 295 // Calls UpdateCategoryStatus() for all provided categories.
282 void UpdateAllCategoryStatus(CategoryStatus status); 296 void UpdateAllCategoryStatus(CategoryStatus status);
283 297
284 void RestoreCategoriesFromPrefs(); 298 void RestoreCategoriesFromPrefs();
285 void StoreCategoriesToPrefs(); 299 void StoreCategoriesToPrefs();
286 300
301 // Implementation for |FetchSnippets| and |Fetch| that calls the snippet
302 // fetcher and replaces or adds the fetched snippets depending on the
303 // |fetch_more| parameter.
304 void FetchSnippetsFromHostsImpl(
305 const std::set<std::string>& hosts,
306 bool interactive_request,
307 bool fetch_more,
308 const std::set<std::string>& known_suggestion_ids,
309 base::Optional<Category> exclusive_category,
310 base::Optional<FetchingCallback> fetched_more_callback);
311
312 // Returns a set of snippet IDs that should not be fetched. These IDs always
313 // include dismissed snippets. If |fetch_more| is set, they include all known
314 // snippet IDs.
315 std::set<std::string> CollectIdsToExclude(
316 bool fetch_more,
317 const std::set<std::string>& additional_ids) const;
318
319 void MarkEmptyCategoriesAsLoading();
320
287 State state_; 321 State state_;
288 322
289 PrefService* pref_service_; 323 PrefService* pref_service_;
290 324
291 const Category articles_category_; 325 const Category articles_category_;
292 326
293 // TODO(sfiera): Reduce duplication of CategoryContent with CategoryInfo. 327 // TODO(sfiera): Reduce duplication of CategoryContent with CategoryInfo.
294 struct CategoryContent { 328 struct CategoryContent {
295 CategoryStatus status = CategoryStatus::INITIALIZING; 329 CategoryStatus status = CategoryStatus::INITIALIZING;
296 330
(...skipping 20 matching lines...) Expand all
317 351
318 // Returns a non-dismissed snippet with the given |id_within_category|, or 352 // Returns a non-dismissed snippet with the given |id_within_category|, or
319 // null if none exist. 353 // null if none exist.
320 const NTPSnippet* FindSnippet(const std::string& id_within_category) const; 354 const NTPSnippet* FindSnippet(const std::string& id_within_category) const;
321 355
322 CategoryContent(); 356 CategoryContent();
323 CategoryContent(CategoryContent&&); 357 CategoryContent(CategoryContent&&);
324 ~CategoryContent(); 358 ~CategoryContent();
325 CategoryContent& operator=(CategoryContent&&); 359 CategoryContent& operator=(CategoryContent&&);
326 }; 360 };
327 std::map<Category, CategoryContent, Category::CompareByID> categories_; 361 using CategoryContentMap =
362 std::map<Category, CategoryContent, Category::CompareByID>;
363 CategoryContentMap categories_;
328 364
329 // The ISO 639-1 code of the language used by the application. 365 // The ISO 639-1 code of the language used by the application.
330 const std::string application_language_code_; 366 const std::string application_language_code_;
331 367
332 // Classifier that tells us how active the user is. Not owned. 368 // Classifier that tells us how active the user is. Not owned.
333 const UserClassifier* user_classifier_; 369 const UserClassifier* user_classifier_;
334 370
335 // Scheduler for fetching snippets. Not owned. 371 // Scheduler for fetching snippets. Not owned.
336 NTPSnippetsScheduler* scheduler_; 372 NTPSnippetsScheduler* scheduler_;
337 373
(...skipping 21 matching lines...) Expand all
359 395
360 // Request throttler for limiting requests to thumbnail images. 396 // Request throttler for limiting requests to thumbnail images.
361 RequestThrottler thumbnail_requests_throttler_; 397 RequestThrottler thumbnail_requests_throttler_;
362 398
363 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); 399 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService);
364 }; 400 };
365 401
366 } // namespace ntp_snippets 402 } // namespace ntp_snippets
367 403
368 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_ 404 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698