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 // This class holds a reference to a TaskHandle to make sure it keeps alive | |
|
dcheng
2016/10/21 06:14:46
Nit: to make sure it keeps alive => to keep it ali
tzik
2016/10/25 07:03:52
Done.
| |
| 10 // while a task is pending in a task queue, and clears its reference to avoid a | |
|
dcheng
2016/10/21 06:14:46
Nit: until we read line 22-23, it's not clear when
| |
| 11 // possible circular references in a case below: | |
|
haraken
2016/10/20 17:07:37
avoid a circular reference like below
tzik
2016/10/25 07:03:52
Done.
| |
| 12 // struct Foo : GarbageCollected<Foo> { | |
| 13 // void bar() {} | |
| 14 // RefPtr<TaskHandle> m_handle; | |
| 15 // }; | |
| 16 // | |
| 17 // foo->m_handle = taskRunner->postCancellableTask( | |
| 18 // BLINK_FROM_HERE, WTF::bind(&Foo::bar, wrapPersistent(foo))); | |
| 19 // | |
| 20 // There is a circular reference in the example above as: | |
| 21 // foo -> m_handle -> m_task -> Persistent<Foo> in WTF::bind. | |
| 22 // CancelOnScopeOut is needed to cut the circle by clearing |m_task| when the | |
|
dcheng
2016/10/21 06:14:46
Nit: cut the circle => break the cycle
tzik
2016/10/25 07:03:52
Done.
| |
| 23 // wrapped WTF::Closure is deleted. | |
| 24 class TaskHandle::CancelOnScopeOut { | |
|
haraken
2016/10/20 17:07:37
I'd rename this to CancelOnTaskDestruction
tzik
2016/10/25 07:03:52
Done.
| |
| 25 public: | |
| 26 explicit CancelOnScopeOut(WTF::PassRefPtr<TaskHandle> handle) | |
|
dcheng
2016/10/21 06:14:46
Nit: RefPtr here (also no WTF:: prefix is needed)
tzik
2016/10/25 07:03:52
Done.
| |
| 27 : m_handle(std::move(handle)) {} | |
| 28 | |
| 29 CancelOnScopeOut(CancelOnScopeOut&&) = default; | |
| 30 | |
| 31 void cancel() const { | |
| 32 if (m_handle) | |
|
dcheng
2016/10/21 06:14:46
Is there a time when this will be null?
tzik
2016/10/25 07:03:52
Yes. We make a temporary object on WTF::bind call
| |
| 33 m_handle->cancel(); | |
| 34 } | |
| 35 | |
| 36 ~CancelOnScopeOut() { cancel(); } | |
| 37 | |
| 38 private: | |
| 39 RefPtr<TaskHandle> m_handle; | |
| 40 }; | |
| 41 | |
| 42 bool TaskHandle::isActive() const { | |
| 43 return static_cast<bool>(m_task); | |
| 44 } | |
| 45 | |
| 46 void TaskHandle::cancel() { | |
| 47 std::unique_ptr<WTF::Closure> task = std::move(m_task); | |
| 48 m_weakPtrFactory.revokeAll(); | |
| 49 } | |
| 50 | |
| 51 TaskHandle::~TaskHandle() {} | |
| 52 | |
| 53 TaskHandle::TaskHandle(std::unique_ptr<WTF::Closure> task) | |
| 54 : m_task(std::move(task)), m_weakPtrFactory(this) { | |
| 55 DCHECK(m_task); | |
| 56 } | |
| 57 | |
| 58 void TaskHandle::run(const CancelOnScopeOut& scoper) { | |
| 59 std::unique_ptr<WTF::Closure> task = std::move(m_task); | |
| 60 cancel(); | |
| 61 (*task)(); | |
| 62 } | |
| 63 | |
| 64 WTF::WeakPtr<TaskHandle> TaskHandle::asWeakPtr() { | |
| 65 return m_weakPtrFactory.createWeakPtr(); | |
| 66 } | |
| 67 | |
| 9 void WebTaskRunner::postTask(const WebTraceLocation& location, | 68 void WebTaskRunner::postTask(const WebTraceLocation& location, |
| 10 std::unique_ptr<CrossThreadClosure> task) { | 69 std::unique_ptr<CrossThreadClosure> task) { |
| 11 toSingleThreadTaskRunner()->PostTask(location, | 70 toSingleThreadTaskRunner()->PostTask(location, |
| 12 convertToBaseCallback(std::move(task))); | 71 convertToBaseCallback(std::move(task))); |
| 13 } | 72 } |
| 14 | 73 |
| 15 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, | 74 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, |
| 16 std::unique_ptr<CrossThreadClosure> task, | 75 std::unique_ptr<CrossThreadClosure> task, |
| 17 long long delayMs) { | 76 long long delayMs) { |
| 18 toSingleThreadTaskRunner()->PostDelayedTask( | 77 toSingleThreadTaskRunner()->PostDelayedTask( |
| 19 location, convertToBaseCallback(std::move(task)), | 78 location, convertToBaseCallback(std::move(task)), |
| 20 base::TimeDelta::FromMilliseconds(delayMs)); | 79 base::TimeDelta::FromMilliseconds(delayMs)); |
| 21 } | 80 } |
| 22 | 81 |
| 23 void WebTaskRunner::postTask(const WebTraceLocation& location, | 82 void WebTaskRunner::postTask(const WebTraceLocation& location, |
| 24 std::unique_ptr<WTF::Closure> task) { | 83 std::unique_ptr<WTF::Closure> task) { |
| 25 toSingleThreadTaskRunner()->PostTask(location, | 84 toSingleThreadTaskRunner()->PostTask(location, |
| 26 convertToBaseCallback(std::move(task))); | 85 convertToBaseCallback(std::move(task))); |
| 27 } | 86 } |
| 28 | 87 |
| 29 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, | 88 void WebTaskRunner::postDelayedTask(const WebTraceLocation& location, |
| 30 std::unique_ptr<WTF::Closure> task, | 89 std::unique_ptr<WTF::Closure> task, |
| 31 long long delayMs) { | 90 long long delayMs) { |
| 32 toSingleThreadTaskRunner()->PostDelayedTask( | 91 toSingleThreadTaskRunner()->PostDelayedTask( |
| 33 location, convertToBaseCallback(std::move(task)), | 92 location, convertToBaseCallback(std::move(task)), |
| 34 base::TimeDelta::FromMilliseconds(delayMs)); | 93 base::TimeDelta::FromMilliseconds(delayMs)); |
| 35 } | 94 } |
| 36 | 95 |
| 96 RefPtr<TaskHandle> WebTaskRunner::postCancellableTask( | |
| 97 const WebTraceLocation& location, | |
| 98 std::unique_ptr<WTF::Closure> task) { | |
| 99 DCHECK(runsTasksOnCurrentThread()); | |
| 100 RefPtr<TaskHandle> handle = adoptRef(new TaskHandle(std::move(task))); | |
| 101 postTask(location, WTF::bind(&TaskHandle::run, handle->asWeakPtr(), | |
| 102 TaskHandle::CancelOnScopeOut(handle))); | |
| 103 return handle; | |
| 104 } | |
| 105 | |
| 106 RefPtr<TaskHandle> WebTaskRunner::postDelayedCancellableTask( | |
| 107 const WebTraceLocation& location, | |
| 108 std::unique_ptr<WTF::Closure> task, | |
| 109 long long delayMs) { | |
| 110 DCHECK(runsTasksOnCurrentThread()); | |
| 111 RefPtr<TaskHandle> handle = adoptRef(new TaskHandle(std::move(task))); | |
| 112 postDelayedTask(location, WTF::bind(&TaskHandle::run, handle->asWeakPtr(), | |
| 113 TaskHandle::CancelOnScopeOut(handle)), | |
| 114 delayMs); | |
| 115 return handle; | |
| 116 } | |
| 117 | |
| 37 } // namespace blink | 118 } // namespace blink |
| OLD | NEW |