[tracker/wip/rishi/tracker-sparql-connection-peer-to-peer: 2/2] libtracker-sparql: Accept NULL D-Bus names for peer-to-peer connections




commit a25b9dedcf9a5222498f48fc911caa5ccc97a374
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Apr 1 00:28:10 2021 +0200

    libtracker-sparql: Accept NULL D-Bus names for peer-to-peer connections
    
    If a GDBusConnection is not a message bus connection, then allow
    specifying a NULL D-Bus name (well-known or unique). This will be
    useful for child worker processes of an application to connect to the
    main process over peer-to-peer D-Bus and talk to a TrackerEndpoint
    exported by it.

 .../bus/tracker-bus-statement.vala                 |  2 +-
 src/libtracker-sparql/bus/tracker-bus.vala         | 10 +++++-----
 src/libtracker-sparql/tracker-backend.vala         | 23 ++++++++++++++++++----
 src/libtracker-sparql/tracker-connection.c         |  6 ++++--
 src/libtracker-sparql/tracker-notifier.c           |  9 +++++++--
 5 files changed, 36 insertions(+), 14 deletions(-)
---
diff --git a/src/libtracker-sparql/bus/tracker-bus-statement.vala 
b/src/libtracker-sparql/bus/tracker-bus-statement.vala
index b14143901..722dd30cb 100644
--- a/src/libtracker-sparql/bus/tracker-bus-statement.vala
+++ b/src/libtracker-sparql/bus/tracker-bus-statement.vala
@@ -26,7 +26,7 @@ public class Tracker.Bus.Statement : Tracker.Sparql.Statement {
        private string object_path;
        private HashTable<string,GLib.Variant> arguments;
 
-       public Statement (DBusConnection bus, string dbus_name, string object_path, string query) {
+       public Statement (DBusConnection bus, string? dbus_name, string object_path, string query) {
                Object ();
                this.bus = bus;
                this.dbus_name = dbus_name;
diff --git a/src/libtracker-sparql/bus/tracker-bus.vala b/src/libtracker-sparql/bus/tracker-bus.vala
index 796a563e4..8c5051776 100644
--- a/src/libtracker-sparql/bus/tracker-bus.vala
+++ b/src/libtracker-sparql/bus/tracker-bus.vala
@@ -41,7 +41,7 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
                get { return object_path; }
        }
 
-       public async Connection (string dbus_name, string object_path, DBusConnection dbus_connection, 
Cancellable? cancellable) throws Sparql.Error, IOError, DBusError, GLib.Error {
+       public async Connection (string? dbus_name, string object_path, DBusConnection dbus_connection, 
Cancellable? cancellable) throws Sparql.Error, IOError, DBusError, GLib.Error {
                Object ();
                this.sandboxed = false;
                this.bus = dbus_connection;
@@ -50,7 +50,7 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
                new Sparql.Error.INTERNAL ("");
 
                // If this environment variable is present, we always go via the portal,
-               if (Environment.get_variable("TRACKER_TEST_PORTAL_FLATPAK_INFO") == null) {
+               if (Environment.get_variable("TRACKER_TEST_PORTAL_FLATPAK_INFO") == null || dbus_name == 
null) {
                        var message = new DBusMessage.method_call (dbus_name, object_path, DBUS_PEER_IFACE, 
"Ping");
 
                        try {
@@ -60,7 +60,7 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
                                this.object_path = object_path;
                                return;
                        } catch (GLib.Error e) {
-                               if (!GLib.FileUtils.test ("/.flatpak-info", GLib.FileTest.EXISTS)) {
+                               if (!GLib.FileUtils.test ("/.flatpak-info", GLib.FileTest.EXISTS) || 
dbus_name == null) {
                                        throw e;
                                }
                        }
@@ -112,7 +112,7 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
                }
        }
 
-       static void send_query (DBusConnection bus, string dbus_name, string object_path, string sparql, 
VariantBuilder? arguments, UnixOutputStream output, Cancellable? cancellable, AsyncReadyCallback? callback) 
throws GLib.IOError, GLib.Error {
+       static void send_query (DBusConnection bus, string? dbus_name, string object_path, string sparql, 
VariantBuilder? arguments, UnixOutputStream output, Cancellable? cancellable, AsyncReadyCallback? callback) 
throws GLib.IOError, GLib.Error {
                var message = new DBusMessage.method_call (dbus_name, object_path, ENDPOINT_IFACE, "Query");
                var fd_list = new UnixFDList ();
                message.set_body (new Variant ("(sha{sv})", sparql, fd_list.append (output.fd), arguments));
@@ -121,7 +121,7 @@ public class Tracker.Bus.Connection : Tracker.Sparql.Connection {
                bus.send_message_with_reply.begin (message, DBusSendMessageFlags.NONE, int.MAX, null, 
cancellable, callback);
        }
 
-       public static async Sparql.Cursor perform_query_call (DBusConnection bus, string dbus_name, string 
object_path, string sparql, VariantBuilder? arguments, Cancellable? cancellable) throws GLib.IOError, 
GLib.Error {
+       public static async Sparql.Cursor perform_query_call (DBusConnection bus, string? dbus_name, string 
object_path, string sparql, VariantBuilder? arguments, Cancellable? cancellable) throws GLib.IOError, 
GLib.Error {
                UnixInputStream input;
                UnixOutputStream output;
                pipe (out input, out output);
diff --git a/src/libtracker-sparql/tracker-backend.vala b/src/libtracker-sparql/tracker-backend.vala
index ae6313118..b53bc13f0 100644
--- a/src/libtracker-sparql/tracker-backend.vala
+++ b/src/libtracker-sparql/tracker-backend.vala
@@ -27,9 +27,16 @@ public static Tracker.Sparql.Connection tracker_sparql_connection_remote_new (st
        return new Tracker.Remote.Connection (url_base);
 }
 
-public static Tracker.Sparql.Connection tracker_sparql_connection_bus_new (string service, string? 
object_path, DBusConnection? conn) throws Tracker.Sparql.Error, IOError, DBusError, GLib.Error {
+public static Tracker.Sparql.Connection tracker_sparql_connection_bus_new (string? service, string? 
object_path, DBusConnection? conn) throws Tracker.Sparql.Error, IOError, DBusError, GLib.Error {
        Tracker.get_debug_flags ();
 
+       return_val_if_fail (service != null || conn is DBusConnection, null);
+
+       if (service == null && conn != null) {
+               var unique_name = conn.get_unique_name();
+               return_val_if_fail (unique_name == null, null);
+       }
+
        var context = new GLib.MainContext ();
        var loop = new GLib.MainLoop(context);
        GLib.Error? error = null;
@@ -55,16 +62,24 @@ public static Tracker.Sparql.Connection tracker_sparql_connection_bus_new (strin
        return sparql_conn;
 }
 
-public static async Tracker.Sparql.Connection tracker_sparql_connection_bus_new_async (string service, 
string? object_path, DBusConnection? conn, Cancellable? cancellable) throws Tracker.Sparql.Error, IOError, 
DBusError, GLib.Error {
+public static async Tracker.Sparql.Connection tracker_sparql_connection_bus_new_async (string? service, 
string? object_path, DBusConnection? conn, Cancellable? cancellable) throws Tracker.Sparql.Error, IOError, 
DBusError, GLib.Error {
        GLib.DBusConnection dbus_conn;
        string path;
 
        Tracker.get_debug_flags ();
 
-       if (conn != null)
+       return_if_fail (service != null || conn is DBusConnection);
+
+       if (conn != null) {
+               if (service == null) {
+                       var unique_name = conn.get_unique_name();
+                       return_if_fail (unique_name == null);
+               }
+
                dbus_conn = conn;
-       else
+       } else {
                dbus_conn = yield GLib.Bus.get (GLib.BusType.SESSION, cancellable);
+       }
 
        if (object_path != null)
                path = object_path;
diff --git a/src/libtracker-sparql/tracker-connection.c b/src/libtracker-sparql/tracker-connection.c
index a1fbc18cf..089f532b1 100644
--- a/src/libtracker-sparql/tracker-connection.c
+++ b/src/libtracker-sparql/tracker-connection.c
@@ -182,7 +182,8 @@ tracker_sparql_connection_lookup_dbus_service (TrackerSparqlConnection  *connect
 
 /**
  * tracker_sparql_connection_bus_new:
- * @service_name: The name of the D-Bus service to connect to.
+ * @service_name: (nullable): The name of the D-Bus service to connect
+ * to, or %NULL if @connection is not a message bus connection.
  * @object_path: (nullable): The path to the object, or %NULL to use the default.
  * @dbus_connection: (nullable): The #GDBusConnection to use, or %NULL to use the session bus
  * @error: pointer to a #GError
@@ -198,7 +199,8 @@ tracker_sparql_connection_lookup_dbus_service (TrackerSparqlConnection  *connect
 
 /**
  * tracker_sparql_connection_bus_new_async:
- * @service_name: The name of the D-Bus service to connect to.
+ * @service_name: (nullable): The name of the D-Bus service to connect
+ * to, or %NULL if @connection is not a message bus connection.
  * @object_path: (nullable): The path to the object, or %NULL to use the default.
  * @dbus_connection: (nullable): The #GDBusConnection to use, or %NULL to use the session bus
  * @cancellable: (nullable): a #GCancellable, or %NULL
diff --git a/src/libtracker-sparql/tracker-notifier.c b/src/libtracker-sparql/tracker-notifier.c
index 9fe293cb9..629d3dbb4 100644
--- a/src/libtracker-sparql/tracker-notifier.c
+++ b/src/libtracker-sparql/tracker-notifier.c
@@ -787,7 +787,8 @@ tracker_notifier_init (TrackerNotifier *notifier)
  * tracker_notifier_signal_subscribe:
  * @notifier: a #TrackerNotifier
  * @connection: a #GDBusConnection
- * @service: DBus service name to subscribe to events for
+ * @service: (nullable): DBus service name to subscribe to events for,
+ * or %NULL if @connection is not a message bus connection.
  * @object_path: (nullable): DBus object path to subscribe to events for, or %NULL
  * @graph: (nullable): graph to listen events for, or %NULL
  *
@@ -812,11 +813,15 @@ tracker_notifier_signal_subscribe (TrackerNotifier *notifier,
 {
        TrackerNotifierSubscription *subscription;
        TrackerNotifierPrivate *priv;
+       const gchar *unique_name;
        gchar *dbus_name = NULL, *dbus_path = NULL, *full_graph = NULL;
 
        g_return_val_if_fail (TRACKER_IS_NOTIFIER (notifier), 0);
        g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
-       g_return_val_if_fail (service != NULL, 0);
+
+       unique_name = g_dbus_connection_get_unique_name (connection);
+       g_return_val_if_fail ((service == NULL && unique_name == NULL) || (service != NULL && unique_name != 
NULL),
+                              0);
 
        priv = tracker_notifier_get_instance_private (notifier);
 


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