Skip to content

Truly independent web browser

Hub: Don’t take WebDriver’s pointer input for the UI’s

sideshowbarker958072880fc6
diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cpp
index b082d256afc..c9381c020ba 100644
--- a/Services/Hub/ConnectionFromUI.cpp
+++ b/Services/Hub/ConnectionFromUI.cpp
@@ -93,9 +93,11 @@ struct InputFromUI final : public Web::BrowserInputData {
u64 input_id { 0 };
};
-u64 ConnectionFromUI::input_id_of(Web::BrowserInputData const& browser_data)
+Optional<u64> ConnectionFromUI::input_id_of(Web::BrowserInputData const* browser_data)
{
- return as<InputFromUI>(browser_data).input_id;
+ if (auto const* input_from_ui = as_if<InputFromUI>(browser_data))
+ return input_from_ui->input_id;
+ return {};
}
template<typename EventType>
diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.h
index e51861c7d44..8929df17c29 100644
--- a/Services/Hub/ConnectionFromUI.h
+++ b/Services/Hub/ConnectionFromUI.h
@@ -30,8 +30,9 @@ public:
static Vector<DownloadState> downloads();
- // The ID that the UI gave an input event, from the browser data of the event.
- static u64 input_id_of(Web::BrowserInputData const&);
+ // The ID that the UI gave an input event, from the browser data of the event. Input the UI didn't send has none,
+ // whether it carries no browser data or someone else's (WebDriver's, e.g.).
+ static Optional<u64> input_id_of(Web::BrowserInputData const*);
virtual void die() override;
diff --git a/Services/Hub/View.cpp b/Services/Hub/View.cpp
index c3836ff1fa1..e0eee223933 100644
--- a/Services/Hub/View.cpp
+++ b/Services/Hub/View.cpp
@@ -268,30 +268,34 @@ void View::forward_callbacks_to_ui(bool is_tab)
on_finish_handling_key_event = [this](Web::KeyEvent const& event) {
// NB: Input that the Hub came up with itself, such as WebDriver's, is nothing the UI knows about.
- if (!event.browser_data)
+ auto input_id = ConnectionFromUI::input_id_of(event.browser_data.ptr());
+ if (!input_id.has_value())
return;
if (auto* ui = ConnectionFromUI::the()) {
push_state_to_ui();
- ui->async_view_finish_handling_key_event(view_id(), ConnectionFromUI::input_id_of(*event.browser_data), event.clone_without_browser_data());
+ ui->async_view_finish_handling_key_event(view_id(), *input_id, event.clone_without_browser_data());
}
};
on_finish_handling_mouse_event = [this](Web::MouseEvent const& event, Web::EventResult result) {
- if (!event.browser_data)
+ // NB: Input that the Hub came up with itself, such as WebDriver's, is nothing the UI knows about.
+ auto input_id = ConnectionFromUI::input_id_of(event.browser_data.ptr());
+ if (!input_id.has_value())
return;
if (auto* ui = ConnectionFromUI::the()) {
push_state_to_ui();
- ui->async_view_finish_handling_mouse_event(view_id(), ConnectionFromUI::input_id_of(*event.browser_data), event.clone_without_browser_data(), result);
+ ui->async_view_finish_handling_mouse_event(view_id(), *input_id, event.clone_without_browser_data(), result);
}
};
on_finish_handling_drag_event = [this](Web::DragEvent const& event) {
// NB: Input that the Hub came up with itself, such as WebDriver's, is nothing the UI knows about.
- if (!event.browser_data)
+ auto input_id = ConnectionFromUI::input_id_of(event.browser_data.ptr());
+ if (!input_id.has_value())
return;
if (auto* ui = ConnectionFromUI::the()) {
push_state_to_ui();
- ui->async_view_finish_handling_drag_event(view_id(), ConnectionFromUI::input_id_of(*event.browser_data), event.clone_without_browser_data());
+ ui->async_view_finish_handling_drag_event(view_id(), *input_id, event.clone_without_browser_data());
}
};
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index 2027b0fccca..076307aa1d2 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -24,6 +24,12 @@ if (BUILD_TESTING AND NOT WIN32)
endforeach()
endif()
+ # The browser's UI runs on Qt's offscreen platform here, with no display. The macOS UI takes every window for an
+ # AppKit view, which only the Cocoa platform makes.
+ if (LINUX)
+ ladybird_python_test(test-webdriver-graphical-pointer-actions.py ARGS $<TARGET_FILE:WebDriver>)
+ endif()
+
ladybird_python_test(test-webdriver-bidi.py ARGS $<TARGET_FILE:WebDriver>)
ladybird_python_test(test-webdriver-command-privileges.py ARGS $<TARGET_FILE:WebDriver>)
ladybird_python_test(test-webdriver-content-blockers.py ARGS $<TARGET_FILE:WebDriver> TIMEOUT 180)
diff --git a/Tests/LibWebView/test-webdriver-graphical-pointer-actions.py b/Tests/LibWebView/test-webdriver-graphical-pointer-actions.py
new file mode 100755
index 00000000000..e2ffcd32059
--- /dev/null
+++ b/Tests/LibWebView/test-webdriver-graphical-pointer-actions.py
@@ -0,0 +1,93 @@
+#!/usr/bin/env python3
+#
+# Copyright (c) 2026-present, the Ladybird developers.
+#
+# SPDX-License-Identifier: BSD-2-Clause
+
+import argparse
+import importlib
+import os
+import subprocess
+import urllib.parse
+
+webdriver = importlib.import_module("test-webdriver-delete-session")
+
+PAGE = """<!doctype html>
+<style>html, body { margin: 0; } button { position: absolute; left: 0; top: 0; width: 200px; height: 100px; }</style>
+<button onclick="window.clicks = (window.clicks || 0) + 1">Click</button>
+"""
+
+
+def click(port, session_id):
+ status, _payload, response = webdriver.request(
+ port,
+ "POST",
+ f"/session/{session_id}/actions",
+ {
+ "actions": [
+ {
+ "type": "pointer",
+ "id": "mouse",
+ "parameters": {"pointerType": "mouse"},
+ "actions": [
+ {"type": "pointerMove", "origin": "viewport", "x": 100, "y": 50},
+ {"type": "pointerDown", "button": 0},
+ {"type": "pointerUp", "button": 0},
+ ],
+ }
+ ]
+ },
+ )
+ assert status == 200, response
+
+
+def clicks(port, session_id):
+ status, payload, response = webdriver.request(
+ port, "POST", f"/session/{session_id}/execute/sync", {"script": "return window.clicks ?? 0;", "args": []}
+ )
+ assert status == 200, response
+ return payload["value"]
+
+
+def run_test(webdriver_binary):
+ # A graphical session runs the browser's UI, which hands its own input to the Hub. Pointer actions are input the Hub
+ # comes up with itself, which the Hub must not take for the UI's. The offscreen platform lets the UI run with no
+ # display, as on CI, unless the environment names another.
+ environment = os.environ.copy()
+ environment.setdefault("QT_QPA_PLATFORM", "offscreen")
+ process, port = webdriver.launch_webdriver(webdriver_binary, env=environment)
+ try:
+ status, payload, response = webdriver.request(
+ port, "POST", "/session", {"capabilities": {"alwaysMatch": {"ladybird:headless": False}}}
+ )
+ assert status == 200, response
+ session_id = payload["value"]["sessionId"]
+
+ url = "data:text/html," + urllib.parse.quote(PAGE)
+ status, _payload, response = webdriver.request(port, "POST", f"/session/{session_id}/url", {"url": url})
+ assert status == 200, response
+
+ click(port, session_id)
+ click(port, session_id)
+ assert clicks(port, session_id) == 2
+
+ status, _payload, response = webdriver.request(port, "DELETE", f"/session/{session_id}")
+ assert status == 200, response
+ print("PASS: Pointer actions in a graphical session reach the page")
+ finally:
+ process.terminate()
+ try:
+ process.wait(timeout=5)
+ except subprocess.TimeoutExpired:
+ process.kill()
+ process.wait()
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ parser.add_argument("webdriver_binary")
+ run_test(parser.parse_args().webdriver_binary)
+
+
+if __name__ == "__main__":
+ main()