| 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 "sky/engine/config.h" | |
| 6 #include "sky/engine/bindings/core/v8/ScriptPromiseResolver.h" | |
| 7 | |
| 8 #include "sky/engine/bindings/core/v8/V8RecursionScope.h" | |
| 9 | |
| 10 namespace blink { | |
| 11 | |
| 12 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState) | |
| 13 : ActiveDOMObject(scriptState->executionContext()) | |
| 14 , m_state(Pending) | |
| 15 , m_scriptState(scriptState) | |
| 16 , m_mode(Default) | |
| 17 , m_timer(this, &ScriptPromiseResolver::onTimerFired) | |
| 18 , m_resolver(scriptState) | |
| 19 #if ENABLE(ASSERT) | |
| 20 , m_isPromiseCalled(false) | |
| 21 #endif | |
| 22 { | |
| 23 if (executionContext()->activeDOMObjectsAreStopped()) | |
| 24 m_state = ResolvedOrRejected; | |
| 25 } | |
| 26 | |
| 27 void ScriptPromiseResolver::suspend() | |
| 28 { | |
| 29 m_timer.stop(); | |
| 30 } | |
| 31 | |
| 32 void ScriptPromiseResolver::resume() | |
| 33 { | |
| 34 if (m_state == Resolving || m_state == Rejecting) | |
| 35 m_timer.startOneShot(0, FROM_HERE); | |
| 36 } | |
| 37 | |
| 38 void ScriptPromiseResolver::stop() | |
| 39 { | |
| 40 m_timer.stop(); | |
| 41 clear(); | |
| 42 } | |
| 43 | |
| 44 void ScriptPromiseResolver::keepAliveWhilePending() | |
| 45 { | |
| 46 if (m_state == ResolvedOrRejected || m_mode == KeepAliveWhilePending) | |
| 47 return; | |
| 48 | |
| 49 // Keep |this| while the promise is Pending. | |
| 50 // deref() will be called in clear(). | |
| 51 m_mode = KeepAliveWhilePending; | |
| 52 ref(); | |
| 53 } | |
| 54 | |
| 55 void ScriptPromiseResolver::onTimerFired(Timer<ScriptPromiseResolver>*) | |
| 56 { | |
| 57 ASSERT(m_state == Resolving || m_state == Rejecting); | |
| 58 ScriptState::Scope scope(m_scriptState.get()); | |
| 59 resolveOrRejectImmediately(); | |
| 60 } | |
| 61 | |
| 62 void ScriptPromiseResolver::resolveOrRejectImmediately() | |
| 63 { | |
| 64 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); | |
| 65 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); | |
| 66 { | |
| 67 if (m_state == Resolving) { | |
| 68 m_resolver.resolve(m_value.newLocal(m_scriptState->isolate())); | |
| 69 } else { | |
| 70 ASSERT(m_state == Rejecting); | |
| 71 m_resolver.reject(m_value.newLocal(m_scriptState->isolate())); | |
| 72 } | |
| 73 } | |
| 74 clear(); | |
| 75 } | |
| 76 | |
| 77 void ScriptPromiseResolver::clear() | |
| 78 { | |
| 79 if (m_state == ResolvedOrRejected) | |
| 80 return; | |
| 81 ResolutionState state = m_state; | |
| 82 m_state = ResolvedOrRejected; | |
| 83 m_resolver.clear(); | |
| 84 m_value.clear(); | |
| 85 if (m_mode == KeepAliveWhilePending) { | |
| 86 // |ref| was called in |keepAliveWhilePending|. | |
| 87 deref(); | |
| 88 } | |
| 89 // |this| may be deleted here, but it is safe to check |state| because | |
| 90 // it doesn't depend on |this|. When |this| is deleted, |state| can't be | |
| 91 // |Resolving| nor |Rejecting| and hence |this->deref()| can't be executed. | |
| 92 if (state == Resolving || state == Rejecting) { | |
| 93 // |ref| was called in |resolveOrReject|. | |
| 94 deref(); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 } // namespace blink | |
| OLD | NEW |