Truly independent web browser
UI/Qt: Request external URL activation tokens through Wayland
Timothy Flynn69969828a08d
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txtindex 227215de215..18d0bb5bbe5 100644--- a/UI/Qt/CMakeLists.txt+++ b/UI/Qt/CMakeLists.txt@@ -5,9 +5,9 @@ set(CMAKE_AUTOUIC ON)if (LINUX AND NOT ANDROID)find_package(PkgConfig REQUIRED)pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0 gio-unix-2.0)- # NB: Without the Wayland client library, a tab dragged out of a window does not take a window along with it.+ # Without the Wayland client library, a tab dragged out of a window does not take a window along with it.pkg_check_modules(WAYLAND_CLIENT IMPORTED_TARGET wayland-client)- # NB: Without the Wayland scanner, the browser cannot ask the Wayland compositor which GPU it presents on.+ # The Wayland scanner is used for external activation and direct page presentation.pkg_check_modules(WAYLAND_SCANNER wayland-scanner)pkg_check_modules(XCB_ICCCM REQUIRED IMPORTED_TARGET xcb-icccm)endif()@@ -102,9 +102,18 @@ if (LINUX AND NOT ANDROID)target_link_libraries(ladybird_impl PRIVATE PkgConfig::WAYLAND_CLIENT)target_compile_definitions(ladybird_impl PUBLIC LADYBIRD_HAS_WAYLAND_CLIENT)endif()- if (WAYLAND_CLIENT_FOUND AND WAYLAND_SCANNER_FOUND AND USE_VULKAN_DMABUF_IMAGES)++ if (WAYLAND_CLIENT_FOUND AND WAYLAND_SCANNER_FOUND)pkg_get_variable(WAYLAND_SCANNER_EXECUTABLE wayland-scanner wayland_scanner)- foreach(protocol linux-dmabuf-v1 viewporter)++ # xdg-activation-v1.xml comes from the wayland-protocols:+ # https://github.com/wayland-mirror/wayland-protocols/blob/1.41/staging/xdg-activation/xdg-activation-v1.xml+ set(wayland_protocols xdg-activation-v1)+ if (USE_VULKAN_DMABUF_IMAGES)+ list(APPEND wayland_protocols linux-dmabuf-v1 viewporter)+ endif()++ foreach(protocol IN LISTS wayland_protocols)set(protocol_xml "${CMAKE_CURRENT_SOURCE_DIR}/Protocols/${protocol}.xml")set(protocol_header "${CMAKE_CURRENT_BINARY_DIR}/${protocol}-client-protocol.h")set(protocol_code "${CMAKE_CURRENT_BINARY_DIR}/${protocol}-protocol.c")@@ -122,15 +131,19 @@ if (LINUX AND NOT ANDROID))target_sources(ladybird_impl PRIVATE "${protocol_header}" "${protocol_code}")endforeach()++ target_sources(ladybird_impl PRIVATE WaylandRegistry.cpp WaylandRegistry.h)+ target_include_directories(ladybird_impl PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")+ target_compile_definitions(ladybird_impl PRIVATE LADYBIRD_HAS_WAYLAND_ACTIVATION)+ endif()++ if (WAYLAND_CLIENT_FOUND AND WAYLAND_SCANNER_FOUND AND USE_VULKAN_DMABUF_IMAGES)target_sources(ladybird_impl PRIVATEWaylandGpu.cppWaylandGpu.hWaylandPageSurface.cppWaylandPageSurface.h- WaylandRegistry.cpp- WaylandRegistry.h)- target_include_directories(ladybird_impl PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")target_compile_definitions(ladybird_impl PUBLIC LADYBIRD_HAS_WAYLAND_GPU_QUERY)else()message(WARNING "Building without the Wayland page surface. Pages on Wayland will use a fallback that can fall "diff --git a/UI/Qt/ExternalURLActivationToken.cpp b/UI/Qt/ExternalURLActivationToken.cppindex 82a56376305..d918843e553 100644--- a/UI/Qt/ExternalURLActivationToken.cpp+++ b/UI/Qt/ExternalURLActivationToken.cpp@@ -4,77 +4,121 @@* SPDX-License-Identifier: BSD-2-Clause*/-#include <AK/RefCounted.h>-#include <AK/RefPtr.h>-#include <QGuiApplication>-#include <QMetaObject>-#include <QTimer>#include <UI/Qt/ExternalURLActivationToken.h>-#include <UI/Qt/StringUtils.h>-#include <qpa/qplatformwindow_p.h>++#if defined(LADYBIRD_HAS_WAYLAND_ACTIVATION)+# include <AK/ByteString.h>+# include <AK/kmalloc.h>+# include <UI/Qt/WaylandRegistry.h>+# include <UI/Qt/WaylandWindow.h>++# include <QGuiApplication>+# include <QTimer>+# include <QWindow>++// The generated listener functions cast the const away from the listeners they pass to libwayland.+# pragma GCC diagnostic push+# pragma GCC diagnostic ignored "-Wcast-qual"+# include <xdg-activation-v1-client-protocol.h>+# pragma GCC diagnostic pop+#endifnamespace Ladybird {-class ActivationTokenRequest final : public RefCounted<ActivationTokenRequest> {+#if defined(LADYBIRD_HAS_WAYLAND_ACTIVATION)+class ActivationTokenRequest final : public QObject {public:- explicit ActivationTokenRequest(Function<void(Optional<String>)> callback)- : m_callback(move(callback))- {- }+ AK_ALLOC_WITH_KMALLOC;- void set_connection(QMetaObject::Connection connection)+ ActivationTokenRequest(QGuiApplication& application, xdg_activation_token_v1& token, Function<void(Optional<String>)> callback)+ : QObject(&application)+ , m_token(&token)+ , m_callback(move(callback)){- m_connection = move(connection);+ static xdg_activation_token_v1_listener const listener {+ .done = [](void* data, xdg_activation_token_v1*, char const* token) {+ auto string = String::from_utf8({ token, strlen(token) });+ static_cast<ActivationTokenRequest*>(data)->complete(string.is_error() ? Optional<String> {} : string.release_value());+ },+ };++ xdg_activation_token_v1_add_listener(m_token, &listener, this);++ QTimer::singleShot(1000, this, [this] { complete({}); });++ QObject::connect(&application, &QCoreApplication::aboutToQuit, this, [this] {+ m_callback = nullptr;+ complete({});+ });}+private:void complete(Optional<String> token){- if (!m_callback)+ if (!m_token)return;- QObject::disconnect(m_connection);- auto callback = move(m_callback);- callback(move(token));+ xdg_activation_token_v1_destroy(exchange(m_token, nullptr));+ deleteLater();++ if (auto callback = move(m_callback))+ callback(move(token));}-private:+ xdg_activation_token_v1* m_token { nullptr };Function<void(Optional<String>)> m_callback;- QMetaObject::Connection m_connection;};-void request_external_url_activation_token(Function<void(Optional<String>)> callback)+static xdg_activation_v1* wayland_activation(){+ static auto* activation = []() -> xdg_activation_v1* {+ auto registry = WaylandRegistry::create();+ if (!registry)+ return nullptr;++ auto* activation = registry->bind<xdg_activation_v1>(xdg_activation_v1_interface, 1);+ if (activation)+ WaylandRegistry::move_to_qt_queue(activation);++ return activation;+ }();++ return activation;+}+#endif++void request_external_url_activation_token([[maybe_unused]] StringView desktop_id, Function<void(Optional<String>)> callback)+{+#if defined(LADYBIRD_HAS_WAYLAND_ACTIVATION)auto* application = as_if<QGuiApplication>(QCoreApplication::instance());- if (!application) {- callback({});- return;- }+ auto* window = application ? QGuiApplication::focusWindow() : nullptr;+ auto* surface = window ? wayland_surface_for_window(*window) : nullptr;- auto* window = QGuiApplication::focusWindow();- auto* wayland_application = application->nativeInterface<QNativeInterface::QWaylandApplication>();- auto* wayland_window = window- ? window->nativeInterface<QNativeInterface::Private::QWaylandWindow>()- : nullptr;- if (!wayland_application || !wayland_window) {- callback({});- return;+ if (surface) {+ auto* wayland = application->nativeInterface<QNativeInterface::QWaylandApplication>();++ if (auto* activation = wayland_activation()) {+ auto* token = xdg_activation_v1_get_activation_token(activation);+ xdg_activation_token_v1_set_surface(token, surface);++ auto serial = wayland->lastInputSerial();+ if (auto* seat = wayland->lastInputSeat(); seat && serial)+ xdg_activation_token_v1_set_serial(token, serial, seat);++ static constexpr auto desktop_suffix = ".desktop"sv;+ if (desktop_id.ends_with(desktop_suffix) && desktop_id.length() > desktop_suffix.length()) {+ auto app_id = desktop_id.substring_view(0, desktop_id.length() - desktop_suffix.length()).to_byte_string();+ xdg_activation_token_v1_set_app_id(token, app_id.characters());+ }++ new ActivationTokenRequest(*application, *token, move(callback));+ xdg_activation_token_v1_commit(token);+ return;+ }}+#endif- auto request = adopt_ref(*new ActivationTokenRequest(move(callback)));- request->set_connection(QObject::connect(- wayland_window,- &QNativeInterface::Private::QWaylandWindow::xdgActivationTokenCreated,- application,- [request](QString const& token) {- request->complete(ak_string_from_qstring(token));- },- Qt::SingleShotConnection));-- wayland_window->requestXdgActivationToken(wayland_application->lastInputSerial());-- QTimer::singleShot(1000, application, [request] {- request->complete({});- });+ callback({});}}diff --git a/UI/Qt/ExternalURLActivationToken.h b/UI/Qt/ExternalURLActivationToken.hindex bf7014cb65a..353573ca53e 100644--- a/UI/Qt/ExternalURLActivationToken.h+++ b/UI/Qt/ExternalURLActivationToken.h@@ -9,9 +9,10 @@#include <AK/Function.h>#include <AK/Optional.h>#include <AK/String.h>+#include <AK/StringView.h>namespace Ladybird {-void request_external_url_activation_token(Function<void(Optional<String>)>);+void request_external_url_activation_token(StringView desktop_id, Function<void(Optional<String>)>);}diff --git a/UI/Qt/ExternalURLHandler.cpp b/UI/Qt/ExternalURLHandler.cppindex d97074fbf8d..4cf78aa18e1 100644--- a/UI/Qt/ExternalURLHandler.cpp+++ b/UI/Qt/ExternalURLHandler.cpp@@ -158,7 +158,11 @@ public:virtual void launch(URL::URL const& url, LaunchCallback callback) const override{NonnullRefPtr protected_this { *this };- request_external_url_activation_token([protected_this = move(protected_this), url, callback = move(callback)](Optional<String> activation_token) mutable {++ auto const* desktop_id_ptr = g_app_info_get_id(m_app_info);+ auto desktop_id = desktop_id_ptr ? StringView { desktop_id_ptr, strlen(desktop_id_ptr) } : StringView {};++ request_external_url_activation_token(desktop_id, [protected_this = move(protected_this), url, callback = move(callback)](Optional<String> activation_token) mutable {callback(protected_this->launch(url, activation_token));});}diff --git a/UI/Qt/Protocols/xdg-activation-v1.xml b/UI/Qt/Protocols/xdg-activation-v1.xmlnew file mode 100644index 00000000000..9adcc274ef1--- /dev/null+++ b/UI/Qt/Protocols/xdg-activation-v1.xml@@ -0,0 +1,200 @@+<?xml version="1.0" encoding="UTF-8"?>+<protocol name="xdg_activation_v1">++ <copyright>+ Copyright © 2020 Aleix Pol Gonzalez <[email protected]>+ Copyright © 2020 Carlos Garnacho <[email protected]>++ Permission is hereby granted, free of charge, to any person obtaining a+ copy of this software and associated documentation files (the "Software"),+ to deal in the Software without restriction, including without limitation+ the rights to use, copy, modify, merge, publish, distribute, sublicense,+ and/or sell copies of the Software, and to permit persons to whom the+ Software is furnished to do so, subject to the following conditions:++ The above copyright notice and this permission notice (including the next+ paragraph) shall be included in all copies or substantial portions of the+ Software.++ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER+ DEALINGS IN THE SOFTWARE.+ </copyright>++ <description summary="Protocol for requesting activation of surfaces">+ The way for a client to pass focus to another toplevel is as follows.++ The client that intends to activate another toplevel uses the+ xdg_activation_v1.get_activation_token request to get an activation token.+ This token is then forwarded to the client, which is supposed to activate+ one of its surfaces, through a separate band of communication.++ One established way of doing this is through the XDG_ACTIVATION_TOKEN+ environment variable of a newly launched child process. The child process+ should unset the environment variable again right after reading it out in+ order to avoid propagating it to other child processes.++ Another established way exists for Applications implementing the D-Bus+ interface org.freedesktop.Application, which should get their token under+ activation-token on their platform_data.++ In general activation tokens may be transferred across clients through+ means not described in this protocol.++ The client to be activated will then pass the token+ it received to the xdg_activation_v1.activate request. The compositor can+ then use this token to decide how to react to the activation request.++ The token the activating client gets may be ineffective either already at+ the time it receives it, for example if it was not focused, for focus+ stealing prevention. The activating client will have no way to discover+ the validity of the token, and may still forward it to the to be activated+ client.++ The created activation token may optionally get information attached to it+ that can be used by the compositor to identify the application that we+ intend to activate. This can for example be used to display a visual hint+ about what application is being started.++ Warning! The protocol described in this file is currently in the testing+ phase. Backward compatible changes may be added together with the+ corresponding interface version bump. Backward incompatible changes can+ only be done by creating a new major version of the extension.+ </description>++ <interface name="xdg_activation_v1" version="1">+ <description summary="interface for activating surfaces">+ A global interface used for informing the compositor about applications+ being activated or started, or for applications to request to be+ activated.+ </description>++ <request name="destroy" type="destructor">+ <description summary="destroy the xdg_activation object">+ Notify the compositor that the xdg_activation object will no longer be+ used.++ The child objects created via this interface are unaffected and should+ be destroyed separately.+ </description>+ </request>++ <request name="get_activation_token">+ <description summary="requests a token">+ Creates an xdg_activation_token_v1 object that will provide+ the initiating client with a unique token for this activation. This+ token should be offered to the clients to be activated.+ </description>++ <arg name="id" type="new_id" interface="xdg_activation_token_v1"/>+ </request>++ <request name="activate">+ <description summary="notify new interaction being available">+ Requests surface activation. It's up to the compositor to display+ this information as desired, for example by placing the surface above+ the rest.++ The compositor may know who requested this by checking the activation+ token and might decide not to follow through with the activation if it's+ considered unwanted.++ Compositors can ignore unknown activation tokens when an invalid+ token is passed.+ </description>+ <arg name="token" type="string" summary="the activation token of the initiating client"/>+ <arg name="surface" type="object" interface="wl_surface"+ summary="the wl_surface to activate"/>+ </request>+ </interface>++ <interface name="xdg_activation_token_v1" version="1">+ <description summary="an exported activation handle">+ An object for setting up a token and receiving a token handle that can+ be passed as an activation token to another client.++ The object is created using the xdg_activation_v1.get_activation_token+ request. This object should then be populated with the app_id, surface+ and serial information and committed. The compositor shall then issue a+ done event with the token. In case the request's parameters are invalid,+ the compositor will provide an invalid token.+ </description>++ <enum name="error">+ <entry name="already_used" value="0"+ summary="The token has already been used previously"/>+ </enum>++ <request name="set_serial">+ <description summary="specifies the seat and serial of the activating event">+ Provides information about the seat and serial event that requested the+ token.++ The serial can come from an input or focus event. For instance, if a+ click triggers the launch of a third-party client, the launcher client+ should send a set_serial request with the serial and seat from the+ wl_pointer.button event.++ Some compositors might refuse to activate toplevels when the token+ doesn't have a valid and recent enough event serial.++ Must be sent before commit. This information is optional.+ </description>+ <arg name="serial" type="uint"+ summary="the serial of the event that triggered the activation"/>+ <arg name="seat" type="object" interface="wl_seat"+ summary="the wl_seat of the event"/>+ </request>++ <request name="set_app_id">+ <description summary="specifies the application being activated">+ The requesting client can specify an app_id to associate the token+ being created with it.++ Must be sent before commit. This information is optional.+ </description>+ <arg name="app_id" type="string"+ summary="the application id of the client being activated."/>+ </request>++ <request name="set_surface">+ <description summary="specifies the surface requesting activation">+ This request sets the surface requesting the activation. Note, this is+ different from the surface that will be activated.++ Some compositors might refuse to activate toplevels when the token+ doesn't have a requesting surface.++ Must be sent before commit. This information is optional.+ </description>+ <arg name="surface" type="object" interface="wl_surface"+ summary="the requesting surface"/>+ </request>++ <request name="commit">+ <description summary="issues the token request">+ Requests an activation token based on the different parameters that+ have been offered through set_serial, set_surface and set_app_id.+ </description>+ </request>++ <event name="done">+ <description summary="the exported activation token">+ The 'done' event contains the unique token of this activation request+ and notifies that the provider is done.+ </description>+ <arg name="token" type="string" summary="the exported activation token"/>+ </event>++ <request name="destroy" type="destructor">+ <description summary="destroy the xdg_activation_token_v1 object">+ Notify the compositor that the xdg_activation_token_v1 object will no+ longer be used. The received token stays valid.+ </description>+ </request>+ </interface>+</protocol>