11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/inspector/v8-console-agent-impl.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/inspector/protocol/Protocol.h"
81cb0ef41Sopenharmony_ci#include "src/inspector/v8-console-message.h"
91cb0ef41Sopenharmony_ci#include "src/inspector/v8-inspector-impl.h"
101cb0ef41Sopenharmony_ci#include "src/inspector/v8-inspector-session-impl.h"
111cb0ef41Sopenharmony_ci#include "src/inspector/v8-stack-trace-impl.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8_inspector {
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cinamespace ConsoleAgentState {
161cb0ef41Sopenharmony_cistatic const char consoleEnabled[] = "consoleEnabled";
171cb0ef41Sopenharmony_ci}  // namespace ConsoleAgentState
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciV8ConsoleAgentImpl::V8ConsoleAgentImpl(
201cb0ef41Sopenharmony_ci    V8InspectorSessionImpl* session, protocol::FrontendChannel* frontendChannel,
211cb0ef41Sopenharmony_ci    protocol::DictionaryValue* state)
221cb0ef41Sopenharmony_ci    : m_session(session),
231cb0ef41Sopenharmony_ci      m_state(state),
241cb0ef41Sopenharmony_ci      m_frontend(frontendChannel),
251cb0ef41Sopenharmony_ci      m_enabled(false) {}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciV8ConsoleAgentImpl::~V8ConsoleAgentImpl() = default;
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciResponse V8ConsoleAgentImpl::enable() {
301cb0ef41Sopenharmony_ci  if (m_enabled) return Response::Success();
311cb0ef41Sopenharmony_ci  m_state->setBoolean(ConsoleAgentState::consoleEnabled, true);
321cb0ef41Sopenharmony_ci  m_enabled = true;
331cb0ef41Sopenharmony_ci  reportAllMessages();
341cb0ef41Sopenharmony_ci  return Response::Success();
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciResponse V8ConsoleAgentImpl::disable() {
381cb0ef41Sopenharmony_ci  if (!m_enabled) return Response::Success();
391cb0ef41Sopenharmony_ci  m_state->setBoolean(ConsoleAgentState::consoleEnabled, false);
401cb0ef41Sopenharmony_ci  m_enabled = false;
411cb0ef41Sopenharmony_ci  return Response::Success();
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciResponse V8ConsoleAgentImpl::clearMessages() { return Response::Success(); }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_civoid V8ConsoleAgentImpl::restore() {
471cb0ef41Sopenharmony_ci  if (!m_state->booleanProperty(ConsoleAgentState::consoleEnabled, false))
481cb0ef41Sopenharmony_ci    return;
491cb0ef41Sopenharmony_ci  enable();
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_civoid V8ConsoleAgentImpl::messageAdded(V8ConsoleMessage* message) {
531cb0ef41Sopenharmony_ci  if (m_enabled) reportMessage(message, true);
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cibool V8ConsoleAgentImpl::enabled() { return m_enabled; }
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_civoid V8ConsoleAgentImpl::reportAllMessages() {
591cb0ef41Sopenharmony_ci  V8ConsoleMessageStorage* storage =
601cb0ef41Sopenharmony_ci      m_session->inspector()->ensureConsoleMessageStorage(
611cb0ef41Sopenharmony_ci          m_session->contextGroupId());
621cb0ef41Sopenharmony_ci  for (const auto& message : storage->messages()) {
631cb0ef41Sopenharmony_ci    if (message->origin() == V8MessageOrigin::kConsole) {
641cb0ef41Sopenharmony_ci      if (!reportMessage(message.get(), false)) return;
651cb0ef41Sopenharmony_ci    }
661cb0ef41Sopenharmony_ci  }
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_cibool V8ConsoleAgentImpl::reportMessage(V8ConsoleMessage* message,
701cb0ef41Sopenharmony_ci                                       bool generatePreview) {
711cb0ef41Sopenharmony_ci  DCHECK_EQ(V8MessageOrigin::kConsole, message->origin());
721cb0ef41Sopenharmony_ci  message->reportToFrontend(&m_frontend);
731cb0ef41Sopenharmony_ci  m_frontend.flush();
741cb0ef41Sopenharmony_ci  return m_session->inspector()->hasConsoleMessageStorage(
751cb0ef41Sopenharmony_ci      m_session->contextGroupId());
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci}  // namespace v8_inspector
79