From f6a927fa20723feb7c9bbb10d83bf7f220477a06 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sat, 4 Sep 2021 11:45:36 +0100 Subject: [PATCH] WebContent: Store messages in WebContentConsoleClient The `WebContentConsoleClient` now keeps a list of console messages it has received, so these are not lost if the ConsoleWidget has not been initialized yet. This change does break JS console output, but only until the next commit. :^) --- .../Services/WebContent/ClientConnection.cpp | 4 +- .../WebContent/WebContentConsoleClient.cpp | 46 +++++++++++++++++-- .../WebContent/WebContentConsoleClient.h | 16 ++++++- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/Userland/Services/WebContent/ClientConnection.cpp b/Userland/Services/WebContent/ClientConnection.cpp index 7d9064ad04..0e1eda7639 100644 --- a/Userland/Services/WebContent/ClientConnection.cpp +++ b/Userland/Services/WebContent/ClientConnection.cpp @@ -318,9 +318,9 @@ void ClientConnection::run_javascript(String const& js_source) } } -void ClientConnection::js_console_request_messages(i32) +void ClientConnection::js_console_request_messages(i32 start_index) { - TODO(); + m_console_client->send_messages(start_index); } Messages::WebContentServer::GetSelectedTextResponse ClientConnection::get_selected_text() diff --git a/Userland/Services/WebContent/WebContentConsoleClient.cpp b/Userland/Services/WebContent/WebContentConsoleClient.cpp index b3a9c0ef9d..eb87b410cd 100644 --- a/Userland/Services/WebContent/WebContentConsoleClient.cpp +++ b/Userland/Services/WebContent/WebContentConsoleClient.cpp @@ -26,7 +26,7 @@ WebContentConsoleClient::WebContentConsoleClient(JS::Console& console, WeakPtrvm().last_value())); } -void WebContentConsoleClient::print_html(const String& line) +void WebContentConsoleClient::print_html(String const& line) { - m_client.async_did_js_console_output("html", line); + m_message_log.append({ .type = ConsoleOutput::Type::HTML, .html = line }); + m_client.async_did_output_js_console_message(m_message_log.size() - 1); } void WebContentConsoleClient::clear_output() { - m_client.async_did_js_console_output("clear_output", {}); + m_message_log.append({ .type = ConsoleOutput::Type::Clear, .html = "" }); + m_client.async_did_output_js_console_message(m_message_log.size() - 1); +} + +void WebContentConsoleClient::send_messages(i32 start_index) +{ + // FIXME: Cap the number of messages we send at once? + auto messages_to_send = m_message_log.size() - start_index; + if (messages_to_send < 1) { + // When the console is first created, it requests any messages that happened before + // then, by requesting with start_index=0. If we don't have any messages at all, that + // is still a valid request, and we can just ignore it. + if (start_index != 0) + m_client.did_misbehave("Requested non-existent console message index."); + return; + } + + // FIXME: Replace with a single Vector of message structs + Vector message_types; + Vector messages; + message_types.ensure_capacity(messages_to_send); + messages.ensure_capacity(messages_to_send); + + for (size_t i = start_index; i < m_message_log.size(); i++) { + auto& message = m_message_log[i]; + switch (message.type) { + case ConsoleOutput::Type::HTML: + message_types.append("html"); + break; + case ConsoleOutput::Type::Clear: + message_types.append("clear"); + break; + } + + messages.append(message.html); + } + + m_client.async_did_get_js_console_messages(start_index, message_types, messages); } JS::Value WebContentConsoleClient::log() diff --git a/Userland/Services/WebContent/WebContentConsoleClient.h b/Userland/Services/WebContent/WebContentConsoleClient.h index 3fbeb18062..16df4bcf9e 100644 --- a/Userland/Services/WebContent/WebContentConsoleClient.h +++ b/Userland/Services/WebContent/WebContentConsoleClient.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2021, Brandon Scott * Copyright (c) 2020, Hunter Salyer + * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -19,7 +20,8 @@ class WebContentConsoleClient final : public JS::ConsoleClient { public: WebContentConsoleClient(JS::Console&, WeakPtr, ClientConnection&); - void handle_input(const String& js_source); + void handle_input(String const& js_source); + void send_messages(i32 start_index); private: virtual JS::Value log() override; @@ -38,7 +40,17 @@ private: JS::Handle m_console_global_object; void clear_output(); - void print_html(const String& line); + void print_html(String const& line); + + struct ConsoleOutput { + enum class Type { + HTML, + Clear + }; + Type type; + String html; + }; + Vector m_message_log; }; }