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

Side by Side Diff: Source/bindings/v8/ScriptPromiseResolverWithContext.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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 ScriptPromiseResolverWithContext_h 5 #ifndef ScriptPromiseResolverWithContext_h
6 #define ScriptPromiseResolverWithContext_h 6 #define ScriptPromiseResolverWithContext_h
7 7
8 #include "bindings/v8/ScopedPersistent.h" 8 #include "bindings/v8/ScopedPersistent.h"
9 #include "bindings/v8/ScriptPromise.h" 9 #include "bindings/v8/ScriptPromise.h"
10 #include "bindings/v8/ScriptPromiseResolver.h" 10 #include "bindings/v8/ScriptPromiseResolver.h"
11 #include "bindings/v8/ScriptState.h" 11 #include "bindings/v8/ScriptState.h"
12 #include "bindings/v8/V8Binding.h" 12 #include "bindings/v8/V8Binding.h"
13 #include "core/dom/ActiveDOMObject.h" 13 #include "core/dom/ActiveDOMObject.h"
14 #include "core/dom/ExecutionContext.h" 14 #include "core/dom/ExecutionContext.h"
15 #include "platform/Timer.h" 15 #include "platform/Timer.h"
16 #include "wtf/RefCounted.h" 16 #include "wtf/RefCounted.h"
17 #include "wtf/Vector.h" 17 #include "wtf/Vector.h"
18 #include <v8.h> 18 #include <v8.h>
19 19
20 namespace WebCore { 20 namespace WebCore {
21 21
22 // This class wraps ScriptPromiseResolver and provides the following 22 // This class wraps ScriptPromiseResolver and provides the following
23 // functionalities in addition to ScriptPromiseResolver's. 23 // functionalities in addition to ScriptPromiseResolver's.
24 // - A ScriptPromiseResolverWithContext retains a ScriptState. A caller 24 // - A ScriptPromiseResolverWithContext retains a ScriptState. A caller
25 // can call resolve or reject from outside of a V8 context. 25 // can call resolve or reject from outside of a V8 context.
26 // - This class is an ActiveDOMObject and keeps track of the associated 26 // - This class is an ActiveDOMObject and keeps track of the associated
27 // ExecutionContext state. When the ExecutionContext is suspended, 27 // ExecutionContext state. When the ExecutionContext is suspended,
28 // resolve or reject will be delayed. When it is stopped, resolve or reject 28 // resolve or reject will be delayed. When it is stopped, resolve or reject
29 // will be ignored. 29 // will be ignored.
30 class ScriptPromiseResolverWithContext : public ActiveDOMObject, public RefCount ed<ScriptPromiseResolverWithContext> { 30 class ScriptPromiseResolverWithContext FINAL : public ActiveDOMObject, public Re fCounted<ScriptPromiseResolverWithContext> {
31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolverWithContext); 31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolverWithContext);
32 32
33 public: 33 public:
34 static PassRefPtr<ScriptPromiseResolverWithContext> create(ScriptState* scri ptState) 34 static PassRefPtr<ScriptPromiseResolverWithContext> create(ScriptState* scri ptState)
35 { 35 {
36 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptP romiseResolverWithContext(scriptState)); 36 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptP romiseResolverWithContext(scriptState));
37 resolver->suspendIfNeeded(); 37 resolver->suspendIfNeeded();
38 return resolver.release(); 38 return resolver.release();
39 } 39 }
40 40
41 virtual ~ScriptPromiseResolverWithContext();
42
41 // Anything that can be passed to toV8Value can be passed to this function. 43 // Anything that can be passed to toV8Value can be passed to this function.
42 template <typename T> 44 template <typename T>
43 void resolve(T value) 45 void resolve(T value)
44 { 46 {
45 resolveOrReject(value, Resolving); 47 resolveOrReject(value, Resolving);
46 } 48 }
47 49
48 // Anything that can be passed to toV8Value can be passed to this function. 50 // Anything that can be passed to toV8Value can be passed to this function.
49 template <typename T> 51 template <typename T>
50 void reject(T value) 52 void reject(T value)
(...skipping 10 matching lines...) Expand all
61 return m_resolver ? m_resolver->promise() : ScriptPromise(); 63 return m_resolver ? m_resolver->promise() : ScriptPromise();
62 } 64 }
63 65
64 ScriptState* scriptState() const { return m_scriptState.get(); } 66 ScriptState* scriptState() const { return m_scriptState.get(); }
65 67
66 // ActiveDOMObject implementation. 68 // ActiveDOMObject implementation.
67 virtual void suspend() OVERRIDE; 69 virtual void suspend() OVERRIDE;
68 virtual void resume() OVERRIDE; 70 virtual void resume() OVERRIDE;
69 virtual void stop() OVERRIDE; 71 virtual void stop() OVERRIDE;
70 72
71 protected: 73 // This function registers |value| to the associated Promise as a hidden
72 explicit ScriptPromiseResolverWithContext(ScriptState*); 74 // property. As a result, |value| will be kept alive until one of the
75 // following events happens:
76 // - the Promise is resolved
77 // - the Promise is rejected
78 // - the Promise is disposed by the GC
79 // - this object is destructed
80 template<typename T>
81 void keepObjectWhilePending(const T& value)
82 {
83 ScriptState::Scope scope(m_scriptState.get());
84 keepObjectWhilePendingInternal(toV8Value(value));
85 }
73 86
74 private: 87 private:
75 enum ResolutionState { 88 enum ResolutionState {
76 Pending, 89 Pending,
77 Resolving, 90 Resolving,
78 Rejecting, 91 Rejecting,
79 ResolvedOrRejected, 92 ResolvedOrRejected,
80 }; 93 };
81 94
95 explicit ScriptPromiseResolverWithContext(ScriptState*);
96
82 template<typename T> 97 template<typename T>
83 v8::Handle<v8::Value> toV8Value(const T& value) 98 v8::Handle<v8::Value> toV8Value(const T& value)
84 { 99 {
85 return ToV8Value<ScriptPromiseResolverWithContext, v8::Handle<v8::Object > >::toV8Value(value, m_scriptState->context()->Global(), m_scriptState->isolate ()); 100 return ToV8Value<ScriptPromiseResolverWithContext, v8::Handle<v8::Object > >::toV8Value(value, m_scriptState->context()->Global(), m_scriptState->isolate ());
86 } 101 }
87 102
88 template <typename T> 103 template <typename T>
89 void resolveOrReject(T value, ResolutionState newState) 104 void resolveOrReject(T value, ResolutionState newState)
90 { 105 {
91 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) 106 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped())
92 return; 107 return;
93 m_state = newState; 108 m_state = newState;
94 // Retain this object until it is actually resolved or rejected. 109 // Retain this object until it is actually resolved or rejected.
95 // |deref| will be called in |clear|. 110 // |deref| will be called in |clear|.
96 ref(); 111 ref();
97 112
98 ScriptState::Scope scope(m_scriptState.get()); 113 ScriptState::Scope scope(m_scriptState.get());
99 m_value.set(m_scriptState->isolate(), toV8Value(value)); 114 m_value.set(m_scriptState->isolate(), toV8Value(value));
100 if (!executionContext()->activeDOMObjectsAreSuspended()) 115 if (!executionContext()->activeDOMObjectsAreSuspended())
101 resolveOrRejectImmediately(); 116 resolveOrRejectImmediately();
102 } 117 }
103 118
104 void resolveOrRejectImmediately(); 119 void resolveOrRejectImmediately();
105 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*); 120 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*);
106 void clear(); 121 void clear();
107 122
123 void keepObjectWhilePendingInternal(v8::Handle<v8::Value>);
124 void unregisterKeptObject();
125
108 ResolutionState m_state; 126 ResolutionState m_state;
109 const RefPtr<ScriptState> m_scriptState; 127 const RefPtr<ScriptState> m_scriptState;
110 Timer<ScriptPromiseResolverWithContext> m_timer; 128 Timer<ScriptPromiseResolverWithContext> m_timer;
111 RefPtr<ScriptPromiseResolver> m_resolver; 129 RefPtr<ScriptPromiseResolver> m_resolver;
112 ScopedPersistent<v8::Value> m_value; 130 ScopedPersistent<v8::Value> m_value;
113 }; 131 };
114 132
115 } // namespace WebCore 133 } // namespace WebCore
116 134
117 #endif // #ifndef ScriptPromiseResolverWithContext_h 135 #endif // #ifndef ScriptPromiseResolverWithContext_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698