OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef BASE_DEFERRED_SEQUENCED_TASKRUNNER_H_ |
| 6 #define BASE_DEFERRED_SEQUENCED_TASKRUNNER_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/base_export.h" |
| 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/sequenced_task_runner.h" |
| 15 #include "base/time.h" |
| 16 #include "base/tracked_objects.h" |
| 17 |
| 18 namespace base { |
| 19 |
| 20 // A DeferredSequencedTaskRunner is a subclass of SequencedTaskRunner that |
| 21 // queues up all requests until the first call to Start() is issued. |
| 22 class BASE_EXPORT DeferredSequencedTaskRunner : public SequencedTaskRunner { |
| 23 public: |
| 24 explicit DeferredSequencedTaskRunner( |
| 25 const scoped_refptr<SequencedTaskRunner>& target_runner); |
| 26 |
| 27 // TaskRunner implementation |
| 28 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 29 const Closure& task, |
| 30 TimeDelta delay) OVERRIDE; |
| 31 |
| 32 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
| 33 |
| 34 // SequencedTaskRunner implementation |
| 35 virtual bool PostNonNestableDelayedTask( |
| 36 const tracked_objects::Location& from_here, |
| 37 const Closure& task, |
| 38 TimeDelta delay) OVERRIDE; |
| 39 |
| 40 // Start the execution - posts all tasks in the |deferred_tasks_queue_| to |
| 41 // the target executor. The deferred tasks are posted with their initial |
| 42 // delay, meaning that the task execution delay is actually measured from |
| 43 // Start. |
| 44 // Fails when called a second time. |
| 45 void Start(); |
| 46 |
| 47 private: |
| 48 struct DeferredTask { |
| 49 DeferredTask(); |
| 50 ~DeferredTask(); |
| 51 tracked_objects::Location posted_from; |
| 52 Closure task; |
| 53 // The delay this task was initially posted with. |
| 54 TimeDelta delay; |
| 55 bool is_non_nestable; |
| 56 }; |
| 57 |
| 58 virtual ~DeferredSequencedTaskRunner(); |
| 59 |
| 60 // Creates a |Task| object and adds it to |deferred_tasks_queue_|. |
| 61 void QueueDeferredTask(const tracked_objects::Location& from_here, |
| 62 const Closure& task, |
| 63 TimeDelta delay, |
| 64 bool is_non_nestable); |
| 65 |
| 66 mutable Lock lock_; |
| 67 |
| 68 // True if this DeferredSequencedTaskRunner has already been started. |
| 69 bool started_; |
| 70 |
| 71 // Target task runner that executes the tasks. |
| 72 const scoped_refptr<SequencedTaskRunner> target_task_runner_; |
| 73 |
| 74 // Queue of tasks to be posted to |target_task_runner_| when this |
| 75 // |DeferredSequencedTaskRunner| is started. |
| 76 std::vector<DeferredTask> deferred_tasks_queue_; |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(DeferredSequencedTaskRunner); |
| 79 }; |
| 80 |
| 81 } // namespace base |
| 82 |
| 83 #endif // BASE_DEFERRED_SEQUENCED_TASKRUNNER_H_ |
OLD | NEW |