| 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" |
| 11 #include "bindings/core/v8/ToV8.h" | 11 #include "bindings/core/v8/ToV8.h" |
| 12 #include "core/CoreExport.h" | 12 #include "core/CoreExport.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 <v8.h> | 17 #include <v8.h> |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 | 20 |
| 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 GarbageCollectedFinalized<Scrip
tPromiseResolver>, public ActiveDOMObject { |
| 30 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ScriptPromiseResolver); | 30 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ScriptPromiseResolver); |
| 31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | 31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); |
| 32 public: | 32 public: |
| 33 static PassRefPtrWillBeRawPtr<ScriptPromiseResolver> create(ScriptState* scr
iptState) | 33 static ScriptPromiseResolver* create(ScriptState* scriptState) |
| 34 { | 34 { |
| 35 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = adoptRefWillBeNoop(
new ScriptPromiseResolver(scriptState)); | 35 ScriptPromiseResolver* resolver = new ScriptPromiseResolver(scriptState)
; |
| 36 resolver->suspendIfNeeded(); | 36 resolver->suspendIfNeeded(); |
| 37 return resolver.release(); | 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 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: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 explicit ScriptPromiseResolver(ScriptState*); | 100 explicit ScriptPromiseResolver(ScriptState*); |
| 101 | 101 |
| 102 private: | 102 private: |
| 103 typedef ScriptPromise::InternalResolver Resolver; | 103 typedef ScriptPromise::InternalResolver Resolver; |
| 104 enum ResolutionState { | 104 enum ResolutionState { |
| 105 Pending, | 105 Pending, |
| 106 Resolving, | 106 Resolving, |
| 107 Rejecting, | 107 Rejecting, |
| 108 ResolvedOrRejected, | 108 ResolvedOrRejected, |
| 109 }; | 109 }; |
| 110 enum LifetimeMode { | |
| 111 Default, | |
| 112 KeepAliveWhilePending, | |
| 113 }; | |
| 114 | 110 |
| 115 template<typename T> | 111 template<typename T> |
| 116 void resolveOrReject(T value, ResolutionState newState) | 112 void resolveOrReject(T value, ResolutionState newState) |
| 117 { | 113 { |
| 118 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | 114 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) |
| 119 return; | 115 return; |
| 120 ASSERT(newState == Resolving || newState == Rejecting); | 116 ASSERT(newState == Resolving || newState == Rejecting); |
| 121 m_state = newState; | 117 m_state = newState; |
| 122 // Retain this object until it is actually resolved or rejected. | |
| 123 // |deref| will be called in |clear|. | |
| 124 ref(); | |
| 125 | 118 |
| 126 ScriptState::Scope scope(m_scriptState.get()); | 119 ScriptState::Scope scope(m_scriptState.get()); |
| 127 m_value.set( | 120 m_value.set( |
| 128 m_scriptState->isolate(), | 121 m_scriptState->isolate(), |
| 129 toV8(value, m_scriptState->context()->Global(), m_scriptState->isola
te())); | 122 toV8(value, m_scriptState->context()->Global(), m_scriptState->isola
te())); |
| 130 if (!executionContext()->activeDOMObjectsAreSuspended()) | 123 |
| 131 resolveOrRejectImmediately(); | 124 if (executionContext()->activeDOMObjectsAreSuspended()) { |
| 125 // Retain this object until it is actually resolved or rejected. |
| 126 keepAliveWhilePending(); |
| 127 return; |
| 128 } |
| 129 resolveOrRejectImmediately(); |
| 132 } | 130 } |
| 133 | 131 |
| 134 void resolveOrRejectImmediately(); | 132 void resolveOrRejectImmediately(); |
| 135 void onTimerFired(Timer<ScriptPromiseResolver>*); | 133 void onTimerFired(Timer<ScriptPromiseResolver>*); |
| 136 void clear(); | 134 void clear(); |
| 137 | 135 |
| 138 ResolutionState m_state; | 136 ResolutionState m_state; |
| 139 const RefPtr<ScriptState> m_scriptState; | 137 const RefPtr<ScriptState> m_scriptState; |
| 140 LifetimeMode m_mode; | |
| 141 Timer<ScriptPromiseResolver> m_timer; | 138 Timer<ScriptPromiseResolver> m_timer; |
| 142 Resolver m_resolver; | 139 Resolver m_resolver; |
| 143 ScopedPersistent<v8::Value> m_value; | 140 ScopedPersistent<v8::Value> m_value; |
| 141 |
| 142 // To support keepAliveWhilePending(), this object needs to keep itself |
| 143 // alive while in that state. |
| 144 SelfKeepAlive<ScriptPromiseResolver> m_keepAlive; |
| 145 |
| 144 #if ENABLE(ASSERT) | 146 #if ENABLE(ASSERT) |
| 145 // True if promise() is called. | 147 // True if promise() is called. |
| 146 bool m_isPromiseCalled; | 148 bool m_isPromiseCalled; |
| 147 #endif | 149 #endif |
| 148 }; | 150 }; |
| 149 | 151 |
| 150 } // namespace blink | 152 } // namespace blink |
| 151 | 153 |
| 152 #endif // ScriptPromiseResolver_h | 154 #endif // ScriptPromiseResolver_h |
| OLD | NEW |