Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: Source/bindings/v8/ScriptPromiseResolverWithContext.cpp

Issue 311733004: Introduce KeepAliveWhilePending to ScriptPromiseResolverWithContext. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@refactor-webmidi-initialization
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 == KeepAliveWhilePending) {
21 // In order to call ref() here, I relax the adoption requirement.
22 relaxAdoptionRequirement();
23 // Keep |this| while the promise is Pending.
24 // deref() will be called in clear().
25 ref();
26 }
27 }
28
29 ScriptPromiseResolverWithContext::~ScriptPromiseResolverWithContext()
30 {
31 if (m_state != ResolvedOrRejected) {
32 ScriptState::Scope scope(m_scriptState.get());
33 reject(v8::Exception::Error(v8::String::NewFromUtf8(m_scriptState->isola te(),
34 "ScriptPromiseResolverWithContext is destructed without resolve / re ject")));
35 }
19 } 36 }
20 37
21 void ScriptPromiseResolverWithContext::suspend() 38 void ScriptPromiseResolverWithContext::suspend()
22 { 39 {
23 m_timer.stop(); 40 m_timer.stop();
24 } 41 }
25 42
26 void ScriptPromiseResolverWithContext::resume() 43 void ScriptPromiseResolverWithContext::resume()
27 { 44 {
28 if (m_state == Resolving || m_state == Rejecting) 45 if (m_state == Resolving || m_state == Rejecting)
29 m_timer.startOneShot(0, FROM_HERE); 46 m_timer.startOneShot(0, FROM_HERE);
30 } 47 }
31 48
32 void ScriptPromiseResolverWithContext::stop() 49 void ScriptPromiseResolverWithContext::stop()
33 { 50 {
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);
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()));
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;
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();
88 if (m_mode == KeepAliveWhilePending) {
89 // |ref| was called in the constructor.
90 deref();
91 }
92 // |this| may be deleted here, but it is safe to check |state| because
93 // it doesn't depend on |this|. When |this| is deleted, |state| can't be
94 // |Resolving| nor |Rejecting| and hence |this->deref()| can't be executed.
70 if (state == Resolving || state == Rejecting) { 95 if (state == Resolving || state == Rejecting) {
71 // |ref| was called in |resolveOrReject|. 96 // |ref| was called in |resolveOrReject|.
72 deref(); 97 deref();
73 } 98 }
74 // |this| may be deleted here.
75 } 99 }
76 100
77 } // namespace WebCore 101 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698