Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(301)

Side by Side Diff: components/ntp_snippets/remote/remote_suggestions_scheduler_impl.h

Issue 2774663002: [Remote suggestions] Refactor the scheduler (Closed)
Patch Set: Add missing files & make it compile Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/time.h" 15 #include "base/time/time.h"
16 #include "components/ntp_snippets/content_suggestions_provider.h" 16 #include "components/ntp_snippets/content_suggestions_provider.h"
(...skipping 10 matching lines...) Expand all
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
60 class SchedulingRemoteSuggestionsProvider final 53 class RemoteSuggestionsSchedulerImpl : public RemoteSuggestionsScheduler {
61 : public RemoteSuggestionsProvider,
62 public RemoteSuggestionsScheduler {
63 public: 54 public:
64 SchedulingRemoteSuggestionsProvider( 55 RemoteSuggestionsSchedulerImpl(RemoteSuggestionsProvider* provider,
65 Observer* observer, 56 PersistentScheduler* persistent_scheduler,
66 std::unique_ptr<RemoteSuggestionsProvider> provider, 57 const UserClassifier* user_classifier,
67 PersistentScheduler* persistent_scheduler, 58 PrefService* profile_prefs,
68 const UserClassifier* user_classifier, 59 PrefService* local_state_prefs,
69 PrefService* profile_prefs, 60 std::unique_ptr<base::Clock> clock);
70 PrefService* local_state_prefs,
71 std::unique_ptr<base::Clock> clock);
72 61
73 ~SchedulingRemoteSuggestionsProvider() override; 62 ~RemoteSuggestionsSchedulerImpl();
74 63
75 static void RegisterProfilePrefs(PrefRegistrySimple* registry); 64 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
76 65
77 // RemoteSuggestionsScheduler implementation. 66 // RemoteSuggestionsScheduler implementation.
78 void OnProviderActivated() override; 67 void OnProviderActivated() override;
79 void OnProviderDeactivated() override; 68 void OnProviderDeactivated() override;
80 void OnSuggestionsCleared() override; 69 void OnSuggestionsCleared() override;
81 void OnHistoryCleared() override; 70 void OnHistoryCleared() override;
82 void RescheduleFetching() override; 71 void RescheduleFetching() override;
72 bool AcquireQuotaForInteractiveFetch() override;
73 void OnInteractiveFetchFinished(Status fetch_status) override;
83 void OnPersistentSchedulerWakeUp() override; 74 void OnPersistentSchedulerWakeUp() override;
84 void OnBrowserForegrounded() override; 75 void OnBrowserForegrounded() override;
85 void OnBrowserColdStart() override; 76 void OnBrowserColdStart() override;
86 void OnNTPOpened() override; 77 void OnNTPOpened() override;
87 78
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: 79 private:
116 // Abstract description of the fetching schedule. 80 // Abstract description of the fetching schedule.
117 struct FetchingSchedule { 81 struct FetchingSchedule {
118 static FetchingSchedule Empty(); 82 static FetchingSchedule Empty();
119 bool operator==(const FetchingSchedule& other) const; 83 bool operator==(const FetchingSchedule& other) const;
120 bool operator!=(const FetchingSchedule& other) const; 84 bool operator!=(const FetchingSchedule& other) const;
121 bool is_empty() const; 85 bool is_empty() const;
122 86
123 base::TimeDelta interval_persistent_wifi; 87 base::TimeDelta interval_persistent_wifi;
124 base::TimeDelta interval_persistent_fallback; 88 base::TimeDelta interval_persistent_fallback;
125 base::TimeDelta interval_soft_on_usage_event; 89 base::TimeDelta interval_soft_on_usage_event;
126 base::TimeDelta interval_soft_on_ntp_opened; 90 base::TimeDelta interval_soft_on_ntp_opened;
127 }; 91 };
128 92
129 enum class TriggerType; 93 enum class TriggerType;
130 94
131 // After the call, updates will be scheduled in the future. Idempotent, can be 95 // After the call, updates will be scheduled in the future. Idempotent, can be
132 // run any time later without impacting the current schedule. 96 // run any time later without impacting the current schedule.
133 // If you want to enforce rescheduling, call Unschedule() and then Schedule(). 97 // If you want to enforce rescheduling, call Unschedule() and then Schedule().
134 void StartScheduling(); 98 void StartScheduling();
135 99
136 // After the call, no updates will happen before another call to Schedule(). 100 // After the call, no updates will happen before another call to Schedule().
137 // Idempotent, can be run any time later without impacting the current 101 // Idempotent, can be run any time later without impacting the current
138 // schedule. 102 // schedule.
139 void StopScheduling(); 103 void StopScheduling();
140 104
141 // Trigger a background refetch for the given |trigger| if enabled. 105 // Trigger a background refetch for the given |trigger| if enabled.
142 void RefetchInTheBackgroundIfEnabled(TriggerType trigger); 106 void RefetchInTheBackgroundIfEnabled(TriggerType trigger);
143 107
108 // Trigger the background refetch.
109 void RefetchInTheBackground();
110
144 // Checks whether it is time to perform a soft background fetch, according to 111 // Checks whether it is time to perform a soft background fetch, according to
145 // |schedule|. 112 // |schedule|.
146 bool ShouldRefetchInTheBackgroundNow(TriggerType trigger); 113 bool ShouldRefetchInTheBackgroundNow(TriggerType trigger);
147 114
148 // Returns whether background fetching (for the given |trigger|) is disabled. 115 // Returns whether background fetching (for the given |trigger|) is disabled.
149 bool BackgroundFetchesDisabled(TriggerType trigger) const; 116 bool BackgroundFetchesDisabled(TriggerType trigger) const;
150 117
151 // Returns true if quota is available for another request. 118 // Returns true if quota is available for another request.
152 bool AcquireQuota(bool interactive_request); 119 bool AcquireQuota(bool interactive_request);
153 120
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. 121 // Callback after RefetchInTheBackground is completed.
160 void RefetchInTheBackgroundFinished( 122 void RefetchInTheBackgroundFinished(Status fetch_status);
161 std::unique_ptr<FetchStatusCallback> callback,
162 Status fetch_status);
163 123
164 // Common function to call after a fetch of any type is finished. 124 // Common function to call after a fetch of any type is finished.
165 void OnFetchCompleted(Status fetch_status); 125 void OnFetchCompleted(Status fetch_status);
166 126
167 // Clears the time of the last fetch so that the provider is ready to make a 127 // 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). 128 // soft fetch at any later time (upon a trigger).
169 void ClearLastFetchAttemptTime(); 129 void ClearLastFetchAttemptTime();
170 130
171 FetchingSchedule GetDesiredFetchingSchedule() const; 131 FetchingSchedule GetDesiredFetchingSchedule() const;
172 132
173 // Load and store |schedule_|. 133 // Load and store |schedule_|.
174 void LoadLastFetchingSchedule(); 134 void LoadLastFetchingSchedule();
175 void StoreFetchingSchedule(); 135 void StoreFetchingSchedule();
176 136
177 // Applies the persistent schedule given by |schedule_|. 137 // Applies the persistent schedule given by |schedule_|.
178 void ApplyPersistentFetchingSchedule(); 138 void ApplyPersistentFetchingSchedule();
179 139
180 // Gets enabled trigger types from the variation parameter. 140 // Gets enabled trigger types from the variation parameter.
181 std::set<TriggerType> GetEnabledTriggerTypes(); 141 std::set<TriggerType> GetEnabledTriggerTypes();
182 142
183 // Gets trigger types enabled by default. 143 // Gets trigger types enabled by default.
184 std::set<TriggerType> GetDefaultEnabledTriggerTypes(); 144 std::set<TriggerType> GetDefaultEnabledTriggerTypes();
185 145
186 // Interface for doing all the actual work (apart from scheduling). 146 // Interface for doing all the actual work (apart from scheduling).
187 std::unique_ptr<RemoteSuggestionsProvider> provider_; 147 RemoteSuggestionsProvider* provider_;
188 148
189 // Interface for scheduling hard fetches, OS dependent. Not owned, may be 149 // Interface for scheduling hard fetches, OS dependent. Not owned, may be
190 // null. 150 // null.
191 PersistentScheduler* persistent_scheduler_; 151 PersistentScheduler* persistent_scheduler_;
192 152
193 FetchingSchedule schedule_; 153 FetchingSchedule schedule_;
194 bool background_fetch_in_progress_; 154 bool background_fetch_in_progress_;
195 155
196 // Used to adapt the schedule based on usage activity of the user. Not owned. 156 // Used to adapt the schedule based on usage activity of the user. Not owned.
197 const UserClassifier* user_classifier_; 157 const UserClassifier* user_classifier_;
198 158
199 // Request throttlers for limiting requests for different classes of users. 159 // Request throttlers for limiting requests for different classes of users.
200 RequestThrottler request_throttler_rare_ntp_user_; 160 RequestThrottler request_throttler_rare_ntp_user_;
201 RequestThrottler request_throttler_active_ntp_user_; 161 RequestThrottler request_throttler_active_ntp_user_;
202 RequestThrottler request_throttler_active_suggestions_consumer_; 162 RequestThrottler request_throttler_active_suggestions_consumer_;
203 163
204 // We should not fetch in background before EULA gets accepted. 164 // We should not fetch in background before EULA gets accepted.
205 std::unique_ptr<EulaState> eula_state_; 165 std::unique_ptr<EulaState> eula_state_;
206 166
207 PrefService* profile_prefs_; 167 PrefService* profile_prefs_;
208 std::unique_ptr<base::Clock> clock_; 168 std::unique_ptr<base::Clock> clock_;
209 std::set<SchedulingRemoteSuggestionsProvider::TriggerType> enabled_triggers_; 169 std::set<RemoteSuggestionsSchedulerImpl::TriggerType> enabled_triggers_;
210 170
211 base::Time background_fetches_allowed_after_; 171 base::Time background_fetches_allowed_after_;
212 172
213 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProvider); 173 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsSchedulerImpl);
214 }; 174 };
215 175
216 } // namespace ntp_snippets 176 } // namespace ntp_snippets
217 177
218 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER _H_ 178 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698