Truly independent web browser
LibWebCommon: Generate the IPC codecs of input events
Andreas Kling39ce3620ce09
diff --git a/Libraries/LibWebCommon/CMakeLists.txt b/Libraries/LibWebCommon/CMakeLists.txtindex bcb1b3aaeae..4645ba56e4c 100644--- a/Libraries/LibWebCommon/CMakeLists.txt+++ b/Libraries/LibWebCommon/CMakeLists.txt@@ -101,6 +101,8 @@ invoke_py_generator("${LADYBIRD_SOURCE_DIR}/Meta/Utils/utils.py")+compile_ipc(Page/InputEvent.ipc Page/InputEventCodecs.h)+compile_ipc(Page/TextEditingCommand.ipc Page/TextEditingCommandCodecs.h)compile_ipc(PixelUnits.ipc PixelUnitsCodecs.h)# NB: The protocols between the UI process and the processes it hosts web content in only use LibWebCommon types, sodiff --git a/Libraries/LibWebCommon/Page/InputEvent.cpp b/Libraries/LibWebCommon/Page/InputEvent.cppindex f97ee8910cb..9ac10617ca4 100644--- a/Libraries/LibWebCommon/Page/InputEvent.cpp+++ b/Libraries/LibWebCommon/Page/InputEvent.cpp@@ -4,10 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/-#include <LibIPC/Decoder.h>-#include <LibIPC/Encoder.h>#include <LibWebCommon/Page/InputEvent.h>-#include <math.h>namespace Web {@@ -22,123 +19,3 @@ MouseEvent MouseEvent::clone_without_browser_data() const}}--template<>-ErrorOr<void> IPC::encode(Encoder& encoder, Web::TextEditingCommand const& command)-{- TRY(encoder.encode(command.type));- TRY(encoder.encode(command.extend_selection));- return {};-}--template<>-ErrorOr<Web::TextEditingCommand> IPC::decode(Decoder& decoder)-{- auto type = TRY(decoder.decode<Web::TextEditingCommand::Type>());- if (to_underlying(type) > to_underlying(Web::TextEditingCommand::Type::DeleteToEndOfParagraph))- return Error::from_string_literal("Invalid text editing command");- auto extend_selection = TRY(decoder.decode<bool>());- return Web::TextEditingCommand { type, extend_selection };-}--template<>-ErrorOr<void> IPC::encode(Encoder& encoder, Web::KeyEvent const& event)-{- TRY(encoder.encode(event.type));- TRY(encoder.encode(event.key));- TRY(encoder.encode(event.modifiers));- TRY(encoder.encode(event.code_point));- TRY(encoder.encode(event.repeat));- TRY(encoder.encode(event.should_insert_text));- TRY(encoder.encode(event.async_scroll_performed_default_action));- TRY(encoder.encode(event.id));- TRY(encoder.encode(event.editing_commands));- TRY(encoder.encode(event.native_editing_binding_resolved));- return {};-}--template<>-ErrorOr<Web::KeyEvent> IPC::decode(Decoder& decoder)-{- auto type = TRY(decoder.decode<Web::KeyEvent::Type>());- auto key = TRY(decoder.decode<Web::UIEvents::KeyCode>());- auto modifiers = TRY(decoder.decode<Web::UIEvents::KeyModifier>());- auto code_point = TRY(decoder.decode<u32>());- auto repeat = TRY(decoder.decode<bool>());- auto should_insert_text = TRY(decoder.decode<bool>());- auto async_scroll_performed_default_action = TRY(decoder.decode<bool>());- auto id = TRY(decoder.decode<u64>());- auto editing_commands = TRY(decoder.decode<Vector<Web::TextEditingCommand>>());- auto native_editing_binding_resolved = TRY(decoder.decode<bool>());-- return Web::KeyEvent { type, key, modifiers, code_point, repeat, should_insert_text, nullptr, async_scroll_performed_default_action, id, move(editing_commands), native_editing_binding_resolved };-}--template<>-ErrorOr<void> IPC::encode(Encoder& encoder, Web::MouseEvent const& event)-{- TRY(encoder.encode(event.type));- TRY(encoder.encode(event.position));- TRY(encoder.encode(event.screen_position));- TRY(encoder.encode(event.button));- TRY(encoder.encode(event.buttons));- TRY(encoder.encode(event.modifiers));- TRY(encoder.encode(event.wheel_delta_x));- TRY(encoder.encode(event.wheel_delta_y));- TRY(encoder.encode(event.wheel_tick_120ths_x));- TRY(encoder.encode(event.wheel_tick_120ths_y));- TRY(encoder.encode(event.wheel_delta_precision));- TRY(encoder.encode(event.scroll_gesture_phase));- TRY(encoder.encode(event.click_count));- TRY(encoder.encode(event.async_scroll_performed_default_action));- TRY(encoder.encode(event.id));- TRY(encoder.encode(event.scrollbar_dragged_by_compositor));- return {};-}--template<>-ErrorOr<Web::MouseEvent> IPC::decode(Decoder& decoder)-{- auto type = TRY(decoder.decode<Web::MouseEvent::Type>());- auto position = TRY(decoder.decode<Web::DevicePixelPoint>());- auto screen_position = TRY(decoder.decode<Web::DevicePixelPoint>());- auto button = TRY(decoder.decode<Web::UIEvents::MouseButton>());- auto buttons = TRY(decoder.decode<Web::UIEvents::MouseButton>());- auto modifiers = TRY(decoder.decode<Web::UIEvents::KeyModifier>());- auto wheel_delta_x = TRY(decoder.decode<double>());- auto wheel_delta_y = TRY(decoder.decode<double>());- auto wheel_tick_120ths_x = TRY(decoder.decode<double>());- auto wheel_tick_120ths_y = TRY(decoder.decode<double>());- auto wheel_delta_precision = TRY(decoder.decode<Web::WheelDeltaPrecision>());- auto scroll_gesture_phase = TRY(decoder.decode<Web::ScrollGesturePhase>());- auto click_count = TRY(decoder.decode<int>());- auto async_scroll_performed_default_action = TRY(decoder.decode<bool>());- auto id = TRY(decoder.decode<u64>());- auto scrollbar_dragged_by_compositor = TRY(decoder.decode<Optional<Web::ScrollbarDraggedByCompositor>>());-- return Web::MouseEvent { type, position, screen_position, button, buttons, modifiers, wheel_delta_x, wheel_delta_y, wheel_tick_120ths_x, wheel_tick_120ths_y, wheel_delta_precision, scroll_gesture_phase, click_count, nullptr, async_scroll_performed_default_action, id, scrollbar_dragged_by_compositor };-}--template<>-WEBCOMMON_API ErrorOr<void> IPC::encode(Encoder& encoder, Web::PinchEvent const& event)-{- TRY(encoder.encode(event.position));- TRY(encoder.encode(event.modifiers));- TRY(encoder.encode(event.scale_delta));- TRY(encoder.encode(event.id));- return {};-}--template<>-WEBCOMMON_API ErrorOr<Web::PinchEvent> IPC::decode(Decoder& decoder)-{- auto position = TRY(decoder.decode<Web::DevicePixelPoint>());- auto modifiers = TRY(decoder.decode<Web::UIEvents::KeyModifier>());- auto scale_delta = TRY(decoder.decode<double>());- auto id = TRY(decoder.decode<u64>());-- if (isnan(scale_delta) || isinf(scale_delta))- return Error::from_string_literal("IPC: Invalid scale_delta value");-- return Web::PinchEvent { position, modifiers, scale_delta, id };-}diff --git a/Libraries/LibWebCommon/Page/InputEvent.h b/Libraries/LibWebCommon/Page/InputEvent.hindex cad280522b1..17c98125ee7 100644--- a/Libraries/LibWebCommon/Page/InputEvent.h+++ b/Libraries/LibWebCommon/Page/InputEvent.h@@ -10,7 +10,6 @@#include <AK/OwnPtr.h>#include <AK/Vector.h>#include <LibGfx/Point.h>-#include <LibIPC/Forward.h>#include <LibWebCommon/Export.h>#include <LibWebCommon/Page/AsyncScrollNodeStableID.h>#include <LibWebCommon/Page/TextEditingCommand.h>@@ -41,7 +40,7 @@ struct WEBCOMMON_API KeyEvent {bool repeat { false };bool should_insert_text { false };- OwnPtr<BrowserInputData> browser_data;+ OwnPtr<BrowserInputData> browser_data {};bool async_scroll_performed_default_action { false };// The UI process numbers the events it sends, and a completion names the event it finished.@@ -129,7 +128,7 @@ struct WEBCOMMON_API MouseEvent {ScrollGesturePhase scroll_gesture_phase { ScrollGesturePhase::None };int click_count { 0 };- OwnPtr<BrowserInputData> browser_data;+ OwnPtr<BrowserInputData> browser_data {};bool async_scroll_performed_default_action { false };u64 id { 0 };Optional<Web::ScrollbarDraggedByCompositor> scrollbar_dragged_by_compositor {};@@ -144,24 +143,4 @@ struct WEBCOMMON_API PinchEvent {}-namespace IPC {--template<>-WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::KeyEvent const&);--template<>-WEBCOMMON_API ErrorOr<Web::KeyEvent> decode(Decoder&);--template<>-WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::MouseEvent const&);--template<>-WEBCOMMON_API ErrorOr<Web::MouseEvent> decode(Decoder&);--template<>-WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::PinchEvent const&);--template<>-WEBCOMMON_API ErrorOr<Web::PinchEvent> decode(Decoder&);--}+#include <LibWebCommon/Page/InputEventCodecs.h>diff --git a/Libraries/LibWebCommon/Page/InputEvent.ipc b/Libraries/LibWebCommon/Page/InputEvent.ipcnew file mode 100644index 00000000000..3f138c19de2--- /dev/null+++ b/Libraries/LibWebCommon/Page/InputEvent.ipc@@ -0,0 +1,48 @@+// Copyright (c) 2026-present, the Ladybird developers.++// NB: The IPC compiler cannot check that each struct here lists every field of its C++ type. A field left out+// here does not cross IPC, as the browser data of the events deliberately does not.++#include <LibWebCommon/Page/InputEvent.h>++[ExistingCpp]+struct Web::KeyEvent {+ Web::KeyEvent::Type type;+ Web::UIEvents::KeyCode key;+ Web::UIEvents::KeyModifier modifiers;+ u32 code_point;+ bool repeat;+ bool should_insert_text;+ bool async_scroll_performed_default_action;+ u64 id;+ Vector<Web::TextEditingCommand> editing_commands;+ bool native_editing_binding_resolved;+}++[ExistingCpp]+struct Web::MouseEvent {+ Web::MouseEvent::Type type;+ Web::DevicePixelPoint position;+ Web::DevicePixelPoint screen_position;+ Web::UIEvents::MouseButton button;+ Web::UIEvents::MouseButton buttons;+ Web::UIEvents::KeyModifier modifiers;+ double wheel_delta_x;+ double wheel_delta_y;+ double wheel_tick_120ths_x;+ double wheel_tick_120ths_y;+ Web::WheelDeltaPrecision wheel_delta_precision;+ Web::ScrollGesturePhase scroll_gesture_phase;+ i32 click_count;+ bool async_scroll_performed_default_action;+ u64 id;+ Optional<Web::ScrollbarDraggedByCompositor> scrollbar_dragged_by_compositor;+}++[ExistingCpp]+struct Web::PinchEvent {+ Web::DevicePixelPoint position;+ Web::UIEvents::KeyModifier modifiers;+ [Finite] double scale_delta;+ u64 id;+}diff --git a/Libraries/LibWebCommon/Page/TextEditingCommand.h b/Libraries/LibWebCommon/Page/TextEditingCommand.hindex 85751935825..aa86d6ea991 100644--- a/Libraries/LibWebCommon/Page/TextEditingCommand.h+++ b/Libraries/LibWebCommon/Page/TextEditingCommand.h@@ -7,8 +7,6 @@#pragma once#include <AK/Types.h>-#include <LibIPC/Forward.h>-#include <LibWebCommon/Export.h>namespace Web {@@ -43,12 +41,4 @@ struct TextEditingCommand {}-namespace IPC {--template<>-WEBCOMMON_API ErrorOr<void> encode(Encoder&, Web::TextEditingCommand const&);--template<>-WEBCOMMON_API ErrorOr<Web::TextEditingCommand> decode(Decoder&);--}+#include <LibWebCommon/Page/TextEditingCommandCodecs.h>diff --git a/Libraries/LibWebCommon/Page/TextEditingCommand.ipc b/Libraries/LibWebCommon/Page/TextEditingCommand.ipcnew file mode 100644index 00000000000..6ed1f7ee3c4--- /dev/null+++ b/Libraries/LibWebCommon/Page/TextEditingCommand.ipc@@ -0,0 +1,35 @@+// Copyright (c) 2026-present, the Ladybird developers.++// NB: The IPC compiler cannot check that this lists every field of TextEditingCommand. A field left out here+// does not cross IPC.++#include <LibWebCommon/Page/TextEditingCommand.h>++[ExistingCpp]+enum Web::TextEditingCommand::Type : u8 {+ MoveBackward,+ MoveForward,+ MoveWordBackward,+ MoveWordForward,+ MoveUp,+ MoveDown,+ MoveToBeginningOfParagraph,+ MoveToEndOfParagraph,+ MoveToBeginningOfLine,+ MoveToEndOfLine,+ MoveToLeftEndOfLine,+ MoveToRightEndOfLine,+ PageUp,+ PageDown,+ DeleteBackward,+ DeleteForward,+ DeleteWordBackward,+ DeleteWordForward,+ DeleteToEndOfParagraph,+}++[ExistingCpp]+struct Web::TextEditingCommand {+ Web::TextEditingCommand::Type type;+ bool extend_selection;+}diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex 6259eaac8b9..10c2ec9b3db 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -41,6 +41,7 @@#include <LibWebView/UserAgent.h>#include <LibWebView/ViewImplementation.h>#include <LibWebView/WebContentClient.h>+#include <math.h>namespace WebView {@@ -714,6 +715,11 @@ static constexpr auto longest_user_activation_delivery = AK::Duration::from_secovoid ViewImplementation::enqueue_input_event(Web::InputEvent event){+ // NB: A pinch event's scale delta must be finite to be sent over IPC. Drop a pinch event the platform gave us+ // with a scale delta that is not finite.+ if (auto const* pinch_event = event.get_pointer<Web::PinchEvent>(); pinch_event && !isfinite(pinch_event->scale_delta))+ return;+auto* key_event = event.get_pointer<Web::KeyEvent>();auto* mouse_event = event.get_pointer<Web::MouseEvent>();if (m_debugger_paused) {