LCOV - code coverage report
Current view: top level - src/inspector - v8-console-message.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 294 309 95.1 %
Date: 2019-04-17 Functions: 34 37 91.9 %

          Line data    Source code
       1             : // Copyright 2016 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #include "src/inspector/v8-console-message.h"
       6             : 
       7             : #include "src/debug/debug-interface.h"
       8             : #include "src/inspector/inspected-context.h"
       9             : #include "src/inspector/protocol/Protocol.h"
      10             : #include "src/inspector/string-util.h"
      11             : #include "src/inspector/v8-console-agent-impl.h"
      12             : #include "src/inspector/v8-inspector-impl.h"
      13             : #include "src/inspector/v8-inspector-session-impl.h"
      14             : #include "src/inspector/v8-runtime-agent-impl.h"
      15             : #include "src/inspector/v8-stack-trace-impl.h"
      16             : #include "src/tracing/trace-event.h"
      17             : 
      18             : #include "include/v8-inspector.h"
      19             : 
      20             : namespace v8_inspector {
      21             : 
      22             : namespace {
      23             : 
      24        6660 : String16 consoleAPITypeValue(ConsoleAPIType type) {
      25        6660 :   switch (type) {
      26             :     case ConsoleAPIType::kLog:
      27        5545 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log;
      28             :     case ConsoleAPIType::kDebug:
      29          20 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Debug;
      30             :     case ConsoleAPIType::kInfo:
      31          20 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Info;
      32             :     case ConsoleAPIType::kError:
      33          70 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Error;
      34             :     case ConsoleAPIType::kWarning:
      35          25 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Warning;
      36             :     case ConsoleAPIType::kClear:
      37          75 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Clear;
      38             :     case ConsoleAPIType::kDir:
      39          65 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Dir;
      40             :     case ConsoleAPIType::kDirXML:
      41          35 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Dirxml;
      42             :     case ConsoleAPIType::kTable:
      43         200 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Table;
      44             :     case ConsoleAPIType::kTrace:
      45         400 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Trace;
      46             :     case ConsoleAPIType::kStartGroup:
      47           5 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::StartGroup;
      48             :     case ConsoleAPIType::kStartGroupCollapsed:
      49           5 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::StartGroupCollapsed;
      50             :     case ConsoleAPIType::kEndGroup:
      51           5 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::EndGroup;
      52             :     case ConsoleAPIType::kAssert:
      53          30 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Assert;
      54             :     case ConsoleAPIType::kTimeEnd:
      55          65 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::TimeEnd;
      56             :     case ConsoleAPIType::kCount:
      57          95 :       return protocol::Runtime::ConsoleAPICalled::TypeEnum::Count;
      58             :   }
      59           0 :   return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log;
      60             : }
      61             : 
      62             : const char kGlobalConsoleMessageHandleLabel[] = "DevTools console";
      63             : const unsigned maxConsoleMessageCount = 1000;
      64             : const int maxConsoleMessageV8Size = 10 * 1024 * 1024;
      65             : const unsigned maxArrayItemsLimit = 10000;
      66             : const unsigned maxStackDepthLimit = 32;
      67             : 
      68       13310 : class V8ValueStringBuilder {
      69             :  public:
      70        6655 :   static String16 toString(v8::Local<v8::Value> value,
      71             :                            v8::Local<v8::Context> context) {
      72       13310 :     V8ValueStringBuilder builder(context);
      73        6655 :     if (!builder.append(value)) return String16();
      74        6655 :     return builder.toString();
      75             :   }
      76             : 
      77             :  private:
      78             :   enum {
      79             :     IgnoreNull = 1 << 0,
      80             :     IgnoreUndefined = 1 << 1,
      81             :   };
      82             : 
      83        6655 :   explicit V8ValueStringBuilder(v8::Local<v8::Context> context)
      84             :       : m_arrayLimit(maxArrayItemsLimit),
      85        6655 :         m_isolate(context->GetIsolate()),
      86             :         m_tryCatch(context->GetIsolate()),
      87       13310 :         m_context(context) {}
      88             : 
      89       36830 :   bool append(v8::Local<v8::Value> value, unsigned ignoreOptions = 0) {
      90       36830 :     if (value.IsEmpty()) return true;
      91       66995 :     if ((ignoreOptions & IgnoreNull) && value->IsNull()) return true;
      92       67005 :     if ((ignoreOptions & IgnoreUndefined) && value->IsUndefined()) return true;
      93       36830 :     if (value->IsString()) return append(v8::Local<v8::String>::Cast(value));
      94       10980 :     if (value->IsStringObject())
      95           5 :       return append(v8::Local<v8::StringObject>::Cast(value)->ValueOf());
      96       10975 :     if (value->IsBigInt()) return append(v8::Local<v8::BigInt>::Cast(value));
      97       10970 :     if (value->IsBigIntObject())
      98           5 :       return append(v8::Local<v8::BigIntObject>::Cast(value)->ValueOf());
      99       10965 :     if (value->IsSymbol()) return append(v8::Local<v8::Symbol>::Cast(value));
     100       10960 :     if (value->IsSymbolObject())
     101           5 :       return append(v8::Local<v8::SymbolObject>::Cast(value)->ValueOf());
     102       10955 :     if (value->IsNumberObject()) {
     103          10 :       m_builder.append(String16::fromDouble(
     104           5 :           v8::Local<v8::NumberObject>::Cast(value)->ValueOf(), 6));
     105           5 :       return true;
     106             :     }
     107       10950 :     if (value->IsBooleanObject()) {
     108          10 :       m_builder.append(v8::Local<v8::BooleanObject>::Cast(value)->ValueOf()
     109             :                            ? "true"
     110           5 :                            : "false");
     111           5 :       return true;
     112             :     }
     113       10945 :     if (value->IsArray()) return append(v8::Local<v8::Array>::Cast(value));
     114         865 :     if (value->IsProxy()) {
     115          10 :       m_builder.append("[object Proxy]");
     116           5 :       return true;
     117             :     }
     118        2005 :     if (value->IsObject() && !value->IsDate() && !value->IsFunction() &&
     119        1140 :         !value->IsNativeError() && !value->IsRegExp()) {
     120             :       v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value);
     121             :       v8::Local<v8::String> stringValue;
     122         270 :       if (object->ObjectProtoToString(m_context).ToLocal(&stringValue))
     123         135 :         return append(stringValue);
     124             :     }
     125             :     v8::Local<v8::String> stringValue;
     126        1450 :     if (!value->ToString(m_context).ToLocal(&stringValue)) return false;
     127         725 :     return append(stringValue);
     128             :   }
     129             : 
     130       10080 :   bool append(v8::Local<v8::Array> array) {
     131       20115 :     for (const auto& it : m_visitedArrays) {
     132       10035 :       if (it == array) return true;
     133             :     }
     134       10080 :     uint32_t length = array->Length();
     135       10080 :     if (length > m_arrayLimit) return false;
     136       10080 :     if (m_visitedArrays.size() > maxStackDepthLimit) return false;
     137             : 
     138             :     bool result = true;
     139       10080 :     m_arrayLimit -= length;
     140       10080 :     m_visitedArrays.push_back(array);
     141       70410 :     for (uint32_t i = 0; i < length; ++i) {
     142       30165 :       if (i) m_builder.append(',');
     143             :       v8::Local<v8::Value> value;
     144       60330 :       if (!array->Get(m_context, i).ToLocal(&value)) continue;
     145       30165 :       if (!append(value, IgnoreNull | IgnoreUndefined)) {
     146             :         result = false;
     147           0 :         break;
     148             :       }
     149             :     }
     150             :     m_visitedArrays.pop_back();
     151       10080 :     return result;
     152             :   }
     153             : 
     154          10 :   bool append(v8::Local<v8::Symbol> symbol) {
     155          20 :     m_builder.append("Symbol(");
     156          10 :     bool result = append(symbol->Name(), IgnoreUndefined);
     157          10 :     m_builder.append(')');
     158          10 :     return result;
     159             :   }
     160             : 
     161          10 :   bool append(v8::Local<v8::BigInt> bigint) {
     162             :     v8::Local<v8::String> bigint_string;
     163          20 :     if (!bigint->ToString(m_context).ToLocal(&bigint_string)) return false;
     164          10 :     bool result = append(bigint_string);
     165          10 :     if (m_tryCatch.HasCaught()) return false;
     166          10 :     m_builder.append('n');
     167          10 :     return result;
     168             :   }
     169             : 
     170       26725 :   bool append(v8::Local<v8::String> string) {
     171       26725 :     if (m_tryCatch.HasCaught()) return false;
     172       26725 :     if (!string.IsEmpty()) {
     173       53450 :       m_builder.append(toProtocolString(m_isolate, string));
     174             :     }
     175             :     return true;
     176             :   }
     177             : 
     178        6655 :   String16 toString() {
     179        6655 :     if (m_tryCatch.HasCaught()) return String16();
     180        6655 :     return m_builder.toString();
     181             :   }
     182             : 
     183             :   uint32_t m_arrayLimit;
     184             :   v8::Isolate* m_isolate;
     185             :   String16Builder m_builder;
     186             :   std::vector<v8::Local<v8::Array>> m_visitedArrays;
     187             :   v8::TryCatch m_tryCatch;
     188             :   v8::Local<v8::Context> m_context;
     189             : };
     190             : 
     191             : }  // namespace
     192             : 
     193        7130 : V8ConsoleMessage::V8ConsoleMessage(V8MessageOrigin origin, double timestamp,
     194             :                                    const String16& message)
     195             :     : m_origin(origin),
     196             :       m_timestamp(timestamp),
     197             :       m_message(message),
     198             :       m_lineNumber(0),
     199             :       m_columnNumber(0),
     200             :       m_scriptId(0),
     201             :       m_contextId(0),
     202             :       m_type(ConsoleAPIType::kLog),
     203             :       m_exceptionId(0),
     204       28520 :       m_revokedExceptionId(0) {}
     205             : 
     206             : V8ConsoleMessage::~V8ConsoleMessage() = default;
     207             : 
     208         370 : void V8ConsoleMessage::setLocation(const String16& url, unsigned lineNumber,
     209             :                                    unsigned columnNumber,
     210             :                                    std::unique_ptr<V8StackTraceImpl> stackTrace,
     211             :                                    int scriptId) {
     212             :   m_url = url;
     213         370 :   m_lineNumber = lineNumber;
     214         370 :   m_columnNumber = columnNumber;
     215             :   m_stackTrace = std::move(stackTrace);
     216         370 :   m_scriptId = scriptId;
     217         370 : }
     218             : 
     219          10 : void V8ConsoleMessage::reportToFrontend(
     220             :     protocol::Console::Frontend* frontend) const {
     221             :   DCHECK_EQ(V8MessageOrigin::kConsole, m_origin);
     222          10 :   String16 level = protocol::Console::ConsoleMessage::LevelEnum::Log;
     223          10 :   if (m_type == ConsoleAPIType::kDebug || m_type == ConsoleAPIType::kCount ||
     224             :       m_type == ConsoleAPIType::kTimeEnd)
     225           0 :     level = protocol::Console::ConsoleMessage::LevelEnum::Debug;
     226          10 :   else if (m_type == ConsoleAPIType::kError ||
     227             :            m_type == ConsoleAPIType::kAssert)
     228          10 :     level = protocol::Console::ConsoleMessage::LevelEnum::Error;
     229           5 :   else if (m_type == ConsoleAPIType::kWarning)
     230           0 :     level = protocol::Console::ConsoleMessage::LevelEnum::Warning;
     231           5 :   else if (m_type == ConsoleAPIType::kInfo)
     232           0 :     level = protocol::Console::ConsoleMessage::LevelEnum::Info;
     233             :   std::unique_ptr<protocol::Console::ConsoleMessage> result =
     234          10 :       protocol::Console::ConsoleMessage::create()
     235          20 :           .setSource(protocol::Console::ConsoleMessage::SourceEnum::ConsoleApi)
     236             :           .setLevel(level)
     237             :           .setText(m_message)
     238             :           .build();
     239          10 :   result->setLine(static_cast<int>(m_lineNumber));
     240          10 :   result->setColumn(static_cast<int>(m_columnNumber));
     241          10 :   result->setUrl(m_url);
     242          20 :   frontend->messageAdded(std::move(result));
     243          10 : }
     244             : 
     245             : std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>>
     246        6660 : V8ConsoleMessage::wrapArguments(V8InspectorSessionImpl* session,
     247             :                                 bool generatePreview) const {
     248             :   V8InspectorImpl* inspector = session->inspector();
     249             :   int contextGroupId = session->contextGroupId();
     250        6660 :   int contextId = m_contextId;
     251        6660 :   if (!m_arguments.size() || !contextId) return nullptr;
     252             :   InspectedContext* inspectedContext =
     253        6660 :       inspector->getContext(contextGroupId, contextId);
     254        6660 :   if (!inspectedContext) return nullptr;
     255             : 
     256        6660 :   v8::Isolate* isolate = inspectedContext->isolate();
     257       13320 :   v8::HandleScope handles(isolate);
     258        6660 :   v8::Local<v8::Context> context = inspectedContext->context();
     259             : 
     260             :   std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>> args =
     261        6660 :       protocol::Array<protocol::Runtime::RemoteObject>::create();
     262             : 
     263             :   v8::Local<v8::Value> value = m_arguments[0]->Get(isolate);
     264        6660 :   if (value->IsObject() && m_type == ConsoleAPIType::kTable &&
     265             :       generatePreview) {
     266             :     v8::MaybeLocal<v8::Array> columns;
     267         165 :     if (m_arguments.size() > 1) {
     268             :       v8::Local<v8::Value> secondArgument = m_arguments[1]->Get(isolate);
     269          10 :       if (secondArgument->IsArray()) {
     270          10 :         columns = v8::Local<v8::Array>::Cast(secondArgument);
     271           0 :       } else if (secondArgument->IsString()) {
     272           0 :         v8::TryCatch tryCatch(isolate);
     273           0 :         v8::Local<v8::Array> array = v8::Array::New(isolate);
     274           0 :         if (array->Set(context, 0, secondArgument).IsJust()) {
     275           0 :           columns = array;
     276             :         }
     277             :       }
     278             :     }
     279             :     std::unique_ptr<protocol::Runtime::RemoteObject> wrapped =
     280             :         session->wrapTable(context, v8::Local<v8::Object>::Cast(value),
     281         165 :                            columns);
     282         165 :     inspectedContext = inspector->getContext(contextGroupId, contextId);
     283         165 :     if (!inspectedContext) return nullptr;
     284         165 :     if (wrapped) {
     285         160 :       args->addItem(std::move(wrapped));
     286             :     } else {
     287             :       args = nullptr;
     288             :     }
     289             :   } else {
     290       19525 :     for (size_t i = 0; i < m_arguments.size(); ++i) {
     291             :       std::unique_ptr<protocol::Runtime::RemoteObject> wrapped =
     292             :           session->wrapObject(context, m_arguments[i]->Get(isolate), "console",
     293       19545 :                               generatePreview);
     294        6515 :       inspectedContext = inspector->getContext(contextGroupId, contextId);
     295        6515 :       if (!inspectedContext) return nullptr;
     296        6515 :       if (!wrapped) {
     297             :         args = nullptr;
     298             :         break;
     299             :       }
     300        6515 :       args->addItem(std::move(wrapped));
     301             :     }
     302             :   }
     303             :   return args;
     304             : }
     305             : 
     306        6915 : void V8ConsoleMessage::reportToFrontend(protocol::Runtime::Frontend* frontend,
     307             :                                         V8InspectorSessionImpl* session,
     308             :                                         bool generatePreview) const {
     309             :   int contextGroupId = session->contextGroupId();
     310             :   V8InspectorImpl* inspector = session->inspector();
     311             : 
     312        6915 :   if (m_origin == V8MessageOrigin::kException) {
     313             :     std::unique_ptr<protocol::Runtime::RemoteObject> exception =
     314         185 :         wrapException(session, generatePreview);
     315         185 :     if (!inspector->hasConsoleMessageStorage(contextGroupId)) return;
     316             :     std::unique_ptr<protocol::Runtime::ExceptionDetails> exceptionDetails =
     317         185 :         protocol::Runtime::ExceptionDetails::create()
     318         185 :             .setExceptionId(m_exceptionId)
     319         185 :             .setText(exception ? m_message : m_detailedMessage)
     320         185 :             .setLineNumber(m_lineNumber ? m_lineNumber - 1 : 0)
     321         185 :             .setColumnNumber(m_columnNumber ? m_columnNumber - 1 : 0)
     322             :             .build();
     323         185 :     if (m_scriptId)
     324         210 :       exceptionDetails->setScriptId(String16::fromInteger(m_scriptId));
     325         185 :     if (!m_url.isEmpty()) exceptionDetails->setUrl(m_url);
     326         185 :     if (m_stackTrace) {
     327             :       exceptionDetails->setStackTrace(
     328         230 :           m_stackTrace->buildInspectorObjectImpl(inspector->debugger()));
     329             :     }
     330         185 :     if (m_contextId) exceptionDetails->setExecutionContextId(m_contextId);
     331         185 :     if (exception) exceptionDetails->setException(std::move(exception));
     332         370 :     frontend->exceptionThrown(m_timestamp, std::move(exceptionDetails));
     333             :     return;
     334             :   }
     335        6730 :   if (m_origin == V8MessageOrigin::kRevokedException) {
     336          70 :     frontend->exceptionRevoked(m_message, m_revokedExceptionId);
     337          70 :     return;
     338             :   }
     339        6660 :   if (m_origin == V8MessageOrigin::kConsole) {
     340             :     std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>>
     341       13320 :         arguments = wrapArguments(session, generatePreview);
     342        6660 :     if (!inspector->hasConsoleMessageStorage(contextGroupId)) return;
     343        6660 :     if (!arguments) {
     344           5 :       arguments = protocol::Array<protocol::Runtime::RemoteObject>::create();
     345           5 :       if (!m_message.isEmpty()) {
     346             :         std::unique_ptr<protocol::Runtime::RemoteObject> messageArg =
     347             :             protocol::Runtime::RemoteObject::create()
     348          10 :                 .setType(protocol::Runtime::RemoteObject::TypeEnum::String)
     349             :                 .build();
     350           5 :         messageArg->setValue(protocol::StringValue::create(m_message));
     351           5 :         arguments->addItem(std::move(messageArg));
     352             :       }
     353             :     }
     354             :     Maybe<String16> consoleContext;
     355        6730 :     if (!m_consoleContext.isEmpty()) consoleContext = m_consoleContext;
     356             :     std::unique_ptr<protocol::Runtime::StackTrace> stackTrace;
     357        6660 :     if (m_stackTrace) {
     358        6615 :       switch (m_type) {
     359             :         case ConsoleAPIType::kAssert:
     360             :         case ConsoleAPIType::kError:
     361             :         case ConsoleAPIType::kTrace:
     362             :         case ConsoleAPIType::kWarning:
     363             :           stackTrace =
     364         960 :               m_stackTrace->buildInspectorObjectImpl(inspector->debugger());
     365         480 :           break;
     366             :         default:
     367             :           stackTrace =
     368       12270 :               m_stackTrace->buildInspectorObjectImpl(inspector->debugger(), 0);
     369        6135 :           break;
     370             :       }
     371             :     }
     372       19980 :     frontend->consoleAPICalled(
     373       19980 :         consoleAPITypeValue(m_type), std::move(arguments), m_contextId,
     374       13320 :         m_timestamp, std::move(stackTrace), std::move(consoleContext));
     375             :     return;
     376             :   }
     377           0 :   UNREACHABLE();
     378             : }
     379             : 
     380             : std::unique_ptr<protocol::Runtime::RemoteObject>
     381         185 : V8ConsoleMessage::wrapException(V8InspectorSessionImpl* session,
     382             :                                 bool generatePreview) const {
     383         185 :   if (!m_arguments.size() || !m_contextId) return nullptr;
     384             :   DCHECK_EQ(1u, m_arguments.size());
     385             :   InspectedContext* inspectedContext =
     386         185 :       session->inspector()->getContext(session->contextGroupId(), m_contextId);
     387         185 :   if (!inspectedContext) return nullptr;
     388             : 
     389         185 :   v8::Isolate* isolate = inspectedContext->isolate();
     390         370 :   v8::HandleScope handles(isolate);
     391             :   // TODO(dgozman): should we use different object group?
     392             :   return session->wrapObject(inspectedContext->context(),
     393             :                              m_arguments[0]->Get(isolate), "console",
     394         555 :                              generatePreview);
     395             : }
     396             : 
     397       14345 : V8MessageOrigin V8ConsoleMessage::origin() const { return m_origin; }
     398             : 
     399       14260 : ConsoleAPIType V8ConsoleMessage::type() const { return m_type; }
     400             : 
     401             : // static
     402        6655 : std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForConsoleAPI(
     403             :     v8::Local<v8::Context> v8Context, int contextId, int groupId,
     404             :     V8InspectorImpl* inspector, double timestamp, ConsoleAPIType type,
     405             :     const std::vector<v8::Local<v8::Value>>& arguments,
     406             :     const String16& consoleContext,
     407             :     std::unique_ptr<V8StackTraceImpl> stackTrace) {
     408        6655 :   v8::Isolate* isolate = v8Context->GetIsolate();
     409             : 
     410             :   std::unique_ptr<V8ConsoleMessage> message(
     411       19965 :       new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16()));
     412        6655 :   if (stackTrace && !stackTrace->isEmpty()) {
     413       13200 :     message->m_url = toString16(stackTrace->topSourceURL());
     414        6600 :     message->m_lineNumber = stackTrace->topLineNumber();
     415        6600 :     message->m_columnNumber = stackTrace->topColumnNumber();
     416             :   }
     417             :   message->m_stackTrace = std::move(stackTrace);
     418             :   message->m_consoleContext = consoleContext;
     419        6655 :   message->m_type = type;
     420        6655 :   message->m_contextId = contextId;
     421       20025 :   for (size_t i = 0; i < arguments.size(); ++i) {
     422             :     std::unique_ptr<v8::Global<v8::Value>> argument(
     423        6685 :         new v8::Global<v8::Value>(isolate, arguments.at(i)));
     424             :     argument->AnnotateStrongRetainer(kGlobalConsoleMessageHandleLabel);
     425        6685 :     message->m_arguments.push_back(std::move(argument));
     426             :     message->m_v8Size +=
     427       13370 :         v8::debug::EstimatedValueSize(isolate, arguments.at(i));
     428             :   }
     429        6655 :   if (arguments.size())
     430             :     message->m_message =
     431       13310 :         V8ValueStringBuilder::toString(arguments[0], v8Context);
     432             : 
     433             :   v8::Isolate::MessageErrorLevel clientLevel = v8::Isolate::kMessageInfo;
     434        6655 :   if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount ||
     435             :       type == ConsoleAPIType::kTimeEnd) {
     436             :     clientLevel = v8::Isolate::kMessageDebug;
     437       12970 :   } else if (type == ConsoleAPIType::kError ||
     438        6485 :              type == ConsoleAPIType::kAssert) {
     439             :     clientLevel = v8::Isolate::kMessageError;
     440        6345 :   } else if (type == ConsoleAPIType::kWarning) {
     441             :     clientLevel = v8::Isolate::kMessageWarning;
     442             :   } else if (type == ConsoleAPIType::kInfo || type == ConsoleAPIType::kLog) {
     443             :     clientLevel = v8::Isolate::kMessageInfo;
     444             :   }
     445             : 
     446        6655 :   if (type != ConsoleAPIType::kClear) {
     447        6605 :     inspector->client()->consoleAPIMessage(
     448       13210 :         groupId, clientLevel, toStringView(message->m_message),
     449       13210 :         toStringView(message->m_url), message->m_lineNumber,
     450       13210 :         message->m_columnNumber, message->m_stackTrace.get());
     451             :   }
     452             : 
     453        6655 :   return message;
     454             : }
     455             : 
     456             : // static
     457         370 : std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForException(
     458             :     double timestamp, const String16& detailedMessage, const String16& url,
     459             :     unsigned lineNumber, unsigned columnNumber,
     460             :     std::unique_ptr<V8StackTraceImpl> stackTrace, int scriptId,
     461             :     v8::Isolate* isolate, const String16& message, int contextId,
     462             :     v8::Local<v8::Value> exception, unsigned exceptionId) {
     463             :   std::unique_ptr<V8ConsoleMessage> consoleMessage(
     464         370 :       new V8ConsoleMessage(V8MessageOrigin::kException, timestamp, message));
     465         370 :   consoleMessage->setLocation(url, lineNumber, columnNumber,
     466         370 :                               std::move(stackTrace), scriptId);
     467         370 :   consoleMessage->m_exceptionId = exceptionId;
     468             :   consoleMessage->m_detailedMessage = detailedMessage;
     469         740 :   if (contextId && !exception.IsEmpty()) {
     470         370 :     consoleMessage->m_contextId = contextId;
     471        1110 :     consoleMessage->m_arguments.push_back(
     472             :         std::unique_ptr<v8::Global<v8::Value>>(
     473             :             new v8::Global<v8::Value>(isolate, exception)));
     474             :     consoleMessage->m_v8Size +=
     475         740 :         v8::debug::EstimatedValueSize(isolate, exception);
     476             :   }
     477         370 :   return consoleMessage;
     478             : }
     479             : 
     480             : // static
     481         105 : std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForRevokedException(
     482             :     double timestamp, const String16& messageText,
     483             :     unsigned revokedExceptionId) {
     484             :   std::unique_ptr<V8ConsoleMessage> message(new V8ConsoleMessage(
     485         105 :       V8MessageOrigin::kRevokedException, timestamp, messageText));
     486         105 :   message->m_revokedExceptionId = revokedExceptionId;
     487         105 :   return message;
     488             : }
     489             : 
     490          45 : void V8ConsoleMessage::contextDestroyed(int contextId) {
     491          60 :   if (contextId != m_contextId) return;
     492          30 :   m_contextId = 0;
     493          30 :   if (m_message.isEmpty()) m_message = "<message collected>";
     494          30 :   Arguments empty;
     495             :   m_arguments.swap(empty);
     496          30 :   m_v8Size = 0;
     497             : }
     498             : 
     499             : // ------------------------ V8ConsoleMessageStorage ----------------------------
     500             : 
     501         463 : V8ConsoleMessageStorage::V8ConsoleMessageStorage(V8InspectorImpl* inspector,
     502             :                                                  int contextGroupId)
     503         926 :     : m_inspector(inspector), m_contextGroupId(contextGroupId) {}
     504             : 
     505         926 : V8ConsoleMessageStorage::~V8ConsoleMessageStorage() { clear(); }
     506             : 
     507             : namespace {
     508             : 
     509        7130 : void TraceV8ConsoleMessageEvent(V8MessageOrigin origin, ConsoleAPIType type) {
     510             :   // Change in this function requires adjustment of Catapult/Telemetry metric
     511             :   // tracing/tracing/metrics/console_error_metric.html.
     512             :   // See https://crbug.com/880432
     513        7130 :   if (origin == V8MessageOrigin::kException) {
     514         740 :     TRACE_EVENT_INSTANT0("v8.console", "V8ConsoleMessage::Exception",
     515             :                          TRACE_EVENT_SCOPE_THREAD);
     516        6760 :   } else if (type == ConsoleAPIType::kError) {
     517         120 :     TRACE_EVENT_INSTANT0("v8.console", "V8ConsoleMessage::Error",
     518             :                          TRACE_EVENT_SCOPE_THREAD);
     519        6700 :   } else if (type == ConsoleAPIType::kAssert) {
     520         160 :     TRACE_EVENT_INSTANT0("v8.console", "V8ConsoleMessage::Assert",
     521             :                          TRACE_EVENT_SCOPE_THREAD);
     522             :   }
     523        7130 : }
     524             : 
     525             : }  // anonymous namespace
     526             : 
     527        7130 : void V8ConsoleMessageStorage::addMessage(
     528             :     std::unique_ptr<V8ConsoleMessage> message) {
     529        7130 :   int contextGroupId = m_contextGroupId;
     530        7130 :   V8InspectorImpl* inspector = m_inspector;
     531        7130 :   if (message->type() == ConsoleAPIType::kClear) clear();
     532             : 
     533        7130 :   TraceV8ConsoleMessageEvent(message->origin(), message->type());
     534             : 
     535        7130 :   inspector->forEachSession(
     536       14430 :       contextGroupId, [&message](V8InspectorSessionImpl* session) {
     537        7215 :         if (message->origin() == V8MessageOrigin::kConsole)
     538        6710 :           session->consoleAgent()->messageAdded(message.get());
     539        7215 :         session->runtimeAgent()->messageAdded(message.get());
     540       14345 :       });
     541        7130 :   if (!inspector->hasConsoleMessageStorage(contextGroupId)) return;
     542             : 
     543             :   DCHECK(m_messages.size() <= maxConsoleMessageCount);
     544        7130 :   if (m_messages.size() == maxConsoleMessageCount) {
     545          50 :     m_estimatedSize -= m_messages.front()->estimatedSize();
     546          50 :     m_messages.pop_front();
     547             :   }
     548       29275 :   while (m_estimatedSize + message->estimatedSize() > maxConsoleMessageV8Size &&
     549             :          !m_messages.empty()) {
     550        5005 :     m_estimatedSize -= m_messages.front()->estimatedSize();
     551        5005 :     m_messages.pop_front();
     552             :   }
     553             : 
     554        7130 :   m_messages.push_back(std::move(message));
     555        7130 :   m_estimatedSize += m_messages.back()->estimatedSize();
     556             : }
     557             : 
     558         518 : void V8ConsoleMessageStorage::clear() {
     559         518 :   m_messages.clear();
     560         518 :   m_estimatedSize = 0;
     561        1036 :   m_inspector->forEachSession(m_contextGroupId,
     562          55 :                               [](V8InspectorSessionImpl* session) {
     563         110 :                                 session->releaseObjectGroup("console");
     564         573 :                               });
     565             :   m_data.clear();
     566         518 : }
     567             : 
     568           0 : bool V8ConsoleMessageStorage::shouldReportDeprecationMessage(
     569             :     int contextId, const String16& method) {
     570             :   std::set<String16>& reportedDeprecationMessages =
     571           0 :       m_data[contextId].m_reportedDeprecationMessages;
     572             :   auto it = reportedDeprecationMessages.find(method);
     573           0 :   if (it != reportedDeprecationMessages.end()) return false;
     574             :   reportedDeprecationMessages.insert(it, method);
     575           0 :   return true;
     576             : }
     577             : 
     578          85 : int V8ConsoleMessageStorage::count(int contextId, const String16& id) {
     579          85 :   return ++m_data[contextId].m_count[id];
     580             : }
     581             : 
     582          65 : void V8ConsoleMessageStorage::time(int contextId, const String16& id) {
     583          65 :   m_data[contextId].m_time[id] = m_inspector->client()->currentTimeMS();
     584          65 : }
     585             : 
     586          20 : bool V8ConsoleMessageStorage::countReset(int contextId, const String16& id) {
     587          20 :   std::map<String16, int>& count_map = m_data[contextId].m_count;
     588          20 :   if (count_map.find(id) == count_map.end()) return false;
     589             : 
     590          15 :   count_map[id] = 0;
     591          15 :   return true;
     592             : }
     593             : 
     594          10 : double V8ConsoleMessageStorage::timeLog(int contextId, const String16& id) {
     595          10 :   std::map<String16, double>& time = m_data[contextId].m_time;
     596             :   auto it = time.find(id);
     597          10 :   if (it == time.end()) return 0.0;
     598          10 :   return m_inspector->client()->currentTimeMS() - it->second;
     599             : }
     600             : 
     601          65 : double V8ConsoleMessageStorage::timeEnd(int contextId, const String16& id) {
     602          65 :   std::map<String16, double>& time = m_data[contextId].m_time;
     603             :   auto it = time.find(id);
     604          65 :   if (it == time.end()) return 0.0;
     605          65 :   double elapsed = m_inspector->client()->currentTimeMS() - it->second;
     606             :   time.erase(it);
     607          65 :   return elapsed;
     608             : }
     609             : 
     610         155 : bool V8ConsoleMessageStorage::hasTimer(int contextId, const String16& id) {
     611         155 :   const std::map<String16, double>& time = m_data[contextId].m_time;
     612         155 :   return time.find(id) != time.end();
     613             : }
     614             : 
     615          20 : void V8ConsoleMessageStorage::contextDestroyed(int contextId) {
     616          20 :   m_estimatedSize = 0;
     617         110 :   for (size_t i = 0; i < m_messages.size(); ++i) {
     618          90 :     m_messages[i]->contextDestroyed(contextId);
     619          45 :     m_estimatedSize += m_messages[i]->estimatedSize();
     620             :   }
     621             :   auto it = m_data.find(contextId);
     622          20 :   if (it != m_data.end()) m_data.erase(contextId);
     623          20 : }
     624             : 
     625             : }  // namespace v8_inspector

Generated by: LCOV version 1.10