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 "wtf/OwnPtr.h" | |
12 #include "wtf/RefPtr.h" | |
13 #include "wtf/WeakPtr.h" | |
14 | |
15 namespace WebCore { | |
16 | |
17 // AsyncInitializerResolver is a helper class of asynchronous initialization | |
18 // using Promises. Once AsyncInitializerResolver<T> is created, the resolver | |
19 // will live until one of the following conditions is met: | |
20 // - the associated promise is resolved | |
21 // - the associated promise is rejected | |
22 // - the associated execution context is destroyed | |
23 // | |
24 // AsyncInitializerResolver<Initializer> takes an actual initializer. | |
25 // It is guaranteed the resolver stays alive while the initializer is alive | |
26 // after start() is called, | |
27 // i.e. You can access the resolver pointer set by start() in the initializer | |
28 // If you want to access the resolver outside of the initializer, | |
29 // use a weak pointer. | |
30 // | |
31 // Note that an AsyncInitializerResolver must not have another | |
32 // AsyncInitializerResolver directly or undirectly, due to a restriction of | |
33 // LifecycleNotifier and LifecycleObserver. | |
34 template<typename T> | |
35 class AsyncInitializerResolver : public LifecycleObserver<AsyncInitializerContex t> { | |
36 public: | |
37 typedef T Initializer; | |
38 | |
39 static WeakPtr<AsyncInitializerResolver<T> > create(ScriptState* scriptState ) | |
kouhei (in TOK)
2014/06/05 07:54:47
Is this used?
yhirano
2014/06/06 07:40:48
Removed.
| |
40 { | |
41 AsyncInitializerResolver* resolver = new AsyncInitializerResolver(script State); | |
42 return resolver->weakPtr(); | |
43 } | |
44 | |
45 void start(PassOwnPtr<Initializer> initializer) | |
46 { | |
47 ASSERT(!m_initializer); | |
48 m_initializer = initializer; | |
49 m_initializer->start(this); | |
50 } | |
51 | |
52 static ScriptPromise createAndStart(ScriptState* scriptState, PassOwnPtr<T> initializer) | |
53 { | |
54 AsyncInitializerResolver* resolver = new AsyncInitializerResolver(script State); | |
55 ScriptPromise promise = resolver->promise(); | |
56 resolver->start(initializer); | |
57 return promise; | |
58 } | |
59 | |
60 virtual ~AsyncInitializerResolver() { } | |
61 | |
62 ScriptPromise promise() { return m_resolver->promise(); } | |
63 ScriptState* scriptState() const { return m_resolver->scriptState(); } | |
64 template<typename U> void resolve(U u) | |
65 { | |
66 m_resolver->resolve(u); | |
67 m_protect = 0; | |
68 } | |
69 template<typename U> void reject(U u) | |
70 { | |
71 m_resolver->reject(u); | |
72 m_protect = 0; | |
73 } | |
74 | |
75 // LifecycleObserver implementation. | |
76 // Because the lifecycle context is held in the associated execution | |
77 // context, this function is called when the execution context is | |
78 // destroyed. | |
79 virtual void contextDestroyed() OVERRIDE | |
80 { | |
81 m_initializer->contextDestroyed(); | |
82 LifecycleObserver<AsyncInitializerContext>::contextDestroyed(); | |
83 m_protect = 0; | |
84 } | |
85 | |
86 WeakPtr<AsyncInitializerResolver<Initializer> > weakPtr() { return m_factory .createWeakPtr(); } | |
87 | |
88 private: | |
89 AsyncInitializerResolver(ScriptState* scriptState) | |
90 : LifecycleObserver<AsyncInitializerContext>(&scriptState->executionCont ext()->asyncInitializerContext()) | |
91 , m_resolver(ScriptPromiseResolverWithContext::create(scriptState)) | |
92 , m_factory(this) | |
93 , m_protect(adoptPtr(this)) | |
94 { | |
95 } | |
96 | |
97 RefPtr<ScriptPromiseResolverWithContext> m_resolver; | |
98 OwnPtr<Initializer> m_initializer; | |
99 WeakPtrFactory<AsyncInitializerResolver<Initializer> > m_factory; | |
100 // In order to keep alive self until one of the termination conditions | |
101 // is met, AsyncInitizlier has an OwnPtr referencing self. | |
102 // Clearing it means deleting it. | |
103 OwnPtr<AsyncInitializerResolver<Initializer> > m_protect; | |
104 }; | |
105 | |
106 // | |
107 // These functions should be specialized for each LifecycleObserver instances. | |
108 // | |
109 template<> void observerContext(AsyncInitializerContext*, LifecycleObserver<Asyn cInitializerContext>*); | |
110 template<> void unobserverContext(AsyncInitializerContext*, LifecycleObserver<As yncInitializerContext>*); | |
111 | |
112 } // namespace WebCore | |
113 | |
114 #endif // #ifndef AsyncInitializerResolver_h | |
OLD | NEW |