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() override; | |
119 void ClearHistory( | 120 void ClearHistory( |
120 base::Time begin, | 121 base::Time begin, |
121 base::Time end, | 122 base::Time end, |
122 const base::Callback<bool(const GURL& url)>& filter) override; | 123 const base::Callback<bool(const GURL& url)>& filter) override; |
123 void ClearCachedSuggestions(Category category) override; | 124 void ClearCachedSuggestions(Category category) override; |
124 void GetDismissedSuggestionsForDebugging( | 125 void GetDismissedSuggestionsForDebugging( |
125 Category category, | 126 Category category, |
126 const DismissedSuggestionsCallback& callback) override; | 127 const DismissedSuggestionsCallback& callback) override; |
127 void ClearDismissedSuggestionsForDebugging(Category category) override; | 128 void ClearDismissedSuggestionsForDebugging(Category category) override; |
128 | 129 |
(...skipping 13 matching lines...) Expand all Loading... | |
142 } | 143 } |
143 | 144 |
144 // Dismissed snippets, only for unit tests. | 145 // Dismissed snippets, only for unit tests. |
145 const NTPSnippet::PtrVector& GetDismissedSnippetsForTesting( | 146 const NTPSnippet::PtrVector& GetDismissedSnippetsForTesting( |
146 Category category) const { | 147 Category category) const { |
147 return categories_.find(category)->second.dismissed; | 148 return categories_.find(category)->second.dismissed; |
148 } | 149 } |
149 | 150 |
150 private: | 151 private: |
151 friend class NTPSnippetsServiceTest; | 152 friend class NTPSnippetsServiceTest; |
153 | |
154 // Forward declaration of the Strategy used to process fetch requests. | |
155 class NewNTPSnippetsHandlingStrategy; | |
156 | |
152 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, | 157 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, |
153 RemoveExpiredDismissedContent); | 158 RemoveExpiredDismissedContent); |
154 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, RescheduleOnStateChange); | 159 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, RescheduleOnStateChange); |
155 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); | 160 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, StatusChanges); |
156 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, | 161 FRIEND_TEST_ALL_PREFIXES(NTPSnippetsServiceTest, |
157 SuggestionsFetchedOnSignInAndSignOut); | 162 SuggestionsFetchedOnSignInAndSignOut); |
158 | 163 |
159 // Possible state transitions: | 164 // Possible state transitions: |
160 // NOT_INITED --------+ | 165 // NOT_INITED --------+ |
161 // / \ | | 166 // / \ | |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 // image_fetcher::ImageFetcherDelegate implementation. | 206 // image_fetcher::ImageFetcherDelegate implementation. |
202 void OnImageDataFetched(const std::string& id_within_category, | 207 void OnImageDataFetched(const std::string& id_within_category, |
203 const std::string& image_data) override; | 208 const std::string& image_data) override; |
204 | 209 |
205 // Callbacks for the NTPSnippetsDatabase. | 210 // Callbacks for the NTPSnippetsDatabase. |
206 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); | 211 void OnDatabaseLoaded(NTPSnippet::PtrVector snippets); |
207 void OnDatabaseError(); | 212 void OnDatabaseError(); |
208 | 213 |
209 // Callback for the NTPSnippetsFetcher. | 214 // Callback for the NTPSnippetsFetcher. |
210 void OnFetchFinished( | 215 void OnFetchFinished( |
216 std::unique_ptr<NewNTPSnippetsHandlingStrategy> strategy, | |
211 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); | 217 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories); |
212 | 218 |
213 // Moves all snippets from |to_archive| into the archive of the |category|. | 219 // 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 | 220 // It also deletes the snippets from the DB and keeps the archive reasonably |
215 // short. | 221 // short. |
216 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive); | 222 void ArchiveSnippets(Category category, NTPSnippet::PtrVector* to_archive); |
217 | 223 |
218 // Replace old snippets in |category| by newly available snippets. | 224 // Add newly available snippets in |category| to the provided content. |
219 void ReplaceSnippets(Category category, NTPSnippet::PtrVector new_snippets); | 225 // If |replace_snippets| is set, archive old snippets. |
226 void StoreSnippets(const Category& category, | |
227 NTPSnippet::PtrVector new_snippets, | |
228 NewNTPSnippetsHandlingStrategy* strategy); | |
220 | 229 |
221 // Removes expired dismissed snippets from the service and the database. | 230 // Removes expired dismissed snippets from the service and the database. |
222 void ClearExpiredDismissedSnippets(); | 231 void ClearExpiredDismissedSnippets(); |
223 | 232 |
224 // Removes images from the DB that are not referenced from any known snippet. | 233 // 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 | 234 // 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. | 235 // keep it small but not too often as it still iterates over the file system. |
227 void ClearOrphanedImages(); | 236 void ClearOrphanedImages(); |
228 | 237 |
229 // Clears all stored snippets and updates the observer. | 238 // Clears all stored snippets and updates the observer. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 | 285 |
277 // Updates the internal status for |category| to |category_status_| and | 286 // Updates the internal status for |category| to |category_status_| and |
278 // notifies the content suggestions observer if it changed. | 287 // notifies the content suggestions observer if it changed. |
279 void UpdateCategoryStatus(Category category, CategoryStatus status); | 288 void UpdateCategoryStatus(Category category, CategoryStatus status); |
280 // Calls UpdateCategoryStatus() for all provided categories. | 289 // Calls UpdateCategoryStatus() for all provided categories. |
281 void UpdateAllCategoryStatus(CategoryStatus status); | 290 void UpdateAllCategoryStatus(CategoryStatus status); |
282 | 291 |
283 void RestoreCategoriesFromPrefs(); | 292 void RestoreCategoriesFromPrefs(); |
284 void StoreCategoriesToPrefs(); | 293 void StoreCategoriesToPrefs(); |
285 | 294 |
295 // Implementation for |FetchSnippets| and |FetchMore| that calls the snippet | |
296 // fetcher and replaces or adds the fetched snippets depending on the passed | |
297 // |strategy|. | |
298 void FetchSnippetsFromHostsImpl( | |
299 const std::set<std::string>& hosts, | |
300 bool interactive_request, | |
301 std::unique_ptr<NewNTPSnippetsHandlingStrategy> strategy); | |
302 | |
303 void MarkEmptyCategoriesAsLoading(); | |
304 | |
305 void AssignExpiryDates(NTPSnippet::PtrVector* snippets); | |
306 | |
307 void AddIncompleteSnippets(NTPSnippet::PtrVector* snippets); | |
308 | |
286 State state_; | 309 State state_; |
287 | 310 |
311 // TODO(fhorschig): Consider moving this into a separate File. | |
312 // This class implements a Strategy pattern to determine the handling of new | |
313 // snippets. | |
314 // As friend of NTPSnippetService, it calls private methods to handle the | |
315 // persistence of snippets, invoking correct notification for observers, etc. | |
316 class NewNTPSnippetsHandlingStrategy { | |
317 public: | |
318 virtual void NotifySuggestions() = 0; | |
319 virtual void SaveNewSnippets(const Category& category, | |
320 NTPSnippet::PtrVector new_snippets) = 0; | |
321 virtual void CollectIdsToExclude(std::set<std::string>* ids) = 0; | |
322 | |
323 protected: | |
324 explicit NewNTPSnippetsHandlingStrategy(NTPSnippetsService* service); | |
325 | |
326 NTPSnippetsService* service_; | |
327 | |
328 private: | |
329 DISALLOW_COPY_AND_ASSIGN(NewNTPSnippetsHandlingStrategy); | |
330 }; | |
331 friend class NewNTPSnippetsHandlingStrategy; | |
Marc Treib
2016/10/18 08:17:45
Hm... TBH, I'm not sure I like this. Clearly, NTPS
fhorschig
2016/10/20 13:10:38
I absolutely agree with the private methods and I
Marc Treib
2016/10/20 16:51:39
I guess that might work... it's hard to judge with
tschumann
2016/10/24 06:38:25
What about introducing a NTPSnippetsFetcher::Param
Marc Treib
2016/10/28 14:49:48
NTPSnippetsFetcher::FetchSnippets now takes a Para
| |
332 | |
333 // This class handles fetch requests that require all existing snippets to be | |
334 // replaced by newly fetched ones. | |
335 // It invokes the |OnNewSuggestions()| notification for observers. | |
336 class ReplaceExistingSnippets : public NewNTPSnippetsHandlingStrategy { | |
337 public: | |
338 explicit ReplaceExistingSnippets(NTPSnippetsService* service); | |
339 void CollectIdsToExclude(std::set<std::string>* ids) override; | |
340 void SaveNewSnippets(const Category& category, | |
341 NTPSnippet::PtrVector new_snippets) override; | |
342 void NotifySuggestions() override; | |
343 }; | |
344 | |
345 // This class handles fetch requests that add more snippets. | |
346 // It invokes the |OnMoreSuggestions()| notification for observers. | |
347 class AddNewSnippets : public NewNTPSnippetsHandlingStrategy{ | |
348 public: | |
349 explicit AddNewSnippets(NTPSnippetsService* service); | |
350 | |
351 void CollectIdsToExclude(std::set<std::string>* ids) override; | |
352 void SaveNewSnippets(const Category& category, | |
353 NTPSnippet::PtrVector new_snippets) override; | |
354 void NotifySuggestions() override; | |
355 }; | |
356 | |
288 PrefService* pref_service_; | 357 PrefService* pref_service_; |
289 | 358 |
290 const Category articles_category_; | 359 const Category articles_category_; |
291 | 360 |
292 // TODO(sfiera): Reduce duplication of CategoryContent with CategoryInfo. | 361 // TODO(sfiera): Reduce duplication of CategoryContent with CategoryInfo. |
293 struct CategoryContent { | 362 struct CategoryContent { |
294 CategoryStatus status = CategoryStatus::INITIALIZING; | 363 CategoryStatus status = CategoryStatus::INITIALIZING; |
295 | 364 |
296 // The title of the section, localized to the running UI language. | 365 // The title of the section, localized to the running UI language. |
297 base::string16 localized_title; | 366 base::string16 localized_title; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
357 | 426 |
358 // Request throttler for limiting requests to thumbnail images. | 427 // Request throttler for limiting requests to thumbnail images. |
359 RequestThrottler thumbnail_requests_throttler_; | 428 RequestThrottler thumbnail_requests_throttler_; |
360 | 429 |
361 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); | 430 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsService); |
362 }; | 431 }; |
363 | 432 |
364 } // namespace ntp_snippets | 433 } // namespace ntp_snippets |
365 | 434 |
366 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_ | 435 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_NTP_SNIPPETS_SERVICE_H_ |
OLD | NEW |