1 // Copyright (c) 2013 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.
5 #include "base/deferred_sequenced_task_runner.h"
8 #include "base/logging.h"
12 DeferredSequencedTaskRunner::DeferredTask::DeferredTask() {
15 DeferredSequencedTaskRunner::DeferredTask::~DeferredTask() {
18 DeferredSequencedTaskRunner::DeferredSequencedTaskRunner(
19 const scoped_refptr
<SequencedTaskRunner
>& target_task_runner
)
21 target_task_runner_(target_task_runner
) {
24 DeferredSequencedTaskRunner::~DeferredSequencedTaskRunner() {
27 bool DeferredSequencedTaskRunner::PostDelayedTask(
28 const tracked_objects::Location
& from_here
,
33 DCHECK(deferred_tasks_queue_
.empty());
34 return target_task_runner_
->PostDelayedTask(from_here
, task
, delay
);
37 QueueDeferredTask(from_here
, task
, delay
, false /* is_non_nestable */);
41 bool DeferredSequencedTaskRunner::RunsTasksOnCurrentThread() const {
42 return target_task_runner_
->RunsTasksOnCurrentThread();
45 bool DeferredSequencedTaskRunner::PostNonNestableDelayedTask(
46 const tracked_objects::Location
& from_here
,
51 DCHECK(deferred_tasks_queue_
.empty());
52 return target_task_runner_
->PostNonNestableDelayedTask(from_here
,
56 QueueDeferredTask(from_here
, task
, delay
, true /* is_non_nestable */);
60 void DeferredSequencedTaskRunner::QueueDeferredTask(
61 const tracked_objects::Location
& from_here
,
64 bool is_non_nestable
) {
65 DeferredTask deferred_task
;
66 deferred_task
.posted_from
= from_here
;
67 deferred_task
.task
= task
;
68 deferred_task
.delay
= delay
;
69 deferred_task
.is_non_nestable
= is_non_nestable
;
70 deferred_tasks_queue_
.push_back(deferred_task
);
74 void DeferredSequencedTaskRunner::Start() {
78 for (std::vector
<DeferredTask
>::iterator i
= deferred_tasks_queue_
.begin();
79 i
!= deferred_tasks_queue_
.end();
81 const DeferredTask
& task
= *i
;
82 if (task
.is_non_nestable
) {
83 target_task_runner_
->PostNonNestableDelayedTask(task
.posted_from
,
87 target_task_runner_
->PostDelayedTask(task
.posted_from
,
91 // Replace the i-th element in the |deferred_tasks_queue_| with an empty
92 // |DelayedTask| to ensure that |task| is destroyed before the next task
96 deferred_tasks_queue_
.clear();