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 "bindings/core/v8/ScriptPromiseResolver.h" | 5 #include "bindings/core/v8/ScriptPromiseResolver.h" |
6 | 6 |
7 #include "core/dom/ExecutionContext.h" | 7 #include "core/dom/ExecutionContext.h" |
8 #include "core/dom/TaskRunnerHelper.h" | 8 #include "core/dom/TaskRunnerHelper.h" |
9 #include "core/probe/CoreProbes.h" | |
10 | 9 |
11 namespace blink { | 10 namespace blink { |
12 | 11 |
13 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* script_state) | 12 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* script_state) |
14 : SuspendableObject(ExecutionContext::From(script_state)), | 13 : SuspendableObject(ExecutionContext::From(script_state)), |
15 state_(kPending), | 14 state_(kPending), |
16 script_state_(script_state), | 15 script_state_(script_state), |
17 timer_(TaskRunnerHelper::Get(TaskType::kMicrotask, GetExecutionContext()), | 16 timer_(TaskRunnerHelper::Get(TaskType::kMicrotask, GetExecutionContext()), |
18 this, | 17 this, |
19 &ScriptPromiseResolver::OnTimerFired), | 18 &ScriptPromiseResolver::OnTimerFired), |
20 resolver_(script_state) { | 19 resolver_(script_state) { |
21 if (GetExecutionContext()->IsContextDestroyed()) { | 20 if (GetExecutionContext()->IsContextDestroyed()) { |
22 state_ = kDetached; | 21 state_ = kDetached; |
23 resolver_.Clear(); | 22 resolver_.Clear(); |
24 } | 23 } |
25 probe::AsyncTaskScheduled(GetExecutionContext(), "Promise", this); | |
dgozman
2017/05/08 23:28:05
This also removes trace event.
| |
26 } | 24 } |
27 | 25 |
28 void ScriptPromiseResolver::Suspend() { | 26 void ScriptPromiseResolver::Suspend() { |
29 timer_.Stop(); | 27 timer_.Stop(); |
30 } | 28 } |
31 | 29 |
32 void ScriptPromiseResolver::Resume() { | 30 void ScriptPromiseResolver::Resume() { |
33 if (state_ == kResolving || state_ == kRejecting) | 31 if (state_ == kResolving || state_ == kRejecting) |
34 timer_.StartOneShot(0, BLINK_FROM_HERE); | 32 timer_.StartOneShot(0, BLINK_FROM_HERE); |
35 } | 33 } |
36 | 34 |
37 void ScriptPromiseResolver::Detach() { | 35 void ScriptPromiseResolver::Detach() { |
38 if (state_ == kDetached) | 36 if (state_ == kDetached) |
39 return; | 37 return; |
40 timer_.Stop(); | 38 timer_.Stop(); |
41 state_ = kDetached; | 39 state_ = kDetached; |
42 resolver_.Clear(); | 40 resolver_.Clear(); |
43 value_.Clear(); | 41 value_.Clear(); |
44 keep_alive_.Clear(); | 42 keep_alive_.Clear(); |
45 probe::AsyncTaskCanceled(GetExecutionContext(), this); | |
46 } | 43 } |
47 | 44 |
48 void ScriptPromiseResolver::KeepAliveWhilePending() { | 45 void ScriptPromiseResolver::KeepAliveWhilePending() { |
49 // keepAliveWhilePending() will be called twice if the resolver | 46 // keepAliveWhilePending() will be called twice if the resolver |
50 // is created in a suspended execution context and the resolver | 47 // is created in a suspended execution context and the resolver |
51 // is then resolved/rejected while in that suspended state. | 48 // is then resolved/rejected while in that suspended state. |
52 if (state_ == kDetached || keep_alive_) | 49 if (state_ == kDetached || keep_alive_) |
53 return; | 50 return; |
54 | 51 |
55 // Keep |this| around while the promise is Pending; | 52 // Keep |this| around while the promise is Pending; |
56 // see detach() for the dual operation. | 53 // see detach() for the dual operation. |
57 keep_alive_ = this; | 54 keep_alive_ = this; |
58 } | 55 } |
59 | 56 |
60 void ScriptPromiseResolver::OnTimerFired(TimerBase*) { | 57 void ScriptPromiseResolver::OnTimerFired(TimerBase*) { |
61 DCHECK(state_ == kResolving || state_ == kRejecting); | 58 DCHECK(state_ == kResolving || state_ == kRejecting); |
62 if (!GetScriptState()->ContextIsValid()) { | 59 if (!GetScriptState()->ContextIsValid()) { |
63 Detach(); | 60 Detach(); |
64 return; | 61 return; |
65 } | 62 } |
66 | 63 |
67 ScriptState::Scope scope(script_state_.Get()); | 64 ScriptState::Scope scope(script_state_.Get()); |
68 ResolveOrRejectImmediately(); | 65 ResolveOrRejectImmediately(); |
69 } | 66 } |
70 | 67 |
71 void ScriptPromiseResolver::ResolveOrRejectImmediately() { | 68 void ScriptPromiseResolver::ResolveOrRejectImmediately() { |
72 DCHECK(!GetExecutionContext()->IsContextDestroyed()); | 69 DCHECK(!GetExecutionContext()->IsContextDestroyed()); |
73 DCHECK(!GetExecutionContext()->IsContextSuspended()); | 70 DCHECK(!GetExecutionContext()->IsContextSuspended()); |
74 { | 71 { |
75 probe::AsyncTask async_task(GetExecutionContext(), this); | |
76 if (state_ == kResolving) { | 72 if (state_ == kResolving) { |
77 resolver_.Resolve(value_.NewLocal(script_state_->GetIsolate())); | 73 resolver_.Resolve(value_.NewLocal(script_state_->GetIsolate())); |
78 } else { | 74 } else { |
79 ASSERT(state_ == kRejecting); | 75 ASSERT(state_ == kRejecting); |
80 resolver_.Reject(value_.NewLocal(script_state_->GetIsolate())); | 76 resolver_.Reject(value_.NewLocal(script_state_->GetIsolate())); |
81 } | 77 } |
82 } | 78 } |
83 Detach(); | 79 Detach(); |
84 } | 80 } |
85 | 81 |
86 DEFINE_TRACE(ScriptPromiseResolver) { | 82 DEFINE_TRACE(ScriptPromiseResolver) { |
87 SuspendableObject::Trace(visitor); | 83 SuspendableObject::Trace(visitor); |
88 } | 84 } |
89 | 85 |
90 } // namespace blink | 86 } // namespace blink |
OLD | NEW |