| 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_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(); |
| 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 const UserClassifier* user_classifier, |
| 43 PrefService* pref_service); |
| 35 | 44 |
| 36 // Schedules both "soft" and "hard" fetches. First removes existing schedule | 45 ~RemoteSuggestionsScheduler(); |
| 37 // before scheduling new updates. | 46 |
| 47 // Sets the object responsible for performing the scheduled updates. If an |
| 48 // update is due before an updater is specified by this function, the update |
| 49 // is skipped. |
| 50 void SetUpdater(Updater* updater); |
| 51 |
| 52 // One of the following two functions must be called on startup of Chrome for |
| 53 // the scheduler to work correctly. |
| 54 // After the call, updates will be scheduled in the future. Idempotent, can be |
| 55 // run any time later without impacting the current schedule. |
| 56 // If you want to enforce rescheduling, call Unschedule() and then Schedule(). |
| 38 void Schedule(); | 57 void Schedule(); |
| 39 | 58 |
| 40 // Removes any existing schedule. | 59 // After the call, no updates will happen before another call to Schedule(). |
| 60 // Idempotent, can be run any time later without impacting the current |
| 61 // schedule. |
| 41 void Unschedule(); | 62 void Unschedule(); |
| 42 | 63 |
| 43 // Schedule periodic fetching of snippets, with different periods depending on | 64 // 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 | 65 // Initiates the hard update. |
| 45 // RemoteSuggestionsUpdater::HardUpdate where RemoteSuggestionsUpdater is | 66 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 | 67 |
| 52 // Cancel any scheduled tasks. | 68 // Must be called after an update successfully finishes (no matter whether the |
| 53 virtual bool Unschedule() = 0; | 69 // scheduler is currently active or not active). |
| 70 void OnSuccessfulUpdate(); |
| 71 |
| 72 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 54 | 73 |
| 55 private: | 74 private: |
| 56 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsHardScheduler); | 75 struct UpdateSchedule { |
| 76 base::TimeDelta interval_wifi; |
| 77 base::TimeDelta interval_fallback; |
| 78 }; |
| 79 |
| 80 void ResetUpdateScheduleFromNow(UpdateSchedule schedule); |
| 81 |
| 82 UpdateSchedule GetCurrentUpdateSchedule(); |
| 83 UpdateSchedule GetLastUpdateSchedule(); |
| 84 void StoreLastUpdateSchedule(UpdateSchedule schedule); |
| 85 |
| 86 // Interface for scheduling hard fetches, OS dependent. Not owned. |
| 87 RemoteSuggestionsHardScheduler* hard_scheduler_; |
| 88 |
| 89 // Interface for doing the actual updates, when they are due. Not owned. |
| 90 Updater* updater_; |
| 91 |
| 92 // Interface for doing the actual updates, when they are due. Not owned. |
| 93 const UserClassifier* user_classifier_; |
| 94 |
| 95 PrefService* pref_service_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsScheduler); |
| 57 }; | 98 }; |
| 58 | 99 |
| 59 } // namespace ntp_snippets | 100 } // namespace ntp_snippets |
| 60 | 101 |
| 61 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_HARD_SCHEDULER_H_ | 102 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_H_ |
| OLD | NEW |