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 namespace { | |
13 const char hiddenPropertyName[] = "blink::keepObjectWhilePending"; | |
14 } // namespace | |
15 | |
12 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(ScriptState* scriptState) | 16 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(ScriptState* scriptState) |
13 : ActiveDOMObject(scriptState->executionContext()) | 17 : ActiveDOMObject(scriptState->executionContext()) |
14 , m_state(Pending) | 18 , m_state(Pending) |
15 , m_scriptState(scriptState) | 19 , m_scriptState(scriptState) |
16 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) | 20 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) |
17 , m_resolver(ScriptPromiseResolver::create(m_scriptState.get())) | 21 , m_resolver(ScriptPromiseResolver::create(m_scriptState.get())) |
18 { | 22 { |
19 } | 23 } |
20 | 24 |
25 ScriptPromiseResolverWithContext::~ScriptPromiseResolverWithContext() | |
26 { | |
27 if (m_state != ResolvedOrRejected) { | |
28 ScriptState::Scope scope(m_scriptState.get()); | |
29 unregisterKeptObject(); | |
30 reject(v8::Exception::Error(v8::String::NewFromUtf8(m_scriptState->isola te(), | |
31 "ScriptPromiseResolverWithContext is destructed without resolve / re ject"))); | |
32 } | |
33 } | |
34 | |
21 void ScriptPromiseResolverWithContext::suspend() | 35 void ScriptPromiseResolverWithContext::suspend() |
22 { | 36 { |
23 m_timer.stop(); | 37 m_timer.stop(); |
24 } | 38 } |
25 | 39 |
26 void ScriptPromiseResolverWithContext::resume() | 40 void ScriptPromiseResolverWithContext::resume() |
27 { | 41 { |
28 if (m_state == Resolving || m_state == Rejecting) | 42 if (m_state == Resolving || m_state == Rejecting) |
29 m_timer.startOneShot(0, FROM_HERE); | 43 m_timer.startOneShot(0, FROM_HERE); |
30 } | 44 } |
31 | 45 |
32 void ScriptPromiseResolverWithContext::stop() | 46 void ScriptPromiseResolverWithContext::stop() |
33 { | 47 { |
48 ScriptState::Scope scope(m_scriptState.get()); | |
49 unregisterKeptObject(); | |
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 } |
44 | 60 |
45 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately() | 61 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately() |
46 { | 62 { |
47 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); | 63 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); |
48 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); | 64 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); |
49 { | 65 { |
50 // FIXME: The V8RecursionScope is only necessary to force microtask deli very for promises | 66 // 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 | 67 // 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). | 68 // at the end of every task (rather than just the main thread). |
53 V8RecursionScope scope(m_scriptState->isolate(), m_scriptState->executio nContext()); | 69 V8RecursionScope scope(m_scriptState->isolate(), m_scriptState->executio nContext()); |
54 if (m_state == Resolving) { | 70 if (m_state == Resolving) { |
55 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); | 71 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); |
72 unregisterKeptObject(); | |
56 } else { | 73 } else { |
57 ASSERT(m_state == Rejecting); | 74 ASSERT(m_state == Rejecting); |
58 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); | 75 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); |
76 unregisterKeptObject(); | |
59 } | 77 } |
60 } | 78 } |
61 clear(); | 79 clear(); |
62 } | 80 } |
63 | 81 |
64 void ScriptPromiseResolverWithContext::clear() | 82 void ScriptPromiseResolverWithContext::clear() |
65 { | 83 { |
66 ResolutionState state = m_state; | 84 ResolutionState state = m_state; |
67 m_state = ResolvedOrRejected; | 85 m_state = ResolvedOrRejected; |
68 m_resolver.clear(); | 86 m_resolver.clear(); |
69 m_value.clear(); | 87 m_value.clear(); |
70 if (state == Resolving || state == Rejecting) { | 88 if (state == Resolving || state == Rejecting) { |
71 // |ref| was called in |resolveOrReject|. | 89 // |ref| was called in |resolveOrReject|. |
72 deref(); | 90 deref(); |
73 } | 91 } |
74 // |this| may be deleted here. | 92 // |this| may be deleted here. |
75 } | 93 } |
76 | 94 |
95 void ScriptPromiseResolverWithContext::keepObjectWhilePendingInternal(v8::Handle <v8::Value> value) | |
96 { | |
97 ScriptState::Scope scope(m_scriptState.get()); | |
98 if (!m_resolver) | |
99 return; | |
100 ScriptPromise promise = this->promise(); | |
101 if (promise.isEmpty()) | |
102 return; | |
103 v8::Local<v8::Promise> v8Promise = promise.v8Value().As<v8::Promise>(); | |
104 v8::Local<v8::String> name = v8::String::NewFromUtf8(m_scriptState->isolate( ), hiddenPropertyName); | |
105 ASSERT(v8Promise->GetHiddenValue(name).IsEmpty()); | |
106 v8Promise->SetHiddenValue(name, value); | |
107 } | |
108 | |
109 void ScriptPromiseResolverWithContext::unregisterKeptObject() | |
110 { | |
111 if (!m_resolver) | |
112 return; | |
113 ASSERT(m_scriptState->isolate()->InContext()); | |
114 ScriptPromise promise = this->promise(); | |
jrummell
2014/06/12 20:10:24
The comment for promise() states "an empty ScriptP
yhirano
2014/06/13 04:19:30
You are right, thanks for pointing it out.
Fixed.
| |
115 if (promise.isEmpty()) | |
116 return; | |
117 v8::Local<v8::Promise> v8Promise = promise.v8Value().As<v8::Promise>(); | |
118 v8::Local<v8::String> name = v8::String::NewFromUtf8(m_scriptState->isolate( ), hiddenPropertyName); | |
119 v8Promise->DeleteHiddenValue(name); | |
120 } | |
121 | |
77 } // namespace WebCore | 122 } // namespace WebCore |
OLD | NEW |