OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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. | |
4 | |
5 #ifndef AsyncInitializerResolver_h | |
6 #define AsyncInitializerResolver_h | |
7 | |
8 #include "bindings/v8/ScriptPromiseResolverWithContext.h" | |
9 #include "core/dom/ContextLifecycleObserver.h" | |
10 #include "core/dom/ExecutionContext.h" | |
11 #include "platform/heap/Heap.h" | |
12 #include "wtf/OwnPtr.h" | |
13 #include "wtf/RefPtr.h" | |
14 #include "wtf/WeakPtr.h" | |
15 | |
16 namespace WebCore { | |
17 | |
18 // AsyncInitializerResolver is a helper class of asynchronous initialization | |
19 // using Promises. Once AsyncInitializerResolver<T> is created, the resolver | |
20 // will live until one of the following conditions is met: | |
21 // - the associated promise is resolved | |
22 // - the associated promise is rejected | |
23 // - the associated execution context is destroyed | |
24 // | |
25 // AsyncInitializerResolver<Initializer> takes an actual initializer. | |
26 // It is guaranteed the resolver stays alive while the initializer is alive | |
27 // after start() is called, | |
28 // i.e. You can access the resolver pointer set by start() in the initializer | |
29 // If you want to access the resolver outside of the initializer, | |
30 // use a weak pointer. | |
31 // | |
32 template<typename T> | |
33 class AsyncInitializerResolver : public ContextLifecycleObserver { | |
34 public: | |
35 typedef T Initializer; | |
36 | |
37 static WeakPtr<AsyncInitializerResolver<T> > create(ScriptState* scriptState ) | |
38 { | |
39 AsyncInitializerResolver* resolver = new AsyncInitializerResolver(script State); | |
40 return resolver->weakPtr(); | |
41 } | |
42 | |
43 void start(PassOwnPtr<Initializer> initializer) | |
44 { | |
45 ASSERT(!m_initializer); | |
46 m_initializer = initializer; | |
47 m_initializer->start(this); | |
48 } | |
49 | |
50 static ScriptPromise createAndStart(ScriptState* scriptState, PassOwnPtr<T> initializer) | |
51 { | |
52 AsyncInitializerResolver* resolver = new AsyncInitializerResolver(script State); | |
53 ScriptPromise promise = resolver->promise(); | |
54 resolver->start(initializer); | |
55 return promise; | |
56 } | |
57 | |
58 virtual ~AsyncInitializerResolver() { } | |
59 | |
60 ScriptPromise promise() { return m_resolver->promise(); } | |
61 ScriptState* scriptState() const { return m_resolver->scriptState(); } | |
62 template<typename U> void resolve(U u) | |
63 { | |
64 m_resolver->resolve(u); | |
65 m_protect.clear(); | |
66 } | |
67 template<typename U> void reject(U u) | |
68 { | |
69 m_resolver->reject(u); | |
70 m_protect.clear(); | |
71 } | |
72 | |
73 // LifecycleObserver implementation. | |
74 // Because the lifecycle context is held in the associated execution | |
75 // context, this function is called when the execution context is | |
76 // destroyed. | |
77 virtual void contextDestroyed() OVERRIDE | |
78 { | |
79 ContextLifecycleObserver::contextDestroyed(); | |
80 m_initializer->contextDestroyed(); | |
81 // |this| will be deleted in the next Oilpan GC. | |
82 m_protect.clear(); | |
83 } | |
84 | |
85 WeakPtr<AsyncInitializerResolver<Initializer> > weakPtr() { return m_factory .createWeakPtr(); } | |
86 | |
87 private: | |
88 class Holder : public GarbageCollectedFinalized<Holder> { | |
89 public: | |
90 Holder(PassOwnPtr<AsyncInitializerResolver<T> > target) : m_protect(targ et) { } | |
91 virtual void trace(Visitor*) { } | |
92 | |
93 private: | |
94 OwnPtr<AsyncInitializerResolver<T> > m_protect; | |
95 }; | |
96 | |
97 AsyncInitializerResolver(ScriptState* scriptState) | |
98 : ContextLifecycleObserver(scriptState->executionContext()) | |
99 , m_resolver(ScriptPromiseResolverWithContext::create(scriptState)) | |
100 , m_factory(this) | |
101 , m_protect(new Holder(adoptPtr(this))) | |
102 { | |
103 } | |
104 | |
105 RefPtr<ScriptPromiseResolverWithContext> m_resolver; | |
106 OwnPtr<Initializer> m_initializer; | |
107 WeakPtrFactory<AsyncInitializerResolver<Initializer> > m_factory; | |
108 // In order to keep alive self until one of the termination conditions | |
109 // is met, AsyncInitizlierResolver has an OwnPtr referencing self. | |
110 // Clearing it means deleting it. | |
111 Persistent<Holder> m_protect; | |
kouhei (in TOK)
2014/06/05 07:54:47
Why don't we make AsyncInitializerResolver Garbage
yhirano
2014/06/05 09:09:19
My compiler complains at having Persistent in Garb
| |
112 }; | |
113 | |
114 } // namespace WebCore | |
115 | |
116 #endif // #ifndef AsyncInitializerResolver_h | |
OLD | NEW |