Index: third_party/WebKit/public/platform/WebTaskRunner.h |
diff --git a/third_party/WebKit/public/platform/WebTaskRunner.h b/third_party/WebKit/public/platform/WebTaskRunner.h |
index c3997edf93925498118ec104038b6191146328d9..4008adfb86181d2a29fadb2cfe5a068ad3892ec3 100644 |
--- a/third_party/WebKit/public/platform/WebTaskRunner.h |
+++ b/third_party/WebKit/public/platform/WebTaskRunner.h |
@@ -12,6 +12,8 @@ |
#ifdef INSIDE_BLINK |
#include "wtf/Functional.h" |
+#include "wtf/RefCounted.h" |
+#include "wtf/WeakPtr.h" |
#endif |
namespace base { |
@@ -22,6 +24,41 @@ namespace blink { |
using SingleThreadTaskRunner = base::SingleThreadTaskRunner; |
+#ifdef INSIDE_BLINK |
+ |
+class BLINK_PLATFORM_EXPORT TaskHandle |
+ : public WTF::ThreadSafeRefCounted<TaskHandle> { |
+ public: |
+ // Returns true if the task will run later. Returns false if the task is |
+ // cancelled or the task is ran already. |
+ // This function is not thread safe. Call this on the thread that the task |
haraken
2016/10/19 15:20:39
the thread that has posted the task
tzik
2016/10/20 16:15:12
Done.
|
+ // is posted to. |
+ bool isActive() const; |
+ |
+ // Cancels the task invocation. Do nothing if the task is cancelled or ran |
+ // already. |
+ // This function is not thread safe. Call this on the thread that the task |
haraken
2016/10/19 15:20:40
Ditto.
tzik
2016/10/20 16:15:12
Done.
|
+ // is posted to. |
+ void cancel(); |
+ |
+ ~TaskHandle(); |
haraken
2016/10/19 15:20:40
Add a comment and mention that the task is not can
alex clarke (OOO till 29th)
2016/10/19 15:36:46
Are you sure? :) Looks to me like the destructor
tzik
2016/10/19 20:46:47
Since the task has a ref to TaskHandle, TaskHandle
|
+ |
+ private: |
+ class ClearOnScopeOut; |
+ friend class WebTaskRunner; |
+ |
+ explicit TaskHandle(std::unique_ptr<WTF::Closure> task); |
+ void run(const ClearOnScopeOut&); |
+ WTF::WeakPtr<TaskHandle> asWeakPtr(); |
+ |
+ std::unique_ptr<WTF::Closure> m_task; |
+ WTF::WeakPtrFactory<TaskHandle> m_weakPtrFactory; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TaskHandle); |
+}; |
+ |
+#endif |
+ |
// The blink representation of a chromium SingleThreadTaskRunner. |
class BLINK_PLATFORM_EXPORT WebTaskRunner { |
public: |
@@ -92,6 +129,33 @@ class BLINK_PLATFORM_EXPORT WebTaskRunner { |
void postDelayedTask(const WebTraceLocation&, |
std::unique_ptr<WTF::Closure>, |
long long delayMs); |
+ |
+ // For same-thread cancellable task posting. Returns a TaskHandle object for |
+ // cancellation. |
+ // This is a replacement of CancellableTaskFactory and oneshot WTF::Timer. |
+ // Example: For |task_runner| and |foo| below. |
haraken
2016/10/19 15:20:39
Instead of adding this comment, shall we add a dep
|
+ // WebTaskRunner* task_runner; |
+ // Foo* foo; |
+ // |
+ // CancellableTaskFactory factory(foo, &Foo::bar); |
+ // task_runner->postTask(BLINK_FROM_HERE, factory.cancelAndCreate()); |
+ // factory.cancel(); |
+ // |
+ // Above is equivalent to below: |
+ // |
+ // std::unique_ptr<WTF::Closure> task = |
+ // WTF::bind(wrapPersistent(foo), &Foo::bar); |
+ // RefPtr<TaskHandle> handle = |
+ // task_runner->postCancellableTask(BLINK_FROM_HERE, std::move(task)); |
+ // handle->cancel(); |
+ // |
+ // Note that the task is not automatically cancelled on TaskHandle scope out. |
+ PassRefPtr<TaskHandle> postCancellableTask(const WebTraceLocation&, |
+ std::unique_ptr<WTF::Closure>); |
+ PassRefPtr<TaskHandle> postDelayedCancellableTask( |
+ const WebTraceLocation&, |
+ std::unique_ptr<WTF::Closure>, |
+ long long delayMs); |
#endif |
}; |