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

Side by Side Diff: components/policy/core/common/policy_scheduler.cc

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 unified diff | Download patch
OLDNEW
(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 : task_(task), callback_(callback), interval_(interval) {
16 ScheduleTaskNow();
17 }
18
19 PolicyScheduler::~PolicyScheduler() {
20 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
21 }
22
23 void PolicyScheduler::ScheduleTaskNow() {
24 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
25 ScheduleDelayedTask(base::TimeDelta());
26 }
27
28 void PolicyScheduler::ScheduleDelayedTask(base::TimeDelta delay) {
29 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
30
31 if (job_) {
32 job_->Cancel();
33 }
34 job_ = base::MakeUnique<base::CancelableClosure>(base::Bind(
35 &PolicyScheduler::RunScheduledTask, weak_ptr_factory_.GetWeakPtr()));
36 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE,
37 job_->callback(), delay);
38 }
39
40 void PolicyScheduler::ScheduleNextTask() {
41 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
42
43 base::TimeDelta interval = overlap_ ? base::TimeDelta() : interval_;
44 const base::TimeTicks now(base::TimeTicks::Now());
45 // Time uses saturated arithmetics thus no under/overflow possible.
46 const base::TimeDelta delay = last_task_ + interval - now;
47 // Clamping delay to non-negative values just to be on the safe side.
48 ScheduleDelayedTask(std::max(base::TimeDelta(), delay));
49 }
50
51 void PolicyScheduler::RunScheduledTask() {
52 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
53
54 if (task_in_progress_) {
55 overlap_ = true;
56 return;
57 }
58
59 overlap_ = false;
60 task_in_progress_ = true;
61 task_.Run(base::BindOnce(&PolicyScheduler::OnTaskDone,
62 weak_ptr_factory_.GetWeakPtr()));
63 }
64
65 void PolicyScheduler::OnTaskDone(bool success) {
66 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
67
68 task_in_progress_ = false;
69 last_task_ = base::TimeTicks::Now();
70 callback_.Run(success);
71 ScheduleNextTask();
72 }
73
74 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/policy_scheduler.h ('k') | components/policy/core/common/policy_scheduler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698