summaryrefslogtreecommitdiff
diff options
authorMarco Trevisan (TreviƱo) <mail@3v1n0.net>2018-02-07 14:10:19 +0000
committerBileto Bot <ci-train-bot@canonical.com>2018-02-07 14:10:19 +0000
commit5493ffa1bf41d254594ad81a0ac4c7e4bb5d8781 (patch)
tree2c05bcc66f9741cdb0f0c3062dbd6db197b542bd
parent7b8be84a6da0ab724a4d329b60672b6781d1401b (diff)
parent4397307f40cd960be860cc1d46bc883d87f2f6b1 (diff)
bamf-window: read snap and flatpak IDs and use it to guess desktop-id (LP: #1747802)
Approved by: Andrea Azzarone (bzr r655.2.1)
-rw-r--r--src/bamf-matcher.c4
-rw-r--r--src/bamf-matcher.h1
-rw-r--r--src/bamf-window.c117
-rw-r--r--src/bamf-window.h2
4 files changed, 120 insertions, 4 deletions
diff --git a/src/bamf-matcher.c b/src/bamf-matcher.c
index e686ec56..52dbd3a8 100644
--- a/src/bamf-matcher.c
+++ b/src/bamf-matcher.c
@@ -556,7 +556,7 @@ get_exec_overridden_desktop_file (const char *exec_string)
return result;
}
-char *
+static char *
get_env_overridden_desktop_file (guint pid)
{
gchar *environ_file;
@@ -1735,7 +1735,7 @@ bamf_matcher_possible_applications_for_window (BamfMatcher *self,
if (!desktop_files)
{
- app_id = bamf_legacy_window_get_hint (window, _GTK_APPLICATION_ID);
+ app_id = bamf_window_get_application_id (bamf_window);
if (app_id)
{
diff --git a/src/bamf-matcher.h b/src/bamf-matcher.h
index 8c31bcde..e8c1fa2e 100644
--- a/src/bamf-matcher.h
+++ b/src/bamf-matcher.h
@@ -46,7 +46,6 @@
#define BAMF_MATCHER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BAMF_TYPE_MATCHER, BamfMatcherClass))
#define _BAMF_DESKTOP_FILE "_BAMF_DESKTOP_FILE"
-#define _GTK_APPLICATION_ID "_GTK_APPLICATION_ID"
typedef struct _BamfMatcher BamfMatcher;
typedef struct _BamfMatcherClass BamfMatcherClass;
diff --git a/src/bamf-window.c b/src/bamf-window.c
index ca7ea262..982e9a53 100644
--- a/src/bamf-window.c
+++ b/src/bamf-window.c
@@ -21,13 +21,18 @@
#include "bamf-window.h"
#include "bamf-legacy-screen.h"
-#ifdef EXPORT_ACTIONS_MENU
#include <glib.h>
+#include <string.h>
+
+#ifdef EXPORT_ACTIONS_MENU
#include <glib/gi18n.h>
#include <libdbusmenu-glib/dbusmenu-glib.h>
#include <libdbusmenu-gtk/parser.h>
#endif
+#define _GTK_APPLICATION_ID "_GTK_APPLICATION_ID"
+#define SNAP_SECURITY_LABEL_PREFIX "snap."
+
#define BAMF_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \
BAMF_TYPE_WINDOW, BamfWindowPrivate))
@@ -236,6 +241,116 @@ bamf_window_get_string_hint (BamfWindow *self, const char* prop)
return bamf_legacy_window_get_hint (self->priv->legacy_window, prop);
}
+static char *
+get_snap_desktop_id (guint pid)
+{
+ char *security_label_filename = NULL;
+ char *security_label_contents = NULL;
+ gsize i, security_label_contents_size = 0;
+ char *contents_start;
+ char *contents_end;
+ char *sandboxed_app_id;
+
+ g_return_val_if_fail (pid != 0, NULL);
+
+ security_label_filename = g_strdup_printf ("/proc/%u/attr/current", pid);
+
+ if (!g_file_get_contents (security_label_filename,
+ &security_label_contents,
+ &security_label_contents_size,
+ NULL))
+ {
+ g_free (security_label_filename);
+ return NULL;
+ }
+
+ if (!g_str_has_prefix (security_label_contents, SNAP_SECURITY_LABEL_PREFIX))
+ {
+ g_free (security_label_filename);
+ g_free (security_label_contents);
+ return NULL;
+ }
+
+ /* We need to translate the security profile into the desktop-id.
+ * The profile is in the form of 'snap.name-space.binary-name (current)'
+ * while the desktop id will be name-space_binary-name.
+ */
+ security_label_contents_size -= sizeof (SNAP_SECURITY_LABEL_PREFIX) - 1;
+ contents_start = security_label_contents + sizeof (SNAP_SECURITY_LABEL_PREFIX) - 1;
+ contents_end = strchr (contents_start, ' ');
+
+ if (contents_end)
+ security_label_contents_size = contents_end - contents_start;
+
+ for (i = 0; i < security_label_contents_size; ++i)
+ {
+ if (contents_start[i] == '.')
+ contents_start[i] = '_';
+ }
+
+ sandboxed_app_id = g_malloc0 (security_label_contents_size + 1);
+ memcpy (sandboxed_app_id, contents_start, security_label_contents_size);
+
+ g_free (security_label_filename);
+ g_free (security_label_contents);
+
+ return sandboxed_app_id;
+}
+
+static char *
+get_flatpak_desktop_id (guint pid)
+{
+ GKeyFile *key_file = NULL;
+ char *info_filename = NULL;
+ char *app_id = NULL;
+
+ g_return_val_if_fail (pid != 0, NULL);
+
+ key_file = g_key_file_new ();
+ info_filename = g_strdup_printf ("/proc/%u/root/.flatpak-info", pid);
+
+ if (!g_key_file_load_from_file (key_file, info_filename, G_KEY_FILE_NONE, NULL))
+ {
+ g_free (info_filename);
+ return NULL;
+ }
+
+ app_id = g_key_file_get_string (key_file, "Application", "name", NULL);
+
+ g_key_file_free (key_file);
+ g_free (info_filename);
+
+ return app_id;
+}
+
+char *
+bamf_window_get_application_id (BamfWindow *self)
+{
+ guint pid;
+ char *app_id;
+
+ g_return_val_if_fail (BAMF_IS_WINDOW (self), NULL);
+
+ pid = bamf_window_get_pid (self);
+
+ if (pid > 0)
+ {
+ app_id = get_snap_desktop_id (pid);
+
+ if (app_id)
+ return app_id;
+
+ app_id = get_flatpak_desktop_id (pid);
+
+ if (app_id)
+ return app_id;
+ }
+
+ app_id = bamf_window_get_string_hint (self, _GTK_APPLICATION_ID);
+
+ return app_id;
+}
+
BamfWindowMaximizationType
bamf_window_maximized (BamfWindow *self)
{
diff --git a/src/bamf-window.h b/src/bamf-window.h
index c9836356..a6f1afe4 100644
--- a/src/bamf-window.h
+++ b/src/bamf-window.h
@@ -73,6 +73,8 @@ gint bamf_window_get_stack_position (BamfWindow *window);
char * bamf_window_get_string_hint (BamfWindow *self, const char* prop);
+char * bamf_window_get_application_id (BamfWindow *self);
+
BamfWindowMaximizationType bamf_window_maximized (BamfWindow *self);
gint bamf_window_get_monitor (BamfWindow *self);