Chromium Code Reviews| 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 #include "components/policy/core/common/policy_scheduler.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/threading/thread_task_runner_handle.h" | |
| 9 | |
| 10 namespace policy { | |
| 11 | |
| 12 PolicyScheduler::PolicyScheduler(Task task, | |
| 13 SchedulerCallback callback, | |
| 14 base::TimeDelta interval, | |
| 15 base::TimeDelta backoff_interval) | |
| 16 : task_(task), | |
| 17 callback_(callback), | |
| 18 interval_(interval), | |
| 19 backoff_interval_(backoff_interval) { | |
| 20 ScheduleTask(base::TimeDelta()); | |
| 21 } | |
| 22 | |
| 23 PolicyScheduler::~PolicyScheduler() = default; | |
| 24 | |
| 25 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
| |
| 26 if (job_) { | |
| 27 job_->Cancel(); | |
| 28 } | |
| 29 job_ = base::MakeUnique<base::CancelableClosure>(base::Bind( | |
| 30 &PolicyScheduler::RunScheduledTask, weak_ptr_factory_.GetWeakPtr())); | |
| 31 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, | |
| 32 job_->callback(), delay); | |
| 33 } | |
| 34 | |
| 35 void PolicyScheduler::ScheduleNextTask() { | |
| 36 base::TimeTicks baseline; | |
| 37 base::TimeDelta interval; | |
| 38 if (backoff_) { | |
| 39 baseline = last_task_; | |
| 40 interval = backoff_interval_; | |
| 41 } 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
| |
| 42 baseline = startup_; | |
| 43 interval = base::TimeDelta(); | |
| 44 } else { | |
| 45 baseline = last_task_; | |
| 46 interval = interval_; | |
| 47 } | |
| 48 const base::TimeTicks now(base::TimeTicks::Now()); | |
| 49 // Time uses saturated arithmetics thus no under/overflow possible. | |
| 50 const base::TimeDelta delay = baseline + interval - now; | |
| 51 // Clamping delay to non-negative values just to be on the safe side. | |
| 52 ScheduleTask(std::max(base::TimeDelta(), delay)); | |
| 53 } | |
| 54 | |
| 55 void PolicyScheduler::RunScheduledTask() { | |
| 56 if (task_in_progress_) { | |
| 57 backoff_ = true; | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 backoff_ = false; | |
| 62 task_in_progress_ = true; | |
| 63 task_.Run(base::BindOnce(&PolicyScheduler::OnTaskDone, | |
| 64 weak_ptr_factory_.GetWeakPtr())); | |
| 65 } | |
| 66 | |
| 67 void PolicyScheduler::OnTaskDone(bool success) { | |
| 68 task_in_progress_ = false; | |
| 69 last_task_ = base::TimeTicks::Now(); | |
| 70 callback_.Run(success); | |
| 71 ScheduleNextTask(); | |
| 72 } | |
| 73 | |
| 74 } // namespace policy | |
| OLD | NEW |