Chromium Code Reviews| 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) | 12 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(ScriptState* scriptState, Mode mode) |
| 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) | |
| 16 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) | 17 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) |
| 17 , m_resolver(ScriptPromiseResolver::create(m_scriptState.get())) | 18 , m_resolver(ScriptPromiseResolver::create(m_scriptState.get())) |
| 18 { | 19 { |
| 20 if (mode == KeepAliveWhilePending) { | |
| 21 // Keep |this| while the promise is Pending. | |
| 22 // deref() will be called in clear(). | |
| 23 ref(); | |
|
eroman
2014/06/16 20:05:50
When I run this patch in debug mode I am hitting t
yhirano
2014/06/17 01:45:44
Thanks for pointing it out.
I would like to call r
| |
| 24 } | |
| 25 } | |
| 26 | |
| 27 ScriptPromiseResolverWithContext::~ScriptPromiseResolverWithContext() | |
| 28 { | |
| 29 if (m_state != ResolvedOrRejected) { | |
| 30 ScriptState::Scope scope(m_scriptState.get()); | |
| 31 reject(v8::Exception::Error(v8::String::NewFromUtf8(m_scriptState->isola te(), | |
| 32 "ScriptPromiseResolverWithContext is destructed without resolve / re ject"))); | |
| 33 } | |
| 19 } | 34 } |
| 20 | 35 |
| 21 void ScriptPromiseResolverWithContext::suspend() | 36 void ScriptPromiseResolverWithContext::suspend() |
| 22 { | 37 { |
| 23 m_timer.stop(); | 38 m_timer.stop(); |
| 24 } | 39 } |
| 25 | 40 |
| 26 void ScriptPromiseResolverWithContext::resume() | 41 void ScriptPromiseResolverWithContext::resume() |
| 27 { | 42 { |
| 28 if (m_state == Resolving || m_state == Rejecting) | 43 if (m_state == Resolving || m_state == Rejecting) |
| 29 m_timer.startOneShot(0, FROM_HERE); | 44 m_timer.startOneShot(0, FROM_HERE); |
| 30 } | 45 } |
| 31 | 46 |
| 32 void ScriptPromiseResolverWithContext::stop() | 47 void ScriptPromiseResolverWithContext::stop() |
| 33 { | 48 { |
| 49 ScriptState::Scope scope(m_scriptState.get()); | |
| 34 m_timer.stop(); | 50 m_timer.stop(); |
| 35 clear(); | 51 clear(); |
| 36 } | 52 } |
| 37 | 53 |
| 38 void ScriptPromiseResolverWithContext::onTimerFired(Timer<ScriptPromiseResolverW ithContext>*) | 54 void ScriptPromiseResolverWithContext::onTimerFired(Timer<ScriptPromiseResolverW ithContext>*) |
| 39 { | 55 { |
| 40 RefPtr<ScriptPromiseResolverWithContext> protect(this); | |
| 41 ScriptState::Scope scope(m_scriptState.get()); | 56 ScriptState::Scope scope(m_scriptState.get()); |
| 42 resolveOrRejectImmediately(); | 57 resolveOrRejectImmediately(); |
| 43 } | 58 } |
| 44 | 59 |
| 45 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately() | 60 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately() |
| 46 { | 61 { |
| 47 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); | 62 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); |
| 48 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); | 63 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); |
| 49 { | 64 { |
| 50 // FIXME: The V8RecursionScope is only necessary to force microtask deli very for promises | 65 // FIXME: The V8RecursionScope is only necessary to force microtask deli very for promises |
| 51 // resolved or rejected in workers. It can be removed once worker thread s run microtasks | 66 // resolved or rejected in workers. It can be removed once worker thread s run microtasks |
| 52 // at the end of every task (rather than just the main thread). | 67 // at the end of every task (rather than just the main thread). |
| 53 V8RecursionScope scope(m_scriptState->isolate(), m_scriptState->executio nContext()); | 68 V8RecursionScope scope(m_scriptState->isolate(), m_scriptState->executio nContext()); |
| 54 if (m_state == Resolving) { | 69 if (m_state == Resolving) { |
| 55 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); | 70 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); |
| 56 } else { | 71 } else { |
| 57 ASSERT(m_state == Rejecting); | 72 ASSERT(m_state == Rejecting); |
| 58 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); | 73 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); |
| 59 } | 74 } |
| 60 } | 75 } |
| 61 clear(); | 76 clear(); |
| 62 } | 77 } |
| 63 | 78 |
| 64 void ScriptPromiseResolverWithContext::clear() | 79 void ScriptPromiseResolverWithContext::clear() |
| 65 { | 80 { |
| 81 if (m_state == ResolvedOrRejected) | |
| 82 return; | |
| 66 ResolutionState state = m_state; | 83 ResolutionState state = m_state; |
| 67 m_state = ResolvedOrRejected; | 84 m_state = ResolvedOrRejected; |
| 68 m_resolver.clear(); | 85 m_resolver.clear(); |
| 69 m_value.clear(); | 86 m_value.clear(); |
| 87 if (m_mode == KeepAliveWhilePending) { | |
| 88 // |ref| was called in the constructor. | |
| 89 deref(); | |
|
eroman
2014/06/16 19:36:35
Is it possible for the state to be Pending here?
yhirano
2014/06/17 01:45:44
I added a comment. Is it clear now?
| |
| 90 } | |
| 70 if (state == Resolving || state == Rejecting) { | 91 if (state == Resolving || state == Rejecting) { |
| 71 // |ref| was called in |resolveOrReject|. | 92 // |ref| was called in |resolveOrReject|. |
| 72 deref(); | 93 deref(); |
| 73 } | 94 } |
| 74 // |this| may be deleted here. | |
| 75 } | 95 } |
| 76 | 96 |
| 77 } // namespace WebCore | 97 } // namespace WebCore |
| OLD | NEW |