| 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 <utility> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/macros.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "components/ntp_snippets/content_suggestions_provider.h" | |
| 17 #include "components/ntp_snippets/remote/persistent_scheduler.h" | |
| 18 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" | |
| 19 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" | |
| 20 #include "components/ntp_snippets/remote/request_throttler.h" | |
| 21 #include "components/web_resource/eula_accepted_notifier.h" | |
| 22 | |
| 23 class PrefRegistrySimple; | |
| 24 class PrefService; | |
| 25 | |
| 26 namespace base { | |
| 27 class Clock; | |
| 28 } | |
| 29 | |
| 30 namespace ntp_snippets { | |
| 31 | |
| 32 struct Status; | |
| 33 class EulaState; | |
| 34 class UserClassifier; | |
| 35 | |
| 36 // A wrapper around RemoteSuggestionsProvider that introduces periodic fetching. | |
| 37 // | |
| 38 // The class initiates fetches on its own in these situations: | |
| 39 // - initial fetch when the provider is constructed and we have no suggestions; | |
| 40 // - regular fetches according to its schedule. | |
| 41 // TODO(jkrcal): After soft fetch on Chrome startup is introduced, remove | |
| 42 // the initial fetch completely. | |
| 43 // | |
| 44 // The class also needs to understand when last fetch trials and successful | |
| 45 // fetches happen and thus it intercepts following interactive fetch requests: | |
| 46 // - Fetch() - after "More" button of a remote section is pressed in the UI; | |
| 47 // TODO(jkrcal): Clarify what Fetch() should do for this provider and maybe stop | |
| 48 // intercepting it. | |
| 49 // TODO(jkrcal): Intercept also ReloadSuggestions() call (after the user swipes | |
| 50 // away everything incl. all empty sections and presses "More"); Not done in the | |
| 51 // first shot because it implements a public interface function without any | |
| 52 // callback. | |
| 53 // This class is final because it does things in its constructor which make it | |
| 54 // unsafe to derive from it. | |
| 55 // TODO(jkrcal): Introduce two-phase initialization and make the class not | |
| 56 // final? (see the same comment for RemoteSuggestionsProvider) | |
| 57 // TODO(jkrcal): Change the interface to ContentSuggestionsProvider. We do not | |
| 58 // need any special functionality, all special should be exposed in the | |
| 59 // Scheduler interface. crbug.com/695447 | |
| 60 class SchedulingRemoteSuggestionsProvider final | |
| 61 : public RemoteSuggestionsProvider, | |
| 62 public RemoteSuggestionsScheduler { | |
| 63 public: | |
| 64 SchedulingRemoteSuggestionsProvider( | |
| 65 Observer* observer, | |
| 66 std::unique_ptr<RemoteSuggestionsProvider> provider, | |
| 67 PersistentScheduler* persistent_scheduler, | |
| 68 const UserClassifier* user_classifier, | |
| 69 PrefService* profile_prefs, | |
| 70 PrefService* local_state_prefs, | |
| 71 std::unique_ptr<base::Clock> clock); | |
| 72 | |
| 73 ~SchedulingRemoteSuggestionsProvider() override; | |
| 74 | |
| 75 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | |
| 76 | |
| 77 // RemoteSuggestionsScheduler implementation. | |
| 78 void OnProviderActivated() override; | |
| 79 void OnProviderDeactivated() override; | |
| 80 void OnSuggestionsCleared() override; | |
| 81 void OnHistoryCleared() override; | |
| 82 void RescheduleFetching() override; | |
| 83 void OnPersistentSchedulerWakeUp() override; | |
| 84 void OnBrowserForegrounded() override; | |
| 85 void OnBrowserColdStart() override; | |
| 86 void OnNTPOpened() override; | |
| 87 | |
| 88 // RemoteSuggestionsProvider implementation. | |
| 89 void RefetchInTheBackground( | |
| 90 std::unique_ptr<FetchStatusCallback> callback) override; | |
| 91 const RemoteSuggestionsFetcher* suggestions_fetcher_for_debugging() | |
| 92 const override; | |
| 93 | |
| 94 // ContentSuggestionsProvider implementation. | |
| 95 CategoryStatus GetCategoryStatus(Category category) override; | |
| 96 CategoryInfo GetCategoryInfo(Category category) override; | |
| 97 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override; | |
| 98 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id, | |
| 99 const ImageFetchedCallback& callback) override; | |
| 100 void Fetch(const Category& category, | |
| 101 const std::set<std::string>& known_suggestion_ids, | |
| 102 const FetchDoneCallback& callback) override; | |
| 103 void ReloadSuggestions() override; | |
| 104 void ClearHistory( | |
| 105 base::Time begin, | |
| 106 base::Time end, | |
| 107 const base::Callback<bool(const GURL& url)>& filter) override; | |
| 108 void ClearCachedSuggestions(Category category) override; | |
| 109 void OnSignInStateChanged() override; | |
| 110 void GetDismissedSuggestionsForDebugging( | |
| 111 Category category, | |
| 112 const DismissedSuggestionsCallback& callback) override; | |
| 113 void ClearDismissedSuggestionsForDebugging(Category category) override; | |
| 114 | |
| 115 private: | |
| 116 // Abstract description of the fetching schedule. | |
| 117 struct FetchingSchedule { | |
| 118 static FetchingSchedule Empty(); | |
| 119 bool operator==(const FetchingSchedule& other) const; | |
| 120 bool operator!=(const FetchingSchedule& other) const; | |
| 121 bool is_empty() const; | |
| 122 | |
| 123 base::TimeDelta interval_persistent_wifi; | |
| 124 base::TimeDelta interval_persistent_fallback; | |
| 125 base::TimeDelta interval_soft_on_usage_event; | |
| 126 base::TimeDelta interval_soft_on_ntp_opened; | |
| 127 }; | |
| 128 | |
| 129 enum class TriggerType; | |
| 130 | |
| 131 // After the call, updates will be scheduled in the future. Idempotent, can be | |
| 132 // run any time later without impacting the current schedule. | |
| 133 // If you want to enforce rescheduling, call Unschedule() and then Schedule(). | |
| 134 void StartScheduling(); | |
| 135 | |
| 136 // After the call, no updates will happen before another call to Schedule(). | |
| 137 // Idempotent, can be run any time later without impacting the current | |
| 138 // schedule. | |
| 139 void StopScheduling(); | |
| 140 | |
| 141 // Trigger a background refetch for the given |trigger| if enabled. | |
| 142 void RefetchInTheBackgroundIfEnabled(TriggerType trigger); | |
| 143 | |
| 144 // Checks whether it is time to perform a soft background fetch, according to | |
| 145 // |schedule|. | |
| 146 bool ShouldRefetchInTheBackgroundNow(TriggerType trigger); | |
| 147 | |
| 148 // Returns whether background fetching (for the given |trigger|) is disabled. | |
| 149 bool BackgroundFetchesDisabled(TriggerType trigger) const; | |
| 150 | |
| 151 // Returns true if quota is available for another request. | |
| 152 bool AcquireQuota(bool interactive_request); | |
| 153 | |
| 154 // Callback after Fetch is completed. | |
| 155 void FetchFinished(const FetchDoneCallback& callback, | |
| 156 Status fetch_status, | |
| 157 std::vector<ContentSuggestion> suggestions); | |
| 158 | |
| 159 // Callback after RefetchInTheBackground is completed. | |
| 160 void RefetchInTheBackgroundFinished( | |
| 161 std::unique_ptr<FetchStatusCallback> callback, | |
| 162 Status fetch_status); | |
| 163 | |
| 164 // Common function to call after a fetch of any type is finished. | |
| 165 void OnFetchCompleted(Status fetch_status); | |
| 166 | |
| 167 // Clears the time of the last fetch so that the provider is ready to make a | |
| 168 // soft fetch at any later time (upon a trigger). | |
| 169 void ClearLastFetchAttemptTime(); | |
| 170 | |
| 171 FetchingSchedule GetDesiredFetchingSchedule() const; | |
| 172 | |
| 173 // Load and store |schedule_|. | |
| 174 void LoadLastFetchingSchedule(); | |
| 175 void StoreFetchingSchedule(); | |
| 176 | |
| 177 // Applies the persistent schedule given by |schedule_|. | |
| 178 void ApplyPersistentFetchingSchedule(); | |
| 179 | |
| 180 // Gets enabled trigger types from the variation parameter. | |
| 181 std::set<TriggerType> GetEnabledTriggerTypes(); | |
| 182 | |
| 183 // Gets trigger types enabled by default. | |
| 184 std::set<TriggerType> GetDefaultEnabledTriggerTypes(); | |
| 185 | |
| 186 // Interface for doing all the actual work (apart from scheduling). | |
| 187 std::unique_ptr<RemoteSuggestionsProvider> provider_; | |
| 188 | |
| 189 // Interface for scheduling hard fetches, OS dependent. Not owned, may be | |
| 190 // null. | |
| 191 PersistentScheduler* persistent_scheduler_; | |
| 192 | |
| 193 FetchingSchedule schedule_; | |
| 194 bool background_fetch_in_progress_; | |
| 195 | |
| 196 // Used to adapt the schedule based on usage activity of the user. Not owned. | |
| 197 const UserClassifier* user_classifier_; | |
| 198 | |
| 199 // Request throttlers for limiting requests for different classes of users. | |
| 200 RequestThrottler request_throttler_rare_ntp_user_; | |
| 201 RequestThrottler request_throttler_active_ntp_user_; | |
| 202 RequestThrottler request_throttler_active_suggestions_consumer_; | |
| 203 | |
| 204 // We should not fetch in background before EULA gets accepted. | |
| 205 std::unique_ptr<EulaState> eula_state_; | |
| 206 | |
| 207 PrefService* profile_prefs_; | |
| 208 std::unique_ptr<base::Clock> clock_; | |
| 209 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> enabled_triggers_; | |
| 210 | |
| 211 base::Time background_fetches_allowed_after_; | |
| 212 | |
| 213 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProvider); | |
| 214 }; | |
| 215 | |
| 216 } // namespace ntp_snippets | |
| 217 | |
| 218 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER
_H_ | |
| OLD | NEW |