| 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" |
| 11 #include "wtf/Allocator.h" | 11 #include "wtf/Allocator.h" |
| 12 #include "wtf/Functional.h" | 12 #include "wtf/Functional.h" |
| 13 #include "wtf/Noncopyable.h" | 13 #include "wtf/Noncopyable.h" |
| 14 #include "wtf/PtrUtil.h" | 14 #include "wtf/OwnPtr.h" |
| 15 #include "wtf/PassOwnPtr.h" |
| 15 #include "wtf/WeakPtr.h" | 16 #include "wtf/WeakPtr.h" |
| 16 #include <memory> | |
| 17 #include <type_traits> | 17 #include <type_traits> |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 | 20 |
| 21 class TraceLocation; | 21 class TraceLocation; |
| 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::SameThreadClosure objects are off-heap, we ha
ve to construct 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 std::unique_ptr<CancellableTaskFactory> create(T* thisObject, void (T
::*method)(), typename std::enable_if<IsGarbageCollectedType<T>::value>::type* =
nullptr) | 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 wrapUnique(new CancellableTaskFactory(WTF::bind(method, CrossThre
adWeakPersistentThisPointer<T>(thisObject)))); | 42 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, CrossThread
WeakPersistentThisPointer<T>(thisObject)))); |
| 43 } | 43 } |
| 44 | 44 |
| 45 template<typename T> | 45 template<typename T> |
| 46 static std::unique_ptr<CancellableTaskFactory> create(T* thisObject, void (T
::*method)(), typename std::enable_if<!IsGarbageCollectedType<T>::value>::type*
= nullptr) | 46 static PassOwnPtr<CancellableTaskFactory> create(T* thisObject, void (T::*me
thod)(), typename std::enable_if<!IsGarbageCollectedType<T>::value>::type* = nul
lptr) |
| 47 { | 47 { |
| 48 return wrapUnique(new CancellableTaskFactory(WTF::bind(method, thisObjec
t))); | 48 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, thisObject)
)); |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool isPending() const | 51 bool isPending() const |
| 52 { | 52 { |
| 53 return m_weakPtrFactory.hasWeakPtrs(); | 53 return m_weakPtrFactory.hasWeakPtrs(); |
| 54 } | 54 } |
| 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 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 85 WeakPtr<CancellableTaskFactory> m_weakPtr; | 85 WeakPtr<CancellableTaskFactory> m_weakPtr; |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 std::unique_ptr<SameThreadClosure> m_closure; | 88 std::unique_ptr<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 |