OLD | NEW |
---|---|
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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 // tasks will be re-scheduled even if they already exist and have the correct | 109 // tasks will be re-scheduled even if they already exist and have the correct |
110 // periods. | 110 // periods. |
111 void RescheduleFetching(bool force); | 111 void RescheduleFetching(bool force); |
112 | 112 |
113 // ContentSuggestionsProvider implementation | 113 // ContentSuggestionsProvider implementation |
114 CategoryStatus GetCategoryStatus(Category category) override; | 114 CategoryStatus GetCategoryStatus(Category category) override; |
115 CategoryInfo GetCategoryInfo(Category category) override; | 115 CategoryInfo GetCategoryInfo(Category category) override; |
116 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; | 116 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; |
117 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, | 117 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, |
118 const ImageFetchedCallback& callback) override; | 118 const ImageFetchedCallback& callback) override; |
119 void FetchMore(const Category& category, | |
120 FetchedMoreCallback callback) override; | |
119 void ClearHistory( | 121 void ClearHistory( |
120 base::Time begin, | 122 base::Time begin, |
121 base::Time end, | 123 base::Time end, |
122 const base::Callback<bool(const GURL& url)>& filter) override; | 124 const base::Callback<bool(const GURL& url)>& filter) override; |
123 void ClearCachedSuggestions(Category category) override; | 125 void ClearCachedSuggestions(Category category) override; |
124 void GetDismissedSuggestionsForDebugging( | 126 void GetDismissedSuggestionsForDebugging( |
125 Category category, | 127 Category category, |
126 const DismissedSuggestionsCallback& callback) override; | 128 const DismissedSuggestionsCallback& callback) override; |
127 void ClearDismissedSuggestionsForDebugging(Category category) override; | 129 void ClearDismissedSuggestionsForDebugging(Category category) override; |
128 | 130 |
(...skipping 13 matching lines...) Expand all Loading... | |
142 } | 144 } |
143 | 145 |
144 // Dismissed snippets, only for unit tests. | 146 // Dismissed snippets, only for unit tests. |
145 const NTPSnippet::PtrVector& GetDismissedSnippetsForTesting( | 147 const NTPSnippet::PtrVector& GetDismissedSnippetsForTesting( |
146 Category category) const { | 148 Category category) const { |
147 return categories_.find(category)->second.dismissed; | 149 return categories_.find(category)->second.dismissed; |
148 } | 150 } |
149 | 151 |
150 private: | 152 private: |
151 friend class NTPSnippetsServiceTest; | 153 friend class NTPSnippetsServiceTest; |
154 | |
155 // Forward declaration of the Strategy used to process fetch requests. | |
156 class NTPSnippetsFetchingStrategy; | |
Marc Treib
2016/10/20 16:51:40
Doesn't exist anymore
Marc Treib
2016/10/28 14:49:49
Done.
| |
157 | |
152 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, | 158 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, |
153 RemoveExpiredDismissedContent); | 159 RemoveExpiredDismissedContent); |
154 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, RescheduleOnStateChange); | 160 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, RescheduleOnStateChange); |
155 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); | 161 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); |
156 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, | 162 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, |
157 SuggestionsFetchedOnSignInAndSignOut); | 163 SuggestionsFetchedOnSignInAndSignOut); |
158 | 164 |
159 // Possible state transitions: | 165 // Possible state transitions: |
160 // NOT_INITED --------+ | 166 // NOT_INITED --------+ |
161 // / \ | | 167 // / \ | |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 // image_fetcher::ImageFetcherDelegate implementation. | 207 // image_fetcher::ImageFetcherDelegate implementation. |
202 void OnImageDataFetched(const std::string& id_within_category, | 208 void OnImageDataFetched(const std::string& id_within_category, |
203 const std::string& image_data) override; | 209 const std::string& image_data) override; |
204 | 210 |
205 // Callbacks for the NTPSnippetsDatabase. | 211 // Callbacks for the NTPSnippetsDatabase. |
206 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 212 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
207 void OnDatabaseError(); | 213 void OnDatabaseError(); |
208 | 214 |
209 // Callback for the NTPSnippetsFetcher. | 215 // Callback for the NTPSnippetsFetcher. |
210 void OnFetchFinished( | 216 void OnFetchFinished( |
217 bool fetched_more, | |
218 FetchedMoreCallback fetched_more_callback, | |
Marc Treib
2016/10/20 16:51:40
Hm. Should we maybe have two different FetchFinish
fhorschig
2016/11/02 05:05:27
I would make that change as soon as we decided how
| |
211 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); | 219 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); |
212 | 220 |
213 // Moves all snippets from |to_archive| into the archive of the |category|. | 221 // Moves all snippets from |to_archive| into the archive of the |category|. |
214 // It also deletes the snippets from the DB and keeps the archive reasonably | 222 // It also deletes the snippets from the DB and keeps the archive reasonably |
215 // short. | 223 // short. |
216 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive); | 224 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive); |
217 | 225 |
218 // Replace old snippets in |category| by newly available snippets. | 226 // Add newly available snippets in |category| to the provided content. |
219 void ReplaceSnippets(Category category, NTPSnippet::PtrVector new_snippets); | 227 // If |replace_snippets| is set, archive old snippets. |
Marc Treib
2016/10/20 16:51:40
s/old/existing/ ?
Marc Treib
2016/10/28 14:49:49
Done.
| |
228 void IncludeSnippets(const Category& category, | |
229 NTPSnippet::PtrVector new_snippets, | |
230 bool replace_snippets); | |
231 | |
232 void SaveSnippets(const Category& category, | |
Marc Treib
2016/10/20 16:51:40
What's the difference between IncludeSnippets and
Marc Treib
2016/10/28 14:49:50
Done. (Merged SaveSnippets into IncludeSnippets)
| |
233 NTPSnippet::PtrVector new_snippets, | |
234 bool replace_snippets); | |
220 | 235 |
221 // Removes expired dismissed snippets from the service and the database. | 236 // Removes expired dismissed snippets from the service and the database. |
222 void ClearExpiredDismissedSnippets(); | 237 void ClearExpiredDismissedSnippets(); |
223 | 238 |
224 // Removes images from the DB that are not referenced from any known snippet. | 239 // Removes images from the DB that are not referenced from any known snippet. |
225 // Needs to iterate the whole snippet database -- so do it often enough to | 240 // Needs to iterate the whole snippet database -- so do it often enough to |
226 // keep it small but not too often as it still iterates over the file system. | 241 // keep it small but not too often as it still iterates over the file system. |
227 void ClearOrphanedImages(); | 242 void ClearOrphanedImages(); |
228 | 243 |
229 // Clears all stored snippets and updates the observer. | 244 // Clears all stored snippets and updates the observer. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 | 280 |
266 // Disables the service. Do not call directly, use |EnterState| instead. | 281 // Disables the service. Do not call directly, use |EnterState| instead. |
267 void EnterStateDisabled(); | 282 void EnterStateDisabled(); |
268 | 283 |
269 // Disables the service permanently because an unrecoverable error occurred. | 284 // Disables the service permanently because an unrecoverable error occurred. |
270 // Do not call directly, use |EnterState| instead. | 285 // Do not call directly, use |EnterState| instead. |
271 void EnterStateError(); | 286 void EnterStateError(); |
272 | 287 |
273 // Converts the cached snippets to article content suggestions and notifies | 288 // Converts the cached snippets to article content suggestions and notifies |
274 // the observers. | 289 // the observers. |
275 void NotifyNewSuggestions(); | 290 void NotifyNewSuggestions(bool fetched_more, |
Marc Treib
2016/10/20 16:51:40
Heads-up: This will conflict with https://chromium
Marc Treib
2016/10/28 14:49:50
Done.
| |
291 FetchedMoreCallback fetched_more_callback); | |
276 | 292 |
277 // Updates the internal status for |category| to |category_status_| and | 293 // Updates the internal status for |category| to |category_status_| and |
278 // notifies the content suggestions observer if it changed. | 294 // notifies the content suggestions observer if it changed. |
279 void UpdateCategoryStatus(Category category, CategoryStatus status); | 295 void UpdateCategoryStatus(Category category, CategoryStatus status); |
280 // Calls UpdateCategoryStatus() for all provided categories. | 296 // Calls UpdateCategoryStatus() for all provided categories. |
281 void UpdateAllCategoryStatus(CategoryStatus status); | 297 void UpdateAllCategoryStatus(CategoryStatus status); |
282 | 298 |
283 void RestoreCategoriesFromPrefs(); | 299 void RestoreCategoriesFromPrefs(); |
284 void StoreCategoriesToPrefs(); | 300 void StoreCategoriesToPrefs(); |
285 | 301 |
302 // Implementation for |FetchSnippets| and |FetchMore| that calls the snippet | |
303 // fetcher and replaces or adds the fetched snippets depending on the passed | |
304 // |strategy|. | |
305 void FetchSnippetsFromHostsImpl(const std::set<std::string>& hosts, | |
306 bool interactive_request, | |
307 bool fetch_more, | |
308 FetchedMoreCallback fetched_more_callback, | |
309 base::Optional<Category> exclusive_category); | |
310 | |
311 // Returns a set of snippet ids that are should not be fetched in subsequent | |
312 // requests. These IDs akways include dismissed snippets. | |
313 // For |fetch more| requests, all known snippets are returned. | |
Marc Treib
2016/10/20 16:51:40
IDs (capitalize)
"are should"
"akways"
fetch_more
Marc Treib
2016/10/28 14:49:50
Done.
| |
314 std::set<std::string> CollectIdsToExclude(bool fetch_more); | |
Marc Treib
2016/10/20 16:51:40
const
Marc Treib
2016/10/28 14:49:50
Done.
| |
315 | |
316 void MarkEmptyCategoriesAsLoading(); | |
317 | |
286 State state_; | 318 State state_; |
287 | 319 |
288 PrefService* pref_service_; | 320 PrefService* pref_service_; |
289 | 321 |
290 const Category articles_category_; | 322 const Category articles_category_; |
291 | 323 |
292 // TODO(sfiera): Reduce duplication of CategoryContent with CategoryInfo. | 324 // TODO(sfiera): Reduce duplication of CategoryContent with CategoryInfo. |
293 struct CategoryContent { | 325 struct CategoryContent { |
294 CategoryStatus status = CategoryStatus::INITIALIZING; | 326 CategoryStatus status = CategoryStatus::INITIALIZING; |
295 | 327 |
(...skipping 20 matching lines...) Expand all Loading... | |
316 | 348 |
317 // Returns a non-dismissed snippet with the given |id_within_category|, or | 349 // Returns a non-dismissed snippet with the given |id_within_category|, or |
318 // null if none exist. | 350 // null if none exist. |
319 const NTPSnippet* FindSnippet(const std::string& id_within_category) const; | 351 const NTPSnippet* FindSnippet(const std::string& id_within_category) const; |
320 | 352 |
321 CategoryContent(); | 353 CategoryContent(); |
322 CategoryContent(CategoryContent&&); | 354 CategoryContent(CategoryContent&&); |
323 ~CategoryContent(); | 355 ~CategoryContent(); |
324 CategoryContent& operator=(CategoryContent&&); | 356 CategoryContent& operator=(CategoryContent&&); |
325 }; | 357 }; |
326 std::map<Category, CategoryContent, Category::CompareByID> categories_; | 358 using CategoryContentMap = |
359 std::map<Category, CategoryContent, Category::CompareByID>; | |
360 CategoryContentMap categories_; | |
Marc Treib
2016/10/20 16:51:40
Any reason for this change?
Marc Treib
2016/10/28 14:49:50
Done. (reverted)
| |
327 | 361 |
328 // The ISO 639-1 code of the language used by the application. | 362 // The ISO 639-1 code of the language used by the application. |
329 const std::string application_language_code_; | 363 const std::string application_language_code_; |
330 | 364 |
331 // Classifier that tells us how active the user is. Not owned. | 365 // Classifier that tells us how active the user is. Not owned. |
332 const UserClassifier* user_classifier_; | 366 const UserClassifier* user_classifier_; |
333 | 367 |
334 // Scheduler for fetching snippets. Not owned. | 368 // Scheduler for fetching snippets. Not owned. |
335 NTPSnippetsScheduler* scheduler_; | 369 NTPSnippetsScheduler* scheduler_; |
336 | 370 |
(...skipping 20 matching lines...) Expand all Loading... | |
357 | 391 |
358 // Request throttler for limiting requests to thumbnail images. | 392 // Request throttler for limiting requests to thumbnail images. |
359 RequestThrottler thumbnail_requests_throttler_; | 393 RequestThrottler thumbnail_requests_throttler_; |
360 | 394 |
361 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 395 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
362 }; | 396 }; |
363 | 397 |
364 } // namespace ntp_snippets | 398 } // namespace ntp_snippets |
365 | 399 |
366 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_ | 400 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_ |
OLD | NEW |