V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
v8-console-agent-impl.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-agent-impl.h"
6 
7 #include "src/inspector/protocol/Protocol.h"
8 #include "src/inspector/v8-console-message.h"
9 #include "src/inspector/v8-inspector-impl.h"
10 #include "src/inspector/v8-inspector-session-impl.h"
11 #include "src/inspector/v8-stack-trace-impl.h"
12 
13 namespace v8_inspector {
14 
15 namespace ConsoleAgentState {
16 static const char consoleEnabled[] = "consoleEnabled";
17 }
18 
19 V8ConsoleAgentImpl::V8ConsoleAgentImpl(
20  V8InspectorSessionImpl* session, protocol::FrontendChannel* frontendChannel,
21  protocol::DictionaryValue* state)
22  : m_session(session),
23  m_state(state),
24  m_frontend(frontendChannel),
25  m_enabled(false) {}
26 
27 V8ConsoleAgentImpl::~V8ConsoleAgentImpl() = default;
28 
29 Response V8ConsoleAgentImpl::enable() {
30  if (m_enabled) return Response::OK();
31  m_state->setBoolean(ConsoleAgentState::consoleEnabled, true);
32  m_enabled = true;
33  m_session->inspector()->enableStackCapturingIfNeeded();
34  reportAllMessages();
35  return Response::OK();
36 }
37 
38 Response V8ConsoleAgentImpl::disable() {
39  if (!m_enabled) return Response::OK();
40  m_session->inspector()->disableStackCapturingIfNeeded();
41  m_state->setBoolean(ConsoleAgentState::consoleEnabled, false);
42  m_enabled = false;
43  return Response::OK();
44 }
45 
46 Response V8ConsoleAgentImpl::clearMessages() { return Response::OK(); }
47 
48 void V8ConsoleAgentImpl::restore() {
49  if (!m_state->booleanProperty(ConsoleAgentState::consoleEnabled, false))
50  return;
51  enable();
52 }
53 
54 void V8ConsoleAgentImpl::messageAdded(V8ConsoleMessage* message) {
55  if (m_enabled) reportMessage(message, true);
56 }
57 
58 bool V8ConsoleAgentImpl::enabled() { return m_enabled; }
59 
60 void V8ConsoleAgentImpl::reportAllMessages() {
61  V8ConsoleMessageStorage* storage =
62  m_session->inspector()->ensureConsoleMessageStorage(
63  m_session->contextGroupId());
64  for (const auto& message : storage->messages()) {
65  if (message->origin() == V8MessageOrigin::kConsole) {
66  if (!reportMessage(message.get(), false)) return;
67  }
68  }
69 }
70 
71 bool V8ConsoleAgentImpl::reportMessage(V8ConsoleMessage* message,
72  bool generatePreview) {
73  DCHECK_EQ(V8MessageOrigin::kConsole, message->origin());
74  message->reportToFrontend(&m_frontend);
75  m_frontend.flush();
76  return m_session->inspector()->hasConsoleMessageStorage(
77  m_session->contextGroupId());
78 }
79 
80 } // namespace v8_inspector