| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #include "sky/engine/config.h" | |
| 27 #include "sky/engine/bindings/core/v8/V8Initializer.h" | |
| 28 | |
| 29 #include "bindings/core/v8/V8DOMException.h" | |
| 30 #include "bindings/core/v8/V8ErrorEvent.h" | |
| 31 #include "bindings/core/v8/V8Location.h" | |
| 32 #include "bindings/core/v8/V8Window.h" | |
| 33 #include "sky/engine/bindings/core/v8/DOMWrapperWorld.h" | |
| 34 #include "sky/engine/bindings/core/v8/ScriptCallStackFactory.h" | |
| 35 #include "sky/engine/bindings/core/v8/ScriptController.h" | |
| 36 #include "sky/engine/bindings/core/v8/ScriptProfiler.h" | |
| 37 #include "sky/engine/bindings/core/v8/V8Binding.h" | |
| 38 #include "sky/engine/bindings/core/v8/V8ErrorHandler.h" | |
| 39 #include "sky/engine/bindings/core/v8/V8GCController.h" | |
| 40 #include "sky/engine/bindings/core/v8/V8PerContextData.h" | |
| 41 #include "sky/engine/core/dom/Document.h" | |
| 42 #include "sky/engine/core/dom/ExceptionCode.h" | |
| 43 #include "sky/engine/core/frame/ConsoleTypes.h" | |
| 44 #include "sky/engine/core/frame/LocalDOMWindow.h" | |
| 45 #include "sky/engine/core/frame/LocalFrame.h" | |
| 46 #include "sky/engine/platform/EventDispatchForbiddenScope.h" | |
| 47 #include "sky/engine/platform/TraceEvent.h" | |
| 48 #include "sky/engine/public/platform/Platform.h" | |
| 49 #include "sky/engine/core/inspector/ScriptCallStack.h" | |
| 50 #include "sky/engine/wtf/RefPtr.h" | |
| 51 #include "sky/engine/wtf/text/WTFString.h" | |
| 52 #include "v8/include/v8-debug.h" | |
| 53 | |
| 54 namespace blink { | |
| 55 | |
| 56 static LocalFrame* findFrame(v8::Local<v8::Object> host, v8::Local<v8::Value> da
ta, v8::Isolate* isolate) | |
| 57 { | |
| 58 const WrapperTypeInfo* type = WrapperTypeInfo::unwrap(data); | |
| 59 | |
| 60 if (V8Window::wrapperTypeInfo.equals(type)) { | |
| 61 v8::Handle<v8::Object> windowWrapper = V8Window::findInstanceInPrototype
Chain(host, isolate); | |
| 62 if (windowWrapper.IsEmpty()) | |
| 63 return 0; | |
| 64 return V8Window::toNative(windowWrapper)->frame(); | |
| 65 } | |
| 66 | |
| 67 if (V8Location::wrapperTypeInfo.equals(type)) | |
| 68 return V8Location::toNative(host)->frame(); | |
| 69 | |
| 70 // This function can handle only those types listed above. | |
| 71 ASSERT_NOT_REACHED(); | |
| 72 return 0; | |
| 73 } | |
| 74 | |
| 75 static void reportFatalErrorInMainThread(const char* location, const char* messa
ge) | |
| 76 { | |
| 77 int memoryUsageMB = blink::Platform::current()->actualMemoryUsageMB(); | |
| 78 printf("V8 error: %s (%s). Current memory usage: %d MB\n", message, locatio
n, memoryUsageMB); | |
| 79 CRASH(); | |
| 80 } | |
| 81 | |
| 82 static LocalFrame* retrieveFrameWithGlobalObjectCheck(v8::Handle<v8::Context> co
ntext) | |
| 83 { | |
| 84 if (context.IsEmpty()) | |
| 85 return 0; | |
| 86 | |
| 87 // FIXME: This is a temporary hack for crbug.com/345014. | |
| 88 // Currently it's possible that V8 can trigger Debugger::ProcessDebugEvent f
or a context | |
| 89 // that is being initialized (i.e., inside Context::New() of the context). | |
| 90 // We should fix the V8 side so that it won't trigger the event for a half-b
aked context | |
| 91 // because there is no way in the embedder side to check if the context is h
alf-baked or not. | |
| 92 if (isMainThread() && DOMWrapperWorld::windowIsBeingInitialized()) | |
| 93 return 0; | |
| 94 | |
| 95 v8::Handle<v8::Value> global = V8Window::findInstanceInPrototypeChain(contex
t->Global(), context->GetIsolate()); | |
| 96 if (global.IsEmpty()) | |
| 97 return 0; | |
| 98 | |
| 99 return toFrameIfNotDetached(context); | |
| 100 } | |
| 101 | |
| 102 static void messageHandlerInMainThread(v8::Handle<v8::Message> message, v8::Hand
le<v8::Value> data) | |
| 103 { | |
| 104 ASSERT(isMainThread()); | |
| 105 // It's possible that messageHandlerInMainThread() is invoked while we're in
itializing a window. | |
| 106 // In that half-baked situation, we don't have a valid context nor a valid w
orld, | |
| 107 // so just return immediately. | |
| 108 if (DOMWrapperWorld::windowIsBeingInitialized()) | |
| 109 return; | |
| 110 | |
| 111 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 112 // Check if we're in the V8DebugContext which does not have a window object. | |
| 113 if (!retrieveFrameWithGlobalObjectCheck(isolate->GetCurrentContext())) { | |
| 114 printf("Unhandled: %s %s\n", | |
| 115 toCoreString(message->Get()).ascii().data(), | |
| 116 toCoreString(message->GetSourceLine()).ascii().data()); | |
| 117 return; | |
| 118 } | |
| 119 // If called during context initialization, there will be no entered window. | |
| 120 LocalDOMWindow* enteredWindow = enteredDOMWindow(isolate); | |
| 121 if (!enteredWindow) | |
| 122 return; | |
| 123 | |
| 124 String errorMessage = toCoreString(message->Get()); | |
| 125 | |
| 126 v8::Handle<v8::StackTrace> stackTrace = message->GetStackTrace(); | |
| 127 RefPtr<ScriptCallStack> callStack = nullptr; | |
| 128 int scriptId = message->GetScriptOrigin().ScriptID()->Value(); | |
| 129 // Currently stack trace is only collected when inspector is open. | |
| 130 if (!stackTrace.IsEmpty() && stackTrace->GetFrameCount() > 0) { | |
| 131 callStack = createScriptCallStack(stackTrace, ScriptCallStack::maxCallSt
ackSizeToCapture, isolate); | |
| 132 bool success = false; | |
| 133 int topScriptId = callStack->at(0).scriptId().toInt(&success); | |
| 134 if (success && topScriptId == scriptId) | |
| 135 scriptId = 0; | |
| 136 } else { | |
| 137 Vector<ScriptCallFrame> callFrames; | |
| 138 callStack = ScriptCallStack::create(callFrames); | |
| 139 } | |
| 140 | |
| 141 v8::Handle<v8::Value> resourceName = message->GetScriptOrigin().ResourceName
(); | |
| 142 bool shouldUseDocumentURL = resourceName.IsEmpty() || !resourceName->IsStrin
g(); | |
| 143 String resource = shouldUseDocumentURL ? enteredWindow->document()->url() :
toCoreString(resourceName.As<v8::String>()); | |
| 144 | |
| 145 ScriptState* scriptState = ScriptState::current(isolate); | |
| 146 RefPtr<ErrorEvent> event = ErrorEvent::create(errorMessage, resource, messag
e->GetLineNumber(), message->GetStartColumn() + 1, &scriptState->world()); | |
| 147 if (V8DOMWrapper::isDOMWrapper(data)) { | |
| 148 v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(data); | |
| 149 const WrapperTypeInfo* type = toWrapperTypeInfo(obj); | |
| 150 if (V8DOMException::wrapperTypeInfo.isSubclass(type)) { | |
| 151 DOMException* exception = V8DOMException::toNative(obj); | |
| 152 if (exception && !exception->messageForConsole().isEmpty()) | |
| 153 event->setUnsanitizedMessage("Uncaught " + exception->toStringFo
rConsole()); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 // This method might be called while we're creating a new context. In this c
ase, we | |
| 158 // avoid storing the exception object, as we can't create a wrapper during c
ontext creation. | |
| 159 // FIXME: Can we even get here during initialization now that we bail out wh
en GetEntered returns an empty handle? | |
| 160 LocalFrame* frame = enteredWindow->document()->frame(); | |
| 161 if (frame && frame->script().existingWindowProxy(scriptState->world())) { | |
| 162 V8ErrorHandler::storeExceptionOnErrorEventWrapper(event.get(), data, scr
iptState->context()->Global(), isolate); | |
| 163 } | |
| 164 | |
| 165 enteredWindow->document()->reportException(event.release(), scriptId, callSt
ack); | |
| 166 } | |
| 167 | |
| 168 static void failedAccessCheckCallbackInMainThread(v8::Local<v8::Object> host, v8
::AccessType type, v8::Local<v8::Value> data) | |
| 169 { | |
| 170 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 171 LocalFrame* target = findFrame(host, data, isolate); | |
| 172 if (!target) | |
| 173 return; | |
| 174 | |
| 175 // FIXME: We should modify V8 to pass in more contextual information (contex
t, property, and object). | |
| 176 ExceptionState exceptionState(ExceptionState::UnknownContext, 0, 0, isolate-
>GetCurrentContext()->Global(), isolate); | |
| 177 exceptionState.throwSecurityError("failedAccessCheckCallbackInMainThread", "
failedAccessCheckCallbackInMainThread"); | |
| 178 exceptionState.throwIfNeeded(); | |
| 179 } | |
| 180 | |
| 181 static void timerTraceProfilerInMainThread(const char* name, int status) | |
| 182 { | |
| 183 if (!status) { | |
| 184 TRACE_EVENT_BEGIN0("v8", name); | |
| 185 } else { | |
| 186 TRACE_EVENT_END0("v8", name); | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 void V8Initializer::initializeMainThreadIfNeeded() | |
| 191 { | |
| 192 ASSERT(isMainThread()); | |
| 193 | |
| 194 static bool initialized = false; | |
| 195 if (initialized) | |
| 196 return; | |
| 197 initialized = true; | |
| 198 | |
| 199 static const char v8Flags[] = "--harmony-classes --harmony-arrays --harmony-
array-includes --harmony-regexps"; | |
| 200 v8::V8::SetFlagsFromString(v8Flags, sizeof(v8Flags) - 1); | |
| 201 | |
| 202 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, v8ArrayBuffe
rAllocator()); | |
| 203 | |
| 204 v8::Isolate* isolate = V8PerIsolateData::initialize(); | |
| 205 | |
| 206 v8::V8::AddGCPrologueCallback(V8GCController::gcPrologue); | |
| 207 v8::V8::AddGCEpilogueCallback(V8GCController::gcEpilogue); | |
| 208 | |
| 209 v8::Debug::SetLiveEditEnabled(isolate, false); | |
| 210 | |
| 211 isolate->SetAutorunMicrotasks(false); | |
| 212 | |
| 213 v8::V8::SetFatalErrorHandler(reportFatalErrorInMainThread); | |
| 214 v8::V8::AddMessageListener(messageHandlerInMainThread); | |
| 215 v8::V8::SetFailedAccessCheckCallbackFunction(failedAccessCheckCallbackInMain
Thread); | |
| 216 | |
| 217 isolate->SetEventLogger(timerTraceProfilerInMainThread); | |
| 218 | |
| 219 ScriptProfiler::initialize(); | |
| 220 } | |
| 221 | |
| 222 } // namespace blink | |
| OLD | NEW |