| 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/WebTaskRunner.h" | 9 #include "platform/WebTaskRunner.h" |
| 10 #include "platform/heap/Handle.h" | 10 #include "platform/heap/Handle.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 // TaskHandle handle = | 33 // TaskHandle handle = |
| 34 // task_runner->postCancellableTask(BLINK_FROM_HERE, std::move(task)); | 34 // task_runner->postCancellableTask(BLINK_FROM_HERE, std::move(task)); |
| 35 // handle.cancel(); | 35 // handle.cancel(); |
| 36 namespace blink { | 36 namespace blink { |
| 37 | 37 |
| 38 class PLATFORM_EXPORT CancellableTaskFactory { | 38 class PLATFORM_EXPORT CancellableTaskFactory { |
| 39 WTF_MAKE_NONCOPYABLE(CancellableTaskFactory); | 39 WTF_MAKE_NONCOPYABLE(CancellableTaskFactory); |
| 40 USING_FAST_MALLOC(CancellableTaskFactory); | 40 USING_FAST_MALLOC(CancellableTaskFactory); |
| 41 | 41 |
| 42 public: | 42 public: |
| 43 // A pair of mutually exclusive factory methods are provided for constructing | 43 // As WTF::Closure objects are off-heap, we have to construct the closure in |
| 44 // a CancellableTaskFactory, one for when a Oilpan heap object owns a | 44 // such a manner that it doesn't end up referring back to the owning heap |
| 45 // CancellableTaskFactory, and one when that owning object isn't controlled | 45 // object with a strong Persistent<> GC root reference. If we do, this will |
| 46 // by Oilpan. | 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.) |
| 47 // | 50 // |
| 48 // In the Oilpan case, as WTF::Closure objects are off-heap, we have to | 51 // DEPRECATED: Please use WebTaskRunner::postCancellableTask instead. |
| 49 // construct the closure in such a manner that it doesn't end up referring | 52 // (https://crbug.com/665285) |
| 50 // back to the owning heap object with a strong Persistent<> GC root | |
| 51 // reference. If we do, this will create a heap <-> off-heap cycle and leak, | |
| 52 // the owning object can never be GCed. Instead, the closure will keep an | |
| 53 // off-heap persistent reference of the weak, which will refer back to the | |
| 54 // owner heap object safely (but weakly.) | |
| 55 // | |
| 56 template <typename T> | 53 template <typename T> |
| 57 static std::unique_ptr<CancellableTaskFactory> create( | 54 static std::unique_ptr<CancellableTaskFactory> create( |
| 58 T* thisObject, | 55 T* thisObject, |
| 59 void (T::*method)(), | 56 void (T::*method)(), |
| 60 typename std::enable_if<IsGarbageCollectedType<T>::value>::type* = | 57 typename std::enable_if<IsGarbageCollectedType<T>::value>::type* = |
| 61 nullptr) { | 58 nullptr) { |
| 62 return wrapUnique(new CancellableTaskFactory( | 59 return wrapUnique(new CancellableTaskFactory( |
| 63 WTF::bind(method, wrapWeakPersistent(thisObject)))); | 60 WTF::bind(method, wrapWeakPersistent(thisObject)))); |
| 64 } | 61 } |
| 65 | 62 |
| 66 template <typename T> | |
| 67 static std::unique_ptr<CancellableTaskFactory> create( | |
| 68 T* thisObject, | |
| 69 void (T::*method)(), | |
| 70 typename std::enable_if<!IsGarbageCollectedType<T>::value>::type* = | |
| 71 nullptr) { | |
| 72 return wrapUnique(new CancellableTaskFactory( | |
| 73 WTF::bind(method, WTF::unretained(thisObject)))); | |
| 74 } | |
| 75 | |
| 76 bool isPending() const { return m_weakPtrFactory.hasWeakPtrs(); } | 63 bool isPending() const { return m_weakPtrFactory.hasWeakPtrs(); } |
| 77 | 64 |
| 78 void cancel(); | 65 void cancel(); |
| 79 | 66 |
| 80 // Returns a task that can be disabled by calling cancel(). The user takes | 67 // Returns a task that can be disabled by calling cancel(). The user takes |
| 81 // ownership of the task. Creating a new task cancels any previous ones. | 68 // ownership of the task. Creating a new task cancels any previous ones. |
| 82 WebTaskRunner::Task* cancelAndCreate(); | 69 WebTaskRunner::Task* cancelAndCreate(); |
| 83 | 70 |
| 84 protected: | 71 protected: |
| 85 // Only intended used by unit tests wanting to stack allocate and/or pass in a | 72 // Only intended used by unit tests wanting to stack allocate and/or pass in a |
| (...skipping 18 matching lines...) Expand all Loading... |
| 104 WeakPtr<CancellableTaskFactory> m_weakPtr; | 91 WeakPtr<CancellableTaskFactory> m_weakPtr; |
| 105 }; | 92 }; |
| 106 | 93 |
| 107 std::unique_ptr<WTF::Closure> m_closure; | 94 std::unique_ptr<WTF::Closure> m_closure; |
| 108 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory; | 95 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory; |
| 109 }; | 96 }; |
| 110 | 97 |
| 111 } // namespace blink | 98 } // namespace blink |
| 112 | 99 |
| 113 #endif // CancellableTaskFactory_h | 100 #endif // CancellableTaskFactory_h |
| OLD | NEW |