Skip to content

Truly independent web browser

Hub+UI/Qt: Send input to the Hub over IPC

Andreas Kling789a4faac405
diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cpp
index ffbb8a6fe51..e75cd0c0b19 100644
--- a/Services/Hub/ConnectionFromUI.cpp
+++ b/Services/Hub/ConnectionFromUI.cpp
@@ -30,6 +30,64 @@ void ConnectionFromUI::die()
{
}
+// What the UI knows an input event by, while the Hub has it. The event carries it through the view's queue, in place of
+// the data about the event that only the UI can hold. Once the event is gone, the UI can let go of that data.
+struct InputFromUI final : public Web::BrowserInputData {
+ InputFromUI(u64 view_id, u64 input_id)
+ : view_id(view_id)
+ , input_id(input_id)
+ {
+ }
+
+ virtual ~InputFromUI() override
+ {
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_view_release_input(view_id, input_id);
+ }
+
+ u64 view_id { 0 };
+ u64 input_id { 0 };
+};
+
+u64 ConnectionFromUI::input_id_of(Web::BrowserInputData const& browser_data)
+{
+ return as<InputFromUI>(browser_data).input_id;
+}
+
+template<typename EventType>
+static void enqueue_input_from_ui(View& view, u64 input_id, EventType event)
+{
+ event.browser_data = make<InputFromUI>(view.view_id(), input_id);
+ view.enqueue_input_event(move(event));
+ view.push_state_to_ui();
+}
+
+void ConnectionFromUI::view_enqueue_key_event(u64 view_id, u64 input_id, Web::KeyEvent event)
+{
+ if (auto* view = find_view(view_id))
+ enqueue_input_from_ui(*view, input_id, move(event));
+}
+
+void ConnectionFromUI::view_enqueue_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event)
+{
+ if (auto* view = find_view(view_id))
+ enqueue_input_from_ui(*view, input_id, move(event));
+}
+
+void ConnectionFromUI::view_enqueue_drag_event(u64 view_id, u64 input_id, Web::DragEvent event)
+{
+ if (auto* view = find_view(view_id))
+ enqueue_input_from_ui(*view, input_id, move(event));
+}
+
+void ConnectionFromUI::view_enqueue_pinch_event(u64 view_id, Web::PinchEvent event)
+{
+ if (auto* view = find_view(view_id)) {
+ view->enqueue_input_event(move(event));
+ view->push_state_to_ui();
+ }
+}
+
View* ConnectionFromUI::find_view(u64 view_id)
{
auto view = WebView::ViewImplementation::find_view_by_id(view_id);
diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.h
index 0e5d4a5c317..db8138ea1a4 100644
--- a/Services/Hub/ConnectionFromUI.h
+++ b/Services/Hub/ConnectionFromUI.h
@@ -22,6 +22,9 @@ public:
static ConnectionFromUI* the();
+ // The ID that the UI gave an input event, from the browser data of the event.
+ static u64 input_id_of(Web::BrowserInputData const&);
+
virtual void die() override;
private:
@@ -30,6 +33,10 @@ private:
static View* find_view(u64 view_id);
// ^HubServerEndpoint
+ virtual void view_enqueue_key_event(u64 view_id, u64 input_id, Web::KeyEvent event) override;
+ virtual void view_enqueue_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event) override;
+ virtual void view_enqueue_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) override;
+ virtual void view_enqueue_pinch_event(u64 view_id, Web::PinchEvent event) override;
virtual void view_load(u64 view_id, URL::URL url, Web::Bindings::NavigationHistoryBehavior history_behavior) override;
virtual void view_load_url_from_user_input(u64 view_id, URL::URL url) override;
virtual void view_load_from_user_input(u64 view_id, ByteString input, Optional<URL::URL> fallback_url) override;
diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipc
index e6aeeb8dc93..c6a3f539e6a 100644
--- a/Services/Hub/HubClient.ipc
+++ b/Services/Hub/HubClient.ipc
@@ -92,6 +92,10 @@ struct Hub::ViewState {
// What the Hub tells the UI.
endpoint HubClient
{
+ view_finish_handling_key_event(u64 view_id, u64 input_id, Web::KeyEvent event) =|
+ view_finish_handling_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event, Web::EventResult result) =|
+ view_finish_handling_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) =|
+ view_release_input(u64 view_id, u64 input_id) =|
view_state_changed(u64 view_id, Hub::ViewState state) =|
view_load_start(u64 view_id) =|
view_load_finish(u64 view_id, URL::URL url) =|
diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipc
index 3f882341af8..327cd642ed8 100644
--- a/Services/Hub/HubServer.ipc
+++ b/Services/Hub/HubServer.ipc
@@ -11,6 +11,10 @@
// What the UI asks of the Hub.
endpoint HubServer
{
+ view_enqueue_key_event(u64 view_id, u64 input_id, Web::KeyEvent event) =|
+ view_enqueue_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event) =|
+ view_enqueue_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) =|
+ view_enqueue_pinch_event(u64 view_id, Web::PinchEvent event) =|
view_load(u64 view_id, URL::URL url, Web::Bindings::NavigationHistoryBehavior history_behavior) =|
view_load_url_from_user_input(u64 view_id, URL::URL url) =|
view_load_from_user_input(u64 view_id, ByteString input, Optional<URL::URL> fallback_url) =|
diff --git a/Services/Hub/View.cpp b/Services/Hub/View.cpp
index 7c2b20f80e0..afd8af767e3 100644
--- a/Services/Hub/View.cpp
+++ b/Services/Hub/View.cpp
@@ -21,6 +21,35 @@ View::View(ViewHost& host, InitialState initial_state)
void View::forward_callbacks_to_ui()
{
+ 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)
+ 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());
+ }
+ };
+
+ on_finish_handling_mouse_event = [this](Web::MouseEvent const& event, Web::EventResult result) {
+ if (!event.browser_data)
+ 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);
+ }
+ };
+
+ 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)
+ 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());
+ }
+ };
+
on_load_start = [this] {
if (auto* ui = ConnectionFromUI::the()) {
push_state_to_ui();
diff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cpp
index 5894300d4eb..0488b25b8ac 100644
--- a/UI/Qt/HubConnection.cpp
+++ b/UI/Qt/HubConnection.cpp
@@ -31,6 +31,30 @@ void HubConnection::die()
{
}
+void HubConnection::view_finish_handling_key_event(u64 view_id, u64 input_id, Web::KeyEvent event)
+{
+ if (auto* view = RemoteView::find(view_id))
+ view->did_finish_handling_key_event(input_id, move(event));
+}
+
+void HubConnection::view_finish_handling_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event, Web::EventResult result)
+{
+ if (auto* view = RemoteView::find(view_id))
+ view->did_finish_handling_mouse_event(input_id, move(event), result);
+}
+
+void HubConnection::view_finish_handling_drag_event(u64 view_id, u64 input_id, Web::DragEvent event)
+{
+ if (auto* view = RemoteView::find(view_id))
+ view->did_finish_handling_drag_event(input_id, move(event));
+}
+
+void HubConnection::view_release_input(u64 view_id, u64 input_id)
+{
+ if (auto* view = RemoteView::find(view_id))
+ view->release_input(input_id);
+}
+
void HubConnection::view_state_changed(u64 view_id, Hub::ViewState state)
{
if (auto* view = RemoteView::find(view_id))
diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.h
index 6d035101883..e05389f6231 100644
--- a/UI/Qt/HubConnection.h
+++ b/UI/Qt/HubConnection.h
@@ -28,6 +28,10 @@ private:
virtual void die() override;
// ^HubClientEndpoint
+ virtual void view_finish_handling_key_event(u64 view_id, u64 input_id, Web::KeyEvent event) override;
+ virtual void view_finish_handling_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event, Web::EventResult result) override;
+ virtual void view_finish_handling_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) override;
+ virtual void view_release_input(u64 view_id, u64 input_id) override;
virtual void view_state_changed(u64 view_id, Hub::ViewState state) override;
virtual void view_load_start(u64 view_id) override;
virtual void view_load_finish(u64 view_id, URL::URL url) override;
diff --git a/UI/Qt/RemoteView.cpp b/UI/Qt/RemoteView.cpp
index 15d26f22a21..4b9932d5151 100644
--- a/UI/Qt/RemoteView.cpp
+++ b/UI/Qt/RemoteView.cpp
@@ -23,6 +23,56 @@ RemoteView::~RemoteView()
m_view->detach_from_host();
}
+void RemoteView::enqueue_input_event(Web::InputEvent event)
+{
+ auto input_id = m_next_input_id++;
+ event.visit(
+ [&](Web::KeyEvent& event) {
+ m_input_in_hub.set(input_id, move(event.browser_data));
+ HubConnection::the().async_view_enqueue_key_event(view_id(), input_id, event.clone_without_browser_data());
+ },
+ [&](Web::MouseEvent& event) {
+ m_input_in_hub.set(input_id, move(event.browser_data));
+ HubConnection::the().async_view_enqueue_mouse_event(view_id(), input_id, event.clone_without_browser_data());
+ },
+ [&](Web::DragEvent& event) {
+ m_input_in_hub.set(input_id, move(event.browser_data));
+ auto event_to_send = event.clone_without_browser_data();
+ event_to_send.files = move(event.files);
+ HubConnection::the().async_view_enqueue_drag_event(view_id(), input_id, move(event_to_send));
+ },
+ [&](Web::PinchEvent& event) {
+ HubConnection::the().async_view_enqueue_pinch_event(view_id(), event);
+ });
+}
+
+void RemoteView::did_finish_handling_key_event(u64 input_id, Web::KeyEvent event)
+{
+ if (!on_finish_handling_key_event)
+ return;
+ if (auto browser_data = m_input_in_hub.get(input_id); browser_data.has_value())
+ event.browser_data = m_input_in_hub.take(input_id).release_value();
+ on_finish_handling_key_event(event);
+}
+
+void RemoteView::did_finish_handling_mouse_event(u64 input_id, Web::MouseEvent event, Web::EventResult result)
+{
+ if (!on_finish_handling_mouse_event)
+ return;
+ if (auto browser_data = m_input_in_hub.get(input_id); browser_data.has_value())
+ event.browser_data = m_input_in_hub.take(input_id).release_value();
+ on_finish_handling_mouse_event(event, result);
+}
+
+void RemoteView::did_finish_handling_drag_event(u64 input_id, Web::DragEvent event)
+{
+ if (!on_finish_handling_drag_event)
+ return;
+ if (auto browser_data = m_input_in_hub.get(input_id); browser_data.has_value())
+ event.browser_data = m_input_in_hub.take(input_id).release_value();
+ on_finish_handling_drag_event(event);
+}
+
RemoteView* RemoteView::find(u64 view_id)
{
return s_views.get(view_id).value_or(nullptr);
diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.h
index ba697930cc6..c17a50cd121 100644
--- a/UI/Qt/RemoteView.h
+++ b/UI/Qt/RemoteView.h
@@ -32,6 +32,11 @@ public:
void set_state(Hub::ViewState state) { m_state = move(state); }
+ void did_finish_handling_key_event(u64 input_id, Web::KeyEvent);
+ void did_finish_handling_mouse_event(u64 input_id, Web::MouseEvent, Web::EventResult);
+ void did_finish_handling_drag_event(u64 input_id, Web::DragEvent);
+ void release_input(u64 input_id) { m_input_in_hub.remove(input_id); }
+
Hub::View& hub_view() { return *m_view; }
Hub::View const& hub_view() const { return *m_view; }
@@ -89,7 +94,7 @@ public:
void update_screen_rects() { HubConnection::the().async_view_update_screen_rects(view_id()); }
void set_scales_frames_to_fit(bool scales_frames_to_fit) { HubConnection::the().async_view_set_scales_frames_to_fit(view_id(), scales_frames_to_fit); }
void enable_picture_in_picture_controls() { HubConnection::the().async_view_enable_picture_in_picture_controls(view_id()); }
- void enqueue_input_event(Web::InputEvent input_event) { m_view->enqueue_input_event(move(input_event)); }
+ void enqueue_input_event(Web::InputEvent);
String force_dark_host() const { return m_state.force_dark_host; }
void update_interface_color_scheme() { HubConnection::the().async_view_update_interface_color_scheme(view_id()); }
void update_system_theme(Core::AnonymousBuffer const& anonymous_buffer) { m_view->update_system_theme(anonymous_buffer); }
@@ -183,6 +188,10 @@ private:
NonnullOwnPtr<Hub::View> m_view;
Hub::ViewState m_state;
+ // What only the UI can hold of the input events that the Hub has yet to finish with.
+ HashMap<u64, OwnPtr<Web::BrowserInputData>> m_input_in_hub;
+ u64 m_next_input_id { 1 };
+
public:
decltype(Hub::View::on_load_start) on_load_start;
decltype(Hub::View::on_load_finish) on_load_finish;
@@ -233,10 +242,10 @@ public:
decltype(Hub::View::on_tab_indicator_change) on_tab_indicator_change;
decltype(Hub::View::on_request_file_picker) on_request_file_picker;
decltype(Hub::View::on_request_select_dropdown) on_request_select_dropdown;
- decltype(Hub::View::on_finish_handling_key_event)& on_finish_handling_key_event { m_view->on_finish_handling_key_event };
- decltype(Hub::View::on_finish_handling_mouse_event)& on_finish_handling_mouse_event { m_view->on_finish_handling_mouse_event };
+ decltype(Hub::View::on_finish_handling_key_event) on_finish_handling_key_event;
+ decltype(Hub::View::on_finish_handling_mouse_event) on_finish_handling_mouse_event;
decltype(Hub::View::on_picture_in_picture_control_activated) on_picture_in_picture_control_activated;
- decltype(Hub::View::on_finish_handling_drag_event)& on_finish_handling_drag_event { m_view->on_finish_handling_drag_event };
+ decltype(Hub::View::on_finish_handling_drag_event) on_finish_handling_drag_event;
decltype(Hub::View::on_find_in_page) on_find_in_page;
decltype(Hub::View::on_page_background_color_change) on_page_background_color_change;
decltype(Hub::View::on_screen_wake_lock_state_changed) on_screen_wake_lock_state_changed;