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 "platform/heap/Handle.h" | 9 #include "platform/heap/Handle.h" |
10 #include "public/platform/WebTaskRunner.h" | 10 #include "public/platform/WebTaskRunner.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 class PLATFORM_EXPORT CancellableTaskFactory { | 23 class PLATFORM_EXPORT CancellableTaskFactory { |
24 WTF_MAKE_NONCOPYABLE(CancellableTaskFactory); | 24 WTF_MAKE_NONCOPYABLE(CancellableTaskFactory); |
25 USING_FAST_MALLOC(CancellableTaskFactory); | 25 USING_FAST_MALLOC(CancellableTaskFactory); |
26 public: | 26 public: |
27 // A pair of mutually exclusive factory methods are provided for constructin
g | 27 // A pair of mutually exclusive factory methods are provided for constructin
g |
28 // a CancellableTaskFactory, one for when a Oilpan heap object owns a | 28 // a CancellableTaskFactory, one for when a Oilpan heap object owns a |
29 // CancellableTaskFactory, and one when that owning object isn't controlled | 29 // CancellableTaskFactory, and one when that owning object isn't controlled |
30 // by Oilpan. | 30 // by Oilpan. |
31 // | 31 // |
32 // In the Oilpan case, as WTF::Closure objects are off-heap, we have to cons
truct the | 32 // In the Oilpan case, as WTF::SameThreadClosure objects are off-heap, we ha
ve to construct the |
33 // closure in such a manner that it doesn't end up referring back to the own
ing heap | 33 // closure in such a manner that it doesn't end up referring back to the own
ing heap |
34 // object with a strong Persistent<> GC root reference. If we do, this will
create | 34 // object with a strong Persistent<> GC root reference. If we do, this will
create |
35 // a heap <-> off-heap cycle and leak, the owning object can never be GCed. | 35 // a heap <-> off-heap cycle and leak, the owning object can never be GCed. |
36 // Instead, the closure will keep an off-heap persistent reference of the we
ak | 36 // Instead, the closure will keep an off-heap persistent reference of the we
ak |
37 // variety, which will refer back to the owner heap object safely (but weakl
y.) | 37 // variety, which will refer back to the owner heap object safely (but weakl
y.) |
38 // | 38 // |
39 template<typename T> | 39 template<typename T> |
40 static PassOwnPtr<CancellableTaskFactory> create(T* thisObject, void (T::*me
thod)(), typename std::enable_if<IsGarbageCollectedType<T>::value>::type* = null
ptr) | 40 static PassOwnPtr<CancellableTaskFactory> create(T* thisObject, void (T::*me
thod)(), typename std::enable_if<IsGarbageCollectedType<T>::value>::type* = null
ptr) |
41 { | 41 { |
42 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, CrossThread
WeakPersistentThisPointer<T>(thisObject)))); | 42 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, CrossThread
WeakPersistentThisPointer<T>(thisObject)))); |
(...skipping 12 matching lines...) Expand all Loading... |
55 | 55 |
56 void cancel(); | 56 void cancel(); |
57 | 57 |
58 // Returns a task that can be disabled by calling cancel(). The user takes | 58 // Returns a task that can be disabled by calling cancel(). The user takes |
59 // ownership of the task. Creating a new task cancels any previous ones. | 59 // ownership of the task. Creating a new task cancels any previous ones. |
60 WebTaskRunner::Task* cancelAndCreate(); | 60 WebTaskRunner::Task* cancelAndCreate(); |
61 | 61 |
62 protected: | 62 protected: |
63 // Only intended used by unit tests wanting to stack allocate and/or pass in
a closure value. | 63 // Only intended used by unit tests wanting to stack allocate and/or pass in
a closure value. |
64 // Please use the create() factory method elsewhere. | 64 // Please use the create() factory method elsewhere. |
65 explicit CancellableTaskFactory(PassOwnPtr<Closure> closure) | 65 explicit CancellableTaskFactory(PassOwnPtr<SameThreadClosure> closure) |
66 : m_closure(std::move(closure)) | 66 : m_closure(std::move(closure)) |
67 , m_weakPtrFactory(this) | 67 , m_weakPtrFactory(this) |
68 { | 68 { |
69 } | 69 } |
70 | 70 |
71 private: | 71 private: |
72 class CancellableTask : public WebTaskRunner::Task { | 72 class CancellableTask : public WebTaskRunner::Task { |
73 USING_FAST_MALLOC(CancellableTask); | 73 USING_FAST_MALLOC(CancellableTask); |
74 WTF_MAKE_NONCOPYABLE(CancellableTask); | 74 WTF_MAKE_NONCOPYABLE(CancellableTask); |
75 | 75 |
76 public: | 76 public: |
77 explicit CancellableTask(WeakPtr<CancellableTaskFactory> weakPtr) | 77 explicit CancellableTask(WeakPtr<CancellableTaskFactory> weakPtr) |
78 : m_weakPtr(weakPtr) {} | 78 : m_weakPtr(weakPtr) {} |
79 | 79 |
80 ~CancellableTask() override {} | 80 ~CancellableTask() override {} |
81 | 81 |
82 void run() override; | 82 void run() override; |
83 | 83 |
84 private: | 84 private: |
85 WeakPtr<CancellableTaskFactory> m_weakPtr; | 85 WeakPtr<CancellableTaskFactory> m_weakPtr; |
86 }; | 86 }; |
87 | 87 |
88 OwnPtr<Closure> m_closure; | 88 OwnPtr<SameThreadClosure> m_closure; |
89 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory; | 89 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory; |
90 }; | 90 }; |
91 | 91 |
92 } // namespace blink | 92 } // namespace blink |
93 | 93 |
94 #endif // CancellableTaskFactory_h | 94 #endif // CancellableTaskFactory_h |
OLD | NEW |