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

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)
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(Default)
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 (executionContext()->activeDOMObjectsAreStopped())
21 m_state = ResolvedOrRejected;
22 }
23
24 ScriptPromiseResolverWithContext::~ScriptPromiseResolverWithContext()
25 {
26 if (m_state != ResolvedOrRejected) {
27 ScriptState::Scope scope(m_scriptState.get());
28 reject(v8::Exception::Error(v8::String::NewFromUtf8(m_scriptState->isola te(),
29 "ScriptPromiseResolverWithContext is destructed without resolve / re ject")));
30 }
19 } 31 }
20 32
21 void ScriptPromiseResolverWithContext::suspend() 33 void ScriptPromiseResolverWithContext::suspend()
22 { 34 {
23 m_timer.stop(); 35 m_timer.stop();
24 } 36 }
25 37
26 void ScriptPromiseResolverWithContext::resume() 38 void ScriptPromiseResolverWithContext::resume()
27 { 39 {
28 if (m_state == Resolving || m_state == Rejecting) 40 if (m_state == Resolving || m_state == Rejecting)
29 m_timer.startOneShot(0, FROM_HERE); 41 m_timer.startOneShot(0, FROM_HERE);
30 } 42 }
31 43
32 void ScriptPromiseResolverWithContext::stop() 44 void ScriptPromiseResolverWithContext::stop()
33 { 45 {
34 m_timer.stop(); 46 m_timer.stop();
35 clear(); 47 clear();
36 } 48 }
37 49
50 void ScriptPromiseResolverWithContext::keepAliveWhilePending()
51 {
52 if (m_state == ResolvedOrRejected || m_mode == KeepAliveWhilePending)
53 return;
54
55 // Keep |this| while the promise is Pending.
56 // deref() will be called in clear().
57 m_mode = KeepAliveWhilePending;
58 ref();
59 }
60
38 void ScriptPromiseResolverWithContext::onTimerFired(Timer<ScriptPromiseResolverW ithContext>*) 61 void ScriptPromiseResolverWithContext::onTimerFired(Timer<ScriptPromiseResolverW ithContext>*)
39 { 62 {
40 RefPtr<ScriptPromiseResolverWithContext> protect(this);
41 ScriptState::Scope scope(m_scriptState.get()); 63 ScriptState::Scope scope(m_scriptState.get());
42 resolveOrRejectImmediately(); 64 resolveOrRejectImmediately();
43 } 65 }
44 66
45 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately() 67 void ScriptPromiseResolverWithContext::resolveOrRejectImmediately()
46 { 68 {
47 ASSERT(!executionContext()->activeDOMObjectsAreStopped()); 69 ASSERT(!executionContext()->activeDOMObjectsAreStopped());
48 ASSERT(!executionContext()->activeDOMObjectsAreSuspended()); 70 ASSERT(!executionContext()->activeDOMObjectsAreSuspended());
49 { 71 {
50 // FIXME: The V8RecursionScope is only necessary to force microtask deli very for promises 72 // 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 73 // 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). 74 // at the end of every task (rather than just the main thread).
53 V8RecursionScope scope(m_scriptState->isolate(), m_scriptState->executio nContext()); 75 V8RecursionScope scope(m_scriptState->isolate(), m_scriptState->executio nContext());
54 if (m_state == Resolving) { 76 if (m_state == Resolving) {
55 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate())); 77 m_resolver->resolve(m_value.newLocal(m_scriptState->isolate()));
56 } else { 78 } else {
57 ASSERT(m_state == Rejecting); 79 ASSERT(m_state == Rejecting);
58 m_resolver->reject(m_value.newLocal(m_scriptState->isolate())); 80 m_resolver->reject(m_value.newLocal(m_scriptState->isolate()));
59 } 81 }
60 } 82 }
61 clear(); 83 clear();
62 } 84 }
63 85
64 void ScriptPromiseResolverWithContext::clear() 86 void ScriptPromiseResolverWithContext::clear()
65 { 87 {
88 if (m_state == ResolvedOrRejected)
89 return;
66 ResolutionState state = m_state; 90 ResolutionState state = m_state;
67 m_state = ResolvedOrRejected; 91 m_state = ResolvedOrRejected;
68 m_resolver.clear(); 92 m_resolver.clear();
69 m_value.clear(); 93 m_value.clear();
94 if (m_mode == KeepAliveWhilePending) {
95 // |ref| was called in the constructor.
96 deref();
97 }
98 // |this| may be deleted here, but it is safe to check |state| because
99 // it doesn't depend on |this|. When |this| is deleted, |state| can't be
100 // |Resolving| nor |Rejecting| and hence |this->deref()| can't be executed.
70 if (state == Resolving || state == Rejecting) { 101 if (state == Resolving || state == Rejecting) {
71 // |ref| was called in |resolveOrReject|. 102 // |ref| was called in |resolveOrReject|.
72 deref(); 103 deref();
73 } 104 }
74 // |this| may be deleted here.
75 } 105 }
76 106
77 } // namespace WebCore 107 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/bindings/v8/ScriptPromiseResolverWithContext.h ('k') | Source/modules/crypto/CryptoResultImpl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698