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

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

Issue 2557363002: [NTP Snippets] Refactor background scheduling for remote suggestions (Closed)
Patch Set: 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 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_REMOTE_SUGGESTIONS_SCHEDULER_H_ 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_H_
6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_H_ 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_H_
7 7
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/time/time.h" 9 #include "base/time/time.h"
10 10
11 class PrefRegistrySimple;
12 class PrefService;
13
11 namespace ntp_snippets { 14 namespace ntp_snippets {
12 15
16 class RemoteSuggestionsHardScheduler;
17 class UserClassifier;
18
13 // Class to take care of scheduling of periodic updates of snippets. There are 19 // Class to take care of scheduling of periodic updates of snippets. There are
14 // two types of scheduled updates: 20 // two types of scheduled updates:
15 // - "hard" ones that should outlive current running instance of Chrome. These 21 // - "hard" ones that should outlive current running instance of Chrome. These
16 // should get triggered according to their schedule even if Chrome is not 22 // should get triggered according to their schedule even if Chrome is not
17 // running at the given moment. This is OS-dependent, may be unavilable on 23 // running at the given moment. This is OS-dependent, may be unavilable on
18 // some platforms. 24 // some platforms.
19 // - "soft" ones that get triggered only if Chrome stays running until the 25 // - "soft" ones that get triggered only if Chrome stays running until the
20 // scheduled point. 26 // scheduled point.
21 class RemoteSuggestionsScheduler { 27 class RemoteSuggestionsScheduler {
22 public: 28 public:
23 // Interface to perform the scheduled update. 29 // Interface to perform the scheduled update.
24 class Updater { 30 class Updater {
25 virtual void HardUpdate(); 31 public:
26 virtual void SoftUpdate(); 32 virtual void UpdateRemoteSuggestionsBySchedule();
Marc Treib 2016/12/08 16:14:58 Also here: There's some part of the patch missing.
27 }; 33 };
28 34
29 // The passed in |updater| is called when an update is due according to the 35 // The passed in |updater| is called when an update is due according to the
30 // schedule. Note that hard fetches get access to the |updater| via the keyed 36 // schedule. Note that hard fetches get access to the |updater| via the keyed
31 // ContentSuggestionService because the concrete instance passed to 37 // ContentSuggestionService because the concrete instance passed to
32 // RemoteSuggestionsScheduler when the hard fetch was scheduled may not exist 38 // RemoteSuggestionsScheduler when the hard fetch was scheduled may not exist
33 // any more when the hard update is due. 39 // any more when the hard update is due.
34 explicit RemoteSuggestionsScheduler(Updater* updater); 40 explicit RemoteSuggestionsScheduler(
41 RemoteSuggestionsHardScheduler* hard_scheduler,
42 Updater* updater,
43 const UserClassifier* user_classifier,
44 PrefService* pref_service);
35 45
36 // Schedules both "soft" and "hard" fetches. First removes existing schedule 46 ~RemoteSuggestionsScheduler();
37 // before scheduling new updates. 47
48 // One of the following two functions must be called on startup of Chrome for
49 // the scheduler to work correctly.
50 // After the call, updates will be scheduled in the future. Idempotent, can be
51 // run any time later without impacting the current schedule.
52 // If you want to enforce rescheduling, call Unschedule() and then Schedule().
38 void Schedule(); 53 void Schedule();
39 54
40 // Removes any existing schedule. 55 // After the call, no updates will happen before another call to Schedule().
56 // Idempotent, can be run any time later without impacting the current
57 // schedule.
41 void Unschedule(); 58 void Unschedule();
42 59
43 // Schedule periodic fetching of snippets, with different periods depending on 60 // Must be called by the OS dependent hard scheduler when hard update is due.
44 // network state. Once per period, the concrete implementation should call 61 // Initiates the hard update.
45 // RemoteSuggestionsUpdater::HardUpdate where RemoteSuggestionsUpdater is 62 void PerformHardUpdate();
46 // obtained from ContentSuggestionsService.
47 // Any of the periods can be zero to indicate that the corresponding task
48 // should not be scheduled.
49 virtual bool Schedule(base::TimeDelta period_wifi,
50 base::TimeDelta period_fallback) = 0;
51 63
52 // Cancel any scheduled tasks. 64 // Must be called after an update successfully finishes (no matter whether the
53 virtual bool Unschedule() = 0; 65 // scheduler is currently active or not active).
66 void OnSuccessfulUpdate();
67
68 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
54 69
55 private: 70 private:
56 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsHardScheduler); 71 struct UpdateSchedule {
72 base::TimeDelta interval_wifi;
73 base::TimeDelta interval_fallback;
74 };
75
76 void ResetUpdateScheduleFromNow(UpdateSchedule schedule);
77
78 UpdateSchedule GetCurrentUpdateSchedule();
79 UpdateSchedule GetLastUpdateSchedule();
80 void StoreLastUpdateSchedule(UpdateSchedule schedule);
81
82 // Interface for scheduling hard fetches, OS dependent. Not owned.
83 RemoteSuggestionsHardScheduler* hard_scheduler_;
84
85 // Interface for doing the actual updates, when they are due. Not owned.
86 Updater* updater_;
87
88 // Interface for doing the actual updates, when they are due. Not owned.
89 const UserClassifier* user_classifier_;
90
91 PrefService* pref_service_;
92
93 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsScheduler);
57 }; 94 };
58 95
59 } // namespace ntp_snippets 96 } // namespace ntp_snippets
60 97
61 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_HARD_SCHEDULER_H_ 98 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698