Commit 15e361e7 authored by Matthias Clasen's avatar Matthias Clasen

Avoid a race in the file chooser portal

Use the new predictable request object path and connect
to the Response signal before issuing the portal call.
This avoids a race that is pretty unlikely to hit in
the filechooser case.
parent c23205dc
......@@ -180,6 +180,7 @@ open_file_msg_cb (GObject *source_object,
GtkFileChooserNative *self = data->self;
GDBusMessage *reply;
GError *error = NULL;
g_autofree char *handle = NULL;
reply = g_dbus_connection_send_message_with_reply_finish (data->connection, res, &error);
......@@ -197,8 +198,7 @@ open_file_msg_cb (GObject *source_object,
g_variant_get_child (g_dbus_message_get_body (reply), 0, "o",
g_variant_get_child (g_dbus_message_get_body (reply), 0, "o", &handle);
if (data->hidden)
......@@ -207,8 +207,13 @@ open_file_msg_cb (GObject *source_object,
filechooser_portal_data_free (data);
self->mode_data = NULL;
else if (strcmp (handle, data->portal_handle) != 0)
g_free (data->portal_handle);
data->portal_handle = g_steal_pointer (&handle);
g_dbus_connection_signal_unsubscribe (data->connection,
data->portal_response_signal_id =
g_dbus_connection_signal_subscribe (data->connection,
......@@ -289,14 +294,42 @@ show_portal_file_chooser (GtkFileChooserNative *self,
GVariantBuilder opt_builder;
gboolean multiple;
const char *title;
char *token;
char *sender;
int i;
message = g_dbus_message_new_method_call ("org.freedesktop.portal.Desktop",
token = g_strdup_printf ("gtk%d", g_random_int_range (0, G_MAXINT));
sender = g_strdup (g_dbus_connection_get_unique_name (data->connection) + 1);
for (i = 0; sender[i]; i++)
if (sender[i] == '.')
sender[i] = '_';
data->portal_handle = g_strdup_printf ("/org/fredesktop/portal/desktop/request/%s/%s", sender, token);
g_free (sender);
data->portal_response_signal_id =
g_dbus_connection_signal_subscribe (data->connection,
self, NULL);
multiple = gtk_file_chooser_get_select_multiple (GTK_FILE_CHOOSER (self));
g_variant_builder_init (&opt_builder, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add (&opt_builder, "{sv}", "handle_token",
g_variant_new_string (token));
g_free (token);
g_variant_builder_add (&opt_builder, "{sv}", "multiple",
g_variant_new_boolean (multiple));
if (self->accept_label)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment