| 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 "public/platform/WebScheduler.h" | 10 #include "public/platform/WebScheduler.h" |
| 10 #include "wtf/AddressSanitizer.h" | |
| 11 #include "wtf/Functional.h" | 11 #include "wtf/Functional.h" |
| 12 #include "wtf/Noncopyable.h" | 12 #include "wtf/Noncopyable.h" |
| 13 #include "wtf/OwnPtr.h" | 13 #include "wtf/OwnPtr.h" |
| 14 #include "wtf/PassOwnPtr.h" | 14 #include "wtf/PassOwnPtr.h" |
| 15 #include "wtf/RefCounted.h" | 15 #include "wtf/RefCounted.h" |
| 16 #include "wtf/TypeTraits.h" |
| 16 #include "wtf/WeakPtr.h" | 17 #include "wtf/WeakPtr.h" |
| 17 | 18 |
| 18 namespace blink { | 19 namespace blink { |
| 20 |
| 19 class TraceLocation; | 21 class TraceLocation; |
| 20 | 22 |
| 21 class PLATFORM_EXPORT CancellableTaskFactory { | 23 class PLATFORM_EXPORT CancellableTaskFactory { |
| 22 WTF_MAKE_NONCOPYABLE(CancellableTaskFactory); | 24 WTF_MAKE_NONCOPYABLE(CancellableTaskFactory); |
| 25 WTF_MAKE_FAST_ALLOCATED(CancellableTaskFactory); |
| 26 public: |
| 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 |
| 29 // CancellableTaskFactory, and one when that owning object isn't controlled |
| 30 // by Oilpan. |
| 31 // |
| 32 // In the Oilpan case, as WTF::Closure objects are off-heap, we have to cons
truct the |
| 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 |
| 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 |
| 37 // variety, which will refer back to the owner heap object safely (but weakl
y.) |
| 38 // |
| 39 template<typename T> |
| 40 static PassOwnPtr<CancellableTaskFactory> create(T* thisObject, void (T::*me
thod)(), typename WTF::EnableIf<IsGarbageCollectedType<T>::value>::Type* = nullp
tr) |
| 41 { |
| 42 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, AllowCrossT
hreadWeakPersistent<T>(thisObject)))); |
| 43 } |
| 23 | 44 |
| 24 public: | 45 template<typename T> |
| 46 static PassOwnPtr<CancellableTaskFactory> create(T* thisObject, void (T::*me
thod)(), typename WTF::EnableIf<!IsGarbageCollectedType<T>::value>::Type* = null
ptr) |
| 47 { |
| 48 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, thisObject)
)); |
| 49 } |
| 50 |
| 51 // Only intended used by unit tests. Please use leak safe create() factory m
ethod, if possible. |
| 25 explicit CancellableTaskFactory(PassOwnPtr<Closure> closure) | 52 explicit CancellableTaskFactory(PassOwnPtr<Closure> closure) |
| 26 : m_closure(closure) | 53 : m_closure(closure) |
| 27 #if defined(ADDRESS_SANITIZER) | |
| 28 , m_unpoisonBeforeUpdate(false) | |
| 29 #endif | |
| 30 , m_weakPtrFactory(this) | 54 , m_weakPtrFactory(this) |
| 31 { | 55 { |
| 32 } | 56 } |
| 33 | 57 |
| 34 bool isPending() const | 58 bool isPending() const |
| 35 { | 59 { |
| 36 return m_weakPtrFactory.hasWeakPtrs(); | 60 return m_weakPtrFactory.hasWeakPtrs(); |
| 37 } | 61 } |
| 38 | 62 |
| 39 void cancel(); | 63 void cancel(); |
| 40 | 64 |
| 41 // Returns a task that can be disabled by calling cancel(). The user takes | 65 // Returns a task that can be disabled by calling cancel(). The user takes |
| 42 // ownership of the task. Creating a new task cancels any previous ones. | 66 // ownership of the task. Creating a new task cancels any previous ones. |
| 43 WebTaskRunner::Task* cancelAndCreate(); | 67 WebTaskRunner::Task* cancelAndCreate(); |
| 44 | 68 |
| 45 #if defined(ADDRESS_SANITIZER) | |
| 46 // The CancellableTaskFactory part object might be within a poisoned heap | |
| 47 // object, hence CancellableTask::run() will access poisoned memory | |
| 48 // when reaching into the factory object to update its state. | |
| 49 // We will allow such access iff the task factory is marked as requiring | |
| 50 // unpoisoning first. | |
| 51 void setUnpoisonBeforeUpdate() { m_unpoisonBeforeUpdate = true; } | |
| 52 #endif | |
| 53 | |
| 54 private: | 69 private: |
| 55 class CancellableTask : public WebTaskRunner::Task { | 70 class CancellableTask : public WebTaskRunner::Task { |
| 56 WTF_MAKE_NONCOPYABLE(CancellableTask); | 71 WTF_MAKE_NONCOPYABLE(CancellableTask); |
| 57 | 72 |
| 58 public: | 73 public: |
| 59 explicit CancellableTask(WeakPtr<CancellableTaskFactory> weakPtr) | 74 explicit CancellableTask(WeakPtr<CancellableTaskFactory> weakPtr) |
| 60 : m_weakPtr(weakPtr) {} | 75 : m_weakPtr(weakPtr) {} |
| 61 | 76 |
| 62 ~CancellableTask() override {} | 77 ~CancellableTask() override {} |
| 63 | 78 |
| 64 void run() override; | 79 void run() override; |
| 65 | 80 |
| 66 private: | 81 private: |
| 67 WeakPtr<CancellableTaskFactory> m_weakPtr; | 82 WeakPtr<CancellableTaskFactory> m_weakPtr; |
| 68 }; | 83 }; |
| 69 | 84 |
| 70 OwnPtr<Closure> m_closure; | 85 OwnPtr<Closure> m_closure; |
| 71 #if defined(ADDRESS_SANITIZER) | |
| 72 bool m_unpoisonBeforeUpdate; | |
| 73 #endif | |
| 74 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory; | 86 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory; |
| 75 }; | 87 }; |
| 76 | 88 |
| 77 } // namespace blink | 89 } // namespace blink |
| 78 | 90 |
| 79 #endif // CancellableTaskFactory_h | 91 #endif // CancellableTaskFactory_h |
| OLD | NEW |