v8-console-agent-impl.cc 2.43 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/inspector/v8-console-agent-impl.h"
6 7

#include "src/inspector/protocol/Protocol.h"
8 9 10 11
#include "src/inspector/v8-console-message.h"
#include "src/inspector/v8-inspector-impl.h"
#include "src/inspector/v8-inspector-session-impl.h"
#include "src/inspector/v8-stack-trace-impl.h"
12 13 14 15 16

namespace v8_inspector {

namespace ConsoleAgentState {
static const char consoleEnabled[] = "consoleEnabled";
17
}  // namespace ConsoleAgentState
18 19 20 21 22 23 24 25 26

V8ConsoleAgentImpl::V8ConsoleAgentImpl(
    V8InspectorSessionImpl* session, protocol::FrontendChannel* frontendChannel,
    protocol::DictionaryValue* state)
    : m_session(session),
      m_state(state),
      m_frontend(frontendChannel),
      m_enabled(false) {}

27
V8ConsoleAgentImpl::~V8ConsoleAgentImpl() = default;
28

29
Response V8ConsoleAgentImpl::enable() {
30
  if (m_enabled) return Response::Success();
31 32 33
  m_state->setBoolean(ConsoleAgentState::consoleEnabled, true);
  m_enabled = true;
  reportAllMessages();
34
  return Response::Success();
35 36
}

37
Response V8ConsoleAgentImpl::disable() {
38
  if (!m_enabled) return Response::Success();
39 40
  m_state->setBoolean(ConsoleAgentState::consoleEnabled, false);
  m_enabled = false;
41
  return Response::Success();
42 43
}

44
Response V8ConsoleAgentImpl::clearMessages() { return Response::Success(); }
45 46 47 48

void V8ConsoleAgentImpl::restore() {
  if (!m_state->booleanProperty(ConsoleAgentState::consoleEnabled, false))
    return;
49
  enable();
50 51 52 53 54 55 56 57 58 59 60 61 62
}

void V8ConsoleAgentImpl::messageAdded(V8ConsoleMessage* message) {
  if (m_enabled) reportMessage(message, true);
}

bool V8ConsoleAgentImpl::enabled() { return m_enabled; }

void V8ConsoleAgentImpl::reportAllMessages() {
  V8ConsoleMessageStorage* storage =
      m_session->inspector()->ensureConsoleMessageStorage(
          m_session->contextGroupId());
  for (const auto& message : storage->messages()) {
63 64 65
    if (message->origin() == V8MessageOrigin::kConsole) {
      if (!reportMessage(message.get(), false)) return;
    }
66 67 68
  }
}

69
bool V8ConsoleAgentImpl::reportMessage(V8ConsoleMessage* message,
70
                                       bool generatePreview) {
71
  DCHECK_EQ(V8MessageOrigin::kConsole, message->origin());
72 73
  message->reportToFrontend(&m_frontend);
  m_frontend.flush();
74 75
  return m_session->inspector()->hasConsoleMessageStorage(
      m_session->contextGroupId());
76 77 78
}

}  // namespace v8_inspector