Skip to content

Truly independent web browser

LibDevTools+LibWebView+WebContent: Autocomplete console input

sideshowbarker4dd08b6ad086
diff --git a/Libraries/LibDevTools/Actors/ConsoleActor.cpp b/Libraries/LibDevTools/Actors/ConsoleActor.cpp
index 5ca2f646ba1..1b20a324705 100644
--- a/Libraries/LibDevTools/Actors/ConsoleActor.cpp
+++ b/Libraries/LibDevTools/Actors/ConsoleActor.cpp
@@ -95,9 +95,39 @@ void ConsoleActor::handle_message(Message const& message)
JsonObject response;
if (message.type == "autocomplete"sv) {
- response.set("matches"sv, JsonArray {});
- response.set("matchProp"sv, String {});
- send_response(message, move(response));
+ auto text = get_required_parameter<String>(message, "text"sv);
+ if (!text.has_value())
+ return;
+
+ // The client asks for the completions at the cursor, a UTF-16 offset into the text, so only the text before it
+ // is completed.
+ auto input = text.release_value();
+ if (auto cursor = message.data.get_integer<size_t>("cursor"sv); cursor.has_value()) {
+ auto utf16_input = Utf16String::from_utf8(input);
+ if (*cursor < utf16_input.length_in_code_units())
+ input = utf16_input.substring_view(0, *cursor).to_utf8_but_should_be_ported_to_utf16();
+ }
+
+ auto tab = m_tab.strong_ref();
+ if (!tab) {
+ response.set("matches"sv, JsonValue {});
+ send_response(message, move(response));
+ return;
+ }
+
+ devtools().delegate().complete_console_input(tab->description(), input,
+ async_handler(message, [](auto&, DevToolsDelegate::ConsoleInputCompletion completion, auto& response) {
+ if (completion.matches.has_value()) {
+ JsonArray matches;
+ for (auto& match : *completion.matches)
+ matches.must_append(move(match));
+ response.set("matches"sv, move(matches));
+ } else {
+ response.set("matches"sv, JsonValue {});
+ }
+ response.set("matchProp"sv, move(completion.match_prop));
+ response.set("isElementAccess"sv, false);
+ }));
return;
}
diff --git a/Libraries/LibDevTools/DevToolsDelegate.h b/Libraries/LibDevTools/DevToolsDelegate.h
index 8c220455165..430ae39e9c5 100644
--- a/Libraries/LibDevTools/DevToolsDelegate.h
+++ b/Libraries/LibDevTools/DevToolsDelegate.h
@@ -153,6 +153,14 @@ public:
using OnResolvedURLReceived = Function<void(ErrorOr<String>)>;
virtual void resolve_dom_node_url(TabDescription const&, Optional<Web::UniqueNodeID>, String const&, OnResolvedURLReceived) const { }
+ struct ConsoleInputCompletion {
+ // No matches at all, rather than an empty list, means the input doesn't end in a name to complete.
+ Optional<Vector<String>> matches;
+ String match_prop;
+ };
+ using OnConsoleInputCompleted = Function<void(ErrorOr<ConsoleInputCompletion>)>;
+ virtual void complete_console_input(TabDescription const&, String const&, OnConsoleInputCompleted on_complete) const { on_complete(ConsoleInputCompletion {}); }
+
using OnStyleSheetsReceived = Function<void(ErrorOr<Vector<Web::CSS::StyleSheetIdentifier>>)>;
using OnStyleSheetSourceReceived = Function<void(Web::CSS::StyleSheetIdentifier const&, Utf16String)>;
virtual void retrieve_style_sheets(TabDescription const&, OnStyleSheetsReceived) const { }
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 74ed941f029..cfa443045b5 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -4248,6 +4248,17 @@ void Application::retrieve_debugger_source_positions(DevTools::TabDescription co
view->retrieve_debugger_source_positions(source_id, move(on_complete));
}
+void Application::complete_console_input(DevTools::TabDescription const& description, String const& input, OnConsoleInputCompleted on_complete) const
+{
+ auto view = ViewImplementation::find_view_by_id(description.id);
+ if (!view.has_value()) {
+ on_complete(DevTools::DevToolsDelegate::ConsoleInputCompletion {});
+ return;
+ }
+
+ view->complete_js_console_input(input, move(on_complete));
+}
+
void Application::resolve_dom_node_url(DevTools::TabDescription const& description, Optional<Web::UniqueNodeID> node_id, String const& url, OnResolvedURLReceived on_complete) const
{
auto view = ViewImplementation::find_view_by_id(description.id);
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 26913c3c4c0..a13797116f9 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -586,6 +586,7 @@ private:
virtual void evaluate_javascript_in_debugger_frame(DevTools::TabDescription const&, u64 frame_id, String const&, OnDebuggerEvaluationComplete) const override;
virtual void retrieve_debugger_object_properties(DevTools::TabDescription const&, u64 object_id, DebuggerObjectScope, OnDebuggerObjectPropertiesReceived) const override;
virtual void retrieve_debugger_source_positions(DevTools::TabDescription const&, Web::HTML::ScriptRegistryIdentifier, OnDebuggerSourcePositionsReceived) const override;
+ virtual void complete_console_input(DevTools::TabDescription const&, String const&, OnConsoleInputCompleted) const override;
virtual void resolve_dom_node_url(DevTools::TabDescription const&, Optional<Web::UniqueNodeID>, String const&, OnResolvedURLReceived) const override;
virtual void evaluate_javascript(DevTools::TabDescription const&, String const&, OnScriptEvaluationComplete) const override;
virtual void listen_for_console_messages(DevTools::TabDescription const&, OnConsoleMessage) const override;
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index e792cc150f4..a43ca394fe1 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -116,6 +116,7 @@ ViewImplementation::~ViewImplementation()
// cancelling the view's location requests that the location is no longer in use.
on_url_change = nullptr;
on_location_in_use_change = nullptr;
+ answer_pending_console_input_completions();
TabPerformanceMonitor::forget_view(view_id());
// Leave the views before the permission manager hears: a one-time grant that ends with this view has it revoke
// location in every view that shows the site, and this one is on its way out.
@@ -2453,6 +2454,22 @@ void ViewImplementation::retrieve_debugger_source_positions(Web::HTML::ScriptReg
page().async_get_debugger_source_positions(request_id, source_id);
}
+// A DevTools client waits for the answer to each console input completion request before the next, so a request that
+// its page can no longer answer gets no completions instead.
+void ViewImplementation::answer_pending_console_input_completions()
+{
+ auto pending_console_input_completions = move(on_completed_js_console_input);
+ for (auto& pending : pending_console_input_completions)
+ pending.value(DevTools::DevToolsDelegate::ConsoleInputCompletion {});
+}
+
+void ViewImplementation::complete_js_console_input(String const& input, DevTools::DevToolsDelegate::OnConsoleInputCompleted on_complete)
+{
+ auto request_id = m_next_js_console_completion_request_id++;
+ on_completed_js_console_input.set(request_id, move(on_complete));
+ page().async_complete_js_console_input(request_id, input);
+}
+
void ViewImplementation::resolve_dom_node_url(Optional<Web::UniqueNodeID> node_id, String const& url, DevTools::DevToolsDelegate::OnResolvedURLReceived on_complete)
{
auto request_id = m_next_resolve_dom_node_url_request_id++;
@@ -3477,6 +3494,9 @@ void ViewImplementation::display_page_changed(RefPtr<WebContentPage> previous_pa
if (auto pending_info_request = move(m_pending_info_request))
pending_info_request->reject(Error::from_string_literal("The displayed page changed before sending its info"));
+ // So did console input completion requests.
+ answer_pending_console_input_completions();
+
auto& page = this->page();
if (previous_page) {
// NB: The page has no hovered link and cannot clear the outgoing page's status label.
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index b3313ad576c..e2ef1061806 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -400,6 +400,7 @@ public:
void retrieve_debugger_object_properties(u64 object_id, DebuggerObjectScope, DevTools::DevToolsDelegate::OnDebuggerObjectPropertiesReceived);
void retrieve_debugger_source_positions(Web::HTML::ScriptRegistryIdentifier, DevTools::DevToolsDelegate::OnDebuggerSourcePositionsReceived);
void resolve_dom_node_url(Optional<Web::UniqueNodeID> node_id, String const& url, DevTools::DevToolsDelegate::OnResolvedURLReceived);
+ void complete_js_console_input(String const& input, DevTools::DevToolsDelegate::OnConsoleInputCompleted);
void clear_inspected_dom_node();
void highlight_dom_node(Web::UniqueNodeID node_id, Optional<Web::CSS::PseudoElement> pseudo_element);
@@ -599,6 +600,7 @@ public:
HashMap<u64, DevTools::DevToolsDelegate::OnSourcesReceived> on_received_devtools_sources;
HashMap<Web::HTML::ScriptRegistryIdentifier, Function<void(Optional<Web::HTML::ScriptRegistryContent>)>> on_received_devtools_source;
HashMap<u64, DevTools::DevToolsDelegate::OnResolvedURLReceived> on_resolved_dom_node_url;
+ HashMap<u64, DevTools::DevToolsDelegate::OnConsoleInputCompleted> on_completed_js_console_input;
Function<void(Web::HTML::ScriptRegistryDescription)> on_devtools_source_available;
DevTools::DevToolsDelegate::OnDebuggerPaused on_debugger_paused;
DevTools::DevToolsDelegate::OnDebuggerResumed on_debugger_resumed;
@@ -1187,6 +1189,8 @@ protected:
u64 m_next_debugger_object_properties_request_id { 1 };
u64 m_next_debugger_source_positions_request_id { 1 };
u64 m_next_resolve_dom_node_url_request_id { 1 };
+ void answer_pending_console_input_completions();
+ u64 m_next_js_console_completion_request_id { 1 };
HashMap<u64, DevTools::DevToolsDelegate::OnDebuggerBreakpointOperationComplete> m_pending_debugger_breakpoint_requests;
HashMap<u64, DevTools::DevToolsDelegate::OnDebuggerEnvironmentsReceived> m_pending_debugger_environments_requests;
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 44d76a89c81..058ae104e60 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -2195,6 +2195,15 @@ void WebContentPage::did_resolve_dom_node_url(u64 request_id, String resolved_ur
}
}
+void WebContentPage::did_complete_js_console_input(u64 request_id, Optional<Vector<String>> matches, String match_prop)
+{
+ if (displays_tab()) {
+ auto handler = view().on_completed_js_console_input.take(request_id);
+ if (handler.has_value())
+ (*handler)(DevTools::DevToolsDelegate::ConsoleInputCompletion { move(matches), move(match_prop) });
+ }
+}
+
void WebContentPage::did_receive_network_response_headers(u64 request_id, u32 status_code, Optional<String> reason_phrase, Vector<HTTP::Header> response_headers, Requests::CacheState cache_state)
{
if (displays_tab()) {
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index e1248c4cd94..d3dd1b3ad8c 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -313,6 +313,7 @@ private:
virtual void did_mutate_dom(Mutation mutation) override;
virtual void did_get_dom_node_html(String html) override;
virtual void did_resolve_dom_node_url(u64 request_id, String resolved_url) override;
+ virtual void did_complete_js_console_input(u64 request_id, Optional<Vector<String>> matches, String match_prop) override;
virtual void did_receive_network_response_headers(u64 request_id, u32 status_code, Optional<String> reason_phrase, Vector<HTTP::Header> response_headers, Requests::CacheState cache_state) override;
virtual void did_update_indexed_database(String update) override;
virtual void did_request_clipboard_entries(u64 request_id) override;
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index cd51da62bbe..1eeb465fa43 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -3394,6 +3394,18 @@ void ConnectionFromClient::js_console_input(Web::PageId page_id, String js_sourc
page->js_console_input(js_source);
}
+void ConnectionFromClient::complete_js_console_input(Web::PageId page_id, u64 request_id, String input)
+{
+ auto page = this->page(page_id);
+ auto completion = page.has_value() ? page->complete_js_console_input(input) : OptionalNone {};
+ if (!completion.has_value()) {
+ async_did_complete_js_console_input(page_id, request_id, OptionalNone {}, String {});
+ return;
+ }
+
+ async_did_complete_js_console_input(page_id, request_id, move(completion->matches), move(completion->match_prop));
+}
+
void ConnectionFromClient::run_javascript(Web::PageId page_id, String js_source)
{
if (auto page = this->page(page_id); page.has_value())
diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.h
index 29746412654..99724a9c9c4 100644
--- a/Services/WebContent/ConnectionFromClient.h
+++ b/Services/WebContent/ConnectionFromClient.h
@@ -227,6 +227,7 @@ private:
virtual void get_debugger_object_properties(Web::PageId page_id, u64 request_id, u64 object_id, WebView::DebuggerObjectScope scope) override;
virtual void get_debugger_source_positions(Web::PageId page_id, u64 request_id, Web::HTML::ScriptRegistry::Identifier) override;
virtual void resolve_dom_node_url(Web::PageId page_id, u64 request_id, Optional<Web::UniqueNodeID> node_id, String url) override;
+ virtual void complete_js_console_input(Web::PageId page_id, u64 request_id, String input) override;
virtual void set_listen_for_dom_mutations(Web::PageId page_id, bool) override;
virtual void set_has_devtools_client(Web::PageId page_id, bool has_devtools_client) override;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 5092aeaa4d4..3f6d07ce13b 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -2395,6 +2395,13 @@ void PageClient::js_console_input(StringView js_source)
m_top_level_document_console_client->handle_input(js_source);
}
+Optional<Web::HTML::ConsoleInputCompletion> PageClient::complete_js_console_input(StringView input)
+{
+ if (!m_top_level_document_console_client)
+ return {};
+ return m_top_level_document_console_client->complete_input(input);
+}
+
void PageClient::run_javascript(StringView js_source)
{
auto active_document = page().local_traversable()->active_document();
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index e932401a8ee..9f193de97f7 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -18,6 +18,7 @@
#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/SameDocumentNavigationEntry.h>
+#include <LibWeb/HTML/Scripting/ConsoleInputCompletion.h>
#include <LibWeb/HTML/Scripting/ScriptRegistry.h>
#include <LibWeb/HTML/SessionHistoryEntry.h>
#include <LibWeb/Page/Page.h>
@@ -122,6 +123,7 @@ public:
void initialize_js_console(Web::DOM::Document& document);
void js_console_input(StringView js_source);
+ Optional<Web::HTML::ConsoleInputCompletion> complete_js_console_input(StringView input);
void did_execute_js_console_input(WebView::DebuggerValue const&);
ErrorOr<WebView::DebuggerObjectProperties> console_object_properties(u64 object_id);
void run_javascript(StringView js_source);
diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipc
index cb891c0f97d..8b4c631fb1d 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -289,6 +289,7 @@ endpoint WebContentClient routed by Web::PageId page_id
gamepad_stop_effects(Web::Gamepad::GamepadHandle handle) =|
did_execute_js_console_input(Web::PageId page_id, WebView::DebuggerValue result) =|
+ did_complete_js_console_input(Web::PageId page_id, u64 request_id, Optional<Vector<String>> matches, String match_prop) =|
did_output_js_console_message(Web::PageId page_id, WebView::ConsoleOutput console_output) =|
did_block_resource(Web::PageId page_id, URL::URL url, String kind) =|
diff --git a/Services/WebContent/WebContentConsoleClient.cpp b/Services/WebContent/WebContentConsoleClient.cpp
index 90f57dce86e..360fce1c9f2 100644
--- a/Services/WebContent/WebContentConsoleClient.cpp
+++ b/Services/WebContent/WebContentConsoleClient.cpp
@@ -37,6 +37,12 @@ void WebContentConsoleClient::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(m_console_global_environment_extensions);
}
+Optional<Web::HTML::ConsoleInputCompletion> WebContentConsoleClient::complete_input(StringView input)
+{
+ auto& settings = Web::HTML::relevant_settings_object(m_console_global_environment_extensions->binding_object());
+ return Web::HTML::complete_console_input(settings.realm(), input);
+}
+
void WebContentConsoleClient::handle_input(StringView js_source)
{
auto& settings = Web::HTML::relevant_settings_object(m_console_global_environment_extensions->binding_object());
diff --git a/Services/WebContent/WebContentConsoleClient.h b/Services/WebContent/WebContentConsoleClient.h
index f72cfebb8d7..5b46aa7a9f1 100644
--- a/Services/WebContent/WebContentConsoleClient.h
+++ b/Services/WebContent/WebContentConsoleClient.h
@@ -12,6 +12,7 @@
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Value.h>
#include <LibWeb/Forward.h>
+#include <LibWeb/HTML/Scripting/ConsoleInputCompletion.h>
#include <WebContent/Forward.h>
namespace WebContent {
@@ -24,6 +25,7 @@ public:
virtual ~WebContentConsoleClient() override;
void handle_input(StringView js_source);
+ Optional<Web::HTML::ConsoleInputCompletion> complete_input(StringView input);
virtual void handle_result(JS::Value) = 0;
diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipc
index cf85d67f2b8..34eb5c3223b 100644
--- a/Services/WebContent/WebContentServer.ipc
+++ b/Services/WebContent/WebContentServer.ipc
@@ -191,6 +191,7 @@ endpoint WebContentServer routed by Web::PageId page_id
get_node_id_at_position(Web::PageId page_id, u64 request_id, Web::DevicePixelPoint position) =|
js_console_input(Web::PageId page_id, String js_source) =|
+ complete_js_console_input(Web::PageId page_id, u64 request_id, String input) =|
run_javascript(Web::PageId page_id, String js_source) =|
list_style_sheets(Web::PageId page_id) =|
diff --git a/Tests/LibDevTools/TestDevToolsProtocol.cpp b/Tests/LibDevTools/TestDevToolsProtocol.cpp
index 57c7c482a78..4e678b4a30e 100644
--- a/Tests/LibDevTools/TestDevToolsProtocol.cpp
+++ b/Tests/LibDevTools/TestDevToolsProtocol.cpp
@@ -1339,6 +1339,12 @@ public:
on_complete(move(properties));
}
+ virtual void complete_console_input(DevTools::TabDescription const&, String const& input, OnConsoleInputCompleted callback) const override
+ {
+ last_console_input_to_complete = input;
+ callback(console_input_completion);
+ }
+
virtual void resolve_dom_node_url(DevTools::TabDescription const&, Optional<Web::UniqueNodeID> node_id, String const& url, OnResolvedURLReceived callback) const override
{
++resolve_dom_node_url_call_count;
@@ -1763,6 +1769,8 @@ public:
mutable size_t last_attribute_count { 0 };
mutable Optional<Web::UniqueNodeID> last_resolved_url_node;
mutable Optional<String> last_url_to_resolve;
+ mutable Optional<String> last_console_input_to_complete;
+ DevToolsDelegate::ConsoleInputCompletion console_input_completion;
mutable String resolved_dom_node_url { "https://example.test/scripts/app.js"_string };
mutable Optional<String> last_navigated_url;
mutable Optional<bool> last_reload_bypass_cache;
@@ -4709,6 +4717,42 @@ TEST_CASE(console_evaluation_result_object_grips)
EXPECT(properties.get_object("ownProperties"sv)->has("answer"sv));
}
+TEST_CASE(console_autocomplete)
+{
+ auto session = create_session();
+ auto& client = *session->client;
+ (void)client.read_message();
+
+ auto target = get_frame_target(client, actor_from(get_tab(client), "actor"sv));
+ auto console_actor = actor_from(target, "consoleActor"sv);
+
+ auto autocomplete = [&](StringView text, Optional<size_t> cursor) {
+ JsonObject request;
+ request.set("to"sv, console_actor);
+ request.set("type"sv, "autocomplete"sv);
+ request.set("text"sv, text);
+ if (cursor.has_value())
+ request.set("cursor"sv, *cursor);
+ return client.request(move(request));
+ };
+
+ session->delegate.console_input_completion = { .matches = Vector { "body"_string, "bodyText"_string }, .match_prop = "bo"_string };
+
+ auto response = autocomplete("document.bo"sv, {});
+ EXPECT_EQ(session->delegate.last_console_input_to_complete.value(), "document.bo"sv);
+ EXPECT_EQ(response.get_array("matches"sv)->size(), 2u);
+ EXPECT_EQ(response.get_array("matches"sv)->at(0).as_string(), "body"sv);
+ EXPECT_EQ(response.get_string("matchProp"sv).value(), "bo"sv);
+ EXPECT_EQ(response.get_bool("isElementAccess"sv).value(), false);
+
+ // Only the text before the cursor is completed.
+ (void)autocomplete("document.bo + 1"sv, 11);
+ EXPECT_EQ(session->delegate.last_console_input_to_complete.value(), "document.bo"sv);
+
+ session->delegate.console_input_completion = {};
+ EXPECT(autocomplete("f().x"sv, {}).get("matches"sv)->is_null());
+}
+
TEST_CASE(inspector_walker_navigation_reloads_root)
{
auto session = create_session();