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_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ |
6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ |
7 | 7 |
8 #include <cstddef> | 8 #include <cstddef> |
9 #include <deque> | 9 #include <deque> |
10 #include <map> | 10 #include <map> |
(...skipping 29 matching lines...) Expand all Loading... | |
40 | 40 |
41 namespace image_fetcher { | 41 namespace image_fetcher { |
42 class ImageDecoder; | 42 class ImageDecoder; |
43 class ImageFetcher; | 43 class ImageFetcher; |
44 } // namespace image_fetcher | 44 } // namespace image_fetcher |
45 | 45 |
46 namespace ntp_snippets { | 46 namespace ntp_snippets { |
47 | 47 |
48 class CategoryRanker; | 48 class CategoryRanker; |
49 class RemoteSuggestionsDatabase; | 49 class RemoteSuggestionsDatabase; |
50 class RemoteSuggestionsScheduler; | |
50 | 51 |
51 // CachedImageFetcher takes care of fetching images from the network and caching | 52 // CachedImageFetcher takes care of fetching images from the network and caching |
52 // them in the database. | 53 // them in the database. |
53 // TODO(tschumann): Move into a separate library and inject the | 54 // TODO(tschumann): Move into a separate library and inject the |
54 // CachedImageFetcher into the RemoteSuggestionsProvider. This allows us to get | 55 // CachedImageFetcher into the RemoteSuggestionsProvider. This allows us to get |
55 // rid of exposing this member for testing and lets us test the caching logic | 56 // rid of exposing this member for testing and lets us test the caching logic |
56 // separately. | 57 // separately. |
57 class CachedImageFetcher : public image_fetcher::ImageFetcherDelegate { | 58 class CachedImageFetcher : public image_fetcher::ImageFetcherDelegate { |
58 public: | 59 public: |
59 // |pref_service| and |database| need to outlive the created image fetcher | 60 // |pref_service| and |database| need to outlive the created image fetcher |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 // Returns whether the service is ready. While this is false, the list of | 131 // Returns whether the service is ready. While this is false, the list of |
131 // suggestions will be empty, and all modifications to it (fetch, dismiss, | 132 // suggestions will be empty, and all modifications to it (fetch, dismiss, |
132 // etc) will be ignored. | 133 // etc) will be ignored. |
133 bool ready() const { return state_ == State::READY; } | 134 bool ready() const { return state_ == State::READY; } |
134 | 135 |
135 // Returns whether the service is successfully initialized. While this is | 136 // Returns whether the service is successfully initialized. While this is |
136 // false, some calls may trigger DCHECKs. | 137 // false, some calls may trigger DCHECKs. |
137 bool initialized() const { return ready() || state_ == State::DISABLED; } | 138 bool initialized() const { return ready() || state_ == State::DISABLED; } |
138 | 139 |
139 // RemoteSuggestionsProvider implementation. | 140 // RemoteSuggestionsProvider implementation. |
140 void SetProviderStatusCallback( | 141 void SetRemoteSuggestionsScheduler( |
141 std::unique_ptr<ProviderStatusCallback> callback) override; | 142 RemoteSuggestionsScheduler* scheduler) override; |
142 void RefetchInTheBackground( | 143 void RefetchInTheBackground( |
143 std::unique_ptr<FetchStatusCallback> callback) override; | 144 std::unique_ptr<FetchStatusCallback> callback) override; |
144 | 145 |
145 // TODO(fhorschig): Remove this getter when there is an interface for the | 146 // TODO(fhorschig): Remove this getter when there is an interface for the |
146 // fetcher that allows better mocks. | 147 // fetcher that allows better mocks. |
147 const RemoteSuggestionsFetcher* suggestions_fetcher_for_debugging() | 148 const RemoteSuggestionsFetcher* suggestions_fetcher_for_debugging() |
148 const override; | 149 const override; |
149 | 150 |
150 // ContentSuggestionsProvider implementation. | 151 // ContentSuggestionsProvider implementation. |
151 CategoryStatus GetCategoryStatus(Category category) override; | 152 CategoryStatus GetCategoryStatus(Category category) override; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 } | 191 } |
191 | 192 |
192 // TODO(tschumann): remove this method as soon as we inject the fetcher into | 193 // TODO(tschumann): remove this method as soon as we inject the fetcher into |
193 // the constructor. | 194 // the constructor. |
194 CachedImageFetcher& GetImageFetcherForTesting() { return image_fetcher_; } | 195 CachedImageFetcher& GetImageFetcherForTesting() { return image_fetcher_; } |
195 | 196 |
196 private: | 197 private: |
197 friend class RemoteSuggestionsProviderImplTest; | 198 friend class RemoteSuggestionsProviderImplTest; |
198 | 199 |
199 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 200 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
200 CallsProviderStatusCallbackWhenReady); | 201 CallsSchedulerOnError); |
201 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 202 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
202 CallsProviderStatusCallbackOnError); | 203 CallsSchedulerWhenDisabled); |
203 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | |
204 CallsProviderStatusCallbackWhenDisabled); | |
205 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 204 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
206 ShouldNotCrashWhenCallingEmptyCallback); | 205 ShouldNotCrashWhenCallingEmptyCallback); |
207 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 206 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
208 DontNotifyIfNotAvailable); | 207 DontNotifyIfNotAvailable); |
209 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 208 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
210 RemoveExpiredDismissedContent); | 209 RemoveExpiredDismissedContent); |
211 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, StatusChanges); | 210 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, StatusChanges); |
212 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 211 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
213 SuggestionsFetchedOnSignInAndSignOut); | 212 SuggestionsFetchedOnSignInAndSignOut); |
213 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | |
tschumann
2017/02/22 11:31:34
please add a comment why these tests need to be fr
jkrcal
2017/02/22 13:17:03
Agreed. I did a clean-up in the friends, removed s
| |
214 CallsSchedulerWhenSignedIn); | |
215 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | |
216 CallsSchedulerWhenSignedOut); | |
214 | 217 |
215 // Possible state transitions: | 218 // Possible state transitions: |
216 // NOT_INITED --------+ | 219 // NOT_INITED --------+ |
217 // / \ | | 220 // / \ | |
218 // v v | | 221 // v v | |
219 // READY <--> DISABLED | | 222 // READY <--> DISABLED | |
220 // \ / | | 223 // \ / | |
221 // v v | | 224 // v v | |
222 // ERROR_OCCURRED <-----+ | 225 // ERROR_OCCURRED <-----+ |
223 enum class State { | 226 enum class State { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
345 | 348 |
346 // Removes expired dismissed suggestions from the service and the database. | 349 // Removes expired dismissed suggestions from the service and the database. |
347 void ClearExpiredDismissedSuggestions(); | 350 void ClearExpiredDismissedSuggestions(); |
348 | 351 |
349 // Removes images from the DB that are not referenced from any known | 352 // Removes images from the DB that are not referenced from any known |
350 // suggestion. Needs to iterate the whole suggestion database -- so do it | 353 // suggestion. Needs to iterate the whole suggestion database -- so do it |
351 // often enough to keep it small but not too often as it still iterates over | 354 // often enough to keep it small but not too often as it still iterates over |
352 // the file system. | 355 // the file system. |
353 void ClearOrphanedImages(); | 356 void ClearOrphanedImages(); |
354 | 357 |
358 // Clears suggestions because any history item has been removed. | |
359 void ClearAnyHistory(); | |
360 | |
361 // Clears suggestions because they became obsolete. | |
362 void ObsoleteSuggestions(); | |
tschumann
2017/02/22 11:31:34
i'd like to avoid coining new terms -- it's pretty
jkrcal
2017/02/22 13:17:03
Makes sense. Done.
| |
363 | |
355 // Clears all stored suggestions and updates the observer. | 364 // Clears all stored suggestions and updates the observer. |
356 void NukeAllSuggestions(); | 365 void NukeAllSuggestions(); |
357 | 366 |
358 // Completes the initialization phase of the service, registering the last | 367 // Completes the initialization phase of the service, registering the last |
359 // observers. This is done after construction, once the database is loaded. | 368 // observers. This is done after construction, once the database is loaded. |
360 void FinishInitialization(); | 369 void FinishInitialization(); |
361 | 370 |
362 // Triggers a state transition depending on the provided status. This method | 371 // Triggers a state transition depending on the provided status. This method |
363 // is called when a change is detected by |status_service_|. | 372 // is called when a change is detected by |status_service_|. |
364 void OnStatusChanged(RemoteSuggestionsStatus old_status, | 373 void OnStatusChanged(RemoteSuggestionsStatus old_status, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 | 444 |
436 // Set to true if FetchSuggestions is called while the service isn't ready. | 445 // Set to true if FetchSuggestions is called while the service isn't ready. |
437 // The fetch will be executed once the service enters the READY state. | 446 // The fetch will be executed once the service enters the READY state. |
438 // TODO(jkrcal): create a struct and have here just one base::Optional<>? | 447 // TODO(jkrcal): create a struct and have here just one base::Optional<>? |
439 bool fetch_when_ready_; | 448 bool fetch_when_ready_; |
440 | 449 |
441 // The parameters for the fetch to perform later. | 450 // The parameters for the fetch to perform later. |
442 bool fetch_when_ready_interactive_; | 451 bool fetch_when_ready_interactive_; |
443 std::unique_ptr<FetchStatusCallback> fetch_when_ready_callback_; | 452 std::unique_ptr<FetchStatusCallback> fetch_when_ready_callback_; |
444 | 453 |
445 std::unique_ptr<ProviderStatusCallback> provider_status_callback_; | 454 RemoteSuggestionsScheduler* remote_suggestions_scheduler_; |
446 | 455 |
447 // Set to true if NukeAllSuggestions is called while the service isn't ready. | 456 // Set to true if ClearAnyHistory is called while the service isn't ready. |
448 // The nuke will be executed once the service finishes initialization or | 457 // The nuke will be executed once the service finishes initialization or |
449 // enters the READY state. | 458 // enters the READY state. |
450 bool nuke_when_initialized_; | 459 bool clear_any_history_when_initialized_; |
451 | 460 |
452 // A clock for getting the time. This allows to inject a clock in tests. | 461 // A clock for getting the time. This allows to inject a clock in tests. |
453 std::unique_ptr<base::Clock> clock_; | 462 std::unique_ptr<base::Clock> clock_; |
454 | 463 |
455 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderImpl); | 464 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderImpl); |
456 }; | 465 }; |
457 | 466 |
458 } // namespace ntp_snippets | 467 } // namespace ntp_snippets |
459 | 468 |
460 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ | 469 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ |
OLD | NEW |