| 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 SKY_ENGINE_BINDINGS_CORE_V8_SCRIPTPROMISERESOLVER_H_ | 5 #ifndef DartScriptPromiseResolver_h |
| 6 #define SKY_ENGINE_BINDINGS_CORE_V8_SCRIPTPROMISERESOLVER_H_ | 6 #define DartScriptPromiseResolver_h |
| 7 | 7 |
| 8 #include "sky/engine/bindings/core/v8/ScopedPersistent.h" | 8 #include "bindings/common/ScriptPromiseResolver.h" |
| 9 #include "sky/engine/bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/common/ScriptState.h" |
| 10 #include "sky/engine/bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/dart/DartDOMException.h" |
| 11 #include "sky/engine/bindings/core/v8/V8Binding.h" | 11 #include "bindings/core/dart/DartDOMWrapper.h" |
| 12 #include "sky/engine/core/dom/ActiveDOMObject.h" | 12 #include "bindings/core/dart/DartScriptPromise.h" |
| 13 #include "sky/engine/core/dom/ExecutionContext.h" | 13 #include "bindings/core/dart/DartUtilities.h" |
| 14 #include "sky/engine/platform/Timer.h" | 14 #include "bindings/core/v8/V8ScriptState.h" |
| 15 #include "sky/engine/wtf/RefCounted.h" | 15 #include "core/dom/ActiveDOMObject.h" |
| 16 #include "v8/include/v8.h" | 16 #include "core/dom/ExecutionContext.h" |
| 17 #include "platform/Timer.h" |
| 18 #include "wtf/RefCounted.h" |
| 19 #include <dart_api.h> |
| 17 | 20 |
| 18 namespace blink { | 21 namespace blink { |
| 19 | 22 |
| 20 // This class wraps v8::Promise::Resolver and provides the following | 23 class ScriptPromiseResolver; |
| 21 // functionalities. | 24 |
| 22 // - A ScriptPromiseResolver retains a ScriptState. A caller | 25 class DartScriptPromiseResolver : public AbstractScriptPromiseResolver { |
| 23 // can call resolve or reject from outside of a V8 context. | 26 WTF_MAKE_NONCOPYABLE(DartScriptPromiseResolver); |
| 24 // - This class is an ActiveDOMObject and keeps track of the associated | |
| 25 // ExecutionContext state. When the ExecutionContext is suspended, | |
| 26 // resolve or reject will be delayed. When it is stopped, resolve or reject | |
| 27 // will be ignored. | |
| 28 class ScriptPromiseResolver : public ActiveDOMObject, public RefCounted<ScriptPr
omiseResolver> { | |
| 29 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | |
| 30 | 27 |
| 31 public: | 28 public: |
| 32 static PassRefPtr<ScriptPromiseResolver> create(ScriptState* scriptState) | 29 static PassOwnPtr<DartScriptPromiseResolver> create(DartScriptState* scriptS
tate, ScriptPromiseResolver* owner) |
| 33 { | 30 { |
| 34 RefPtr<ScriptPromiseResolver> resolver = adoptRef(new ScriptPromiseResol
ver(scriptState)); | 31 return adoptPtr(new DartScriptPromiseResolver(scriptState, owner)); |
| 35 resolver->suspendIfNeeded(); | |
| 36 return resolver.release(); | |
| 37 } | 32 } |
| 38 | 33 |
| 39 virtual ~ScriptPromiseResolver() | 34 virtual ~DartScriptPromiseResolver() |
| 40 { | 35 { |
| 41 // This assertion fails if: | 36 // This assertion fails if: |
| 42 // - promise() is called at least once and | 37 // - promise() is called at least once and |
| 43 // - this resolver is destructed before it is resolved, rejected or | 38 // - this resolver is destructed before it is resolved, rejected or |
| 44 // the associated ExecutionContext is stopped. | 39 // the associated ExecutionContext is stopped. |
| 45 ASSERT(m_state == ResolvedOrRejected || !m_isPromiseCalled); | 40 ASSERT(m_state == ResolvedOrRejected || !m_isPromiseCalled); |
| 46 } | 41 } |
| 47 | 42 |
| 43 ExecutionContext* executionContext() { return m_scriptState->executionContex
t(); } |
| 44 |
| 45 #define DECLARE_RESOLUTION_METHODS(type) \ |
| 46 void resolve(type); \ |
| 47 void reject(type); |
| 48 PROMISE_RESOLUTION_TYPES_LIST(DECLARE_RESOLUTION_METHODS); |
| 49 #undef DECLARE_RESOLUTION_METHODS |
| 50 |
| 48 // Anything that can be passed to toV8Value can be passed to this function. | 51 // Anything that can be passed to toV8Value can be passed to this function. |
| 49 template <typename T> | 52 template <typename T> |
| 50 void resolve(T value) | 53 void resolveInternal(T value) |
| 51 { | 54 { |
| 52 resolveOrReject(value, Resolving); | 55 resolveOrReject(value, Resolving); |
| 53 } | 56 } |
| 54 | 57 |
| 55 // Anything that can be passed to toV8Value can be passed to this function. | 58 // Anything that can be passed to toV8Value can be passed to this function. |
| 56 template <typename T> | 59 template <typename T> |
| 57 void reject(T value) | 60 void rejectInternal(T value) |
| 58 { | 61 { |
| 59 resolveOrReject(value, Rejecting); | 62 resolveOrReject(value, Rejecting); |
| 60 } | 63 } |
| 61 | 64 |
| 62 void resolve() { resolve(V8UndefinedType()); } | 65 void resolve() { resolve(V8UndefinedType()); } |
| 63 void reject() { reject(V8UndefinedType()); } | 66 void reject() { reject(V8UndefinedType()); } |
| 64 | 67 |
| 65 ScriptState* scriptState() { return m_scriptState.get(); } | 68 ScriptState* scriptState() { return m_scriptState.get(); } |
| 66 | 69 |
| 67 // Note that an empty ScriptPromise will be returned after resolve or | 70 // Note that an empty ScriptPromise will be returned after resolve or |
| 68 // reject is called. | 71 // reject is called. |
| 69 ScriptPromise promise() | 72 PassRefPtr<AbstractScriptPromise> promise() |
| 70 { | 73 { |
| 71 #if ENABLE(ASSERT) | 74 #if ENABLE(ASSERT) |
| 72 m_isPromiseCalled = true; | 75 m_isPromiseCalled = true; |
| 73 #endif | 76 #endif |
| 74 return m_resolver.promise(); | 77 ASSERT(m_completer); |
| 78 Dart_Handle future = Dart_GetField(m_completer, Dart_NewStringFromCStrin
g("future")); |
| 79 return DartScriptPromise::create(m_scriptState.get(), future); |
| 75 } | 80 } |
| 76 | 81 |
| 77 ScriptState* scriptState() const { return m_scriptState.get(); } | 82 ScriptState* scriptState() const { return m_scriptState.get(); } |
| 78 | 83 |
| 79 // ActiveDOMObject implementation. | 84 // ActiveDOMObject implementation. |
| 80 virtual void suspend() override; | 85 virtual void suspend(); |
| 81 virtual void resume() override; | 86 virtual void resume(); |
| 82 virtual void stop() override; | 87 virtual void stop(); |
| 83 | 88 |
| 84 // Once this function is called this resolver stays alive while the | 89 // Once this function is called this resolver stays alive while the |
| 85 // promise is pending and the associated ExecutionContext isn't stopped. | 90 // promise is pending and the associated ExecutionContext isn't stopped. |
| 86 void keepAliveWhilePending(); | 91 void keepAliveWhilePending(); |
| 87 | 92 |
| 88 protected: | 93 protected: |
| 89 // You need to call suspendIfNeeded after the construction because | 94 // You need to call suspendIfNeeded after the construction because |
| 90 // this is an ActiveDOMObject. | 95 // this is an ActiveDOMObject. |
| 91 explicit ScriptPromiseResolver(ScriptState*); | 96 explicit DartScriptPromiseResolver(DartScriptState*, ScriptPromiseResolver*)
; |
| 92 | 97 |
| 93 private: | 98 private: |
| 94 typedef ScriptPromise::InternalResolver Resolver; | |
| 95 enum ResolutionState { | 99 enum ResolutionState { |
| 96 Pending, | 100 Pending, |
| 97 Resolving, | 101 Resolving, |
| 98 Rejecting, | 102 Rejecting, |
| 99 ResolvedOrRejected, | 103 ResolvedOrRejected, |
| 100 }; | 104 }; |
| 101 enum LifetimeMode { | 105 enum LifetimeMode { |
| 102 Default, | 106 Default, |
| 103 KeepAliveWhilePending, | 107 KeepAliveWhilePending, |
| 104 }; | 108 }; |
| 105 | 109 |
| 106 template<typename T> | |
| 107 v8::Handle<v8::Value> toV8Value(const T& value) | |
| 108 { | |
| 109 return V8ValueTraits<T>::toV8Value(value, m_scriptState->context()->Glob
al(), m_scriptState->isolate()); | |
| 110 } | |
| 111 | |
| 112 template <typename T> | 110 template <typename T> |
| 113 void resolveOrReject(T value, ResolutionState newState) | 111 void resolveOrReject(T value, ResolutionState newState) |
| 114 { | 112 { |
| 115 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | 113 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) |
| 116 return; | 114 return; |
| 117 ASSERT(newState == Resolving || newState == Rejecting); | 115 ASSERT(newState == Resolving || newState == Rejecting); |
| 118 m_state = newState; | 116 m_state = newState; |
| 119 // Retain this object until it is actually resolved or rejected. | 117 // Retain this object until it is actually resolved or rejected. |
| 120 // |deref| will be called in |clear|. | 118 // |deref| will be called in |clear|. |
| 121 ref(); | 119 m_owner->ref(); |
| 122 | 120 |
| 123 ScriptState::Scope scope(m_scriptState.get()); | 121 DartIsolateScope scope(m_scriptState->isolate()); |
| 124 m_value.set(m_scriptState->isolate(), toV8Value(value)); | 122 DartApiScope apiScope; |
| 123 // FIXMEDART: Remove this. |
| 124 V8ScriptState::Scope v8Scope(m_scriptState->v8ScriptState()); |
| 125 |
| 126 // FIXMEDART: Should be able to get DartDOMData from a DartScriptState i
nstead of TLS. |
| 127 Dart_Handle v = DartValueTraits<T>::toDartValue(value, DartDOMData::curr
ent()); |
| 128 m_value = Dart_NewPersistentHandle(v); |
| 125 if (!executionContext()->activeDOMObjectsAreSuspended()) | 129 if (!executionContext()->activeDOMObjectsAreSuspended()) |
| 126 resolveOrRejectImmediately(); | 130 resolveOrRejectImmediately(); |
| 127 } | 131 } |
| 128 | 132 |
| 129 void resolveOrRejectImmediately(); | 133 void resolveOrRejectImmediately(); |
| 130 void onTimerFired(Timer<ScriptPromiseResolver>*); | 134 void onTimerFired(Timer<DartScriptPromiseResolver>*); |
| 131 void clear(); | 135 void clear(); |
| 132 | 136 |
| 133 ResolutionState m_state; | 137 ResolutionState m_state; |
| 134 const RefPtr<ScriptState> m_scriptState; | 138 const RefPtr<DartScriptState> m_scriptState; |
| 139 ScriptPromiseResolver* m_owner; |
| 135 LifetimeMode m_mode; | 140 LifetimeMode m_mode; |
| 136 Timer<ScriptPromiseResolver> m_timer; | 141 Timer<DartScriptPromiseResolver> m_timer; |
| 137 Resolver m_resolver; | 142 |
| 138 ScopedPersistent<v8::Value> m_value; | 143 Dart_PersistentHandle m_completer; |
| 144 Dart_PersistentHandle m_value; |
| 145 |
| 139 #if ENABLE(ASSERT) | 146 #if ENABLE(ASSERT) |
| 140 // True if promise() is called. | 147 // True if promise() is called. |
| 141 bool m_isPromiseCalled; | 148 bool m_isPromiseCalled; |
| 142 #endif | 149 #endif |
| 143 }; | 150 }; |
| 144 | 151 |
| 145 } // namespace blink | 152 } // namespace blink |
| 146 | 153 |
| 147 #endif // SKY_ENGINE_BINDINGS_CORE_V8_SCRIPTPROMISERESOLVER_H_ | 154 #endif // #ifndef DartScriptPromiseResolver_h |
| OLD | NEW |