OLD | NEW |
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 ScriptPromiseResolver_h | 5 #ifndef ScriptPromiseResolver_h |
6 #define ScriptPromiseResolver_h | 6 #define ScriptPromiseResolver_h |
7 | 7 |
8 #include "bindings/core/v8/ScopedPersistent.h" | 8 #include "bindings/core/v8/ScopedPersistent.h" |
9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 // This class wraps v8::Promise::Resolver and provides the following | 21 // This class wraps v8::Promise::Resolver and provides the following |
22 // functionalities. | 22 // functionalities. |
23 // - A ScriptPromiseResolver retains a ScriptState. A caller | 23 // - A ScriptPromiseResolver retains a ScriptState. A caller |
24 // can call resolve or reject from outside of a V8 context. | 24 // can call resolve or reject from outside of a V8 context. |
25 // - This class is an ActiveDOMObject and keeps track of the associated | 25 // - This class is an ActiveDOMObject and keeps track of the associated |
26 // ExecutionContext state. When the ExecutionContext is suspended, | 26 // ExecutionContext state. When the ExecutionContext is suspended, |
27 // resolve or reject will be delayed. When it is stopped, resolve or reject | 27 // resolve or reject will be delayed. When it is stopped, resolve or reject |
28 // will be ignored. | 28 // will be ignored. |
29 class CORE_EXPORT ScriptPromiseResolver : public RefCountedWillBeRefCountedGarba
geCollected<ScriptPromiseResolver>, public ActiveDOMObject { | 29 class CORE_EXPORT ScriptPromiseResolver : public RefCountedWillBeRefCountedGarba
geCollected<ScriptPromiseResolver>, public ActiveDOMObject { |
30 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ScriptPromiseResolver); | 30 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ScriptPromiseResolver); |
31 #if ENABLE(ASSERT) | |
32 WILL_BE_USING_PRE_FINALIZER(ScriptPromiseResolver, assertNotPending); | |
33 #endif | |
34 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | 31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); |
35 public: | 32 public: |
36 static PassRefPtrWillBeRawPtr<ScriptPromiseResolver> create(ScriptState* scr
iptState) | 33 static PassRefPtrWillBeRawPtr<ScriptPromiseResolver> create(ScriptState* scr
iptState) |
37 { | 34 { |
38 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = adoptRefWillBeNoop(
new ScriptPromiseResolver(scriptState)); | 35 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = adoptRefWillBeNoop(
new ScriptPromiseResolver(scriptState)); |
39 resolver->suspendIfNeeded(); | 36 resolver->suspendIfNeeded(); |
40 return resolver.release(); | 37 return resolver.release(); |
41 } | 38 } |
42 | 39 |
43 #if !ENABLE(OILPAN) && ENABLE(ASSERT) | 40 #if ENABLE(ASSERT) |
| 41 // Eagerly finalized so as to ensure valid access to executionContext() |
| 42 // from the destructor's assert. |
| 43 EAGERLY_FINALIZE(); |
| 44 |
44 ~ScriptPromiseResolver() override | 45 ~ScriptPromiseResolver() override |
45 { | 46 { |
46 assertNotPending(); | 47 // This assertion fails if: |
| 48 // - promise() is called at least once and |
| 49 // - this resolver is destructed before it is resolved, rejected or |
| 50 // the associated ExecutionContext is stopped. |
| 51 ASSERT(m_state == ResolvedOrRejected || !m_isPromiseCalled || !execution
Context() || executionContext()->activeDOMObjectsAreStopped()); |
47 } | 52 } |
48 #endif | 53 #endif |
49 | 54 |
50 // Anything that can be passed to toV8 can be passed to this function. | 55 // Anything that can be passed to toV8 can be passed to this function. |
51 template<typename T> | 56 template<typename T> |
52 void resolve(T value) | 57 void resolve(T value) |
53 { | 58 { |
54 resolveOrReject(value, Resolving); | 59 resolveOrReject(value, Resolving); |
55 } | 60 } |
56 | 61 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 Pending, | 105 Pending, |
101 Resolving, | 106 Resolving, |
102 Rejecting, | 107 Rejecting, |
103 ResolvedOrRejected, | 108 ResolvedOrRejected, |
104 }; | 109 }; |
105 enum LifetimeMode { | 110 enum LifetimeMode { |
106 Default, | 111 Default, |
107 KeepAliveWhilePending, | 112 KeepAliveWhilePending, |
108 }; | 113 }; |
109 | 114 |
110 #if ENABLE(ASSERT) | |
111 void assertNotPending() | |
112 { | |
113 // This assertion fails if: | |
114 // - promise() is called at least once and | |
115 // - this resolver is destructed before it is resolved, rejected or | |
116 // the associated ExecutionContext is stopped. | |
117 // This function cannot be run in the destructor if | |
118 // ScriptPromiseResolver is on-heap. | |
119 ASSERT(m_state == ResolvedOrRejected || !m_isPromiseCalled || !execution
Context() || executionContext()->activeDOMObjectsAreStopped()); | |
120 } | |
121 #endif | |
122 | |
123 template<typename T> | 115 template<typename T> |
124 void resolveOrReject(T value, ResolutionState newState) | 116 void resolveOrReject(T value, ResolutionState newState) |
125 { | 117 { |
126 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | 118 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) |
127 return; | 119 return; |
128 ASSERT(newState == Resolving || newState == Rejecting); | 120 ASSERT(newState == Resolving || newState == Rejecting); |
129 m_state = newState; | 121 m_state = newState; |
130 // Retain this object until it is actually resolved or rejected. | 122 // Retain this object until it is actually resolved or rejected. |
131 // |deref| will be called in |clear|. | 123 // |deref| will be called in |clear|. |
132 ref(); | 124 ref(); |
(...skipping 18 matching lines...) Expand all Loading... |
151 ScopedPersistent<v8::Value> m_value; | 143 ScopedPersistent<v8::Value> m_value; |
152 #if ENABLE(ASSERT) | 144 #if ENABLE(ASSERT) |
153 // True if promise() is called. | 145 // True if promise() is called. |
154 bool m_isPromiseCalled; | 146 bool m_isPromiseCalled; |
155 #endif | 147 #endif |
156 }; | 148 }; |
157 | 149 |
158 } // namespace blink | 150 } // namespace blink |
159 | 151 |
160 #endif // ScriptPromiseResolver_h | 152 #endif // ScriptPromiseResolver_h |
OLD | NEW |