OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/RejectedPromises.h" | 5 #include "bindings/core/v8/RejectedPromises.h" |
6 | 6 |
7 #include "bindings/core/v8/ScopedPersistent.h" | 7 #include "bindings/core/v8/ScopedPersistent.h" |
8 #include "bindings/core/v8/ScriptCallStack.h" | 8 #include "bindings/core/v8/ScriptCallStack.h" |
9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
10 #include "bindings/core/v8/ScriptValue.h" | 10 #include "bindings/core/v8/ScriptValue.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 if (value.IsEmpty() || !value->IsPromise()) | 61 if (value.IsEmpty() || !value->IsPromise()) |
62 return; | 62 return; |
63 ASSERT(!hasHandler()); | 63 ASSERT(!hasHandler()); |
64 | 64 |
65 EventTarget* target = executionContext->errorEventTarget(); | 65 EventTarget* target = executionContext->errorEventTarget(); |
66 if (RuntimeEnabledFeatures::promiseRejectionEventEnabled() && target &&
!executionContext->shouldSanitizeScriptError(m_resourceName, m_corsStatus)) { | 66 if (RuntimeEnabledFeatures::promiseRejectionEventEnabled() && target &&
!executionContext->shouldSanitizeScriptError(m_resourceName, m_corsStatus)) { |
67 PromiseRejectionEventInit init; | 67 PromiseRejectionEventInit init; |
68 init.setPromise(ScriptPromise(m_scriptState, value)); | 68 init.setPromise(ScriptPromise(m_scriptState, value)); |
69 init.setReason(ScriptValue(m_scriptState, reason)); | 69 init.setReason(ScriptValue(m_scriptState, reason)); |
70 init.setCancelable(true); | 70 init.setCancelable(true); |
71 RefPtrWillBeRawPtr<PromiseRejectionEvent> event = PromiseRejectionEv
ent::create(m_scriptState, EventTypeNames::unhandledrejection, init); | 71 RawPtr<PromiseRejectionEvent> event = PromiseRejectionEvent::create(
m_scriptState, EventTypeNames::unhandledrejection, init); |
72 // Log to console if event was not canceled. | 72 // Log to console if event was not canceled. |
73 m_shouldLogToConsole = target->dispatchEvent(event) == DispatchEvent
Result::NotCanceled; | 73 m_shouldLogToConsole = target->dispatchEvent(event) == DispatchEvent
Result::NotCanceled; |
74 } | 74 } |
75 | 75 |
76 if (m_shouldLogToConsole) { | 76 if (m_shouldLogToConsole) { |
77 const String errorMessage = "Uncaught (in promise)"; | 77 const String errorMessage = "Uncaught (in promise)"; |
78 Vector<ScriptValue> args; | 78 Vector<ScriptValue> args; |
79 args.append(ScriptValue(m_scriptState, v8String(m_scriptState->isola
te(), errorMessage))); | 79 args.append(ScriptValue(m_scriptState, v8String(m_scriptState->isola
te(), errorMessage))); |
80 args.append(ScriptValue(m_scriptState, reason)); | 80 args.append(ScriptValue(m_scriptState, reason)); |
81 RefPtrWillBeRawPtr<ScriptArguments> arguments = ScriptArguments::cre
ate(m_scriptState, args); | 81 RawPtr<ScriptArguments> arguments = ScriptArguments::create(m_script
State, args); |
82 | 82 |
83 String embedderErrorMessage = m_errorMessage; | 83 String embedderErrorMessage = m_errorMessage; |
84 if (embedderErrorMessage.isEmpty()) | 84 if (embedderErrorMessage.isEmpty()) |
85 embedderErrorMessage = errorMessage; | 85 embedderErrorMessage = errorMessage; |
86 else if (embedderErrorMessage.startsWith("Uncaught ")) | 86 else if (embedderErrorMessage.startsWith("Uncaught ")) |
87 embedderErrorMessage.insert(" (in promise)", 8); | 87 embedderErrorMessage.insert(" (in promise)", 8); |
88 | 88 |
89 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::
create(JSMessageSource, ErrorMessageLevel, embedderErrorMessage, m_resourceName,
m_lineNumber, m_columnNumber); | 89 RawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(JSMes
sageSource, ErrorMessageLevel, embedderErrorMessage, m_resourceName, m_lineNumbe
r, m_columnNumber); |
90 consoleMessage->setScriptArguments(arguments); | 90 consoleMessage->setScriptArguments(arguments); |
91 consoleMessage->setCallStack(m_callStack); | 91 consoleMessage->setCallStack(m_callStack); |
92 consoleMessage->setScriptId(m_scriptId); | 92 consoleMessage->setScriptId(m_scriptId); |
93 m_consoleMessageId = consoleMessage->assignMessageId(); | 93 m_consoleMessageId = consoleMessage->assignMessageId(); |
94 executionContext->addConsoleMessage(consoleMessage.release()); | 94 executionContext->addConsoleMessage(consoleMessage.release()); |
95 } | 95 } |
96 | 96 |
97 m_callStack.clear(); | 97 m_callStack.clear(); |
98 } | 98 } |
99 | 99 |
100 void revoke() | 100 void revoke() |
101 { | 101 { |
102 ExecutionContext* executionContext = m_scriptState->getExecutionContext(
); | 102 ExecutionContext* executionContext = m_scriptState->getExecutionContext(
); |
103 if (!executionContext) | 103 if (!executionContext) |
104 return; | 104 return; |
105 | 105 |
106 ScriptState::Scope scope(m_scriptState); | 106 ScriptState::Scope scope(m_scriptState); |
107 v8::Local<v8::Value> value = m_promise.newLocal(m_scriptState->isolate()
); | 107 v8::Local<v8::Value> value = m_promise.newLocal(m_scriptState->isolate()
); |
108 v8::Local<v8::Value> reason = m_exception.newLocal(m_scriptState->isolat
e()); | 108 v8::Local<v8::Value> reason = m_exception.newLocal(m_scriptState->isolat
e()); |
109 // Either collected or https://crbug.com/450330 | 109 // Either collected or https://crbug.com/450330 |
110 if (value.IsEmpty() || !value->IsPromise()) | 110 if (value.IsEmpty() || !value->IsPromise()) |
111 return; | 111 return; |
112 | 112 |
113 EventTarget* target = executionContext->errorEventTarget(); | 113 EventTarget* target = executionContext->errorEventTarget(); |
114 if (RuntimeEnabledFeatures::promiseRejectionEventEnabled() && target &&
!executionContext->shouldSanitizeScriptError(m_resourceName, m_corsStatus)) { | 114 if (RuntimeEnabledFeatures::promiseRejectionEventEnabled() && target &&
!executionContext->shouldSanitizeScriptError(m_resourceName, m_corsStatus)) { |
115 PromiseRejectionEventInit init; | 115 PromiseRejectionEventInit init; |
116 init.setPromise(ScriptPromise(m_scriptState, value)); | 116 init.setPromise(ScriptPromise(m_scriptState, value)); |
117 init.setReason(ScriptValue(m_scriptState, reason)); | 117 init.setReason(ScriptValue(m_scriptState, reason)); |
118 RefPtrWillBeRawPtr<PromiseRejectionEvent> event = PromiseRejectionEv
ent::create(m_scriptState, EventTypeNames::rejectionhandled, init); | 118 RawPtr<PromiseRejectionEvent> event = PromiseRejectionEvent::create(
m_scriptState, EventTypeNames::rejectionhandled, init); |
119 target->dispatchEvent(event); | 119 target->dispatchEvent(event); |
120 } | 120 } |
121 | 121 |
122 if (m_shouldLogToConsole) { | 122 if (m_shouldLogToConsole) { |
123 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::
create(JSMessageSource, RevokedErrorMessageLevel, "Handler added to rejected pro
mise"); | 123 RawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(JSMes
sageSource, RevokedErrorMessageLevel, "Handler added to rejected promise"); |
124 consoleMessage->setRelatedMessageId(m_consoleMessageId); | 124 consoleMessage->setRelatedMessageId(m_consoleMessageId); |
125 executionContext->addConsoleMessage(consoleMessage.release()); | 125 executionContext->addConsoleMessage(consoleMessage.release()); |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 void makePromiseWeak() | 129 void makePromiseWeak() |
130 { | 130 { |
131 ASSERT(!m_promise.isEmpty() && !m_promise.isWeak()); | 131 ASSERT(!m_promise.isEmpty() && !m_promise.isWeak()); |
132 m_promise.setWeak(this, &Message::didCollectPromise); | 132 m_promise.setWeak(this, &Message::didCollectPromise); |
133 m_exception.setWeak(this, &Message::didCollectException); | 133 m_exception.setWeak(this, &Message::didCollectException); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 m_queue.remove(it); | 213 m_queue.remove(it); |
214 return; | 214 return; |
215 } | 215 } |
216 } | 216 } |
217 | 217 |
218 // Then look it up in the reported errors. | 218 // Then look it up in the reported errors. |
219 for (size_t i = 0; i < m_reportedAsErrors.size(); ++i) { | 219 for (size_t i = 0; i < m_reportedAsErrors.size(); ++i) { |
220 auto& message = m_reportedAsErrors.at(i); | 220 auto& message = m_reportedAsErrors.at(i); |
221 if (!message->isCollected() && message->hasPromise(data.GetPromise())) { | 221 if (!message->isCollected() && message->hasPromise(data.GetPromise())) { |
222 message->makePromiseStrong(); | 222 message->makePromiseStrong(); |
223 Platform::current()->currentThread()->scheduler()->timerTaskRunner()
->postTask(BLINK_FROM_HERE, bind(&RejectedPromises::revokeNow, PassRefPtrWillBeR
awPtr<RejectedPromises>(this), message.release())); | 223 Platform::current()->currentThread()->scheduler()->timerTaskRunner()
->postTask(BLINK_FROM_HERE, bind(&RejectedPromises::revokeNow, RawPtr<RejectedPr
omises>(this), message.release())); |
224 m_reportedAsErrors.remove(i); | 224 m_reportedAsErrors.remove(i); |
225 return; | 225 return; |
226 } | 226 } |
227 } | 227 } |
228 } | 228 } |
229 | 229 |
230 PassOwnPtr<RejectedPromises::MessageQueue> RejectedPromises::createMessageQueue(
) | 230 PassOwnPtr<RejectedPromises::MessageQueue> RejectedPromises::createMessageQueue(
) |
231 { | 231 { |
232 return adoptPtr(new MessageQueue()); | 232 return adoptPtr(new MessageQueue()); |
233 } | 233 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 } | 275 } |
276 } | 276 } |
277 } | 277 } |
278 | 278 |
279 void RejectedPromises::revokeNow(PassOwnPtr<Message> message) | 279 void RejectedPromises::revokeNow(PassOwnPtr<Message> message) |
280 { | 280 { |
281 message->revoke(); | 281 message->revoke(); |
282 } | 282 } |
283 | 283 |
284 } // namespace blink | 284 } // namespace blink |
OLD | NEW |