Index: components/policy/core/common/policy_scheduler.cc |
diff --git a/components/policy/core/common/policy_scheduler.cc b/components/policy/core/common/policy_scheduler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..566a6f519d4c20e2f4fafee2364ee53090a5e237 |
--- /dev/null |
+++ b/components/policy/core/common/policy_scheduler.cc |
@@ -0,0 +1,74 @@ |
+// 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. |
+ |
+#include "components/policy/core/common/policy_scheduler.h" |
+ |
+#include "base/memory/ptr_util.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+ |
+namespace policy { |
+ |
+PolicyScheduler::PolicyScheduler(Task task, |
+ SchedulerCallback callback, |
+ base::TimeDelta interval, |
+ base::TimeDelta backoff_interval) |
+ : task_(task), |
+ callback_(callback), |
+ interval_(interval), |
+ backoff_interval_(backoff_interval) { |
+ ScheduleTask(base::TimeDelta()); |
+} |
+ |
+PolicyScheduler::~PolicyScheduler() = default; |
+ |
+void PolicyScheduler::ScheduleTask(base::TimeDelta delay) { |
pmarko
2017/06/19 10:14:37
Have you considered using SequenceChecker to make
Thiemo Nagel
2017/06/19 22:29:51
Good point. I've added it to all methods to save
|
+ if (job_) { |
+ job_->Cancel(); |
+ } |
+ job_ = base::MakeUnique<base::CancelableClosure>(base::Bind( |
+ &PolicyScheduler::RunScheduledTask, weak_ptr_factory_.GetWeakPtr())); |
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, |
+ job_->callback(), delay); |
+} |
+ |
+void PolicyScheduler::ScheduleNextTask() { |
+ base::TimeTicks baseline; |
+ base::TimeDelta interval; |
+ if (backoff_) { |
+ baseline = last_task_; |
+ interval = backoff_interval_; |
+ } else if (last_task_ == base::TimeTicks()) { |
pmarko
2017/06/19 10:14:37
Can this happen? ScheduleNextTask() is private and
Thiemo Nagel
2017/06/19 22:29:51
Good catch! I think that part only made sense in t
|
+ baseline = startup_; |
+ interval = base::TimeDelta(); |
+ } else { |
+ baseline = last_task_; |
+ interval = interval_; |
+ } |
+ const base::TimeTicks now(base::TimeTicks::Now()); |
+ // Time uses saturated arithmetics thus no under/overflow possible. |
+ const base::TimeDelta delay = baseline + interval - now; |
+ // Clamping delay to non-negative values just to be on the safe side. |
+ ScheduleTask(std::max(base::TimeDelta(), delay)); |
+} |
+ |
+void PolicyScheduler::RunScheduledTask() { |
+ if (task_in_progress_) { |
+ backoff_ = true; |
+ return; |
+ } |
+ |
+ backoff_ = false; |
+ task_in_progress_ = true; |
+ task_.Run(base::BindOnce(&PolicyScheduler::OnTaskDone, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void PolicyScheduler::OnTaskDone(bool success) { |
+ task_in_progress_ = false; |
+ last_task_ = base::TimeTicks::Now(); |
+ callback_.Run(success); |
+ ScheduleNextTask(); |
+} |
+ |
+} // namespace policy |