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/V8HiddenValue.h" | |
8 #include "bindings/v8/V8RecursionScope.h" | 9 #include "bindings/v8/V8RecursionScope.h" |
9 | 10 |
10 namespace WebCore { | 11 namespace WebCore { |
11 | 12 |
13 namespace { | |
14 const char hiddenPropertyName[] = "blink::keepObjectWhilePending"; | |
haraken
2014/06/13 14:23:31
Can you use V8HiddenPropertyName::pendingObject? Y
| |
15 } // namespace | |
16 | |
12 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(ScriptState* scriptState) | 17 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(ScriptState* scriptState) |
13 : ActiveDOMObject(scriptState->executionContext()) | 18 : ActiveDOMObject(scriptState->executionContext()) |
14 , m_state(Pending) | 19 , m_state(Pending) |
15 , m_scriptState(scriptState) | 20 , m_scriptState(scriptState) |
16 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) | 21 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) |
17 , m_resolver(ScriptPromiseResolver::create(m_scriptState.get())) | 22 , m_resolver(ScriptPromiseResolver::create(m_scriptState.get())) |
18 { | 23 { |
19 } | 24 } |
20 | 25 |
26 ScriptPromiseResolverWithContext::~ScriptPromiseResolverWithContext() | |
27 { | |
28 if (m_state != ResolvedOrRejected) { | |
29 ScriptState::Scope scope(m_scriptState.get()); | |
30 unregisterKeptObject(); | |
31 reject(v8::Exception::Error(v8::String::NewFromUtf8(m_scriptState->isola te(), | |
32 "ScriptPromiseResolverWithContext is destructed without resolve / re ject"))); | |
33 } | |
34 } | |
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()); | |
50 unregisterKeptObject(); | |
34 m_timer.stop(); | 51 m_timer.stop(); |
35 clear(); | 52 clear(); |
36 } | 53 } |
37 | 54 |
38 void ScriptPromiseResolverWithContext::onTimerFired(Timer<ScriptPromiseResolverW ithContext>*) | 55 void ScriptPromiseResolverWithContext::onTimerFired(Timer<ScriptPromiseResolverW ithContext>*) |
39 { | 56 { |
40 RefPtr<ScriptPromiseResolverWithContext> protect(this); | 57 RefPtr<ScriptPromiseResolverWithContext> protect(this); |
41 ScriptState::Scope scope(m_scriptState.get()); | 58 ScriptState::Scope scope(m_scriptState.get()); |
42 resolveOrRejectImmediately(); | 59 resolveOrRejectImmediately(); |
43 } | 60 } |
44 | 61 |
45 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately() | 62 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately() |
46 { | 63 { |
47 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); | 64 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); |
48 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); | 65 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); |
66 unregisterKeptObject(); | |
haraken
2014/06/13 14:23:31
I think you need to enter ScriptState::Scope befor
| |
49 { | 67 { |
50 // FIXME: The V8RecursionScope is only necessary to force microtask deli very for promises | 68 // 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 | 69 // 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). | 70 // at the end of every task (rather than just the main thread). |
53 V8RecursionScope scope(m_scriptState->isolate(), m_scriptState->executio nContext()); | 71 V8RecursionScope scope(m_scriptState->isolate(), m_scriptState->executio nContext()); |
54 if (m_state == Resolving) { | 72 if (m_state == Resolving) { |
55 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); | 73 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); |
56 } else { | 74 } else { |
57 ASSERT(m_state == Rejecting); | 75 ASSERT(m_state == Rejecting); |
58 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); | 76 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); |
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 if (!m_resolver) | |
98 return; | |
99 ASSERT(m_scriptState->isolate()->InContext()); | |
100 ScriptPromise promise = this->promise(); | |
101 if (promise.isEmpty()) | |
102 return; | |
103 v8::Isolate* isolate = m_scriptState->isolate(); | |
104 v8::Local<v8::Promise> v8Promise = promise.v8Value().As<v8::Promise>(); | |
105 v8::Local<v8::String> name = v8::String::NewFromUtf8(isolate, hiddenProperty Name); | |
106 ASSERT(V8HiddenValue::getHiddenValue(isolate, v8Promise, name).IsEmpty()); | |
107 V8HiddenValue::setHiddenValue(isolate, v8Promise, name, value); | |
108 } | |
109 | |
110 void ScriptPromiseResolverWithContext::unregisterKeptObject() | |
111 { | |
112 if (!m_resolver) | |
113 return; | |
114 ASSERT(m_scriptState->isolate()->InContext()); | |
115 ScriptPromise promise = this->promise(); | |
116 if (promise.isEmpty()) | |
117 return; | |
118 v8::Isolate* isolate = m_scriptState->isolate(); | |
119 v8::Local<v8::Promise> v8Promise = promise.v8Value().As<v8::Promise>(); | |
120 v8::Local<v8::String> name = v8::String::NewFromUtf8(isolate, hiddenProperty Name); | |
121 V8HiddenValue::deleteHiddenValue(isolate, v8Promise, name); | |
122 } | |
123 | |
77 } // namespace WebCore | 124 } // namespace WebCore |
OLD | NEW |