From f8b6369c238a57abb2fb3f5b093c5b629070e1f4 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 20 Mar 2023 19:52:00 -0400 Subject: [PATCH] WebContent+Everywhere: Add a WebContent IPC to activate a tab --- Ladybird/BrowserWindow.cpp | 5 +++++ Ladybird/BrowserWindow.h | 1 + Ladybird/Tab.cpp | 4 ++++ Ladybird/WebContentView.cpp | 5 +++++ Ladybird/WebContentView.h | 2 ++ Userland/Applications/Browser/BrowserWindow.cpp | 4 ++++ Userland/Applications/Browser/Tab.cpp | 4 ++++ Userland/Applications/Browser/Tab.h | 1 + Userland/Libraries/LibWeb/Page/Page.h | 1 + Userland/Libraries/LibWebView/OutOfProcessWebView.cpp | 6 ++++++ Userland/Libraries/LibWebView/OutOfProcessWebView.h | 2 ++ Userland/Libraries/LibWebView/ViewImplementation.h | 1 + Userland/Libraries/LibWebView/WebContentClient.cpp | 5 +++++ Userland/Libraries/LibWebView/WebContentClient.h | 1 + Userland/Services/WebContent/PageHost.cpp | 5 +++++ Userland/Services/WebContent/PageHost.h | 1 + Userland/Services/WebContent/WebContentClient.ipc | 1 + Userland/Utilities/headless-browser.cpp | 1 + 18 files changed, 50 insertions(+) diff --git a/Ladybird/BrowserWindow.cpp b/Ladybird/BrowserWindow.cpp index 5b4c61d7fc..c346187d8f 100644 --- a/Ladybird/BrowserWindow.cpp +++ b/Ladybird/BrowserWindow.cpp @@ -401,6 +401,11 @@ Tab& BrowserWindow::new_tab(QString const& url, Web::HTML::ActivateTab activate_ return *tab_ptr; } +void BrowserWindow::activate_tab(int index) +{ + m_tabs_container->setCurrentIndex(index); +} + void BrowserWindow::close_tab(int index) { auto* tab = m_tabs_container->widget(index); diff --git a/Ladybird/BrowserWindow.h b/Ladybird/BrowserWindow.h index 1e6ed62ebf..bdf6f58110 100644 --- a/Ladybird/BrowserWindow.h +++ b/Ladybird/BrowserWindow.h @@ -36,6 +36,7 @@ public slots: void tab_title_changed(int index, QString const&); void tab_favicon_changed(int index, QIcon icon); Tab& new_tab(QString const&, Web::HTML::ActivateTab); + void activate_tab(int index); void close_tab(int index); void close_current_tab(); void open_next_tab(); diff --git a/Ladybird/Tab.cpp b/Ladybird/Tab.cpp index 1c5ac9349f..3da6992bf0 100644 --- a/Ladybird/Tab.cpp +++ b/Ladybird/Tab.cpp @@ -64,6 +64,10 @@ Tab::Tab(BrowserWindow* window, StringView webdriver_content_ipc_path) m_toolbar->addAction(m_home_action); m_toolbar->addWidget(m_location_edit); + QObject::connect(m_view, &WebContentView::activate_tab, [this] { + m_window->activate_tab(tab_index()); + }); + QObject::connect(m_view, &WebContentView::close, [this] { m_window->close_tab(tab_index()); }); diff --git a/Ladybird/WebContentView.cpp b/Ladybird/WebContentView.cpp index f4f4ed5c2e..7f3769c89c 100644 --- a/Ladybird/WebContentView.cpp +++ b/Ladybird/WebContentView.cpp @@ -988,6 +988,11 @@ String WebContentView::notify_server_did_request_new_tab(Badge return {}; } +void WebContentView::notify_server_did_request_activate_tab(Badge) +{ + emit activate_tab(); +} + void WebContentView::notify_server_did_update_resource_count(i32 count_waiting) { // FIXME diff --git a/Ladybird/WebContentView.h b/Ladybird/WebContentView.h index 4aac35aa05..9d84e1604d 100644 --- a/Ladybird/WebContentView.h +++ b/Ladybird/WebContentView.h @@ -148,6 +148,7 @@ public: virtual void notify_server_did_set_cookie(Badge, const AK::URL& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source) override; virtual void notify_server_did_update_cookie(Badge, Web::Cookie::Cookie const& cookie) override; virtual String notify_server_did_request_new_tab(Badge, Web::HTML::ActivateTab activate_tab) override; + virtual void notify_server_did_request_activate_tab(Badge) override; virtual void notify_server_did_close_browsing_context(Badge) override; virtual void notify_server_did_update_resource_count(i32 count_waiting) override; virtual void notify_server_did_request_restore_window() override; @@ -160,6 +161,7 @@ public: virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) override; signals: + void activate_tab(); void close(); void link_hovered(QString, int timeout = 0); void link_unhovered(); diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index de2545656e..b796b8c586 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -590,6 +590,10 @@ Tab& BrowserWindow::create_new_tab(URL url, Web::HTML::ActivateTab activate) create_new_tab(url, Web::HTML::ActivateTab::Yes); }; + new_tab.on_activate_tab_request = [this](auto& tab) { + m_tab_widget->set_active_widget(&tab); + }; + new_tab.on_tab_close_request = [this](auto& tab) { m_tab_widget->deferred_invoke([this, &tab] { m_tab_widget->remove_tab(tab); diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 02e1a5a8ea..40759fea08 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -473,6 +473,10 @@ Tab::Tab(BrowserWindow& window) return tab.view().handle(); }; + view().on_activate_tab = [this]() { + on_activate_tab_request(*this); + }; + view().on_close = [this] { on_tab_close_request(*this); }; diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index 84ef7d316d..bb7f13ed32 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -62,6 +62,7 @@ public: Function on_title_change; Function on_tab_open_request; + Function on_activate_tab_request; Function on_tab_close_request; Function on_tab_close_other_request; Function on_window_open_request; diff --git a/Userland/Libraries/LibWeb/Page/Page.h b/Userland/Libraries/LibWeb/Page/Page.h index f3b54501aa..d78834cd38 100644 --- a/Userland/Libraries/LibWeb/Page/Page.h +++ b/Userland/Libraries/LibWeb/Page/Page.h @@ -203,6 +203,7 @@ public: virtual void page_did_update_cookie(Web::Cookie::Cookie) { } virtual void page_did_update_resource_count(i32) { } virtual String page_did_request_new_tab(HTML::ActivateTab) { return {}; } + virtual void page_did_request_activate_tab() { } virtual void page_did_close_browsing_context(HTML::BrowsingContext const&) { } virtual void request_file(FileRequest) = 0; diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp index 8470c1296c..fe767b09b7 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp @@ -480,6 +480,12 @@ String OutOfProcessWebView::notify_server_did_request_new_tab(Badge) +{ + if (on_activate_tab) + on_activate_tab(); +} + void OutOfProcessWebView::notify_server_did_close_browsing_context(Badge) { if (on_close) diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.h b/Userland/Libraries/LibWebView/OutOfProcessWebView.h index 66a8e364f7..e521b29751 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.h @@ -59,6 +59,7 @@ public: void set_content_scales_to_viewport(bool); Function on_new_tab; + Function on_activate_tab; Function on_close; Function on_context_menu_request; Function on_link_click; @@ -164,6 +165,7 @@ private: virtual void notify_server_did_set_cookie(Badge, const AK::URL& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source) override; virtual void notify_server_did_update_cookie(Badge, Web::Cookie::Cookie const& cookie) override; virtual String notify_server_did_request_new_tab(Badge, Web::HTML::ActivateTab activate_tab) override; + virtual void notify_server_did_request_activate_tab(Badge) override; virtual void notify_server_did_close_browsing_context(Badge) override; virtual void notify_server_did_update_resource_count(i32 count_waiting) override; virtual void notify_server_did_request_restore_window() override; diff --git a/Userland/Libraries/LibWebView/ViewImplementation.h b/Userland/Libraries/LibWebView/ViewImplementation.h index a88b3bd125..613cad0410 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.h +++ b/Userland/Libraries/LibWebView/ViewImplementation.h @@ -100,6 +100,7 @@ public: virtual void notify_server_did_set_cookie(Badge, const AK::URL& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source) = 0; virtual void notify_server_did_update_cookie(Badge, Web::Cookie::Cookie const& cookie) = 0; virtual String notify_server_did_request_new_tab(Badge, Web::HTML::ActivateTab activate_tab) = 0; + virtual void notify_server_did_request_activate_tab(Badge) = 0; virtual void notify_server_did_close_browsing_context(Badge) = 0; virtual void notify_server_did_update_resource_count(i32 count_waiting) = 0; virtual void notify_server_did_request_restore_window() = 0; diff --git a/Userland/Libraries/LibWebView/WebContentClient.cpp b/Userland/Libraries/LibWebView/WebContentClient.cpp index d9b85d350c..5e53927197 100644 --- a/Userland/Libraries/LibWebView/WebContentClient.cpp +++ b/Userland/Libraries/LibWebView/WebContentClient.cpp @@ -245,6 +245,11 @@ Messages::WebContentClient::DidRequestNewTabResponse WebContentClient::did_reque return m_view.notify_server_did_request_new_tab({}, activate_tab); } +void WebContentClient::did_request_activate_tab() +{ + m_view.notify_server_did_request_activate_tab({}); +} + void WebContentClient::did_close_browsing_context() { m_view.notify_server_did_close_browsing_context({}); diff --git a/Userland/Libraries/LibWebView/WebContentClient.h b/Userland/Libraries/LibWebView/WebContentClient.h index a7d32ec695..295affda79 100644 --- a/Userland/Libraries/LibWebView/WebContentClient.h +++ b/Userland/Libraries/LibWebView/WebContentClient.h @@ -71,6 +71,7 @@ private: virtual void did_set_cookie(AK::URL const&, Web::Cookie::ParsedCookie const&, u8) override; virtual void did_update_cookie(Web::Cookie::Cookie const&) override; virtual Messages::WebContentClient::DidRequestNewTabResponse did_request_new_tab(Web::HTML::ActivateTab const& activate_tab) override; + virtual void did_request_activate_tab() override; virtual void did_close_browsing_context() override; virtual void did_update_resource_count(i32 count_waiting) override; virtual void did_request_restore_window() override; diff --git a/Userland/Services/WebContent/PageHost.cpp b/Userland/Services/WebContent/PageHost.cpp index 0ddc552154..005a619fde 100644 --- a/Userland/Services/WebContent/PageHost.cpp +++ b/Userland/Services/WebContent/PageHost.cpp @@ -382,6 +382,11 @@ String PageHost::page_did_request_new_tab(Web::HTML::ActivateTab activate_tab) return m_client.did_request_new_tab(activate_tab); } +void PageHost::page_did_request_activate_tab() +{ + m_client.async_did_request_activate_tab(); +} + void PageHost::page_did_close_browsing_context(Web::HTML::BrowsingContext const&) { m_client.async_did_close_browsing_context(); diff --git a/Userland/Services/WebContent/PageHost.h b/Userland/Services/WebContent/PageHost.h index faf8e2250b..7d81a7cb2a 100644 --- a/Userland/Services/WebContent/PageHost.h +++ b/Userland/Services/WebContent/PageHost.h @@ -99,6 +99,7 @@ private: virtual void page_did_update_cookie(Web::Cookie::Cookie) override; virtual void page_did_update_resource_count(i32) override; virtual String page_did_request_new_tab(Web::HTML::ActivateTab activate_tab) override; + virtual void page_did_request_activate_tab() override; virtual void page_did_close_browsing_context(Web::HTML::BrowsingContext const&) override; virtual void request_file(Web::FileRequest) override; diff --git a/Userland/Services/WebContent/WebContentClient.ipc b/Userland/Services/WebContent/WebContentClient.ipc index 4d7df2ef50..06f0516a30 100644 --- a/Userland/Services/WebContent/WebContentClient.ipc +++ b/Userland/Services/WebContent/WebContentClient.ipc @@ -48,6 +48,7 @@ endpoint WebContentClient did_update_cookie(Web::Cookie::Cookie cookie) =| did_update_resource_count(i32 count_waiting) =| did_request_new_tab(Web::HTML::ActivateTab activate_tab) => (String handle) + did_request_activate_tab() =| did_close_browsing_context() =| did_request_restore_window() =| did_request_reposition_window(Gfx::IntPoint position) => (Gfx::IntPoint window_position) diff --git a/Userland/Utilities/headless-browser.cpp b/Userland/Utilities/headless-browser.cpp index a3e4599f57..2f081e2415 100644 --- a/Userland/Utilities/headless-browser.cpp +++ b/Userland/Utilities/headless-browser.cpp @@ -134,6 +134,7 @@ private: void notify_server_did_set_cookie(Badge, const URL&, Web::Cookie::ParsedCookie const&, Web::Cookie::Source) override { } void notify_server_did_update_cookie(Badge, Web::Cookie::Cookie const&) override { } String notify_server_did_request_new_tab(Badge, Web::HTML::ActivateTab) override { return {}; } + void notify_server_did_request_activate_tab(Badge) override { } void notify_server_did_close_browsing_context(Badge) override { } void notify_server_did_update_resource_count(i32) override { } void notify_server_did_request_restore_window() override { }