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 #ifndef WebTaskRunner_h | 5 #ifndef WebTaskRunner_h |
| 6 #define WebTaskRunner_h | 6 #define WebTaskRunner_h |
| 7 | 7 |
| 8 #include "WebCommon.h" | 8 #include "WebCommon.h" |
| 9 #include "base/callback_forward.h" | 9 #include "base/callback_forward.h" |
| 10 #include "public/platform/WebTraceLocation.h" | 10 #include "public/platform/WebTraceLocation.h" |
| 11 #include <memory> | 11 #include <memory> |
| 12 | 12 |
| 13 #ifdef INSIDE_BLINK | 13 #ifdef INSIDE_BLINK |
| 14 #include "wtf/Functional.h" | 14 #include "wtf/Functional.h" |
| 15 #include "wtf/RefCounted.h" | |
| 16 #include "wtf/WeakPtr.h" | |
| 15 #endif | 17 #endif |
| 16 | 18 |
| 17 namespace base { | 19 namespace base { |
| 18 class SingleThreadTaskRunner; | 20 class SingleThreadTaskRunner; |
| 19 } | 21 } |
| 20 | 22 |
| 21 namespace blink { | 23 namespace blink { |
| 22 | 24 |
| 23 using SingleThreadTaskRunner = base::SingleThreadTaskRunner; | 25 using SingleThreadTaskRunner = base::SingleThreadTaskRunner; |
| 24 | 26 |
| 27 #ifdef INSIDE_BLINK | |
| 28 | |
| 29 class BLINK_PLATFORM_EXPORT TaskHandle | |
| 30 : public WTF::ThreadSafeRefCounted<TaskHandle> { | |
| 31 public: | |
| 32 // Returns true if the task will run later. Returns false if the task is | |
| 33 // cancelled or the task is ran already. | |
| 34 // 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.
| |
| 35 // is posted to. | |
| 36 bool isActive() const; | |
| 37 | |
| 38 // Cancels the task invocation. Do nothing if the task is cancelled or ran | |
| 39 // already. | |
| 40 // 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.
| |
| 41 // is posted to. | |
| 42 void cancel(); | |
| 43 | |
| 44 ~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
| |
| 45 | |
| 46 private: | |
| 47 class ClearOnScopeOut; | |
| 48 friend class WebTaskRunner; | |
| 49 | |
| 50 explicit TaskHandle(std::unique_ptr<WTF::Closure> task); | |
| 51 void run(const ClearOnScopeOut&); | |
| 52 WTF::WeakPtr<TaskHandle> asWeakPtr(); | |
| 53 | |
| 54 std::unique_ptr<WTF::Closure> m_task; | |
| 55 WTF::WeakPtrFactory<TaskHandle> m_weakPtrFactory; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(TaskHandle); | |
| 58 }; | |
| 59 | |
| 60 #endif | |
| 61 | |
| 25 // The blink representation of a chromium SingleThreadTaskRunner. | 62 // The blink representation of a chromium SingleThreadTaskRunner. |
| 26 class BLINK_PLATFORM_EXPORT WebTaskRunner { | 63 class BLINK_PLATFORM_EXPORT WebTaskRunner { |
| 27 public: | 64 public: |
| 28 virtual ~WebTaskRunner() {} | 65 virtual ~WebTaskRunner() {} |
| 29 | 66 |
| 30 class BLINK_PLATFORM_EXPORT Task { | 67 class BLINK_PLATFORM_EXPORT Task { |
| 31 public: | 68 public: |
| 32 virtual ~Task() {} | 69 virtual ~Task() {} |
| 33 virtual void run() = 0; | 70 virtual void run() = 0; |
| 34 }; | 71 }; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 void postTask(const WebTraceLocation&, std::unique_ptr<CrossThreadClosure>); | 122 void postTask(const WebTraceLocation&, std::unique_ptr<CrossThreadClosure>); |
| 86 void postDelayedTask(const WebTraceLocation&, | 123 void postDelayedTask(const WebTraceLocation&, |
| 87 std::unique_ptr<CrossThreadClosure>, | 124 std::unique_ptr<CrossThreadClosure>, |
| 88 long long delayMs); | 125 long long delayMs); |
| 89 | 126 |
| 90 // For same-thread posting. Must be called from the associated WebThread. | 127 // For same-thread posting. Must be called from the associated WebThread. |
| 91 void postTask(const WebTraceLocation&, std::unique_ptr<WTF::Closure>); | 128 void postTask(const WebTraceLocation&, std::unique_ptr<WTF::Closure>); |
| 92 void postDelayedTask(const WebTraceLocation&, | 129 void postDelayedTask(const WebTraceLocation&, |
| 93 std::unique_ptr<WTF::Closure>, | 130 std::unique_ptr<WTF::Closure>, |
| 94 long long delayMs); | 131 long long delayMs); |
| 132 | |
| 133 // For same-thread cancellable task posting. Returns a TaskHandle object for | |
| 134 // cancellation. | |
| 135 // This is a replacement of CancellableTaskFactory and oneshot WTF::Timer. | |
| 136 // Example: For |task_runner| and |foo| below. | |
|
haraken
2016/10/19 15:20:39
Instead of adding this comment, shall we add a dep
| |
| 137 // WebTaskRunner* task_runner; | |
| 138 // Foo* foo; | |
| 139 // | |
| 140 // CancellableTaskFactory factory(foo, &Foo::bar); | |
| 141 // task_runner->postTask(BLINK_FROM_HERE, factory.cancelAndCreate()); | |
| 142 // factory.cancel(); | |
| 143 // | |
| 144 // Above is equivalent to below: | |
| 145 // | |
| 146 // std::unique_ptr<WTF::Closure> task = | |
| 147 // WTF::bind(wrapPersistent(foo), &Foo::bar); | |
| 148 // RefPtr<TaskHandle> handle = | |
| 149 // task_runner->postCancellableTask(BLINK_FROM_HERE, std::move(task)); | |
| 150 // handle->cancel(); | |
| 151 // | |
| 152 // Note that the task is not automatically cancelled on TaskHandle scope out. | |
| 153 PassRefPtr<TaskHandle> postCancellableTask(const WebTraceLocation&, | |
| 154 std::unique_ptr<WTF::Closure>); | |
| 155 PassRefPtr<TaskHandle> postDelayedCancellableTask( | |
| 156 const WebTraceLocation&, | |
| 157 std::unique_ptr<WTF::Closure>, | |
| 158 long long delayMs); | |
| 95 #endif | 159 #endif |
| 96 }; | 160 }; |
| 97 | 161 |
| 98 } // namespace blink | 162 } // namespace blink |
| 99 | 163 |
| 100 #endif // WebTaskRunner_h | 164 #endif // WebTaskRunner_h |
| OLD | NEW |