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/WebScheduler.h" | 10 #include "public/platform/WebScheduler.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 { | 41 { |
42 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, AllowCrossT
hreadWeakPersistent<T>(thisObject)))); | 42 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, AllowCrossT
hreadWeakPersistent<T>(thisObject)))); |
43 } | 43 } |
44 | 44 |
45 template<typename T> | 45 template<typename T> |
46 static PassOwnPtr<CancellableTaskFactory> create(T* thisObject, void (T::*me
thod)(), typename WTF::EnableIf<!IsGarbageCollectedType<T>::value>::Type* = null
ptr) | 46 static PassOwnPtr<CancellableTaskFactory> create(T* thisObject, void (T::*me
thod)(), typename WTF::EnableIf<!IsGarbageCollectedType<T>::value>::Type* = null
ptr) |
47 { | 47 { |
48 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, thisObject)
)); | 48 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, thisObject)
)); |
49 } | 49 } |
50 | 50 |
51 // Only intended used by unit tests. Please use leak safe create() factory m
ethod, if possible. | |
52 explicit CancellableTaskFactory(PassOwnPtr<Closure> closure) | |
53 : m_closure(closure) | |
54 , m_weakPtrFactory(this) | |
55 { | |
56 } | |
57 | |
58 bool isPending() const | 51 bool isPending() const |
59 { | 52 { |
60 return m_weakPtrFactory.hasWeakPtrs(); | 53 return m_weakPtrFactory.hasWeakPtrs(); |
61 } | 54 } |
62 | 55 |
63 void cancel(); | 56 void cancel(); |
64 | 57 |
65 // 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 |
66 // 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. |
67 WebTaskRunner::Task* cancelAndCreate(); | 60 WebTaskRunner::Task* cancelAndCreate(); |
68 | 61 |
| 62 protected: |
| 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. |
| 65 explicit CancellableTaskFactory(PassOwnPtr<Closure> closure) |
| 66 : m_closure(closure) |
| 67 , m_weakPtrFactory(this) |
| 68 { |
| 69 } |
| 70 |
69 private: | 71 private: |
70 class CancellableTask : public WebTaskRunner::Task { | 72 class CancellableTask : public WebTaskRunner::Task { |
71 WTF_MAKE_NONCOPYABLE(CancellableTask); | 73 WTF_MAKE_NONCOPYABLE(CancellableTask); |
72 | 74 |
73 public: | 75 public: |
74 explicit CancellableTask(WeakPtr<CancellableTaskFactory> weakPtr) | 76 explicit CancellableTask(WeakPtr<CancellableTaskFactory> weakPtr) |
75 : m_weakPtr(weakPtr) {} | 77 : m_weakPtr(weakPtr) {} |
76 | 78 |
77 ~CancellableTask() override {} | 79 ~CancellableTask() override {} |
78 | 80 |
79 void run() override; | 81 void run() override; |
80 | 82 |
81 private: | 83 private: |
82 WeakPtr<CancellableTaskFactory> m_weakPtr; | 84 WeakPtr<CancellableTaskFactory> m_weakPtr; |
83 }; | 85 }; |
84 | 86 |
85 OwnPtr<Closure> m_closure; | 87 OwnPtr<Closure> m_closure; |
86 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory; | 88 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory; |
87 }; | 89 }; |
88 | 90 |
89 } // namespace blink | 91 } // namespace blink |
90 | 92 |
91 #endif // CancellableTaskFactory_h | 93 #endif // CancellableTaskFactory_h |
OLD | NEW |