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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 clock_ = std::move(clock); | 190 clock_ = std::move(clock); |
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 |
200 // TODO(jkrcal): Mock the database to trigger the error naturally (or remote | |
Marc Treib
2017/02/23 13:52:24
s/remote/remove/ ?
jkrcal
2017/02/24 09:21:43
Done.
| |
201 // the error state and get rid of the test). | |
199 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 202 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
200 CallsProviderStatusCallbackWhenReady); | 203 CallsSchedulerOnError); |
204 // TODO(jkrcal): Mock the status service and remove these friend declarations. | |
201 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 205 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
202 CallsProviderStatusCallbackOnError); | 206 CallsSchedulerWhenDisabled); |
203 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | |
204 CallsProviderStatusCallbackWhenDisabled); | |
205 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | |
206 ShouldNotCrashWhenCallingEmptyCallback); | |
207 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 207 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
208 DontNotifyIfNotAvailable); | 208 DontNotifyIfNotAvailable); |
209 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 209 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
210 RemoveExpiredDismissedContent); | 210 CallsSchedulerWhenSignedIn); |
211 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, StatusChanges); | |
212 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, | 211 FRIEND_TEST_ALL_PREFIXES(RemoteSuggestionsProviderImplTest, |
213 SuggestionsFetchedOnSignInAndSignOut); | 212 CallsSchedulerWhenSignedOut); |
214 | 213 |
215 // Possible state transitions: | 214 // Possible state transitions: |
216 // NOT_INITED --------+ | 215 // NOT_INITED --------+ |
217 // / \ | | 216 // / \ | |
218 // v v | | 217 // v v | |
219 // READY <--> DISABLED | | 218 // READY <--> DISABLED | |
220 // \ / | | 219 // \ / | |
221 // v v | | 220 // v v | |
222 // ERROR_OCCURRED <-----+ | 221 // ERROR_OCCURRED <-----+ |
222 // TODO(jkrcal): Do we need to keep the distinction between states DISABLED | |
223 // and ERROR_OCCURED? | |
223 enum class State { | 224 enum class State { |
224 // The service has just been created. Can change to states: | 225 // The service has just been created. Can change to states: |
225 // - DISABLED: After the database is done loading, | 226 // - DISABLED: After the database is done loading, |
226 // GetStateForDependenciesStatus can identify the next state to | 227 // GetStateForDependenciesStatus can identify the next state to |
227 // be DISABLED. | 228 // be DISABLED. |
228 // - READY: if GetStateForDependenciesStatus returns it, after the database | 229 // - READY: if GetStateForDependenciesStatus returns it, after the database |
229 // is done loading. | 230 // is done loading. |
230 // - ERROR_OCCURRED: when an unrecoverable error occurred. | 231 // - ERROR_OCCURRED: when an unrecoverable error occurred. |
231 NOT_INITED, | 232 NOT_INITED, |
232 | 233 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
345 | 346 |
346 // Removes expired dismissed suggestions from the service and the database. | 347 // Removes expired dismissed suggestions from the service and the database. |
347 void ClearExpiredDismissedSuggestions(); | 348 void ClearExpiredDismissedSuggestions(); |
348 | 349 |
349 // Removes images from the DB that are not referenced from any known | 350 // Removes images from the DB that are not referenced from any known |
350 // suggestion. Needs to iterate the whole suggestion database -- so do it | 351 // 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 | 352 // often enough to keep it small but not too often as it still iterates over |
352 // the file system. | 353 // the file system. |
353 void ClearOrphanedImages(); | 354 void ClearOrphanedImages(); |
354 | 355 |
356 // Clears suggestions because any history item has been removed. | |
357 void ClearAnyHistory(); | |
Marc Treib
2017/02/23 13:52:24
Should this be "AnyHistoryCleared"? It doesn't cle
jkrcal
2017/02/24 09:21:43
I prefer the active form, stick to ClearHistoryDep
| |
358 | |
359 // Clears suggestions for any non-history related reason (e.g., sign-in status | |
360 // change, etc.). | |
361 void ClearSuggestions(); | |
362 | |
355 // Clears all stored suggestions and updates the observer. | 363 // Clears all stored suggestions and updates the observer. |
356 void NukeAllSuggestions(); | 364 void NukeAllSuggestions(); |
357 | 365 |
358 // Completes the initialization phase of the service, registering the last | 366 // Completes the initialization phase of the service, registering the last |
359 // observers. This is done after construction, once the database is loaded. | 367 // observers. This is done after construction, once the database is loaded. |
360 void FinishInitialization(); | 368 void FinishInitialization(); |
361 | 369 |
362 // Triggers a state transition depending on the provided status. This method | 370 // Triggers a state transition depending on the provided status. This method |
363 // is called when a change is detected by |status_service_|. | 371 // is called when a change is detected by |status_service_|. |
364 void OnStatusChanged(RemoteSuggestionsStatus old_status, | 372 void OnStatusChanged(RemoteSuggestionsStatus old_status, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 | 443 |
436 // Set to true if FetchSuggestions is called while the service isn't ready. | 444 // 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. | 445 // 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<>? | 446 // TODO(jkrcal): create a struct and have here just one base::Optional<>? |
439 bool fetch_when_ready_; | 447 bool fetch_when_ready_; |
440 | 448 |
441 // The parameters for the fetch to perform later. | 449 // The parameters for the fetch to perform later. |
442 bool fetch_when_ready_interactive_; | 450 bool fetch_when_ready_interactive_; |
443 std::unique_ptr<FetchStatusCallback> fetch_when_ready_callback_; | 451 std::unique_ptr<FetchStatusCallback> fetch_when_ready_callback_; |
444 | 452 |
445 std::unique_ptr<ProviderStatusCallback> provider_status_callback_; | 453 RemoteSuggestionsScheduler* remote_suggestions_scheduler_; |
446 | 454 |
447 // Set to true if NukeAllSuggestions is called while the service isn't ready. | 455 // 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 | 456 // The nuke will be executed once the service finishes initialization or |
449 // enters the READY state. | 457 // enters the READY state. |
450 bool nuke_when_initialized_; | 458 bool clear_any_history_when_initialized_; |
451 | 459 |
452 // A clock for getting the time. This allows to inject a clock in tests. | 460 // A clock for getting the time. This allows to inject a clock in tests. |
453 std::unique_ptr<base::Clock> clock_; | 461 std::unique_ptr<base::Clock> clock_; |
454 | 462 |
455 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderImpl); | 463 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderImpl); |
456 }; | 464 }; |
457 | 465 |
458 } // namespace ntp_snippets | 466 } // namespace ntp_snippets |
459 | 467 |
460 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ | 468 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ |
OLD | NEW |