LCOV - code coverage report
Current view: top level - src/inspector - v8-console-message.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 289 304 95.1 %
Date: 2019-03-21 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        6655 : String16 consoleAPITypeValue(ConsoleAPIType type) {
      25        6655 :   switch (type) {
      26             :     case ConsoleAPIType::kLog:
      27        5560 :       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         380 :       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       13300 : class V8ValueStringBuilder {
      69             :  public:
      70        6650 :   static String16 toString(v8::Local<v8::Value> value,
      71             :                            v8::Local<v8::Context> context) {
      72       13300 :     V8ValueStringBuilder builder(context);
      73        6650 :     if (!builder.append(value)) return String16();
      74        6650 :     return builder.toString();
      75             :   }
      76             : 
      77             :  private:
      78             :   enum {
      79             :     IgnoreNull = 1 << 0,
      80             :     IgnoreUndefined = 1 << 1,
      81             :   };
      82             : 
      83        6650 :   explicit V8ValueStringBuilder(v8::Local<v8::Context> context)
      84             :       : m_arrayLimit(maxArrayItemsLimit),
      85        6650 :         m_isolate(context->GetIsolate()),
      86             :         m_tryCatch(context->GetIsolate()),
      87       13300 :         m_context(context) {}
      88             : 
      89       36825 :   bool append(v8::Local<v8::Value> value, unsigned ignoreOptions = 0) {
      90       36825 :     if (value.IsEmpty()) return true;
      91       66990 :     if ((ignoreOptions & IgnoreNull) && value->IsNull()) return true;
      92       67000 :     if ((ignoreOptions & IgnoreUndefined) && value->IsUndefined()) return true;
      93       36825 :     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       26720 :   bool append(v8::Local<v8::String> string) {
     171       26720 :     if (m_tryCatch.HasCaught()) return false;
     172       26720 :     if (!string.IsEmpty()) {
     173       53440 :       m_builder.append(toProtocolString(m_isolate, string));
     174             :     }
     175             :     return true;
     176             :   }
     177             : 
     178        6650 :   String16 toString() {
     179        6650 :     if (m_tryCatch.HasCaught()) return String16();
     180        6650 :     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        7125 : 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       28500 :       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        6655 : V8ConsoleMessage::wrapArguments(V8InspectorSessionImpl* session,
     247             :                                 bool generatePreview) const {
     248             :   V8InspectorImpl* inspector = session->inspector();
     249             :   int contextGroupId = session->contextGroupId();
     250        6655 :   int contextId = m_contextId;
     251        6655 :   if (!m_arguments.size() || !contextId) return nullptr;
     252             :   InspectedContext* inspectedContext =
     253        6655 :       inspector->getContext(contextGroupId, contextId);
     254        6655 :   if (!inspectedContext) return nullptr;
     255             : 
     256        6655 :   v8::Isolate* isolate = inspectedContext->isolate();
     257       13310 :   v8::HandleScope handles(isolate);
     258        6655 :   v8::Local<v8::Context> context = inspectedContext->context();
     259             : 
     260             :   std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>> args =
     261        6655 :       protocol::Array<protocol::Runtime::RemoteObject>::create();
     262             : 
     263             :   v8::Local<v8::Value> value = m_arguments[0]->Get(isolate);
     264        6655 :   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       19510 :     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       19530 :                               generatePreview);
     294        6510 :       inspectedContext = inspector->getContext(contextGroupId, contextId);
     295        6510 :       if (!inspectedContext) return nullptr;
     296        6510 :       if (!wrapped) {
     297             :         args = nullptr;
     298             :         break;
     299             :       }
     300        6510 :       args->addItem(std::move(wrapped));
     301             :     }
     302             :   }
     303             :   return args;
     304             : }
     305             : 
     306        6910 : 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        6910 :   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        6725 :   if (m_origin == V8MessageOrigin::kRevokedException) {
     336          70 :     frontend->exceptionRevoked(m_message, m_revokedExceptionId);
     337          70 :     return;
     338             :   }
     339        6655 :   if (m_origin == V8MessageOrigin::kConsole) {
     340             :     std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>>
     341       13310 :         arguments = wrapArguments(session, generatePreview);
     342        6655 :     if (!inspector->hasConsoleMessageStorage(contextGroupId)) return;
     343        6655 :     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        6725 :     if (!m_consoleContext.isEmpty()) consoleContext = m_consoleContext;
     356       39885 :     frontend->consoleAPICalled(
     357       19965 :         consoleAPITypeValue(m_type), std::move(arguments), m_contextId,
     358        6655 :         m_timestamp,
     359             :         m_stackTrace
     360             :             ? m_stackTrace->buildInspectorObjectImpl(inspector->debugger())
     361             :             : nullptr,
     362        6655 :         std::move(consoleContext));
     363             :     return;
     364             :   }
     365           0 :   UNREACHABLE();
     366             : }
     367             : 
     368             : std::unique_ptr<protocol::Runtime::RemoteObject>
     369         185 : V8ConsoleMessage::wrapException(V8InspectorSessionImpl* session,
     370             :                                 bool generatePreview) const {
     371         185 :   if (!m_arguments.size() || !m_contextId) return nullptr;
     372             :   DCHECK_EQ(1u, m_arguments.size());
     373             :   InspectedContext* inspectedContext =
     374         185 :       session->inspector()->getContext(session->contextGroupId(), m_contextId);
     375         185 :   if (!inspectedContext) return nullptr;
     376             : 
     377         185 :   v8::Isolate* isolate = inspectedContext->isolate();
     378         370 :   v8::HandleScope handles(isolate);
     379             :   // TODO(dgozman): should we use different object group?
     380             :   return session->wrapObject(inspectedContext->context(),
     381             :                              m_arguments[0]->Get(isolate), "console",
     382         555 :                              generatePreview);
     383             : }
     384             : 
     385       14335 : V8MessageOrigin V8ConsoleMessage::origin() const { return m_origin; }
     386             : 
     387       14250 : ConsoleAPIType V8ConsoleMessage::type() const { return m_type; }
     388             : 
     389             : // static
     390        6650 : std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForConsoleAPI(
     391             :     v8::Local<v8::Context> v8Context, int contextId, int groupId,
     392             :     V8InspectorImpl* inspector, double timestamp, ConsoleAPIType type,
     393             :     const std::vector<v8::Local<v8::Value>>& arguments,
     394             :     const String16& consoleContext,
     395             :     std::unique_ptr<V8StackTraceImpl> stackTrace) {
     396        6650 :   v8::Isolate* isolate = v8Context->GetIsolate();
     397             : 
     398             :   std::unique_ptr<V8ConsoleMessage> message(
     399       19950 :       new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16()));
     400        6650 :   if (stackTrace && !stackTrace->isEmpty()) {
     401       13190 :     message->m_url = toString16(stackTrace->topSourceURL());
     402        6595 :     message->m_lineNumber = stackTrace->topLineNumber();
     403        6595 :     message->m_columnNumber = stackTrace->topColumnNumber();
     404             :   }
     405             :   message->m_stackTrace = std::move(stackTrace);
     406             :   message->m_consoleContext = consoleContext;
     407        6650 :   message->m_type = type;
     408        6650 :   message->m_contextId = contextId;
     409       20010 :   for (size_t i = 0; i < arguments.size(); ++i) {
     410             :     std::unique_ptr<v8::Global<v8::Value>> argument(
     411        6680 :         new v8::Global<v8::Value>(isolate, arguments.at(i)));
     412             :     argument->AnnotateStrongRetainer(kGlobalConsoleMessageHandleLabel);
     413        6680 :     message->m_arguments.push_back(std::move(argument));
     414             :     message->m_v8Size +=
     415       13360 :         v8::debug::EstimatedValueSize(isolate, arguments.at(i));
     416             :   }
     417        6650 :   if (arguments.size())
     418             :     message->m_message =
     419       13300 :         V8ValueStringBuilder::toString(arguments[0], v8Context);
     420             : 
     421             :   v8::Isolate::MessageErrorLevel clientLevel = v8::Isolate::kMessageInfo;
     422        6650 :   if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount ||
     423             :       type == ConsoleAPIType::kTimeEnd) {
     424             :     clientLevel = v8::Isolate::kMessageDebug;
     425       12960 :   } else if (type == ConsoleAPIType::kError ||
     426        6480 :              type == ConsoleAPIType::kAssert) {
     427             :     clientLevel = v8::Isolate::kMessageError;
     428        6340 :   } else if (type == ConsoleAPIType::kWarning) {
     429             :     clientLevel = v8::Isolate::kMessageWarning;
     430             :   } else if (type == ConsoleAPIType::kInfo || type == ConsoleAPIType::kLog) {
     431             :     clientLevel = v8::Isolate::kMessageInfo;
     432             :   }
     433             : 
     434        6650 :   if (type != ConsoleAPIType::kClear) {
     435        6600 :     inspector->client()->consoleAPIMessage(
     436       13200 :         groupId, clientLevel, toStringView(message->m_message),
     437       13200 :         toStringView(message->m_url), message->m_lineNumber,
     438       13200 :         message->m_columnNumber, message->m_stackTrace.get());
     439             :   }
     440             : 
     441        6650 :   return message;
     442             : }
     443             : 
     444             : // static
     445         370 : std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForException(
     446             :     double timestamp, const String16& detailedMessage, const String16& url,
     447             :     unsigned lineNumber, unsigned columnNumber,
     448             :     std::unique_ptr<V8StackTraceImpl> stackTrace, int scriptId,
     449             :     v8::Isolate* isolate, const String16& message, int contextId,
     450             :     v8::Local<v8::Value> exception, unsigned exceptionId) {
     451             :   std::unique_ptr<V8ConsoleMessage> consoleMessage(
     452         370 :       new V8ConsoleMessage(V8MessageOrigin::kException, timestamp, message));
     453         370 :   consoleMessage->setLocation(url, lineNumber, columnNumber,
     454         370 :                               std::move(stackTrace), scriptId);
     455         370 :   consoleMessage->m_exceptionId = exceptionId;
     456             :   consoleMessage->m_detailedMessage = detailedMessage;
     457         740 :   if (contextId && !exception.IsEmpty()) {
     458         370 :     consoleMessage->m_contextId = contextId;
     459        1110 :     consoleMessage->m_arguments.push_back(
     460             :         std::unique_ptr<v8::Global<v8::Value>>(
     461             :             new v8::Global<v8::Value>(isolate, exception)));
     462             :     consoleMessage->m_v8Size +=
     463         740 :         v8::debug::EstimatedValueSize(isolate, exception);
     464             :   }
     465         370 :   return consoleMessage;
     466             : }
     467             : 
     468             : // static
     469         105 : std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForRevokedException(
     470             :     double timestamp, const String16& messageText,
     471             :     unsigned revokedExceptionId) {
     472             :   std::unique_ptr<V8ConsoleMessage> message(new V8ConsoleMessage(
     473         105 :       V8MessageOrigin::kRevokedException, timestamp, messageText));
     474         105 :   message->m_revokedExceptionId = revokedExceptionId;
     475         105 :   return message;
     476             : }
     477             : 
     478          45 : void V8ConsoleMessage::contextDestroyed(int contextId) {
     479          60 :   if (contextId != m_contextId) return;
     480          30 :   m_contextId = 0;
     481          30 :   if (m_message.isEmpty()) m_message = "<message collected>";
     482          30 :   Arguments empty;
     483             :   m_arguments.swap(empty);
     484          30 :   m_v8Size = 0;
     485             : }
     486             : 
     487             : // ------------------------ V8ConsoleMessageStorage ----------------------------
     488             : 
     489         463 : V8ConsoleMessageStorage::V8ConsoleMessageStorage(V8InspectorImpl* inspector,
     490             :                                                  int contextGroupId)
     491         926 :     : m_inspector(inspector), m_contextGroupId(contextGroupId) {}
     492             : 
     493         926 : V8ConsoleMessageStorage::~V8ConsoleMessageStorage() { clear(); }
     494             : 
     495             : namespace {
     496             : 
     497        7125 : void TraceV8ConsoleMessageEvent(V8MessageOrigin origin, ConsoleAPIType type) {
     498             :   // Change in this function requires adjustment of Catapult/Telemetry metric
     499             :   // tracing/tracing/metrics/console_error_metric.html.
     500             :   // See https://crbug.com/880432
     501        7125 :   if (origin == V8MessageOrigin::kException) {
     502         740 :     TRACE_EVENT_INSTANT0("v8.console", "V8ConsoleMessage::Exception",
     503             :                          TRACE_EVENT_SCOPE_THREAD);
     504        6755 :   } else if (type == ConsoleAPIType::kError) {
     505         120 :     TRACE_EVENT_INSTANT0("v8.console", "V8ConsoleMessage::Error",
     506             :                          TRACE_EVENT_SCOPE_THREAD);
     507        6695 :   } else if (type == ConsoleAPIType::kAssert) {
     508         160 :     TRACE_EVENT_INSTANT0("v8.console", "V8ConsoleMessage::Assert",
     509             :                          TRACE_EVENT_SCOPE_THREAD);
     510             :   }
     511        7125 : }
     512             : 
     513             : }  // anonymous namespace
     514             : 
     515        7125 : void V8ConsoleMessageStorage::addMessage(
     516             :     std::unique_ptr<V8ConsoleMessage> message) {
     517        7125 :   int contextGroupId = m_contextGroupId;
     518        7125 :   V8InspectorImpl* inspector = m_inspector;
     519        7125 :   if (message->type() == ConsoleAPIType::kClear) clear();
     520             : 
     521        7125 :   TraceV8ConsoleMessageEvent(message->origin(), message->type());
     522             : 
     523        7125 :   inspector->forEachSession(
     524       14420 :       contextGroupId, [&message](V8InspectorSessionImpl* session) {
     525        7210 :         if (message->origin() == V8MessageOrigin::kConsole)
     526        6705 :           session->consoleAgent()->messageAdded(message.get());
     527        7210 :         session->runtimeAgent()->messageAdded(message.get());
     528       14335 :       });
     529        7125 :   if (!inspector->hasConsoleMessageStorage(contextGroupId)) return;
     530             : 
     531             :   DCHECK(m_messages.size() <= maxConsoleMessageCount);
     532        7125 :   if (m_messages.size() == maxConsoleMessageCount) {
     533          50 :     m_estimatedSize -= m_messages.front()->estimatedSize();
     534          50 :     m_messages.pop_front();
     535             :   }
     536       29265 :   while (m_estimatedSize + message->estimatedSize() > maxConsoleMessageV8Size &&
     537             :          !m_messages.empty()) {
     538        5005 :     m_estimatedSize -= m_messages.front()->estimatedSize();
     539        5005 :     m_messages.pop_front();
     540             :   }
     541             : 
     542        7125 :   m_messages.push_back(std::move(message));
     543        7125 :   m_estimatedSize += m_messages.back()->estimatedSize();
     544             : }
     545             : 
     546         518 : void V8ConsoleMessageStorage::clear() {
     547         518 :   m_messages.clear();
     548         518 :   m_estimatedSize = 0;
     549        1036 :   m_inspector->forEachSession(m_contextGroupId,
     550          55 :                               [](V8InspectorSessionImpl* session) {
     551         110 :                                 session->releaseObjectGroup("console");
     552         573 :                               });
     553             :   m_data.clear();
     554         518 : }
     555             : 
     556           0 : bool V8ConsoleMessageStorage::shouldReportDeprecationMessage(
     557             :     int contextId, const String16& method) {
     558             :   std::set<String16>& reportedDeprecationMessages =
     559           0 :       m_data[contextId].m_reportedDeprecationMessages;
     560             :   auto it = reportedDeprecationMessages.find(method);
     561           0 :   if (it != reportedDeprecationMessages.end()) return false;
     562             :   reportedDeprecationMessages.insert(it, method);
     563           0 :   return true;
     564             : }
     565             : 
     566          85 : int V8ConsoleMessageStorage::count(int contextId, const String16& id) {
     567          85 :   return ++m_data[contextId].m_count[id];
     568             : }
     569             : 
     570          65 : void V8ConsoleMessageStorage::time(int contextId, const String16& id) {
     571          65 :   m_data[contextId].m_time[id] = m_inspector->client()->currentTimeMS();
     572          65 : }
     573             : 
     574          20 : bool V8ConsoleMessageStorage::countReset(int contextId, const String16& id) {
     575          20 :   std::map<String16, int>& count_map = m_data[contextId].m_count;
     576          20 :   if (count_map.find(id) == count_map.end()) return false;
     577             : 
     578          15 :   count_map[id] = 0;
     579          15 :   return true;
     580             : }
     581             : 
     582          10 : double V8ConsoleMessageStorage::timeLog(int contextId, const String16& id) {
     583          10 :   std::map<String16, double>& time = m_data[contextId].m_time;
     584             :   auto it = time.find(id);
     585          10 :   if (it == time.end()) return 0.0;
     586          10 :   return m_inspector->client()->currentTimeMS() - it->second;
     587             : }
     588             : 
     589          65 : double V8ConsoleMessageStorage::timeEnd(int contextId, const String16& id) {
     590          65 :   std::map<String16, double>& time = m_data[contextId].m_time;
     591             :   auto it = time.find(id);
     592          65 :   if (it == time.end()) return 0.0;
     593          65 :   double elapsed = m_inspector->client()->currentTimeMS() - it->second;
     594             :   time.erase(it);
     595          65 :   return elapsed;
     596             : }
     597             : 
     598         155 : bool V8ConsoleMessageStorage::hasTimer(int contextId, const String16& id) {
     599         155 :   const std::map<String16, double>& time = m_data[contextId].m_time;
     600         155 :   return time.find(id) != time.end();
     601             : }
     602             : 
     603          20 : void V8ConsoleMessageStorage::contextDestroyed(int contextId) {
     604          20 :   m_estimatedSize = 0;
     605         110 :   for (size_t i = 0; i < m_messages.size(); ++i) {
     606          90 :     m_messages[i]->contextDestroyed(contextId);
     607          45 :     m_estimatedSize += m_messages[i]->estimatedSize();
     608             :   }
     609             :   auto it = m_data.find(contextId);
     610          20 :   if (it != m_data.end()) m_data.erase(contextId);
     611          20 : }
     612             : 
     613             : }  // namespace v8_inspector

Generated by: LCOV version 1.10