| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 explicit ScriptPromiseResolver(ScriptState*); | 101 explicit ScriptPromiseResolver(ScriptState*); |
| 102 | 102 |
| 103 private: | 103 private: |
| 104 typedef ScriptPromise::InternalResolver Resolver; | 104 typedef ScriptPromise::InternalResolver Resolver; |
| 105 enum ResolutionState { | 105 enum ResolutionState { |
| 106 Pending, | 106 Pending, |
| 107 Resolving, | 107 Resolving, |
| 108 Rejecting, | 108 Rejecting, |
| 109 ResolvedOrRejected, | 109 ResolvedOrRejected, |
| 110 }; | 110 }; |
| 111 enum LifetimeMode { | |
| 112 Default, | |
| 113 KeepAliveWhilePending, | |
| 114 }; | |
| 115 | 111 |
| 116 template<typename T> | 112 template<typename T> |
| 117 void resolveOrReject(T value, ResolutionState newState) | 113 void resolveOrReject(T value, ResolutionState newState) |
| 118 { | 114 { |
| 119 if (m_state != Pending || !scriptState()->contextIsValid() || !execution
Context() || executionContext()->activeDOMObjectsAreStopped()) | 115 if (m_state != Pending || !scriptState()->contextIsValid() || !execution
Context() || executionContext()->activeDOMObjectsAreStopped()) |
| 120 return; | 116 return; |
| 121 ASSERT(newState == Resolving || newState == Rejecting); | 117 ASSERT(newState == Resolving || newState == Rejecting); |
| 122 m_state = newState; | 118 m_state = newState; |
| 123 // Retain this object until it is actually resolved or rejected. | |
| 124 // |deref| will be called in |clear|. | |
| 125 ref(); | |
| 126 | 119 |
| 127 ScriptState::Scope scope(m_scriptState.get()); | 120 ScriptState::Scope scope(m_scriptState.get()); |
| 128 m_value.set( | 121 m_value.set( |
| 129 m_scriptState->isolate(), | 122 m_scriptState->isolate(), |
| 130 toV8(value, m_scriptState->context()->Global(), m_scriptState->isola
te())); | 123 toV8(value, m_scriptState->context()->Global(), m_scriptState->isola
te())); |
| 131 if (!executionContext()->activeDOMObjectsAreSuspended()) | 124 |
| 132 resolveOrRejectImmediately(); | 125 if (executionContext()->activeDOMObjectsAreSuspended()) { |
| 126 // Retain this object until it is actually resolved or rejected. |
| 127 keepAliveWhilePending(); |
| 128 return; |
| 129 } |
| 130 resolveOrRejectImmediately(); |
| 133 } | 131 } |
| 134 | 132 |
| 135 void resolveOrRejectImmediately(); | 133 void resolveOrRejectImmediately(); |
| 136 void onTimerFired(Timer<ScriptPromiseResolver>*); | 134 void onTimerFired(Timer<ScriptPromiseResolver>*); |
| 137 void clear(); | 135 void clear(); |
| 138 | 136 |
| 139 ResolutionState m_state; | 137 ResolutionState m_state; |
| 140 const RefPtr<ScriptState> m_scriptState; | 138 const RefPtr<ScriptState> m_scriptState; |
| 141 LifetimeMode m_mode; | |
| 142 Timer<ScriptPromiseResolver> m_timer; | 139 Timer<ScriptPromiseResolver> m_timer; |
| 143 Resolver m_resolver; | 140 Resolver m_resolver; |
| 144 ScopedPersistent<v8::Value> m_value; | 141 ScopedPersistent<v8::Value> m_value; |
| 142 |
| 143 // To support keepAliveWhilePending(), this object needs to keep itself |
| 144 // alive while in that state. |
| 145 SelfKeepAlive<ScriptPromiseResolver> m_keepAlive; |
| 146 |
| 145 #if ENABLE(ASSERT) | 147 #if ENABLE(ASSERT) |
| 146 // True if promise() is called. | 148 // True if promise() is called. |
| 147 bool m_isPromiseCalled; | 149 bool m_isPromiseCalled; |
| 148 #endif | 150 #endif |
| 149 }; | 151 }; |
| 150 | 152 |
| 151 } // namespace blink | 153 } // namespace blink |
| 152 | 154 |
| 153 #endif // ScriptPromiseResolver_h | 155 #endif // ScriptPromiseResolver_h |
| OLD | NEW |