V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
v8-console-message.cc
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 String16 consoleAPITypeValue(ConsoleAPIType type) {
25  switch (type) {
26  case ConsoleAPIType::kLog:
27  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Log;
28  case ConsoleAPIType::kDebug:
29  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Debug;
30  case ConsoleAPIType::kInfo:
31  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Info;
32  case ConsoleAPIType::kError:
33  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Error;
34  case ConsoleAPIType::kWarning:
35  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Warning;
36  case ConsoleAPIType::kClear:
37  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Clear;
38  case ConsoleAPIType::kDir:
39  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Dir;
40  case ConsoleAPIType::kDirXML:
41  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Dirxml;
42  case ConsoleAPIType::kTable:
43  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Table;
44  case ConsoleAPIType::kTrace:
45  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Trace;
46  case ConsoleAPIType::kStartGroup:
47  return protocol::Runtime::ConsoleAPICalled::TypeEnum::StartGroup;
48  case ConsoleAPIType::kStartGroupCollapsed:
49  return protocol::Runtime::ConsoleAPICalled::TypeEnum::StartGroupCollapsed;
50  case ConsoleAPIType::kEndGroup:
51  return protocol::Runtime::ConsoleAPICalled::TypeEnum::EndGroup;
52  case ConsoleAPIType::kAssert:
53  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Assert;
54  case ConsoleAPIType::kTimeEnd:
55  return protocol::Runtime::ConsoleAPICalled::TypeEnum::TimeEnd;
56  case ConsoleAPIType::kCount:
57  return protocol::Runtime::ConsoleAPICalled::TypeEnum::Count;
58  }
59  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 class V8ValueStringBuilder {
69  public:
70  static String16 toString(v8::Local<v8::Value> value,
71  v8::Local<v8::Context> context) {
72  V8ValueStringBuilder builder(context);
73  if (!builder.append(value)) return String16();
74  return builder.toString();
75  }
76 
77  private:
78  enum {
79  IgnoreNull = 1 << 0,
80  IgnoreUndefined = 1 << 1,
81  };
82 
83  explicit V8ValueStringBuilder(v8::Local<v8::Context> context)
84  : m_arrayLimit(maxArrayItemsLimit),
85  m_isolate(context->GetIsolate()),
86  m_tryCatch(context->GetIsolate()),
87  m_context(context) {}
88 
89  bool append(v8::Local<v8::Value> value, unsigned ignoreOptions = 0) {
90  if (value.IsEmpty()) return true;
91  if ((ignoreOptions & IgnoreNull) && value->IsNull()) return true;
92  if ((ignoreOptions & IgnoreUndefined) && value->IsUndefined()) return true;
93  if (value->IsString()) return append(v8::Local<v8::String>::Cast(value));
94  if (value->IsStringObject())
95  return append(v8::Local<v8::StringObject>::Cast(value)->ValueOf());
96  if (value->IsBigInt()) return append(v8::Local<v8::BigInt>::Cast(value));
97  if (value->IsBigIntObject())
98  return append(v8::Local<v8::BigIntObject>::Cast(value)->ValueOf());
99  if (value->IsSymbol()) return append(v8::Local<v8::Symbol>::Cast(value));
100  if (value->IsSymbolObject())
101  return append(v8::Local<v8::SymbolObject>::Cast(value)->ValueOf());
102  if (value->IsNumberObject()) {
103  m_builder.append(String16::fromDouble(
104  v8::Local<v8::NumberObject>::Cast(value)->ValueOf(), 6));
105  return true;
106  }
107  if (value->IsBooleanObject()) {
108  m_builder.append(v8::Local<v8::BooleanObject>::Cast(value)->ValueOf()
109  ? "true"
110  : "false");
111  return true;
112  }
113  if (value->IsArray()) return append(v8::Local<v8::Array>::Cast(value));
114  if (value->IsProxy()) {
115  m_builder.append("[object Proxy]");
116  return true;
117  }
118  if (value->IsObject() && !value->IsDate() && !value->IsFunction() &&
119  !value->IsNativeError() && !value->IsRegExp()) {
121  v8::Local<v8::String> stringValue;
122  if (object->ObjectProtoToString(m_context).ToLocal(&stringValue))
123  return append(stringValue);
124  }
125  v8::Local<v8::String> stringValue;
126  if (!value->ToString(m_context).ToLocal(&stringValue)) return false;
127  return append(stringValue);
128  }
129 
130  bool append(v8::Local<v8::Array> array) {
131  for (const auto& it : m_visitedArrays) {
132  if (it == array) return true;
133  }
134  uint32_t length = array->Length();
135  if (length > m_arrayLimit) return false;
136  if (m_visitedArrays.size() > maxStackDepthLimit) return false;
137 
138  bool result = true;
139  m_arrayLimit -= length;
140  m_visitedArrays.push_back(array);
141  for (uint32_t i = 0; i < length; ++i) {
142  if (i) m_builder.append(',');
143  v8::Local<v8::Value> value;
144  if (!array->Get(m_context, i).ToLocal(&value)) continue;
145  if (!append(value, IgnoreNull | IgnoreUndefined)) {
146  result = false;
147  break;
148  }
149  }
150  m_visitedArrays.pop_back();
151  return result;
152  }
153 
154  bool append(v8::Local<v8::Symbol> symbol) {
155  m_builder.append("Symbol(");
156  bool result = append(symbol->Name(), IgnoreUndefined);
157  m_builder.append(')');
158  return result;
159  }
160 
161  bool append(v8::Local<v8::BigInt> bigint) {
162  v8::Local<v8::String> bigint_string;
163  if (!bigint->ToString(m_context).ToLocal(&bigint_string)) return false;
164  bool result = append(bigint_string);
165  if (m_tryCatch.HasCaught()) return false;
166  m_builder.append('n');
167  return result;
168  }
169 
170  bool append(v8::Local<v8::String> string) {
171  if (m_tryCatch.HasCaught()) return false;
172  if (!string.IsEmpty()) {
173  m_builder.append(toProtocolString(m_isolate, string));
174  }
175  return true;
176  }
177 
178  String16 toString() {
179  if (m_tryCatch.HasCaught()) return String16();
180  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 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  m_revokedExceptionId(0) {}
205 
206 V8ConsoleMessage::~V8ConsoleMessage() = default;
207 
208 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  m_lineNumber = lineNumber;
214  m_columnNumber = columnNumber;
215  m_stackTrace = std::move(stackTrace);
216  m_scriptId = scriptId;
217 }
218 
219 void V8ConsoleMessage::reportToFrontend(
220  protocol::Console::Frontend* frontend) const {
221  DCHECK_EQ(V8MessageOrigin::kConsole, m_origin);
222  String16 level = protocol::Console::ConsoleMessage::LevelEnum::Log;
223  if (m_type == ConsoleAPIType::kDebug || m_type == ConsoleAPIType::kCount ||
224  m_type == ConsoleAPIType::kTimeEnd)
225  level = protocol::Console::ConsoleMessage::LevelEnum::Debug;
226  else if (m_type == ConsoleAPIType::kError ||
227  m_type == ConsoleAPIType::kAssert)
228  level = protocol::Console::ConsoleMessage::LevelEnum::Error;
229  else if (m_type == ConsoleAPIType::kWarning)
230  level = protocol::Console::ConsoleMessage::LevelEnum::Warning;
231  else if (m_type == ConsoleAPIType::kInfo)
232  level = protocol::Console::ConsoleMessage::LevelEnum::Info;
233  std::unique_ptr<protocol::Console::ConsoleMessage> result =
234  protocol::Console::ConsoleMessage::create()
235  .setSource(protocol::Console::ConsoleMessage::SourceEnum::ConsoleApi)
236  .setLevel(level)
237  .setText(m_message)
238  .build();
239  result->setLine(static_cast<int>(m_lineNumber));
240  result->setColumn(static_cast<int>(m_columnNumber));
241  result->setUrl(m_url);
242  frontend->messageAdded(std::move(result));
243 }
244 
245 std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>>
246 V8ConsoleMessage::wrapArguments(V8InspectorSessionImpl* session,
247  bool generatePreview) const {
248  V8InspectorImpl* inspector = session->inspector();
249  int contextGroupId = session->contextGroupId();
250  int contextId = m_contextId;
251  if (!m_arguments.size() || !contextId) return nullptr;
252  InspectedContext* inspectedContext =
253  inspector->getContext(contextGroupId, contextId);
254  if (!inspectedContext) return nullptr;
255 
256  v8::Isolate* isolate = inspectedContext->isolate();
257  v8::HandleScope handles(isolate);
258  v8::Local<v8::Context> context = inspectedContext->context();
259 
260  std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>> args =
261  protocol::Array<protocol::Runtime::RemoteObject>::create();
262 
263  v8::Local<v8::Value> value = m_arguments[0]->Get(isolate);
264  if (value->IsObject() && m_type == ConsoleAPIType::kTable &&
265  generatePreview) {
267  if (m_arguments.size() > 1) {
268  v8::Local<v8::Value> secondArgument = m_arguments[1]->Get(isolate);
269  if (secondArgument->IsArray()) {
270  columns = v8::Local<v8::Array>::Cast(secondArgument);
271  } else if (secondArgument->IsString()) {
272  v8::TryCatch tryCatch(isolate);
273  v8::Local<v8::Array> array = v8::Array::New(isolate);
274  if (array->Set(context, 0, secondArgument).IsJust()) {
275  columns = array;
276  }
277  }
278  }
279  std::unique_ptr<protocol::Runtime::RemoteObject> wrapped =
280  session->wrapTable(context, v8::Local<v8::Object>::Cast(value),
281  columns);
282  inspectedContext = inspector->getContext(contextGroupId, contextId);
283  if (!inspectedContext) return nullptr;
284  if (wrapped) {
285  args->addItem(std::move(wrapped));
286  } else {
287  args = nullptr;
288  }
289  } else {
290  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  generatePreview);
294  inspectedContext = inspector->getContext(contextGroupId, contextId);
295  if (!inspectedContext) return nullptr;
296  if (!wrapped) {
297  args = nullptr;
298  break;
299  }
300  args->addItem(std::move(wrapped));
301  }
302  }
303  return args;
304 }
305 
306 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  if (m_origin == V8MessageOrigin::kException) {
313  std::unique_ptr<protocol::Runtime::RemoteObject> exception =
314  wrapException(session, generatePreview);
315  if (!inspector->hasConsoleMessageStorage(contextGroupId)) return;
316  std::unique_ptr<protocol::Runtime::ExceptionDetails> exceptionDetails =
317  protocol::Runtime::ExceptionDetails::create()
318  .setExceptionId(m_exceptionId)
319  .setText(exception ? m_message : m_detailedMessage)
320  .setLineNumber(m_lineNumber ? m_lineNumber - 1 : 0)
321  .setColumnNumber(m_columnNumber ? m_columnNumber - 1 : 0)
322  .build();
323  if (m_scriptId)
324  exceptionDetails->setScriptId(String16::fromInteger(m_scriptId));
325  if (!m_url.isEmpty()) exceptionDetails->setUrl(m_url);
326  if (m_stackTrace) {
327  exceptionDetails->setStackTrace(
328  m_stackTrace->buildInspectorObjectImpl(inspector->debugger()));
329  }
330  if (m_contextId) exceptionDetails->setExecutionContextId(m_contextId);
331  if (exception) exceptionDetails->setException(std::move(exception));
332  frontend->exceptionThrown(m_timestamp, std::move(exceptionDetails));
333  return;
334  }
335  if (m_origin == V8MessageOrigin::kRevokedException) {
336  frontend->exceptionRevoked(m_message, m_revokedExceptionId);
337  return;
338  }
339  if (m_origin == V8MessageOrigin::kConsole) {
340  std::unique_ptr<protocol::Array<protocol::Runtime::RemoteObject>>
341  arguments = wrapArguments(session, generatePreview);
342  if (!inspector->hasConsoleMessageStorage(contextGroupId)) return;
343  if (!arguments) {
344  arguments = protocol::Array<protocol::Runtime::RemoteObject>::create();
345  if (!m_message.isEmpty()) {
346  std::unique_ptr<protocol::Runtime::RemoteObject> messageArg =
347  protocol::Runtime::RemoteObject::create()
348  .setType(protocol::Runtime::RemoteObject::TypeEnum::String)
349  .build();
350  messageArg->setValue(protocol::StringValue::create(m_message));
351  arguments->addItem(std::move(messageArg));
352  }
353  }
354  Maybe<String16> consoleContext;
355  if (!m_consoleContext.isEmpty()) consoleContext = m_consoleContext;
356  frontend->consoleAPICalled(
357  consoleAPITypeValue(m_type), std::move(arguments), m_contextId,
358  m_timestamp,
359  m_stackTrace
360  ? m_stackTrace->buildInspectorObjectImpl(inspector->debugger())
361  : nullptr,
362  std::move(consoleContext));
363  return;
364  }
365  UNREACHABLE();
366 }
367 
368 std::unique_ptr<protocol::Runtime::RemoteObject>
369 V8ConsoleMessage::wrapException(V8InspectorSessionImpl* session,
370  bool generatePreview) const {
371  if (!m_arguments.size() || !m_contextId) return nullptr;
372  DCHECK_EQ(1u, m_arguments.size());
373  InspectedContext* inspectedContext =
374  session->inspector()->getContext(session->contextGroupId(), m_contextId);
375  if (!inspectedContext) return nullptr;
376 
377  v8::Isolate* isolate = inspectedContext->isolate();
378  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  generatePreview);
383 }
384 
385 V8MessageOrigin V8ConsoleMessage::origin() const { return m_origin; }
386 
387 ConsoleAPIType V8ConsoleMessage::type() const { return m_type; }
388 
389 // static
390 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  v8::Isolate* isolate = v8Context->GetIsolate();
397 
398  std::unique_ptr<V8ConsoleMessage> message(
399  new V8ConsoleMessage(V8MessageOrigin::kConsole, timestamp, String16()));
400  if (stackTrace && !stackTrace->isEmpty()) {
401  message->m_url = toString16(stackTrace->topSourceURL());
402  message->m_lineNumber = stackTrace->topLineNumber();
403  message->m_columnNumber = stackTrace->topColumnNumber();
404  }
405  message->m_stackTrace = std::move(stackTrace);
406  message->m_consoleContext = consoleContext;
407  message->m_type = type;
408  message->m_contextId = contextId;
409  for (size_t i = 0; i < arguments.size(); ++i) {
410  std::unique_ptr<v8::Global<v8::Value>> argument(
411  new v8::Global<v8::Value>(isolate, arguments.at(i)));
412  argument->AnnotateStrongRetainer(kGlobalConsoleMessageHandleLabel);
413  message->m_arguments.push_back(std::move(argument));
414  message->m_v8Size +=
415  v8::debug::EstimatedValueSize(isolate, arguments.at(i));
416  }
417  if (arguments.size())
418  message->m_message =
419  V8ValueStringBuilder::toString(arguments[0], v8Context);
420 
421  v8::Isolate::MessageErrorLevel clientLevel = v8::Isolate::kMessageInfo;
422  if (type == ConsoleAPIType::kDebug || type == ConsoleAPIType::kCount ||
423  type == ConsoleAPIType::kTimeEnd) {
424  clientLevel = v8::Isolate::kMessageDebug;
425  } else if (type == ConsoleAPIType::kError ||
426  type == ConsoleAPIType::kAssert) {
427  clientLevel = v8::Isolate::kMessageError;
428  } 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  if (type != ConsoleAPIType::kClear) {
435  inspector->client()->consoleAPIMessage(
436  groupId, clientLevel, toStringView(message->m_message),
437  toStringView(message->m_url), message->m_lineNumber,
438  message->m_columnNumber, message->m_stackTrace.get());
439  }
440 
441  return message;
442 }
443 
444 // static
445 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  new V8ConsoleMessage(V8MessageOrigin::kException, timestamp, message));
453  consoleMessage->setLocation(url, lineNumber, columnNumber,
454  std::move(stackTrace), scriptId);
455  consoleMessage->m_exceptionId = exceptionId;
456  consoleMessage->m_detailedMessage = detailedMessage;
457  if (contextId && !exception.IsEmpty()) {
458  consoleMessage->m_contextId = contextId;
459  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  v8::debug::EstimatedValueSize(isolate, exception);
464  }
465  return consoleMessage;
466 }
467 
468 // static
469 std::unique_ptr<V8ConsoleMessage> V8ConsoleMessage::createForRevokedException(
470  double timestamp, const String16& messageText,
471  unsigned revokedExceptionId) {
472  std::unique_ptr<V8ConsoleMessage> message(new V8ConsoleMessage(
473  V8MessageOrigin::kRevokedException, timestamp, messageText));
474  message->m_revokedExceptionId = revokedExceptionId;
475  return message;
476 }
477 
478 void V8ConsoleMessage::contextDestroyed(int contextId) {
479  if (contextId != m_contextId) return;
480  m_contextId = 0;
481  if (m_message.isEmpty()) m_message = "<message collected>";
482  Arguments empty;
483  m_arguments.swap(empty);
484  m_v8Size = 0;
485 }
486 
487 // ------------------------ V8ConsoleMessageStorage ----------------------------
488 
489 V8ConsoleMessageStorage::V8ConsoleMessageStorage(V8InspectorImpl* inspector,
490  int contextGroupId)
491  : m_inspector(inspector), m_contextGroupId(contextGroupId) {}
492 
493 V8ConsoleMessageStorage::~V8ConsoleMessageStorage() { clear(); }
494 
495 namespace {
496 
497 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  if (origin == V8MessageOrigin::kException) {
502  TRACE_EVENT_INSTANT0("v8.console", "V8ConsoleMessage::Exception",
503  TRACE_EVENT_SCOPE_THREAD);
504  } else if (type == ConsoleAPIType::kError) {
505  TRACE_EVENT_INSTANT0("v8.console", "V8ConsoleMessage::Error",
506  TRACE_EVENT_SCOPE_THREAD);
507  } else if (type == ConsoleAPIType::kAssert) {
508  TRACE_EVENT_INSTANT0("v8.console", "V8ConsoleMessage::Assert",
509  TRACE_EVENT_SCOPE_THREAD);
510  }
511 }
512 
513 } // anonymous namespace
514 
515 void V8ConsoleMessageStorage::addMessage(
516  std::unique_ptr<V8ConsoleMessage> message) {
517  int contextGroupId = m_contextGroupId;
518  V8InspectorImpl* inspector = m_inspector;
519  if (message->type() == ConsoleAPIType::kClear) clear();
520 
521  TraceV8ConsoleMessageEvent(message->origin(), message->type());
522 
523  inspector->forEachSession(
524  contextGroupId, [&message](V8InspectorSessionImpl* session) {
525  if (message->origin() == V8MessageOrigin::kConsole)
526  session->consoleAgent()->messageAdded(message.get());
527  session->runtimeAgent()->messageAdded(message.get());
528  });
529  if (!inspector->hasConsoleMessageStorage(contextGroupId)) return;
530 
531  DCHECK(m_messages.size() <= maxConsoleMessageCount);
532  if (m_messages.size() == maxConsoleMessageCount) {
533  m_estimatedSize -= m_messages.front()->estimatedSize();
534  m_messages.pop_front();
535  }
536  while (m_estimatedSize + message->estimatedSize() > maxConsoleMessageV8Size &&
537  !m_messages.empty()) {
538  m_estimatedSize -= m_messages.front()->estimatedSize();
539  m_messages.pop_front();
540  }
541 
542  m_messages.push_back(std::move(message));
543  m_estimatedSize += m_messages.back()->estimatedSize();
544 }
545 
546 void V8ConsoleMessageStorage::clear() {
547  m_messages.clear();
548  m_estimatedSize = 0;
549  m_inspector->forEachSession(m_contextGroupId,
550  [](V8InspectorSessionImpl* session) {
551  session->releaseObjectGroup("console");
552  });
553  m_data.clear();
554 }
555 
556 bool V8ConsoleMessageStorage::shouldReportDeprecationMessage(
557  int contextId, const String16& method) {
558  std::set<String16>& reportedDeprecationMessages =
559  m_data[contextId].m_reportedDeprecationMessages;
560  auto it = reportedDeprecationMessages.find(method);
561  if (it != reportedDeprecationMessages.end()) return false;
562  reportedDeprecationMessages.insert(it, method);
563  return true;
564 }
565 
566 int V8ConsoleMessageStorage::count(int contextId, const String16& id) {
567  return ++m_data[contextId].m_count[id];
568 }
569 
570 void V8ConsoleMessageStorage::time(int contextId, const String16& id) {
571  m_data[contextId].m_time[id] = m_inspector->client()->currentTimeMS();
572 }
573 
574 bool V8ConsoleMessageStorage::countReset(int contextId, const String16& id) {
575  std::map<String16, int>& count_map = m_data[contextId].m_count;
576  if (count_map.find(id) == count_map.end()) return false;
577 
578  count_map[id] = 0;
579  return true;
580 }
581 
582 double V8ConsoleMessageStorage::timeLog(int contextId, const String16& id) {
583  std::map<String16, double>& time = m_data[contextId].m_time;
584  auto it = time.find(id);
585  if (it == time.end()) return 0.0;
586  return m_inspector->client()->currentTimeMS() - it->second;
587 }
588 
589 double V8ConsoleMessageStorage::timeEnd(int contextId, const String16& id) {
590  std::map<String16, double>& time = m_data[contextId].m_time;
591  auto it = time.find(id);
592  if (it == time.end()) return 0.0;
593  double elapsed = m_inspector->client()->currentTimeMS() - it->second;
594  time.erase(it);
595  return elapsed;
596 }
597 
598 bool V8ConsoleMessageStorage::hasTimer(int contextId, const String16& id) {
599  const std::map<String16, double>& time = m_data[contextId].m_time;
600  return time.find(id) != time.end();
601 }
602 
603 void V8ConsoleMessageStorage::contextDestroyed(int contextId) {
604  m_estimatedSize = 0;
605  for (size_t i = 0; i < m_messages.size(); ++i) {
606  m_messages[i]->contextDestroyed(contextId);
607  m_estimatedSize += m_messages[i]->estimatedSize();
608  }
609  auto it = m_data.find(contextId);
610  if (it != m_data.end()) m_data.erase(contextId);
611 }
612 
613 } // namespace v8_inspector
V8_INLINE bool IsString() const
Definition: v8.h:10016
static V8_INLINE Local< T > Cast(Local< S > that)
Definition: v8.h:251
bool IsArray() const
Definition: api.cc:3343
V8_INLINE bool IsEmpty() const
Definition: v8.h:195
static Local< Array > New(Isolate *isolate, int length=0)
Definition: api.cc:6966
V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local< S > *out) const
Definition: v8.h:347
V8_WARN_UNUSED_RESULT MaybeLocal< String > ObjectProtoToString(Local< Context > context)
Definition: api.cc:4434
Local< Value > Name() const
Definition: api.cc:5718