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 "bindings/core/v8/ScriptPromiseResolver.h" | 5 #include "bindings/core/v8/ScriptPromiseResolver.h" |
6 | 6 |
7 namespace blink { | 7 namespace blink { |
8 | 8 |
9 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState) | 9 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState) |
10 : ActiveDOMObject(scriptState->executionContext()) | 10 : ActiveDOMObject(scriptState->executionContext()) |
11 , m_state(Pending) | 11 , m_state(Pending) |
12 , m_scriptState(scriptState) | 12 , m_scriptState(scriptState) |
13 , m_timer(this, &ScriptPromiseResolver::onTimerFired) | 13 , m_timer(this, &ScriptPromiseResolver::onTimerFired) |
14 , m_resolver(scriptState) | 14 , m_resolver(scriptState) |
15 #if ENABLE(ASSERT) | 15 #if ENABLE(ASSERT) |
16 , m_isPromiseCalled(false) | 16 , m_isPromiseCalled(false) |
17 #endif | 17 #endif |
18 { | 18 { |
19 if (executionContext()->activeDOMObjectsAreStopped()) { | 19 if (executionContext()->activeDOMObjectsAreStopped()) { |
20 m_state = ResolvedOrRejected; | 20 m_state = Detached; |
21 m_resolver.clear(); | 21 m_resolver.clear(); |
22 } | 22 } |
23 } | 23 } |
24 | 24 |
25 void ScriptPromiseResolver::suspend() | 25 void ScriptPromiseResolver::suspend() |
26 { | 26 { |
27 m_timer.stop(); | 27 m_timer.stop(); |
28 } | 28 } |
29 | 29 |
30 void ScriptPromiseResolver::resume() | 30 void ScriptPromiseResolver::resume() |
31 { | 31 { |
32 if (m_state == Resolving || m_state == Rejecting) | 32 if (m_state == Resolving || m_state == Rejecting) |
33 m_timer.startOneShot(0, BLINK_FROM_HERE); | 33 m_timer.startOneShot(0, BLINK_FROM_HERE); |
34 } | 34 } |
35 | 35 |
36 void ScriptPromiseResolver::stop() | 36 void ScriptPromiseResolver::detach() |
37 { | 37 { |
| 38 if (m_state == Detached) |
| 39 return; |
38 m_timer.stop(); | 40 m_timer.stop(); |
39 clear(); | 41 m_state = Detached; |
| 42 m_resolver.clear(); |
| 43 m_value.clear(); |
| 44 m_keepAlive.clear(); |
40 } | 45 } |
41 | 46 |
42 void ScriptPromiseResolver::keepAliveWhilePending() | 47 void ScriptPromiseResolver::keepAliveWhilePending() |
43 { | 48 { |
44 // keepAliveWhilePending() will be called twice if the resolver | 49 // keepAliveWhilePending() will be called twice if the resolver |
45 // is created in a suspended execution context and the resolver | 50 // is created in a suspended execution context and the resolver |
46 // is then resolved/rejected while in that suspended state. | 51 // is then resolved/rejected while in that suspended state. |
47 if (m_state == ResolvedOrRejected || m_keepAlive) | 52 if (m_state == Detached || m_keepAlive) |
48 return; | 53 return; |
49 | 54 |
50 // Keep |this| around while the promise is Pending; | 55 // Keep |this| around while the promise is Pending; |
51 // see clear() for the dual operation. | 56 // see detach() for the dual operation. |
52 m_keepAlive = this; | 57 m_keepAlive = this; |
53 } | 58 } |
54 | 59 |
55 void ScriptPromiseResolver::onTimerFired(Timer<ScriptPromiseResolver>*) | 60 void ScriptPromiseResolver::onTimerFired(Timer<ScriptPromiseResolver>*) |
56 { | 61 { |
57 ASSERT(m_state == Resolving || m_state == Rejecting); | 62 ASSERT(m_state == Resolving || m_state == Rejecting); |
58 if (!scriptState()->contextIsValid()) { | 63 if (!scriptState()->contextIsValid()) { |
59 clear(); | 64 detach(); |
60 return; | 65 return; |
61 } | 66 } |
62 | 67 |
63 ScriptState::Scope scope(m_scriptState.get()); | 68 ScriptState::Scope scope(m_scriptState.get()); |
64 resolveOrRejectImmediately(); | 69 resolveOrRejectImmediately(); |
65 } | 70 } |
66 | 71 |
67 void ScriptPromiseResolver::resolveOrRejectImmediately() | 72 void ScriptPromiseResolver::resolveOrRejectImmediately() |
68 { | 73 { |
69 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); | 74 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); |
70 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); | 75 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); |
71 { | 76 { |
72 if (m_state == Resolving) { | 77 if (m_state == Resolving) { |
73 m_resolver.resolve(m_value.newLocal(m_scriptState->isolate())); | 78 m_resolver.resolve(m_value.newLocal(m_scriptState->isolate())); |
74 } else { | 79 } else { |
75 ASSERT(m_state == Rejecting); | 80 ASSERT(m_state == Rejecting); |
76 m_resolver.reject(m_value.newLocal(m_scriptState->isolate())); | 81 m_resolver.reject(m_value.newLocal(m_scriptState->isolate())); |
77 } | 82 } |
78 } | 83 } |
79 clear(); | 84 detach(); |
80 } | |
81 | |
82 void ScriptPromiseResolver::clear() | |
83 { | |
84 if (m_state == ResolvedOrRejected) | |
85 return; | |
86 m_state = ResolvedOrRejected; | |
87 m_resolver.clear(); | |
88 m_value.clear(); | |
89 m_keepAlive.clear(); | |
90 } | 85 } |
91 | 86 |
92 DEFINE_TRACE(ScriptPromiseResolver) | 87 DEFINE_TRACE(ScriptPromiseResolver) |
93 { | 88 { |
94 ActiveDOMObject::trace(visitor); | 89 ActiveDOMObject::trace(visitor); |
95 } | 90 } |
96 | 91 |
97 } // namespace blink | 92 } // namespace blink |
OLD | NEW |