Chromium Code Reviews| Index: third_party/WebKit/Source/bindings/core/v8/custom/V8ConsoleBaseCustom.cpp |
| diff --git a/third_party/WebKit/Source/bindings/core/v8/custom/V8ConsoleBaseCustom.cpp b/third_party/WebKit/Source/bindings/core/v8/custom/V8ConsoleBaseCustom.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..312489fe93fccbcc9c2af887ac1ee7dc17ce2ab6 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/bindings/core/v8/custom/V8ConsoleBaseCustom.cpp |
| @@ -0,0 +1,328 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "bindings/core/v8/V8ConsoleBase.h" |
| + |
| +#include "bindings/core/v8/ExceptionState.h" |
| +#include "bindings/core/v8/ScriptCallStack.h" |
| +#include "bindings/core/v8/ScriptState.h" |
| +#include "core/frame/Deprecation.h" |
| +#include "core/inspector/ConsoleMessage.h" |
| +#include "core/inspector/InspectorConsoleInstrumentation.h" |
| +#include "core/inspector/InspectorTraceEvents.h" |
| +#include "core/inspector/ScriptArguments.h" |
| + |
| +namespace blink { |
| + |
| +static String toString(v8::Local<v8::Value> value) |
| +{ |
| + if (value.IsEmpty() || !value->IsString()) |
| + return String(); |
| + return v8StringToWebCoreString<String>(value.As<v8::String>(), DoNotExternalize); |
| +} |
| + |
| +static void internalAddMessage(MessageType type, MessageLevel level, const v8::FunctionCallbackInfo<v8::Value>& info, bool allowEmptyArguments, int firstArg) |
| +{ |
| + if (!allowEmptyArguments && !info.Length()) |
| + return; |
| + ScriptState* scriptState = ScriptState::current(info.GetIsolate()); |
| + RawPtr<ScriptArguments> scriptArguments(ScriptArguments::create(scriptState, info, firstArg)); |
| + if (scriptState && !scriptState->contextIsValid()) |
| + scriptArguments.clear(); |
| + String message; |
| + if (scriptArguments) |
| + scriptArguments->getFirstArgumentAsString(message); |
| + |
| + ConsoleMessage* consoleMessage = ConsoleMessage::create(ConsoleAPIMessageSource, level, message); |
| + consoleMessage->setType(type); |
| + consoleMessage->setScriptState(scriptState); |
| + consoleMessage->setScriptArguments(scriptArguments.release()); |
| + consoleMessage->setCallStack(ScriptCallStack::captureForConsole()); |
| + |
| + ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); |
| + if (!console) |
| + return; |
| + console->reportMessageToConsole(consoleMessage); |
| +} |
| + |
| +static void debugFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(LogMessageType, DebugMessageLevel, info, false, 0); |
| +} |
| + |
| +void V8ConsoleBase::debugAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), debugFunction, info.Holder())); |
|
dgozman
2016/04/06 15:51:33
Is it fine to create new function for each call? W
kozy
2016/04/06 17:45:20
I've compared perfomance: https://docs.google.com/
|
| +} |
| + |
| +static void errorFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(LogMessageType, ErrorMessageLevel, info, false, 0); |
| +} |
| + |
| +void V8ConsoleBase::errorAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), errorFunction, info.Holder())); |
| +} |
| + |
| +static void infoFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(LogMessageType, InfoMessageLevel, info, false, 0); |
| +} |
| + |
| +void V8ConsoleBase::infoAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), infoFunction, info.Holder())); |
| +} |
| + |
| +static void logFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(LogMessageType, LogMessageLevel, info, false, 0); |
| +} |
| + |
| +void V8ConsoleBase::logAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), logFunction, info.Holder())); |
| +} |
| + |
| +static void warnFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(LogMessageType, WarningMessageLevel, info, false, 0); |
| +} |
| + |
| +void V8ConsoleBase::warnAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), warnFunction, info.Holder())); |
| +} |
| + |
| +static void dirFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(DirMessageType, LogMessageLevel, info, false, 0); |
| +} |
| + |
| +void V8ConsoleBase::dirAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), dirFunction, info.Holder())); |
| +} |
| + |
| +static void dirxmlFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(DirXMLMessageType, LogMessageLevel, info, false, 0); |
| +} |
| + |
| +void V8ConsoleBase::dirxmlAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), dirxmlFunction, info.Holder())); |
| +} |
| + |
| +static void tableFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(TableMessageType, LogMessageLevel, info, false, 0); |
| +} |
| + |
| +void V8ConsoleBase::tableAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), tableFunction, info.Holder())); |
| +} |
| + |
| +static void traceFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(TraceMessageType, LogMessageLevel, info, true, 0); |
| +} |
| + |
| +void V8ConsoleBase::traceAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), traceFunction, info.Holder())); |
| +} |
| + |
| +static void groupFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(StartGroupMessageType, LogMessageLevel, info, true, 0); |
| +} |
| + |
| +void V8ConsoleBase::groupAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), groupFunction, info.Holder())); |
| +} |
| + |
| +static void groupCollapsedFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(StartGroupCollapsedMessageType, LogMessageLevel, info, true, 0); |
| +} |
| + |
| +void V8ConsoleBase::groupCollapsedAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), groupCollapsedFunction, info.Holder())); |
| +} |
| + |
| +static void groupEndFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(EndGroupMessageType, LogMessageLevel, info, true, 0); |
| +} |
| + |
| +void V8ConsoleBase::groupEndAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), groupEndFunction, info.Holder())); |
| +} |
| + |
| +static void clearFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + internalAddMessage(ClearMessageType, LogMessageLevel, info, true, 0); |
| +} |
| + |
| +void V8ConsoleBase::clearAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), clearFunction, info.Holder())); |
| +} |
| + |
| +static void countFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); |
| + if (!console) |
| + return; |
| + ScriptState* scriptState = ScriptState::current(info.GetIsolate()); |
| + RawPtr<ScriptArguments> scriptArguments(ScriptArguments::create(scriptState, info, 0)); |
| + console->count(scriptState, scriptArguments.release()); |
| +} |
| + |
| +void V8ConsoleBase::countAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), countFunction, info.Holder())); |
| +} |
| + |
| +static void assertFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + ExceptionState exceptionState(ExceptionState::ExecutionContext, "assert", "ConsoleBase", info.Holder(), info.GetIsolate()); |
| + bool condition = false; |
| + if (info.Length() > 0) { |
| + condition = toBoolean(info.GetIsolate(), info[0], exceptionState); |
| + if (exceptionState.throwIfNeeded()) |
| + return; |
| + } |
| + if (condition) |
| + return; |
| + internalAddMessage(AssertMessageType, ErrorMessageLevel, info, true, 1); |
| +} |
| + |
| +void V8ConsoleBase::assertAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), assertFunction, info.Holder())); |
| +} |
| + |
| +static void markTimelineFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + Deprecation::countDeprecationIfNotPrivateScript(info.GetIsolate(), currentExecutionContext(info.GetIsolate()), UseCounter::ConsoleMarkTimeline); |
| + String title = info.Length() > 0 ? toString(info[0]) : String(); |
| + TRACE_EVENT_INSTANT1("devtools.timeline", "TimeStamp", TRACE_EVENT_SCOPE_THREAD, "data", InspectorTimeStampEvent::data(currentExecutionContext(info.GetIsolate()), title)); |
| +} |
| + |
| +void V8ConsoleBase::markTimelineAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), markTimelineFunction, info.Holder())); |
| +} |
| + |
| +static void profileFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); |
| + if (!console) |
| + return; |
| + String title = info.Length() > 0 ? toString(info[0]) : String(); |
| + InspectorInstrumentation::consoleProfile(currentExecutionContext(info.GetIsolate()), title); |
| +} |
| + |
| +void V8ConsoleBase::profileAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), profileFunction, info.Holder())); |
| +} |
| + |
| +static void profileEndFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); |
| + if (!console) |
| + return; |
| + String title = info.Length() > 0 ? toString(info[0]) : String(); |
| + InspectorInstrumentation::consoleProfileEnd(currentExecutionContext(info.GetIsolate()), title); |
| +} |
| + |
| +void V8ConsoleBase::profileEndAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), profileEndFunction, info.Holder())); |
| +} |
| + |
| +static String formatTimelineTitle(const String& title) |
| +{ |
| + return String::format("Timeline '%s'", title.utf8().data()); |
| +} |
| + |
| +static void timelineFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + Deprecation::countDeprecationIfNotPrivateScript(info.GetIsolate(), currentExecutionContext(info.GetIsolate()), UseCounter::ConsoleTimeline); |
| + ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); |
| + if (!console) |
| + return; |
| + String title = info.Length() > 0 ? toString(info[0]) : String(); |
| + TRACE_EVENT_COPY_ASYNC_BEGIN0("blink.console", formatTimelineTitle(title).utf8().data(), console); |
| +} |
| + |
| +void V8ConsoleBase::timelineAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), timelineFunction, info.Holder())); |
| +} |
| + |
| +static void timelineEndFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + Deprecation::countDeprecationIfNotPrivateScript(info.GetIsolate(), currentExecutionContext(info.GetIsolate()), UseCounter::ConsoleTimelineEnd); |
| + ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); |
| + if (!console) |
| + return; |
| + String title = info.Length() > 0 ? toString(info[0]) : String(); |
| + TRACE_EVENT_COPY_ASYNC_END0("blink.console", formatTimelineTitle(title).utf8().data(), console); |
| +} |
| + |
| +void V8ConsoleBase::timelineEndAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), timelineEndFunction, info.Holder())); |
| +} |
| + |
| +static void timeFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); |
| + if (!console) |
| + return; |
| + String title = info.Length() > 0 ? toString(info[0]) : String(); |
| + console->time(title); |
| +} |
| + |
| +void V8ConsoleBase::timeAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), timeFunction, info.Holder())); |
| +} |
| + |
| +static void timeEndFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + ConsoleBase* console = V8ConsoleBase::toImplWithTypeCheck(info.GetIsolate(), info.Data()); |
| + if (!console) |
| + return; |
| + String title = info.Length() > 0 ? toString(info[0]) : String(); |
| + ScriptState* scriptState = ScriptState::current(info.GetIsolate()); |
| + console->timeEnd(scriptState, title); |
| +} |
| + |
| +void V8ConsoleBase::timeEndAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), timeEndFunction, info.Holder())); |
| +} |
| + |
| +static void timeStampFunction(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + String title = info.Length() > 0 ? toString(info[0]) : String(); |
| + TRACE_EVENT_INSTANT1("devtools.timeline", "TimeStamp", TRACE_EVENT_SCOPE_THREAD, "data", InspectorTimeStampEvent::data(currentExecutionContext(info.GetIsolate()), title)); |
| +} |
| + |
| +void V8ConsoleBase::timeStampAttributeGetterCustom(const v8::FunctionCallbackInfo<v8::Value>& info) |
| +{ |
| + info.GetReturnValue().Set(v8::Function::New(info.GetIsolate(), timeStampFunction, info.Holder())); |
| +} |
| + |
| +} // namespace blink |