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

Unified Diff: Source/bindings/core/v8/RejectedPromises.cpp

Issue 1179113007: Implement onunhandledrejection / onrejectionhandled events (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: updates Created 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « LayoutTests/webexposed/global-interface-listing-shared-worker-expected.txt ('k') | Source/core/core.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/bindings/core/v8/RejectedPromises.cpp
diff --git a/Source/bindings/core/v8/RejectedPromises.cpp b/Source/bindings/core/v8/RejectedPromises.cpp
index ee1a2f158e0e89131feb6b9e14d26b7a3a04c00a..b7bfe26fc29ffd18aa896a6cb73594dde2c620c0 100644
--- a/Source/bindings/core/v8/RejectedPromises.cpp
+++ b/Source/bindings/core/v8/RejectedPromises.cpp
@@ -10,8 +10,11 @@
#include "bindings/core/v8/ScriptValue.h"
#include "bindings/core/v8/V8Binding.h"
#include "core/dom/ExecutionContext.h"
+#include "core/events/EventTarget.h"
+#include "core/events/PromiseRejectionEvent.h"
#include "core/inspector/ConsoleMessage.h"
#include "core/inspector/ScriptArguments.h"
+#include "platform/RuntimeEnabledFeatures.h"
namespace blink {
@@ -58,27 +61,39 @@ public:
return;
ASSERT(!v8::Local<v8::Promise>::Cast(value)->HasHandler());
- const String errorMessage = "Uncaught (in promise)";
- Vector<ScriptValue> args;
- args.append(ScriptValue(m_scriptState, v8String(m_scriptState->isolate(), errorMessage)));
- args.append(m_exception);
- RefPtrWillBeRawPtr<ScriptArguments> arguments = ScriptArguments::create(m_scriptState, args);
-
- String embedderErrorMessage = m_errorMessage;
- if (embedderErrorMessage.isEmpty())
- embedderErrorMessage = errorMessage;
- else if (embedderErrorMessage.startsWith("Uncaught "))
- embedderErrorMessage.insert(" (in promise)", 8);
-
- RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(JSMessageSource, ErrorMessageLevel, embedderErrorMessage, m_resourceName, m_lineNumber, m_columnNumber);
- consoleMessage->setScriptArguments(arguments);
- consoleMessage->setCallStack(m_callStack);
- consoleMessage->setScriptId(m_scriptId);
- m_consoleMessageId = consoleMessage->assignMessageId();
- executionContext->addConsoleMessage(consoleMessage.release());
+ EventTarget* target = executionContext->errorEventTarget();
+ if (RuntimeEnabledFeatures::promiseRejectionEventEnabled() && target) {
+ PromiseRejectionEventInit init;
+ init.setPromise(ScriptPromise(m_scriptState, value));
+ init.setReason(m_exception);
+ init.setCancelable(true);
+ RefPtrWillBeRawPtr<PromiseRejectionEvent> event = PromiseRejectionEvent::create(EventTypeNames::unhandledrejection, init);
+ // Log to console if event was not preventDefault()'ed.
+ m_shouldLogToConsole = target->dispatchEvent(event);
+ }
+
+ if (m_shouldLogToConsole) {
+ const String errorMessage = "Uncaught (in promise)";
+ Vector<ScriptValue> args;
+ args.append(ScriptValue(m_scriptState, v8String(m_scriptState->isolate(), errorMessage)));
+ args.append(m_exception);
+ RefPtrWillBeRawPtr<ScriptArguments> arguments = ScriptArguments::create(m_scriptState, args);
+
+ String embedderErrorMessage = m_errorMessage;
+ if (embedderErrorMessage.isEmpty())
+ embedderErrorMessage = errorMessage;
+ else if (embedderErrorMessage.startsWith("Uncaught "))
+ embedderErrorMessage.insert(" (in promise)", 8);
+
+ RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(JSMessageSource, ErrorMessageLevel, embedderErrorMessage, m_resourceName, m_lineNumber, m_columnNumber);
+ consoleMessage->setScriptArguments(arguments);
+ consoleMessage->setCallStack(m_callStack);
+ consoleMessage->setScriptId(m_scriptId);
+ m_consoleMessageId = consoleMessage->assignMessageId();
+ executionContext->addConsoleMessage(consoleMessage.release());
+ }
m_callStack.clear();
- m_exception.clear();
}
void revoke()
@@ -87,9 +102,26 @@ public:
if (!executionContext)
return;
- RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(JSMessageSource, RevokedErrorMessageLevel, "Handler added to rejected promise");
- consoleMessage->setRelatedMessageId(m_consoleMessageId);
- executionContext->addConsoleMessage(consoleMessage.release());
+ ScriptState::Scope scope(m_scriptState);
+ v8::Local<v8::Value> value = m_promise.newLocal(m_scriptState->isolate());
+ // Either collected or https://crbug.com/450330
+ if (value.IsEmpty() || !value->IsPromise())
+ return;
+
+ EventTarget* target = executionContext->errorEventTarget();
+ if (RuntimeEnabledFeatures::promiseRejectionEventEnabled() && target) {
+ PromiseRejectionEventInit init;
+ init.setPromise(ScriptPromise(m_scriptState, value));
+ init.setReason(m_exception);
+ RefPtrWillBeRawPtr<PromiseRejectionEvent> event = PromiseRejectionEvent::create(EventTypeNames::rejectionhandled, init);
+ m_shouldLogToConsole &= target->dispatchEvent(event);
+ }
+
+ if (m_shouldLogToConsole) {
+ RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(JSMessageSource, RevokedErrorMessageLevel, "Handler added to rejected promise");
+ consoleMessage->setRelatedMessageId(m_consoleMessageId);
+ executionContext->addConsoleMessage(consoleMessage.release());
+ }
}
private:
@@ -105,6 +137,7 @@ private:
, m_callStack(callStack)
, m_consoleMessageId(0)
, m_collected(false)
+ , m_shouldLogToConsole(true)
{
m_promise.setWeak(this, &Message::didCollectPromise);
}
@@ -126,6 +159,7 @@ private:
RefPtrWillBeMember<ScriptCallStack> m_callStack;
unsigned m_consoleMessageId;
bool m_collected;
+ bool m_shouldLogToConsole;
};
RejectedPromises::RejectedPromises()
« no previous file with comments | « LayoutTests/webexposed/global-interface-listing-shared-worker-expected.txt ('k') | Source/core/core.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698