blob: e5b6d61cd17d99bbe8a86c5b269aad57d7d7c860 [file] [log] [blame]
[email protected]afecfb72013-04-18 17:17:331// 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.
4
danakj0a448602015-03-10 00:31:165#ifndef BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_
6#define BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_
[email protected]afecfb72013-04-18 17:17:337
8#include <vector>
9
10#include "base/base_export.h"
[email protected]afecfb72013-04-18 17:17:3311#include "base/callback.h"
12#include "base/compiler_specific.h"
avi9b6f42932015-12-26 22:15:1413#include "base/macros.h"
[email protected]afecfb72013-04-18 17:17:3314#include "base/memory/ref_counted.h"
[email protected]fb441962013-05-08 05:35:2415#include "base/sequenced_task_runner.h"
[email protected]afecfb72013-04-18 17:17:3316#include "base/synchronization/lock.h"
[email protected]99084f62013-06-28 00:49:0717#include "base/time/time.h"
[email protected]afecfb72013-04-18 17:17:3318#include "base/tracked_objects.h"
19
20namespace base {
21
22// A DeferredSequencedTaskRunner is a subclass of SequencedTaskRunner that
23// queues up all requests until the first call to Start() is issued.
24class BASE_EXPORT DeferredSequencedTaskRunner : public SequencedTaskRunner {
25 public:
26 explicit DeferredSequencedTaskRunner(
vmpstr82b0c16d2016-03-18 19:17:2827 scoped_refptr<SequencedTaskRunner> target_runner);
[email protected]afecfb72013-04-18 17:17:3328
29 // TaskRunner implementation
dcheng56488182014-10-21 10:54:5130 bool PostDelayedTask(const tracked_objects::Location& from_here,
tzik6e427842017-04-05 10:13:2131 OnceClosure task,
dcheng56488182014-10-21 10:54:5132 TimeDelta delay) override;
peary23322df62017-05-09 03:55:4833 bool RunsTasksInCurrentSequence() const override;
[email protected]afecfb72013-04-18 17:17:3334
35 // SequencedTaskRunner implementation
dcheng56488182014-10-21 10:54:5136 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
tzik6e427842017-04-05 10:13:2137 OnceClosure task,
dcheng56488182014-10-21 10:54:5138 TimeDelta delay) override;
[email protected]afecfb72013-04-18 17:17:3339
40 // Start the execution - posts all queued tasks to the target executor. The
41 // deferred tasks are posted with their initial delay, meaning that the task
42 // execution delay is actually measured from Start.
43 // Fails when called a second time.
44 void Start();
45
46 private:
47 struct DeferredTask {
48 DeferredTask();
tzik070c8ffb2017-03-29 05:28:1249 DeferredTask(DeferredTask&& other);
[email protected]afecfb72013-04-18 17:17:3350 ~DeferredTask();
tzik070c8ffb2017-03-29 05:28:1251 DeferredTask& operator=(DeferredTask&& other);
[email protected]afecfb72013-04-18 17:17:3352
53 tracked_objects::Location posted_from;
tzik6e427842017-04-05 10:13:2154 OnceClosure task;
[email protected]afecfb72013-04-18 17:17:3355 // The delay this task was initially posted with.
56 TimeDelta delay;
57 bool is_non_nestable;
58 };
59
dcheng56488182014-10-21 10:54:5160 ~DeferredSequencedTaskRunner() override;
[email protected]afecfb72013-04-18 17:17:3361
62 // Creates a |Task| object and adds it to |deferred_tasks_queue_|.
63 void QueueDeferredTask(const tracked_objects::Location& from_here,
tzik6e427842017-04-05 10:13:2164 OnceClosure task,
[email protected]afecfb72013-04-18 17:17:3365 TimeDelta delay,
66 bool is_non_nestable);
67
68 // // Protects |started_| and |deferred_tasks_queue_|.
69 mutable Lock lock_;
70
71 bool started_;
72 const scoped_refptr<SequencedTaskRunner> target_task_runner_;
73 std::vector<DeferredTask> deferred_tasks_queue_;
74
75 DISALLOW_COPY_AND_ASSIGN(DeferredSequencedTaskRunner);
76};
77
78} // namespace base
79
danakj0a448602015-03-10 00:31:1680#endif // BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_