OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CancellableTaskFactory_h | |
6 #define CancellableTaskFactory_h | |
7 | |
8 #include "platform/PlatformExport.h" | |
9 #include "platform/WebTaskRunner.h" | |
10 #include "platform/heap/Handle.h" | |
11 #include "wtf/Allocator.h" | |
12 #include "wtf/Functional.h" | |
13 #include "wtf/Noncopyable.h" | |
14 #include "wtf/PtrUtil.h" | |
15 #include "wtf/WeakPtr.h" | |
16 #include <memory> | |
17 #include <type_traits> | |
18 | |
19 // WebTaskRunner::postCancellableTask will replace CancellableTaskFactory. | |
20 // Use postCancellableTask in new code. | |
21 // Example: For |task_runner| and |foo| below. | |
22 // WebTaskRunner* task_runner; | |
23 // Foo* foo; | |
24 // | |
25 // CancellableTaskFactory factory(foo, &Foo::bar); | |
26 // task_runner->postTask(BLINK_FROM_HERE, factory.cancelAndCreate()); | |
27 // factory.cancel(); | |
28 // | |
29 // Above is equivalent to below: | |
30 // | |
31 // std::unique_ptr<WTF::Closure> task = | |
32 // WTF::bind(wrapPersistent(foo), &Foo::bar); | |
33 // TaskHandle handle = | |
34 // task_runner->postCancellableTask(BLINK_FROM_HERE, std::move(task)); | |
35 // handle.cancel(); | |
36 namespace blink { | |
37 | |
38 class PLATFORM_EXPORT CancellableTaskFactory { | |
39 WTF_MAKE_NONCOPYABLE(CancellableTaskFactory); | |
40 USING_FAST_MALLOC(CancellableTaskFactory); | |
41 | |
42 public: | |
43 // As WTF::Closure objects are off-heap, we have to construct the closure in | |
44 // such a manner that it doesn't end up referring back to the owning heap | |
45 // object with a strong Persistent<> GC root reference. If we do, this will | |
46 // create a heap <-> off-heap cycle and leak, the owning object can never be | |
47 // GCed. Instead, the closure will keep an off-heap persistent reference of | |
48 // the weak, which will refer back to the owner heap object safely (but | |
49 // weakly.) | |
50 // | |
51 // DEPRECATED: Please use WebTaskRunner::postCancellableTask instead. | |
52 // (https://crbug.com/665285) | |
53 template <typename T> | |
54 static std::unique_ptr<CancellableTaskFactory> create( | |
55 T* thisObject, | |
56 void (T::*method)(), | |
57 typename std::enable_if<IsGarbageCollectedType<T>::value>::type* = | |
58 nullptr) { | |
59 return wrapUnique(new CancellableTaskFactory( | |
60 WTF::bind(method, wrapWeakPersistent(thisObject)))); | |
61 } | |
62 | |
63 bool isPending() const { return m_weakPtrFactory.hasWeakPtrs(); } | |
64 | |
65 void cancel(); | |
66 | |
67 // Returns a task that can be disabled by calling cancel(). The user takes | |
68 // ownership of the task. Creating a new task cancels any previous ones. | |
69 WebTaskRunner::Task* cancelAndCreate(); | |
70 | |
71 protected: | |
72 // Only intended used by unit tests wanting to stack allocate and/or pass in a | |
73 // closure value. Please use the create() factory method elsewhere. | |
74 explicit CancellableTaskFactory(std::unique_ptr<WTF::Closure> closure) | |
75 : m_closure(std::move(closure)), m_weakPtrFactory(this) {} | |
76 | |
77 private: | |
78 class CancellableTask : public WebTaskRunner::Task { | |
79 USING_FAST_MALLOC(CancellableTask); | |
80 WTF_MAKE_NONCOPYABLE(CancellableTask); | |
81 | |
82 public: | |
83 explicit CancellableTask(WeakPtr<CancellableTaskFactory> weakPtr) | |
84 : m_weakPtr(weakPtr) {} | |
85 | |
86 ~CancellableTask() override {} | |
87 | |
88 void run() override; | |
89 | |
90 private: | |
91 WeakPtr<CancellableTaskFactory> m_weakPtr; | |
92 }; | |
93 | |
94 std::unique_ptr<WTF::Closure> m_closure; | |
95 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory; | |
96 }; | |
97 | |
98 } // namespace blink | |
99 | |
100 #endif // CancellableTaskFactory_h | |
OLD | NEW |