| 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); |
| 214 | 213 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 | 344 |
| 346 // Removes expired dismissed suggestions from the service and the database. | 345 // Removes expired dismissed suggestions from the service and the database. |
| 347 void ClearExpiredDismissedSuggestions(); | 346 void ClearExpiredDismissedSuggestions(); |
| 348 | 347 |
| 349 // Removes images from the DB that are not referenced from any known | 348 // Removes images from the DB that are not referenced from any known |
| 350 // suggestion. Needs to iterate the whole suggestion database -- so do it | 349 // 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 | 350 // often enough to keep it small but not too often as it still iterates over |
| 352 // the file system. | 351 // the file system. |
| 353 void ClearOrphanedImages(); | 352 void ClearOrphanedImages(); |
| 354 | 353 |
| 354 // Clears suggestions because any history item has been removed. |
| 355 void ClearAnyHistory(); |
| 356 |
| 355 // Clears all stored suggestions and updates the observer. | 357 // Clears all stored suggestions and updates the observer. |
| 356 void NukeAllSuggestions(); | 358 void NukeAllSuggestions(); |
| 357 | 359 |
| 358 // Completes the initialization phase of the service, registering the last | 360 // Completes the initialization phase of the service, registering the last |
| 359 // observers. This is done after construction, once the database is loaded. | 361 // observers. This is done after construction, once the database is loaded. |
| 360 void FinishInitialization(); | 362 void FinishInitialization(); |
| 361 | 363 |
| 362 // Triggers a state transition depending on the provided status. This method | 364 // Triggers a state transition depending on the provided status. This method |
| 363 // is called when a change is detected by |status_service_|. | 365 // is called when a change is detected by |status_service_|. |
| 364 void OnStatusChanged(RemoteSuggestionsStatus old_status, | 366 void OnStatusChanged(RemoteSuggestionsStatus old_status, |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 | 437 |
| 436 // Set to true if FetchSuggestions is called while the service isn't ready. | 438 // 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. | 439 // 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<>? | 440 // TODO(jkrcal): create a struct and have here just one base::Optional<>? |
| 439 bool fetch_when_ready_; | 441 bool fetch_when_ready_; |
| 440 | 442 |
| 441 // The parameters for the fetch to perform later. | 443 // The parameters for the fetch to perform later. |
| 442 bool fetch_when_ready_interactive_; | 444 bool fetch_when_ready_interactive_; |
| 443 std::unique_ptr<FetchStatusCallback> fetch_when_ready_callback_; | 445 std::unique_ptr<FetchStatusCallback> fetch_when_ready_callback_; |
| 444 | 446 |
| 445 std::unique_ptr<ProviderStatusCallback> provider_status_callback_; | 447 RemoteSuggestionsScheduler* remote_suggestions_scheduler_; |
| 446 | 448 |
| 447 // Set to true if NukeAllSuggestions is called while the service isn't ready. | 449 // 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 | 450 // The nuke will be executed once the service finishes initialization or |
| 449 // enters the READY state. | 451 // enters the READY state. |
| 450 bool nuke_when_initialized_; | 452 bool clear_any_history_when_initialized_; |
| 451 | 453 |
| 452 // A clock for getting the time. This allows to inject a clock in tests. | 454 // A clock for getting the time. This allows to inject a clock in tests. |
| 453 std::unique_ptr<base::Clock> clock_; | 455 std::unique_ptr<base::Clock> clock_; |
| 454 | 456 |
| 455 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderImpl); | 457 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsProviderImpl); |
| 456 }; | 458 }; |
| 457 | 459 |
| 458 } // namespace ntp_snippets | 460 } // namespace ntp_snippets |
| 459 | 461 |
| 460 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ | 462 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_PROVIDER_IMPL_H_ |
| OLD | NEW |