1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 10:01:13 +09:00
ladybird/Services/WebContent/WebContentConsoleClient.cpp
Timothy Flynn 37f07c176a LibWebView+WebContent: Create a different console client for DevTools
Our existing WebContentConsoleClient is very specific to our home-grown
Inspector. It renders console output to an HTML string. For DevTools, we
will not want this behavior; we will want to send representations of raw
JS values.

This patch makes WebContentConsoleClient a base class to handle console
input from the user, either from the Inspector or from DevTools. It then
moves the HTML rendering needed for the Inspector to a new class,
InspectorConsoleClient. And we add a DevToolsConsoleClient (currently
just stubbed) to handle needs specific to DevTools.

We choose at runtime which console client to install, based on the
--devtools command line flag.
2025-02-28 13:08:33 +01:00

86 lines
3.1 KiB
C++

/*
* Copyright (c) 2021, Brandon Scott <xeon.productions@gmail.com>
* Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2024, Gasim Gasimzada <gasim@gasimzada.net>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/ObjectEnvironment.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <WebContent/ConsoleGlobalEnvironmentExtensions.h>
#include <WebContent/PageClient.h>
#include <WebContent/WebContentConsoleClient.h>
namespace WebContent {
GC_DEFINE_ALLOCATOR(WebContentConsoleClient);
WebContentConsoleClient::WebContentConsoleClient(JS::Realm& realm, JS::Console& console, PageClient& client, ConsoleGlobalEnvironmentExtensions& console_global_environment_extensions)
: ConsoleClient(console)
, m_realm(realm)
, m_client(client)
, m_console_global_environment_extensions(console_global_environment_extensions)
{
}
WebContentConsoleClient::~WebContentConsoleClient() = default;
void WebContentConsoleClient::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_realm);
visitor.visit(m_client);
visitor.visit(m_console_global_environment_extensions);
}
void WebContentConsoleClient::handle_input(StringView js_source)
{
auto& settings = Web::HTML::relevant_settings_object(*m_console_global_environment_extensions);
auto script = Web::HTML::ClassicScript::create("(console)", js_source, settings.realm(), settings.api_base_url());
auto with_scope = JS::new_object_environment(*m_console_global_environment_extensions, true, &settings.realm().global_environment());
// FIXME: Add parse error printouts back once ClassicScript can report parse errors.
auto result = script->run(Web::HTML::ClassicScript::RethrowErrors::No, with_scope);
if (result.value().has_value()) {
m_console_global_environment_extensions->set_most_recent_result(*result.value());
handle_result(*result.value());
}
}
void WebContentConsoleClient::print_html(String const& line)
{
m_message_log.append({ .type = ConsoleOutput::Type::HTML, .data = line });
m_client->did_output_js_console_message(m_message_log.size() - 1);
}
void WebContentConsoleClient::clear()
{
clear_output();
}
void WebContentConsoleClient::clear_output()
{
m_message_log.append({ .type = ConsoleOutput::Type::Clear, .data = String {} });
m_client->did_output_js_console_message(m_message_log.size() - 1);
}
void WebContentConsoleClient::begin_group(String const& label, bool start_expanded)
{
m_message_log.append({ .type = start_expanded ? ConsoleOutput::Type::BeginGroup : ConsoleOutput::Type::BeginGroupCollapsed, .data = label });
m_client->did_output_js_console_message(m_message_log.size() - 1);
}
void WebContentConsoleClient::end_group()
{
m_message_log.append({ .type = ConsoleOutput::Type::EndGroup, .data = String {} });
m_client->did_output_js_console_message(m_message_log.size() - 1);
}
}