remote_suggestions_scheduler_impl.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_IMPL_H_
  5. #define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_IMPL_H_
  6. #include <memory>
  7. #include <set>
  8. #include <utility>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/time/clock.h"
  11. #include "base/time/time.h"
  12. #include "components/ntp_snippets/content_suggestions_provider.h"
  13. #include "components/ntp_snippets/remote/persistent_scheduler.h"
  14. #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h"
  15. #include "components/ntp_snippets/remote/request_throttler.h"
  16. #include "components/web_resource/eula_accepted_notifier.h"
  17. class PrefRegistrySimple;
  18. class PrefService;
  19. namespace base {
  20. class Clock;
  21. }
  22. namespace ntp_snippets {
  23. struct Status;
  24. class EulaState;
  25. class UserClassifier;
  26. // A client of RemoteSuggestionsProvider that introduces periodic fetching.
  27. class RemoteSuggestionsSchedulerImpl : public RemoteSuggestionsScheduler {
  28. public:
  29. RemoteSuggestionsSchedulerImpl(PersistentScheduler* persistent_scheduler,
  30. const UserClassifier* user_classifier,
  31. PrefService* profile_prefs,
  32. PrefService* local_state_prefs,
  33. base::Clock* clock);
  34. RemoteSuggestionsSchedulerImpl(const RemoteSuggestionsSchedulerImpl&) =
  35. delete;
  36. RemoteSuggestionsSchedulerImpl& operator=(
  37. const RemoteSuggestionsSchedulerImpl&) = delete;
  38. ~RemoteSuggestionsSchedulerImpl() override;
  39. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  40. // RemoteSuggestionsScheduler implementation.
  41. void SetProvider(RemoteSuggestionsProvider* provider) override;
  42. void OnProviderActivated() override;
  43. void OnProviderDeactivated() override;
  44. void OnSuggestionsCleared() override;
  45. void OnHistoryCleared() override;
  46. void OnBrowserUpgraded() override;
  47. bool AcquireQuotaForInteractiveFetch() override;
  48. void OnInteractiveFetchFinished(Status fetch_status) override;
  49. void OnPersistentSchedulerWakeUp() override;
  50. void OnBrowserForegrounded() override;
  51. void OnBrowserColdStart() override;
  52. void OnSuggestionsSurfaceOpened() override;
  53. private:
  54. // Abstract description of the fetching schedule. See the enum
  55. // FetchingInterval for more documentation.
  56. struct FetchingSchedule {
  57. static FetchingSchedule Empty();
  58. bool operator==(const FetchingSchedule& other) const;
  59. bool operator!=(const FetchingSchedule& other) const;
  60. bool is_empty() const;
  61. // Interval since the last successful fetch after which to consider the
  62. // current content stale.
  63. base::TimeDelta GetStalenessInterval() const;
  64. // Intervals since the last fetch attempt after which to fetch again
  65. // (depending on the trigger and connectivity).
  66. base::TimeDelta interval_persistent_wifi;
  67. base::TimeDelta interval_persistent_fallback;
  68. base::TimeDelta interval_startup_wifi;
  69. base::TimeDelta interval_startup_fallback;
  70. base::TimeDelta interval_shown_wifi;
  71. base::TimeDelta interval_shown_fallback;
  72. };
  73. enum class TriggerType;
  74. // After the call, updates will be scheduled in the future. Idempotent, can be
  75. // run any time later without impacting the current schedule.
  76. // If you want to enforce rescheduling, call Unschedule() and then Schedule().
  77. void StartScheduling();
  78. // After the call, no updates will happen before another call to Schedule().
  79. // Idempotent, can be run any time later without impacting the current
  80. // schedule.
  81. void StopScheduling();
  82. bool IsLastSuccessfulFetchStale() const;
  83. // Trigger a background refetch for the given |trigger| if enabled and if the
  84. // timing is appropriate for another fetch.
  85. void RefetchIfAppropriate(TriggerType trigger);
  86. // Checks whether it is time to perform a soft background fetch for |trigger|,
  87. // according to |schedule|.
  88. bool ShouldRefetchNow(base::Time last_fetch_attempt_time,
  89. TriggerType trigger);
  90. // Returns whether all components are ready for background fetches.
  91. bool IsReadyForBackgroundFetches() const;
  92. // Runs any queued triggers if the system is ready for background fetches.
  93. void RunQueuedTriggersIfReady();
  94. // Returns true if quota is available for another request.
  95. bool AcquireQuota(bool interactive_request);
  96. // Callback after Refetch is completed.
  97. void RefetchFinished(Status fetch_status);
  98. // Common function to call after a fetch of any type is finished.
  99. void OnFetchCompleted(Status fetch_status);
  100. // Clears the time of the last fetch so that the provider is ready to make a
  101. // soft fetch at any later time (upon a trigger), treating the last fetch as
  102. // stale.
  103. void ClearLastFetchAttemptTime();
  104. FetchingSchedule GetDesiredFetchingSchedule() const;
  105. // Load and store |schedule_|.
  106. void LoadLastFetchingSchedule();
  107. void StoreFetchingSchedule();
  108. // Applies the persistent schedule given by |schedule_|.
  109. void ApplyPersistentFetchingSchedule();
  110. // Gets enabled trigger types from the variation parameter.
  111. std::set<TriggerType> GetEnabledTriggerTypes();
  112. // Gets trigger types enabled by default.
  113. std::set<TriggerType> GetDefaultEnabledTriggerTypes();
  114. // Interface for scheduling hard fetches, OS dependent. Not owned, may be
  115. // null.
  116. raw_ptr<PersistentScheduler> persistent_scheduler_;
  117. // Interface for doing all the actual work (apart from scheduling). Not owned.
  118. raw_ptr<RemoteSuggestionsProvider> provider_;
  119. FetchingSchedule schedule_;
  120. bool background_fetch_in_progress_;
  121. // Used to adapt the schedule based on usage activity of the user. Not owned.
  122. raw_ptr<const UserClassifier> user_classifier_;
  123. // Request throttlers for limiting requests for different classes of users.
  124. RequestThrottler request_throttler_rare_ntp_user_;
  125. RequestThrottler request_throttler_active_ntp_user_;
  126. RequestThrottler request_throttler_active_suggestions_consumer_;
  127. // Variables to make sure we only report the first trigger of each kind to
  128. // UMA.
  129. bool time_until_first_shown_trigger_reported_;
  130. bool time_until_first_startup_trigger_reported_;
  131. // We should not fetch in background before EULA gets accepted.
  132. std::unique_ptr<EulaState> eula_state_;
  133. raw_ptr<PrefService> profile_prefs_;
  134. raw_ptr<base::Clock> clock_;
  135. std::set<TriggerType> enabled_triggers_;
  136. std::set<TriggerType> queued_triggers_;
  137. base::Time background_fetches_allowed_after_;
  138. };
  139. } // namespace ntp_snippets
  140. #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_SCHEDULER_IMPL_H_