File: policy_scheduler.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (102 lines) | stat: -rw-r--r-- 3,765 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_POLICY_CORE_COMMON_POLICY_SCHEDULER_H_
#define COMPONENTS_POLICY_CORE_COMMON_POLICY_SCHEDULER_H_

#include <memory>

#include "base/cancelable_callback.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "components/policy/policy_export.h"

namespace policy {

// Scheduler for driving repeated asynchronous tasks such as e.g. policy
// fetches. Subsequent tasks are guaranteed not to overlap. Tasks are posted to
// the current thread and therefore must not block (suitable e.g. for
// asynchronous D-Bus calls).
// Tasks scheduling begins immediately after instantiation of the class. Upon
// destruction, scheduled but not yet started tasks are cancelled. The result of
// started but not finished tasks is NOT reported.
class POLICY_EXPORT PolicyScheduler {
 public:
  // Callback for the task to report success or failure.
  using TaskCallback = base::OnceCallback<void(bool success)>;

  // Task to be performed at regular intervals. The task takes a |callback| to
  // return success or failure.
  using Task = base::RepeatingCallback<void(TaskCallback callback)>;

  // Callback for PolicyScheduler to report success or failure of the tasks.
  using SchedulerCallback = base::RepeatingCallback<void(bool success)>;

  // Defines the |task| to be run every |interval| and the |callback| for the
  // scheduler to report the result. (Intervals are computed as the time
  // difference between the end of the previous and the start of the subsequent
  // task.) Calling the constructor starts the loop and schedules the first task
  // to be run without delay.
  PolicyScheduler(Task task,
                  SchedulerCallback callback,
                  base::TimeDelta interval);
  PolicyScheduler(const PolicyScheduler&) = delete;
  PolicyScheduler& operator=(const PolicyScheduler&) = delete;
  ~PolicyScheduler();

  // Schedules a task to run immediately. Deletes any previously scheduled but
  // not yet started tasks. In case a task is running currently, the new task is
  // scheduled to run immediately after the end of the currently running task.
  void ScheduleTaskNow();

  base::TimeDelta interval() const { return interval_; }

  base::Time last_refresh_attempt() const { return last_refresh_attempt_; }

 private:
  // Schedules next task to run in |delay|. Deletes any previously scheduled
  // tasks.
  void ScheduleDelayedTask(base::TimeDelta delay);

  // Schedules next task to run in |interval_| or immediately in case of
  // overlap. Deletes any previously scheduled tasks.
  void ScheduleNextTask();

  // Actually executes the scheduled task.
  void RunScheduledTask();

  // Reports back the |result| of the previous task and schedules the next one.
  void OnTaskDone(bool result);

  Task task_;
  SchedulerCallback callback_;
  // Tasks are being run every |interval_|.
  const base::TimeDelta interval_;

  // Whether a task is in progress.
  bool task_in_progress_ = false;

  // Whether there had been an overlap of tasks and thus the next task needs to
  // be scheduled without delay.
  bool overlap_ = false;

  // End time of the previous task. Zero in case no task has ended yet.
  base::TimeTicks last_task_;

  // Last time refresh has been attempted.
  base::Time last_refresh_attempt_;

  std::unique_ptr<base::CancelableOnceClosure> job_;

  SEQUENCE_CHECKER(sequence_checker_);

  // Must be last member.
  base::WeakPtrFactory<PolicyScheduler> weak_ptr_factory_{this};
};

}  // namespace policy

#endif  // COMPONENTS_POLICY_CORE_COMMON_POLICY_SCHEDULER_H_