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 CancellableTaskFactory_h | 5 #ifndef CancellableTaskFactory_h |
6 #define CancellableTaskFactory_h | 6 #define CancellableTaskFactory_h |
7 | 7 |
8 #include "platform/PlatformExport.h" | 8 #include "platform/PlatformExport.h" |
9 #include "public/platform/WebScheduler.h" | 9 #include "public/platform/WebScheduler.h" |
10 #include "wtf/Functional.h" | 10 #include "wtf/Functional.h" |
11 #include "wtf/Noncopyable.h" | 11 #include "wtf/Noncopyable.h" |
12 #include "wtf/OwnPtr.h" | 12 #include "wtf/OwnPtr.h" |
13 #include "wtf/PassOwnPtr.h" | 13 #include "wtf/PassOwnPtr.h" |
14 #include "wtf/RefCounted.h" | 14 #include "wtf/RefCounted.h" |
15 #include "wtf/WeakPtr.h" | 15 #include "wtf/WeakPtr.h" |
16 | 16 |
17 namespace blink { | 17 namespace blink { |
18 class TraceLocation; | 18 class TraceLocation; |
19 | 19 |
| 20 // NOTE CancellableTaskFactory is NOT threadsafe. |
20 class PLATFORM_EXPORT CancellableTaskFactory { | 21 class PLATFORM_EXPORT CancellableTaskFactory { |
21 WTF_MAKE_NONCOPYABLE(CancellableTaskFactory); | 22 WTF_MAKE_NONCOPYABLE(CancellableTaskFactory); |
22 | 23 |
23 public: | 24 public: |
24 explicit CancellableTaskFactory(PassOwnPtr<Closure> closure) | 25 explicit CancellableTaskFactory(PassOwnPtr<Closure> closure) |
25 : m_closure(closure) | 26 : m_closure(closure) |
26 , m_weakPtrFactory(this) | 27 , m_task(nullptr) |
| 28 #if ENABLE(ASSERT) |
| 29 , m_thread(currentThread()) |
| 30 #endif |
27 { | 31 { |
28 } | 32 } |
29 | 33 |
| 34 ~CancellableTaskFactory() |
| 35 { |
| 36 cancel(); |
| 37 } |
| 38 |
30 bool isPending() const | 39 bool isPending() const |
31 { | 40 { |
32 return m_weakPtrFactory.hasWeakPtrs(); | 41 return m_task; |
33 } | 42 } |
34 | 43 |
35 void cancel(); | 44 void cancel(); |
36 | 45 |
37 // Returns a task that can be disabled by calling cancel(). The user takes | 46 // Returns a task that can be disabled by calling cancel(). The user takes |
38 // ownership of the task. Creating a new task cancels any previous ones. | 47 // ownership of the task. Creating a new task cancels any previous ones. |
39 WebThread::Task* task(); | 48 WebThread::Task* task(); |
40 | 49 |
41 private: | 50 private: |
42 class CancellableTask : public WebThread::Task { | 51 class CancellableTask : public WebThread::Task { |
43 WTF_MAKE_NONCOPYABLE(CancellableTask); | 52 WTF_MAKE_NONCOPYABLE(CancellableTask); |
| 53 WTF_MAKE_FAST_ALLOCATED; |
44 | 54 |
45 public: | 55 public: |
46 explicit CancellableTask(WeakPtr<CancellableTaskFactory> weakPtr) | 56 explicit CancellableTask(CancellableTaskFactory* factory) |
47 : m_weakPtr(weakPtr) { } | 57 : m_factory(factory) |
| 58 { |
| 59 } |
48 | 60 |
49 virtual ~CancellableTask() { } | 61 virtual ~CancellableTask() { } |
50 | 62 |
51 void run() override; | 63 void run() override; |
52 | 64 |
| 65 void cancel() |
| 66 { |
| 67 m_factory = nullptr; |
| 68 } |
| 69 |
53 private: | 70 private: |
54 WeakPtr<CancellableTaskFactory> m_weakPtr; | 71 CancellableTaskFactory* m_factory; // NOT OWNED |
55 }; | 72 }; |
56 | 73 |
57 OwnPtr<Closure> m_closure; | 74 OwnPtr<Closure> m_closure; |
58 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory; | 75 CancellableTask* m_task; // NOT OWNED |
| 76 |
| 77 #if ENABLE(ASSERT) |
| 78 ThreadIdentifier m_thread; |
| 79 #endif |
59 }; | 80 }; |
60 | 81 |
61 } // namespace blink | 82 } // namespace blink |
62 | 83 |
63 #endif // CancellableTaskFactory_h | 84 #endif // CancellableTaskFactory_h |
OLD | NEW |