Chromium Code Reviews| 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 "public/platform/WebTaskRunner.h" | 5 #include "public/platform/WebTaskRunner.h" |
| 6 | 6 |
| 7 namespace blink { | 7 namespace blink { |
| 8 | 8 |
| 9 class TaskHandle::ClearOnScopeOut { | |
|
haraken
2016/10/19 15:20:39
Sorry, would you help me understand why we need Cl
tzik
2016/10/19 20:46:47
I'll add a comment for this. This is to avoid a ci
haraken
2016/10/20 09:42:46
That makes sense.
However, then why is it not eno
tzik
2016/10/20 16:15:12
That still leaves a circular reference if a task i
| |
| 10 public: | |
| 11 explicit ClearOnScopeOut(WTF::PassRefPtr<TaskHandle> handle) | |
| 12 : m_handle(std::move(handle)) {} | |
| 13 | |
| 14 ClearOnScopeOut(ClearOnScopeOut&&) = default; | |
| 15 | |
| 16 void cancel() const { | |
| 17 if (m_handle) | |
| 18 m_handle->cancel(); | |
| 19 } | |
| 20 | |
| 21 ~ClearOnScopeOut() { cancel(); } | |
|
haraken
2016/10/19 15:20:39
For example, why we need to call cancel() when Cle
tzik
2016/10/20 16:15:12
This is to clear the TaskHandle::m_task to cut the
| |
| 22 | |
| 23 private: | |
| 24 RefPtr<TaskHandle> m_handle; | |
| 25 }; | |
| 26 | |
| 27 bool TaskHandle::isActive() const { | |
| 28 return static_cast<bool>(m_task); | |
| 29 } | |
| 30 | |
| 31 void TaskHandle::cancel() { | |
| 32 std::unique_ptr<WTF::Closure> task = std::move(m_task); | |
|
alex clarke (OOO till 29th)
2016/10/19 15:36:46
Is there any reason to prefer this over: m_task =
tzik
2016/10/19 20:46:47
Just in case. This is to avoid potential reentranc
dcheng
2016/10/19 22:21:47
FWIW, probably worth adding a comment for this.
| |
| 33 m_weakPtrFactory.revokeAll(); | |
| 34 } | |
| 35 | |
| 36 TaskHandle::~TaskHandle() {} | |
| 37 | |
| 38 TaskHandle::TaskHandle(std::unique_ptr<WTF::Closure> task) | |
| 39 : m_task(std::move(task)), m_weakPtrFactory(this) { | |
| 40 DCHECK(m_task); | |
| 41 } | |
| 42 | |
| 43 void TaskHandle::run(const ClearOnScopeOut& scoper) { | |
| 44 std::unique_ptr<WTF::Closure> task = std::move(m_task); | |
| 45 scoper.cancel(); | |
|
haraken
2016/10/19 15:20:39
Can we simply call cancel() instead of scoper.canc
alex clarke (OOO till 29th)
2016/10/19 15:36:46
+1 Haraken is essentially suggesting what TimerBas
tzik
2016/10/20 16:15:12
Done.
| |
| 46 (*task)(); | |
| 47 } | |
| 48 | |
| 49 WTF::WeakPtr<TaskHandle> TaskHandle::asWeakPtr() { | |
| 50 return m_weakPtrFactory.createWeakPtr(); | |
| 51 } | |
| 52 | |
| 9 void WebTaskRunner::postTask(const WebTraceLocation& location, | 53 void WebTaskRunner::postTask(const WebTraceLocation& location, |
| 10 std::unique_ptr<CrossThreadClosure> task) { | 54 std::unique_ptr<CrossThreadClosure> task) { |
| 11 toSingleThreadTaskRunner()->PostTask(location, | 55 toSingleThreadTaskRunner()->PostTask(location, |
| 12 convertToBaseCallback(std::move(task))); | 56 convertToBaseCallback(std::move(task))); |
| 13 } | 57 } |
| 14 | 58 |
| 15 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, | 59 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, |
| 16 std::unique_ptr<CrossThreadClosure> task, | 60 std::unique_ptr<CrossThreadClosure> task, |
| 17 long long delayMs) { | 61 long long delayMs) { |
| 18 toSingleThreadTaskRunner()->PostDelayedTask( | 62 toSingleThreadTaskRunner()->PostDelayedTask( |
| 19 location, convertToBaseCallback(std::move(task)), | 63 location, convertToBaseCallback(std::move(task)), |
| 20 base::TimeDelta::FromMilliseconds(delayMs)); | 64 base::TimeDelta::FromMilliseconds(delayMs)); |
| 21 } | 65 } |
| 22 | 66 |
| 23 void WebTaskRunner::postTask(const WebTraceLocation& location, | 67 void WebTaskRunner::postTask(const WebTraceLocation& location, |
| 24 std::unique_ptr<WTF::Closure> task) { | 68 std::unique_ptr<WTF::Closure> task) { |
| 25 toSingleThreadTaskRunner()->PostTask(location, | 69 toSingleThreadTaskRunner()->PostTask(location, |
| 26 convertToBaseCallback(std::move(task))); | 70 convertToBaseCallback(std::move(task))); |
| 27 } | 71 } |
| 28 | 72 |
| 29 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, | 73 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, |
| 30 std::unique_ptr<WTF::Closure> task, | 74 std::unique_ptr<WTF::Closure> task, |
| 31 long long delayMs) { | 75 long long delayMs) { |
| 32 toSingleThreadTaskRunner()->PostDelayedTask( | 76 toSingleThreadTaskRunner()->PostDelayedTask( |
| 33 location, convertToBaseCallback(std::move(task)), | 77 location, convertToBaseCallback(std::move(task)), |
| 34 base::TimeDelta::FromMilliseconds(delayMs)); | 78 base::TimeDelta::FromMilliseconds(delayMs)); |
| 35 } | 79 } |
| 36 | 80 |
| 81 PassRefPtr<TaskHandle> WebTaskRunner::postCancellableTask( | |
|
dcheng
2016/10/19 22:21:47
My understanding is that PassRefPtr is deprecated,
tzik
2016/10/20 16:15:12
Done.
| |
| 82 const WebTraceLocation& location, | |
| 83 std::unique_ptr<WTF::Closure> task) { | |
| 84 DCHECK(runsTasksOnCurrentThread()); | |
| 85 RefPtr<TaskHandle> handle = adoptRef(new TaskHandle(std::move(task))); | |
| 86 postTask(location, WTF::bind(&TaskHandle::run, handle->asWeakPtr(), | |
| 87 TaskHandle::ClearOnScopeOut(handle))); | |
| 88 return handle.release(); | |
| 89 } | |
| 90 | |
| 91 PassRefPtr<TaskHandle> WebTaskRunner::postDelayedCancellableTask( | |
| 92 const WebTraceLocation& location, | |
| 93 std::unique_ptr<WTF::Closure> task, | |
| 94 long long delayMs) { | |
| 95 DCHECK(runsTasksOnCurrentThread()); | |
| 96 RefPtr<TaskHandle> handle = adoptRef(new TaskHandle(std::move(task))); | |
| 97 postDelayedTask(location, WTF::bind(&TaskHandle::run, handle->asWeakPtr(), | |
| 98 TaskHandle::ClearOnScopeOut(handle)), | |
| 99 delayMs); | |
| 100 return handle.release(); | |
| 101 } | |
| 102 | |
| 37 } // namespace blink | 103 } // namespace blink |
| OLD | NEW |