Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: Source/core/dom/AsyncInitializerResolver.h

Issue 311733004: Introduce KeepAliveWhilePending to ScriptPromiseResolverWithContext. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@refactor-webmidi-initialization
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/core.gypi ('k') | Source/modules/webmidi/MIDIAccess.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
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 // i.e. You can access the raw resolver pointer set by start() in the
27 // initializer.
28 //
29 template<typename T>
30 class AsyncInitializerResolver : public ContextLifecycleObserver {
31 public:
32 typedef T Initializer;
33
34 static ScriptPromise start(ScriptState* scriptState, PassOwnPtr<Initializer> initializer)
35 {
36 AsyncInitializerResolver* resolver = new AsyncInitializerResolver(script State, initializer);
37 ScriptPromise promise = resolver->promise();
38 resolver->m_initializer->start(resolver);
39 return promise;
40 }
41
42 virtual ~AsyncInitializerResolver() { }
43
44 ScriptPromise promise() { return m_resolver->promise(); }
45 ScriptState* scriptState() const { return m_resolver->scriptState(); }
46 template<typename U> void resolve(U u)
47 {
48 m_resolver->resolve(u);
49 m_protect.clear();
50 }
51 template<typename U> void reject(U u)
52 {
53 m_resolver->reject(u);
54 m_protect.clear();
55 }
56
57 // LifecycleObserver implementation.
58 // Because the lifecycle context is held in the associated execution
59 // context, this function is called when the execution context is
60 // destroyed.
61 virtual void contextDestroyed() OVERRIDE
62 {
63 ContextLifecycleObserver::contextDestroyed();
64 m_initializer->contextDestroyed();
65 // |this| will be deleted in the next Oilpan GC.
66 m_protect.clear();
67 }
68
69 private:
70 class Holder : public GarbageCollectedFinalized<Holder> {
71 public:
72 Holder(PassOwnPtr<AsyncInitializerResolver<T> > target) : m_protect(targ et) { }
73 virtual void trace(Visitor*) { }
74
75 private:
76 OwnPtr<AsyncInitializerResolver<T> > m_protect;
77 };
78
79 AsyncInitializerResolver(ScriptState* scriptState, PassOwnPtr<Initializer> i nitializer)
80 : ContextLifecycleObserver(scriptState->executionContext())
81 , m_resolver(ScriptPromiseResolverWithContext::create(scriptState))
82 , m_initializer(initializer)
83 , m_protect(new Holder(adoptPtr(this)))
84 {
85 }
86
87 RefPtr<ScriptPromiseResolverWithContext> m_resolver;
88 OwnPtr<Initializer> m_initializer;
89 // In order to keep alive self until one of the termination conditions
90 // is met, AsyncInitizlierResolver has an OwnPtr referencing self.
91 // Clearing it means deleting it.
92 Persistent<Holder> m_protect;
haraken 2014/06/10 10:54:59 I wonder why you need oilpan to keep the Initializ
yhirano 2014/06/11 06:54:28 We can't destroy a ContextLifecycleObserver in ano
93 };
94
95 } // namespace WebCore
96
97 #endif // #ifndef AsyncInitializerResolver_h
OLDNEW
« no previous file with comments | « Source/core/core.gypi ('k') | Source/modules/webmidi/MIDIAccess.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698