From 1ffd23dbabe1f95ceaf3121475eb35aca566ab71 Mon Sep 17 00:00:00 2001 From: Georges Basile Stavracas Neto Date: Wed, 11 Sep 2024 08:56:25 -0300 Subject: [PATCH] a11y: Use session bus to check Flatpak portal The Flatpak portal (and so far all other portals) live in the session bus, not in the a11y bus. Use the session bus to get the Flatpak portal version. Avoid too many synchronous D-Bus calls on startup by using g_once_init_* helpers. --- gtk/a11y/gtkatspiroot.c | 83 ++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/gtk/a11y/gtkatspiroot.c b/gtk/a11y/gtkatspiroot.c index 6ab349b73a..47d6f889ba 100644 --- a/gtk/a11y/gtkatspiroot.c +++ b/gtk/a11y/gtkatspiroot.c @@ -594,42 +594,63 @@ on_event_listener_deregistered (GDBusConnection *connection, } static bool -check_flatpak_portal_version (GDBusConnection *connection, - unsigned int minimum_version) +check_flatpak_portal_version (unsigned int minimum_version) { - GVariant *child; - GError *error = NULL; + static uint32_t flatpak_portal_version = 0; + static size_t initialized = 0; - GVariant *res = - g_dbus_connection_call_sync (connection, - "org.freedesktop.portal.Flatpak", - "/org/freedesktop/portal/Flatpak", - "org.freedesktop.DBus.Properties", - "Get", - g_variant_new ("(ss)", "org.freedesktop.portal.Flatpak", "version"), - G_VARIANT_TYPE ("(v)"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - - if (error != NULL) + if (g_once_init_enter (&initialized)) { - g_warning ("Unable to retrieve the Flatpak portal version: %s", - error->message); - g_clear_error (&error); - return false; + GDBusConnection *session_bus; + GError *error = NULL; + GVariant *child; + GVariant *res; + + session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); + + if (error != NULL) + { + g_warning ("Unable to retrieve the session bus: %s", + error->message); + g_clear_error (&error); + g_once_init_leave (&initialized, 1); + return false; + } + + res = + g_dbus_connection_call_sync (session_bus, + "org.freedesktop.portal.Flatpak", + "/org/freedesktop/portal/Flatpak", + "org.freedesktop.DBus.Properties", + "Get", + g_variant_new ("(ss)", "org.freedesktop.portal.Flatpak", "version"), + G_VARIANT_TYPE ("(v)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + + if (error != NULL) + { + g_warning ("Unable to retrieve the Flatpak portal version: %s", + error->message); + g_clear_error (&error); + g_once_init_leave (&initialized, 1); + return false; + } + + g_variant_get (res, "(v)", &child); + g_variant_unref (res); + + flatpak_portal_version = g_variant_get_uint32 (child); + g_variant_unref (child); + + g_once_init_leave (&initialized, 1); } - g_variant_get (res, "(v)", &child); - g_variant_unref (res); + GTK_DEBUG (A11Y, "Flatpak portal version: %u (required: %u)", flatpak_portal_version, minimum_version); - guint32 version = g_variant_get_uint32 (child); - g_variant_unref (child); - - GTK_DEBUG (A11Y, "Flatpak portal version: %u (required: %u)", version, minimum_version); - - return version >= minimum_version; + return flatpak_portal_version >= minimum_version; } static void @@ -749,7 +770,7 @@ on_registration_reply (GObject *gobject, * check if the version of the Flatpak portal is recent enough. */ if (gdk_should_use_portal () && - !check_flatpak_portal_version (self->connection, 7)) + !check_flatpak_portal_version (7)) { GTK_DEBUG (A11Y, "Sandboxed does not allow event listener registration"); self->can_use_event_listeners = false;