Skip to content

Commit

Permalink
Merge pull request #566 from emersion/activation-token
Browse files Browse the repository at this point in the history
openuri: add an activation_token option
  • Loading branch information
matthiasclasen authored Sep 15, 2021
2 parents 71b9c5e + 39a34ba commit 820343c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
22 changes: 22 additions & 0 deletions data/org.freedesktop.impl.portal.AppChooser.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
This backend can be used by portal implementations that
need to choose an application from a list of applications.
This documentation describes version 2 of this interface.
-->
<interface name="org.freedesktop.impl.portal.AppChooser">
<!--
Expand Down Expand Up @@ -62,6 +64,14 @@
<listitem><para>The filename to choose an application for. Note that this
is just a basename, without a path.</para></listitem>
</varlistentry>
<varlistentry>
<term>activation_token s</term>
<listitem><para>
A token that can be used to activate the application chooser.
</para><para>
The activation_token option was introduced in version 2 of the interface.
</para></listitem>
</varlistentry>
</variablelist>
The following results get returned via the @results vardict:
Expand All @@ -70,6 +80,18 @@
<term>choice s</term>
<listitem><para>The app id that was selected.</para></listitem>
</varlistentry>
<varlistentry>
<term>activation_token s</term>
<listitem><para>
A token that can be used to activate the chosen application. If
the application selection has involved user interaction, a new
token should be generated by the portal implementation.
Otherwise, this token may be the same as the one passed in
@options.
</para><para>
The activation_token option was introduced in version 2 of the interface.
</para></listitem>
</varlistentry>
</variablelist>
-->
<method name="ChooseApplication">
Expand Down
26 changes: 25 additions & 1 deletion data/org.freedesktop.portal.OpenURI.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
URIs (e.g. a http: link to the applications homepage)
under the control of the user.
This documentation describes version 3 of this interface.
This documentation describes version 4 of this interface.
-->
<interface name="org.freedesktop.portal.OpenURI">
<!--
Expand Down Expand Up @@ -71,6 +71,14 @@
The ask option was introduced in version 3 of the interface.
</para></listitem>
</varlistentry>
<varlistentry>
<term>activation_token s</term>
<listitem><para>
A token that can be used to activate the chosen application.
</para><para>
The activation_token option was introduced in version 4 of the interface.
</para></listitem>
</varlistentry>
</variablelist>
-->
Expand Down Expand Up @@ -119,6 +127,14 @@
The ask option was introduced in version 3 of the interface.
</para></listitem>
</varlistentry>
<varlistentry>
<term>activation_token s</term>
<listitem><para>
A token that can be used to activate the chosen application.
</para><para>
The activation_token option was introduced in version 4 of the interface.
</para></listitem>
</varlistentry>
</variablelist>
The OpenFile method was introduced in version 2 of the OpenURI portal API.
Expand Down Expand Up @@ -150,6 +166,14 @@
more information about the @handle.
</para></listitem>
</varlistentry>
<varlistentry>
<term>activation_token s</term>
<listitem><para>
A token that can be used to activate the chosen application.
</para><para>
The activation_token option was introduced in version 4 of the interface.
</para></listitem>
</varlistentry>
</variablelist>
The OpenDirectory method was introduced in version 3 of the OpenURI portal API.
Expand Down
33 changes: 31 additions & 2 deletions src/open-uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ launch_application_with_uri (const char *choice_id,
const char *uri,
const char *parent_window,
gboolean writable,
const char *activation_token,
GError **error)
{
g_autofree char *desktop_id = g_strconcat (choice_id, ".desktop", NULL);
Expand Down Expand Up @@ -271,6 +272,9 @@ launch_application_with_uri (const char *choice_id,

g_app_launch_context_setenv (context, "PARENT_WINDOW_ID", parent_window);

if (activation_token)
g_app_launch_context_setenv (context, "XDG_ACTIVATION_TOKEN", activation_token);

uris.data = (gpointer)ruri;
uris.next = NULL;

Expand Down Expand Up @@ -363,6 +367,7 @@ send_response_in_thread_func (GTask *task,
const char *parent_window;
gboolean writable;
const char *content_type;
const char *activation_token = NULL;

g_debug ("Received choice %s", choice);

Expand All @@ -371,7 +376,9 @@ send_response_in_thread_func (GTask *task,
writable = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (request), "writable"));
content_type = (const char *)g_object_get_data (G_OBJECT (request), "content-type");

if (launch_application_with_uri (choice, uri, parent_window, writable, NULL))
g_variant_lookup (options, "activation_token", "&s", &activation_token);

if (launch_application_with_uri (choice, uri, parent_window, writable, activation_token, NULL))
update_permissions_store (xdp_app_info_get_id (request->app_info), content_type, choice);
}

Expand Down Expand Up @@ -580,6 +587,7 @@ handle_open_in_thread_func (GTask *task,
Request *request = (Request *)task_data;
const char *parent_window;
const char *app_id = xdp_app_info_get_id (request->app_info);
const char *activation_token;
g_autofree char *uri = NULL;
g_autoptr(XdpImplRequest) impl_request = NULL;
g_autofree char *default_app = NULL;
Expand Down Expand Up @@ -607,6 +615,7 @@ handle_open_in_thread_func (GTask *task,
writable = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (request), "writable"));
ask = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (request), "ask"));
open_dir = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (request), "open-dir"));
activation_token = (const char *)g_object_get_data (G_OBJECT (request), "activation-token");

REQUEST_AUTOLOCK (request);

Expand Down Expand Up @@ -809,7 +818,7 @@ handle_open_in_thread_func (GTask *task,

g_debug ("Skipping app chooser");

gboolean result = launch_application_with_uri (app, uri, parent_window, writable, &error);
gboolean result = launch_application_with_uri (app, uri, parent_window, writable, activation_token, &error);
if (request->exported)
{
if (!result)
Expand Down Expand Up @@ -839,6 +848,8 @@ handle_open_in_thread_func (GTask *task,
g_variant_builder_add (&opts_builder, "{sv}", "filename", g_variant_new_string (basename));
if (uri)
g_variant_builder_add (&opts_builder, "{sv}", "uri", g_variant_new_string (uri));
if (activation_token)
g_variant_builder_add (&opts_builder, "{sv}", "activation_token", g_variant_new_string (uri));

impl_request = xdp_impl_request_proxy_new_sync (g_dbus_proxy_get_connection (G_DBUS_PROXY (impl)),
G_DBUS_PROXY_FLAGS_NONE,
Expand Down Expand Up @@ -874,6 +885,7 @@ handle_open_uri (XdpOpenURI *object,
g_autoptr(GTask) task = NULL;
gboolean writable;
gboolean ask;
const char *activation_token = NULL;

if (xdp_impl_lockdown_get_disable_application_handlers (lockdown))
{
Expand All @@ -891,12 +903,17 @@ handle_open_uri (XdpOpenURI *object,
if (!g_variant_lookup (arg_options, "ask", "b", &ask))
ask = FALSE;

g_variant_lookup (arg_options, "activation_token", "&s", &activation_token);

g_object_set_data (G_OBJECT (request), "fd", GINT_TO_POINTER (-1));
g_object_set_data_full (G_OBJECT (request), "uri", g_strdup (arg_uri), g_free);
g_object_set_data_full (G_OBJECT (request), "parent-window", g_strdup (arg_parent_window), g_free);
g_object_set_data (G_OBJECT (request), "writable", GINT_TO_POINTER (writable));
g_object_set_data (G_OBJECT (request), "ask", GINT_TO_POINTER (ask));

if (activation_token)
g_object_set_data_full (G_OBJECT (request), "activation-token", g_strdup (activation_token), g_free);

request_export (request, g_dbus_method_invocation_get_connection (invocation));
xdp_open_uri_complete_open_uri (object, invocation, request->id);

Expand All @@ -920,6 +937,7 @@ handle_open_file (XdpOpenURI *object,
gboolean writable;
gboolean ask;
int fd_id, fd;
const char *activation_token = NULL;
g_autoptr(GError) error = NULL;

if (xdp_impl_lockdown_get_disable_application_handlers (lockdown))
Expand All @@ -946,11 +964,16 @@ handle_open_file (XdpOpenURI *object,
return TRUE;
}

g_variant_lookup (arg_options, "activation_token", "&s", &activation_token);

g_object_set_data (G_OBJECT (request), "fd", GINT_TO_POINTER (fd));
g_object_set_data_full (G_OBJECT (request), "parent-window", g_strdup (arg_parent_window), g_free);
g_object_set_data (G_OBJECT (request), "writable", GINT_TO_POINTER (writable));
g_object_set_data (G_OBJECT (request), "ask", GINT_TO_POINTER (ask));

if (activation_token)
g_object_set_data_full (G_OBJECT (request), "activation-token", g_strdup (activation_token), g_free);

request_export (request, g_dbus_method_invocation_get_connection (invocation));
xdp_open_uri_complete_open_file (object, invocation, NULL, request->id);

Expand All @@ -972,6 +995,7 @@ handle_open_directory (XdpOpenURI *object,
Request *request = request_from_invocation (invocation);
g_autoptr(GTask) task = NULL;
int fd_id, fd;
const char *activation_token = NULL;
g_autoptr(GError) error = NULL;

if (xdp_impl_lockdown_get_disable_application_handlers (lockdown))
Expand All @@ -992,12 +1016,17 @@ handle_open_directory (XdpOpenURI *object,
return TRUE;
}

g_variant_lookup (arg_options, "activation_token", "&s", &activation_token);

g_object_set_data (G_OBJECT (request), "fd", GINT_TO_POINTER (fd));
g_object_set_data_full (G_OBJECT (request), "parent-window", g_strdup (arg_parent_window), g_free);
g_object_set_data (G_OBJECT (request), "writable", GINT_TO_POINTER (0));
g_object_set_data (G_OBJECT (request), "ask", GINT_TO_POINTER (0));
g_object_set_data (G_OBJECT (request), "open-dir", GINT_TO_POINTER (1));

if (activation_token)
g_object_set_data_full (G_OBJECT (request), "activation-token", g_strdup (activation_token), g_free);

request_export (request, g_dbus_method_invocation_get_connection (invocation));
xdp_open_uri_complete_open_file (object, invocation, NULL, request->id);

Expand Down

0 comments on commit 820343c

Please sign in to comment.