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 == KeepSelfWhilePending) { | |
21 // Keep |this| while the promise is Pending. | |
22 // deref() will be called in clear(). | |
23 ref(); | |
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); | 56 RefPtr<ScriptPromiseResolverWithContext> protect(this); |
41 ScriptState::Scope scope(m_scriptState.get()); | 57 ScriptState::Scope scope(m_scriptState.get()); |
42 resolveOrRejectImmediately(); | 58 resolveOrRejectImmediately(); |
43 } | 59 } |
(...skipping 12 matching lines...) Expand all Loading... | |
56 } else { | 72 } else { |
57 ASSERT(m_state == Rejecting); | 73 ASSERT(m_state == Rejecting); |
58 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); | 74 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); |
59 } | 75 } |
60 } | 76 } |
61 clear(); | 77 clear(); |
62 } | 78 } |
63 | 79 |
64 void ScriptPromiseResolverWithContext::clear() | 80 void ScriptPromiseResolverWithContext::clear() |
65 { | 81 { |
82 if (m_state == ResolvedOrRejected) | |
83 return; | |
84 RefPtr<ScriptPromiseResolverWithContext> protect(this); | |
haraken
2014/06/16 10:54:04
Why is this protect needed?
yhirano
2014/06/16 11:47:51
It was needed because I checked m_mode after calli
| |
66 ResolutionState state = m_state; | 85 ResolutionState state = m_state; |
67 m_state = ResolvedOrRejected; | 86 m_state = ResolvedOrRejected; |
68 m_resolver.clear(); | 87 m_resolver.clear(); |
69 m_value.clear(); | 88 m_value.clear(); |
70 if (state == Resolving || state == Rejecting) { | 89 if (state == Resolving || state == Rejecting) { |
71 // |ref| was called in |resolveOrReject|. | 90 // |ref| was called in |resolveOrReject|. |
72 deref(); | 91 deref(); |
haraken
2014/06/16 10:54:04
Just help me understand: What's the relationship b
yhirano
2014/06/16 11:47:51
ref() in constructor is for keeping the resolver w
| |
73 } | 92 } |
74 // |this| may be deleted here. | 93 if (m_mode == KeepSelfWhilePending) { |
94 // |ref| was called in the constructor. | |
95 deref(); | |
96 } | |
75 } | 97 } |
76 | 98 |
77 } // namespace WebCore | 99 } // namespace WebCore |
OLD | NEW |