Merge lp:~3v1n0/bamf/snap-exec-matching-fix-a into lp:bamf/artful

Proposed by Marco Trevisan (Treviño)
Status: Approved
Approved by: Marco Trevisan (Treviño)
Approved revision: 656
Proposed branch: lp:~3v1n0/bamf/snap-exec-matching-fix-a
Merge into: lp:bamf/artful
Diff against target: 202 lines (+127/-4)
5 files modified
debian/changelog (+7/-0)
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-a
Reviewer Review Type Date Requested Status
Unity Team Pending
Review via email: mp+337356@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.

Unmerged revisions

656. By Marco Trevisan (Treviño) on 2018-02-07

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

Preview Diff

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

Subscribers

People subscribed via source and target branches