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

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

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

Powered by Google App Engine
This is Rietveld 408576698