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

Unified Diff: third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp

Issue 1859293002: [DevTools] Move Console to v8_inspector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
Index: third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp
diff --git a/third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp b/third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..42435ef4b37c0af40290bab64f140fde3abc391f
--- /dev/null
+++ b/third_party/WebKit/Source/platform/v8_inspector/V8Console.cpp
@@ -0,0 +1,379 @@
+// 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 "platform/v8_inspector/V8Console.h"
+
+#include "platform/inspector_protocol/String16.h"
+#include "platform/v8_inspector/V8StackTraceImpl.h"
+#include "platform/v8_inspector/V8StringUtil.h"
+#include "platform/v8_inspector/public/ConsoleAPITypes.h"
+#include "platform/v8_inspector/public/ConsoleTypes.h"
+#include "platform/v8_inspector/public/V8DebuggerClient.h"
+
+namespace blink {
+
+namespace {
+
+v8::MaybeLocal<v8::Object> consoleObjectFromInfo(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ if (info.Data().IsEmpty() || !info.Data()->IsObject())
+ return v8::MaybeLocal<v8::Object>();
+ return info.Data().As<v8::Object>();
+}
+
+v8::Local<v8::Private> clientPrivateKey(v8::Isolate* isolate)
+{
+ return v8::Private::ForApi(isolate, toV8StringInternalized(isolate, "V8Console#V8DebuggerClient"));
+}
+
+V8DebuggerClient* clientFromConsoleObject(v8::Isolate* isolate, v8::Local<v8::Object> console)
+{
+ v8::Local<v8::Value> value;
+ if (!console->GetPrivate(isolate->GetCurrentContext(), clientPrivateKey(isolate)).ToLocal(&value))
+ return nullptr;
+ if (!value->IsExternal())
dgozman 2016/04/08 18:18:46 ASSERT
kozy 2016/04/08 23:56:51 Done.
+ return nullptr;
+ V8DebuggerClient* client = static_cast<V8DebuggerClient*>(value.As<v8::External>()->Value());
+ return client;
+}
+
+void internalAddMessage(MessageType type, MessageLevel level, const v8::FunctionCallbackInfo<v8::Value>& info, bool allowEmptyArguments, int firstArg)
dgozman 2016/04/08 18:18:46 skipArgumentCount
kozy 2016/04/08 23:56:51 Done.
+{
+ if (!allowEmptyArguments && !info.Length())
+ return;
+ v8::Local<v8::Object> consoleObject;
+ if (!consoleObjectFromInfo(info).ToLocal(&consoleObject))
+ return;
+ v8::Isolate* isolate = info.GetIsolate();
+ V8DebuggerClient* client = clientFromConsoleObject(isolate, consoleObject);
+ if (!client)
+ return;
+ client->reportMessageToConsole(isolate->GetCurrentContext(), type, level, info, firstArg);
+}
+
+bool createBoundFunctionProperty(v8::Local<v8::Context> context, v8::Local<v8::Object> obj, const char* name, v8::FunctionCallback callback)
+{
+ v8::Local<v8::String> funcName = toV8StringInternalized(context->GetIsolate(), name);
+ v8::Local<v8::Function> func;
+ if (!v8::Function::New(context, callback, obj).ToLocal(&func))
+ return false;
+ func->SetName(funcName);
+ return obj->CreateDataProperty(context, funcName, func).FromMaybe(false);
dgozman 2016/04/08 18:18:46 ASSERT
kozy 2016/04/08 23:56:51 Done.
+}
+
+v8::MaybeLocal<v8::Map> privateMapFromConsoleObject(v8::Isolate* isolate, v8::Local<v8::Object> console, const char* name)
+{
+ v8::Local<v8::Context> context = isolate->GetCurrentContext();
+
+ v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, toV8StringInternalized(isolate, name));
+ v8::Local<v8::Value> mapValue;
+ if (!console->GetPrivate(context, privateKey).ToLocal(&mapValue))
+ return v8::MaybeLocal<v8::Map>();
+ if (mapValue->IsUndefined()) {
+ v8::Local<v8::Map> map = v8::Map::New(isolate);
+ if (!console->SetPrivate(context, privateKey, map).FromMaybe(false))
+ return v8::MaybeLocal<v8::Map>();
+ return map;
+ }
+ return mapValue->IsMap() ? mapValue.As<v8::Map>() : v8::MaybeLocal<v8::Map>();
+}
+
+} // namespace
+
+void V8Console::debugCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(LogMessageType, DebugMessageLevel, info, false, 0);
+}
+
+void V8Console::errorCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(LogMessageType, ErrorMessageLevel, info, false, 0);
+}
+
+void V8Console::infoCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(LogMessageType, InfoMessageLevel, info, false, 0);
+}
+
+void V8Console::logCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(LogMessageType, LogMessageLevel, info, false, 0);
+}
+
+void V8Console::warnCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(LogMessageType, WarningMessageLevel, info, false, 0);
+}
+
+void V8Console::dirCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(DirMessageType, LogMessageLevel, info, false, 0);
+}
+
+void V8Console::dirxmlCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(DirXMLMessageType, LogMessageLevel, info, false, 0);
+}
+
+void V8Console::tableCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(TableMessageType, LogMessageLevel, info, false, 0);
+}
+
+void V8Console::traceCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(TraceMessageType, LogMessageLevel, info, true, 0);
+}
+
+void V8Console::groupCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(StartGroupMessageType, LogMessageLevel, info, true, 0);
+}
+
+void V8Console::groupCollapsedCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(StartGroupCollapsedMessageType, LogMessageLevel, info, true, 0);
+}
+
+void V8Console::groupEndCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(EndGroupMessageType, LogMessageLevel, info, true, 0);
+}
+
+void V8Console::clearCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ internalAddMessage(ClearMessageType, LogMessageLevel, info, true, 0);
+}
+
+void V8Console::countCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ v8::Isolate* isolate = info.GetIsolate();
+ v8::Local<v8::Context> context = isolate->GetCurrentContext();
+
+ v8::Local<v8::Object> consoleObject;
+ if (!consoleObjectFromInfo(info).ToLocal(&consoleObject))
+ return;
+ V8DebuggerClient* client = clientFromConsoleObject(isolate, consoleObject);
+ if (!client)
+ return;
+
+ // Follow Firebug's behavior of counting with null and undefined title in
+ // the same bucket as no argument
+ String16 title;
+ if (info.Length() > 0 && (!info[0]->IsNull() || !info[0]->IsUndefined())) {
+ v8::Local<v8::String> titleValue;
+ if (info[0]->IsObject()) {
+ if (!info[0].As<v8::Object>()->ObjectProtoToString(context).ToLocal(&titleValue))
+ return;
+ } else {
+ if (!info[0]->ToString(context).ToLocal(&titleValue))
+ return;
+ }
+ title = toProtocolString(titleValue);
+ }
+
+ String16 identifier;
+ if (title.isEmpty()) {
+ OwnPtr<V8StackTraceImpl> stackTrace = V8StackTraceImpl::capture(nullptr, 1);
+ if (stackTrace)
+ identifier = stackTrace->topSourceURL() + ":" + String16::number(stackTrace->topLineNumber());
+ } else {
+ identifier = title + "@";
+ }
+
+ v8::Local<v8::Map> countMap;
+ if (!privateMapFromConsoleObject(info.GetIsolate(), consoleObject, "V8Console#countMap").ToLocal(&countMap))
+ return;
+
+ v8::Local<v8::String> key = toV8String(isolate, identifier);
+ int64_t count = 0;
+ if (countMap->Has(context, key).FromMaybe(false)) {
+ v8::Local<v8::Value> countValue;
+ if (!countMap->Get(context, key).ToLocal(&countValue))
+ return;
+ count = countValue.As<v8::Integer>()->Value();
+ }
+ ++count;
+ if (!countMap->Set(context, key, v8::Integer::New(isolate, count)).ToLocal(&countMap))
+ return;
+ String16 message = title + ": " + String16::number(count);
+ client->reportMessageToConsole(context, CountMessageType, DebugMessageLevel, message);
+}
+
+void V8Console::assertCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ bool condition = false;
+ if (info.Length() > 0) {
+ if (info[0]->IsBoolean())
+ condition = info[0].As<v8::Boolean>()->Value();
+ else
+ condition = info[0]->BooleanValue(info.GetIsolate()->GetCurrentContext()).FromMaybe(false);
+ }
+ if (condition)
+ return;
+ internalAddMessage(AssertMessageType, ErrorMessageLevel, info, true, 1);
+}
+
+void V8Console::markTimelineCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ if (!info.Data()->IsObject())
+ return;
+ v8::Local<v8::Object> consoleObject = info.Data().As<v8::Object>();
+ V8DebuggerClient* client = clientFromConsoleObject(info.GetIsolate(), consoleObject);
+ if (!client)
+ return;
+ client->reportMessageToConsole(info.GetIsolate()->GetCurrentContext(), LogMessageType, WarningMessageLevel, "console.markTimeline method was removed.");
+}
+
+void V8Console::profileCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ v8::Local<v8::Object> consoleObject;
+ if (!consoleObjectFromInfo(info).ToLocal(&consoleObject))
+ return;
+ V8DebuggerClient* client = clientFromConsoleObject(info.GetIsolate(), consoleObject);
+ if (!client)
+ return;
+ String16 title = info.Length() > 0 ? toProtocolStringWithTypeCheck(info[0]) : String16();
+ client->profile(info.GetIsolate()->GetCurrentContext(), title);
+}
+
+void V8Console::profileEndCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ v8::Local<v8::Object> consoleObject;
+ if (!consoleObjectFromInfo(info).ToLocal(&consoleObject))
+ return;
+ V8DebuggerClient* client = clientFromConsoleObject(info.GetIsolate(), consoleObject);
+ if (!client)
+ return;
+ String16 title = info.Length() > 0 ? toProtocolStringWithTypeCheck(info[0]) : String16();
+ client->profileEnd(info.GetIsolate()->GetCurrentContext(), title);
+}
+
+void V8Console::timelineCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ v8::Local<v8::Object> consoleObject;
+ if (!consoleObjectFromInfo(info).ToLocal(&consoleObject))
+ return;
+ V8DebuggerClient* client = clientFromConsoleObject(info.GetIsolate(), consoleObject);
+ if (!client)
+ return;
+ client->reportMessageToConsole(info.GetIsolate()->GetCurrentContext(), LogMessageType, WarningMessageLevel, "console.timeline method was removed. Please use console.time imstead.");
+}
+
+void V8Console::timelineEndCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ v8::Local<v8::Object> consoleObject;
+ if (!consoleObjectFromInfo(info).ToLocal(&consoleObject))
+ return;
+ V8DebuggerClient* client = clientFromConsoleObject(info.GetIsolate(), consoleObject);
+ if (!client)
+ return;
+ client->reportMessageToConsole(info.GetIsolate()->GetCurrentContext(), LogMessageType, WarningMessageLevel, "console.timelineEnd method was removed. Please use console.timeEnd imstead.");
+}
+
+void V8Console::timeCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ v8::Isolate* isolate = info.GetIsolate();
+ v8::Local<v8::Context> context = isolate->GetCurrentContext();
+ v8::Local<v8::Object> consoleObject;
+ if (!consoleObjectFromInfo(info).ToLocal(&consoleObject))
+ return;
+ V8DebuggerClient* client = clientFromConsoleObject(isolate, consoleObject);
+ if (!client)
+ return;
+ v8::Local<v8::Map> timeMap;
+ if (!privateMapFromConsoleObject(isolate, consoleObject, "V8Console#timeMap").ToLocal(&timeMap))
+ return;
+ v8::Local<v8::String> title;
+ if (!info[0]->ToString(context).ToLocal(&title))
+ return;
+ if (!timeMap->Set(context, title, v8::Number::New(isolate, client->currentTimeMS())).ToLocal(&timeMap))
+ return;
+}
+
+void V8Console::timeEndCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ v8::Isolate* isolate = info.GetIsolate();
+ v8::Local<v8::Context> context = isolate->GetCurrentContext();
+ v8::Local<v8::Object> consoleObject;
+ if (!consoleObjectFromInfo(info).ToLocal(&consoleObject))
+ return;
+ V8DebuggerClient* client = clientFromConsoleObject(isolate, consoleObject);
+ if (!client)
+ return;
+ v8::Local<v8::Map> timeMap;
+ if (!privateMapFromConsoleObject(isolate, consoleObject, "V8Console#timeMap").ToLocal(&timeMap))
+ return;
+ v8::Local<v8::String> title;
+ if (!info[0]->ToString(context).ToLocal(&title))
+ return;
+ v8::Local<v8::Value> timeValue;
+ if (!timeMap->Get(context, title).ToLocal(&timeValue) || !timeValue->IsNumber())
+ return;
+ double elapsed = client->currentTimeMS() - timeValue.As<v8::Number>()->Value();
+ String16 message = toProtocolString(title) + ": " + String16::fromDouble(elapsed, 3) + "ms";
+ client->reportMessageToConsole(info.GetIsolate()->GetCurrentContext(), TimeEndMessageType, DebugMessageLevel, message);
+}
+
+void V8Console::timeStampCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ v8::Local<v8::Object> consoleObject;
+ if (!consoleObjectFromInfo(info).ToLocal(&consoleObject))
+ return;
+ V8DebuggerClient* client = clientFromConsoleObject(info.GetIsolate(), consoleObject);
+ if (!client)
+ return;
+ String16 title = info.Length() > 0 ? toProtocolStringWithTypeCheck(info[0]) : String16();
+ client->timeStamp(info.GetIsolate(), title);
+}
+
+void V8Console::memoryGetterCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
+{
+ v8::Local<v8::Object> consoleObject;
+ if (!consoleObjectFromInfo(info).ToLocal(&consoleObject))
+ return;
+ V8DebuggerClient* client = clientFromConsoleObject(info.GetIsolate(), consoleObject);
+ if (!client)
+ return;
+ v8::Local<v8::Value> memoryValue;
+ if (!client->memoryInfo(info.GetIsolate(), info.GetIsolate()->GetCurrentContext(), info.Holder()).ToLocal(&memoryValue))
+ return;
+ info.GetReturnValue().Set(memoryValue);
+}
+
+v8::Local<v8::Object> V8Console::create(v8::Local<v8::Context> context, V8DebuggerClient* client)
+{
+ v8::Isolate* isolate = context->GetIsolate();
+ v8::Local<v8::Object> console = v8::Object::New(isolate);
+ createBoundFunctionProperty(context, console, "debug", V8Console::debugCallback);
+ createBoundFunctionProperty(context, console, "error", V8Console::errorCallback);
+ createBoundFunctionProperty(context, console, "info", V8Console::infoCallback);
+ createBoundFunctionProperty(context, console, "log", V8Console::logCallback);
+ createBoundFunctionProperty(context, console, "warn", V8Console::warnCallback);
+ createBoundFunctionProperty(context, console, "dir", V8Console::dirCallback);
+ createBoundFunctionProperty(context, console, "dirxml", V8Console::dirxmlCallback);
+ createBoundFunctionProperty(context, console, "table", V8Console::tableCallback);
+ createBoundFunctionProperty(context, console, "trace", V8Console::traceCallback);
+ createBoundFunctionProperty(context, console, "group", V8Console::groupCallback);
+ createBoundFunctionProperty(context, console, "groupCollapsed", V8Console::groupCollapsedCallback);
+ createBoundFunctionProperty(context, console, "groupEnd", V8Console::groupEndCallback);
+ createBoundFunctionProperty(context, console, "clear", V8Console::clearCallback);
+ createBoundFunctionProperty(context, console, "count", V8Console::countCallback);
+ createBoundFunctionProperty(context, console, "assert", V8Console::assertCallback);
+ createBoundFunctionProperty(context, console, "markTimeline", V8Console::markTimelineCallback);
+ createBoundFunctionProperty(context, console, "profile", V8Console::profileCallback);
+ createBoundFunctionProperty(context, console, "profileEnd", V8Console::profileEndCallback);
+ createBoundFunctionProperty(context, console, "timeline", V8Console::timelineCallback);
+ createBoundFunctionProperty(context, console, "timelineEnd", V8Console::timelineEndCallback);
+ createBoundFunctionProperty(context, console, "time", V8Console::timeCallback);
+ createBoundFunctionProperty(context, console, "timeEnd", V8Console::timeEndCallback);
+ createBoundFunctionProperty(context, console, "timeStamp", V8Console::timeStampCallback);
+ if (client->hasMemoryOnConsole(context))
+ console->SetAccessorProperty(toV8StringInternalized(isolate, "memory"), v8::Function::New(isolate, V8Console::memoryGetterCallback, console), v8::Local<v8::Function>(), static_cast<v8::PropertyAttribute>(v8::None), v8::DEFAULT);
+
+ console->SetPrivate(context, clientPrivateKey(isolate), v8::External::New(isolate, client));
+ return console;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698