static void messageHandlerInMainThread(v8::Local<v8::Message> message, v8::Local<v8::Value> data) { ASSERT(isMainThread()); v8::Isolate* isolate = v8::Isolate::GetCurrent(); if (isolate->GetEnteredContext().IsEmpty()) return; // If called during context initialization, there will be no entered context. ScriptState* scriptState = ScriptState::current(isolate); if (!scriptState->contextIsValid()) return; ExecutionContext* context = scriptState->getExecutionContext(); std::unique_ptr<SourceLocation> location = SourceLocation::fromMessage(isolate, message, context); AccessControlStatus accessControlStatus = NotSharableCrossOrigin; if (message->IsOpaque()) accessControlStatus = OpaqueResource; else if (message->IsSharedCrossOrigin()) accessControlStatus = SharableCrossOrigin; ErrorEvent* event = ErrorEvent::create(toCoreStringWithNullCheck(message->Get()), std::move(location), &scriptState->world()); String messageForConsole = extractMessageForConsole(isolate, data); if (!messageForConsole.isEmpty()) event->setUnsanitizedMessage("Uncaught " + messageForConsole); // This method might be called while we're creating a new context. In this // case, we avoid storing the exception object, as we can't create a wrapper // during context creation. // FIXME: Can we even get here during initialization now that we bail out when // GetEntered returns an empty handle? if (context->isDocument()) { LocalFrame* frame = toDocument(context)->frame(); if (frame && frame->script().existingWindowProxy(scriptState->world())) { V8ErrorHandler::storeExceptionOnErrorEventWrapper( scriptState, event, data, scriptState->context()->Global()); } } if (scriptState->world().isPrivateScriptIsolatedWorld()) { // We allow a private script to dispatch error events even in a // EventDispatchForbiddenScope scope. Without having this ability, it's // hard to debug the private script because syntax errors in the private // script are not reported to console (the private script just crashes // silently). Allowing error events in private scripts is safe because // error events don't propagate to other isolated worlds (which means that // the error events won't fire any event listeners in user's scripts). EventDispatchForbiddenScope::AllowUserAgentEvents allowUserAgentEvents; context->dispatchErrorEvent(event, accessControlStatus); } else { context->dispatchErrorEvent(event, accessControlStatus); } }
static void promiseRejectHandlerInMainThread(v8::PromiseRejectMessage data) { ASSERT(isMainThread()); if (data.GetEvent() == v8::kPromiseHandlerAddedAfterReject) { rejectedPromisesOnMainThread().handlerAdded(data); return; } ASSERT(data.GetEvent() == v8::kPromiseRejectWithNoHandler); v8::Local<v8::Promise> promise = data.GetPromise(); v8::Isolate* isolate = promise->GetIsolate(); // There is no entered window during microtask callbacks from V8, // thus we call toDOMWindow() instead of enteredDOMWindow(). LocalDOMWindow* window = currentDOMWindow(isolate); if (!window || !window->isCurrentlyDisplayedInFrame()) return; v8::Local<v8::Value> exception = data.GetValue(); if (V8DOMWrapper::isWrapper(isolate, exception)) { // Try to get the stack & location from a wrapped exception object (e.g. DOMException). ASSERT(exception->IsObject()); v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(exception); v8::Local<v8::Value> error = V8HiddenValue::getHiddenValue(isolate, obj, V8HiddenValue::error(isolate)); if (!error.IsEmpty()) exception = error; } int scriptId = 0; int lineNumber = 0; int columnNumber = 0; String resourceName; String errorMessage; RefPtrWillBeRawPtr<ScriptCallStack> callStack = nullptr; v8::Local<v8::Message> message = v8::Exception::CreateMessage(exception); if (!message.IsEmpty()) { if (v8Call(message->GetLineNumber(isolate->GetCurrentContext()), lineNumber) && v8Call(message->GetStartColumn(isolate->GetCurrentContext()), columnNumber)) ++columnNumber; resourceName = extractResourceName(message, window->document()); errorMessage = toCoreStringWithNullCheck(message->Get()); callStack = extractCallStack(isolate, message, &scriptId); } else if (!exception.IsEmpty() && exception->IsInt32()) { // For Smi's the message would be empty. errorMessage = "Uncaught " + String::number(exception.As<v8::Integer>()->Value()); } String messageForConsole = extractMessageForConsole(isolate, data.GetValue()); if (!messageForConsole.isEmpty()) errorMessage = "Uncaught " + messageForConsole; ScriptState* scriptState = ScriptState::current(isolate); rejectedPromisesOnMainThread().rejectedWithNoHandler(scriptState, data, errorMessage, resourceName, scriptId, lineNumber, columnNumber, callStack); }
static void promiseRejectHandler(v8::PromiseRejectMessage data, RejectedPromises& rejectedPromises, const String& fallbackResourceName) { if (data.GetEvent() == v8::kPromiseHandlerAddedAfterReject) { rejectedPromises.handlerAdded(data); return; } ASSERT(data.GetEvent() == v8::kPromiseRejectWithNoHandler); v8::Local<v8::Promise> promise = data.GetPromise(); v8::Isolate* isolate = promise->GetIsolate(); ScriptState* scriptState = ScriptState::current(isolate); v8::Local<v8::Value> exception = data.GetValue(); if (V8DOMWrapper::isWrapper(isolate, exception)) { // Try to get the stack & location from a wrapped exception object (e.g. DOMException). ASSERT(exception->IsObject()); v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(exception); v8::Local<v8::Value> error = V8HiddenValue::getHiddenValue(scriptState, obj, V8HiddenValue::error(isolate)); if (!error.IsEmpty()) exception = error; } int scriptId = 0; int lineNumber = 0; int columnNumber = 0; String resourceName = fallbackResourceName; String errorMessage; AccessControlStatus corsStatus = NotSharableCrossOrigin; RefPtrWillBeRawPtr<ScriptCallStack> callStack = nullptr; v8::Local<v8::Message> message = v8::Exception::CreateMessage(isolate, exception); if (!message.IsEmpty()) { V8StringResource<> v8ResourceName(message->GetScriptOrigin().ResourceName()); if (v8ResourceName.prepare()) resourceName = v8ResourceName; scriptId = message->GetScriptOrigin().ScriptID()->Value(); if (v8Call(message->GetLineNumber(scriptState->context()), lineNumber) && v8Call(message->GetStartColumn(scriptState->context()), columnNumber)) ++columnNumber; // message->Get() can be empty here. https://crbug.com/450330 errorMessage = toCoreStringWithNullCheck(message->Get()); callStack = extractCallStack(isolate, message, &scriptId); if (message->IsSharedCrossOrigin()) corsStatus = SharableCrossOrigin; } String messageForConsole = extractMessageForConsole(isolate, data.GetValue()); if (!messageForConsole.isEmpty()) errorMessage = "Uncaught " + messageForConsole; rejectedPromises.rejectedWithNoHandler(scriptState, data, errorMessage, resourceName, scriptId, lineNumber, columnNumber, callStack, corsStatus); }
static void messageHandlerInMainThread(v8::Local<v8::Message> message, v8::Local<v8::Value> data) { ASSERT(isMainThread()); v8::Isolate* isolate = v8::Isolate::GetCurrent(); // If called during context initialization, there will be no entered window. LocalDOMWindow* enteredWindow = enteredDOMWindow(isolate); if (!enteredWindow || !enteredWindow->isCurrentlyDisplayedInFrame()) return; int scriptId = 0; RefPtrWillBeRawPtr<ScriptCallStack> callStack = extractCallStack(isolate, message, &scriptId); String resourceName = extractResourceName(message, enteredWindow->document()); AccessControlStatus accessControlStatus = NotSharableCrossOrigin; if (message->IsOpaque()) accessControlStatus = OpaqueResource; else if (message->IsSharedCrossOrigin()) accessControlStatus = SharableCrossOrigin; ScriptState* scriptState = ScriptState::current(isolate); String errorMessage = toCoreStringWithNullCheck(message->Get()); int lineNumber = 0; int columnNumber = 0; if (v8Call(message->GetLineNumber(scriptState->context()), lineNumber) && v8Call(message->GetStartColumn(scriptState->context()), columnNumber)) ++columnNumber; RefPtrWillBeRawPtr<ErrorEvent> event = ErrorEvent::create(errorMessage, resourceName, lineNumber, columnNumber, &scriptState->world()); String messageForConsole = extractMessageForConsole(isolate, data); if (!messageForConsole.isEmpty()) event->setUnsanitizedMessage("Uncaught " + messageForConsole); // This method might be called while we're creating a new context. In this case, we // avoid storing the exception object, as we can't create a wrapper during context creation. // FIXME: Can we even get here during initialization now that we bail out when GetEntered returns an empty handle? LocalFrame* frame = enteredWindow->document()->frame(); if (frame && frame->script().existingWindowProxy(scriptState->world())) { V8ErrorHandler::storeExceptionOnErrorEventWrapper(isolate, event.get(), data, scriptState->context()->Global()); } if (scriptState->world().isPrivateScriptIsolatedWorld()) { // We allow a private script to dispatch error events even in a EventDispatchForbiddenScope scope. // Without having this ability, it's hard to debug the private script because syntax errors // in the private script are not reported to console (the private script just crashes silently). // Allowing error events in private scripts is safe because error events don't propagate to // other isolated worlds (which means that the error events won't fire any event listeners // in user's scripts). EventDispatchForbiddenScope::AllowUserAgentEvents allowUserAgentEvents; enteredWindow->document()->reportException(event.release(), scriptId, callStack, accessControlStatus); } else { enteredWindow->document()->reportException(event.release(), scriptId, callStack, accessControlStatus); } }
static void promiseRejectHandler(v8::PromiseRejectMessage data, RejectedPromises& rejectedPromises, ScriptState* scriptState) { if (data.GetEvent() == v8::kPromiseHandlerAddedAfterReject) { rejectedPromises.handlerAdded(data); return; } ASSERT(data.GetEvent() == v8::kPromiseRejectWithNoHandler); v8::Local<v8::Promise> promise = data.GetPromise(); v8::Isolate* isolate = promise->GetIsolate(); ExecutionContext* context = scriptState->getExecutionContext(); v8::Local<v8::Value> exception = data.GetValue(); if (V8DOMWrapper::isWrapper(isolate, exception)) { // Try to get the stack & location from a wrapped exception object (e.g. // DOMException). ASSERT(exception->IsObject()); auto privateError = V8PrivateProperty::getDOMExceptionError(isolate); v8::Local<v8::Value> error = privateError.getOrUndefined( scriptState->context(), exception.As<v8::Object>()); if (!error->IsUndefined()) exception = error; } String errorMessage; AccessControlStatus corsStatus = NotSharableCrossOrigin; std::unique_ptr<SourceLocation> location; v8::Local<v8::Message> message = v8::Exception::CreateMessage(isolate, exception); if (!message.IsEmpty()) { // message->Get() can be empty here. https://crbug.com/450330 errorMessage = toCoreStringWithNullCheck(message->Get()); location = SourceLocation::fromMessage(isolate, message, context); if (message->IsSharedCrossOrigin()) corsStatus = SharableCrossOrigin; } else { location = SourceLocation::create(context->url().getString(), 0, 0, nullptr); } String messageForConsole = extractMessageForConsole(isolate, data.GetValue()); if (!messageForConsole.isEmpty()) errorMessage = "Uncaught " + messageForConsole; rejectedPromises.rejectedWithNoHandler(scriptState, data, errorMessage, std::move(location), corsStatus); }