variations_request_scheduler.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2013 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_VARIATIONS_VARIATIONS_REQUEST_SCHEDULER_H_
  5. #define COMPONENTS_VARIATIONS_VARIATIONS_REQUEST_SCHEDULER_H_
  6. #include "base/bind.h"
  7. #include "base/component_export.h"
  8. #include "base/gtest_prod_util.h"
  9. #include "base/time/time.h"
  10. #include "base/timer/timer.h"
  11. class PrefService;
  12. namespace variations {
  13. // A helper class that makes VariationsService requests at the correct times.
  14. class COMPONENT_EXPORT(VARIATIONS) VariationsRequestScheduler {
  15. public:
  16. VariationsRequestScheduler(const VariationsRequestScheduler&) = delete;
  17. VariationsRequestScheduler& operator=(const VariationsRequestScheduler&) =
  18. delete;
  19. virtual ~VariationsRequestScheduler();
  20. // Starts the task. This can be a repeated event or a one-off.
  21. virtual void Start();
  22. // Resets the scheduler if it is currently on a timer.
  23. virtual void Reset();
  24. // Schedules a fetch shortly, for example to re-try the initial request which
  25. // may have failed.
  26. void ScheduleFetchShortly();
  27. // Called when the application has been foregrounded. This may fetch a new
  28. // seed.
  29. virtual void OnAppEnterForeground();
  30. // Factory method for this class.
  31. static VariationsRequestScheduler* Create(const base::RepeatingClosure& task,
  32. PrefService* local_state);
  33. protected:
  34. // |task| is the closure to call when the scheduler deems ready.
  35. explicit VariationsRequestScheduler(const base::RepeatingClosure& task);
  36. // Returns the time interval between variations seed fetches.
  37. base::TimeDelta GetFetchPeriod() const;
  38. // Getter for derived classes.
  39. base::RepeatingClosure task() const;
  40. private:
  41. FRIEND_TEST_ALL_PREFIXES(VariationsRequestSchedulerTest,
  42. ScheduleFetchShortly);
  43. // The task scheduled by this class.
  44. base::RepeatingClosure task_;
  45. // The timer used to repeatedly ping the server. Keep this as an instance
  46. // member so if VariationsRequestScheduler goes out of scope, the timer is
  47. // automatically canceled.
  48. base::RepeatingTimer timer_;
  49. // A one-shot timer used for scheduling out-of-band fetches.
  50. base::OneShotTimer one_shot_timer_;
  51. };
  52. } // namespace variations
  53. #endif // COMPONENTS_VARIATIONS_VARIATIONS_REQUEST_SCHEDULER_H_