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 28 matching lines...) Expand all Loading... |
39 | 39 |
40 #if ENABLE(ASSERT) | 40 #if ENABLE(ASSERT) |
41 // Eagerly finalized so as to ensure valid access to executionContext() | 41 // Eagerly finalized so as to ensure valid access to executionContext() |
42 // from the destructor's assert. | 42 // from the destructor's assert. |
43 EAGERLY_FINALIZE(); | 43 EAGERLY_FINALIZE(); |
44 | 44 |
45 ~ScriptPromiseResolver() override | 45 ~ScriptPromiseResolver() override |
46 { | 46 { |
47 // This assertion fails if: | 47 // This assertion fails if: |
48 // - promise() is called at least once and | 48 // - promise() is called at least once and |
49 // - this resolver is destructed before it is resolved, rejected, the | 49 // - this resolver is destructed before it is resolved, rejected, |
50 // V8 isolate is terminated or the associated ExecutionContext is | 50 // detached, the V8 isolate is terminated or the associated |
51 // stopped. | 51 // ExecutionContext is stopped. |
52 ASSERT(m_state == ResolvedOrRejected || !m_isPromiseCalled || !scriptSta
te()->contextIsValid() || !executionContext() || executionContext()->activeDOMOb
jectsAreStopped()); | 52 ASSERT(m_state == Detached || !m_isPromiseCalled || !scriptState()->cont
extIsValid() || !executionContext() || executionContext()->activeDOMObjectsAreSt
opped()); |
53 } | 53 } |
54 #endif | 54 #endif |
55 | 55 |
56 // Anything that can be passed to toV8 can be passed to this function. | 56 // Anything that can be passed to toV8 can be passed to this function. |
57 template<typename T> | 57 template<typename T> |
58 void resolve(T value) | 58 void resolve(T value) |
59 { | 59 { |
60 resolveOrReject(value, Resolving); | 60 resolveOrReject(value, Resolving); |
61 } | 61 } |
62 | 62 |
(...skipping 17 matching lines...) Expand all Loading... |
80 m_isPromiseCalled = true; | 80 m_isPromiseCalled = true; |
81 #endif | 81 #endif |
82 return m_resolver.promise(); | 82 return m_resolver.promise(); |
83 } | 83 } |
84 | 84 |
85 ScriptState* scriptState() const { return m_scriptState.get(); } | 85 ScriptState* scriptState() const { return m_scriptState.get(); } |
86 | 86 |
87 // ActiveDOMObject implementation. | 87 // ActiveDOMObject implementation. |
88 void suspend() override; | 88 void suspend() override; |
89 void resume() override; | 89 void resume() override; |
90 void stop() override; | 90 void stop() override { detach(); } |
| 91 |
| 92 // Calling this function makes the resolver release its internal resources. |
| 93 // That means the associated promise will never be resolved or rejected |
| 94 // unless it's already been resolved or rejected. |
| 95 // Do not call this function unless you truly need the behavior. |
| 96 void detach(); |
91 | 97 |
92 // Once this function is called this resolver stays alive while the | 98 // Once this function is called this resolver stays alive while the |
93 // promise is pending and the associated ExecutionContext isn't stopped. | 99 // promise is pending and the associated ExecutionContext isn't stopped. |
94 void keepAliveWhilePending(); | 100 void keepAliveWhilePending(); |
95 | 101 |
96 DECLARE_VIRTUAL_TRACE(); | 102 DECLARE_VIRTUAL_TRACE(); |
97 | 103 |
98 protected: | 104 protected: |
99 // You need to call suspendIfNeeded after the construction because | 105 // You need to call suspendIfNeeded after the construction because |
100 // this is an ActiveDOMObject. | 106 // this is an ActiveDOMObject. |
101 explicit ScriptPromiseResolver(ScriptState*); | 107 explicit ScriptPromiseResolver(ScriptState*); |
102 | 108 |
103 private: | 109 private: |
104 typedef ScriptPromise::InternalResolver Resolver; | 110 typedef ScriptPromise::InternalResolver Resolver; |
105 enum ResolutionState { | 111 enum ResolutionState { |
106 Pending, | 112 Pending, |
107 Resolving, | 113 Resolving, |
108 Rejecting, | 114 Rejecting, |
109 ResolvedOrRejected, | 115 Detached, |
110 }; | 116 }; |
111 | 117 |
112 template<typename T> | 118 template<typename T> |
113 void resolveOrReject(T value, ResolutionState newState) | 119 void resolveOrReject(T value, ResolutionState newState) |
114 { | 120 { |
115 if (m_state != Pending || !scriptState()->contextIsValid() || !execution
Context() || executionContext()->activeDOMObjectsAreStopped()) | 121 if (m_state != Pending || !scriptState()->contextIsValid() || !execution
Context() || executionContext()->activeDOMObjectsAreStopped()) |
116 return; | 122 return; |
117 ASSERT(newState == Resolving || newState == Rejecting); | 123 ASSERT(newState == Resolving || newState == Rejecting); |
118 m_state = newState; | 124 m_state = newState; |
119 | 125 |
120 ScriptState::Scope scope(m_scriptState.get()); | 126 ScriptState::Scope scope(m_scriptState.get()); |
121 m_value.set( | 127 m_value.set( |
122 m_scriptState->isolate(), | 128 m_scriptState->isolate(), |
123 toV8(value, m_scriptState->context()->Global(), m_scriptState->isola
te())); | 129 toV8(value, m_scriptState->context()->Global(), m_scriptState->isola
te())); |
124 | 130 |
125 if (executionContext()->activeDOMObjectsAreSuspended()) { | 131 if (executionContext()->activeDOMObjectsAreSuspended()) { |
126 // Retain this object until it is actually resolved or rejected. | 132 // Retain this object until it is actually resolved or rejected. |
127 keepAliveWhilePending(); | 133 keepAliveWhilePending(); |
128 return; | 134 return; |
129 } | 135 } |
130 resolveOrRejectImmediately(); | 136 resolveOrRejectImmediately(); |
131 } | 137 } |
132 | 138 |
133 void resolveOrRejectImmediately(); | 139 void resolveOrRejectImmediately(); |
134 void onTimerFired(Timer<ScriptPromiseResolver>*); | 140 void onTimerFired(Timer<ScriptPromiseResolver>*); |
135 void clear(); | |
136 | 141 |
137 ResolutionState m_state; | 142 ResolutionState m_state; |
138 const RefPtr<ScriptState> m_scriptState; | 143 const RefPtr<ScriptState> m_scriptState; |
139 Timer<ScriptPromiseResolver> m_timer; | 144 Timer<ScriptPromiseResolver> m_timer; |
140 Resolver m_resolver; | 145 Resolver m_resolver; |
141 ScopedPersistent<v8::Value> m_value; | 146 ScopedPersistent<v8::Value> m_value; |
142 | 147 |
143 // To support keepAliveWhilePending(), this object needs to keep itself | 148 // To support keepAliveWhilePending(), this object needs to keep itself |
144 // alive while in that state. | 149 // alive while in that state. |
145 SelfKeepAlive<ScriptPromiseResolver> m_keepAlive; | 150 SelfKeepAlive<ScriptPromiseResolver> m_keepAlive; |
146 | 151 |
147 #if ENABLE(ASSERT) | 152 #if ENABLE(ASSERT) |
148 // True if promise() is called. | 153 // True if promise() is called. |
149 bool m_isPromiseCalled; | 154 bool m_isPromiseCalled; |
150 #endif | 155 #endif |
151 }; | 156 }; |
152 | 157 |
153 } // namespace blink | 158 } // namespace blink |
154 | 159 |
155 #endif // ScriptPromiseResolver_h | 160 #endif // ScriptPromiseResolver_h |
OLD | NEW |