OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 | |
5 #ifndef COMPONENTS_POLICY_CORE_COMMON_POLICY_SCHEDULER_H_ | |
6 #define COMPONENTS_POLICY_CORE_COMMON_POLICY_SCHEDULER_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/cancelable_callback.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/time/time.h" | |
15 #include "components/policy/policy_export.h" | |
16 | |
17 namespace policy { | |
18 | |
19 // Scheduler for driving repeated asynchronous tasks such as e.g. policy | |
20 // fetches. Subsequent tasks are guaranteed not to overlap. Tasks are posted to | |
21 // the current thread and therefore must not block (suitable e.g. for | |
22 // asynchronous D-Bus calls). | |
23 // Tasks scheduling begins immediately after instantiation of the class. Upon | |
24 // destruction, scheduled but not yet started tasks are cancelled. The result of | |
25 // started but not finished tasks is NOT reported. | |
26 class POLICY_EXPORT PolicyScheduler { | |
27 public: | |
28 // Callback for the task to report success or failure. | |
29 using TaskCallback = base::OnceCallback<void(bool success)>; | |
30 | |
31 // Task to be performed at regular intervals. The task takes a |callback| to | |
32 // return success or failure. | |
33 using Task = base::RepeatingCallback<void(TaskCallback callback)>; | |
34 | |
35 // Callback for PolicyScheduler to report success or failure of the tasks. | |
36 using SchedulerCallback = base::RepeatingCallback<void(bool success)>; | |
37 | |
38 // Defines the |task| to be run every |interval| and the |callback| for the | |
39 // scheduler to report the result. In case a task runs longer than the | |
40 // |interval| the next task is backed off the completion time of the previous | |
41 // task by |backoff_interval|. (Intervals are computed as the time difference | |
42 // between the end of the previous and the start of the subsequent task.) | |
43 PolicyScheduler(Task task, | |
pmarko
2017/06/19 10:14:37
Although it means repeating the comment on the cla
Thiemo Nagel
2017/06/19 22:29:51
Done.
| |
44 SchedulerCallback callback, | |
45 base::TimeDelta interval, | |
46 base::TimeDelta backoff_interval); | |
47 ~PolicyScheduler(); | |
48 | |
49 // Schedules a task to run after |delay|. Deletes any previously scheduled but | |
50 // not yet started task. | |
51 void ScheduleTask(base::TimeDelta delay); | |
pmarko
2017/06/19 10:14:37
Do we need the delay parameter in the public API?
Thiemo Nagel
2017/06/19 22:29:51
Good point. Right now, I'm not aware of a use case
Thiemo Nagel
2017/06/19 22:50:04
Nah, you're right, it does.
And now I'm thinking,
pmarko
2017/06/20 20:01:05
Re: Omitting the parameter completely vs leaving i
Thiemo Nagel
2017/06/21 07:05:31
Good point. Done.
pmarko
2017/06/21 08:04:54
Yeah, agree that it shouldn't be make a big differ
| |
52 | |
53 private: | |
54 // Automatically schedules next task based on |interval_| or | |
55 // |backoff_interval_|. Deletes any previously scheduled tasks. | |
56 void ScheduleNextTask(); | |
57 | |
58 // Actually executes the scheduled task. | |
59 void RunScheduledTask(); | |
60 | |
61 // Reports back the |result| of the previous task and schedules the next one. | |
62 void OnTaskDone(bool result); | |
63 | |
64 Task task_; | |
65 SchedulerCallback callback_; | |
66 const base::TimeDelta interval_; | |
67 const base::TimeDelta backoff_interval_; | |
68 | |
69 // Whether a task is in progress. | |
70 bool task_in_progress_ = false; | |
71 | |
72 // Whether there had been an overlap of tasks and thus the next task needs to | |
73 // be scheduled at |backoff_interval_|. | |
74 bool backoff_ = false; | |
75 | |
76 base::TimeTicks last_task_; | |
pmarko
2017/06/19 10:14:37
Please add a comment that this captures the _end_
Thiemo Nagel
2017/06/19 22:29:51
Done.
| |
77 const base::TimeTicks startup_ = base::TimeTicks::Now(); | |
78 std::unique_ptr<base::CancelableClosure> job_; | |
79 | |
80 // Must be last member. | |
81 base::WeakPtrFactory<PolicyScheduler> weak_ptr_factory_{this}; | |
82 | |
83 DISALLOW_COPY_AND_ASSIGN(PolicyScheduler); | |
84 }; | |
85 | |
86 } // namespace policy | |
87 | |
88 #endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_SCHEDULER_H_ | |
OLD | NEW |