Move dbus invocation peer app detection to lib/

tingping/wmclass
Alexander Larsson 2015-10-21 10:23:37 +02:00
parent 3a47f0b282
commit 64d7c00045
5 changed files with 215 additions and 212 deletions

View File

@ -471,7 +471,7 @@ got_app_id_cb (GObject *source_object,
g_autofree char *app_id = NULL;
PortalMethod portal_method = user_data;
app_id = xdp_invocation_lookup_app_id_finish (invocation, res, &error);
app_id = xdg_app_invocation_lookup_app_id_finish (invocation, res, &error);
if (app_id == NULL)
g_dbus_method_invocation_return_gerror (invocation, error);
@ -483,7 +483,7 @@ static gboolean
handle_method (GCallback method_callback,
GDBusMethodInvocation *invocation)
{
xdp_invocation_lookup_app_id (invocation, NULL, got_app_id_cb, method_callback);
xdg_app_invocation_lookup_app_id (invocation, NULL, got_app_id_cb, method_callback);
return TRUE;
}
@ -511,7 +511,7 @@ on_bus_acquired (GDBusConnection *connection,
g_signal_connect_swapped (helper, "handle-revoke-permissions", G_CALLBACK (handle_method), portal_revoke_permissions);
g_signal_connect_swapped (helper, "handle-delete", G_CALLBACK (handle_method), portal_delete);
xdp_connection_track_name_owners (connection);
xdg_app_connection_track_name_owners (connection);
if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (helper),
connection,

View File

@ -180,201 +180,3 @@ xdp_entry_stat (XdgAppDbEntry *entry,
return 0;
}
static GHashTable *app_ids;
typedef struct {
char *name;
char *app_id;
gboolean exited;
GList *pending;
} AppIdInfo;
static void
app_id_info_free (AppIdInfo *info)
{
g_free (info->name);
g_free (info->app_id);
g_free (info);
}
static void
ensure_app_ids (void)
{
if (app_ids == NULL)
app_ids = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, (GDestroyNotify)app_id_info_free);
}
static void
got_credentials_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
AppIdInfo *info = user_data;
g_autoptr (GDBusMessage) reply = NULL;
g_autoptr (GError) error = NULL;
GList *l;
reply = g_dbus_connection_send_message_with_reply_finish (G_DBUS_CONNECTION (source_object),
res, &error);
if (!info->exited && reply != NULL)
{
GVariant *body = g_dbus_message_get_body (reply);
guint32 pid;
g_autofree char *path = NULL;
g_autofree char *content = NULL;
g_variant_get (body, "(u)", &pid);
path = g_strdup_printf ("/proc/%u/cgroup", pid);
if (g_file_get_contents (path, &content, NULL, NULL))
{
gchar **lines = g_strsplit (content, "\n", -1);
int i;
for (i = 0; lines[i] != NULL; i++)
{
if (g_str_has_prefix (lines[i], "1:name=systemd:"))
{
const char *unit = lines[i] + strlen ("1:name=systemd:");
g_autofree char *scope = g_path_get_basename (unit);
if (g_str_has_prefix (scope, "xdg-app-") &&
g_str_has_suffix (scope, ".scope"))
{
const char *name = scope + strlen("xdg-app-");
char *dash = strchr (name, '-');
if (dash != NULL)
{
*dash = 0;
info->app_id = g_strdup (name);
}
}
else
info->app_id = g_strdup ("");
}
}
g_strfreev (lines);
}
}
for (l = info->pending; l != NULL; l = l->next)
{
GTask *task = l->data;
if (info->app_id == NULL)
g_task_return_new_error (task, XDG_APP_ERROR, XDG_APP_ERROR_FAILED,
"Can't find app id");
else
g_task_return_pointer (task, g_strdup (info->app_id), g_free);
}
g_list_free_full (info->pending, g_object_unref);
info->pending = NULL;
if (info->app_id == NULL)
g_hash_table_remove (app_ids, info->name);
}
void
xdp_invocation_lookup_app_id (GDBusMethodInvocation *invocation,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GDBusConnection *connection = g_dbus_method_invocation_get_connection (invocation);
const gchar *sender = g_dbus_method_invocation_get_sender (invocation);
g_autoptr(GTask) task = NULL;
AppIdInfo *info;
task = g_task_new (invocation, cancellable, callback, user_data);
ensure_app_ids ();
info = g_hash_table_lookup (app_ids, sender);
if (info == NULL)
{
info = g_new0 (AppIdInfo, 1);
info->name = g_strdup (sender);
g_hash_table_insert (app_ids, info->name, info);
}
if (info->app_id)
g_task_return_pointer (task, g_strdup (info->app_id), g_free);
else
{
if (info->pending == NULL)
{
g_autoptr (GDBusMessage) msg = g_dbus_message_new_method_call ("org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"GetConnectionUnixProcessID");
g_dbus_message_set_body (msg, g_variant_new ("(s)", sender));
g_dbus_connection_send_message_with_reply (connection, msg,
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
30000,
NULL,
cancellable,
got_credentials_cb,
info);
}
info->pending = g_list_prepend (info->pending, g_object_ref (task));
}
}
char *
xdp_invocation_lookup_app_id_finish (GDBusMethodInvocation *invocation,
GAsyncResult *result,
GError **error)
{
return g_task_propagate_pointer (G_TASK (result), error);
}
static void
name_owner_changed (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
const char *name, *from, *to;
g_variant_get (parameters, "(sss)", &name, &from, &to);
ensure_app_ids ();
if (name[0] == ':' &&
strcmp (name, from) == 0 &&
strcmp (to, "") == 0)
{
AppIdInfo *info = g_hash_table_lookup (app_ids, name);
if (info != NULL)
{
info->exited = TRUE;
if (info->pending == NULL)
g_hash_table_remove (app_ids, name);
}
}
}
void
xdp_connection_track_name_owners (GDBusConnection *connection)
{
g_dbus_connection_signal_subscribe (connection,
"org.freedesktop.DBus",
"org.freedesktop.DBus",
"NameOwnerChanged",
"/org/freedesktop/DBus",
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
name_owner_changed,
NULL, NULL);
}

View File

@ -33,17 +33,6 @@ guint32 xdp_id_from_name (const char *name);
char * xdp_name_from_id (guint32 doc_id);
void xdp_invocation_lookup_app_id (GDBusMethodInvocation *invocation,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
char *xdp_invocation_lookup_app_id_finish (GDBusMethodInvocation *invocation,
GAsyncResult *result,
GError **error);
void xdp_connection_track_name_owners (GDBusConnection *connection);
G_END_DECLS
#endif /* XDP_UTIL_H */

View File

@ -22,6 +22,7 @@
#include "xdg-app-utils.h"
#include "xdg-app-dir.h"
#include "xdg-app-error.h"
#include <string.h>
#include <stdlib.h>
@ -891,3 +892,202 @@ xdg_app_table_printer_print (XdgAppTablePrinter *printer)
g_print ("\n");
}
}
static GHashTable *app_ids;
typedef struct {
char *name;
char *app_id;
gboolean exited;
GList *pending;
} AppIdInfo;
static void
app_id_info_free (AppIdInfo *info)
{
g_free (info->name);
g_free (info->app_id);
g_free (info);
}
static void
ensure_app_ids (void)
{
if (app_ids == NULL)
app_ids = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, (GDestroyNotify)app_id_info_free);
}
static void
got_credentials_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
AppIdInfo *info = user_data;
g_autoptr (GDBusMessage) reply = NULL;
g_autoptr (GError) error = NULL;
GList *l;
reply = g_dbus_connection_send_message_with_reply_finish (G_DBUS_CONNECTION (source_object),
res, &error);
if (!info->exited && reply != NULL)
{
GVariant *body = g_dbus_message_get_body (reply);
guint32 pid;
g_autofree char *path = NULL;
g_autofree char *content = NULL;
g_variant_get (body, "(u)", &pid);
path = g_strdup_printf ("/proc/%u/cgroup", pid);
if (g_file_get_contents (path, &content, NULL, NULL))
{
gchar **lines = g_strsplit (content, "\n", -1);
int i;
for (i = 0; lines[i] != NULL; i++)
{
if (g_str_has_prefix (lines[i], "1:name=systemd:"))
{
const char *unit = lines[i] + strlen ("1:name=systemd:");
g_autofree char *scope = g_path_get_basename (unit);
if (g_str_has_prefix (scope, "xdg-app-") &&
g_str_has_suffix (scope, ".scope"))
{
const char *name = scope + strlen("xdg-app-");
char *dash = strchr (name, '-');
if (dash != NULL)
{
*dash = 0;
info->app_id = g_strdup (name);
}
}
else
info->app_id = g_strdup ("");
}
}
g_strfreev (lines);
}
}
for (l = info->pending; l != NULL; l = l->next)
{
GTask *task = l->data;
if (info->app_id == NULL)
g_task_return_new_error (task, XDG_APP_ERROR, XDG_APP_ERROR_FAILED,
"Can't find app id");
else
g_task_return_pointer (task, g_strdup (info->app_id), g_free);
}
g_list_free_full (info->pending, g_object_unref);
info->pending = NULL;
if (info->app_id == NULL)
g_hash_table_remove (app_ids, info->name);
}
void
xdg_app_invocation_lookup_app_id (GDBusMethodInvocation *invocation,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GDBusConnection *connection = g_dbus_method_invocation_get_connection (invocation);
const gchar *sender = g_dbus_method_invocation_get_sender (invocation);
g_autoptr(GTask) task = NULL;
AppIdInfo *info;
task = g_task_new (invocation, cancellable, callback, user_data);
ensure_app_ids ();
info = g_hash_table_lookup (app_ids, sender);
if (info == NULL)
{
info = g_new0 (AppIdInfo, 1);
info->name = g_strdup (sender);
g_hash_table_insert (app_ids, info->name, info);
}
if (info->app_id)
g_task_return_pointer (task, g_strdup (info->app_id), g_free);
else
{
if (info->pending == NULL)
{
g_autoptr (GDBusMessage) msg = g_dbus_message_new_method_call ("org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"GetConnectionUnixProcessID");
g_dbus_message_set_body (msg, g_variant_new ("(s)", sender));
g_dbus_connection_send_message_with_reply (connection, msg,
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
30000,
NULL,
cancellable,
got_credentials_cb,
info);
}
info->pending = g_list_prepend (info->pending, g_object_ref (task));
}
}
char *
xdg_app_invocation_lookup_app_id_finish (GDBusMethodInvocation *invocation,
GAsyncResult *result,
GError **error)
{
return g_task_propagate_pointer (G_TASK (result), error);
}
static void
name_owner_changed (GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data)
{
const char *name, *from, *to;
g_variant_get (parameters, "(sss)", &name, &from, &to);
ensure_app_ids ();
if (name[0] == ':' &&
strcmp (name, from) == 0 &&
strcmp (to, "") == 0)
{
AppIdInfo *info = g_hash_table_lookup (app_ids, name);
if (info != NULL)
{
info->exited = TRUE;
if (info->pending == NULL)
g_hash_table_remove (app_ids, name);
}
}
}
void
xdg_app_connection_track_name_owners (GDBusConnection *connection)
{
g_dbus_connection_signal_subscribe (connection,
"org.freedesktop.DBus",
"org.freedesktop.DBus",
"NameOwnerChanged",
"/org/freedesktop/DBus",
NULL,
G_DBUS_SIGNAL_FLAGS_NONE,
name_owner_changed,
NULL, NULL);
}

View File

@ -79,6 +79,18 @@ gboolean ostree_repo_load_summary (const char *repository_url,
GCancellable *cancellable,
GError **error);
void xdg_app_invocation_lookup_app_id (GDBusMethodInvocation *invocation,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
char *xdg_app_invocation_lookup_app_id_finish (GDBusMethodInvocation *invocation,
GAsyncResult *result,
GError **error);
void xdg_app_connection_track_name_owners (GDBusConnection *connection);
#if !GLIB_CHECK_VERSION(2,40,0)
static inline gboolean
g_key_file_save_to_file (GKeyFile *key_file,