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

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Andrea Azzarone
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) Approve
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.
Revision history for this message
Andrea Azzarone (azzar1) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/bamf-matcher.c'
--- src/bamf-matcher.c 2016-06-28 16:55:06 +0000
+++ src/bamf-matcher.c 2018-02-07 14:45:17 +0000
@@ -556,7 +556,7 @@
556 return result;556 return result;
557}557}
558558
559char *559static char *
560get_env_overridden_desktop_file (guint pid)560get_env_overridden_desktop_file (guint pid)
561{561{
562 gchar *environ_file;562 gchar *environ_file;
@@ -1734,7 +1734,7 @@
17341734
1735 if (!desktop_files)1735 if (!desktop_files)
1736 {1736 {
1737 app_id = bamf_legacy_window_get_hint (window, _GTK_APPLICATION_ID);1737 app_id = bamf_window_get_application_id (bamf_window);
17381738
1739 if (app_id)1739 if (app_id)
1740 {1740 {
17411741
=== modified file 'src/bamf-matcher.h'
--- src/bamf-matcher.h 2016-06-28 16:55:06 +0000
+++ src/bamf-matcher.h 2018-02-07 14:45:17 +0000
@@ -46,7 +46,6 @@
46#define BAMF_MATCHER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BAMF_TYPE_MATCHER, BamfMatcherClass))46#define BAMF_MATCHER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), BAMF_TYPE_MATCHER, BamfMatcherClass))
4747
48#define _BAMF_DESKTOP_FILE "_BAMF_DESKTOP_FILE"48#define _BAMF_DESKTOP_FILE "_BAMF_DESKTOP_FILE"
49#define _GTK_APPLICATION_ID "_GTK_APPLICATION_ID"
5049
51typedef struct _BamfMatcher BamfMatcher;50typedef struct _BamfMatcher BamfMatcher;
52typedef struct _BamfMatcherClass BamfMatcherClass;51typedef struct _BamfMatcherClass BamfMatcherClass;
5352
=== modified file 'src/bamf-window.c'
--- src/bamf-window.c 2016-04-12 11:10:18 +0000
+++ src/bamf-window.c 2018-02-07 14:45:17 +0000
@@ -21,13 +21,18 @@
21#include "bamf-window.h"21#include "bamf-window.h"
22#include "bamf-legacy-screen.h"22#include "bamf-legacy-screen.h"
2323
24#ifdef EXPORT_ACTIONS_MENU
25#include <glib.h>24#include <glib.h>
25#include <string.h>
26
27#ifdef EXPORT_ACTIONS_MENU
26#include <glib/gi18n.h>28#include <glib/gi18n.h>
27#include <libdbusmenu-glib/dbusmenu-glib.h>29#include <libdbusmenu-glib/dbusmenu-glib.h>
28#include <libdbusmenu-gtk/parser.h>30#include <libdbusmenu-gtk/parser.h>
29#endif31#endif
3032
33#define _GTK_APPLICATION_ID "_GTK_APPLICATION_ID"
34#define SNAP_SECURITY_LABEL_PREFIX "snap."
35
31#define BAMF_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \36#define BAMF_WINDOW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \
32BAMF_TYPE_WINDOW, BamfWindowPrivate))37BAMF_TYPE_WINDOW, BamfWindowPrivate))
3338
@@ -236,6 +241,116 @@
236 return bamf_legacy_window_get_hint (self->priv->legacy_window, prop);241 return bamf_legacy_window_get_hint (self->priv->legacy_window, prop);
237}242}
238243
244static char *
245get_snap_desktop_id (guint pid)
246{
247 char *security_label_filename = NULL;
248 char *security_label_contents = NULL;
249 gsize i, security_label_contents_size = 0;
250 char *contents_start;
251 char *contents_end;
252 char *sandboxed_app_id;
253
254 g_return_val_if_fail (pid != 0, NULL);
255
256 security_label_filename = g_strdup_printf ("/proc/%u/attr/current", pid);
257
258 if (!g_file_get_contents (security_label_filename,
259 &security_label_contents,
260 &security_label_contents_size,
261 NULL))
262 {
263 g_free (security_label_filename);
264 return NULL;
265 }
266
267 if (!g_str_has_prefix (security_label_contents, SNAP_SECURITY_LABEL_PREFIX))
268 {
269 g_free (security_label_filename);
270 g_free (security_label_contents);
271 return NULL;
272 }
273
274 /* We need to translate the security profile into the desktop-id.
275 * The profile is in the form of 'snap.name-space.binary-name (current)'
276 * while the desktop id will be name-space_binary-name.
277 */
278 security_label_contents_size -= sizeof (SNAP_SECURITY_LABEL_PREFIX) - 1;
279 contents_start = security_label_contents + sizeof (SNAP_SECURITY_LABEL_PREFIX) - 1;
280 contents_end = strchr (contents_start, ' ');
281
282 if (contents_end)
283 security_label_contents_size = contents_end - contents_start;
284
285 for (i = 0; i < security_label_contents_size; ++i)
286 {
287 if (contents_start[i] == '.')
288 contents_start[i] = '_';
289 }
290
291 sandboxed_app_id = g_malloc0 (security_label_contents_size + 1);
292 memcpy (sandboxed_app_id, contents_start, security_label_contents_size);
293
294 g_free (security_label_filename);
295 g_free (security_label_contents);
296
297 return sandboxed_app_id;
298}
299
300static char *
301get_flatpak_desktop_id (guint pid)
302{
303 GKeyFile *key_file = NULL;
304 char *info_filename = NULL;
305 char *app_id = NULL;
306
307 g_return_val_if_fail (pid != 0, NULL);
308
309 key_file = g_key_file_new ();
310 info_filename = g_strdup_printf ("/proc/%u/root/.flatpak-info", pid);
311
312 if (!g_key_file_load_from_file (key_file, info_filename, G_KEY_FILE_NONE, NULL))
313 {
314 g_free (info_filename);
315 return NULL;
316 }
317
318 app_id = g_key_file_get_string (key_file, "Application", "name", NULL);
319
320 g_key_file_free (key_file);
321 g_free (info_filename);
322
323 return app_id;
324}
325
326char *
327bamf_window_get_application_id (BamfWindow *self)
328{
329 guint pid;
330 char *app_id;
331
332 g_return_val_if_fail (BAMF_IS_WINDOW (self), NULL);
333
334 pid = bamf_window_get_pid (self);
335
336 if (pid > 0)
337 {
338 app_id = get_snap_desktop_id (pid);
339
340 if (app_id)
341 return app_id;
342
343 app_id = get_flatpak_desktop_id (pid);
344
345 if (app_id)
346 return app_id;
347 }
348
349 app_id = bamf_window_get_string_hint (self, _GTK_APPLICATION_ID);
350
351 return app_id;
352}
353
239BamfWindowMaximizationType354BamfWindowMaximizationType
240bamf_window_maximized (BamfWindow *self)355bamf_window_maximized (BamfWindow *self)
241{356{
242357
=== modified file 'src/bamf-window.h'
--- src/bamf-window.h 2016-04-09 02:53:03 +0000
+++ src/bamf-window.h 2018-02-07 14:45:17 +0000
@@ -73,6 +73,8 @@
7373
74char * bamf_window_get_string_hint (BamfWindow *self, const char* prop);74char * bamf_window_get_string_hint (BamfWindow *self, const char* prop);
7575
76char * bamf_window_get_application_id (BamfWindow *self);
77
76BamfWindowMaximizationType bamf_window_maximized (BamfWindow *self);78BamfWindowMaximizationType bamf_window_maximized (BamfWindow *self);
7779
78gint bamf_window_get_monitor (BamfWindow *self);80gint bamf_window_get_monitor (BamfWindow *self);

Subscribers

People subscribed via source and target branches