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 } // namespace ConsoleAgentState
18
V8ConsoleAgentImpl(V8InspectorSessionImpl * session,protocol::FrontendChannel * frontendChannel,protocol::DictionaryValue * state)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
enable()29 Response V8ConsoleAgentImpl::enable() {
30 if (m_enabled) return Response::Success();
31 m_state->setBoolean(ConsoleAgentState::consoleEnabled, true);
32 m_enabled = true;
33 reportAllMessages();
34 return Response::Success();
35 }
36
disable()37 Response V8ConsoleAgentImpl::disable() {
38 if (!m_enabled) return Response::Success();
39 m_state->setBoolean(ConsoleAgentState::consoleEnabled, false);
40 m_enabled = false;
41 return Response::Success();
42 }
43
clearMessages()44 Response V8ConsoleAgentImpl::clearMessages() { return Response::Success(); }
45
restore()46 void V8ConsoleAgentImpl::restore() {
47 if (!m_state->booleanProperty(ConsoleAgentState::consoleEnabled, false))
48 return;
49 enable();
50 }
51
messageAdded(V8ConsoleMessage * message)52 void V8ConsoleAgentImpl::messageAdded(V8ConsoleMessage* message) {
53 if (m_enabled) reportMessage(message, true);
54 }
55
enabled()56 bool V8ConsoleAgentImpl::enabled() { return m_enabled; }
57
reportAllMessages()58 void V8ConsoleAgentImpl::reportAllMessages() {
59 V8ConsoleMessageStorage* storage =
60 m_session->inspector()->ensureConsoleMessageStorage(
61 m_session->contextGroupId());
62 for (const auto& message : storage->messages()) {
63 if (message->origin() == V8MessageOrigin::kConsole) {
64 if (!reportMessage(message.get(), false)) return;
65 }
66 }
67 }
68
reportMessage(V8ConsoleMessage * message,bool generatePreview)69 bool V8ConsoleAgentImpl::reportMessage(V8ConsoleMessage* message,
70 bool generatePreview) {
71 DCHECK_EQ(V8MessageOrigin::kConsole, message->origin());
72 message->reportToFrontend(&m_frontend);
73 m_frontend.flush();
74 return m_session->inspector()->hasConsoleMessageStorage(
75 m_session->contextGroupId());
76 }
77
78 } // namespace v8_inspector
79