Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(236)

Unified Diff: components/policy/core/common/policy_scheduler.h

Issue 2942373002: Extract AD policy scheduler into separate class (Closed)
Patch Set: Simplify public interface (and implementation) Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/policy/core/common/BUILD.gn ('k') | components/policy/core/common/policy_scheduler.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..a5701439cfd1e08efb8ec52129f0230191417839
--- /dev/null
+++ b/components/policy/core/common/policy_scheduler.h
@@ -0,0 +1,95 @@
+// 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/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();
+
+ // 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();
+
+ 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_;
+ 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_;
+
+ std::unique_ptr<base::CancelableClosure> job_;
+
+ SEQUENCE_CHECKER(sequence_checker_);
+
+ // 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_
« no previous file with comments | « components/policy/core/common/BUILD.gn ('k') | components/policy/core/common/policy_scheduler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698