Merge lp:~3v1n0/bamf/snap-exec-matching-fix-x into lp:bamf/xenial

Proposed by Marco Trevisan (Treviño) on 2018-02-07
Status: Merged
Approved by: Andrea Azzarone on 2018-02-07
Approved revision: 641
Merged at revision: 641
Proposed branch: lp:~3v1n0/bamf/snap-exec-matching-fix-x
Merge into: lp:bamf/xenial
Diff against target: 187 lines (+120/-4)
4 files modified
src/bamf-matcher.c (+2/-2)
src/bamf-matcher.h (+0/-1)
src/bamf-window.c (+116/-1)
src/bamf-window.h (+2/-0)
To merge this branch: bzr merge lp:~3v1n0/bamf/snap-exec-matching-fix-x
Reviewer Review Type Date Requested Status
Andrea Azzarone (community) 2018-02-07 Approve on 2018-02-07
Review via email: mp+337272@code.launchpad.net

Commit message

bamf-window: read snap and flatpak IDs and use it to guess desktop-id

To post a comment you must log in.
Andrea Azzarone (azzar1) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/bamf-matcher.c'
2--- src/bamf-matcher.c 2016-06-28 16:55:06 +0000
3+++ src/bamf-matcher.c 2018-02-07 14:45:17 +0000
4@@ -556,7 +556,7 @@
5 return result;
6 }
7
8-char *
9+static char *
10 get_env_overridden_desktop_file (guint pid)
11 {
12 gchar *environ_file;
13@@ -1734,7 +1734,7 @@
14
15 if (!desktop_files)
16 {
17- app_id = bamf_legacy_window_get_hint (window, _GTK_APPLICATION_ID);
18+ app_id = bamf_window_get_application_id (bamf_window);
19
20 if (app_id)
21 {
22
23=== modified file 'src/bamf-matcher.h'
24--- src/bamf-matcher.h 2016-06-28 16:55:06 +0000
25+++ src/bamf-matcher.h 2018-02-07 14:45:17 +0000
26@@ -46,7 +46,6 @@
27 #define BAMF_MATCHER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BAMF_TYPE_MATCHER, BamfMatcherClass))
28
29 #define _BAMF_DESKTOP_FILE "_BAMF_DESKTOP_FILE"
30-#define _GTK_APPLICATION_ID "_GTK_APPLICATION_ID"
31
32 typedef struct _BamfMatcher BamfMatcher;
33 typedef struct _BamfMatcherClass BamfMatcherClass;
34
35=== modified file 'src/bamf-window.c'
36--- src/bamf-window.c 2016-04-12 11:10:18 +0000
37+++ src/bamf-window.c 2018-02-07 14:45:17 +0000
38@@ -21,13 +21,18 @@
39 #include "bamf-window.h"
40 #include "bamf-legacy-screen.h"
41
42-#ifdef EXPORT_ACTIONS_MENU
43 #include <glib.h>
44+#include <string.h>
45+
46+#ifdef EXPORT_ACTIONS_MENU
47 #include <glib/gi18n.h>
48 #include <libdbusmenu-glib/dbusmenu-glib.h>
49 #include <libdbusmenu-gtk/parser.h>
50 #endif
51
52+#define _GTK_APPLICATION_ID "_GTK_APPLICATION_ID"
53+#define SNAP_SECURITY_LABEL_PREFIX "snap."
54+
55 #define BAMF_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \
56 BAMF_TYPE_WINDOW, BamfWindowPrivate))
57
58@@ -236,6 +241,116 @@
59 return bamf_legacy_window_get_hint (self->priv->legacy_window, prop);
60 }
61
62+static char *
63+get_snap_desktop_id (guint pid)
64+{
65+ char *security_label_filename = NULL;
66+ char *security_label_contents = NULL;
67+ gsize i, security_label_contents_size = 0;
68+ char *contents_start;
69+ char *contents_end;
70+ char *sandboxed_app_id;
71+
72+ g_return_val_if_fail (pid != 0, NULL);
73+
74+ security_label_filename = g_strdup_printf ("/proc/%u/attr/current", pid);
75+
76+ if (!g_file_get_contents (security_label_filename,
77+ &security_label_contents,
78+ &security_label_contents_size,
79+ NULL))
80+ {
81+ g_free (security_label_filename);
82+ return NULL;
83+ }
84+
85+ if (!g_str_has_prefix (security_label_contents, SNAP_SECURITY_LABEL_PREFIX))
86+ {
87+ g_free (security_label_filename);
88+ g_free (security_label_contents);
89+ return NULL;
90+ }
91+
92+ /* We need to translate the security profile into the desktop-id.
93+ * The profile is in the form of 'snap.name-space.binary-name (current)'
94+ * while the desktop id will be name-space_binary-name.
95+ */
96+ security_label_contents_size -= sizeof (SNAP_SECURITY_LABEL_PREFIX) - 1;
97+ contents_start = security_label_contents + sizeof (SNAP_SECURITY_LABEL_PREFIX) - 1;
98+ contents_end = strchr (contents_start, ' ');
99+
100+ if (contents_end)
101+ security_label_contents_size = contents_end - contents_start;
102+
103+ for (i = 0; i < security_label_contents_size; ++i)
104+ {
105+ if (contents_start[i] == '.')
106+ contents_start[i] = '_';
107+ }
108+
109+ sandboxed_app_id = g_malloc0 (security_label_contents_size + 1);
110+ memcpy (sandboxed_app_id, contents_start, security_label_contents_size);
111+
112+ g_free (security_label_filename);
113+ g_free (security_label_contents);
114+
115+ return sandboxed_app_id;
116+}
117+
118+static char *
119+get_flatpak_desktop_id (guint pid)
120+{
121+ GKeyFile *key_file = NULL;
122+ char *info_filename = NULL;
123+ char *app_id = NULL;
124+
125+ g_return_val_if_fail (pid != 0, NULL);
126+
127+ key_file = g_key_file_new ();
128+ info_filename = g_strdup_printf ("/proc/%u/root/.flatpak-info", pid);
129+
130+ if (!g_key_file_load_from_file (key_file, info_filename, G_KEY_FILE_NONE, NULL))
131+ {
132+ g_free (info_filename);
133+ return NULL;
134+ }
135+
136+ app_id = g_key_file_get_string (key_file, "Application", "name", NULL);
137+
138+ g_key_file_free (key_file);
139+ g_free (info_filename);
140+
141+ return app_id;
142+}
143+
144+char *
145+bamf_window_get_application_id (BamfWindow *self)
146+{
147+ guint pid;
148+ char *app_id;
149+
150+ g_return_val_if_fail (BAMF_IS_WINDOW (self), NULL);
151+
152+ pid = bamf_window_get_pid (self);
153+
154+ if (pid > 0)
155+ {
156+ app_id = get_snap_desktop_id (pid);
157+
158+ if (app_id)
159+ return app_id;
160+
161+ app_id = get_flatpak_desktop_id (pid);
162+
163+ if (app_id)
164+ return app_id;
165+ }
166+
167+ app_id = bamf_window_get_string_hint (self, _GTK_APPLICATION_ID);
168+
169+ return app_id;
170+}
171+
172 BamfWindowMaximizationType
173 bamf_window_maximized (BamfWindow *self)
174 {
175
176=== modified file 'src/bamf-window.h'
177--- src/bamf-window.h 2016-04-09 02:53:03 +0000
178+++ src/bamf-window.h 2018-02-07 14:45:17 +0000
179@@ -73,6 +73,8 @@
180
181 char * bamf_window_get_string_hint (BamfWindow *self, const char* prop);
182
183+char * bamf_window_get_application_id (BamfWindow *self);
184+
185 BamfWindowMaximizationType bamf_window_maximized (BamfWindow *self);
186
187 gint bamf_window_get_monitor (BamfWindow *self);

Subscribers

People subscribed via source and target branches