| 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 #include "config.h" | 5 #include "config.h" |
| 6 #include "bindings/v8/ScriptPromiseResolverWithContext.h" | 6 #include "bindings/v8/ScriptPromiseResolverWithContext.h" |
| 7 | 7 |
| 8 #include "bindings/v8/V8RecursionScope.h" | 8 #include "bindings/v8/V8RecursionScope.h" |
| 9 | 9 |
| 10 namespace WebCore { | 10 namespace WebCore { |
| 11 | 11 |
| 12 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(ScriptState*
scriptState, Mode mode) | 12 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(ScriptState*
scriptState) |
| 13 : ActiveDOMObject(scriptState->executionContext()) | 13 : ActiveDOMObject(scriptState->executionContext()) |
| 14 , m_state(Pending) | 14 , m_state(Pending) |
| 15 , m_scriptState(scriptState) | 15 , m_scriptState(scriptState) |
| 16 , m_mode(mode) | |
| 17 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) | 16 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) |
| 18 , m_resolver(ScriptPromiseResolver::create(m_scriptState.get())) | 17 , m_resolver(ScriptPromiseResolver::create(m_scriptState.get())) |
| 19 { | 18 { |
| 20 if (mode == KeepAliveWhilePending) { | |
| 21 // In order to call ref() here, I relax the adoption requirement. | |
| 22 // adoptRef is required neverthless. | |
| 23 relaxAdoptionRequirement(); | |
| 24 // Keep |this| while the promise is Pending. | |
| 25 // deref() will be called in clear(). | |
| 26 ref(); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 ScriptPromiseResolverWithContext::~ScriptPromiseResolverWithContext() | |
| 31 { | |
| 32 if (m_state != ResolvedOrRejected) { | |
| 33 ScriptState::Scope scope(m_scriptState.get()); | |
| 34 reject(v8::Exception::Error(v8::String::NewFromUtf8(m_scriptState->isola
te(), | |
| 35 "ScriptPromiseResolverWithContext is destructed without resolve / re
ject"))); | |
| 36 } | |
| 37 } | 19 } |
| 38 | 20 |
| 39 void ScriptPromiseResolverWithContext::suspend() | 21 void ScriptPromiseResolverWithContext::suspend() |
| 40 { | 22 { |
| 41 m_timer.stop(); | 23 m_timer.stop(); |
| 42 } | 24 } |
| 43 | 25 |
| 44 void ScriptPromiseResolverWithContext::resume() | 26 void ScriptPromiseResolverWithContext::resume() |
| 45 { | 27 { |
| 46 if (m_state == Resolving || m_state == Rejecting) | 28 if (m_state == Resolving || m_state == Rejecting) |
| 47 m_timer.startOneShot(0, FROM_HERE); | 29 m_timer.startOneShot(0, FROM_HERE); |
| 48 } | 30 } |
| 49 | 31 |
| 50 void ScriptPromiseResolverWithContext::stop() | 32 void ScriptPromiseResolverWithContext::stop() |
| 51 { | 33 { |
| 52 m_timer.stop(); | 34 m_timer.stop(); |
| 53 clear(); | 35 clear(); |
| 54 } | 36 } |
| 55 | 37 |
| 56 void ScriptPromiseResolverWithContext::onTimerFired(Timer<ScriptPromiseResolverW
ithContext>*) | 38 void ScriptPromiseResolverWithContext::onTimerFired(Timer<ScriptPromiseResolverW
ithContext>*) |
| 57 { | 39 { |
| 40 RefPtr<ScriptPromiseResolverWithContext> protect(this); |
| 58 ScriptState::Scope scope(m_scriptState.get()); | 41 ScriptState::Scope scope(m_scriptState.get()); |
| 59 resolveOrRejectImmediately(); | 42 resolveOrRejectImmediately(); |
| 60 } | 43 } |
| 61 | 44 |
| 62 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately() | 45 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately() |
| 63 { | 46 { |
| 64 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); | 47 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); |
| 65 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); | 48 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); |
| 66 { | 49 { |
| 67 // FIXME: The V8RecursionScope is only necessary to force microtask deli
very for promises | 50 // FIXME: The V8RecursionScope is only necessary to force microtask deli
very for promises |
| 68 // resolved or rejected in workers. It can be removed once worker thread
s run microtasks | 51 // resolved or rejected in workers. It can be removed once worker thread
s run microtasks |
| 69 // at the end of every task (rather than just the main thread). | 52 // at the end of every task (rather than just the main thread). |
| 70 V8RecursionScope scope(m_scriptState->isolate(), m_scriptState->executio
nContext()); | 53 V8RecursionScope scope(m_scriptState->isolate(), m_scriptState->executio
nContext()); |
| 71 if (m_state == Resolving) { | 54 if (m_state == Resolving) { |
| 72 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); | 55 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); |
| 73 } else { | 56 } else { |
| 74 ASSERT(m_state == Rejecting); | 57 ASSERT(m_state == Rejecting); |
| 75 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); | 58 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); |
| 76 } | 59 } |
| 77 } | 60 } |
| 78 clear(); | 61 clear(); |
| 79 } | 62 } |
| 80 | 63 |
| 81 void ScriptPromiseResolverWithContext::clear() | 64 void ScriptPromiseResolverWithContext::clear() |
| 82 { | 65 { |
| 83 if (m_state == ResolvedOrRejected) | |
| 84 return; | |
| 85 ResolutionState state = m_state; | 66 ResolutionState state = m_state; |
| 86 m_state = ResolvedOrRejected; | 67 m_state = ResolvedOrRejected; |
| 87 m_resolver.clear(); | 68 m_resolver.clear(); |
| 88 m_value.clear(); | 69 m_value.clear(); |
| 89 if (m_mode == KeepAliveWhilePending) { | |
| 90 // |ref| was called in the constructor. | |
| 91 deref(); | |
| 92 } | |
| 93 // |this| may be deleted here, but it is safe to check |state| because | |
| 94 // it doesn't depend on |this|. When |this| is deleted, |state| can't be | |
| 95 // |Resolving| nor |Rejecting| and hence |this->deref()| can't be executed. | |
| 96 if (state == Resolving || state == Rejecting) { | 70 if (state == Resolving || state == Rejecting) { |
| 97 // |ref| was called in |resolveOrReject|. | 71 // |ref| was called in |resolveOrReject|. |
| 98 deref(); | 72 deref(); |
| 99 } | 73 } |
| 74 // |this| may be deleted here. |
| 100 } | 75 } |
| 101 | 76 |
| 102 } // namespace WebCore | 77 } // namespace WebCore |
| OLD | NEW |