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

Unified Diff: components/ntp_snippets/remote/remote_suggestions_scheduler.h

Issue 2557363002: [NTP Snippets] Refactor background scheduling for remote suggestions (Closed)
Patch Set: Marc's comments #1 Created 4 years 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/remote/remote_suggestions_scheduler.h
diff --git a/components/ntp_snippets/remote/remote_suggestions_scheduler.h b/components/ntp_snippets/remote/remote_suggestions_scheduler.h
index bad65c9519b266ec84ec1d2424eaec178f149eb3..7711b6861db4bb600f7c0b30b8d5f698d4d09edb 100644
--- a/components/ntp_snippets/remote/remote_suggestions_scheduler.h
+++ b/components/ntp_snippets/remote/remote_suggestions_scheduler.h
@@ -8,8 +8,14 @@
#include "base/macros.h"
#include "base/time/time.h"
+class PrefRegistrySimple;
+class PrefService;
+
namespace ntp_snippets {
+class RemoteSuggestionsHardScheduler;
+class UserClassifier;
+
// Class to take care of scheduling of periodic updates of snippets. There are
// two types of scheduled updates:
// - "hard" ones that should outlive current running instance of Chrome. These
@@ -22,8 +28,8 @@ class RemoteSuggestionsScheduler {
public:
// Interface to perform the scheduled update.
class Updater {
- virtual void HardUpdate();
- virtual void SoftUpdate();
+ public:
+ virtual void UpdateRemoteSuggestionsBySchedule();
};
// The passed in |updater| is called when an update is due according to the
@@ -31,31 +37,66 @@ class RemoteSuggestionsScheduler {
// ContentSuggestionService because the concrete instance passed to
// RemoteSuggestionsScheduler when the hard fetch was scheduled may not exist
// any more when the hard update is due.
- explicit RemoteSuggestionsScheduler(Updater* updater);
+ explicit RemoteSuggestionsScheduler(
+ RemoteSuggestionsHardScheduler* hard_scheduler,
+ const UserClassifier* user_classifier,
+ PrefService* pref_service);
+
+ ~RemoteSuggestionsScheduler();
+
+ // Sets the object responsible for performing the scheduled updates. If an
+ // update is due before an updater is specified by this function, the update
+ // is skipped.
+ void SetUpdater(Updater* updater);
- // Schedules both "soft" and "hard" fetches. First removes existing schedule
- // before scheduling new updates.
+ // One of the following two functions must be called on startup of Chrome for
+ // the scheduler to work correctly.
+ // After the call, updates will be scheduled in the future. Idempotent, can be
+ // run any time later without impacting the current schedule.
+ // If you want to enforce rescheduling, call Unschedule() and then Schedule().
void Schedule();
- // Removes any existing schedule.
+ // After the call, no updates will happen before another call to Schedule().
+ // Idempotent, can be run any time later without impacting the current
+ // schedule.
void Unschedule();
- // Schedule periodic fetching of snippets, with different periods depending on
- // network state. Once per period, the concrete implementation should call
- // RemoteSuggestionsUpdater::HardUpdate where RemoteSuggestionsUpdater is
- // obtained from ContentSuggestionsService.
- // Any of the periods can be zero to indicate that the corresponding task
- // should not be scheduled.
- virtual bool Schedule(base::TimeDelta period_wifi,
- base::TimeDelta period_fallback) = 0;
+ // Must be called by the OS dependent hard scheduler when hard update is due.
+ // Initiates the hard update.
+ void PerformHardUpdate();
- // Cancel any scheduled tasks.
- virtual bool Unschedule() = 0;
+ // Must be called after an update successfully finishes (no matter whether the
+ // scheduler is currently active or not active).
+ void OnSuccessfulUpdate();
+
+ static void RegisterProfilePrefs(PrefRegistrySimple* registry);
private:
- DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsHardScheduler);
+ struct UpdateSchedule {
+ base::TimeDelta interval_wifi;
+ base::TimeDelta interval_fallback;
+ };
+
+ void ResetUpdateScheduleFromNow(UpdateSchedule schedule);
+
+ UpdateSchedule GetCurrentUpdateSchedule();
+ UpdateSchedule GetLastUpdateSchedule();
+ void StoreLastUpdateSchedule(UpdateSchedule schedule);
+
+ // Interface for scheduling hard fetches, OS dependent. Not owned.
+ RemoteSuggestionsHardScheduler* hard_scheduler_;
+
+ // Interface for doing the actual updates, when they are due. Not owned.
+ Updater* updater_;
+
+ // Interface for doing the actual updates, when they are due. Not owned.
+ const UserClassifier* user_classifier_;
+
+ PrefService* pref_service_;
+
+ DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsScheduler);
};
} // namespace ntp_snippets
-#endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_HARD_SCHEDULER_H_
+#endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_H_

Powered by Google App Engine
This is Rietveld 408576698