OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <set> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/macros.h" |
| 14 #include "base/time/time.h" |
| 15 #include "components/ntp_snippets/content_suggestions_provider.h" |
| 16 #include "components/ntp_snippets/remote/persistent_scheduler.h" |
| 17 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" |
| 18 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" |
| 19 |
| 20 class PrefRegistrySimple; |
| 21 class PrefService; |
| 22 |
| 23 namespace ntp_snippets { |
| 24 |
| 25 struct Status; |
| 26 class UserClassifier; |
| 27 |
| 28 // A wrapper around RemoteSuggestionsProvider that introduces periodic fetching. |
| 29 // |
| 30 // The class initiates fetches on its own in these situations: |
| 31 // - initial fetch when the provider is constructed and we have no suggestions; |
| 32 // - regular fetches according to its schedule. |
| 33 // TODO(jkrcal): After soft fetch on Chrome startup is introduced, remove |
| 34 // the initial fetch completely. |
| 35 // |
| 36 // The class also needs to understand when last fetch trials and successful |
| 37 // fetches happen and thus it intercepts following interactive fetch requests: |
| 38 // - Fetch() - after "More" button of a remote section is pressed in the UI; |
| 39 // TODO(jkrcal): Clarify what Fetch() should do for this provider and maybe stop |
| 40 // intercepting it. |
| 41 // TODO(jkrcal): Intercept also ReloadSuggestions() call (after the user swipes |
| 42 // away everything incl. all empty sections and presses "More"); Not done in the |
| 43 // first shot because it implements a public interface function without any |
| 44 // callback. |
| 45 // This class is final because it does things in its constructor which make it |
| 46 // unsafe to derive from it. |
| 47 // TODO(jkrcal): Introduce two-phase initialization and make the class not |
| 48 // final? (see the same comment for RemoteSuggestionsProvider) |
| 49 class SchedulingRemoteSuggestionsProvider final |
| 50 : public RemoteSuggestionsProvider, |
| 51 public RemoteSuggestionsScheduler { |
| 52 public: |
| 53 SchedulingRemoteSuggestionsProvider( |
| 54 Observer* observer, |
| 55 std::unique_ptr<RemoteSuggestionsProvider> provider, |
| 56 PersistentScheduler* persistent_scheduler, |
| 57 const UserClassifier* user_classifier, |
| 58 PrefService* pref_service); |
| 59 |
| 60 ~SchedulingRemoteSuggestionsProvider() override; |
| 61 |
| 62 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 63 |
| 64 // RemoteSuggestionsScheduler implementation. |
| 65 void RescheduleFetching() override; |
| 66 void OnPersistentSchedulerWakeUp() override; |
| 67 |
| 68 // RemoteSuggestionsProvider implementation. |
| 69 void SetProviderStatusCallback( |
| 70 std::unique_ptr<ProviderStatusCallback> callback) override; |
| 71 void RefetchInTheBackground( |
| 72 std::unique_ptr<FetchStatusCallback> callback) override; |
| 73 const NTPSnippetsFetcher* snippets_fetcher_for_testing_and_debugging() |
| 74 const override; |
| 75 |
| 76 // ContentSuggestionsProvider implementation. |
| 77 CategoryStatus GetCategoryStatus(Category category) override; |
| 78 CategoryInfo GetCategoryInfo(Category category) override; |
| 79 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; |
| 80 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, |
| 81 const ImageFetchedCallback& callback) override; |
| 82 void Fetch(const Category& category, |
| 83 const std::set<std::string>& known_suggestion_ids, |
| 84 const FetchDoneCallback& callback) override; |
| 85 void ReloadSuggestions() override; |
| 86 void ClearHistory( |
| 87 base::Time begin, |
| 88 base::Time end, |
| 89 const base::Callback<bool(const GURL& url)>& filter) override; |
| 90 void ClearCachedSuggestions(Category category) override; |
| 91 void OnSignInStateChanged() override; |
| 92 void GetDismissedSuggestionsForDebugging( |
| 93 Category category, |
| 94 const DismissedSuggestionsCallback& callback) override; |
| 95 void ClearDismissedSuggestionsForDebugging(Category category) override; |
| 96 |
| 97 private: |
| 98 // Abstract description of the fetching schedule. |
| 99 struct FetchingSchedule; |
| 100 |
| 101 // Callback that is notified whenever the status of |provider_| changes. |
| 102 void OnProviderStatusChanged( |
| 103 RemoteSuggestionsProvider::ProviderStatus status); |
| 104 |
| 105 // After the call, updates will be scheduled in the future. Idempotent, can be |
| 106 // run any time later without impacting the current schedule. |
| 107 // If you want to enforce rescheduling, call Unschedule() and then Schedule(). |
| 108 void StartScheduling(); |
| 109 |
| 110 // After the call, no updates will happen before another call to Schedule(). |
| 111 // Idempotent, can be run any time later without impacting the current |
| 112 // schedule. |
| 113 void StopScheduling(); |
| 114 |
| 115 // Callback after Fetch is completed. |
| 116 void FetchFinished(const FetchDoneCallback& callback, |
| 117 Status status_code, |
| 118 std::vector<ContentSuggestion> suggestions); |
| 119 |
| 120 FetchingSchedule GetDesiredFetchingSchedule() const; |
| 121 FetchingSchedule GetLastFetchingSchedule() const; |
| 122 void StoreLastFetchingSchedule(const FetchingSchedule& schedule); |
| 123 |
| 124 // Common function to call after each fetch. |
| 125 void OnFetchCompleted(Status status); |
| 126 |
| 127 // Applies the provided |schedule|. |
| 128 void ApplyFetchingSchedule(const FetchingSchedule& schedule); |
| 129 |
| 130 // Interface for doing all the actual work (apart from scheduling). |
| 131 std::unique_ptr<RemoteSuggestionsProvider> provider_; |
| 132 |
| 133 // Interface for scheduling hard fetches, OS dependent. Not owned, may be |
| 134 // null. |
| 135 PersistentScheduler* persistent_scheduler_; |
| 136 |
| 137 // Used to adapt the schedule based on usage activity of the user. Not owned. |
| 138 const UserClassifier* user_classifier_; |
| 139 |
| 140 PrefService* pref_service_; |
| 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProvider); |
| 143 }; |
| 144 |
| 145 } // namespace ntp_snippets |
| 146 |
| 147 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER
_H_ |
OLD | NEW |