OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/WebTaskRunner.h" | 5 #include "platform/WebTaskRunner.h" |
6 | 6 |
| 7 #include "base/bind_helpers.h" |
7 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
8 | 9 |
| 10 namespace base { |
| 11 |
| 12 using RunnerMethodType = |
| 13 void (blink::TaskHandle::Runner::*)(const blink::TaskHandle&); |
| 14 |
| 15 template <> |
| 16 struct CallbackCancellationTraits< |
| 17 RunnerMethodType, |
| 18 std::tuple<WTF::WeakPtr<blink::TaskHandle::Runner>, blink::TaskHandle>> { |
| 19 static constexpr bool is_cancellable = true; |
| 20 |
| 21 static bool IsCancelled(RunnerMethodType, |
| 22 const WTF::WeakPtr<blink::TaskHandle::Runner>&, |
| 23 const blink::TaskHandle& handle) { |
| 24 return !handle.isActive(); |
| 25 } |
| 26 }; |
| 27 |
| 28 } // namespace base |
| 29 |
9 namespace blink { | 30 namespace blink { |
10 | 31 |
11 class TaskHandle::Runner : public WTF::ThreadSafeRefCounted<Runner> { | 32 class TaskHandle::Runner : public WTF::ThreadSafeRefCounted<Runner> { |
12 public: | 33 public: |
13 explicit Runner(std::unique_ptr<WTF::Closure> task) | 34 explicit Runner(std::unique_ptr<WTF::Closure> task) |
14 : m_task(std::move(task)), m_weakPtrFactory(this) {} | 35 : m_task(std::move(task)), m_weakPtrFactory(this) {} |
15 | 36 |
16 WTF::WeakPtr<Runner> asWeakPtr() { return m_weakPtrFactory.createWeakPtr(); } | 37 WTF::WeakPtr<Runner> asWeakPtr() { return m_weakPtrFactory.createWeakPtr(); } |
17 | 38 |
18 bool isActive() { return static_cast<bool>(m_task); } | 39 bool isActive() const { return m_task && !m_task->isCancelled(); } |
19 | 40 |
20 void cancel() { | 41 void cancel() { |
21 std::unique_ptr<WTF::Closure> task = std::move(m_task); | 42 std::unique_ptr<WTF::Closure> task = std::move(m_task); |
22 m_weakPtrFactory.revokeAll(); | 43 m_weakPtrFactory.revokeAll(); |
23 } | 44 } |
24 | 45 |
25 ~Runner() { cancel(); } | 46 ~Runner() { cancel(); } |
26 | 47 |
27 // The TaskHandle parameter on run() holds a reference to the Runner to keep | 48 // The TaskHandle parameter on run() holds a reference to the Runner to keep |
28 // it alive while a task is pending in a task queue, and clears the reference | 49 // it alive while a task is pending in a task queue, and clears the reference |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 DCHECK(runsTasksOnCurrentThread()); | 149 DCHECK(runsTasksOnCurrentThread()); |
129 RefPtr<TaskHandle::Runner> runner = | 150 RefPtr<TaskHandle::Runner> runner = |
130 adoptRef(new TaskHandle::Runner(std::move(task))); | 151 adoptRef(new TaskHandle::Runner(std::move(task))); |
131 postDelayedTask(location, WTF::bind(&TaskHandle::Runner::run, | 152 postDelayedTask(location, WTF::bind(&TaskHandle::Runner::run, |
132 runner->asWeakPtr(), TaskHandle(runner)), | 153 runner->asWeakPtr(), TaskHandle(runner)), |
133 delayMs); | 154 delayMs); |
134 return TaskHandle(runner); | 155 return TaskHandle(runner); |
135 } | 156 } |
136 | 157 |
137 } // namespace blink | 158 } // namespace blink |
OLD | NEW |