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/V8PerIsolateData.h" |
| 9 #include "core/dom/ExecutionContextTask.h" |
| 10 #include "wtf/PassOwnPtr.h" |
| 11 |
8 namespace WebCore { | 12 namespace WebCore { |
9 | 13 |
| 14 namespace { |
| 15 |
| 16 class RunMicrotasksTask FINAL : public ExecutionContextTask { |
| 17 public: |
| 18 static PassOwnPtr<RunMicrotasksTask> create(NewScriptState* scriptState) |
| 19 { |
| 20 return adoptPtr<RunMicrotasksTask>(new RunMicrotasksTask(scriptState)); |
| 21 } |
| 22 |
| 23 virtual void performTask(ExecutionContext* executionContext) OVERRIDE |
| 24 { |
| 25 if (m_scriptState->contextIsEmpty()) |
| 26 return; |
| 27 if (executionContext->activeDOMObjectsAreStopped()) |
| 28 return; |
| 29 NewScriptState::Scope scope(m_scriptState.get()); |
| 30 v8::V8::RunMicrotasks(m_scriptState->isolate()); |
| 31 } |
| 32 |
| 33 private: |
| 34 explicit RunMicrotasksTask(NewScriptState* scriptState) : m_scriptState(scri
ptState) { } |
| 35 RefPtr<NewScriptState> m_scriptState; |
| 36 }; |
| 37 |
| 38 } // namespace |
| 39 |
10 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(NewScriptStat
e* scriptState) | 40 ScriptPromiseResolverWithContext::ScriptPromiseResolverWithContext(NewScriptStat
e* scriptState) |
11 : ActiveDOMObject(scriptState->executionContext()) | 41 : ActiveDOMObject(scriptState->executionContext()) |
12 , m_state(Pending) | 42 , m_state(Pending) |
13 , m_scriptState(scriptState) | 43 , m_scriptState(scriptState) |
14 , m_timer(this, &ScriptPromiseResolverWithContext::resolveOrRejectImmediatel
y) | 44 , m_timer(this, &ScriptPromiseResolverWithContext::onTimerFired) |
15 , m_resolver(ScriptPromiseResolver::create(m_scriptState->executionContext()
)) { } | 45 , m_resolver(ScriptPromiseResolver::create(m_scriptState->executionContext()
)) { } |
16 | 46 |
17 void ScriptPromiseResolverWithContext::suspend() | 47 void ScriptPromiseResolverWithContext::suspend() |
18 { | 48 { |
19 m_timer.stop(); | 49 m_timer.stop(); |
20 } | 50 } |
21 | 51 |
22 void ScriptPromiseResolverWithContext::resume() | 52 void ScriptPromiseResolverWithContext::resume() |
23 { | 53 { |
24 if (m_state == Resolving || m_state == Rejecting) | 54 if (m_state == Resolving || m_state == Rejecting) |
25 m_timer.startOneShot(0, FROM_HERE); | 55 m_timer.startOneShot(0, FROM_HERE); |
26 } | 56 } |
27 | 57 |
28 void ScriptPromiseResolverWithContext::stop() | 58 void ScriptPromiseResolverWithContext::stop() |
29 { | 59 { |
30 m_timer.stop(); | 60 m_timer.stop(); |
31 clear(); | 61 clear(); |
32 } | 62 } |
33 | 63 |
34 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately(Timer<ScriptPr
omiseResolverWithContext>*) | 64 void ScriptPromiseResolverWithContext::onTimerFired(Timer<ScriptPromiseResolverW
ithContext>*) |
| 65 { |
| 66 RefPtr<ScriptPromiseResolverWithContext> protect(this); |
| 67 NewScriptState::Scope scope(m_scriptState.get()); |
| 68 v8::Isolate* isolate = m_scriptState->isolate(); |
| 69 resolveOrRejectImmediately(); |
| 70 |
| 71 // There is no need to post a RunMicrotasksTask because it is safe to |
| 72 // call RunMicrotasks here. |
| 73 v8::V8::RunMicrotasks(isolate); |
| 74 } |
| 75 |
| 76 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately() |
35 { | 77 { |
36 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); | 78 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); |
37 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); | 79 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); |
38 if (m_state == Resolving) { | 80 if (m_state == Resolving) { |
39 NewScriptState::Scope scope(m_scriptState.get()); | |
40 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); | 81 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); |
41 } else { | 82 } else { |
42 ASSERT(m_state == Rejecting); | 83 ASSERT(m_state == Rejecting); |
43 NewScriptState::Scope scope(m_scriptState.get()); | |
44 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); | 84 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); |
45 } | 85 } |
46 m_state = ResolvedOrRejected; | |
47 clear(); | 86 clear(); |
48 } | 87 } |
49 | 88 |
| 89 void ScriptPromiseResolverWithContext::postRunMicrotasks() |
| 90 { |
| 91 executionContext()->postTask(RunMicrotasksTask::create(m_scriptState.get()))
; |
| 92 } |
| 93 |
50 void ScriptPromiseResolverWithContext::clear() | 94 void ScriptPromiseResolverWithContext::clear() |
51 { | 95 { |
| 96 ResolutionState state = m_state; |
| 97 m_state = ResolvedOrRejected; |
52 m_resolver.clear(); | 98 m_resolver.clear(); |
53 m_value.clear(); | 99 m_value.clear(); |
| 100 if (state == Resolving || state == Rejecting) { |
| 101 // |ref| was called in |resolveOrReject|. |
| 102 deref(); |
| 103 } |
| 104 // |this| may be deleted here. |
54 } | 105 } |
55 | 106 |
56 } // namespace WebCore | 107 } // namespace WebCore |
OLD | NEW |