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