* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: David Zeuthen <davidz@redhat.com>
*/
* kind of pitfalls it avoids
* - Export objects before claiming names
* - Talk about auto-starting services (cf. GBusNameWatcherFlags)
- *
- * - use abstract sockets in test code
- * - right now it doesn't work, dbus-daemon(1) fails with
- *
- * /gdbus/connection/filter: Failed to start message bus: Failed to bind
- * socket "/tmp/g-dbus-tests-pid-28531": Address already in use
- * ** WARNING **: Error reading address from dbus daemon, 0 bytes read
- *
- * or similar.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
-#include <sys/types.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
#include "gdbusauth.h"
#include "gdbusutils.h"
#include "gasyncinitable.h"
#include "giostream.h"
#include "gasyncresult.h"
-#include "gsimpleasyncresult.h"
+#include "gtask.h"
+#include "gmarshal-internal.h"
#ifdef G_OS_UNIX
#include "gunixconnection.h"
* The #GDBusConnection type is used for D-Bus connections to remote
* peers such as a message buses. It is a low-level API that offers a
* lot of flexibility. For instance, it lets you establish a connection
- * over any transport that can by represented as an #GIOStream.
+ * over any transport that can by represented as a #GIOStream.
*
* This class is rarely used directly in D-Bus clients. If you are writing
- * an D-Bus client, it is often easier to use the g_bus_own_name(),
+ * a D-Bus client, it is often easier to use the g_bus_own_name(),
* g_bus_watch_name() or g_dbus_proxy_new_for_bus() APIs.
*
- * As an exception to the usual GLib rule that a particular object must not be
- * used by two threads at the same time, #GDBusConnection's methods may be
- * called from any thread<footnote>
- * <para>
- * This is so that g_bus_get() and g_bus_get_sync() can safely return the
- * same #GDBusConnection when called from any thread.
- * </para>
- * </footnote>.
+ * As an exception to the usual GLib rule that a particular object must not
+ * be used by two threads at the same time, #GDBusConnection's methods may be
+ * called from any thread. This is so that g_bus_get() and g_bus_get_sync()
+ * can safely return the same #GDBusConnection when called from any thread.
*
* Most of the ways to obtain a #GDBusConnection automatically initialize it
* (i.e. connect to D-Bus): for instance, g_dbus_connection_new() and
* #GError, the only valid thing you can do with that #GDBusConnection is to
* free it with g_object_unref().
*
- * <example id="gdbus-server"><title>D-Bus server example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-server.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
+ * ## An example D-Bus server # {#gdbus-server}
+ *
+ * Here is an example for a D-Bus server:
+ * [gdbus-example-server.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-server.c)
+ *
+ * ## An example for exporting a subtree # {#gdbus-subtree-server}
+ *
+ * Here is an example for exporting a subtree:
+ * [gdbus-example-subtree.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-subtree.c)
+ *
+ * ## An example for file descriptor passing # {#gdbus-unix-fd-client}
*
- * <example id="gdbus-subtree-server"><title>D-Bus subtree example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-subtree.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
+ * Here is an example for passing UNIX file descriptors:
+ * [gdbus-unix-fd-client.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-unix-fd-client.c)
*
- * <example id="gdbus-unix-fd-client"><title>D-Bus UNIX File Descriptor example</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-unix-fd-client.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
+ * ## An example for exporting a GObject # {#gdbus-export}
*
- * <example id="gdbus-export"><title>Exporting a GObject</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-export.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
+ * Here is an example for exporting a #GObject:
+ * [gdbus-example-export.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-export.c)
*/
/* ---------------------------------------------------------------------------------------------------- */
* message, and we're being called from its thread, so no memory barrier is
* required before accessing them.
*/
-#define SEND_MESSAGE_FLAGS_INITIALIZING (1<<31)
+#define SEND_MESSAGE_FLAGS_INITIALIZING (1u << 31)
/* Same as SEND_MESSAGE_FLAGS_INITIALIZING, but in GDBusCallFlags */
-#define CALL_FLAGS_INITIALIZING (1<<31)
+#define CALL_FLAGS_INITIALIZING (1u << 31)
/* ---------------------------------------------------------------------------------------------------- */
/*
* call_destroy_notify: <internal>
- * @context: A #GMainContext or %NULL.
- * @callback: A #GDestroyNotify or %NULL.
+ * @context: (nullable): A #GMainContext or %NULL.
+ * @callback: (nullable): A #GDestroyNotify or %NULL.
* @user_data: Data to pass to @callback.
*
* Schedules @callback to run in @context.
call_destroy_notify_data_in_idle,
data,
(GDestroyNotify) call_destroy_notify_data_free);
+ g_source_set_name (idle_source, "[gio] call_destroy_notify_data_in_idle");
g_source_attach (idle_source, data->context);
g_source_unref (idle_source);
GMutex init_lock;
/* Set (by loading the contents of /var/lib/dbus/machine-id) the first time
- * someone calls org.freedesktop.DBus.GetMachineId(). Protected by @lock.
+ * someone calls org.freedesktop.DBus.Peer.GetMachineId(). Protected by @lock.
*/
gchar *machine_id;
GDBusConnectionFlags flags;
/* Map used for managing method replies, protected by @lock */
- GHashTable *map_method_serial_to_send_message_data; /* guint32 -> SendMessageData* */
+ GHashTable *map_method_serial_to_task; /* guint32 -> GTask* */
/* Maps used for managing signal subscription, protected by @lock */
GHashTable *map_rule_to_signal_data; /* match rule (gchar*) -> SignalData */
GHashTable *map_object_path_to_es; /* gchar* -> ExportedSubtree* */
GHashTable *map_id_to_es; /* guint -> ExportedSubtree* */
+ /* Map used for storing last used serials for each thread, protected by @lock */
+ GHashTable *map_thread_to_last_serial;
+
/* Structure used for message filters, protected by @lock */
GPtrArray *filters;
static void purge_all_signal_subscriptions (GDBusConnection *connection);
static void purge_all_filters (GDBusConnection *connection);
+static void schedule_method_call (GDBusConnection *connection,
+ GDBusMessage *message,
+ guint registration_id,
+ guint subtree_registration_id,
+ const GDBusInterfaceInfo *interface_info,
+ const GDBusMethodInfo *method_info,
+ const GDBusPropertyInfo *property_info,
+ GVariant *parameters,
+ const GDBusInterfaceVTable *vtable,
+ GMainContext *main_context,
+ gpointer user_data);
+
#define _G_ENSURE_LOCK(name) do { \
if (G_UNLIKELY (G_TRYLOCK(name))) \
{ \
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
- "_G_ENSURE_LOCK: Lock `" #name "' is not locked"); \
+ "_G_ENSURE_LOCK: Lock '" #name "' is not locked"); \
} \
} while (FALSE) \
else
{
if (alive_connections != NULL)
- g_warn_if_fail (g_hash_table_lookup (alive_connections, connection) == NULL);
+ g_warn_if_fail (!g_hash_table_contains (alive_connections, connection));
}
CONNECTION_UNLOCK (connection);
G_UNLOCK (message_bus_lock);
if (connection->initialization_error != NULL)
g_error_free (connection->initialization_error);
- g_hash_table_unref (connection->map_method_serial_to_send_message_data);
+ g_hash_table_unref (connection->map_method_serial_to_task);
g_hash_table_unref (connection->map_rule_to_signal_data);
g_hash_table_unref (connection->map_id_to_signal_data);
g_hash_table_unref (connection->map_id_to_es);
g_hash_table_unref (connection->map_object_path_to_es);
+ g_hash_table_unref (connection->map_thread_to_last_serial);
+
g_main_context_unref (connection->main_context_at_construction);
g_free (connection->machine_id);
g_value_set_flags (value, g_dbus_connection_get_capabilities (connection));
break;
+ case PROP_FLAGS:
+ g_value_set_flags (value, g_dbus_connection_get_flags (connection));
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
(flags & FLAG_INITIALIZED) != 0 &&
connection->initialization_error == NULL)
{
- if (error != NULL)
- {
- g_print ("%s: Remote peer vanished with error: %s (%s, %d). Exiting.\n",
- G_STRFUNC,
- error->message,
- g_quark_to_string (error->domain), error->code);
- }
- else
- {
- g_print ("%s: Remote peer vanished. Exiting.\n", G_STRFUNC);
- }
raise (SIGTERM);
}
}
P_("Flags"),
G_TYPE_DBUS_CONNECTION_FLAGS,
G_DBUS_CONNECTION_FLAGS_NONE,
+ G_PARAM_READABLE |
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NAME |
* GDBusConnection:exit-on-close:
*
* A boolean specifying whether the process will be terminated (by
- * calling <literal>raise(SIGTERM)</literal>) if the connection
- * is closed by the remote peer.
+ * calling `raise(SIGTERM)`) if the connection is closed by the
+ * remote peer.
*
- * Note that #GDBusConnection objects returned by g_bus_get_finish() and
- * g_bus_get_sync() will (usually) have this property set to %TRUE.
+ * Note that #GDBusConnection objects returned by g_bus_get_finish()
+ * and g_bus_get_sync() will (usually) have this property set to %TRUE.
*
* Since: 2.26
*/
/**
* GDBusConnection::closed:
- * @connection: The #GDBusConnection emitting the signal.
+ * @connection: the #GDBusConnection emitting the signal
* @remote_peer_vanished: %TRUE if @connection is closed because the
- * remote peer closed its end of the connection.
- * @error: A #GError with more details about the event or %NULL.
+ * remote peer closed its end of the connection
+ * @error: (nullable): a #GError with more details about the event or %NULL
*
* Emitted when the connection is closed.
*
* The cause of this event can be
- * <itemizedlist>
- * <listitem><para>
- * If g_dbus_connection_close() is called. In this case
- * @remote_peer_vanished is set to %FALSE and @error is %NULL.
- * </para></listitem>
- * <listitem><para>
- * If the remote peer closes the connection. In this case
- * @remote_peer_vanished is set to %TRUE and @error is set.
- * </para></listitem>
- * <listitem><para>
- * If the remote peer sends invalid or malformed data. In this
- * case @remote_peer_vanished is set to %FALSE and @error
- * is set.
- * </para></listitem>
- * </itemizedlist>
+ *
+ * - If g_dbus_connection_close() is called. In this case
+ * @remote_peer_vanished is set to %FALSE and @error is %NULL.
+ *
+ * - If the remote peer closes the connection. In this case
+ * @remote_peer_vanished is set to %TRUE and @error is set.
+ *
+ * - If the remote peer sends invalid or malformed data. In this
+ * case @remote_peer_vanished is set to %FALSE and @error is set.
*
* Upon receiving this signal, you should give up your reference to
* @connection. You are guaranteed that this signal is emitted only
*
* Since: 2.26
*/
- signals[CLOSED_SIGNAL] = g_signal_new ("closed",
+ signals[CLOSED_SIGNAL] = g_signal_new (I_("closed"),
G_TYPE_DBUS_CONNECTION,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GDBusConnectionClass, closed),
NULL,
NULL,
- NULL,
+ _g_cclosure_marshal_VOID__BOOLEAN_BOXED,
G_TYPE_NONE,
2,
G_TYPE_BOOLEAN,
G_TYPE_ERROR);
+ g_signal_set_va_marshaller (signals[CLOSED_SIGNAL],
+ G_TYPE_FROM_CLASS (klass),
+ _g_cclosure_marshal_VOID__BOOLEAN_BOXEDv);
}
static void
g_mutex_init (&connection->lock);
g_mutex_init (&connection->init_lock);
- connection->map_method_serial_to_send_message_data = g_hash_table_new (g_direct_hash, g_direct_equal);
+ connection->map_method_serial_to_task = g_hash_table_new (g_direct_hash, g_direct_equal);
connection->map_rule_to_signal_data = g_hash_table_new (g_str_hash,
g_str_equal);
connection->map_id_to_es = g_hash_table_new (g_direct_hash,
g_direct_equal);
+ connection->map_thread_to_last_serial = g_hash_table_new (g_direct_hash,
+ g_direct_equal);
+
connection->main_context_at_construction = g_main_context_ref_thread_default ();
connection->filters = g_ptr_array_new ();
/**
* g_dbus_connection_start_message_processing:
- * @connection: A #GDBusConnection.
+ * @connection: a #GDBusConnection
*
* If @connection was created with
* %G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, this method
/**
* g_dbus_connection_is_closed:
- * @connection: A #GDBusConnection.
+ * @connection: a #GDBusConnection
*
* Gets whether @connection is closed.
*
- * Returns: %TRUE if the connection is closed, %FALSE otherwise.
+ * Returns: %TRUE if the connection is closed, %FALSE otherwise
*
* Since: 2.26
*/
/**
* g_dbus_connection_get_capabilities:
- * @connection: A #GDBusConnection.
+ * @connection: a #GDBusConnection
*
* Gets the capabilities negotiated with the remote peer
*
- * Returns: Zero or more flags from the #GDBusCapabilityFlags enumeration.
+ * Returns: zero or more flags from the #GDBusCapabilityFlags enumeration
*
* Since: 2.26
*/
return connection->capabilities;
}
+/**
+ * g_dbus_connection_get_flags:
+ * @connection: a #GDBusConnection
+ *
+ * Gets the flags used to construct this connection
+ *
+ * Returns: zero or more flags from the #GDBusConnectionFlags enumeration
+ *
+ * Since: 2.60
+ */
+GDBusConnectionFlags
+g_dbus_connection_get_flags (GDBusConnection *connection)
+{
+ g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), G_DBUS_CONNECTION_FLAGS_NONE);
+
+ /* do not use g_return_val_if_fail(), we want the memory barrier */
+ if (!check_initialized (connection))
+ return G_DBUS_CONNECTION_FLAGS_NONE;
+
+ return connection->flags;
+}
+
/* ---------------------------------------------------------------------------------------------------- */
/* Called in a temporary thread without holding locks. */
static void
-flush_in_thread_func (GSimpleAsyncResult *res,
- GObject *object,
- GCancellable *cancellable)
+flush_in_thread_func (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
{
- GError *error;
+ GError *error = NULL;
- error = NULL;
- if (!g_dbus_connection_flush_sync (G_DBUS_CONNECTION (object),
- cancellable,
- &error))
- g_simple_async_result_take_error (res, error);
+ if (g_dbus_connection_flush_sync (source_object,
+ cancellable,
+ &error))
+ g_task_return_boolean (task, TRUE);
+ else
+ g_task_return_error (task, error);
}
/**
* g_dbus_connection_flush:
- * @connection: A #GDBusConnection.
- * @cancellable: A #GCancellable or %NULL.
- * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
- * satisfied or %NULL if you don't care about the result.
- * @user_data: The data to pass to @callback.
+ * @connection: a #GDBusConnection
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @callback: (nullable): a #GAsyncReadyCallback to call when the
+ * request is satisfied or %NULL if you don't care about the result
+ * @user_data: The data to pass to @callback
*
* Asynchronously flushes @connection, that is, writes all queued
* outgoing message to the transport and then flushes the transport
* (using g_output_stream_flush_async()). This is useful in programs
- * that wants to emit a D-Bus signal and then exit
- * immediately. Without flushing the connection, there is no guarantee
- * that the message has been sent to the networking buffers in the OS
- * kernel.
+ * that wants to emit a D-Bus signal and then exit immediately. Without
+ * flushing the connection, there is no guaranteed that the message has
+ * been sent to the networking buffers in the OS kernel.
*
* This is an asynchronous method. When the operation is finished,
- * @callback will be invoked in the <link
- * linkend="g-main-context-push-thread-default">thread-default main
- * loop</link> of the thread you are calling this method from. You can
+ * @callback will be invoked in the
+ * [thread-default main context][g-main-context-push-thread-default]
+ * of the thread you are calling this method from. You can
* then call g_dbus_connection_flush_finish() to get the result of the
- * operation. See g_dbus_connection_flush_sync() for the synchronous
+ * operation. See g_dbus_connection_flush_sync() for the synchronous
* version.
*
* Since: 2.26
GAsyncReadyCallback callback,
gpointer user_data)
{
- GSimpleAsyncResult *simple;
+ GTask *task;
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
- simple = g_simple_async_result_new (G_OBJECT (connection),
- callback,
- user_data,
- g_dbus_connection_flush);
- g_simple_async_result_run_in_thread (simple,
- flush_in_thread_func,
- G_PRIORITY_DEFAULT,
- cancellable);
- g_object_unref (simple);
+ task = g_task_new (connection, cancellable, callback, user_data);
+ g_task_set_source_tag (task, g_dbus_connection_flush);
+ g_task_run_in_thread (task, flush_in_thread_func);
+ g_object_unref (task);
}
/**
* g_dbus_connection_flush_finish:
- * @connection: A #GDBusConnection.
- * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_flush().
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
+ * to g_dbus_connection_flush()
+ * @error: return location for error or %NULL
*
* Finishes an operation started with g_dbus_connection_flush().
*
- * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
+ * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
*
* Since: 2.26
*/
GAsyncResult *res,
GError **error)
{
- GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
- gboolean ret;
-
- ret = FALSE;
-
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
- g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
+ g_return_val_if_fail (g_task_is_valid (res, connection), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
- g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_connection_flush);
-
- if (g_simple_async_result_propagate_error (simple, error))
- goto out;
-
- ret = TRUE;
-
- out:
- return ret;
+ return g_task_propagate_boolean (G_TASK (res), error);
}
/**
* g_dbus_connection_flush_sync:
- * @connection: A #GDBusConnection.
- * @cancellable: A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @error: return location for error or %NULL
*
* Synchronously flushes @connection. The calling thread is blocked
* until this is done. See g_dbus_connection_flush() for the
* asynchronous version of this method and more details about what it
* does.
*
- * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
+ * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
*
* Since: 2.26
*/
emit_closed_in_idle,
data,
(GDestroyNotify) emit_closed_data_free);
+ g_source_set_name (idle_source, "[gio] emit_closed_in_idle");
g_source_attach (idle_source, connection->main_context_at_construction);
g_source_unref (idle_source);
}
/**
* g_dbus_connection_close:
- * @connection: A #GDBusConnection.
- * @cancellable: A #GCancellable or %NULL.
- * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
- * satisfied or %NULL if you don't care about the result.
- * @user_data: The data to pass to @callback.
+ * @connection: a #GDBusConnection
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @callback: (nullable): a #GAsyncReadyCallback to call when the request is
+ * satisfied or %NULL if you don't care about the result
+ * @user_data: The data to pass to @callback
*
* Closes @connection. Note that this never causes the process to
* exit (this might only happen if the other end of a shared message
* %G_IO_ERROR_CLOSED.
*
* When @connection has been closed, the #GDBusConnection::closed
- * signal is emitted in the <link
- * linkend="g-main-context-push-thread-default">thread-default main
- * loop</link> of the thread that @connection was constructed in.
+ * signal is emitted in the
+ * [thread-default main context][g-main-context-push-thread-default]
+ * of the thread that @connection was constructed in.
*
* This is an asynchronous method. When the operation is finished,
- * @callback will be invoked in the <link
- * linkend="g-main-context-push-thread-default">thread-default main
- * loop</link> of the thread you are calling this method from. You can
+ * @callback will be invoked in the
+ * [thread-default main context][g-main-context-push-thread-default]
+ * of the thread you are calling this method from. You can
* then call g_dbus_connection_close_finish() to get the result of the
- * operation. See g_dbus_connection_close_sync() for the synchronous
+ * operation. See g_dbus_connection_close_sync() for the synchronous
* version.
*
* Since: 2.26
GAsyncReadyCallback callback,
gpointer user_data)
{
- GSimpleAsyncResult *simple;
+ GTask *task;
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
g_assert (connection->worker != NULL);
- simple = g_simple_async_result_new (G_OBJECT (connection),
- callback,
- user_data,
- g_dbus_connection_close);
- _g_dbus_worker_close (connection->worker, cancellable, simple);
- g_object_unref (simple);
+ task = g_task_new (connection, cancellable, callback, user_data);
+ g_task_set_source_tag (task, g_dbus_connection_close);
+ _g_dbus_worker_close (connection->worker, task);
+ g_object_unref (task);
}
/**
* g_dbus_connection_close_finish:
- * @connection: A #GDBusConnection.
- * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_close().
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
+ * to g_dbus_connection_close()
+ * @error: return location for error or %NULL
*
* Finishes an operation started with g_dbus_connection_close().
*
- * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
+ * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
*
* Since: 2.26
*/
GAsyncResult *res,
GError **error)
{
- GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
- gboolean ret;
-
- ret = FALSE;
-
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
- g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
+ g_return_val_if_fail (g_task_is_valid (res, connection), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
- g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_connection_close);
-
- if (g_simple_async_result_propagate_error (simple, error))
- goto out;
-
- ret = TRUE;
-
- out:
- return ret;
+ return g_task_propagate_boolean (G_TASK (res), error);
}
typedef struct {
/**
* g_dbus_connection_close_sync:
- * @connection: A #GDBusConnection.
- * @cancellable: A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @error: return location for error or %NULL
*
- * Synchronously closees @connection. The calling thread is blocked
+ * Synchronously closes @connection. The calling thread is blocked
* until this is done. See g_dbus_connection_close() for the
* asynchronous version of this method and more details about what it
* does.
*
- * Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
+ * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
*
* Since: 2.26
*/
gboolean
-g_dbus_connection_close_sync (GDBusConnection *connection,
- GCancellable *cancellable,
- GError **error)
+g_dbus_connection_close_sync (GDBusConnection *connection,
+ GCancellable *cancellable,
+ GError **error)
{
gboolean ret;
/* ---------------------------------------------------------------------------------------------------- */
+/**
+ * g_dbus_connection_get_last_serial:
+ * @connection: a #GDBusConnection
+ *
+ * Retrieves the last serial number assigned to a #GDBusMessage on
+ * the current thread. This includes messages sent via both low-level
+ * API such as g_dbus_connection_send_message() as well as
+ * high-level API such as g_dbus_connection_emit_signal(),
+ * g_dbus_connection_call() or g_dbus_proxy_call().
+ *
+ * Returns: the last used serial or zero when no message has been sent
+ * within the current thread
+ *
+ * Since: 2.34
+ */
+guint32
+g_dbus_connection_get_last_serial (GDBusConnection *connection)
+{
+ guint32 ret;
+
+ g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
+
+ CONNECTION_LOCK (connection);
+ ret = GPOINTER_TO_UINT (g_hash_table_lookup (connection->map_thread_to_last_serial,
+ g_thread_self ()));
+ CONNECTION_UNLOCK (connection);
+
+ return ret;
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
/* Can be called by any thread, with the connection lock held */
static gboolean
g_dbus_connection_send_message_unlocked (GDBusConnection *connection,
if (out_serial != NULL)
*out_serial = serial_to_use;
+ /* store used serial for the current thread */
+ /* TODO: watch the thread disposal and remove associated record
+ * from hashtable
+ * - see https://bugzilla.gnome.org/show_bug.cgi?id=676825#c7
+ */
+ g_hash_table_replace (connection->map_thread_to_last_serial,
+ g_thread_self (),
+ GUINT_TO_POINTER (serial_to_use));
+
if (!(flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL))
g_dbus_message_set_serial (message, serial_to_use);
/**
* g_dbus_connection_send_message:
- * @connection: A #GDBusConnection.
- * @message: A #GDBusMessage
- * @flags: Flags affecting how the message is sent.
- * @out_serial: (out) (allow-none): Return location for serial number assigned
- * to @message when sending it or %NULL.
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @message: a #GDBusMessage
+ * @flags: flags affecting how the message is sent
+ * @out_serial: (out) (optional): return location for serial number assigned
+ * to @message when sending it or %NULL
+ * @error: Return location for error or %NULL
*
* Asynchronously sends @message to the peer represented by @connection.
*
* %G_IO_ERROR_CLOSED. If @message is not well-formed,
* the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
*
- * See <xref linkend="gdbus-server"/> and <xref
- * linkend="gdbus-unix-fd-client"/> for an example of how to use this
- * low-level API to send and receive UNIX file descriptors.
+ * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
+ * for an example of how to use this low-level API to send and receive
+ * UNIX file descriptors.
*
* Note that @message must be unlocked, unless @flags contain the
* %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
*
* Returns: %TRUE if the message was well-formed and queued for
- * transmission, %FALSE if @error is set.
+ * transmission, %FALSE if @error is set
*
* Since: 2.26
*/
gboolean
-g_dbus_connection_send_message (GDBusConnection *connection,
- GDBusMessage *message,
- GDBusSendMessageFlags flags,
- volatile guint32 *out_serial,
- GError **error)
+g_dbus_connection_send_message (GDBusConnection *connection,
+ GDBusMessage *message,
+ GDBusSendMessageFlags flags,
+ volatile guint32 *out_serial,
+ GError **error)
{
gboolean ret;
typedef struct
{
- volatile gint ref_count;
- GDBusConnection *connection;
guint32 serial;
- GSimpleAsyncResult *simple;
-
- GMainContext *main_context;
-
- GCancellable *cancellable;
gulong cancellable_handler_id;
} SendMessageData;
/* Can be called from any thread with or without lock held */
-static SendMessageData *
-send_message_data_ref (SendMessageData *data)
-{
- g_atomic_int_inc (&data->ref_count);
- return data;
-}
-
-/* Can be called from any thread with or without lock held */
static void
-send_message_data_unref (SendMessageData *data)
+send_message_data_free (SendMessageData *data)
{
- if (g_atomic_int_dec_and_test (&data->ref_count))
- {
- g_assert (data->timeout_source == NULL);
- g_assert (data->simple == NULL);
- g_assert (data->cancellable_handler_id == 0);
- g_object_unref (data->connection);
- if (data->cancellable != NULL)
- g_object_unref (data->cancellable);
- g_main_context_unref (data->main_context);
- g_free (data);
- }
+ g_assert (data->timeout_source == NULL);
+ g_assert (data->cancellable_handler_id == 0);
+
+ g_slice_free (SendMessageData, data);
}
/* ---------------------------------------------------------------------------------------------------- */
-/* can be called from any thread with lock held - caller must have prepared GSimpleAsyncResult already */
+/* can be called from any thread with lock held; @task is (transfer full) */
static void
-send_message_with_reply_deliver (SendMessageData *data, gboolean remove)
+send_message_with_reply_cleanup (GTask *task, gboolean remove)
{
- CONNECTION_ENSURE_LOCK (data->connection);
+ GDBusConnection *connection = g_task_get_source_object (task);
+ SendMessageData *data = g_task_get_task_data (task);
+
+ CONNECTION_ENSURE_LOCK (connection);
g_assert (!data->delivered);
data->delivered = TRUE;
- g_simple_async_result_complete_in_idle (data->simple);
- g_object_unref (data->simple);
- data->simple = NULL;
-
if (data->timeout_source != NULL)
{
g_source_destroy (data->timeout_source);
}
if (data->cancellable_handler_id > 0)
{
- g_cancellable_disconnect (data->cancellable, data->cancellable_handler_id);
+ g_cancellable_disconnect (g_task_get_cancellable (task), data->cancellable_handler_id);
data->cancellable_handler_id = 0;
}
if (remove)
{
- g_warn_if_fail (g_hash_table_remove (data->connection->map_method_serial_to_send_message_data,
- GUINT_TO_POINTER (data->serial)));
+ gboolean removed = g_hash_table_remove (connection->map_method_serial_to_task,
+ GUINT_TO_POINTER (data->serial));
+ g_warn_if_fail (removed);
}
- send_message_data_unref (data);
+ g_object_unref (task);
}
/* ---------------------------------------------------------------------------------------------------- */
-/* Can be called from any thread with lock held */
+/* Called from GDBus worker thread with lock held; @task is (transfer full). */
static void
-send_message_data_deliver_reply_unlocked (SendMessageData *data,
+send_message_data_deliver_reply_unlocked (GTask *task,
GDBusMessage *reply)
{
+ SendMessageData *data = g_task_get_task_data (task);
+
if (data->delivered)
goto out;
- g_simple_async_result_set_op_res_gpointer (data->simple,
- g_object_ref (reply),
- g_object_unref);
+ g_task_return_pointer (task, g_object_ref (reply), g_object_unref);
- send_message_with_reply_deliver (data, TRUE);
+ send_message_with_reply_cleanup (task, TRUE);
out:
;
}
-/* ---------------------------------------------------------------------------------------------------- */
-
/* Called from a user thread, lock is not held */
-static gboolean
-send_message_with_reply_cancelled_idle_cb (gpointer user_data)
+static void
+send_message_data_deliver_error (GTask *task,
+ GQuark domain,
+ gint code,
+ const char *message)
{
- SendMessageData *data = user_data;
+ GDBusConnection *connection = g_task_get_source_object (task);
+ SendMessageData *data = g_task_get_task_data (task);
- CONNECTION_LOCK (data->connection);
+ CONNECTION_LOCK (connection);
if (data->delivered)
- goto out;
+ {
+ CONNECTION_UNLOCK (connection);
+ return;
+ }
- g_simple_async_result_set_error (data->simple,
- G_IO_ERROR,
- G_IO_ERROR_CANCELLED,
- _("Operation was cancelled"));
+ g_object_ref (task);
+ send_message_with_reply_cleanup (task, TRUE);
+ CONNECTION_UNLOCK (connection);
- send_message_with_reply_deliver (data, TRUE);
+ g_task_return_new_error (task, domain, code, "%s", message);
+ g_object_unref (task);
+}
- out:
- CONNECTION_UNLOCK (data->connection);
+/* ---------------------------------------------------------------------------------------------------- */
+
+/* Called from a user thread, lock is not held; @task is (transfer full) */
+static gboolean
+send_message_with_reply_cancelled_idle_cb (gpointer user_data)
+{
+ GTask *task = user_data;
+
+ send_message_data_deliver_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED,
+ _("Operation was cancelled"));
return FALSE;
}
send_message_with_reply_cancelled_cb (GCancellable *cancellable,
gpointer user_data)
{
- SendMessageData *data = user_data;
+ GTask *task = user_data;
GSource *idle_source;
/* postpone cancellation to idle handler since we may be called directly
* via g_cancellable_connect() (e.g. holding lock)
*/
idle_source = g_idle_source_new ();
- g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
- g_source_set_callback (idle_source,
- send_message_with_reply_cancelled_idle_cb,
- send_message_data_ref (data),
- (GDestroyNotify) send_message_data_unref);
- g_source_attach (idle_source, data->main_context);
+ g_source_set_name (idle_source, "[gio] send_message_with_reply_cancelled_idle_cb");
+ g_task_attach_source (task, idle_source, send_message_with_reply_cancelled_idle_cb);
g_source_unref (idle_source);
}
/* ---------------------------------------------------------------------------------------------------- */
-/* Called from a user thread, lock is not held */
+/* Called from a user thread, lock is not held; @task is (transfer full) */
static gboolean
send_message_with_reply_timeout_cb (gpointer user_data)
{
- SendMessageData *data = user_data;
-
- CONNECTION_LOCK (data->connection);
- if (data->delivered)
- goto out;
+ GTask *task = user_data;
- g_simple_async_result_set_error (data->simple,
- G_IO_ERROR,
- G_IO_ERROR_TIMED_OUT,
+ send_message_data_deliver_error (task, G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
_("Timeout was reached"));
-
- send_message_with_reply_deliver (data, TRUE);
-
- out:
- CONNECTION_UNLOCK (data->connection);
-
return FALSE;
}
GAsyncReadyCallback callback,
gpointer user_data)
{
- GSimpleAsyncResult *simple;
+ GTask *task;
SendMessageData *data;
- GError *error;
+ GError *error = NULL;
volatile guint32 serial;
- data = NULL;
-
if (out_serial == NULL)
out_serial = &serial;
if (timeout_msec == -1)
timeout_msec = 25 * 1000;
- simple = g_simple_async_result_new (G_OBJECT (connection),
- callback,
- user_data,
- g_dbus_connection_send_message_with_reply);
+ data = g_slice_new0 (SendMessageData);
+ task = g_task_new (connection, cancellable, callback, user_data);
+ g_task_set_source_tag (task,
+ g_dbus_connection_send_message_with_reply_unlocked);
+ g_task_set_task_data (task, data, (GDestroyNotify) send_message_data_free);
- if (g_cancellable_is_cancelled (cancellable))
+ if (g_task_return_error_if_cancelled (task))
{
- g_simple_async_result_set_error (simple,
- G_IO_ERROR,
- G_IO_ERROR_CANCELLED,
- _("Operation was cancelled"));
- g_simple_async_result_complete_in_idle (simple);
- g_object_unref (simple);
- goto out;
+ g_object_unref (task);
+ return;
}
- error = NULL;
if (!g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, &error))
{
- g_simple_async_result_take_error (simple, error);
- g_simple_async_result_complete_in_idle (simple);
- g_object_unref (simple);
- goto out;
+ g_task_return_error (task, error);
+ g_object_unref (task);
+ return;
}
-
- data = g_new0 (SendMessageData, 1);
- data->ref_count = 1;
- data->connection = g_object_ref (connection);
- data->simple = simple;
data->serial = *out_serial;
- data->main_context = g_main_context_ref_thread_default ();
if (cancellable != NULL)
{
- data->cancellable = g_object_ref (cancellable);
data->cancellable_handler_id = g_cancellable_connect (cancellable,
G_CALLBACK (send_message_with_reply_cancelled_cb),
- send_message_data_ref (data),
- (GDestroyNotify) send_message_data_unref);
- g_object_set_data_full (G_OBJECT (simple),
- "cancellable",
- g_object_ref (cancellable),
- (GDestroyNotify) g_object_unref);
+ g_object_ref (task),
+ g_object_unref);
}
if (timeout_msec != G_MAXINT)
{
data->timeout_source = g_timeout_source_new (timeout_msec);
- g_source_set_priority (data->timeout_source, G_PRIORITY_DEFAULT);
- g_source_set_callback (data->timeout_source,
- send_message_with_reply_timeout_cb,
- send_message_data_ref (data),
- (GDestroyNotify) send_message_data_unref);
- g_source_attach (data->timeout_source, data->main_context);
+ g_task_attach_source (task, data->timeout_source,
+ (GSourceFunc) send_message_with_reply_timeout_cb);
g_source_unref (data->timeout_source);
}
- g_hash_table_insert (connection->map_method_serial_to_send_message_data,
+ g_hash_table_insert (connection->map_method_serial_to_task,
GUINT_TO_POINTER (*out_serial),
- data);
-
- out:
- ;
+ g_steal_pointer (&task));
}
/**
* g_dbus_connection_send_message_with_reply:
- * @connection: A #GDBusConnection.
- * @message: A #GDBusMessage.
- * @flags: Flags affecting how the message is sent.
- * @timeout_msec: The timeout in milliseconds, -1 to use the default
- * timeout or %G_MAXINT for no timeout.
- * @out_serial: (out) (allow-none): Return location for serial number assigned
- * to @message when sending it or %NULL.
- * @cancellable: A #GCancellable or %NULL.
- * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
- * satisfied or %NULL if you don't care about the result.
- * @user_data: The data to pass to @callback.
+ * @connection: a #GDBusConnection
+ * @message: a #GDBusMessage
+ * @flags: flags affecting how the message is sent
+ * @timeout_msec: the timeout in milliseconds, -1 to use the default
+ * timeout or %G_MAXINT for no timeout
+ * @out_serial: (out) (optional): return location for serial number assigned
+ * to @message when sending it or %NULL
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @callback: (nullable): a #GAsyncReadyCallback to call when the request
+ * is satisfied or %NULL if you don't care about the result
+ * @user_data: The data to pass to @callback
*
* Asynchronously sends @message to the peer represented by @connection.
*
* fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
* the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
*
- * This is an asynchronous method. When the operation is finished, @callback will be invoked
- * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
+ * This is an asynchronous method. When the operation is finished, @callback
+ * will be invoked in the
+ * [thread-default main context][g-main-context-push-thread-default]
* of the thread you are calling this method from. You can then call
* g_dbus_connection_send_message_with_reply_finish() to get the result of the operation.
* See g_dbus_connection_send_message_with_reply_sync() for the synchronous version.
* Note that @message must be unlocked, unless @flags contain the
* %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
*
- * See <xref linkend="gdbus-server"/> and <xref
- * linkend="gdbus-unix-fd-client"/> for an example of how to use this
- * low-level API to send and receive UNIX file descriptors.
+ * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
+ * for an example of how to use this low-level API to send and receive
+ * UNIX file descriptors.
*
* Since: 2.26
*/
void
-g_dbus_connection_send_message_with_reply (GDBusConnection *connection,
- GDBusMessage *message,
- GDBusSendMessageFlags flags,
- gint timeout_msec,
- volatile guint32 *out_serial,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data)
+g_dbus_connection_send_message_with_reply (GDBusConnection *connection,
+ GDBusMessage *message,
+ GDBusSendMessageFlags flags,
+ gint timeout_msec,
+ volatile guint32 *out_serial,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
g_return_if_fail (G_IS_DBUS_MESSAGE (message));
/**
* g_dbus_connection_send_message_with_reply_finish:
* @connection: a #GDBusConnection
- * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_send_message_with_reply().
- * @error: Return location for error or %NULL.
+ * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to
+ * g_dbus_connection_send_message_with_reply()
+ * @error: teturn location for error or %NULL
*
* Finishes an operation started with g_dbus_connection_send_message_with_reply().
*
* be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
* g_dbus_message_to_gerror() to transcode this to a #GError.
*
- * See <xref linkend="gdbus-server"/> and <xref
- * linkend="gdbus-unix-fd-client"/> for an example of how to use this
- * low-level API to send and receive UNIX file descriptors.
+ * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
+ * for an example of how to use this low-level API to send and receive
+ * UNIX file descriptors.
*
- * Returns: (transfer full): A locked #GDBusMessage or %NULL if @error is set.
+ * Returns: (transfer full): a locked #GDBusMessage or %NULL if @error is set
*
* Since: 2.26
*/
GAsyncResult *res,
GError **error)
{
- GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
- GDBusMessage *reply;
- GCancellable *cancellable;
-
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
+ g_return_val_if_fail (g_task_is_valid (res, connection), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
- reply = NULL;
-
- g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_connection_send_message_with_reply);
-
- if (g_simple_async_result_propagate_error (simple, error))
- goto out;
-
- reply = g_object_ref (g_simple_async_result_get_op_res_gpointer (simple));
- cancellable = g_object_get_data (G_OBJECT (simple), "cancellable");
- if (cancellable != NULL && g_cancellable_is_cancelled (cancellable))
- {
- g_object_unref (reply);
- reply = NULL;
- g_set_error_literal (error,
- G_IO_ERROR,
- G_IO_ERROR_CANCELLED,
- _("Operation was cancelled"));
- }
- out:
- return reply;
+ return g_task_propagate_pointer (G_TASK (res), error);
}
/* ---------------------------------------------------------------------------------------------------- */
/**
* g_dbus_connection_send_message_with_reply_sync:
- * @connection: A #GDBusConnection.
- * @message: A #GDBusMessage.
- * @flags: Flags affecting how the message is sent.
- * @timeout_msec: The timeout in milliseconds, -1 to use the default
- * timeout or %G_MAXINT for no timeout.
- * @out_serial: (out) (allow-none): Return location for serial number assigned
- * to @message when sending it or %NULL.
- * @cancellable: A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @message: a #GDBusMessage
+ * @flags: flags affecting how the message is sent.
+ * @timeout_msec: the timeout in milliseconds, -1 to use the default
+ * timeout or %G_MAXINT for no timeout
+ * @out_serial: (out) (optional): return location for serial number
+ * assigned to @message when sending it or %NULL
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @error: return location for error or %NULL
*
* Synchronously sends @message to the peer represented by @connection
* and blocks the calling thread until a reply is received or the
* be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
* g_dbus_message_to_gerror() to transcode this to a #GError.
*
- * See <xref linkend="gdbus-server"/> and <xref
- * linkend="gdbus-unix-fd-client"/> for an example of how to use this
- * low-level API to send and receive UNIX file descriptors.
+ * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
+ * for an example of how to use this low-level API to send and receive
+ * UNIX file descriptors.
*
* Note that @message must be unlocked, unless @flags contain the
* %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
*
- * Returns: (transfer full): A locked #GDBusMessage that is the reply to @message or %NULL if @error is set.
+ * Returns: (transfer full): a locked #GDBusMessage that is the reply
+ * to @message or %NULL if @error is set
*
* Since: 2.26
*/
GDBusMessage *
-g_dbus_connection_send_message_with_reply_sync (GDBusConnection *connection,
- GDBusMessage *message,
- GDBusSendMessageFlags flags,
- gint timeout_msec,
- volatile guint32 *out_serial,
- GCancellable *cancellable,
- GError **error)
-{
- SendMessageSyncData *data;
+g_dbus_connection_send_message_with_reply_sync (GDBusConnection *connection,
+ GDBusMessage *message,
+ GDBusSendMessageFlags flags,
+ gint timeout_msec,
+ volatile guint32 *out_serial,
+ GCancellable *cancellable,
+ GError **error)
+{
+ SendMessageSyncData data;
GDBusMessage *reply;
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
g_return_val_if_fail (timeout_msec >= 0 || timeout_msec == -1, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
- data = g_new0 (SendMessageSyncData, 1);
- data->context = g_main_context_new ();
- data->loop = g_main_loop_new (data->context, FALSE);
+ data.res = NULL;
+ data.context = g_main_context_new ();
+ data.loop = g_main_loop_new (data.context, FALSE);
- g_main_context_push_thread_default (data->context);
+ g_main_context_push_thread_default (data.context);
g_dbus_connection_send_message_with_reply (connection,
message,
out_serial,
cancellable,
(GAsyncReadyCallback) send_message_with_reply_sync_cb,
- data);
- g_main_loop_run (data->loop);
+ &data);
+ g_main_loop_run (data.loop);
reply = g_dbus_connection_send_message_with_reply_finish (connection,
- data->res,
+ data.res,
error);
- g_main_context_pop_thread_default (data->context);
+ g_main_context_pop_thread_default (data.context);
- g_main_context_unref (data->context);
- g_main_loop_unref (data->loop);
- g_object_unref (data->res);
- g_free (data);
+ g_main_context_unref (data.context);
+ g_main_loop_unref (data.loop);
+ if (data.res)
+ g_object_unref (data.res);
return reply;
}
typedef struct
{
- GDBusMessageFilterFunction func;
- gpointer user_data;
-} FilterCallback;
-
-typedef struct
-{
guint id;
+ guint ref_count;
GDBusMessageFilterFunction filter_function;
gpointer user_data;
GDestroyNotify user_data_free_func;
+ GMainContext *context;
} FilterData;
+/* requires CONNECTION_LOCK */
+static FilterData **
+copy_filter_list (GPtrArray *filters)
+{
+ FilterData **copy;
+ guint n;
+
+ copy = g_new (FilterData *, filters->len + 1);
+ for (n = 0; n < filters->len; n++)
+ {
+ copy[n] = filters->pdata[n];
+ copy[n]->ref_count++;
+ }
+ copy[n] = NULL;
+
+ return copy;
+}
+
+/* requires CONNECTION_LOCK */
+static void
+free_filter_list (FilterData **filters)
+{
+ guint n;
+
+ for (n = 0; filters[n]; n++)
+ {
+ filters[n]->ref_count--;
+ if (filters[n]->ref_count == 0)
+ {
+ call_destroy_notify (filters[n]->context,
+ filters[n]->user_data_free_func,
+ filters[n]->user_data);
+ g_main_context_unref (filters[n]->context);
+ g_free (filters[n]);
+ }
+ }
+ g_free (filters);
+}
+
/* Called in GDBusWorker's thread - we must not block - with no lock held */
static void
on_worker_message_received (GDBusWorker *worker,
gpointer user_data)
{
GDBusConnection *connection;
- FilterCallback *filters;
- guint num_filters;
+ FilterData **filters;
guint n;
gboolean alive;
G_LOCK (message_bus_lock);
- alive = (g_hash_table_lookup (alive_connections, user_data) != NULL);
+ alive = g_hash_table_contains (alive_connections, user_data);
if (!alive)
{
G_UNLOCK (message_bus_lock);
/* First collect the set of callback functions */
CONNECTION_LOCK (connection);
- num_filters = connection->filters->len;
- filters = g_new0 (FilterCallback, num_filters);
- for (n = 0; n < num_filters; n++)
- {
- FilterData *data = connection->filters->pdata[n];
- filters[n].func = data->filter_function;
- filters[n].user_data = data->user_data;
- }
+ filters = copy_filter_list (connection->filters);
CONNECTION_UNLOCK (connection);
/* then call the filters in order (without holding the lock) */
- for (n = 0; n < num_filters; n++)
+ for (n = 0; filters[n]; n++)
{
- message = filters[n].func (connection,
- message,
- TRUE,
- filters[n].user_data);
+ message = filters[n]->filter_function (connection,
+ message,
+ TRUE,
+ filters[n]->user_data);
if (message == NULL)
break;
g_dbus_message_lock (message);
}
+ CONNECTION_LOCK (connection);
+ free_filter_list (filters);
+ CONNECTION_UNLOCK (connection);
+
/* Standard dispatch unless the filter ate the message - no need to
* do anything if the message was altered
*/
if (message_type == G_DBUS_MESSAGE_TYPE_METHOD_RETURN || message_type == G_DBUS_MESSAGE_TYPE_ERROR)
{
guint32 reply_serial;
- SendMessageData *send_message_data;
+ GTask *task;
reply_serial = g_dbus_message_get_reply_serial (message);
CONNECTION_LOCK (connection);
- send_message_data = g_hash_table_lookup (connection->map_method_serial_to_send_message_data,
- GUINT_TO_POINTER (reply_serial));
- if (send_message_data != NULL)
+ task = g_hash_table_lookup (connection->map_method_serial_to_task,
+ GUINT_TO_POINTER (reply_serial));
+ if (task != NULL)
{
+ /* This removes @task from @map_method_serial_to_task. */
//g_debug ("delivering reply/error for serial %d for %p", reply_serial, connection);
- send_message_data_deliver_reply_unlocked (send_message_data, message);
+ send_message_data_deliver_reply_unlocked (task, message);
}
else
{
if (message != NULL)
g_object_unref (message);
g_object_unref (connection);
- g_free (filters);
}
/* Called in GDBusWorker's thread, lock is not held */
gpointer user_data)
{
GDBusConnection *connection;
- FilterCallback *filters;
- guint num_filters;
+ FilterData **filters;
guint n;
gboolean alive;
G_LOCK (message_bus_lock);
- alive = (g_hash_table_lookup (alive_connections, user_data) != NULL);
+ alive = g_hash_table_contains (alive_connections, user_data);
if (!alive)
{
G_UNLOCK (message_bus_lock);
/* First collect the set of callback functions */
CONNECTION_LOCK (connection);
- num_filters = connection->filters->len;
- filters = g_new0 (FilterCallback, num_filters);
- for (n = 0; n < num_filters; n++)
- {
- FilterData *data = connection->filters->pdata[n];
- filters[n].func = data->filter_function;
- filters[n].user_data = data->user_data;
- }
+ filters = copy_filter_list (connection->filters);
CONNECTION_UNLOCK (connection);
/* then call the filters in order (without holding the lock) */
- for (n = 0; n < num_filters; n++)
+ for (n = 0; filters[n]; n++)
{
g_dbus_message_lock (message);
- message = filters[n].func (connection,
- message,
- FALSE,
- filters[n].user_data);
+ message = filters[n]->filter_function (connection,
+ message,
+ FALSE,
+ filters[n]->user_data);
if (message == NULL)
break;
}
+ CONNECTION_LOCK (connection);
+ free_filter_list (filters);
+ CONNECTION_UNLOCK (connection);
+
g_object_unref (connection);
- g_free (filters);
return message;
}
static gboolean
cancel_method_on_close (gpointer key, gpointer value, gpointer user_data)
{
- SendMessageData *data = value;
+ GTask *task = value;
+ SendMessageData *data = g_task_get_task_data (task);
if (data->delivered)
return FALSE;
- g_simple_async_result_set_error (data->simple,
- G_IO_ERROR,
- G_IO_ERROR_CLOSED,
- _("The connection is closed"));
+ g_task_return_new_error (task,
+ G_IO_ERROR,
+ G_IO_ERROR_CLOSED,
+ _("The connection is closed"));
- /* Ask send_message_with_reply_deliver not to remove the element from the
+ /* Ask send_message_with_reply_cleanup not to remove the element from the
* hash table - we're in the middle of a foreach; that would be unsafe.
* Instead, return TRUE from this function so that it gets removed safely.
*/
- send_message_with_reply_deliver (data, FALSE);
+ send_message_with_reply_cleanup (task, FALSE);
return TRUE;
}
guint old_atomic_flags;
G_LOCK (message_bus_lock);
- alive = (g_hash_table_lookup (alive_connections, user_data) != NULL);
+ alive = g_hash_table_contains (alive_connections, user_data);
if (!alive)
{
G_UNLOCK (message_bus_lock);
if (!(old_atomic_flags & FLAG_CLOSED))
{
- g_hash_table_foreach_remove (connection->map_method_serial_to_send_message_data, cancel_method_on_close, NULL);
+ g_hash_table_foreach_remove (connection->map_method_serial_to_task, cancel_method_on_close, NULL);
schedule_closed_unlocked (connection, remote_peer_vanished, error);
}
CONNECTION_UNLOCK (connection);
g_assert (connection->guid == NULL);
connection->auth = _g_dbus_auth_new (connection->stream);
connection->guid = _g_dbus_auth_run_client (connection->auth,
+ connection->authentication_observer,
get_offered_capabilities_max (connection),
&connection->capabilities,
cancellable,
G_LOCK (message_bus_lock);
if (alive_connections == NULL)
alive_connections = g_hash_table_new (g_direct_hash, g_direct_equal);
- g_hash_table_insert (alive_connections, connection, connection);
+ g_hash_table_add (alive_connections, connection);
G_UNLOCK (message_bus_lock);
connection->worker = _g_dbus_worker_new (connection->stream,
g_variant_get (hello_result, "(s)", &connection->bus_unique_name);
g_variant_unref (hello_result);
- //g_debug ("unique name is `%s'", connection->bus_unique_name);
+ //g_debug ("unique name is '%s'", connection->bus_unique_name);
}
ret = TRUE;
/**
* g_dbus_connection_new:
- * @stream: A #GIOStream.
- * @guid: (allow-none): The GUID to use if a authenticating as a server or %NULL.
- * @flags: Flags describing how to make the connection.
- * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
- * @cancellable: A #GCancellable or %NULL.
- * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
- * @user_data: The data to pass to @callback.
+ * @stream: a #GIOStream
+ * @guid: (nullable): the GUID to use if a authenticating as a server or %NULL
+ * @flags: flags describing how to make the connection
+ * @observer: (nullable): a #GDBusAuthObserver or %NULL
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @callback: a #GAsyncReadyCallback to call when the request is satisfied
+ * @user_data: the data to pass to @callback
*
* Asynchronously sets up a D-Bus connection for exchanging D-Bus messages
* with the end represented by @stream.
* then call g_dbus_connection_new_finish() to get the result of the
* operation.
*
- * This is a asynchronous failable constructor. See
+ * This is an asynchronous failable constructor. See
* g_dbus_connection_new_sync() for the synchronous
* version.
*
GAsyncReadyCallback callback,
gpointer user_data)
{
+ _g_dbus_initialize ();
+
g_return_if_fail (G_IS_IO_STREAM (stream));
+
g_async_initable_new_async (G_TYPE_DBUS_CONNECTION,
G_PRIORITY_DEFAULT,
cancellable,
/**
* g_dbus_connection_new_finish:
- * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_new().
- * @error: Return location for error or %NULL.
+ * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback
+ * passed to g_dbus_connection_new().
+ * @error: return location for error or %NULL
*
* Finishes an operation started with g_dbus_connection_new().
*
- * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
+ * Returns: a #GDBusConnection or %NULL if @error is set. Free
+ * with g_object_unref().
*
* Since: 2.26
*/
/**
* g_dbus_connection_new_sync:
- * @stream: A #GIOStream.
- * @guid: (allow-none): The GUID to use if a authenticating as a server or %NULL.
- * @flags: Flags describing how to make the connection.
- * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
- * @cancellable: A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
+ * @stream: a #GIOStream
+ * @guid: (nullable): the GUID to use if a authenticating as a server or %NULL
+ * @flags: flags describing how to make the connection
+ * @observer: (nullable): a #GDBusAuthObserver or %NULL
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @error: return location for error or %NULL
*
* Synchronously sets up a D-Bus connection for exchanging D-Bus messages
* with the end represented by @stream.
* This is a synchronous failable constructor. See
* g_dbus_connection_new() for the asynchronous version.
*
- * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
+ * Returns: a #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
*
* Since: 2.26
*/
GCancellable *cancellable,
GError **error)
{
+ _g_dbus_initialize ();
g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
return g_initable_new (G_TYPE_DBUS_CONNECTION,
/**
* g_dbus_connection_new_for_address:
- * @address: A D-Bus address.
- * @flags: Flags describing how to make the connection.
- * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
- * @cancellable: A #GCancellable or %NULL.
- * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
- * @user_data: The data to pass to @callback.
+ * @address: a D-Bus address
+ * @flags: flags describing how to make the connection
+ * @observer: (nullable): a #GDBusAuthObserver or %NULL
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @callback: a #GAsyncReadyCallback to call when the request is satisfied
+ * @user_data: the data to pass to @callback
*
* Asynchronously connects and sets up a D-Bus client connection for
* exchanging D-Bus messages with an endpoint specified by @address
- * which must be in the D-Bus address format.
+ * which must be in the
+ * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
*
* This constructor can only be used to initiate client-side
* connections - use g_dbus_connection_new() if you need to act as the
* %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
*
* When the operation is finished, @callback will be invoked. You can
- * then call g_dbus_connection_new_finish() to get the result of the
- * operation.
+ * then call g_dbus_connection_new_for_address_finish() to get the result of
+ * the operation.
*
* If @observer is not %NULL it may be used to control the
* authentication process.
*
- * This is a asynchronous failable constructor. See
+ * This is an asynchronous failable constructor. See
* g_dbus_connection_new_for_address_sync() for the synchronous
* version.
*
GAsyncReadyCallback callback,
gpointer user_data)
{
+ _g_dbus_initialize ();
+
g_return_if_fail (address != NULL);
+
g_async_initable_new_async (G_TYPE_DBUS_CONNECTION,
G_PRIORITY_DEFAULT,
cancellable,
/**
* g_dbus_connection_new_for_address_finish:
- * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_new().
- * @error: Return location for error or %NULL.
+ * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
+ * to g_dbus_connection_new()
+ * @error: return location for error or %NULL
*
* Finishes an operation started with g_dbus_connection_new_for_address().
*
- * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
+ * Returns: a #GDBusConnection or %NULL if @error is set. Free with
+ * g_object_unref().
*
* Since: 2.26
*/
/**
* g_dbus_connection_new_for_address_sync:
- * @address: A D-Bus address.
- * @flags: Flags describing how to make the connection.
- * @observer: (allow-none): A #GDBusAuthObserver or %NULL.
- * @cancellable: A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
+ * @address: a D-Bus address
+ * @flags: flags describing how to make the connection
+ * @observer: (nullable): a #GDBusAuthObserver or %NULL
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @error: return location for error or %NULL
*
* Synchronously connects and sets up a D-Bus client connection for
* exchanging D-Bus messages with an endpoint specified by @address
- * which must be in the D-Bus address format.
+ * which must be in the
+ * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
*
* This constructor can only be used to initiate client-side
* connections - use g_dbus_connection_new_sync() if you need to act
* If @observer is not %NULL it may be used to control the
* authentication process.
*
- * Returns: A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
+ * Returns: a #GDBusConnection or %NULL if @error is set. Free with
+ * g_object_unref().
*
* Since: 2.26
*/
GCancellable *cancellable,
GError **error)
{
+ _g_dbus_initialize ();
+
g_return_val_if_fail (address != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
return g_initable_new (G_TYPE_DBUS_CONNECTION,
/**
* g_dbus_connection_set_exit_on_close:
- * @connection: A #GDBusConnection.
- * @exit_on_close: Whether the process should be terminated
- * when @connection is closed by the remote peer.
+ * @connection: a #GDBusConnection
+ * @exit_on_close: whether the process should be terminated
+ * when @connection is closed by the remote peer
*
* Sets whether the process should be terminated when @connection is
* closed by the remote peer. See #GDBusConnection:exit-on-close for
* more details.
*
* Note that this function should be used with care. Most modern UNIX
- * desktops tie the notion of a user session the session bus, and expect
- * all of a users applications to quit when their bus connection goes away.
+ * desktops tie the notion of a user session with the session bus, and expect
+ * all of a user's applications to quit when their bus connection goes away.
* If you are setting @exit_on_close to %FALSE for the shared session
* bus connection, you should make sure that your application exits
* when the user session ends.
/**
* g_dbus_connection_get_exit_on_close:
- * @connection: A #GDBusConnection.
+ * @connection: a #GDBusConnection
*
* Gets whether the process is terminated when @connection is
* closed by the remote peer. See
* #GDBusConnection:exit-on-close for more details.
*
- * Returns: Whether the process is terminated when @connection is
- * closed by the remote peer.
+ * Returns: whether the process is terminated when @connection is
+ * closed by the remote peer
*
* Since: 2.26
*/
/**
* g_dbus_connection_get_guid:
- * @connection: A #GDBusConnection.
+ * @connection: a #GDBusConnection
*
* The GUID of the peer performing the role of server when
* authenticating. See #GDBusConnection:guid for more details.
*
* Returns: The GUID. Do not free this string, it is owned by
- * @connection.
+ * @connection.
*
* Since: 2.26
*/
/**
* g_dbus_connection_get_unique_name:
- * @connection: A #GDBusConnection.
+ * @connection: a #GDBusConnection
*
* Gets the unique name of @connection as assigned by the message
* bus. This can also be used to figure out if @connection is a
* message bus connection.
*
- * Returns: The unique name or %NULL if @connection is not a message
- * bus connection. Do not free this string, it is owned by
- * @connection.
+ * Returns: (nullable): the unique name or %NULL if @connection is not a message
+ * bus connection. Do not free this string, it is owned by
+ * @connection.
*
* Since: 2.26
*/
/**
* g_dbus_connection_get_peer_credentials:
- * @connection: A #GDBusConnection.
+ * @connection: a #GDBusConnection
*
* Gets the credentials of the authenticated peer. This will always
* return %NULL unless @connection acted as a server
* each application is a client. So this method will always return
* %NULL for message bus clients.
*
- * Returns: (transfer none): A #GCredentials or %NULL if not available. Do not free
- * this object, it is owned by @connection.
+ * Returns: (transfer none) (nullable): a #GCredentials or %NULL if not
+ * available. Do not free this object, it is owned by @connection.
*
* Since: 2.26
*/
/* ---------------------------------------------------------------------------------------------------- */
-static guint _global_filter_id = 1;
+static volatile guint _global_filter_id = 1;
/**
* g_dbus_connection_add_filter:
- * @connection: A #GDBusConnection.
- * @filter_function: A filter function.
- * @user_data: User data to pass to @filter_function.
- * @user_data_free_func: Function to free @user_data with when filter
- * is removed or %NULL.
+ * @connection: a #GDBusConnection
+ * @filter_function: a filter function
+ * @user_data: user data to pass to @filter_function
+ * @user_data_free_func: function to free @user_data with when filter
+ * is removed or %NULL
*
* Adds a message filter. Filters are handlers that are run on all
* incoming and outgoing messages, prior to standard dispatch. Filters
* dispatched anywhere else - not even the standard dispatch machinery
* (that API such as g_dbus_connection_signal_subscribe() and
* g_dbus_connection_send_message_with_reply() relies on) will see the
- * message. Similary, if a filter consumes an outgoing message, the
+ * message. Similarly, if a filter consumes an outgoing message, the
* message will not be sent to the other peer.
*
- * Returns: A filter identifier that can be used with
- * g_dbus_connection_remove_filter().
+ * If @user_data_free_func is non-%NULL, it will be called (in the
+ * thread-default main context of the thread you are calling this
+ * method from) at some point after @user_data is no longer
+ * needed. (It is not guaranteed to be called synchronously when the
+ * filter is removed, and may be called after @connection has been
+ * destroyed.)
+ *
+ * Returns: a filter identifier that can be used with
+ * g_dbus_connection_remove_filter()
*
* Since: 2.26
*/
CONNECTION_LOCK (connection);
data = g_new0 (FilterData, 1);
- data->id = _global_filter_id++; /* TODO: overflow etc. */
+ data->id = g_atomic_int_add (&_global_filter_id, 1); /* TODO: overflow etc. */
+ data->ref_count = 1;
data->filter_function = filter_function;
data->user_data = user_data;
data->user_data_free_func = user_data_free_func;
+ data->context = g_main_context_ref_thread_default ();
g_ptr_array_add (connection->filters, data);
CONNECTION_UNLOCK (connection);
for (n = 0; n < connection->filters->len; n++)
{
FilterData *data = connection->filters->pdata[n];
- if (data->user_data_free_func != NULL)
- data->user_data_free_func (data->user_data);
+
+ call_destroy_notify (data->context,
+ data->user_data_free_func,
+ data->user_data);
+ g_main_context_unref (data->context);
g_free (data);
}
}
*
* Removes a filter.
*
+ * Note that since filters run in a different thread, there is a race
+ * condition where it is possible that the filter will be running even
+ * after calling g_dbus_connection_remove_filter(), so you cannot just
+ * free data that the filter might be using. Instead, you should pass
+ * a #GDestroyNotify to g_dbus_connection_add_filter(), which will be
+ * called when it is guaranteed that the data is no longer needed.
+ *
* Since: 2.26
*/
void
guint filter_id)
{
guint n;
+ gboolean found;
FilterData *to_destroy;
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
g_return_if_fail (check_initialized (connection));
CONNECTION_LOCK (connection);
+ found = FALSE;
to_destroy = NULL;
for (n = 0; n < connection->filters->len; n++)
{
FilterData *data = connection->filters->pdata[n];
if (data->id == filter_id)
{
+ found = TRUE;
g_ptr_array_remove_index (connection->filters, n);
- to_destroy = data;
+ data->ref_count--;
+ if (data->ref_count == 0)
+ to_destroy = data;
break;
}
}
{
if (to_destroy->user_data_free_func != NULL)
to_destroy->user_data_free_func (to_destroy->user_data);
+ g_main_context_unref (to_destroy->context);
g_free (to_destroy);
}
- else
+ else if (!found)
{
g_warning ("g_dbus_connection_remove_filter: No filter found for filter_id %d", filter_id);
}
gchar *member;
gchar *object_path;
gchar *arg0;
+ GDBusSignalFlags flags;
GArray *subscribers;
} SignalData;
}
static gchar *
-args_to_rule (const gchar *sender,
- const gchar *interface_name,
- const gchar *member,
- const gchar *object_path,
- const gchar *arg0,
- gboolean negate)
+args_to_rule (const gchar *sender,
+ const gchar *interface_name,
+ const gchar *member,
+ const gchar *object_path,
+ const gchar *arg0,
+ GDBusSignalFlags flags)
{
GString *rule;
rule = g_string_new ("type='signal'");
- if (negate)
+ if (flags & G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE)
g_string_prepend_c (rule, '-');
if (sender != NULL)
g_string_append_printf (rule, ",sender='%s'", sender);
g_string_append_printf (rule, ",member='%s'", member);
if (object_path != NULL)
g_string_append_printf (rule, ",path='%s'", object_path);
+
if (arg0 != NULL)
- g_string_append_printf (rule, ",arg0='%s'", arg0);
+ {
+ if (flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH)
+ g_string_append_printf (rule, ",arg0path='%s'", arg0);
+ else if (flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE)
+ g_string_append_printf (rule, ",arg0namespace='%s'", arg0);
+ else
+ g_string_append_printf (rule, ",arg0='%s'", arg0);
+ }
return g_string_free (rule, FALSE);
}
-static guint _global_subscriber_id = 1;
-static guint _global_registration_id = 1;
-static guint _global_subtree_registration_id = 1;
+static volatile guint _global_subscriber_id = 1;
+static volatile guint _global_registration_id = 1;
+static volatile guint _global_subtree_registration_id = 1;
/* ---------------------------------------------------------------------------------------------------- */
/**
* g_dbus_connection_signal_subscribe:
- * @connection: A #GDBusConnection.
- * @sender: (allow-none): Sender name to match on (unique or well-known name)
- * or %NULL to listen from all senders.
- * @interface_name: (allow-none): D-Bus interface name to match on or %NULL to
- * match on all interfaces.
- * @member: (allow-none): D-Bus signal name to match on or %NULL to match on all signals.
- * @object_path: (allow-none): Object path to match on or %NULL to match on all object paths.
- * @arg0: (allow-none): Contents of first string argument to match on or %NULL
- * to match on all kinds of arguments.
- * @flags: Flags describing how to subscribe to the signal (currently unused).
- * @callback: Callback to invoke when there is a signal matching the requested data.
- * @user_data: User data to pass to @callback.
- * @user_data_free_func: (allow-none): Function to free @user_data with when
- * subscription is removed or %NULL.
- *
- * Subscribes to signals on @connection and invokes @callback with a
- * whenever the signal is received. Note that @callback
- * will be invoked in the <link
- * linkend="g-main-context-push-thread-default">thread-default main
- * loop</link> of the thread you are calling this method from.
+ * @connection: a #GDBusConnection
+ * @sender: (nullable): sender name to match on (unique or well-known name)
+ * or %NULL to listen from all senders
+ * @interface_name: (nullable): D-Bus interface name to match on or %NULL to
+ * match on all interfaces
+ * @member: (nullable): D-Bus signal name to match on or %NULL to match on
+ * all signals
+ * @object_path: (nullable): object path to match on or %NULL to match on
+ * all object paths
+ * @arg0: (nullable): contents of first string argument to match on or %NULL
+ * to match on all kinds of arguments
+ * @flags: #GDBusSignalFlags describing how arg0 is used in subscribing to the
+ * signal
+ * @callback: callback to invoke when there is a signal matching the requested data
+ * @user_data: user data to pass to @callback
+ * @user_data_free_func: (nullable): function to free @user_data with when
+ * subscription is removed or %NULL
+ *
+ * Subscribes to signals on @connection and invokes @callback with a whenever
+ * the signal is received. Note that @callback will be invoked in the
+ * [thread-default main context][g-main-context-push-thread-default]
+ * of the thread you are calling this method from.
*
* If @connection is not a message bus connection, @sender must be
* %NULL.
* tracking the name owner of the well-known name and use that when
* processing the received signal.
*
- * Returns: A subscription identifier that can be used with g_dbus_connection_signal_unsubscribe().
+ * If one of %G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE or
+ * %G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH are given, @arg0 is
+ * interpreted as part of a namespace or path. The first argument
+ * of a signal is matched against that part as specified by D-Bus.
+ *
+ * If @user_data_free_func is non-%NULL, it will be called (in the
+ * thread-default main context of the thread you are calling this
+ * method from) at some point after @user_data is no longer
+ * needed. (It is not guaranteed to be called synchronously when the
+ * signal is unsubscribed from, and may be called after @connection
+ * has been destroyed.)
+ *
+ * The returned subscription identifier is an opaque value which is guaranteed
+ * to never be zero.
+ *
+ * This function can never fail.
+ *
+ * Returns: a subscription identifier that can be used with g_dbus_connection_signal_unsubscribe()
*
* Since: 2.26
*/
g_return_val_if_fail (object_path == NULL || g_variant_is_object_path (object_path), 0);
g_return_val_if_fail (callback != NULL, 0);
g_return_val_if_fail (check_initialized (connection), 0);
+ g_return_val_if_fail (!((flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH) && (flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE)), 0);
+ g_return_val_if_fail (!(arg0 == NULL && (flags & (G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH | G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE))), 0);
CONNECTION_LOCK (connection);
* the usual way, but the '-' prevents the match rule from ever
* actually being send to the bus (either for add or remove).
*/
- rule = args_to_rule (sender, interface_name, member, object_path, arg0,
- (flags & G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE) != 0);
+ rule = args_to_rule (sender, interface_name, member, object_path, arg0, flags);
if (sender != NULL && (g_dbus_is_unique_name (sender) || g_strcmp0 (sender, "org.freedesktop.DBus") == 0))
sender_unique_name = sender;
subscriber.callback = callback;
subscriber.user_data = user_data;
subscriber.user_data_free_func = user_data_free_func;
- subscriber.id = _global_subscriber_id++; /* TODO: overflow etc. */
+ subscriber.id = g_atomic_int_add (&_global_subscriber_id, 1); /* TODO: overflow etc. */
subscriber.context = g_main_context_ref_thread_default ();
/* see if we've already have this rule */
signal_data->member = g_strdup (member);
signal_data->object_path = g_strdup (object_path);
signal_data->arg0 = g_strdup (arg0);
+ signal_data->flags = flags;
signal_data->subscribers = g_array_new (FALSE, FALSE, sizeof (SignalSubscriber));
g_array_append_val (signal_data->subscribers, subscriber);
/**
* g_dbus_connection_signal_unsubscribe:
- * @connection: A #GDBusConnection.
- * @subscription_id: A subscription id obtained from g_dbus_connection_signal_subscribe().
+ * @connection: a #GDBusConnection
+ * @subscription_id: a subscription id obtained from
+ * g_dbus_connection_signal_subscribe()
*
* Unsubscribes from signals.
*
g_free (signal_instance);
}
+static gboolean
+namespace_rule_matches (const gchar *namespace,
+ const gchar *name)
+{
+ gint len_namespace;
+ gint len_name;
+
+ len_namespace = strlen (namespace);
+ len_name = strlen (name);
+
+ if (len_name < len_namespace)
+ return FALSE;
+
+ if (memcmp (namespace, name, len_namespace) != 0)
+ return FALSE;
+
+ return len_namespace == len_name || name[len_namespace] == '.';
+}
+
+static gboolean
+path_rule_matches (const gchar *path_a,
+ const gchar *path_b)
+{
+ gint len_a, len_b;
+
+ len_a = strlen (path_a);
+ len_b = strlen (path_b);
+
+ if (len_a < len_b && (len_a == 0 || path_a[len_a - 1] != '/'))
+ return FALSE;
+
+ if (len_b < len_a && (len_b == 0 || path_b[len_b - 1] != '/'))
+ return FALSE;
+
+ return memcmp (path_a, path_b, MIN (len_a, len_b)) == 0;
+}
+
/* called in GDBusWorker thread WITH lock held */
static void
schedule_callbacks (GDBusConnection *connection,
#if 0
g_print ("In schedule_callbacks:\n"
- " sender = `%s'\n"
- " interface = `%s'\n"
- " member = `%s'\n"
- " path = `%s'\n"
- " arg0 = `%s'\n",
+ " sender = '%s'\n"
+ " interface = '%s'\n"
+ " member = '%s'\n"
+ " path = '%s'\n"
+ " arg0 = '%s'\n",
sender,
interface,
member,
if (signal_data->object_path != NULL && g_strcmp0 (signal_data->object_path, path) != 0)
continue;
- if (signal_data->arg0 != NULL && g_strcmp0 (signal_data->arg0, arg0) != 0)
- continue;
+ if (signal_data->arg0 != NULL)
+ {
+ if (arg0 == NULL)
+ continue;
+
+ if (signal_data->flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE)
+ {
+ if (!namespace_rule_matches (signal_data->arg0, arg0))
+ continue;
+ }
+ else if (signal_data->flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH)
+ {
+ if (!path_rule_matches (signal_data->arg0, arg0))
+ continue;
+ }
+ else if (!g_str_equal (signal_data->arg0, arg0))
+ continue;
+ }
for (m = 0; m < signal_data->subscribers->len; m++)
{
emit_signal_instance_in_idle_cb,
signal_instance,
(GDestroyNotify) signal_instance_free);
+ g_source_set_name (idle_source, "[gio] emit_signal_instance_in_idle_cb");
g_source_attach (idle_source, subscriber->context);
g_source_unref (idle_source);
}
{
reply = g_dbus_message_new_method_error (data->message,
"org.freedesktop.DBus.Error.UnknownMethod",
- _("No such interface `org.freedesktop.DBus.Properties' on object at path %s"),
+ _("No such interface “org.freedesktop.DBus.Properties” on object at path %s"),
g_dbus_message_get_path (data->message));
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_object_unref (reply);
NULL,
&value);
- /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the type
- * of the given value is wrong
- */
- if (g_strcmp0 (g_variant_get_type_string (value), data->property_info->signature) != 0)
- {
- reply = g_dbus_message_new_method_error (data->message,
- "org.freedesktop.DBus.Error.InvalidArgs",
- _("Error setting property `%s': Expected type `%s' but got `%s'"),
- data->property_info->name,
- data->property_info->signature,
- g_variant_get_type_string (value));
- goto out;
- }
-
if (!data->vtable->set_property (data->connection,
g_dbus_message_get_sender (data->message),
g_dbus_message_get_path (data->message),
reply = g_dbus_message_new_method_reply (data->message);
}
- out:
g_assert (reply != NULL);
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_object_unref (reply);
&property_name,
NULL);
-
- if (is_get)
- {
- if (vtable == NULL || vtable->get_property == NULL)
- goto out;
- }
- else
- {
- if (vtable == NULL || vtable->set_property == NULL)
- goto out;
- }
+ if (vtable == NULL)
+ goto out;
/* Check that the property exists - if not fail with org.freedesktop.DBus.Error.InvalidArgs
*/
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
- _("No such property `%s'"),
+ _("No such property “%s”"),
property_name);
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_object_unref (reply);
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
- _("Property `%s' is not readable"),
+ _("Property “%s” is not readable"),
property_name);
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_object_unref (reply);
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
- _("Property `%s' is not writable"),
+ _("Property “%s” is not writable"),
property_name);
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_object_unref (reply);
goto out;
}
+ if (!is_get)
+ {
+ GVariant *value;
+
+ /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the type
+ * of the given value is wrong
+ */
+ g_variant_get_child (g_dbus_message_get_body (message), 2, "v", &value);
+ if (g_strcmp0 (g_variant_get_type_string (value), property_info->signature) != 0)
+ {
+ reply = g_dbus_message_new_method_error (message,
+ "org.freedesktop.DBus.Error.InvalidArgs",
+ _("Error setting property “%s”: Expected type “%s” but got “%s”"),
+ property_name, property_info->signature,
+ g_variant_get_type_string (value));
+ g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+ g_variant_unref (value);
+ g_object_unref (reply);
+ handled = TRUE;
+ goto out;
+ }
+
+ g_variant_unref (value);
+ }
+
+ /* If the vtable pointer for get_property() resp. set_property() is
+ * NULL then dispatch the call via the method_call() handler.
+ */
+ if (is_get)
+ {
+ if (vtable->get_property == NULL)
+ {
+ schedule_method_call (connection, message, registration_id, subtree_registration_id,
+ interface_info, NULL, property_info, g_dbus_message_get_body (message),
+ vtable, main_context, user_data);
+ handled = TRUE;
+ goto out;
+ }
+ }
+ else
+ {
+ if (vtable->set_property == NULL)
+ {
+ schedule_method_call (connection, message, registration_id, subtree_registration_id,
+ interface_info, NULL, property_info, g_dbus_message_get_body (message),
+ vtable, main_context, user_data);
+ handled = TRUE;
+ goto out;
+ }
+ }
+
/* ok, got the property info - call user code in an idle handler */
property_data = g_new0 (PropertyData, 1);
property_data->connection = g_object_ref (connection);
is_get ? invoke_get_property_in_idle_cb : invoke_set_property_in_idle_cb,
property_data,
(GDestroyNotify) property_data_free);
+ if (is_get)
+ g_source_set_name (idle_source, "[gio] invoke_get_property_in_idle_cb");
+ else
+ g_source_set_name (idle_source, "[gio] invoke_set_property_in_idle_cb");
g_source_attach (idle_source, main_context);
g_source_unref (idle_source);
GDBusMessage *reply;
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
- _("No such interface `%s'"),
+ _("No such interface “%s”"),
interface_name);
g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_object_unref (reply);
{
reply = g_dbus_message_new_method_error (data->message,
"org.freedesktop.DBus.Error.UnknownMethod",
- _("No such interface `org.freedesktop.DBus.Properties' on object at path %s"),
+ _("No such interface “org.freedesktop.DBus.Properties” on object at path %s"),
g_dbus_message_get_path (data->message));
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_object_unref (reply);
return FALSE;
}
+static gboolean
+interface_has_readable_properties (GDBusInterfaceInfo *interface_info)
+{
+ gint i;
+
+ if (!interface_info->properties)
+ return FALSE;
+
+ for (i = 0; interface_info->properties[i]; i++)
+ if (interface_info->properties[i]->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
+ return TRUE;
+
+ return FALSE;
+}
+
/* called in any thread with connection's lock held */
static gboolean
validate_and_maybe_schedule_property_get_all (GDBusConnection *connection,
gpointer user_data)
{
gboolean handled;
- const char *interface_name;
GSource *idle_source;
PropertyGetAllData *property_get_all_data;
handled = FALSE;
- g_variant_get (g_dbus_message_get_body (message),
- "(&s)",
- &interface_name);
-
- if (vtable == NULL || vtable->get_property == NULL)
+ if (vtable == NULL)
goto out;
+ /* If the vtable pointer for get_property() is NULL but we have a
+ * non-zero number of readable properties, then dispatch the call via
+ * the method_call() handler.
+ */
+ if (vtable->get_property == NULL && interface_has_readable_properties (interface_info))
+ {
+ schedule_method_call (connection, message, registration_id, subtree_registration_id,
+ interface_info, NULL, NULL, g_dbus_message_get_body (message),
+ vtable, main_context, user_data);
+ handled = TRUE;
+ goto out;
+ }
+
/* ok, got the property info - call user in an idle handler */
property_get_all_data = g_new0 (PropertyGetAllData, 1);
property_get_all_data->connection = g_object_ref (connection);
invoke_get_all_properties_in_idle_cb,
property_get_all_data,
(GDestroyNotify) property_get_all_data_free);
+ g_source_set_name (idle_source, "[gio] invoke_get_all_properties_in_idle_cb");
g_source_attach (idle_source, main_context);
g_source_unref (idle_source);
GDBusMessage *reply;
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
- _("No such interface"),
+ _("No such interface “%s”"),
interface_name);
g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_object_unref (reply);
else
s = g_strdup (begin);
- if (g_hash_table_lookup (set, s) == NULL)
- g_hash_table_insert (set, s, GUINT_TO_POINTER (1));
+ if (!g_hash_table_contains (set, s))
+ g_hash_table_add (set, s);
else
g_free (s);
}
guint registration_id;
guint subtree_registration_id;
- vtable = g_object_get_data (G_OBJECT (invocation), "g-dbus-interface-vtable");
- g_assert (vtable != NULL && vtable->method_call != NULL);
-
registration_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation), "g-dbus-registration-id"));
subtree_registration_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id"));
GDBusMessage *reply;
reply = g_dbus_message_new_method_error (g_dbus_method_invocation_get_message (invocation),
"org.freedesktop.DBus.Error.UnknownMethod",
- _("No such interface `%s' on object at path %s"),
+ _("No such interface “%s” on object at path %s"),
g_dbus_method_invocation_get_interface_name (invocation),
g_dbus_method_invocation_get_object_path (invocation));
g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
goto out;
}
+ vtable = g_object_get_data (G_OBJECT (invocation), "g-dbus-interface-vtable");
+ g_assert (vtable != NULL && vtable->method_call != NULL);
+
vtable->method_call (g_dbus_method_invocation_get_connection (invocation),
g_dbus_method_invocation_get_sender (invocation),
g_dbus_method_invocation_get_object_path (invocation),
}
/* called in GDBusWorker thread with connection's lock held */
+static void
+schedule_method_call (GDBusConnection *connection,
+ GDBusMessage *message,
+ guint registration_id,
+ guint subtree_registration_id,
+ const GDBusInterfaceInfo *interface_info,
+ const GDBusMethodInfo *method_info,
+ const GDBusPropertyInfo *property_info,
+ GVariant *parameters,
+ const GDBusInterfaceVTable *vtable,
+ GMainContext *main_context,
+ gpointer user_data)
+{
+ GDBusMethodInvocation *invocation;
+ GSource *idle_source;
+
+ invocation = _g_dbus_method_invocation_new (g_dbus_message_get_sender (message),
+ g_dbus_message_get_path (message),
+ g_dbus_message_get_interface (message),
+ g_dbus_message_get_member (message),
+ method_info,
+ property_info,
+ connection,
+ message,
+ parameters,
+ user_data);
+
+ /* TODO: would be nicer with a real MethodData like we already
+ * have PropertyData and PropertyGetAllData... */
+ g_object_set_data (G_OBJECT (invocation), "g-dbus-interface-vtable", (gpointer) vtable);
+ g_object_set_data (G_OBJECT (invocation), "g-dbus-registration-id", GUINT_TO_POINTER (registration_id));
+ g_object_set_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id", GUINT_TO_POINTER (subtree_registration_id));
+
+ idle_source = g_idle_source_new ();
+ g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
+ g_source_set_callback (idle_source,
+ call_in_idle_cb,
+ invocation,
+ g_object_unref);
+ g_source_set_name (idle_source, "[gio, " __FILE__ "] call_in_idle_cb");
+ g_source_attach (idle_source, main_context);
+ g_source_unref (idle_source);
+}
+
+/* called in GDBusWorker thread with connection's lock held */
static gboolean
validate_and_maybe_schedule_method_call (GDBusConnection *connection,
GDBusMessage *message,
GMainContext *main_context,
gpointer user_data)
{
- GDBusMethodInvocation *invocation;
- const GDBusMethodInfo *method_info;
+ GDBusMethodInfo *method_info;
GDBusMessage *reply;
GVariant *parameters;
- GSource *idle_source;
gboolean handled;
GVariantType *in_type;
{
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.UnknownMethod",
- _("No such method `%s'"),
+ _("No such method “%s”"),
g_dbus_message_get_member (message));
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_object_unref (reply);
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
- _("Type of message, `%s', does not match expected type `%s'"),
+ _("Type of message, “%s”, does not match expected type “%s”"),
g_variant_get_type_string (parameters),
type_string);
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_variant_type_free (in_type);
/* schedule the call in idle */
- invocation = _g_dbus_method_invocation_new (g_dbus_message_get_sender (message),
- g_dbus_message_get_path (message),
- g_dbus_message_get_interface (message),
- g_dbus_message_get_member (message),
- method_info,
- connection,
- message,
- parameters,
- user_data);
+ schedule_method_call (connection, message, registration_id, subtree_registration_id,
+ interface_info, method_info, NULL, parameters,
+ vtable, main_context, user_data);
g_variant_unref (parameters);
-
- /* TODO: would be nicer with a real MethodData like we already
- * have PropertyData and PropertyGetAllData... */
- g_object_set_data (G_OBJECT (invocation), "g-dbus-interface-vtable", (gpointer) vtable);
- g_object_set_data (G_OBJECT (invocation), "g-dbus-registration-id", GUINT_TO_POINTER (registration_id));
- g_object_set_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id", GUINT_TO_POINTER (subtree_registration_id));
-
- idle_source = g_idle_source_new ();
- g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
- g_source_set_callback (idle_source,
- call_in_idle_cb,
- invocation,
- g_object_unref);
- g_source_attach (idle_source, main_context);
- g_source_unref (idle_source);
-
handled = TRUE;
out:
/**
* g_dbus_connection_register_object:
- * @connection: A #GDBusConnection.
- * @object_path: The object path to register at.
- * @interface_info: Introspection data for the interface.
- * @vtable: (allow-none): A #GDBusInterfaceVTable to call into or %NULL.
- * @user_data: (allow-none): Data to pass to functions in @vtable.
- * @user_data_free_func: Function to call when the object path is unregistered.
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @object_path: the object path to register at
+ * @interface_info: introspection data for the interface
+ * @vtable: (nullable): a #GDBusInterfaceVTable to call into or %NULL
+ * @user_data: (nullable): data to pass to functions in @vtable
+ * @user_data_free_func: function to call when the object path is unregistered
+ * @error: return location for error or %NULL
*
* Registers callbacks for exported objects at @object_path with the
* D-Bus interface that is described in @interface_info.
*
- * Calls to functions in @vtable (and @user_data_free_func) will
- * happen in the <link linkend="g-main-context-push-thread-default">thread-default main
- * loop</link> of the thread you are calling this method from.
+ * Calls to functions in @vtable (and @user_data_free_func) will happen
+ * in the
+ * [thread-default main context][g-main-context-push-thread-default]
+ * of the thread you are calling this method from.
*
* Note that all #GVariant values passed to functions in @vtable will match
* the signature given in @interface_info - if a remote caller passes
- * incorrect values, the <literal>org.freedesktop.DBus.Error.InvalidArgs</literal>
+ * incorrect values, the `org.freedesktop.DBus.Error.InvalidArgs`
* is returned to the remote caller.
*
* Additionally, if the remote caller attempts to invoke methods or
* access properties not mentioned in @interface_info the
- * <literal>org.freedesktop.DBus.Error.UnknownMethod</literal> resp.
- * <literal>org.freedesktop.DBus.Error.InvalidArgs</literal> errors
+ * `org.freedesktop.DBus.Error.UnknownMethod` resp.
+ * `org.freedesktop.DBus.Error.InvalidArgs` errors
* are returned to the caller.
*
* It is considered a programming error if the
*
* GDBus automatically implements the standard D-Bus interfaces
* org.freedesktop.DBus.Properties, org.freedesktop.DBus.Introspectable
- * and org.freedesktop.Peer, so you don't have to implement those for
- * the objects you export. You <emphasis>can</emphasis> implement
- * org.freedesktop.DBus.Properties yourself, e.g. to handle getting
- * and setting of properties asynchronously.
+ * and org.freedesktop.Peer, so you don't have to implement those for the
+ * objects you export. You can implement org.freedesktop.DBus.Properties
+ * yourself, e.g. to handle getting and setting of properties asynchronously.
*
* Note that the reference count on @interface_info will be
* incremented by 1 (unless allocated statically, e.g. if the
* reference count is -1, see g_dbus_interface_info_ref()) for as long
* as the object is exported. Also note that @vtable will be copied.
*
- * See <xref linkend="gdbus-server"/> for an example of how to use this method.
+ * See this [server][gdbus-server] for an example of how to use this method.
*
* Returns: 0 if @error is set, otherwise a registration id (never 0)
- * that can be used with g_dbus_connection_unregister_object() .
+ * that can be used with g_dbus_connection_unregister_object()
*
* Since: 2.26
*/
guint
-g_dbus_connection_register_object (GDBusConnection *connection,
- const gchar *object_path,
- GDBusInterfaceInfo *interface_info,
- const GDBusInterfaceVTable *vtable,
- gpointer user_data,
- GDestroyNotify user_data_free_func,
- GError **error)
+g_dbus_connection_register_object (GDBusConnection *connection,
+ const gchar *object_path,
+ GDBusInterfaceInfo *interface_info,
+ const GDBusInterfaceVTable *vtable,
+ gpointer user_data,
+ GDestroyNotify user_data_free_func,
+ GError **error)
{
ExportedObject *eo;
ExportedInterface *ei;
}
ei = g_new0 (ExportedInterface, 1);
- ei->id = _global_registration_id++; /* TODO: overflow etc. */
+ ei->id = g_atomic_int_add (&_global_registration_id, 1); /* TODO: overflow etc. */
ei->eo = eo;
ei->user_data = user_data;
ei->user_data_free_func = user_data_free_func;
/**
* g_dbus_connection_unregister_object:
- * @connection: A #GDBusConnection.
- * @registration_id: A registration id obtained from g_dbus_connection_register_object().
+ * @connection: a #GDBusConnection
+ * @registration_id: a registration id obtained from
+ * g_dbus_connection_register_object()
*
* Unregisters an object.
*
- * Returns: %TRUE if the object was unregistered, %FALSE otherwise.
+ * Returns: %TRUE if the object was unregistered, %FALSE otherwise
*
* Since: 2.26
*/
return ret;
}
-/* ---------------------------------------------------------------------------------------------------- */
+typedef struct {
+ GClosure *method_call_closure;
+ GClosure *get_property_closure;
+ GClosure *set_property_closure;
+} RegisterObjectData;
+
+static RegisterObjectData *
+register_object_data_new (GClosure *method_call_closure,
+ GClosure *get_property_closure,
+ GClosure *set_property_closure)
+{
+ RegisterObjectData *data;
+
+ data = g_new0 (RegisterObjectData, 1);
+
+ if (method_call_closure != NULL)
+ {
+ data->method_call_closure = g_closure_ref (method_call_closure);
+ g_closure_sink (method_call_closure);
+ if (G_CLOSURE_NEEDS_MARSHAL (method_call_closure))
+ g_closure_set_marshal (method_call_closure, g_cclosure_marshal_generic);
+ }
+
+ if (get_property_closure != NULL)
+ {
+ data->get_property_closure = g_closure_ref (get_property_closure);
+ g_closure_sink (get_property_closure);
+ if (G_CLOSURE_NEEDS_MARSHAL (get_property_closure))
+ g_closure_set_marshal (get_property_closure, g_cclosure_marshal_generic);
+ }
+
+ if (set_property_closure != NULL)
+ {
+ data->set_property_closure = g_closure_ref (set_property_closure);
+ g_closure_sink (set_property_closure);
+ if (G_CLOSURE_NEEDS_MARSHAL (set_property_closure))
+ g_closure_set_marshal (set_property_closure, g_cclosure_marshal_generic);
+ }
+
+ return data;
+}
+
+static void
+register_object_free_func (gpointer user_data)
+{
+ RegisterObjectData *data = user_data;
+
+ g_clear_pointer (&data->method_call_closure, g_closure_unref);
+ g_clear_pointer (&data->get_property_closure, g_closure_unref);
+ g_clear_pointer (&data->set_property_closure, g_closure_unref);
+
+ g_free (data);
+}
+
+static void
+register_with_closures_on_method_call (GDBusConnection *connection,
+ const gchar *sender,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ RegisterObjectData *data = user_data;
+ GValue params[] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
+
+ g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
+ g_value_set_object (¶ms[0], connection);
+
+ g_value_init (¶ms[1], G_TYPE_STRING);
+ g_value_set_string (¶ms[1], sender);
+
+ g_value_init (¶ms[2], G_TYPE_STRING);
+ g_value_set_string (¶ms[2], object_path);
+
+ g_value_init (¶ms[3], G_TYPE_STRING);
+ g_value_set_string (¶ms[3], interface_name);
+
+ g_value_init (¶ms[4], G_TYPE_STRING);
+ g_value_set_string (¶ms[4], method_name);
+
+ g_value_init (¶ms[5], G_TYPE_VARIANT);
+ g_value_set_variant (¶ms[5], parameters);
+
+ g_value_init (¶ms[6], G_TYPE_DBUS_METHOD_INVOCATION);
+ g_value_set_object (¶ms[6], invocation);
+
+ g_closure_invoke (data->method_call_closure, NULL, G_N_ELEMENTS (params), params, NULL);
+
+ g_value_unset (params + 0);
+ g_value_unset (params + 1);
+ g_value_unset (params + 2);
+ g_value_unset (params + 3);
+ g_value_unset (params + 4);
+ g_value_unset (params + 5);
+ g_value_unset (params + 6);
+}
+
+static GVariant *
+register_with_closures_on_get_property (GDBusConnection *connection,
+ const gchar *sender,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *property_name,
+ GError **error,
+ gpointer user_data)
+{
+ RegisterObjectData *data = user_data;
+ GValue params[] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
+ GValue result_value = G_VALUE_INIT;
+ GVariant *result;
+
+ g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
+ g_value_set_object (¶ms[0], connection);
+
+ g_value_init (¶ms[1], G_TYPE_STRING);
+ g_value_set_string (¶ms[1], sender);
+
+ g_value_init (¶ms[2], G_TYPE_STRING);
+ g_value_set_string (¶ms[2], object_path);
+
+ g_value_init (¶ms[3], G_TYPE_STRING);
+ g_value_set_string (¶ms[3], interface_name);
+
+ g_value_init (¶ms[4], G_TYPE_STRING);
+ g_value_set_string (¶ms[4], property_name);
+
+ g_value_init (&result_value, G_TYPE_VARIANT);
+
+ g_closure_invoke (data->get_property_closure, &result_value, G_N_ELEMENTS (params), params, NULL);
+
+ result = g_value_get_variant (&result_value);
+ if (result)
+ g_variant_ref (result);
+
+ g_value_unset (params + 0);
+ g_value_unset (params + 1);
+ g_value_unset (params + 2);
+ g_value_unset (params + 3);
+ g_value_unset (params + 4);
+ g_value_unset (&result_value);
+
+ if (!result)
+ g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
+ _("Unable to retrieve property %s.%s"),
+ interface_name, property_name);
+
+ return result;
+}
+
+static gboolean
+register_with_closures_on_set_property (GDBusConnection *connection,
+ const gchar *sender,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *property_name,
+ GVariant *value,
+ GError **error,
+ gpointer user_data)
+{
+ RegisterObjectData *data = user_data;
+ GValue params[] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
+ GValue result_value = G_VALUE_INIT;
+ gboolean result;
+
+ g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
+ g_value_set_object (¶ms[0], connection);
+
+ g_value_init (¶ms[1], G_TYPE_STRING);
+ g_value_set_string (¶ms[1], sender);
+
+ g_value_init (¶ms[2], G_TYPE_STRING);
+ g_value_set_string (¶ms[2], object_path);
+
+ g_value_init (¶ms[3], G_TYPE_STRING);
+ g_value_set_string (¶ms[3], interface_name);
+
+ g_value_init (¶ms[4], G_TYPE_STRING);
+ g_value_set_string (¶ms[4], property_name);
+
+ g_value_init (¶ms[5], G_TYPE_VARIANT);
+ g_value_set_variant (¶ms[5], value);
+
+ g_value_init (&result_value, G_TYPE_BOOLEAN);
+
+ g_closure_invoke (data->set_property_closure, &result_value, G_N_ELEMENTS (params), params, NULL);
+
+ result = g_value_get_boolean (&result_value);
+
+ g_value_unset (params + 0);
+ g_value_unset (params + 1);
+ g_value_unset (params + 2);
+ g_value_unset (params + 3);
+ g_value_unset (params + 4);
+ g_value_unset (params + 5);
+ g_value_unset (&result_value);
+
+ if (!result)
+ g_set_error (error,
+ G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
+ _("Unable to set property %s.%s"),
+ interface_name, property_name);
+
+ return result;
+}
/**
- * g_dbus_connection_emit_signal:
+ * g_dbus_connection_register_object_with_closures: (rename-to g_dbus_connection_register_object)
* @connection: A #GDBusConnection.
- * @destination_bus_name: (allow-none): The unique bus name for the destination
- * for the signal or %NULL to emit to all listeners.
- * @object_path: Path of remote object.
- * @interface_name: D-Bus interface to emit a signal on.
- * @signal_name: The name of the signal to emit.
- * @parameters: (allow-none): A #GVariant tuple with parameters for the signal
- * or %NULL if not passing parameters.
+ * @object_path: The object path to register at.
+ * @interface_info: Introspection data for the interface.
+ * @method_call_closure: (nullable): #GClosure for handling incoming method calls.
+ * @get_property_closure: (nullable): #GClosure for getting a property.
+ * @set_property_closure: (nullable): #GClosure for setting a property.
* @error: Return location for error or %NULL.
*
+ * Version of g_dbus_connection_register_object() using closures instead of a
+ * #GDBusInterfaceVTable for easier binding in other languages.
+ *
+ * Returns: 0 if @error is set, otherwise a registration id (never 0)
+ * that can be used with g_dbus_connection_unregister_object() .
+ *
+ * Since: 2.46
+ */
+guint
+g_dbus_connection_register_object_with_closures (GDBusConnection *connection,
+ const gchar *object_path,
+ GDBusInterfaceInfo *interface_info,
+ GClosure *method_call_closure,
+ GClosure *get_property_closure,
+ GClosure *set_property_closure,
+ GError **error)
+{
+ RegisterObjectData *data;
+ GDBusInterfaceVTable vtable =
+ {
+ method_call_closure != NULL ? register_with_closures_on_method_call : NULL,
+ get_property_closure != NULL ? register_with_closures_on_get_property : NULL,
+ set_property_closure != NULL ? register_with_closures_on_set_property : NULL
+ };
+
+ data = register_object_data_new (method_call_closure, get_property_closure, set_property_closure);
+
+ return g_dbus_connection_register_object (connection,
+ object_path,
+ interface_info,
+ &vtable,
+ data,
+ register_object_free_func,
+ error);
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+/**
+ * g_dbus_connection_emit_signal:
+ * @connection: a #GDBusConnection
+ * @destination_bus_name: (nullable): the unique bus name for the destination
+ * for the signal or %NULL to emit to all listeners
+ * @object_path: path of remote object
+ * @interface_name: D-Bus interface to emit a signal on
+ * @signal_name: the name of the signal to emit
+ * @parameters: (nullable): a #GVariant tuple with parameters for the signal
+ * or %NULL if not passing parameters
+ * @error: Return location for error or %NULL
+ *
* Emits a signal.
*
* If the parameters GVariant is floating, it is consumed.
*
- * This can only fail if @parameters is not compatible with the D-Bus protocol.
+ * This can only fail if @parameters is not compatible with the D-Bus protocol
+ * (%G_IO_ERROR_INVALID_ARGUMENT), or if @connection has been closed
+ * (%G_IO_ERROR_CLOSED).
*
- * Returns: %TRUE unless @error is set.
+ * Returns: %TRUE unless @error is set
*
* Since: 2.26
*/
add_call_flags (GDBusMessage *message,
GDBusCallFlags flags)
{
+ GDBusMessageFlags msg_flags = 0;
+
if (flags & G_DBUS_CALL_FLAGS_NO_AUTO_START)
- g_dbus_message_set_flags (message, G_DBUS_MESSAGE_FLAGS_NO_AUTO_START);
+ msg_flags |= G_DBUS_MESSAGE_FLAGS_NO_AUTO_START;
+ if (flags & G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION)
+ msg_flags |= G_DBUS_MESSAGE_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION;
+ if (msg_flags)
+ g_dbus_message_set_flags (message, msg_flags);
}
static GVariant *
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
- _("Method `%s' returned type `%s', but expected `%s'"),
+ _("Method “%s” returned type “%s”, but expected “%s”"),
method_name, g_variant_get_type_string (result), type_string);
g_variant_unref (result);
typedef struct
{
- GSimpleAsyncResult *simple;
GVariantType *reply_type;
gchar *method_name; /* for error message */
- guint32 serial;
- GVariant *value;
GUnixFDList *fd_list;
} CallState;
g_variant_type_free (state->reply_type);
g_free (state->method_name);
- if (state->value != NULL)
- g_variant_unref (state->value);
if (state->fd_list != NULL)
g_object_unref (state->fd_list);
g_slice_free (CallState, state);
GAsyncResult *result,
gpointer user_data)
{
- GSimpleAsyncResult *simple;
GDBusConnection *connection = G_DBUS_CONNECTION (source);
- CallState *state = user_data;
- GError *error;
+ GTask *task = user_data;
+ CallState *state = g_task_get_task_data (task);
+ GError *error = NULL;
GDBusMessage *reply;
+ GVariant *value = NULL;
- error = NULL;
reply = g_dbus_connection_send_message_with_reply_finish (connection,
result,
&error);
" <<<< ASYNC COMPLETE %s() (serial %d)\n"
" ",
state->method_name,
- state->serial);
+ g_dbus_message_get_reply_serial (reply));
if (reply != NULL)
{
g_print ("SUCCESS\n");
}
if (reply != NULL)
- state->value = decode_method_reply (reply, state->method_name, state->reply_type, &state->fd_list, &error);
+ value = decode_method_reply (reply, state->method_name, state->reply_type, &state->fd_list, &error);
- simple = state->simple; /* why? because state is freed before we unref simple.. */
if (error != NULL)
- {
- g_simple_async_result_take_error (state->simple, error);
- g_simple_async_result_complete (state->simple);
- call_state_free (state);
- }
+ g_task_return_error (task, error);
else
- {
- g_simple_async_result_set_op_res_gpointer (state->simple, state, (GDestroyNotify) call_state_free);
- g_simple_async_result_complete (state->simple);
- g_object_unref (reply);
- }
- g_object_unref (simple);
+ g_task_return_pointer (task, value, (GDestroyNotify) g_variant_unref);
+
+ g_clear_object (&reply);
+ g_object_unref (task);
}
/* called in any thread, with the connection's lock not held */
gpointer user_data)
{
GDBusMessage *message;
- CallState *state;
+ guint32 serial;
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
g_return_if_fail (bus_name == NULL || g_dbus_is_name (bus_name));
g_return_if_fail (fd_list == NULL);
#endif
- state = g_slice_new0 (CallState);
- state->simple = g_simple_async_result_new (G_OBJECT (connection),
- callback, user_data,
- g_dbus_connection_call_internal);
- state->method_name = g_strjoin (".", interface_name, method_name, NULL);
-
- if (reply_type == NULL)
- reply_type = G_VARIANT_TYPE_ANY;
-
- state->reply_type = g_variant_type_copy (reply_type);
-
message = g_dbus_message_new_method_call (bus_name,
object_path,
interface_name,
g_dbus_message_set_unix_fd_list (message, fd_list);
#endif
- g_dbus_connection_send_message_with_reply (connection,
- message,
- G_DBUS_SEND_MESSAGE_FLAGS_NONE,
- timeout_msec,
- &state->serial,
- cancellable,
- g_dbus_connection_call_done,
- state);
+ /* If the user has no callback then we can just send the message with
+ * the G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set and skip all
+ * the logic for processing the reply. If the service sends the reply
+ * anyway then it will just be ignored.
+ */
+ if (callback != NULL)
+ {
+ CallState *state;
+ GTask *task;
+
+ state = g_slice_new0 (CallState);
+ state->method_name = g_strjoin (".", interface_name, method_name, NULL);
+
+ if (reply_type == NULL)
+ reply_type = G_VARIANT_TYPE_ANY;
+
+ state->reply_type = g_variant_type_copy (reply_type);
+
+ task = g_task_new (connection, cancellable, callback, user_data);
+ g_task_set_source_tag (task, g_dbus_connection_call_internal);
+ g_task_set_task_data (task, state, (GDestroyNotify) call_state_free);
+
+ g_dbus_connection_send_message_with_reply (connection,
+ message,
+ G_DBUS_SEND_MESSAGE_FLAGS_NONE,
+ timeout_msec,
+ &serial,
+ cancellable,
+ g_dbus_connection_call_done,
+ task);
+ }
+ else
+ {
+ GDBusMessageFlags flags;
+
+ flags = g_dbus_message_get_flags (message);
+ flags |= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
+ g_dbus_message_set_flags (message, flags);
+
+ g_dbus_connection_send_message (connection,
+ message,
+ G_DBUS_SEND_MESSAGE_FLAGS_NONE,
+ &serial, NULL);
+ }
if (G_UNLIKELY (_g_dbus_debug_call ()))
{
method_name,
object_path,
bus_name != NULL ? bus_name : "(none)",
- state->serial);
+ serial);
_g_dbus_debug_print_unlock ();
}
GAsyncResult *res,
GError **error)
{
- GSimpleAsyncResult *simple;
+ GTask *task;
CallState *state;
+ GVariant *ret;
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
- g_return_val_if_fail (g_simple_async_result_is_valid (res, G_OBJECT (connection),
- g_dbus_connection_call_internal), NULL);
+ g_return_val_if_fail (g_task_is_valid (res, connection), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
- simple = G_SIMPLE_ASYNC_RESULT (res);
+ task = G_TASK (res);
+ state = g_task_get_task_data (task);
- if (g_simple_async_result_propagate_error (simple, error))
+ ret = g_task_propagate_pointer (task, error);
+ if (!ret)
return NULL;
- state = g_simple_async_result_get_op_res_gpointer (simple);
if (out_fd_list != NULL)
*out_fd_list = state->fd_list != NULL ? g_object_ref (state->fd_list) : NULL;
- return g_variant_ref (state->value);
+ return ret;
}
/* called in any user thread, with the connection's lock not held */
/**
* g_dbus_connection_call:
- * @connection: A #GDBusConnection.
- * @bus_name: (allow-none): A unique or well-known bus name or %NULL if
- * @connection is not a message bus connection.
- * @object_path: Path of remote object.
- * @interface_name: D-Bus interface to invoke method on.
- * @method_name: The name of the method to invoke.
- * @parameters: (allow-none): A #GVariant tuple with parameters for the method
- * or %NULL if not passing parameters.
- * @reply_type: (allow-none): The expected type of the reply, or %NULL.
- * @flags: Flags from the #GDBusCallFlags enumeration.
- * @timeout_msec: The timeout in milliseconds, -1 to use the default
- * timeout or %G_MAXINT for no timeout.
- * @cancellable: A #GCancellable or %NULL.
- * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
- * satisfied or %NULL if you don't care about the result of the
- * method invocation.
- * @user_data: The data to pass to @callback.
+ * @connection: a #GDBusConnection
+ * @bus_name: (nullable): a unique or well-known bus name or %NULL if
+ * @connection is not a message bus connection
+ * @object_path: path of remote object
+ * @interface_name: D-Bus interface to invoke method on
+ * @method_name: the name of the method to invoke
+ * @parameters: (nullable): a #GVariant tuple with parameters for the method
+ * or %NULL if not passing parameters
+ * @reply_type: (nullable): the expected type of the reply (which will be a
+ * tuple), or %NULL
+ * @flags: flags from the #GDBusCallFlags enumeration
+ * @timeout_msec: the timeout in milliseconds, -1 to use the default
+ * timeout or %G_MAXINT for no timeout
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @callback: (nullable): a #GAsyncReadyCallback to call when the request
+ * is satisfied or %NULL if you don't care about the result of the
+ * method invocation
+ * @user_data: the data to pass to @callback
*
* Asynchronously invokes the @method_name method on the
* @interface_name D-Bus interface on the remote object at
*
* If @reply_type is non-%NULL then the reply will be checked for having this type and an
* error will be raised if it does not match. Said another way, if you give a @reply_type
- * then any non-%NULL return value will be of this type.
+ * then any non-%NULL return value will be of this type. Unless it’s
+ * %G_VARIANT_TYPE_UNIT, the @reply_type will be a tuple containing one or more
+ * values.
*
* If the @parameters #GVariant is floating, it is consumed. This allows
* convenient 'inline' use of g_variant_new(), e.g.:
- * |[
+ * |[<!-- language="C" -->
* g_dbus_connection_call (connection,
* "org.freedesktop.StringThings",
* "/org/freedesktop/StringThings",
* NULL);
* ]|
*
- * This is an asynchronous method. When the operation is finished, @callback will be invoked
- * in the <link linkend="g-main-context-push-thread-default">thread-default main loop</link>
+ * This is an asynchronous method. When the operation is finished,
+ * @callback will be invoked in the
+ * [thread-default main context][g-main-context-push-thread-default]
* of the thread you are calling this method from. You can then call
* g_dbus_connection_call_finish() to get the result of the operation.
* See g_dbus_connection_call_sync() for the synchronous version of this
* function.
*
+ * If @callback is %NULL then the D-Bus method call message will be sent with
+ * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
+ *
* Since: 2.26
*/
void
-g_dbus_connection_call (GDBusConnection *connection,
- const gchar *bus_name,
- const gchar *object_path,
- const gchar *interface_name,
- const gchar *method_name,
- GVariant *parameters,
- const GVariantType *reply_type,
- GDBusCallFlags flags,
- gint timeout_msec,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data)
+g_dbus_connection_call (GDBusConnection *connection,
+ const gchar *bus_name,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ const GVariantType *reply_type,
+ GDBusCallFlags flags,
+ gint timeout_msec,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
g_dbus_connection_call_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, NULL, cancellable, callback, user_data);
}
/**
* g_dbus_connection_call_finish:
- * @connection: A #GDBusConnection.
- * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call().
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call()
+ * @error: return location for error or %NULL
*
* Finishes an operation started with g_dbus_connection_call().
*
* Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
- * return values. Free with g_variant_unref().
+ * return values. Free with g_variant_unref().
*
* Since: 2.26
*/
/**
* g_dbus_connection_call_sync:
- * @connection: A #GDBusConnection.
- * @bus_name: A unique or well-known bus name.
- * @object_path: Path of remote object.
- * @interface_name: D-Bus interface to invoke method on.
- * @method_name: The name of the method to invoke.
- * @parameters: (allow-none): A #GVariant tuple with parameters for the method
- * or %NULL if not passing parameters.
- * @reply_type: (allow-none): The expected type of the reply, or %NULL.
- * @flags: Flags from the #GDBusCallFlags enumeration.
- * @timeout_msec: The timeout in milliseconds, -1 to use the default
- * timeout or %G_MAXINT for no timeout.
- * @cancellable: A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @bus_name: (nullable): a unique or well-known bus name or %NULL if
+ * @connection is not a message bus connection
+ * @object_path: path of remote object
+ * @interface_name: D-Bus interface to invoke method on
+ * @method_name: the name of the method to invoke
+ * @parameters: (nullable): a #GVariant tuple with parameters for the method
+ * or %NULL if not passing parameters
+ * @reply_type: (nullable): the expected type of the reply, or %NULL
+ * @flags: flags from the #GDBusCallFlags enumeration
+ * @timeout_msec: the timeout in milliseconds, -1 to use the default
+ * timeout or %G_MAXINT for no timeout
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @error: return location for error or %NULL
*
* Synchronously invokes the @method_name method on the
* @interface_name D-Bus interface on the remote object at
* operation will fail with %G_IO_ERROR_CANCELLED. If @parameters
* contains a value not compatible with the D-Bus protocol, the operation
* fails with %G_IO_ERROR_INVALID_ARGUMENT.
-
+ *
* If @reply_type is non-%NULL then the reply will be checked for having
* this type and an error will be raised if it does not match. Said
* another way, if you give a @reply_type then any non-%NULL return
*
* If the @parameters #GVariant is floating, it is consumed.
* This allows convenient 'inline' use of g_variant_new(), e.g.:
- * |[
+ * |[<!-- language="C" -->
* g_dbus_connection_call_sync (connection,
* "org.freedesktop.StringThings",
* "/org/freedesktop/StringThings",
* G_DBUS_CALL_FLAGS_NONE,
* -1,
* NULL,
- * &error);
+ * &error);
* ]|
*
* The calling thread is blocked until a reply is received. See
* this method.
*
* Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
- * return values. Free with g_variant_unref().
+ * return values. Free with g_variant_unref().
*
* Since: 2.26
*/
GVariant *
-g_dbus_connection_call_sync (GDBusConnection *connection,
- const gchar *bus_name,
- const gchar *object_path,
- const gchar *interface_name,
- const gchar *method_name,
- GVariant *parameters,
- const GVariantType *reply_type,
- GDBusCallFlags flags,
- gint timeout_msec,
- GCancellable *cancellable,
- GError **error)
+g_dbus_connection_call_sync (GDBusConnection *connection,
+ const gchar *bus_name,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ const GVariantType *reply_type,
+ GDBusCallFlags flags,
+ gint timeout_msec,
+ GCancellable *cancellable,
+ GError **error)
{
return g_dbus_connection_call_sync_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, NULL, NULL, cancellable, error);
}
/**
* g_dbus_connection_call_with_unix_fd_list:
- * @connection: A #GDBusConnection.
- * @bus_name: (allow-none): A unique or well-known bus name or %NULL if
- * @connection is not a message bus connection.
- * @object_path: Path of remote object.
- * @interface_name: D-Bus interface to invoke method on.
- * @method_name: The name of the method to invoke.
- * @parameters: (allow-none): A #GVariant tuple with parameters for the method
- * or %NULL if not passing parameters.
- * @reply_type: (allow-none): The expected type of the reply, or %NULL.
- * @flags: Flags from the #GDBusCallFlags enumeration.
- * @timeout_msec: The timeout in milliseconds, -1 to use the default
- * timeout or %G_MAXINT for no timeout.
- * @fd_list: (allow-none): A #GUnixFDList or %NULL.
- * @cancellable: A #GCancellable or %NULL.
- * @callback: (allow-none): A #GAsyncReadyCallback to call when the request is
- * satisfied or %NULL if you don't * care about the result of the
- * method invocation.
+ * @connection: a #GDBusConnection
+ * @bus_name: (nullable): a unique or well-known bus name or %NULL if
+ * @connection is not a message bus connection
+ * @object_path: path of remote object
+ * @interface_name: D-Bus interface to invoke method on
+ * @method_name: the name of the method to invoke
+ * @parameters: (nullable): a #GVariant tuple with parameters for the method
+ * or %NULL if not passing parameters
+ * @reply_type: (nullable): the expected type of the reply, or %NULL
+ * @flags: flags from the #GDBusCallFlags enumeration
+ * @timeout_msec: the timeout in milliseconds, -1 to use the default
+ * timeout or %G_MAXINT for no timeout
+ * @fd_list: (nullable): a #GUnixFDList or %NULL
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @callback: (nullable): a #GAsyncReadyCallback to call when the request is
+ * satisfied or %NULL if you don't * care about the result of the
+ * method invocation
* @user_data: The data to pass to @callback.
*
* Like g_dbus_connection_call() but also takes a #GUnixFDList object.
* Since: 2.30
*/
void
-g_dbus_connection_call_with_unix_fd_list (GDBusConnection *connection,
- const gchar *bus_name,
- const gchar *object_path,
- const gchar *interface_name,
- const gchar *method_name,
- GVariant *parameters,
- const GVariantType *reply_type,
- GDBusCallFlags flags,
- gint timeout_msec,
- GUnixFDList *fd_list,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data)
+g_dbus_connection_call_with_unix_fd_list (GDBusConnection *connection,
+ const gchar *bus_name,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ const GVariantType *reply_type,
+ GDBusCallFlags flags,
+ gint timeout_msec,
+ GUnixFDList *fd_list,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
{
g_dbus_connection_call_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, fd_list, cancellable, callback, user_data);
}
/**
* g_dbus_connection_call_with_unix_fd_list_finish:
- * @connection: A #GDBusConnection.
- * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
- * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call_with_unix_fd_list().
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @out_fd_list: (out) (optional): return location for a #GUnixFDList or %NULL
+ * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to
+ * g_dbus_connection_call_with_unix_fd_list()
+ * @error: return location for error or %NULL
*
* Finishes an operation started with g_dbus_connection_call_with_unix_fd_list().
*
* Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
- * return values. Free with g_variant_unref().
+ * return values. Free with g_variant_unref().
*
* Since: 2.30
*/
/**
* g_dbus_connection_call_with_unix_fd_list_sync:
- * @connection: A #GDBusConnection.
- * @bus_name: A unique or well-known bus name.
- * @object_path: Path of remote object.
- * @interface_name: D-Bus interface to invoke method on.
- * @method_name: The name of the method to invoke.
- * @parameters: (allow-none): A #GVariant tuple with parameters for the method
- * or %NULL if not passing parameters.
- * @reply_type: (allow-none): The expected type of the reply, or %NULL.
- * @flags: Flags from the #GDBusCallFlags enumeration.
- * @timeout_msec: The timeout in milliseconds, -1 to use the default
- * timeout or %G_MAXINT for no timeout.
- * @fd_list: (allow-none): A #GUnixFDList or %NULL.
- * @out_fd_list: (out): Return location for a #GUnixFDList or %NULL.
- * @cancellable: A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @bus_name: (nullable): a unique or well-known bus name or %NULL
+ * if @connection is not a message bus connection
+ * @object_path: path of remote object
+ * @interface_name: D-Bus interface to invoke method on
+ * @method_name: the name of the method to invoke
+ * @parameters: (nullable): a #GVariant tuple with parameters for
+ * the method or %NULL if not passing parameters
+ * @reply_type: (nullable): the expected type of the reply, or %NULL
+ * @flags: flags from the #GDBusCallFlags enumeration
+ * @timeout_msec: the timeout in milliseconds, -1 to use the default
+ * timeout or %G_MAXINT for no timeout
+ * @fd_list: (nullable): a #GUnixFDList or %NULL
+ * @out_fd_list: (out) (optional): return location for a #GUnixFDList or %NULL
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @error: return location for error or %NULL
*
* Like g_dbus_connection_call_sync() but also takes and returns #GUnixFDList objects.
*
* This method is only available on UNIX.
*
* Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
- * return values. Free with g_variant_unref().
+ * return values. Free with g_variant_unref().
*
* Since: 2.30
*/
GVariant *
-g_dbus_connection_call_with_unix_fd_list_sync (GDBusConnection *connection,
- const gchar *bus_name,
- const gchar *object_path,
- const gchar *interface_name,
- const gchar *method_name,
- GVariant *parameters,
- const GVariantType *reply_type,
- GDBusCallFlags flags,
- gint timeout_msec,
- GUnixFDList *fd_list,
- GUnixFDList **out_fd_list,
- GCancellable *cancellable,
- GError **error)
+g_dbus_connection_call_with_unix_fd_list_sync (GDBusConnection *connection,
+ const gchar *bus_name,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ const GVariantType *reply_type,
+ GDBusCallFlags flags,
+ gint timeout_msec,
+ GUnixFDList *fd_list,
+ GUnixFDList **out_fd_list,
+ GCancellable *cancellable,
+ GError **error)
{
return g_dbus_connection_call_sync_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
}
GDBusMessage *reply;
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.InvalidArgs",
- _("No such interface `%s'"),
+ _("No such interface “%s”"),
interface_name);
g_dbus_connection_send_message (es->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
g_object_unref (reply);
GDBusMessage *reply;
reply = g_dbus_message_new_method_error (data->message,
"org.freedesktop.DBus.Error.UnknownMethod",
- _("Method `%s' on interface `%s' with signature `%s' does not exist"),
+ _("Method “%s” on interface “%s” with signature “%s” does not exist"),
g_dbus_message_get_member (data->message),
g_dbus_message_get_interface (data->message),
g_dbus_message_get_signature (data->message));
process_subtree_vtable_message_in_idle_cb,
data,
(GDestroyNotify) subtree_deferred_data_free);
+ g_source_set_name (idle_source, "[gio] process_subtree_vtable_message_in_idle_cb");
g_source_attach (idle_source, es->context);
g_source_unref (idle_source);
/**
* g_dbus_connection_register_subtree:
- * @connection: A #GDBusConnection.
- * @object_path: The object path to register the subtree at.
- * @vtable: A #GDBusSubtreeVTable to enumerate, introspect and dispatch nodes in the subtree.
- * @flags: Flags used to fine tune the behavior of the subtree.
- * @user_data: Data to pass to functions in @vtable.
- * @user_data_free_func: Function to call when the subtree is unregistered.
- * @error: Return location for error or %NULL.
+ * @connection: a #GDBusConnection
+ * @object_path: the object path to register the subtree at
+ * @vtable: a #GDBusSubtreeVTable to enumerate, introspect and
+ * dispatch nodes in the subtree
+ * @flags: flags used to fine tune the behavior of the subtree
+ * @user_data: data to pass to functions in @vtable
+ * @user_data_free_func: function to call when the subtree is unregistered
+ * @error: return location for error or %NULL
*
- * Registers a whole subtree of <quote>dynamic</quote> objects.
+ * Registers a whole subtree of dynamic objects.
*
* The @enumerate and @introspection functions in @vtable are used to
* convey, to remote callers, what nodes exist in the subtree rooted
* #gpointer will be used to call into the interface vtable for processing
* the request.
*
- * All calls into user-provided code will be invoked in the <link
- * linkend="g-main-context-push-thread-default">thread-default main
- * loop</link> of the thread you are calling this method from.
+ * All calls into user-provided code will be invoked in the
+ * [thread-default main context][g-main-context-push-thread-default]
+ * of the thread you are calling this method from.
*
* If an existing subtree is already registered at @object_path or
* then @error is set to #G_IO_ERROR_EXISTS.
* g_dbus_connection_register_object()) in a subtree registered with
* g_dbus_connection_register_subtree() - if so, the subtree handler
* is tried as the last resort. One way to think about a subtree
- * handler is to consider it a <quote>fallback handler</quote>
- * for object paths not registered via g_dbus_connection_register_object()
- * or other bindings.
+ * handler is to consider it a fallback handler for object paths not
+ * registered via g_dbus_connection_register_object() or other bindings.
*
* Note that @vtable will be copied so you cannot change it after
* registration.
*
- * See <xref linkend="gdbus-subtree-server"/> for an example of how to use this method.
+ * See this [server][gdbus-subtree-server] for an example of how to use
+ * this method.
*
* Returns: 0 if @error is set, otherwise a subtree registration id (never 0)
* that can be used with g_dbus_connection_unregister_subtree() .
es->vtable = _g_dbus_subtree_vtable_copy (vtable);
es->flags = flags;
- es->id = _global_subtree_registration_id++; /* TODO: overflow etc. */
+ es->id = g_atomic_int_add (&_global_subtree_registration_id, 1); /* TODO: overflow etc. */
es->user_data = user_data;
es->user_data_free_func = user_data_free_func;
es->context = g_main_context_ref_thread_default ();
/**
* g_dbus_connection_unregister_subtree:
- * @connection: A #GDBusConnection.
- * @registration_id: A subtree registration id obtained from g_dbus_connection_register_subtree().
+ * @connection: a #GDBusConnection
+ * @registration_id: a subtree registration id obtained from
+ * g_dbus_connection_register_subtree()
*
* Unregisters a subtree.
*
- * Returns: %TRUE if the subtree was unregistered, %FALSE otherwise.
+ * Returns: %TRUE if the subtree was unregistered, %FALSE otherwise
*
* Since: 2.26
*/
/* if we end up here, the message has not been not handled - so return an error saying this */
reply = g_dbus_message_new_method_error (message,
"org.freedesktop.DBus.Error.UnknownMethod",
- _("No such interface `%s' on object at path %s"),
+ _("No such interface “%s” on object at path %s"),
interface_name,
object_path);
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
_("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
- " - unknown value `%s'"),
+ " — unknown value “%s”"),
starter_bus);
}
else
return ret;
}
+/* May be called from any thread. Must not hold message_bus_lock. */
+GDBusConnection *
+_g_bus_get_singleton_if_exists (GBusType bus_type)
+{
+ GWeakRef *singleton;
+ GDBusConnection *ret = NULL;
+
+ G_LOCK (message_bus_lock);
+ singleton = message_bus_get_singleton (bus_type, NULL);
+ if (singleton == NULL)
+ goto out;
+
+ ret = g_weak_ref_get (singleton);
+
+ out:
+ G_UNLOCK (message_bus_lock);
+ return ret;
+}
+
+/* May be called from any thread. Must not hold message_bus_lock. */
+void
+_g_bus_forget_singleton (GBusType bus_type)
+{
+ GWeakRef *singleton;
+
+ G_LOCK (message_bus_lock);
+
+ singleton = message_bus_get_singleton (bus_type, NULL);
+
+ if (singleton != NULL)
+ g_weak_ref_set (singleton, NULL);
+
+ G_UNLOCK (message_bus_lock);
+}
+
/**
* g_bus_get_sync:
- * @bus_type: A #GBusType.
- * @cancellable: A #GCancellable or %NULL.
- * @error: Return location for error or %NULL.
+ * @bus_type: a #GBusType
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @error: return location for error or %NULL
*
* Synchronously connects to the message bus specified by @bus_type.
* Note that the returned object may shared with other callers,
* Note that the returned #GDBusConnection object will (usually) have
* the #GDBusConnection:exit-on-close property set to %TRUE.
*
- * Returns: (transfer full): A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
+ * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
+ * Free with g_object_unref().
*
* Since: 2.26
*/
{
GDBusConnection *connection;
+ _g_dbus_initialize ();
+
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
connection = get_uninitialized_connection (bus_type, cancellable, error);
GAsyncResult *res,
gpointer user_data)
{
- GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
- GError *error;
+ GTask *task = user_data;
+ GError *error = NULL;
- error = NULL;
if (!g_async_initable_init_finish (G_ASYNC_INITABLE (source_object),
res,
&error))
{
g_assert (error != NULL);
- g_simple_async_result_take_error (simple, error);
+ g_task_return_error (task, error);
g_object_unref (source_object);
}
else
{
- g_simple_async_result_set_op_res_gpointer (simple,
- source_object,
- g_object_unref);
+ g_task_return_pointer (task, source_object, g_object_unref);
}
- g_simple_async_result_complete_in_idle (simple);
- g_object_unref (simple);
+ g_object_unref (task);
}
/**
* g_bus_get:
- * @bus_type: A #GBusType.
- * @cancellable: A #GCancellable or %NULL.
- * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
- * @user_data: The data to pass to @callback.
+ * @bus_type: a #GBusType
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @callback: a #GAsyncReadyCallback to call when the request is satisfied
+ * @user_data: the data to pass to @callback
*
* Asynchronously connects to the message bus specified by @bus_type.
*
* When the operation is finished, @callback will be invoked. You can
* then call g_bus_get_finish() to get the result of the operation.
*
- * This is a asynchronous failable function. See g_bus_get_sync() for
+ * This is an asynchronous failable function. See g_bus_get_sync() for
* the synchronous version.
*
* Since: 2.26
gpointer user_data)
{
GDBusConnection *connection;
- GSimpleAsyncResult *simple;
- GError *error;
+ GTask *task;
+ GError *error = NULL;
- simple = g_simple_async_result_new (NULL,
- callback,
- user_data,
- g_bus_get);
+ _g_dbus_initialize ();
+
+ task = g_task_new (NULL, cancellable, callback, user_data);
+ g_task_set_source_tag (task, g_bus_get);
- error = NULL;
connection = get_uninitialized_connection (bus_type, cancellable, &error);
if (connection == NULL)
{
g_assert (error != NULL);
- g_simple_async_result_take_error (simple, error);
- g_simple_async_result_complete_in_idle (simple);
- g_object_unref (simple);
+ g_task_return_error (task, error);
+ g_object_unref (task);
}
else
{
G_PRIORITY_DEFAULT,
cancellable,
bus_get_async_initable_cb,
- simple);
+ task);
}
}
/**
* g_bus_get_finish:
- * @res: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_bus_get().
- * @error: Return location for error or %NULL.
+ * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
+ * to g_bus_get()
+ * @error: return location for error or %NULL
*
* Finishes an operation started with g_bus_get().
*
* Note that the returned #GDBusConnection object will (usually) have
* the #GDBusConnection:exit-on-close property set to %TRUE.
*
- * Returns: (transfer full): A #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
+ * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
+ * Free with g_object_unref().
*
* Since: 2.26
*/
g_bus_get_finish (GAsyncResult *res,
GError **error)
{
- GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
- GObject *object;
- GDBusConnection *ret;
-
+ g_return_val_if_fail (g_task_is_valid (res, NULL), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
- g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_bus_get);
-
- ret = NULL;
-
- if (g_simple_async_result_propagate_error (simple, error))
- goto out;
-
- object = g_simple_async_result_get_op_res_gpointer (simple);
- g_assert (object != NULL);
- ret = g_object_ref (G_DBUS_CONNECTION (object));
-
- out:
- return ret;
+ return g_task_propagate_pointer (G_TASK (res), error);
}
/* ---------------------------------------------------------------------------------------------------- */