Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / scheduler / CancellableTaskFactory.h
blobdc378d3a4927a7e3bfa55892313a1d67e2f64704
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
3 // found in the LICENSE file.
5 #ifndef CancellableTaskFactory_h
6 #define CancellableTaskFactory_h
8 #include "platform/PlatformExport.h"
9 #include "platform/heap/Handle.h"
10 #include "public/platform/WebScheduler.h"
11 #include "wtf/Functional.h"
12 #include "wtf/Noncopyable.h"
13 #include "wtf/OwnPtr.h"
14 #include "wtf/PassOwnPtr.h"
15 #include "wtf/RefCounted.h"
16 #include "wtf/TypeTraits.h"
17 #include "wtf/WeakPtr.h"
19 namespace blink {
21 class TraceLocation;
23 class PLATFORM_EXPORT 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 constructing
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.
32 // In the Oilpan case, as WTF::Closure objects are off-heap, we have to construct the
33 // closure in such a manner that it doesn't end up referring back to the owning 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 weak
37 // variety, which will refer back to the owner heap object safely (but weakly.)
39 template<typename T>
40 static PassOwnPtr<CancellableTaskFactory> create(T* thisObject, void (T::*method)(), typename WTF::EnableIf<IsGarbageCollectedType<T>::value>::Type* = nullptr)
42 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, AllowCrossThreadWeakPersistent<T>(thisObject))));
45 template<typename T>
46 static PassOwnPtr<CancellableTaskFactory> create(T* thisObject, void (T::*method)(), typename WTF::EnableIf<!IsGarbageCollectedType<T>::value>::Type* = nullptr)
48 return adoptPtr(new CancellableTaskFactory(WTF::bind(method, thisObject)));
51 bool isPending() const
53 return m_weakPtrFactory.hasWeakPtrs();
56 void cancel();
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.
60 WebTaskRunner::Task* cancelAndCreate();
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)
71 private:
72 class CancellableTask : public WebTaskRunner::Task {
73 WTF_MAKE_NONCOPYABLE(CancellableTask);
75 public:
76 explicit CancellableTask(WeakPtr<CancellableTaskFactory> weakPtr)
77 : m_weakPtr(weakPtr) {}
79 ~CancellableTask() override {}
81 void run() override;
83 private:
84 WeakPtr<CancellableTaskFactory> m_weakPtr;
87 OwnPtr<Closure> m_closure;
88 WeakPtrFactory<CancellableTaskFactory> m_weakPtrFactory;
91 } // namespace blink
93 #endif // CancellableTaskFactory_h