[email protected] | fc12e0b | 2013-03-27 18:06:26 | [diff] [blame] | 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 | |||||
blundell | 5f9d142 | 2015-08-17 15:30:21 | [diff] [blame] | 5 | #ifndef COMPONENTS_VARIATIONS_VARIATIONS_REQUEST_SCHEDULER_H_ |
6 | #define COMPONENTS_VARIATIONS_VARIATIONS_REQUEST_SCHEDULER_H_ | ||||
[email protected] | fc12e0b | 2013-03-27 18:06:26 | [diff] [blame] | 7 | |
8 | #include "base/bind.h" | ||||
[email protected] | e266cd9 | 2013-06-20 17:02:02 | [diff] [blame] | 9 | #include "base/gtest_prod_util.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | 8481347 | 2013-06-28 00:25:19 | [diff] [blame] | 11 | #include "base/time/time.h" |
12 | #include "base/timer/timer.h" | ||||
[email protected] | fc12e0b | 2013-03-27 18:06:26 | [diff] [blame] | 13 | |
[email protected] | d00c07e | 2013-04-09 21:53:58 | [diff] [blame] | 14 | class PrefService; |
15 | |||||
blundell | 57bcfed | 2015-09-04 08:44:45 | [diff] [blame] | 16 | namespace variations { |
[email protected] | fc12e0b | 2013-03-27 18:06:26 | [diff] [blame] | 17 | |
18 | // A helper class that makes VariationsService requests at the correct times. | ||||
19 | class VariationsRequestScheduler { | ||||
20 | public: | ||||
[email protected] | fc12e0b | 2013-03-27 18:06:26 | [diff] [blame] | 21 | virtual ~VariationsRequestScheduler(); |
22 | |||||
[email protected] | de71a37 | 2014-02-15 05:14:23 | [diff] [blame] | 23 | // Starts the task. This can be a repeated event or a one-off. |
[email protected] | d00c07e | 2013-04-09 21:53:58 | [diff] [blame] | 24 | virtual void Start(); |
25 | |||||
[email protected] | fc12e0b | 2013-03-27 18:06:26 | [diff] [blame] | 26 | // Resets the scheduler if it is currently on a timer. |
[email protected] | d00c07e | 2013-04-09 21:53:58 | [diff] [blame] | 27 | virtual void Reset(); |
28 | |||||
[email protected] | 348201f | 2013-06-11 17:24:23 | [diff] [blame] | 29 | // Schedules a fetch shortly, for example to re-try the initial request which |
30 | // may have failed. | ||||
31 | void ScheduleFetchShortly(); | ||||
32 | |||||
[email protected] | de71a37 | 2014-02-15 05:14:23 | [diff] [blame] | 33 | // Called when the application has been foregrounded. This may fetch a new |
34 | // seed. | ||||
35 | virtual void OnAppEnterForeground(); | ||||
36 | |||||
[email protected] | d00c07e | 2013-04-09 21:53:58 | [diff] [blame] | 37 | // 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 | |||||
asvitkine | a28fbc9 | 2015-08-25 20:21:42 | [diff] [blame] | 45 | // Returns the time interval between variations seed fetches. |
46 | base::TimeDelta GetFetchPeriod() const; | ||||
47 | |||||
[email protected] | d00c07e | 2013-04-09 21:53:58 | [diff] [blame] | 48 | // Getter for derived classes. |
49 | base::Closure task() const; | ||||
[email protected] | fc12e0b | 2013-03-27 18:06:26 | [diff] [blame] | 50 | |
51 | private: | ||||
[email protected] | e266cd9 | 2013-06-20 17:02:02 | [diff] [blame] | 52 | FRIEND_TEST_ALL_PREFIXES(VariationsRequestSchedulerTest, |
53 | ScheduleFetchShortly); | ||||
54 | |||||
[email protected] | fc12e0b | 2013-03-27 18:06:26 | [diff] [blame] | 55 | // 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. | ||||
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 61 | base::RepeatingTimer timer_; |
[email protected] | d00c07e | 2013-04-09 21:53:58 | [diff] [blame] | 62 | |
[email protected] | 348201f | 2013-06-11 17:24:23 | [diff] [blame] | 63 | // A one-shot timer used for scheduling out-of-band fetches. |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 64 | base::OneShotTimer one_shot_timer_; |
[email protected] | 348201f | 2013-06-11 17:24:23 | [diff] [blame] | 65 | |
[email protected] | d00c07e | 2013-04-09 21:53:58 | [diff] [blame] | 66 | DISALLOW_COPY_AND_ASSIGN(VariationsRequestScheduler); |
[email protected] | fc12e0b | 2013-03-27 18:06:26 | [diff] [blame] | 67 | }; |
68 | |||||
blundell | 57bcfed | 2015-09-04 08:44:45 | [diff] [blame] | 69 | } // namespace variations |
[email protected] | fc12e0b | 2013-03-27 18:06:26 | [diff] [blame] | 70 | |
blundell | 5f9d142 | 2015-08-17 15:30:21 | [diff] [blame] | 71 | #endif // COMPONENTS_VARIATIONS_VARIATIONS_REQUEST_SCHEDULER_H_ |