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()
13 : is_non_nestable(false) {
16 DeferredSequencedTaskRunner::DeferredTask::~DeferredTask() {
19 DeferredSequencedTaskRunner::DeferredSequencedTaskRunner(
20 const scoped_refptr
<SequencedTaskRunner
>& target_task_runner
)
22 target_task_runner_(target_task_runner
) {
25 DeferredSequencedTaskRunner::~DeferredSequencedTaskRunner() {
28 bool DeferredSequencedTaskRunner::PostDelayedTask(
29 const tracked_objects::Location
& from_here
,
34 DCHECK(deferred_tasks_queue_
.empty());
35 return target_task_runner_
->PostDelayedTask(from_here
, task
, delay
);
38 QueueDeferredTask(from_here
, task
, delay
, false /* is_non_nestable */);
42 bool DeferredSequencedTaskRunner::RunsTasksOnCurrentThread() const {
43 return target_task_runner_
->RunsTasksOnCurrentThread();
46 bool DeferredSequencedTaskRunner::PostNonNestableDelayedTask(
47 const tracked_objects::Location
& from_here
,
52 DCHECK(deferred_tasks_queue_
.empty());
53 return target_task_runner_
->PostNonNestableDelayedTask(from_here
,
57 QueueDeferredTask(from_here
, task
, delay
, true /* is_non_nestable */);
61 void DeferredSequencedTaskRunner::QueueDeferredTask(
62 const tracked_objects::Location
& from_here
,
65 bool is_non_nestable
) {
66 DeferredTask deferred_task
;
67 deferred_task
.posted_from
= from_here
;
68 deferred_task
.task
= task
;
69 deferred_task
.delay
= delay
;
70 deferred_task
.is_non_nestable
= is_non_nestable
;
71 deferred_tasks_queue_
.push_back(deferred_task
);
75 void DeferredSequencedTaskRunner::Start() {
79 for (std::vector
<DeferredTask
>::iterator i
= deferred_tasks_queue_
.begin();
80 i
!= deferred_tasks_queue_
.end();
82 const DeferredTask
& task
= *i
;
83 if (task
.is_non_nestable
) {
84 target_task_runner_
->PostNonNestableDelayedTask(task
.posted_from
,
88 target_task_runner_
->PostDelayedTask(task
.posted_from
,
92 // Replace the i-th element in the |deferred_tasks_queue_| with an empty
93 // |DelayedTask| to ensure that |task| is destroyed before the next task
97 deferred_tasks_queue_
.clear();