blob: 2b945010690116ec01e8eda6e7912c29f5fb97ed [file] [log] [blame]
[email protected]fc12e0b2013-03-27 18:06:261// 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
blundell5f9d1422015-08-17 15:30:215#ifndef COMPONENTS_VARIATIONS_VARIATIONS_REQUEST_SCHEDULER_H_
6#define COMPONENTS_VARIATIONS_VARIATIONS_REQUEST_SCHEDULER_H_
[email protected]fc12e0b2013-03-27 18:06:267
8#include "base/bind.h"
[email protected]e266cd92013-06-20 17:02:029#include "base/gtest_prod_util.h"
avi5dd91f82015-12-25 22:30:4610#include "base/macros.h"
[email protected]84813472013-06-28 00:25:1911#include "base/time/time.h"
12#include "base/timer/timer.h"
[email protected]fc12e0b2013-03-27 18:06:2613
[email protected]d00c07e2013-04-09 21:53:5814class PrefService;
15
blundell57bcfed2015-09-04 08:44:4516namespace variations {
[email protected]fc12e0b2013-03-27 18:06:2617
18// A helper class that makes VariationsService requests at the correct times.
19class VariationsRequestScheduler {
20 public:
[email protected]fc12e0b2013-03-27 18:06:2621 virtual ~VariationsRequestScheduler();
22
[email protected]de71a372014-02-15 05:14:2323 // Starts the task. This can be a repeated event or a one-off.
[email protected]d00c07e2013-04-09 21:53:5824 virtual void Start();
25
[email protected]fc12e0b2013-03-27 18:06:2626 // Resets the scheduler if it is currently on a timer.
[email protected]d00c07e2013-04-09 21:53:5827 virtual void Reset();
28
[email protected]348201f2013-06-11 17:24:2329 // Schedules a fetch shortly, for example to re-try the initial request which
30 // may have failed.
31 void ScheduleFetchShortly();
32
[email protected]de71a372014-02-15 05:14:2333 // Called when the application has been foregrounded. This may fetch a new
34 // seed.
35 virtual void OnAppEnterForeground();
36
[email protected]d00c07e2013-04-09 21:53:5837 // Factory method for this class.
38 static VariationsRequestScheduler* Create(const base::Closure& task,
39 PrefService* local_state);
40
41 protected:
42 // |task| is the closure to call when the scheduler deems ready.
43 explicit VariationsRequestScheduler(const base::Closure& task);
44
asvitkinea28fbc92015-08-25 20:21:4245 // Returns the time interval between variations seed fetches.
46 base::TimeDelta GetFetchPeriod() const;
47
[email protected]d00c07e2013-04-09 21:53:5848 // Getter for derived classes.
49 base::Closure task() const;
[email protected]fc12e0b2013-03-27 18:06:2650
51 private:
[email protected]e266cd92013-06-20 17:02:0252 FRIEND_TEST_ALL_PREFIXES(VariationsRequestSchedulerTest,
53 ScheduleFetchShortly);
54
[email protected]fc12e0b2013-03-27 18:06:2655 // The task scheduled by this class.
56 base::Closure task_;
57
58 // The timer used to repeatedly ping the server. Keep this as an instance
59 // member so if VariationsRequestScheduler goes out of scope, the timer is
60 // automatically canceled.
danakj8c3eb802015-09-24 07:53:0061 base::RepeatingTimer timer_;
[email protected]d00c07e2013-04-09 21:53:5862
[email protected]348201f2013-06-11 17:24:2363 // A one-shot timer used for scheduling out-of-band fetches.
danakj8c3eb802015-09-24 07:53:0064 base::OneShotTimer one_shot_timer_;
[email protected]348201f2013-06-11 17:24:2365
[email protected]d00c07e2013-04-09 21:53:5866 DISALLOW_COPY_AND_ASSIGN(VariationsRequestScheduler);
[email protected]fc12e0b2013-03-27 18:06:2667};
68
blundell57bcfed2015-09-04 08:44:4569} // namespace variations
[email protected]fc12e0b2013-03-27 18:06:2670
blundell5f9d1422015-08-17 15:30:2171#endif // COMPONENTS_VARIATIONS_VARIATIONS_REQUEST_SCHEDULER_H_