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/core/v8/ScriptPromiseResolver.h" | 6 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/V8RecursionScope.h" | 8 #include "bindings/core/v8/V8RecursionScope.h" |
| 9 #include "platform/LifecycleObserver.h" | 9 #include "platform/LifecycleObserver.h" |
| 10 | 10 |
| 11 namespace blink { | 11 namespace blink { |
| 12 | 12 |
| 13 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState) | 13 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState) |
| 14 : ActiveDOMObject(scriptState->executionContext()) | 14 : ActiveDOMObject(scriptState->executionContext()) |
| 15 , m_state(Pending) | 15 , m_state(Pending) |
| 16 , m_scriptState(scriptState) | 16 , m_scriptState(scriptState) |
| 17 , m_mode(Default) | |
| 18 , m_timer(this, &ScriptPromiseResolver::onTimerFired) | 17 , m_timer(this, &ScriptPromiseResolver::onTimerFired) |
| 19 , m_resolver(scriptState) | 18 , m_resolver(scriptState) |
| 20 #if ENABLE(ASSERT) | 19 #if ENABLE(ASSERT) |
| 21 , m_isPromiseCalled(false) | 20 , m_isPromiseCalled(false) |
| 22 #endif | 21 #endif |
| 23 { | 22 { |
| 24 if (executionContext()->activeDOMObjectsAreStopped()) { | 23 if (executionContext()->activeDOMObjectsAreStopped()) { |
| 25 m_state = ResolvedOrRejected; | 24 m_state = ResolvedOrRejected; |
| 26 m_resolver.clear(); | 25 m_resolver.clear(); |
| 27 } | 26 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 39 } | 38 } |
| 40 | 39 |
| 41 void ScriptPromiseResolver::stop() | 40 void ScriptPromiseResolver::stop() |
| 42 { | 41 { |
| 43 m_timer.stop(); | 42 m_timer.stop(); |
| 44 clear(); | 43 clear(); |
| 45 } | 44 } |
| 46 | 45 |
| 47 void ScriptPromiseResolver::keepAliveWhilePending() | 46 void ScriptPromiseResolver::keepAliveWhilePending() |
| 48 { | 47 { |
| 49 if (m_state == ResolvedOrRejected || m_mode == KeepAliveWhilePending) | 48 if (m_state == ResolvedOrRejected || m_keepAlive) |
|
haraken
2015/07/18 08:01:23
Instead of adding '|| m_keepAlive', can we add ASS
sof
2015/07/18 20:46:08
I don't think that's accurate - how do you reason
haraken
2015/07/19 07:25:50
Makes sense! Would be worth having a comment -- ke
sof
2015/07/20 11:01:43
Added clarifying comment.
| |
| 50 return; | 49 return; |
| 51 | 50 |
| 52 // Keep |this| while the promise is Pending. | 51 // Keep |this| around while the promise is Pending; |
| 53 // deref() will be called in clear(). | 52 // see clear() for the dual operation. |
| 54 m_mode = KeepAliveWhilePending; | 53 m_keepAlive = this; |
| 55 ref(); | |
| 56 } | 54 } |
| 57 | 55 |
| 58 void ScriptPromiseResolver::onTimerFired(Timer<ScriptPromiseResolver>*) | 56 void ScriptPromiseResolver::onTimerFired(Timer<ScriptPromiseResolver>*) |
| 59 { | 57 { |
| 60 ASSERT(m_state == Resolving || m_state == Rejecting); | 58 ASSERT(m_state == Resolving || m_state == Rejecting); |
| 61 ScriptState::Scope scope(m_scriptState.get()); | 59 ScriptState::Scope scope(m_scriptState.get()); |
| 62 resolveOrRejectImmediately(); | 60 resolveOrRejectImmediately(); |
| 63 } | 61 } |
| 64 | 62 |
| 65 void ScriptPromiseResolver::resolveOrRejectImmediately() | 63 void ScriptPromiseResolver::resolveOrRejectImmediately() |
| 66 { | 64 { |
| 67 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); | 65 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); |
| 68 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); | 66 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); |
| 69 { | 67 { |
| 70 if (m_state == Resolving) { | 68 if (m_state == Resolving) { |
| 71 m_resolver.resolve(m_value.newLocal(m_scriptState->isolate())); | 69 m_resolver.resolve(m_value.newLocal(m_scriptState->isolate())); |
| 72 } else { | 70 } else { |
| 73 ASSERT(m_state == Rejecting); | 71 ASSERT(m_state == Rejecting); |
| 74 m_resolver.reject(m_value.newLocal(m_scriptState->isolate())); | 72 m_resolver.reject(m_value.newLocal(m_scriptState->isolate())); |
| 75 } | 73 } |
| 76 } | 74 } |
| 77 clear(); | 75 clear(); |
| 78 } | 76 } |
| 79 | 77 |
| 80 void ScriptPromiseResolver::clear() | 78 void ScriptPromiseResolver::clear() |
| 81 { | 79 { |
| 82 if (m_state == ResolvedOrRejected) | 80 if (m_state == ResolvedOrRejected) |
| 83 return; | 81 return; |
| 84 ResolutionState state = m_state; | |
| 85 m_state = ResolvedOrRejected; | 82 m_state = ResolvedOrRejected; |
| 86 m_resolver.clear(); | 83 m_resolver.clear(); |
| 87 m_value.clear(); | 84 m_value.clear(); |
| 88 if (m_mode == KeepAliveWhilePending) { | 85 m_keepAlive.clear(); |
| 89 // |ref| was called in |keepAliveWhilePending|. | |
| 90 deref(); | |
| 91 } | |
| 92 // |this| may be deleted here, but it is safe to check |state| because | |
| 93 // it doesn't depend on |this|. When |this| is deleted, |state| can't be | |
| 94 // |Resolving| nor |Rejecting| and hence |this->deref()| can't be executed. | |
| 95 if (state == Resolving || state == Rejecting) { | |
| 96 // |ref| was called in |resolveOrReject|. | |
| 97 deref(); | |
| 98 } | |
| 99 } | 86 } |
| 100 | 87 |
| 101 DEFINE_TRACE(ScriptPromiseResolver) | 88 DEFINE_TRACE(ScriptPromiseResolver) |
| 102 { | 89 { |
| 103 ActiveDOMObject::trace(visitor); | 90 ActiveDOMObject::trace(visitor); |
| 104 } | 91 } |
| 105 | 92 |
| 106 } // namespace blink | 93 } // namespace blink |
| OLD | NEW |