[tracker/wip/carlosg/portal: 129/141] libtracker-bus: Fallback through portal on bus connections



commit 7d7ec419abef59c7894bc4bdf420214a6fac1338
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu Jan 23 23:33:46 2020 +0100

    libtracker-bus: Fallback through portal on bus connections
    
    If the DBus name is not accessible and we are inside a flatpak
    sandbox, fallback through the portal service so we can get a
    connection filtered by policies.

 src/libtracker-bus/tracker-bus.vala | 41 +++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)
---
diff --git a/src/libtracker-bus/tracker-bus.vala b/src/libtracker-bus/tracker-bus.vala
index 03b3030f1..445b955bf 100644
--- a/src/libtracker-bus/tracker-bus.vala
+++ b/src/libtracker-bus/tracker-bus.vala
@@ -22,16 +22,53 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
        string dbus_name;
        string object_path;
 
+       private const string DBUS_PEER_IFACE = "org.freedesktop.DBus.Peer";
+
+       private const string PORTAL_NAME = "org.freedesktop.portal.Tracker";
+       private const string PORTAL_PATH = "/org/freedesktop/portal/Tracker";
+       private const string PORTAL_IFACE = "org.freedesktop.portal.Tracker";
+
        private const string ENDPOINT_IFACE = "org.freedesktop.Tracker1.Endpoint";
 
+       private const int timeout = 30000;
+
        public Connection (string dbus_name, string object_path, DBusConnection? dbus_connection) throws 
Sparql.Error, IOError, DBusError, GLib.Error {
                Object ();
-               this.dbus_name = dbus_name;
                this.bus = dbus_connection;
-               this.object_path = object_path;
 
                // ensure that error domain is registered with GDBus
                new Sparql.Error.INTERNAL ("");
+
+               var message = new DBusMessage.method_call (dbus_name, object_path, DBUS_PEER_IFACE, "Ping");
+
+               try {
+                       this.bus.send_message_with_reply_sync (message, 0, timeout, null).to_gerror();
+                       this.dbus_name = dbus_name;
+                       this.object_path = object_path;
+               } catch (GLib.Error e) {
+                       if (GLib.FileUtils.test ("/.flatpak-info", GLib.FileTest.EXISTS)) {
+                               /* We are in a flatpak sandbox, check going through the portal */
+
+                               if (object_path == "/org/freedesktop/Tracker1/Endpoint")
+                                       object_path = null;
+
+                               string uri = Tracker.util_build_dbus_uri (GLib.BusType.SESSION, dbus_name, 
object_path);
+                               message = new DBusMessage.method_call (PORTAL_NAME, PORTAL_PATH, 
PORTAL_IFACE, "CreateSession");
+                               message.set_body (new Variant ("(s)", uri));
+
+                               var reply = this.bus.send_message_with_reply_sync (message, 0, timeout, null);
+
+                               reply.to_gerror();
+
+                               var variant = reply.get_body ();
+                               variant.get_child(0, "o", out object_path);
+
+                               this.dbus_name = PORTAL_NAME;
+                               this.object_path = object_path;
+                       } else {
+                               throw e;
+                       }
+               }
        }
 
        void pipe (out UnixInputStream input, out UnixOutputStream output) throws IOError {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]