This source file includes following definitions.
- findFrame
- reportFatalErrorInMainThread
- messageHandlerInMainThread
- failedAccessCheckCallbackInMainThread
- codeGenerationCheckCallbackInMainThread
- timerTraceProfilerInMainThread
- initializeV8Common
- initializeMainThreadIfNeeded
- reportFatalErrorInWorker
- messageHandlerInWorker
- initializeWorker
#include "config.h"
#include "bindings/v8/V8Initializer.h"
#include "V8DOMException.h"
#include "V8ErrorEvent.h"
#include "V8History.h"
#include "V8Location.h"
#include "V8Window.h"
#include "bindings/v8/DOMWrapperWorld.h"
#include "bindings/v8/ScriptCallStackFactory.h"
#include "bindings/v8/ScriptController.h"
#include "bindings/v8/ScriptProfiler.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8ErrorHandler.h"
#include "bindings/v8/V8GCController.h"
#include "bindings/v8/V8PerContextData.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "core/frame/ConsoleTypes.h"
#include "core/frame/DOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/csp/ContentSecurityPolicy.h"
#include "core/inspector/ScriptCallStack.h"
#include "platform/TraceEvent.h"
#include "public/platform/Platform.h"
#include "wtf/RefPtr.h"
#include "wtf/text/WTFString.h"
#include <v8-debug.h>
namespace WebCore {
static LocalFrame* findFrame(v8::Local<v8::Object> host, v8::Local<v8::Value> data, v8::Isolate* isolate)
{
const WrapperTypeInfo* type = WrapperTypeInfo::unwrap(data);
if (V8Window::wrapperTypeInfo.equals(type)) {
v8::Handle<v8::Object> windowWrapper = V8Window::findInstanceInPrototypeChain(host, isolate);
if (windowWrapper.IsEmpty())
return 0;
return V8Window::toNative(windowWrapper)->frame();
}
if (V8History::wrapperTypeInfo.equals(type))
return V8History::toNative(host)->frame();
if (V8Location::wrapperTypeInfo.equals(type))
return V8Location::toNative(host)->frame();
ASSERT_NOT_REACHED();
return 0;
}
static void reportFatalErrorInMainThread(const char* location, const char* message)
{
int memoryUsageMB = blink::Platform::current()->actualMemoryUsageMB();
printf("V8 error: %s (%s). Current memory usage: %d MB\n", message, location, memoryUsageMB);
CRASH();
}
static void messageHandlerInMainThread(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data)
{
ASSERT(isMainThread());
if (DOMWrapperWorld::windowIsBeingInitialized())
return;
v8::Isolate* isolate = v8::Isolate::GetCurrent();
DOMWindow* enteredWindow = enteredDOMWindow(isolate);
if (!enteredWindow || !enteredWindow->isCurrentlyDisplayedInFrame())
return;
String errorMessage = toCoreString(message->Get());
v8::Handle<v8::StackTrace> stackTrace = message->GetStackTrace();
RefPtr<ScriptCallStack> callStack;
if (!stackTrace.IsEmpty() && stackTrace->GetFrameCount() > 0)
callStack = createScriptCallStack(stackTrace, ScriptCallStack::maxCallStackSizeToCapture, isolate);
v8::Handle<v8::Value> resourceName = message->GetScriptResourceName();
bool shouldUseDocumentURL = resourceName.IsEmpty() || !resourceName->IsString();
String resource = shouldUseDocumentURL ? enteredWindow->document()->url() : toCoreString(resourceName.As<v8::String>());
AccessControlStatus corsStatus = message->IsSharedCrossOrigin() ? SharableCrossOrigin : NotSharableCrossOrigin;
DOMWrapperWorld& world = DOMWrapperWorld::current(isolate);
RefPtrWillBeRawPtr<ErrorEvent> event = ErrorEvent::create(errorMessage, resource, message->GetLineNumber(), message->GetStartColumn() + 1, &world);
if (V8DOMWrapper::isDOMWrapper(data)) {
v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(data);
const WrapperTypeInfo* type = toWrapperTypeInfo(obj);
if (V8DOMException::wrapperTypeInfo.isSubclass(type)) {
DOMException* exception = V8DOMException::toNative(obj);
if (exception && !exception->messageForConsole().isEmpty())
event->setUnsanitizedMessage("Uncaught " + exception->toStringForConsole());
}
}
LocalFrame* frame = enteredWindow->document()->frame();
if (frame && frame->script().existingWindowShell(world))
V8ErrorHandler::storeExceptionOnErrorEventWrapper(event.get(), data, v8::Isolate::GetCurrent());
enteredWindow->document()->reportException(event.release(), callStack, corsStatus);
}
static void failedAccessCheckCallbackInMainThread(v8::Local<v8::Object> host, v8::AccessType type, v8::Local<v8::Value> data)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
LocalFrame* target = findFrame(host, data, isolate);
if (!target)
return;
DOMWindow* targetWindow = target->domWindow();
ExceptionState exceptionState(ExceptionState::UnknownContext, 0, 0, v8::Handle<v8::Object>(), isolate);
exceptionState.throwSecurityError(targetWindow->sanitizedCrossDomainAccessErrorMessage(callingDOMWindow(isolate)), targetWindow->crossDomainAccessErrorMessage(callingDOMWindow(isolate)));
exceptionState.throwIfNeeded();
}
static bool codeGenerationCheckCallbackInMainThread(v8::Local<v8::Context> context)
{
if (ExecutionContext* executionContext = toExecutionContext(context)) {
if (ContentSecurityPolicy* policy = toDocument(executionContext)->contentSecurityPolicy())
return policy->allowEval(ScriptState::forContext(context));
}
return false;
}
static void timerTraceProfilerInMainThread(const char* name, int status)
{
if (!status) {
TRACE_EVENT_BEGIN0("V8", name);
} else {
TRACE_EVENT_END0("V8", name);
}
}
static void initializeV8Common(v8::Isolate* isolate)
{
v8::ResourceConstraints constraints;
constraints.ConfigureDefaults(static_cast<uint64_t>(blink::Platform::current()->physicalMemoryMB()) << 20, static_cast<uint32_t>(blink::Platform::current()->numberOfProcessors()));
v8::SetResourceConstraints(isolate, &constraints);
v8::V8::AddGCPrologueCallback(V8GCController::gcPrologue);
v8::V8::AddGCEpilogueCallback(V8GCController::gcEpilogue);
v8::Debug::SetLiveEditEnabled(false);
}
void V8Initializer::initializeMainThreadIfNeeded(v8::Isolate* isolate)
{
ASSERT(isMainThread());
static bool initialized = false;
if (initialized)
return;
initialized = true;
initializeV8Common(isolate);
v8::V8::SetFatalErrorHandler(reportFatalErrorInMainThread);
V8PerIsolateData::ensureInitialized(isolate);
v8::V8::AddMessageListener(messageHandlerInMainThread);
v8::V8::SetFailedAccessCheckCallbackFunction(failedAccessCheckCallbackInMainThread);
v8::V8::SetAllowCodeGenerationFromStringsCallback(codeGenerationCheckCallbackInMainThread);
isolate->SetEventLogger(timerTraceProfilerInMainThread);
ScriptProfiler::initialize();
}
static void reportFatalErrorInWorker(const char* location, const char* message)
{
CRASH();
}
static void messageHandlerInWorker(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data)
{
static bool isReportingException = false;
if (isReportingException)
return;
isReportingException = true;
v8::Isolate* isolate = v8::Isolate::GetCurrent();
if (ExecutionContext* context = currentExecutionContext(isolate)) {
String errorMessage = toCoreString(message->Get());
V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, sourceURL, message->GetScriptResourceName());
RefPtrWillBeRawPtr<ErrorEvent> event = ErrorEvent::create(errorMessage, sourceURL, message->GetLineNumber(), message->GetStartColumn() + 1, &DOMWrapperWorld::current(isolate));
AccessControlStatus corsStatus = message->IsSharedCrossOrigin() ? SharableCrossOrigin : NotSharableCrossOrigin;
V8ErrorHandler::storeExceptionOnErrorEventWrapper(event.get(), data, isolate);
context->reportException(event.release(), nullptr, corsStatus);
}
isReportingException = false;
}
static const int kWorkerMaxStackSize = 500 * 1024;
void V8Initializer::initializeWorker(v8::Isolate* isolate)
{
initializeV8Common(isolate);
v8::V8::AddMessageListener(messageHandlerInWorker);
v8::V8::SetFatalErrorHandler(reportFatalErrorInWorker);
v8::ResourceConstraints resourceConstraints;
uint32_t here;
resourceConstraints.set_stack_limit(&here - kWorkerMaxStackSize / sizeof(uint32_t*));
v8::SetResourceConstraints(isolate, &resourceConstraints);
}
}