| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef DartScriptPromiseResolver_h | |
| 6 #define DartScriptPromiseResolver_h | |
| 7 | |
| 8 #include "bindings/common/ScriptPromiseResolver.h" | |
| 9 #include "bindings/common/ScriptState.h" | |
| 10 #include "bindings/core/dart/DartDOMException.h" | |
| 11 #include "bindings/core/dart/DartDOMWrapper.h" | |
| 12 #include "bindings/core/dart/DartScriptPromise.h" | |
| 13 #include "bindings/core/dart/DartUtilities.h" | |
| 14 #include "bindings/core/v8/V8ScriptState.h" | |
| 15 #include "core/dom/ActiveDOMObject.h" | |
| 16 #include "core/dom/ExecutionContext.h" | |
| 17 #include "platform/Timer.h" | |
| 18 #include "wtf/RefCounted.h" | |
| 19 #include <dart_api.h> | |
| 20 | |
| 21 namespace blink { | |
| 22 | |
| 23 class ScriptPromiseResolver; | |
| 24 | |
| 25 class DartScriptPromiseResolver : public AbstractScriptPromiseResolver { | |
| 26 WTF_MAKE_NONCOPYABLE(DartScriptPromiseResolver); | |
| 27 | |
| 28 public: | |
| 29 static PassOwnPtr<DartScriptPromiseResolver> create(DartScriptState* scriptS
tate, ScriptPromiseResolver* owner) | |
| 30 { | |
| 31 return adoptPtr(new DartScriptPromiseResolver(scriptState, owner)); | |
| 32 } | |
| 33 | |
| 34 virtual ~DartScriptPromiseResolver() | |
| 35 { | |
| 36 // This assertion fails if: | |
| 37 // - promise() is called at least once and | |
| 38 // - this resolver is destructed before it is resolved, rejected or | |
| 39 // the associated ExecutionContext is stopped. | |
| 40 ASSERT(m_state == ResolvedOrRejected || !m_isPromiseCalled); | |
| 41 } | |
| 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 | |
| 51 // Anything that can be passed to toV8Value can be passed to this function. | |
| 52 template <typename T> | |
| 53 void resolveInternal(T value) | |
| 54 { | |
| 55 resolveOrReject(value, Resolving); | |
| 56 } | |
| 57 | |
| 58 // Anything that can be passed to toV8Value can be passed to this function. | |
| 59 template <typename T> | |
| 60 void rejectInternal(T value) | |
| 61 { | |
| 62 resolveOrReject(value, Rejecting); | |
| 63 } | |
| 64 | |
| 65 void resolve() { resolve(V8UndefinedType()); } | |
| 66 void reject() { reject(V8UndefinedType()); } | |
| 67 | |
| 68 ScriptState* scriptState() { return m_scriptState.get(); } | |
| 69 | |
| 70 // Note that an empty ScriptPromise will be returned after resolve or | |
| 71 // reject is called. | |
| 72 PassRefPtr<AbstractScriptPromise> promise() | |
| 73 { | |
| 74 #if ENABLE(ASSERT) | |
| 75 m_isPromiseCalled = true; | |
| 76 #endif | |
| 77 ASSERT(m_completer); | |
| 78 Dart_Handle future = Dart_GetField(m_completer, Dart_NewStringFromCStrin
g("future")); | |
| 79 return DartScriptPromise::create(m_scriptState.get(), future); | |
| 80 } | |
| 81 | |
| 82 ScriptState* scriptState() const { return m_scriptState.get(); } | |
| 83 | |
| 84 // ActiveDOMObject implementation. | |
| 85 virtual void suspend(); | |
| 86 virtual void resume(); | |
| 87 virtual void stop(); | |
| 88 | |
| 89 // Once this function is called this resolver stays alive while the | |
| 90 // promise is pending and the associated ExecutionContext isn't stopped. | |
| 91 void keepAliveWhilePending(); | |
| 92 | |
| 93 protected: | |
| 94 // You need to call suspendIfNeeded after the construction because | |
| 95 // this is an ActiveDOMObject. | |
| 96 explicit DartScriptPromiseResolver(DartScriptState*, ScriptPromiseResolver*)
; | |
| 97 | |
| 98 private: | |
| 99 enum ResolutionState { | |
| 100 Pending, | |
| 101 Resolving, | |
| 102 Rejecting, | |
| 103 ResolvedOrRejected, | |
| 104 }; | |
| 105 enum LifetimeMode { | |
| 106 Default, | |
| 107 KeepAliveWhilePending, | |
| 108 }; | |
| 109 | |
| 110 template <typename T> | |
| 111 void resolveOrReject(T value, ResolutionState newState) | |
| 112 { | |
| 113 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | |
| 114 return; | |
| 115 ASSERT(newState == Resolving || newState == Rejecting); | |
| 116 m_state = newState; | |
| 117 // Retain this object until it is actually resolved or rejected. | |
| 118 // |deref| will be called in |clear|. | |
| 119 m_owner->ref(); | |
| 120 | |
| 121 DartIsolateScope scope(m_scriptState->isolate()); | |
| 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); | |
| 129 if (!executionContext()->activeDOMObjectsAreSuspended()) | |
| 130 resolveOrRejectImmediately(); | |
| 131 } | |
| 132 | |
| 133 void resolveOrRejectImmediately(); | |
| 134 void onTimerFired(Timer<DartScriptPromiseResolver>*); | |
| 135 void clear(); | |
| 136 | |
| 137 ResolutionState m_state; | |
| 138 const RefPtr<DartScriptState> m_scriptState; | |
| 139 ScriptPromiseResolver* m_owner; | |
| 140 LifetimeMode m_mode; | |
| 141 Timer<DartScriptPromiseResolver> m_timer; | |
| 142 | |
| 143 Dart_PersistentHandle m_completer; | |
| 144 Dart_PersistentHandle m_value; | |
| 145 | |
| 146 #if ENABLE(ASSERT) | |
| 147 // True if promise() is called. | |
| 148 bool m_isPromiseCalled; | |
| 149 #endif | |
| 150 }; | |
| 151 | |
| 152 } // namespace blink | |
| 153 | |
| 154 #endif // #ifndef DartScriptPromiseResolver_h | |
| OLD | NEW |