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 namespace blink { | 8 namespace blink { |
9 | 9 |
10 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState) | 10 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState) |
11 : ActiveDOMObject(scriptState->executionContext()) | 11 : ActiveDOMObject(scriptState->executionContext()) |
12 , m_state(Pending) | 12 , m_state(Pending) |
13 , m_scriptState(scriptState) | 13 , m_scriptState(scriptState) |
14 , m_mode(Default) | |
15 , m_timer(this, &ScriptPromiseResolver::onTimerFired) | 14 , m_timer(this, &ScriptPromiseResolver::onTimerFired) |
16 , m_resolver(scriptState) | 15 , m_resolver(scriptState) |
17 #if ENABLE(ASSERT) | 16 #if ENABLE(ASSERT) |
18 , m_isPromiseCalled(false) | 17 , m_isPromiseCalled(false) |
19 #endif | 18 #endif |
20 { | 19 { |
21 if (executionContext()->activeDOMObjectsAreStopped()) { | 20 if (executionContext()->activeDOMObjectsAreStopped()) { |
22 m_state = ResolvedOrRejected; | 21 m_state = ResolvedOrRejected; |
23 m_resolver.clear(); | 22 m_resolver.clear(); |
24 } | 23 } |
(...skipping 11 matching lines...) Expand all Loading... |
36 } | 35 } |
37 | 36 |
38 void ScriptPromiseResolver::stop() | 37 void ScriptPromiseResolver::stop() |
39 { | 38 { |
40 m_timer.stop(); | 39 m_timer.stop(); |
41 clear(); | 40 clear(); |
42 } | 41 } |
43 | 42 |
44 void ScriptPromiseResolver::keepAliveWhilePending() | 43 void ScriptPromiseResolver::keepAliveWhilePending() |
45 { | 44 { |
46 if (m_state == ResolvedOrRejected || m_mode == KeepAliveWhilePending) | 45 // keepAliveWhilePending() will be called twice if the resolver |
| 46 // is created in a suspended execution context and the resolver |
| 47 // is then resolved/rejected while in that suspended state. |
| 48 if (m_state == ResolvedOrRejected || m_keepAlive) |
47 return; | 49 return; |
48 | 50 |
49 // Keep |this| while the promise is Pending. | 51 // Keep |this| around while the promise is Pending; |
50 // deref() will be called in clear(). | 52 // see clear() for the dual operation. |
51 m_mode = KeepAliveWhilePending; | 53 m_keepAlive = this; |
52 ref(); | |
53 } | 54 } |
54 | 55 |
55 void ScriptPromiseResolver::onTimerFired(Timer<ScriptPromiseResolver>*) | 56 void ScriptPromiseResolver::onTimerFired(Timer<ScriptPromiseResolver>*) |
56 { | 57 { |
57 ASSERT(m_state == Resolving || m_state == Rejecting); | 58 ASSERT(m_state == Resolving || m_state == Rejecting); |
58 if (!scriptState()->contextIsValid()) { | 59 if (!scriptState()->contextIsValid()) { |
59 clear(); | 60 clear(); |
60 return; | 61 return; |
61 } | 62 } |
62 | 63 |
(...skipping 13 matching lines...) Expand all Loading... |
76 m_resolver.reject(m_value.newLocal(m_scriptState->isolate())); | 77 m_resolver.reject(m_value.newLocal(m_scriptState->isolate())); |
77 } | 78 } |
78 } | 79 } |
79 clear(); | 80 clear(); |
80 } | 81 } |
81 | 82 |
82 void ScriptPromiseResolver::clear() | 83 void ScriptPromiseResolver::clear() |
83 { | 84 { |
84 if (m_state == ResolvedOrRejected) | 85 if (m_state == ResolvedOrRejected) |
85 return; | 86 return; |
86 ResolutionState state = m_state; | |
87 m_state = ResolvedOrRejected; | 87 m_state = ResolvedOrRejected; |
88 m_resolver.clear(); | 88 m_resolver.clear(); |
89 m_value.clear(); | 89 m_value.clear(); |
90 if (m_mode == KeepAliveWhilePending) { | 90 m_keepAlive.clear(); |
91 // |ref| was called in |keepAliveWhilePending|. | |
92 deref(); | |
93 } | |
94 // |this| may be deleted here, but it is safe to check |state| because | |
95 // it doesn't depend on |this|. When |this| is deleted, |state| can't be | |
96 // |Resolving| nor |Rejecting| and hence |this->deref()| can't be executed. | |
97 if (state == Resolving || state == Rejecting) { | |
98 // |ref| was called in |resolveOrReject|. | |
99 deref(); | |
100 } | |
101 } | 91 } |
102 | 92 |
103 DEFINE_TRACE(ScriptPromiseResolver) | 93 DEFINE_TRACE(ScriptPromiseResolver) |
104 { | 94 { |
105 ActiveDOMObject::trace(visitor); | 95 ActiveDOMObject::trace(visitor); |
106 } | 96 } |
107 | 97 |
108 } // namespace blink | 98 } // namespace blink |
OLD | NEW |