Chromium Code Reviews| Index: components/policy/core/common/policy_scheduler.h |
| diff --git a/components/policy/core/common/policy_scheduler.h b/components/policy/core/common/policy_scheduler.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5956882b6572e78696dd954328f3c4cee08a7489 |
| --- /dev/null |
| +++ b/components/policy/core/common/policy_scheduler.h |
| @@ -0,0 +1,88 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// 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/callback.h" |
| +#include "base/cancelable_callback.h" |
| +#include "base/macros.h" |
| +#include "base/memory/weak_ptr.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. In case a task runs longer than the |
| + // |interval| the next task is backed off the completion time of the previous |
| + // task by |backoff_interval|. (Intervals are computed as the time difference |
| + // between the end of the previous and the start of the subsequent task.) |
| + 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.
|
| + SchedulerCallback callback, |
| + base::TimeDelta interval, |
| + base::TimeDelta backoff_interval); |
| + ~PolicyScheduler(); |
| + |
| + // Schedules a task to run after |delay|. Deletes any previously scheduled but |
| + // not yet started task. |
| + 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
|
| + |
| + private: |
| + // Automatically schedules next task based on |interval_| or |
| + // |backoff_interval_|. 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_; |
| + const base::TimeDelta interval_; |
| + const base::TimeDelta backoff_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 at |backoff_interval_|. |
| + bool backoff_ = false; |
| + |
| + 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.
|
| + const base::TimeTicks startup_ = base::TimeTicks::Now(); |
| + std::unique_ptr<base::CancelableClosure> job_; |
| + |
| + // Must be last member. |
| + base::WeakPtrFactory<PolicyScheduler> weak_ptr_factory_{this}; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PolicyScheduler); |
| +}; |
| + |
| +} // namespace policy |
| + |
| +#endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_SCHEDULER_H_ |