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 20 matching lines...) Expand all Loading... |
31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | 31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); |
32 public: | 32 public: |
33 static ScriptPromiseResolver* create(ScriptState* scriptState) | 33 static ScriptPromiseResolver* create(ScriptState* scriptState) |
34 { | 34 { |
35 ScriptPromiseResolver* resolver = new ScriptPromiseResolver(scriptState)
; | 35 ScriptPromiseResolver* resolver = new ScriptPromiseResolver(scriptState)
; |
36 resolver->suspendIfNeeded(); | 36 resolver->suspendIfNeeded(); |
37 return resolver; | 37 return resolver; |
38 } | 38 } |
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 getExecutionContext() |
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, | 49 // - this resolver is destructed before it is resolved, rejected, |
50 // detached, the V8 isolate is terminated or the associated | 50 // detached, the V8 isolate is terminated or the associated |
51 // ExecutionContext is stopped. | 51 // ExecutionContext is stopped. |
52 ASSERT(m_state == Detached || !m_isPromiseCalled || !scriptState()->cont
extIsValid() || !executionContext() || executionContext()->activeDOMObjectsAreSt
opped()); | 52 ASSERT(m_state == Detached || !m_isPromiseCalled || !getScriptState()->c
ontextIsValid() || !getExecutionContext() || getExecutionContext()->activeDOMObj
ectsAreStopped()); |
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 |
63 // Anything that can be passed to toV8 can be passed to this function. | 63 // Anything that can be passed to toV8 can be passed to this function. |
64 template<typename T> | 64 template<typename T> |
65 void reject(T value) | 65 void reject(T value) |
66 { | 66 { |
67 resolveOrReject(value, Rejecting); | 67 resolveOrReject(value, Rejecting); |
68 } | 68 } |
69 | 69 |
70 void resolve() { resolve(ToV8UndefinedGenerator()); } | 70 void resolve() { resolve(ToV8UndefinedGenerator()); } |
71 void reject() { reject(ToV8UndefinedGenerator()); } | 71 void reject() { reject(ToV8UndefinedGenerator()); } |
72 | 72 |
73 ScriptState* scriptState() { return m_scriptState.get(); } | 73 ScriptState* getScriptState() { return m_scriptState.get(); } |
74 | 74 |
75 // Note that an empty ScriptPromise will be returned after resolve or | 75 // Note that an empty ScriptPromise will be returned after resolve or |
76 // reject is called. | 76 // reject is called. |
77 ScriptPromise promise() | 77 ScriptPromise promise() |
78 { | 78 { |
79 #if ENABLE(ASSERT) | 79 #if ENABLE(ASSERT) |
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* getScriptState() 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 { detach(); } | 90 void stop() override { detach(); } |
91 | 91 |
92 // Calling this function makes the resolver release its internal resources. | 92 // Calling this function makes the resolver release its internal resources. |
93 // That means the associated promise will never be resolved or rejected | 93 // That means the associated promise will never be resolved or rejected |
94 // unless it's already been resolved or rejected. | 94 // unless it's already been resolved or rejected. |
95 // Do not call this function unless you truly need the behavior. | 95 // Do not call this function unless you truly need the behavior. |
(...skipping 15 matching lines...) Expand all Loading... |
111 enum ResolutionState { | 111 enum ResolutionState { |
112 Pending, | 112 Pending, |
113 Resolving, | 113 Resolving, |
114 Rejecting, | 114 Rejecting, |
115 Detached, | 115 Detached, |
116 }; | 116 }; |
117 | 117 |
118 template<typename T> | 118 template<typename T> |
119 void resolveOrReject(T value, ResolutionState newState) | 119 void resolveOrReject(T value, ResolutionState newState) |
120 { | 120 { |
121 if (m_state != Pending || !scriptState()->contextIsValid() || !execution
Context() || executionContext()->activeDOMObjectsAreStopped()) | 121 if (m_state != Pending || !getScriptState()->contextIsValid() || !getExe
cutionContext() || getExecutionContext()->activeDOMObjectsAreStopped()) |
122 return; | 122 return; |
123 ASSERT(newState == Resolving || newState == Rejecting); | 123 ASSERT(newState == Resolving || newState == Rejecting); |
124 m_state = newState; | 124 m_state = newState; |
125 | 125 |
126 ScriptState::Scope scope(m_scriptState.get()); | 126 ScriptState::Scope scope(m_scriptState.get()); |
127 m_value.set( | 127 m_value.set( |
128 m_scriptState->isolate(), | 128 m_scriptState->isolate(), |
129 toV8(value, m_scriptState->context()->Global(), m_scriptState->isola
te())); | 129 toV8(value, m_scriptState->context()->Global(), m_scriptState->isola
te())); |
130 | 130 |
131 if (executionContext()->activeDOMObjectsAreSuspended()) { | 131 if (getExecutionContext()->activeDOMObjectsAreSuspended()) { |
132 // Retain this object until it is actually resolved or rejected. | 132 // Retain this object until it is actually resolved or rejected. |
133 keepAliveWhilePending(); | 133 keepAliveWhilePending(); |
134 return; | 134 return; |
135 } | 135 } |
136 resolveOrRejectImmediately(); | 136 resolveOrRejectImmediately(); |
137 } | 137 } |
138 | 138 |
139 void resolveOrRejectImmediately(); | 139 void resolveOrRejectImmediately(); |
140 void onTimerFired(Timer<ScriptPromiseResolver>*); | 140 void onTimerFired(Timer<ScriptPromiseResolver>*); |
141 | 141 |
142 ResolutionState m_state; | 142 ResolutionState m_state; |
143 const RefPtr<ScriptState> m_scriptState; | 143 const RefPtr<ScriptState> m_scriptState; |
144 Timer<ScriptPromiseResolver> m_timer; | 144 Timer<ScriptPromiseResolver> m_timer; |
145 Resolver m_resolver; | 145 Resolver m_resolver; |
146 ScopedPersistent<v8::Value> m_value; | 146 ScopedPersistent<v8::Value> m_value; |
147 | 147 |
148 // To support keepAliveWhilePending(), this object needs to keep itself | 148 // To support keepAliveWhilePending(), this object needs to keep itself |
149 // alive while in that state. | 149 // alive while in that state. |
150 SelfKeepAlive<ScriptPromiseResolver> m_keepAlive; | 150 SelfKeepAlive<ScriptPromiseResolver> m_keepAlive; |
151 | 151 |
152 #if ENABLE(ASSERT) | 152 #if ENABLE(ASSERT) |
153 // True if promise() is called. | 153 // True if promise() is called. |
154 bool m_isPromiseCalled; | 154 bool m_isPromiseCalled; |
155 #endif | 155 #endif |
156 }; | 156 }; |
157 | 157 |
158 } // namespace blink | 158 } // namespace blink |
159 | 159 |
160 #endif // ScriptPromiseResolver_h | 160 #endif // ScriptPromiseResolver_h |
OLD | NEW |