Truly independent web browser
LibWeb+LibDevTools+WebContent: Give logged objects expandable grips
sideshowbarkerdad8de663847
diff --git a/Libraries/LibDevTools/Actors/FrameActor.cpp b/Libraries/LibDevTools/Actors/FrameActor.cppindex 6b70e6a876a..9ff07583180 100644--- a/Libraries/LibDevTools/Actors/FrameActor.cpp+++ b/Libraries/LibDevTools/Actors/FrameActor.cpp@@ -463,7 +463,12 @@ void FrameActor::on_console_message(WebView::ConsoleOutput console_output)if (log.stacktrace.has_value())message.set("stacktrace"sv, serialize_console_stacktrace(*log.stacktrace));message.set("timeStamp"sv, console_output.timestamp.milliseconds_since_epoch());- message.set("arguments"sv, JsonArray { move(log.arguments) });+ // An object argument names the object by its ID, which needs an actor before the client can expand it.+ JsonArray arguments;+ auto thread = m_thread.strong_ref();+ for (auto& argument : log.arguments)+ arguments.must_append(thread ? thread->resolve_object_references(argument, WebView::DebuggerObjectScope::Console) : move(argument));+ message.set("arguments"sv, move(arguments));console_messages.must_append(move(message));},diff --git a/Libraries/LibDevTools/Actors/ThreadActor.h b/Libraries/LibDevTools/Actors/ThreadActor.hindex f5b542b963a..ffa0a4fa7b9 100644--- a/Libraries/LibDevTools/Actors/ThreadActor.h+++ b/Libraries/LibDevTools/Actors/ThreadActor.h@@ -32,6 +32,7 @@ public:JsonObject serialize_source(Web::HTML::ScriptRegistryDescription const&);JsonArray serialize_sources(Vector<Web::HTML::ScriptRegistryDescription> const&);JsonValue serialize_debugger_value(WebView::DebuggerValue const&, WebView::DebuggerObjectScope = WebView::DebuggerObjectScope::Pause);+ JsonValue resolve_object_references(JsonValue const&, WebView::DebuggerObjectScope);Optional<u64> frame_id_for_actor(StringView actor) const;void get_frame_environment(DebuggerFrameActor&, Actor::Message const&, u64 frame_id);void get_object_properties(ObjectActor&, Actor::Message const&, ObjectPropertiesRequest, Optional<String> property_name = {});@@ -50,7 +51,6 @@ private:void prune_source_actors(Vector<Web::HTML::ScriptRegistryDescription> const&);SourceActor& source_actor_for(Web::HTML::ScriptRegistryDescription const&);ObjectActor& object_actor_for(u64 object_id, String const& class_name, WebView::DebuggerObjectScope);- JsonValue resolve_object_references(JsonValue const&, WebView::DebuggerObjectScope);void track_actor(Actor&, WebView::DebuggerObjectScope);JsonObject serialize_environment_chain(Vector<WebView::DebuggerEnvironment>);JsonObject serialize_property_descriptor(WebView::DebuggerProperty const&, WebView::DebuggerObjectScope);diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txtindex f3354694ac0..e3b4661982a 100644--- a/Libraries/LibWeb/CMakeLists.txt+++ b/Libraries/LibWeb/CMakeLists.txt@@ -629,6 +629,7 @@ set(SOURCESHTML/Scripting/Agent.cppHTML/Scripting/ClassicScript.cppHTML/Scripting/ConsoleInputCompletion.cpp+ HTML/Scripting/DevToolsLoggedObjects.cppHTML/Scripting/DevToolsObjectTable.cppHTML/Scripting/Environments.cppHTML/Scripting/ExceptionReporter.cppdiff --git a/Libraries/LibWeb/HTML/Scripting/DevToolsLoggedObjects.cpp b/Libraries/LibWeb/HTML/Scripting/DevToolsLoggedObjects.cppnew file mode 100644index 00000000000..53496d51101--- /dev/null+++ b/Libraries/LibWeb/HTML/Scripting/DevToolsLoggedObjects.cpp@@ -0,0 +1,54 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/StdLibExtras.h>+#include <LibJS/Runtime/Value.h>+#include <LibWeb/HTML/Scripting/DevToolsLoggedObjects.h>++namespace Web::HTML {++Vector<JsonValue> DevToolsLoggedObjects::serialize_message_arguments(ReadonlySpan<JS::Value> arguments)+{+ if (m_messages_in_current_table == MESSAGES_PER_TABLE) {+ swap(m_current_table, m_previous_table);+ m_current_table.clear();+ m_messages_in_current_table = 0;+ }+ ++m_messages_in_current_table;++ Vector<JsonValue> serialized_arguments;+ serialized_arguments.ensure_capacity(arguments.size());+ for (auto argument : arguments)+ serialized_arguments.unchecked_append(m_current_table.serialize_value_as_grip(argument));+ return serialized_arguments;+}++bool DevToolsLoggedObjects::contains(u64 object_id) const+{+ return m_current_table.contains(object_id) || m_previous_table.contains(object_id);+}++ErrorOr<WebView::DebuggerObjectProperties> DevToolsLoggedObjects::properties_for_object(u64 object_id)+{+ if (m_previous_table.contains(object_id))+ return m_previous_table.properties_for_object(object_id);+ return m_current_table.properties_for_object(object_id);+}++void DevToolsLoggedObjects::clear()+{+ m_current_table.clear();+ m_previous_table.clear();+ m_messages_in_current_table = 0;+}++void DevToolsLoggedObjects::visit_edges(GC::Cell::Visitor& visitor)+{+ m_current_table.visit_edges(visitor);+ m_previous_table.visit_edges(visitor);+}++}diff --git a/Libraries/LibWeb/HTML/Scripting/DevToolsLoggedObjects.h b/Libraries/LibWeb/HTML/Scripting/DevToolsLoggedObjects.hnew file mode 100644index 00000000000..3d1ed45dfb6--- /dev/null+++ b/Libraries/LibWeb/HTML/Scripting/DevToolsLoggedObjects.h@@ -0,0 +1,38 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/JsonValue.h>+#include <AK/Span.h>+#include <AK/Vector.h>+#include <LibGC/Cell.h>+#include <LibJS/Forward.h>+#include <LibWeb/Export.h>+#include <LibWeb/HTML/Scripting/DevToolsObjectTable.h>++namespace Web::HTML {++// The objects of the messages that a page logs to its console, which DevTools may expand. Only the latest messages keep+// theirs, so a page that logs in a loop doesn't keep alive everything it ever logged: A message's objects go into the+// current table, and once that holds MESSAGES_PER_TABLE messages, it replaces the previous table.+class WEB_API DevToolsLoggedObjects {+public:+ static constexpr size_t MESSAGES_PER_TABLE = 500;++ Vector<JsonValue> serialize_message_arguments(ReadonlySpan<JS::Value>);+ bool contains(u64 object_id) const;+ ErrorOr<WebView::DebuggerObjectProperties> properties_for_object(u64 object_id);+ void clear();+ void visit_edges(GC::Cell::Visitor&);++private:+ DevToolsObjectTable m_current_table { DevToolsObjectTable::RootObjects::No };+ DevToolsObjectTable m_previous_table { DevToolsObjectTable::RootObjects::No };+ size_t m_messages_in_current_table { 0 };+};++}diff --git a/Libraries/LibWeb/HTML/Scripting/DevToolsObjectTable.h b/Libraries/LibWeb/HTML/Scripting/DevToolsObjectTable.hindex 0c0d187337d..c1353bda99d 100644--- a/Libraries/LibWeb/HTML/Scripting/DevToolsObjectTable.h+++ b/Libraries/LibWeb/HTML/Scripting/DevToolsObjectTable.h@@ -34,6 +34,7 @@ public:WebView::DebuggerValue serialize_value(JS::Value);JsonValue serialize_value_as_grip(JS::Value);ErrorOr<WebView::DebuggerObjectProperties> properties_for_object(u64 object_id);+ bool contains(u64 object_id) const { return m_objects.contains(object_id); }void clear();void visit_edges(GC::Cell::Visitor&);diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex 569850df2fa..e76cdef06d8 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -184,6 +184,7 @@ void Internals::visit_edges(GC::Cell::Visitor& visitor)visitor.visit(m_gamepads);if (m_devtools_object_table.has_value())m_devtools_object_table->visit_edges(visitor);+ m_devtools_logged_objects.visit_edges(visitor);}void Internals::signal_test_is_done(Utf16String const& text)@@ -1180,6 +1181,32 @@ Utf16String Internals::devtools_properties(JS::Value value)return Utf16String::from_utf8(result.serialized());}+// The grips that the DevTools console shows for the arguments of one logged message. The objects stay in the same table+// across calls, as they do for a document's console.+Utf16String Internals::devtools_log(GC::RootVector<JS::Value> const& arguments)+{+ JsonArray grips;+ for (auto& grip : m_devtools_logged_objects.serialize_message_arguments(arguments.span()))+ grips.must_append(move(grip));+ return Utf16String::from_utf8(grips.serialized());+}++// The names of the own properties of a logged object, if it's still kept.+Utf16String Internals::devtools_logged_properties(u64 object_id)+{+ if (!m_devtools_logged_objects.contains(object_id))+ return "not kept"_utf16;++ auto properties = m_devtools_logged_objects.properties_for_object(object_id);+ if (properties.is_error())+ return Utf16String::from_utf8(properties.error().string_literal());++ JsonArray names;+ for (auto const& property : properties.value().properties)+ names.must_append(property.name.to_utf8());+ return Utf16String::from_utf8(names.serialized());+}+Optional<Vector<String>> Internals::complete_console_input(Utf16String const& input){auto completion = HTML::complete_console_input(HTML::relevant_realm(window()), input.to_utf8_but_should_be_ported_to_utf16());diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.hindex d61f73618c8..c73fa7f1ddf 100644--- a/Libraries/LibWeb/Internals/Internals.h+++ b/Libraries/LibWeb/Internals/Internals.h@@ -13,6 +13,7 @@#include <LibWeb/DOM/Document.h>#include <LibWeb/Export.h>#include <LibWeb/Forward.h>+#include <LibWeb/HTML/Scripting/DevToolsLoggedObjects.h>#include <LibWeb/HTML/Scripting/DevToolsObjectTable.h>#include <LibWeb/Internals/InternalAnimationTimeline.h>#include <LibWeb/Internals/InternalsBase.h>@@ -137,6 +138,8 @@ public:Optional<Utf16String> generate_text_fragment_url(DOM::Range&, Utf16String const& current_url);Utf16String devtools_grip(JS::Value);Utf16String devtools_properties(JS::Value);+ Utf16String devtools_log(GC::RootVector<JS::Value> const& arguments);+ Utf16String devtools_logged_properties(u64 object_id);Optional<Vector<String>> complete_console_input(Utf16String const& input);void find_in_page(Utf16String const& query, bool highlight_all);@@ -360,6 +363,7 @@ private:Optional<Utf16String> m_pending_action_test_result;Vector<GC::Ref<InternalGamepad>> m_gamepads;Optional<HTML::DevToolsObjectTable> m_devtools_object_table;+ HTML::DevToolsLoggedObjects m_devtools_logged_objects;};}diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idlindex 47e03e9e3e3..b62c02d058f 100644--- a/Libraries/LibWeb/Internals/Internals.idl+++ b/Libraries/LibWeb/Internals/Internals.idl@@ -129,6 +129,8 @@ interface Internals {DOMString? generateTextFragmentURL(Range range, USVString currentURL);DOMString devtoolsGrip(any value);DOMString devtoolsProperties(any value);+ DOMString devtoolsLog(sequence<any> arguments);+ DOMString devtoolsLoggedProperties(unsigned long long objectId);sequence<DOMString>? completeConsoleInput(DOMString input);// Drive marked/preedit-text composition via the same path platform input methods use. setMarkedTextFromInputMethoddiff --git a/Services/WebContent/DevToolsConsoleClient.cpp b/Services/WebContent/DevToolsConsoleClient.cppindex 9a828acc49b..02929d4d67c 100644--- a/Services/WebContent/DevToolsConsoleClient.cpp+++ b/Services/WebContent/DevToolsConsoleClient.cpp@@ -102,6 +102,7 @@ void DevToolsConsoleClient::visit_edges(JS::Cell::Visitor& visitor){Base::visit_edges(visitor);m_object_table.visit_edges(visitor);+ m_logged_objects.visit_edges(visitor);}void DevToolsConsoleClient::handle_result(JS::Value result)@@ -115,6 +116,8 @@ ErrorOr<WebView::DebuggerObjectProperties> DevToolsConsoleClient::properties_for{auto& settings = Web::HTML::relevant_settings_object(m_console_global_environment_extensions->binding_object());Web::HTML::TemporaryExecutionContext execution_context { settings.realm() };+ if (m_logged_objects.contains(object_id))+ return m_logged_objects.properties_for_object(object_id);return m_object_table.properties_for_object(object_id);}@@ -204,11 +207,15 @@ JS::ThrowCompletionOr<JS::Value> DevToolsConsoleClient::printer(JS::Console::Logauto output = TRY(generically_format_values(argument_values));m_console->output_debug_message(log_level, output);+ // An attached DevTools client gets grips for the objects, which it may expand; otherwise they're printed.Vector<JsonValue> serialized_arguments;- serialized_arguments.ensure_capacity(argument_values.size());-- for (auto value : argument_values)- serialized_arguments.unchecked_append(serialize_value(m_console->realm(), value));+ if (m_client->has_devtools_client()) {+ serialized_arguments = m_logged_objects.serialize_message_arguments(argument_values.span());+ } else {+ serialized_arguments.ensure_capacity(argument_values.size());+ for (auto value : argument_values)+ serialized_arguments.unchecked_append(serialize_value(m_console->realm(), value));+ }send_console_output({.timestamp = UnixDateTime::now(),diff --git a/Services/WebContent/DevToolsConsoleClient.h b/Services/WebContent/DevToolsConsoleClient.hindex bef0dfb4389..2e4acf2fb49 100644--- a/Services/WebContent/DevToolsConsoleClient.h+++ b/Services/WebContent/DevToolsConsoleClient.h@@ -11,6 +11,7 @@#include <LibJS/Console.h>#include <LibJS/Forward.h>#include <LibWeb/Forward.h>+#include <LibWeb/HTML/Scripting/DevToolsLoggedObjects.h>#include <LibWeb/HTML/Scripting/DevToolsObjectTable.h>#include <LibWebCommon/WebView/ConsoleOutput.h>#include <WebContent/Forward.h>@@ -28,6 +29,7 @@ public:virtual ~DevToolsConsoleClient() override;ErrorOr<WebView::DebuggerObjectProperties> properties_for_object(u64 object_id);+ void forget_logged_objects() { m_logged_objects.clear(); }private:DevToolsConsoleClient(JS::Console&, PageClient&, ConsoleGlobalEnvironmentExtensions&);@@ -45,6 +47,9 @@ private:// The objects that the console's results hold, which DevTools may expand for as long as the document is shown.Web::HTML::DevToolsObjectTable m_object_table { Web::HTML::DevToolsObjectTable::RootObjects::No };++ // The objects of the messages that the page logged while a DevTools client was attached.+ Web::HTML::DevToolsLoggedObjects m_logged_objects;};}diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cppindex 0d9db56dc84..5dd2fecc4dc 100644--- a/Services/WebContent/PageClient.cpp+++ b/Services/WebContent/PageClient.cpp@@ -2370,6 +2370,10 @@ void PageClient::set_has_devtools_client(bool has_devtools_client)elseactive_document->clear_devtools_layout_inspection_data();}++ // No client is left to expand the objects of the messages logged for the last one.+ if (!has_devtools_client && m_top_level_document_console_client)+ as<DevToolsConsoleClient>(*m_top_level_document_console_client).forget_logged_objects();}void PageClient::page_did_finish_network_request(u64 request_id, u64 body_size, Requests::RequestTimingInfo const& timing_info, Optional<Requests::NetworkError> const& network_error)diff --git a/Tests/LibDevTools/TestDevToolsProtocol.cpp b/Tests/LibDevTools/TestDevToolsProtocol.cppindex 4e678b4a30e..5a1df57d50b 100644--- a/Tests/LibDevTools/TestDevToolsProtocol.cpp+++ b/Tests/LibDevTools/TestDevToolsProtocol.cpp@@ -4717,6 +4717,32 @@ TEST_CASE(console_evaluation_result_object_grips)EXPECT(properties.get_object("ownProperties"sv)->has("answer"sv));}+TEST_CASE(console_log_object_grips)+{+ auto session = create_session();+ auto& client = *session->client;+ (void)client.read_message();++ (void)get_frame_target(client, actor_from(get_tab(client), "actor"sv));++ // WebContent names a logged object by ID; the server makes it an object actor.+ auto logged_object = MUST(JsonValue::from_string(R"~~~({"type":"object","objectId":9,"class":"Object",+ "preview":{"kind":"Object","ownProperties":{"answer":42},"ownPropertiesLength":1}})~~~"sv));+ session->delegate.emit_console_log(JS::ConsoleLogLevel::Log, { "label"_string, move(logged_object) });+ auto arguments = read_resource(client, "console-message"sv).get_array("arguments"sv).release_value();+ EXPECT_EQ(arguments.at(0).as_string(), "label"sv);+ auto grip = arguments.at(1).as_object();+ EXPECT_EQ(grip.get_string("class"sv).value(), "Object"sv);+ EXPECT(!grip.has("objectId"sv));+ EXPECT_EQ(grip.get_object("preview"sv)->get_object("ownProperties"sv)->get_integer<u32>("answer"sv).value(), 42u);++ // Expanding a logged object asks for the properties of the console's object, not the debugger's.+ auto properties = client.request(actor_from(grip, "actor"sv), "prototypeAndProperties"sv);+ EXPECT_EQ(session->delegate.last_debugger_object_id.value(), 9u);+ EXPECT_EQ(session->delegate.last_debugger_object_scope.value(), WebView::DebuggerObjectScope::Console);+ EXPECT(properties.get_object("ownProperties"sv)->has("answer"sv));+}+TEST_CASE(console_autocomplete){auto session = create_session();diff --git a/Tests/LibWeb/Text/expected/Internals/devtools-logged-objects.txt b/Tests/LibWeb/Text/expected/Internals/devtools-logged-objects.txtnew file mode 100644index 00000000000..ecaac87eaa9--- /dev/null+++ b/Tests/LibWeb/Text/expected/Internals/devtools-logged-objects.txt@@ -0,0 +1,7 @@+first object: class Object, has an object ID: true+other arguments: ["text",1,{"type":"undefined"}]+first message kept: ["answer"]+after 1000 messages, first message kept: ["answer"]+after 1001 messages, first message kept: not kept+message 500 kept: not kept+message 501 kept: ["message"]diff --git a/Tests/LibWeb/Text/input/Internals/devtools-logged-objects.html b/Tests/LibWeb/Text/input/Internals/devtools-logged-objects.htmlnew file mode 100644index 00000000000..789ad259a1f--- /dev/null+++ b/Tests/LibWeb/Text/input/Internals/devtools-logged-objects.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ test(() => {+ const log = (...values) => JSON.parse(internals.devtoolsLog(values));+ const kept = grip => internals.devtoolsLoggedProperties(grip.objectId);++ // Object IDs depend on the run, so they're shown as whether they're there.+ const [first, ...rest] = log({ answer: 42 }, "text", 1, undefined);+ println(`first object: class ${first.class}, has an object ID: ${typeof first.objectId === "number"}`);+ println(`other arguments: ${JSON.stringify(rest)}`);+ println(`first message kept: ${kept(first)}`);++ // The first 500 messages share a table, and the next 500 share another.+ let lastOfFirstTable, firstOfSecondTable;+ for (let message = 2; message <= 1000; ++message) {+ const [grip] = log({ message });+ if (message === 500)+ lastOfFirstTable = grip;+ if (message === 501)+ firstOfSecondTable = grip;+ }+ println(`after 1000 messages, first message kept: ${kept(first)}`);++ // The 1001st message starts a new table, so the first table's messages are dropped.+ log({ message: 1001 });+ println(`after 1001 messages, first message kept: ${kept(first)}`);+ println(`message 500 kept: ${kept(lastOfFirstTable)}`);+ println(`message 501 kept: ${kept(firstOfSecondTable)}`);+ });+</script>