| 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 #include "config.h" |
| 6 #include "bindings/core/dart/DartScriptPromiseResolver.h" |
| 7 |
| 8 #include "bindings/common/ScriptPromiseResolverIncludes.h" |
| 9 |
| 10 namespace blink { |
| 11 |
| 12 DartScriptPromiseResolver::DartScriptPromiseResolver(DartScriptState* scriptStat
e, ScriptPromiseResolver* owner) |
| 13 : AbstractScriptPromiseResolver() |
| 14 , m_state(Pending) |
| 15 , m_scriptState(scriptState) |
| 16 , m_owner(owner) |
| 17 , m_mode(Default) |
| 18 , m_timer(this, &DartScriptPromiseResolver::onTimerFired) |
| 19 , m_completer() |
| 20 #if ENABLE(ASSERT) |
| 21 , m_isPromiseCalled(false) |
| 22 #endif |
| 23 { |
| 24 Dart_Handle asyncLib = Dart_LookupLibrary(Dart_NewStringFromCString("dart:as
ync")); |
| 25 Dart_Handle completerClass = Dart_GetType(asyncLib, Dart_NewStringFromCStrin
g("Completer"), 0, 0); |
| 26 Dart_Handle completer = Dart_New(completerClass, Dart_NewStringFromCString("
"), 0, 0); |
| 27 m_completer = Dart_NewPersistentHandle(completer); |
| 28 |
| 29 if (executionContext()->activeDOMObjectsAreStopped()) |
| 30 m_state = ResolvedOrRejected; |
| 31 } |
| 32 |
| 33 #define DEFINE_RESOLUTION_METHODS(type) \ |
| 34 void DartScriptPromiseResolver::resolve(type value) { resolveInternal(value); }
\ |
| 35 void DartScriptPromiseResolver::reject(type error) { rejectInternal(error); } |
| 36 PROMISE_RESOLUTION_TYPES_LIST(DEFINE_RESOLUTION_METHODS); |
| 37 #undef DEFINE_RESOLUTION_METHODS |
| 38 |
| 39 void DartScriptPromiseResolver::suspend() |
| 40 { |
| 41 m_timer.stop(); |
| 42 } |
| 43 |
| 44 void DartScriptPromiseResolver::resume() |
| 45 { |
| 46 if (m_state == Resolving || m_state == Rejecting) |
| 47 m_timer.startOneShot(0, FROM_HERE); |
| 48 } |
| 49 |
| 50 void DartScriptPromiseResolver::stop() |
| 51 { |
| 52 m_timer.stop(); |
| 53 clear(); |
| 54 } |
| 55 |
| 56 void DartScriptPromiseResolver::keepAliveWhilePending() |
| 57 { |
| 58 if (m_state == ResolvedOrRejected || m_mode == KeepAliveWhilePending) |
| 59 return; |
| 60 |
| 61 // Keep |this| while the promise is Pending. |
| 62 // deref() will be called in clear(). |
| 63 m_mode = KeepAliveWhilePending; |
| 64 m_owner->ref(); |
| 65 } |
| 66 |
| 67 void DartScriptPromiseResolver::onTimerFired(Timer<DartScriptPromiseResolver>*) |
| 68 { |
| 69 ASSERT(m_state == Resolving || m_state == Rejecting); |
| 70 DartIsolateScope scope(m_scriptState->isolate()); |
| 71 DartApiScope apiScope; |
| 72 // FIXMEDART: Remove this. |
| 73 V8ScriptState::Scope v8Scope(m_scriptState->v8ScriptState()); |
| 74 resolveOrRejectImmediately(); |
| 75 } |
| 76 |
| 77 void DartScriptPromiseResolver::resolveOrRejectImmediately() |
| 78 { |
| 79 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); |
| 80 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); |
| 81 { |
| 82 if (m_state == Resolving) { |
| 83 Dart_Handle value = Dart_HandleFromPersistent(m_value); |
| 84 Dart_Invoke(m_completer, Dart_NewStringFromCString("complete"), 1, &
value); |
| 85 } else { |
| 86 ASSERT(m_state == Rejecting); |
| 87 Dart_Handle error = Dart_HandleFromPersistent(m_value); |
| 88 Dart_Invoke(m_completer, Dart_NewStringFromCString("completeError"),
1, &error); |
| 89 } |
| 90 } |
| 91 clear(); |
| 92 } |
| 93 |
| 94 void DartScriptPromiseResolver::clear() |
| 95 { |
| 96 if (m_state == ResolvedOrRejected) |
| 97 return; |
| 98 ResolutionState state = m_state; |
| 99 m_state = ResolvedOrRejected; |
| 100 Dart_DeletePersistentHandle(m_completer); |
| 101 Dart_DeletePersistentHandle(m_value); |
| 102 |
| 103 if (m_mode == KeepAliveWhilePending) { |
| 104 // |ref| was called in |keepAliveWhilePending|. |
| 105 m_owner->deref(); |
| 106 } |
| 107 // |this| may be deleted here, but it is safe to check |state| because |
| 108 // it doesn't depend on |this|. When |this| is deleted, |state| can't be |
| 109 // |Resolving| nor |Rejecting| and hence |this->deref()| can't be executed. |
| 110 if (state == Resolving || state == Rejecting) { |
| 111 // |ref| was called in |resolveOrReject|. |
| 112 m_owner->deref(); |
| 113 } |
| 114 } |
| 115 |
| 116 } // namespace blink |
| OLD | NEW |