diff options
author | Alexander Larsson <alexl@redhat.com> | 2015-10-21 10:23:37 +0200 |
---|---|---|
committer | Alexander Larsson <alexl@redhat.com> | 2015-10-21 10:23:37 +0200 |
commit | 64d7c00045b7e0031f4ce4c87475b9ef22622ead (patch) | |
tree | d513d2fdafe53bfc2e678d8b218bfc6705164641 /document-portal | |
parent | 3a47f0b282817fec23a9774a02a20976fcc7cc9a (diff) | |
download | xdg-app-64d7c00045b7e0031f4ce4c87475b9ef22622ead.tar.gz |
Move dbus invocation peer app detection to lib/
Diffstat (limited to 'document-portal')
-rw-r--r-- | document-portal/xdp-main.c | 6 | ||||
-rw-r--r-- | document-portal/xdp-util.c | 198 | ||||
-rw-r--r-- | document-portal/xdp-util.h | 11 |
3 files changed, 3 insertions, 212 deletions
diff --git a/document-portal/xdp-main.c b/document-portal/xdp-main.c index 4168e22..33b8757 100644 --- a/document-portal/xdp-main.c +++ b/document-portal/xdp-main.c @@ -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, diff --git a/document-portal/xdp-util.c b/document-portal/xdp-util.c index 51c4daf..e02cc70 100644 --- a/document-portal/xdp-util.c +++ b/document-portal/xdp-util.c @@ -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); -} diff --git a/document-portal/xdp-util.h b/document-portal/xdp-util.h index b0967ba..bc1bfc6 100644 --- a/document-portal/xdp-util.h +++ b/document-portal/xdp-util.h @@ -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 */ |