2010-05-06 20:13:59 +02:00
|
|
|
|
/* GDBus - GLib D-Bus Library
|
|
|
|
|
*
|
2010-05-09 19:14:55 +02:00
|
|
|
|
* Copyright (C) 2008-2010 Red Hat, Inc.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
|
*
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* 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
|
2017-05-27 18:21:30 +02:00
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Author: David Zeuthen <davidz@redhat.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2010-05-06 22:34:23 +02:00
|
|
|
|
#include <string.h>
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
#include "gdbusutils.h"
|
|
|
|
|
#include "gdbusnamewatching.h"
|
|
|
|
|
#include "gdbuserror.h"
|
|
|
|
|
#include "gdbusprivate.h"
|
|
|
|
|
#include "gdbusconnection.h"
|
|
|
|
|
|
2010-05-06 22:34:23 +02:00
|
|
|
|
#include "glibintl.h"
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
G_LOCK_DEFINE_STATIC (lock);
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
PREVIOUS_CALL_NONE = 0,
|
|
|
|
|
PREVIOUS_CALL_APPEARED,
|
|
|
|
|
PREVIOUS_CALL_VANISHED,
|
|
|
|
|
} PreviousCall;
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2020-11-11 19:28:23 +01:00
|
|
|
|
gint ref_count; /* (atomic) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
guint id;
|
|
|
|
|
gchar *name;
|
|
|
|
|
GBusNameWatcherFlags flags;
|
|
|
|
|
gchar *name_owner;
|
|
|
|
|
GBusNameAppearedCallback name_appeared_handler;
|
|
|
|
|
GBusNameVanishedCallback name_vanished_handler;
|
|
|
|
|
gpointer user_data;
|
|
|
|
|
GDestroyNotify user_data_free_func;
|
|
|
|
|
GMainContext *main_context;
|
|
|
|
|
|
|
|
|
|
GDBusConnection *connection;
|
|
|
|
|
gulong disconnected_signal_handler_id;
|
|
|
|
|
guint name_owner_changed_subscription_id;
|
|
|
|
|
|
|
|
|
|
PreviousCall previous_call;
|
|
|
|
|
|
|
|
|
|
gboolean cancelled;
|
|
|
|
|
gboolean initialized;
|
|
|
|
|
} Client;
|
|
|
|
|
|
2017-02-03 11:03:56 +01:00
|
|
|
|
/* Must be accessed atomically. */
|
2020-11-11 19:28:23 +01:00
|
|
|
|
static guint next_global_id = 1; /* (atomic) */
|
2017-02-03 11:03:56 +01:00
|
|
|
|
|
|
|
|
|
/* Must be accessed with @lock held. */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static GHashTable *map_id_to_client = NULL;
|
|
|
|
|
|
|
|
|
|
static Client *
|
|
|
|
|
client_ref (Client *client)
|
|
|
|
|
{
|
|
|
|
|
g_atomic_int_inc (&client->ref_count);
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 15:16:36 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
free_user_data_cb (gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
/* The user data is actually freed by the GDestroyNotify for the idle source */
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
client_unref (Client *client)
|
|
|
|
|
{
|
|
|
|
|
if (g_atomic_int_dec_and_test (&client->ref_count))
|
|
|
|
|
{
|
|
|
|
|
if (client->connection != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (client->name_owner_changed_subscription_id > 0)
|
|
|
|
|
g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
|
|
|
|
|
if (client->disconnected_signal_handler_id > 0)
|
|
|
|
|
g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
|
|
|
|
|
g_object_unref (client->connection);
|
|
|
|
|
}
|
|
|
|
|
g_free (client->name);
|
|
|
|
|
g_free (client->name_owner);
|
2021-06-21 15:16:36 +02:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (client->user_data_free_func != NULL)
|
2021-06-21 15:16:36 +02:00
|
|
|
|
{
|
|
|
|
|
/* Ensure client->user_data_free_func() is called from the right thread */
|
|
|
|
|
if (client->main_context != g_main_context_get_thread_default ())
|
|
|
|
|
{
|
|
|
|
|
GSource *idle_source = g_idle_source_new ();
|
|
|
|
|
g_source_set_callback (idle_source, free_user_data_cb,
|
|
|
|
|
client->user_data,
|
|
|
|
|
client->user_data_free_func);
|
|
|
|
|
g_source_set_name (idle_source, "[gio, gdbusnamewatching.c] free_user_data_cb");
|
|
|
|
|
g_source_attach (idle_source, client->main_context);
|
|
|
|
|
g_source_unref (idle_source);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
client->user_data_free_func (client->user_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_main_context_unref (client->main_context);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_free (client);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
CALL_TYPE_NAME_APPEARED,
|
|
|
|
|
CALL_TYPE_NAME_VANISHED
|
|
|
|
|
} CallType;
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
Client *client;
|
|
|
|
|
|
|
|
|
|
/* keep this separate because client->connection may
|
|
|
|
|
* be set to NULL after scheduling the call
|
|
|
|
|
*/
|
|
|
|
|
GDBusConnection *connection;
|
|
|
|
|
|
|
|
|
|
/* ditto */
|
|
|
|
|
gchar *name_owner;
|
|
|
|
|
|
|
|
|
|
CallType call_type;
|
|
|
|
|
} CallHandlerData;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
call_handler_data_free (CallHandlerData *data)
|
|
|
|
|
{
|
|
|
|
|
if (data->connection != NULL)
|
|
|
|
|
g_object_unref (data->connection);
|
|
|
|
|
g_free (data->name_owner);
|
|
|
|
|
client_unref (data->client);
|
|
|
|
|
g_free (data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
actually_do_call (Client *client, GDBusConnection *connection, const gchar *name_owner, CallType call_type)
|
|
|
|
|
{
|
2020-01-20 20:17:13 +01:00
|
|
|
|
/* The client might have been cancelled (g_bus_unwatch_name()) while we were
|
|
|
|
|
* sitting in the #GMainContext dispatch queue. */
|
|
|
|
|
if (client->cancelled)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
switch (call_type)
|
|
|
|
|
{
|
|
|
|
|
case CALL_TYPE_NAME_APPEARED:
|
|
|
|
|
if (client->name_appeared_handler != NULL)
|
|
|
|
|
{
|
|
|
|
|
client->name_appeared_handler (connection,
|
|
|
|
|
client->name,
|
|
|
|
|
name_owner,
|
|
|
|
|
client->user_data);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CALL_TYPE_NAME_VANISHED:
|
|
|
|
|
if (client->name_vanished_handler != NULL)
|
|
|
|
|
{
|
|
|
|
|
client->name_vanished_handler (connection,
|
|
|
|
|
client->name,
|
|
|
|
|
client->user_data);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
call_in_idle_cb (gpointer _data)
|
|
|
|
|
{
|
|
|
|
|
CallHandlerData *data = _data;
|
|
|
|
|
actually_do_call (data->client, data->connection, data->name_owner, data->call_type);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
schedule_call_in_idle (Client *client, CallType call_type)
|
|
|
|
|
{
|
|
|
|
|
CallHandlerData *data;
|
|
|
|
|
GSource *idle_source;
|
|
|
|
|
|
|
|
|
|
data = g_new0 (CallHandlerData, 1);
|
|
|
|
|
data->client = client_ref (client);
|
|
|
|
|
data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
|
|
|
|
|
data->name_owner = g_strdup (client->name_owner);
|
|
|
|
|
data->call_type = call_type;
|
|
|
|
|
|
|
|
|
|
idle_source = g_idle_source_new ();
|
|
|
|
|
g_source_set_priority (idle_source, G_PRIORITY_HIGH);
|
|
|
|
|
g_source_set_callback (idle_source,
|
|
|
|
|
call_in_idle_cb,
|
|
|
|
|
data,
|
|
|
|
|
(GDestroyNotify) call_handler_data_free);
|
2021-07-26 11:53:02 +02:00
|
|
|
|
g_source_set_static_name (idle_source, "[gio, gdbusnamewatching.c] call_in_idle_cb");
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_source_attach (idle_source, client->main_context);
|
|
|
|
|
g_source_unref (idle_source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
do_call (Client *client, CallType call_type)
|
|
|
|
|
{
|
2011-10-05 16:46:57 +02:00
|
|
|
|
GMainContext *current_context;
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/* only schedule in idle if we're not in the right thread */
|
2011-10-05 16:46:57 +02:00
|
|
|
|
current_context = g_main_context_ref_thread_default ();
|
|
|
|
|
if (current_context != client->main_context)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
schedule_call_in_idle (client, call_type);
|
|
|
|
|
else
|
|
|
|
|
actually_do_call (client, client->connection, client->name_owner, call_type);
|
2011-10-05 16:46:57 +02:00
|
|
|
|
g_main_context_unref (current_context);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
call_appeared_handler (Client *client)
|
|
|
|
|
{
|
|
|
|
|
if (client->previous_call != PREVIOUS_CALL_APPEARED)
|
|
|
|
|
{
|
|
|
|
|
client->previous_call = PREVIOUS_CALL_APPEARED;
|
|
|
|
|
if (!client->cancelled && client->name_appeared_handler != NULL)
|
|
|
|
|
{
|
|
|
|
|
do_call (client, CALL_TYPE_NAME_APPEARED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2020-01-20 20:12:49 +01:00
|
|
|
|
call_vanished_handler (Client *client)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
if (client->previous_call != PREVIOUS_CALL_VANISHED)
|
|
|
|
|
{
|
|
|
|
|
client->previous_call = PREVIOUS_CALL_VANISHED;
|
2020-01-20 20:12:49 +01:00
|
|
|
|
if (!client->cancelled && client->name_vanished_handler != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
do_call (client, CALL_TYPE_NAME_VANISHED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2017-02-06 09:41:10 +01:00
|
|
|
|
/* Return a reference to the #Client for @watcher_id, or %NULL if it’s been
|
|
|
|
|
* unwatched. This is safe to call from any thread. */
|
|
|
|
|
static Client *
|
|
|
|
|
dup_client (guint watcher_id)
|
|
|
|
|
{
|
|
|
|
|
Client *client;
|
|
|
|
|
|
|
|
|
|
G_LOCK (lock);
|
|
|
|
|
|
|
|
|
|
g_assert (watcher_id != 0);
|
|
|
|
|
g_assert (map_id_to_client != NULL);
|
|
|
|
|
|
|
|
|
|
client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id));
|
|
|
|
|
|
|
|
|
|
if (client != NULL)
|
|
|
|
|
client_ref (client);
|
|
|
|
|
|
|
|
|
|
G_UNLOCK (lock);
|
|
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Could be called from any thread, so it could be called after client_unref()
|
|
|
|
|
* has started finalising the #Client. Avoid that by looking up the #Client
|
|
|
|
|
* atomically. */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
on_connection_disconnected (GDBusConnection *connection,
|
|
|
|
|
gboolean remote_peer_vanished,
|
|
|
|
|
GError *error,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2017-02-06 09:41:10 +01:00
|
|
|
|
guint watcher_id = GPOINTER_TO_UINT (user_data);
|
|
|
|
|
Client *client = NULL;
|
|
|
|
|
|
|
|
|
|
client = dup_client (watcher_id);
|
|
|
|
|
if (client == NULL)
|
|
|
|
|
return;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
if (client->name_owner_changed_subscription_id > 0)
|
|
|
|
|
g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
|
|
|
|
|
if (client->disconnected_signal_handler_id > 0)
|
|
|
|
|
g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
|
|
|
|
|
g_object_unref (client->connection);
|
|
|
|
|
client->disconnected_signal_handler_id = 0;
|
|
|
|
|
client->name_owner_changed_subscription_id = 0;
|
|
|
|
|
client->connection = NULL;
|
|
|
|
|
|
2020-01-20 20:12:49 +01:00
|
|
|
|
call_vanished_handler (client);
|
2017-02-06 09:41:10 +01:00
|
|
|
|
|
|
|
|
|
client_unref (client);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2017-02-06 09:41:10 +01:00
|
|
|
|
/* Will always be called from the thread which acquired client->main_context. */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
on_name_owner_changed (GDBusConnection *connection,
|
|
|
|
|
const gchar *sender_name,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *signal_name,
|
|
|
|
|
GVariant *parameters,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2017-02-06 09:41:10 +01:00
|
|
|
|
guint watcher_id = GPOINTER_TO_UINT (user_data);
|
|
|
|
|
Client *client = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const gchar *name;
|
|
|
|
|
const gchar *old_owner;
|
|
|
|
|
const gchar *new_owner;
|
|
|
|
|
|
2017-02-06 09:41:10 +01:00
|
|
|
|
client = dup_client (watcher_id);
|
|
|
|
|
if (client == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (!client->initialized)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
|
|
|
|
|
g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
|
|
|
|
|
g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
g_variant_get (parameters,
|
2010-05-13 19:20:26 +02:00
|
|
|
|
"(&s&s&s)",
|
2010-05-06 20:13:59 +02:00
|
|
|
|
&name,
|
|
|
|
|
&old_owner,
|
|
|
|
|
&new_owner);
|
|
|
|
|
|
|
|
|
|
/* we only care about a specific name */
|
|
|
|
|
if (g_strcmp0 (name, client->name) != 0)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_free (client->name_owner);
|
|
|
|
|
client->name_owner = NULL;
|
2020-01-20 20:12:49 +01:00
|
|
|
|
call_vanished_handler (client);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (new_owner != NULL && strlen (new_owner) > 0)
|
|
|
|
|
{
|
|
|
|
|
g_warn_if_fail (client->name_owner == NULL);
|
|
|
|
|
g_free (client->name_owner);
|
|
|
|
|
client->name_owner = g_strdup (new_owner);
|
|
|
|
|
call_appeared_handler (client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out:
|
2017-02-06 09:41:10 +01:00
|
|
|
|
client_unref (client);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
get_name_owner_cb (GObject *source_object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
Client *client = user_data;
|
|
|
|
|
GVariant *result;
|
|
|
|
|
const char *name_owner;
|
|
|
|
|
|
|
|
|
|
name_owner = NULL;
|
|
|
|
|
result = NULL;
|
|
|
|
|
|
2010-05-10 17:47:08 +02:00
|
|
|
|
result = g_dbus_connection_call_finish (client->connection,
|
|
|
|
|
res,
|
|
|
|
|
NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (result != NULL)
|
|
|
|
|
{
|
2010-05-13 19:20:26 +02:00
|
|
|
|
g_variant_get (result, "(&s)", &name_owner);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (name_owner != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_warn_if_fail (client->name_owner == NULL);
|
|
|
|
|
client->name_owner = g_strdup (name_owner);
|
|
|
|
|
call_appeared_handler (client);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-20 20:12:49 +01:00
|
|
|
|
call_vanished_handler (client);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client->initialized = TRUE;
|
|
|
|
|
|
|
|
|
|
if (result != NULL)
|
|
|
|
|
g_variant_unref (result);
|
|
|
|
|
client_unref (client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
invoke_get_name_owner (Client *client)
|
|
|
|
|
{
|
2010-05-10 17:47:08 +02:00
|
|
|
|
g_dbus_connection_call (client->connection,
|
|
|
|
|
"org.freedesktop.DBus", /* bus name */
|
|
|
|
|
"/org/freedesktop/DBus", /* object path */
|
|
|
|
|
"org.freedesktop.DBus", /* interface name */
|
|
|
|
|
"GetNameOwner", /* method name */
|
|
|
|
|
g_variant_new ("(s)", client->name),
|
2010-05-24 22:46:24 +02:00
|
|
|
|
G_VARIANT_TYPE ("(s)"),
|
2010-05-10 17:47:08 +02:00
|
|
|
|
G_DBUS_CALL_FLAGS_NONE,
|
|
|
|
|
-1,
|
|
|
|
|
NULL,
|
|
|
|
|
(GAsyncReadyCallback) get_name_owner_cb,
|
|
|
|
|
client_ref (client));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
start_service_by_name_cb (GObject *source_object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
Client *client = user_data;
|
|
|
|
|
GVariant *result;
|
|
|
|
|
|
|
|
|
|
result = NULL;
|
|
|
|
|
|
2010-05-10 17:47:08 +02:00
|
|
|
|
result = g_dbus_connection_call_finish (client->connection,
|
|
|
|
|
res,
|
|
|
|
|
NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (result != NULL)
|
|
|
|
|
{
|
|
|
|
|
guint32 start_service_result;
|
|
|
|
|
g_variant_get (result, "(u)", &start_service_result);
|
|
|
|
|
|
|
|
|
|
if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
|
|
|
|
|
{
|
|
|
|
|
invoke_get_name_owner (client);
|
|
|
|
|
}
|
|
|
|
|
else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
|
|
|
|
|
{
|
|
|
|
|
invoke_get_name_owner (client);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
|
2020-01-20 20:12:49 +01:00
|
|
|
|
call_vanished_handler (client);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
client->initialized = TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* Errors are not unexpected; the bus will reply e.g.
|
|
|
|
|
*
|
|
|
|
|
* org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
|
|
|
|
|
* was not provided by any .service files
|
|
|
|
|
*
|
2010-05-13 05:12:14 +02:00
|
|
|
|
* This doesn't mean that the name doesn't have an owner, just
|
|
|
|
|
* that it's not provided by a .service file. So proceed to
|
|
|
|
|
* invoke GetNameOwner().
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
2010-05-13 05:12:14 +02:00
|
|
|
|
invoke_get_name_owner (client);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result != NULL)
|
|
|
|
|
g_variant_unref (result);
|
|
|
|
|
client_unref (client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
has_connection (Client *client)
|
|
|
|
|
{
|
|
|
|
|
/* listen for disconnection */
|
|
|
|
|
client->disconnected_signal_handler_id = g_signal_connect (client->connection,
|
|
|
|
|
"closed",
|
|
|
|
|
G_CALLBACK (on_connection_disconnected),
|
2017-02-06 09:41:10 +01:00
|
|
|
|
GUINT_TO_POINTER (client->id));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* start listening to NameOwnerChanged messages immediately */
|
|
|
|
|
client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
|
|
|
|
|
"org.freedesktop.DBus", /* name */
|
|
|
|
|
"org.freedesktop.DBus", /* if */
|
|
|
|
|
"NameOwnerChanged", /* signal */
|
|
|
|
|
"/org/freedesktop/DBus", /* path */
|
|
|
|
|
client->name,
|
2010-07-19 21:03:00 +02:00
|
|
|
|
G_DBUS_SIGNAL_FLAGS_NONE,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
on_name_owner_changed,
|
2017-02-06 09:41:10 +01:00
|
|
|
|
GUINT_TO_POINTER (client->id),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
|
|
|
|
|
{
|
2010-05-10 17:47:08 +02:00
|
|
|
|
g_dbus_connection_call (client->connection,
|
|
|
|
|
"org.freedesktop.DBus", /* bus name */
|
|
|
|
|
"/org/freedesktop/DBus", /* object path */
|
|
|
|
|
"org.freedesktop.DBus", /* interface name */
|
|
|
|
|
"StartServiceByName", /* method name */
|
|
|
|
|
g_variant_new ("(su)", client->name, 0),
|
2010-05-24 22:46:24 +02:00
|
|
|
|
G_VARIANT_TYPE ("(u)"),
|
2010-05-10 17:47:08 +02:00
|
|
|
|
G_DBUS_CALL_FLAGS_NONE,
|
|
|
|
|
-1,
|
|
|
|
|
NULL,
|
|
|
|
|
(GAsyncReadyCallback) start_service_by_name_cb,
|
|
|
|
|
client_ref (client));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* check owner */
|
|
|
|
|
invoke_get_name_owner (client);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
connection_get_cb (GObject *source_object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
Client *client = user_data;
|
|
|
|
|
|
|
|
|
|
client->connection = g_bus_get_finish (res, NULL);
|
|
|
|
|
if (client->connection == NULL)
|
|
|
|
|
{
|
2020-01-20 20:12:49 +01:00
|
|
|
|
call_vanished_handler (client);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
has_connection (client);
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
client_unref (client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_bus_watch_name:
|
|
|
|
|
* @bus_type: The type of bus to watch a name on.
|
|
|
|
|
* @name: The name (well-known or unique) to watch.
|
|
|
|
|
* @flags: Flags from the #GBusNameWatcherFlags enumeration.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @name_appeared_handler: (nullable): Handler to invoke when @name is known to exist or %NULL.
|
|
|
|
|
* @name_vanished_handler: (nullable): Handler to invoke when @name is known to not exist or %NULL.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* @user_data: User data to pass to handlers.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @user_data_free_func: (nullable): Function for freeing @user_data or %NULL.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Starts watching @name on the bus specified by @bus_type and calls
|
|
|
|
|
* @name_appeared_handler and @name_vanished_handler when the name is
|
2019-08-23 23:45:16 +02:00
|
|
|
|
* known to have an owner respectively known to lose its
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* owner. Callbacks will be invoked in the
|
|
|
|
|
* [thread-default main context][g-main-context-push-thread-default]
|
|
|
|
|
* of the thread you are calling this function from.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* You are guaranteed that one of the handlers will be invoked after
|
|
|
|
|
* calling this function. When you are done watching the name, just
|
|
|
|
|
* call g_bus_unwatch_name() with the watcher id this function
|
|
|
|
|
* returns.
|
|
|
|
|
*
|
|
|
|
|
* If the name vanishes or appears (for example the application owning
|
|
|
|
|
* the name could restart), the handlers are also invoked. If the
|
|
|
|
|
* #GDBusConnection that is used for watching the name disconnects, then
|
|
|
|
|
* @name_vanished_handler is invoked since it is no longer
|
|
|
|
|
* possible to access the name.
|
|
|
|
|
*
|
|
|
|
|
* Another guarantee is that invocations of @name_appeared_handler
|
|
|
|
|
* and @name_vanished_handler are guaranteed to alternate; that
|
|
|
|
|
* is, if @name_appeared_handler is invoked then you are
|
|
|
|
|
* guaranteed that the next time one of the handlers is invoked, it
|
|
|
|
|
* will be @name_vanished_handler. The reverse is also true.
|
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* This behavior makes it very simple to write applications that want
|
|
|
|
|
* to take action when a certain [name exists][gdbus-watching-names].
|
|
|
|
|
* Basically, the application should create object proxies in
|
|
|
|
|
* @name_appeared_handler and destroy them again (if any) in
|
|
|
|
|
* @name_vanished_handler.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2019-08-23 23:45:16 +02:00
|
|
|
|
* Returns: An identifier (never 0) that can be used with
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* g_bus_unwatch_name() to stop watching the name.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
guint
|
|
|
|
|
g_bus_watch_name (GBusType bus_type,
|
|
|
|
|
const gchar *name,
|
|
|
|
|
GBusNameWatcherFlags flags,
|
|
|
|
|
GBusNameAppearedCallback name_appeared_handler,
|
|
|
|
|
GBusNameVanishedCallback name_vanished_handler,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GDestroyNotify user_data_free_func)
|
|
|
|
|
{
|
|
|
|
|
Client *client;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (g_dbus_is_name (name), 0);
|
|
|
|
|
|
|
|
|
|
G_LOCK (lock);
|
|
|
|
|
|
|
|
|
|
client = g_new0 (Client, 1);
|
|
|
|
|
client->ref_count = 1;
|
2019-09-21 10:45:36 +02:00
|
|
|
|
client->id = (guint) g_atomic_int_add (&next_global_id, 1); /* TODO: uh oh, handle overflow */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
client->name = g_strdup (name);
|
|
|
|
|
client->flags = flags;
|
|
|
|
|
client->name_appeared_handler = name_appeared_handler;
|
|
|
|
|
client->name_vanished_handler = name_vanished_handler;
|
|
|
|
|
client->user_data = user_data;
|
|
|
|
|
client->user_data_free_func = user_data_free_func;
|
2011-10-05 16:46:57 +02:00
|
|
|
|
client->main_context = g_main_context_ref_thread_default ();
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
if (map_id_to_client == NULL)
|
|
|
|
|
{
|
|
|
|
|
map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
|
|
|
}
|
|
|
|
|
g_hash_table_insert (map_id_to_client,
|
|
|
|
|
GUINT_TO_POINTER (client->id),
|
|
|
|
|
client);
|
|
|
|
|
|
|
|
|
|
g_bus_get (bus_type,
|
|
|
|
|
NULL,
|
|
|
|
|
connection_get_cb,
|
|
|
|
|
client_ref (client));
|
|
|
|
|
|
|
|
|
|
G_UNLOCK (lock);
|
|
|
|
|
|
|
|
|
|
return client->id;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-13 05:12:14 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_bus_watch_name_on_connection:
|
2010-06-29 21:19:56 +02:00
|
|
|
|
* @connection: A #GDBusConnection.
|
2010-05-13 05:12:14 +02:00
|
|
|
|
* @name: The name (well-known or unique) to watch.
|
|
|
|
|
* @flags: Flags from the #GBusNameWatcherFlags enumeration.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @name_appeared_handler: (nullable): Handler to invoke when @name is known to exist or %NULL.
|
|
|
|
|
* @name_vanished_handler: (nullable): Handler to invoke when @name is known to not exist or %NULL.
|
2010-05-13 05:12:14 +02:00
|
|
|
|
* @user_data: User data to pass to handlers.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @user_data_free_func: (nullable): Function for freeing @user_data or %NULL.
|
2010-05-13 05:12:14 +02:00
|
|
|
|
*
|
|
|
|
|
* Like g_bus_watch_name() but takes a #GDBusConnection instead of a
|
|
|
|
|
* #GBusType.
|
|
|
|
|
*
|
2019-08-23 23:45:16 +02:00
|
|
|
|
* Returns: An identifier (never 0) that can be used with
|
2010-05-13 05:12:14 +02:00
|
|
|
|
* g_bus_unwatch_name() to stop watching the name.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
|
|
|
|
guint g_bus_watch_name_on_connection (GDBusConnection *connection,
|
|
|
|
|
const gchar *name,
|
|
|
|
|
GBusNameWatcherFlags flags,
|
|
|
|
|
GBusNameAppearedCallback name_appeared_handler,
|
|
|
|
|
GBusNameVanishedCallback name_vanished_handler,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GDestroyNotify user_data_free_func)
|
|
|
|
|
{
|
|
|
|
|
Client *client;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
|
|
|
|
|
g_return_val_if_fail (g_dbus_is_name (name), 0);
|
|
|
|
|
|
|
|
|
|
G_LOCK (lock);
|
|
|
|
|
|
|
|
|
|
client = g_new0 (Client, 1);
|
|
|
|
|
client->ref_count = 1;
|
2019-09-21 10:45:36 +02:00
|
|
|
|
client->id = (guint) g_atomic_int_add (&next_global_id, 1); /* TODO: uh oh, handle overflow */
|
2010-05-13 05:12:14 +02:00
|
|
|
|
client->name = g_strdup (name);
|
|
|
|
|
client->flags = flags;
|
|
|
|
|
client->name_appeared_handler = name_appeared_handler;
|
|
|
|
|
client->name_vanished_handler = name_vanished_handler;
|
|
|
|
|
client->user_data = user_data;
|
|
|
|
|
client->user_data_free_func = user_data_free_func;
|
2011-10-05 16:46:57 +02:00
|
|
|
|
client->main_context = g_main_context_ref_thread_default ();
|
2010-05-13 05:12:14 +02:00
|
|
|
|
|
|
|
|
|
if (map_id_to_client == NULL)
|
2010-08-07 22:27:38 +02:00
|
|
|
|
map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
|
|
|
|
2010-05-13 05:12:14 +02:00
|
|
|
|
g_hash_table_insert (map_id_to_client,
|
|
|
|
|
GUINT_TO_POINTER (client->id),
|
|
|
|
|
client);
|
|
|
|
|
|
|
|
|
|
client->connection = g_object_ref (connection);
|
|
|
|
|
G_UNLOCK (lock);
|
|
|
|
|
|
|
|
|
|
has_connection (client);
|
|
|
|
|
|
|
|
|
|
return client->id;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-10 18:29:23 +02:00
|
|
|
|
typedef struct {
|
|
|
|
|
GClosure *name_appeared_closure;
|
|
|
|
|
GClosure *name_vanished_closure;
|
|
|
|
|
} WatchNameData;
|
|
|
|
|
|
2010-08-07 22:27:38 +02:00
|
|
|
|
static WatchNameData *
|
|
|
|
|
watch_name_data_new (GClosure *name_appeared_closure,
|
|
|
|
|
GClosure *name_vanished_closure)
|
|
|
|
|
{
|
|
|
|
|
WatchNameData *data;
|
|
|
|
|
|
|
|
|
|
data = g_new0 (WatchNameData, 1);
|
|
|
|
|
|
|
|
|
|
if (name_appeared_closure != NULL)
|
|
|
|
|
{
|
|
|
|
|
data->name_appeared_closure = g_closure_ref (name_appeared_closure);
|
|
|
|
|
g_closure_sink (name_appeared_closure);
|
|
|
|
|
if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure))
|
2011-06-20 15:06:07 +02:00
|
|
|
|
g_closure_set_marshal (name_appeared_closure, g_cclosure_marshal_generic);
|
2010-08-07 22:27:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (name_vanished_closure != NULL)
|
|
|
|
|
{
|
|
|
|
|
data->name_vanished_closure = g_closure_ref (name_vanished_closure);
|
|
|
|
|
g_closure_sink (name_vanished_closure);
|
|
|
|
|
if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure))
|
2011-06-20 15:06:07 +02:00
|
|
|
|
g_closure_set_marshal (name_vanished_closure, g_cclosure_marshal_generic);
|
2010-08-07 22:27:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-10 18:29:23 +02:00
|
|
|
|
static void
|
|
|
|
|
watch_with_closures_on_name_appeared (GDBusConnection *connection,
|
|
|
|
|
const gchar *name,
|
|
|
|
|
const gchar *name_owner,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
WatchNameData *data = user_data;
|
2011-09-30 18:08:15 +02:00
|
|
|
|
GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
|
2010-06-10 18:29:23 +02:00
|
|
|
|
|
|
|
|
|
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], name);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[2], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[2], name_owner);
|
|
|
|
|
|
|
|
|
|
g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
|
2010-08-07 22:27:38 +02:00
|
|
|
|
|
|
|
|
|
g_value_unset (params + 0);
|
|
|
|
|
g_value_unset (params + 1);
|
|
|
|
|
g_value_unset (params + 2);
|
2010-06-10 18:29:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
watch_with_closures_on_name_vanished (GDBusConnection *connection,
|
|
|
|
|
const gchar *name,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
WatchNameData *data = user_data;
|
2011-09-30 18:08:15 +02:00
|
|
|
|
GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
|
2010-06-10 18:29:23 +02:00
|
|
|
|
|
|
|
|
|
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], name);
|
|
|
|
|
|
|
|
|
|
g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
|
2010-08-07 22:27:38 +02:00
|
|
|
|
|
|
|
|
|
g_value_unset (params + 0);
|
|
|
|
|
g_value_unset (params + 1);
|
2010-06-10 18:29:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
bus_watch_name_free_func (gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
WatchNameData *data = user_data;
|
|
|
|
|
|
|
|
|
|
if (data->name_appeared_closure != NULL)
|
|
|
|
|
g_closure_unref (data->name_appeared_closure);
|
|
|
|
|
|
|
|
|
|
if (data->name_vanished_closure != NULL)
|
|
|
|
|
g_closure_unref (data->name_vanished_closure);
|
|
|
|
|
|
|
|
|
|
g_free (data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-12 21:53:18 +01:00
|
|
|
|
* g_bus_watch_name_with_closures: (rename-to g_bus_watch_name)
|
2010-06-10 18:29:23 +02:00
|
|
|
|
* @bus_type: The type of bus to watch a name on.
|
|
|
|
|
* @name: The name (well-known or unique) to watch.
|
|
|
|
|
* @flags: Flags from the #GBusNameWatcherFlags enumeration.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @name_appeared_closure: (nullable): #GClosure to invoke when @name is known
|
2010-06-10 18:29:23 +02:00
|
|
|
|
* to exist or %NULL.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @name_vanished_closure: (nullable): #GClosure to invoke when @name is known
|
2010-06-10 18:29:23 +02:00
|
|
|
|
* to not exist or %NULL.
|
|
|
|
|
*
|
|
|
|
|
* Version of g_bus_watch_name() using closures instead of callbacks for
|
|
|
|
|
* easier binding in other languages.
|
|
|
|
|
*
|
2019-08-23 23:45:16 +02:00
|
|
|
|
* Returns: An identifier (never 0) that can be used with
|
2010-06-10 18:29:23 +02:00
|
|
|
|
* g_bus_unwatch_name() to stop watching the name.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
|
|
|
|
guint
|
|
|
|
|
g_bus_watch_name_with_closures (GBusType bus_type,
|
|
|
|
|
const gchar *name,
|
|
|
|
|
GBusNameWatcherFlags flags,
|
|
|
|
|
GClosure *name_appeared_closure,
|
|
|
|
|
GClosure *name_vanished_closure)
|
|
|
|
|
{
|
|
|
|
|
return g_bus_watch_name (bus_type,
|
|
|
|
|
name,
|
|
|
|
|
flags,
|
|
|
|
|
name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
|
|
|
|
|
name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
|
2010-08-07 22:27:38 +02:00
|
|
|
|
watch_name_data_new (name_appeared_closure, name_vanished_closure),
|
2010-06-10 18:29:23 +02:00
|
|
|
|
bus_watch_name_free_func);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-12 21:53:18 +01:00
|
|
|
|
* g_bus_watch_name_on_connection_with_closures: (rename-to g_bus_watch_name_on_connection)
|
2010-06-29 21:19:56 +02:00
|
|
|
|
* @connection: A #GDBusConnection.
|
2010-06-10 18:29:23 +02:00
|
|
|
|
* @name: The name (well-known or unique) to watch.
|
|
|
|
|
* @flags: Flags from the #GBusNameWatcherFlags enumeration.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @name_appeared_closure: (nullable): #GClosure to invoke when @name is known
|
2010-06-10 18:29:23 +02:00
|
|
|
|
* to exist or %NULL.
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @name_vanished_closure: (nullable): #GClosure to invoke when @name is known
|
2010-06-10 18:29:23 +02:00
|
|
|
|
* to not exist or %NULL.
|
|
|
|
|
*
|
|
|
|
|
* Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
|
|
|
|
|
* easier binding in other languages.
|
|
|
|
|
*
|
2019-08-23 23:45:16 +02:00
|
|
|
|
* Returns: An identifier (never 0) that can be used with
|
2010-06-10 18:29:23 +02:00
|
|
|
|
* g_bus_unwatch_name() to stop watching the name.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
|
|
|
|
guint g_bus_watch_name_on_connection_with_closures (
|
|
|
|
|
GDBusConnection *connection,
|
|
|
|
|
const gchar *name,
|
|
|
|
|
GBusNameWatcherFlags flags,
|
|
|
|
|
GClosure *name_appeared_closure,
|
|
|
|
|
GClosure *name_vanished_closure)
|
|
|
|
|
{
|
|
|
|
|
return g_bus_watch_name_on_connection (connection,
|
|
|
|
|
name,
|
|
|
|
|
flags,
|
|
|
|
|
name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
|
|
|
|
|
name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
|
2010-08-07 22:27:38 +02:00
|
|
|
|
watch_name_data_new (name_appeared_closure, name_vanished_closure),
|
2010-06-10 18:29:23 +02:00
|
|
|
|
bus_watch_name_free_func);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_bus_unwatch_name:
|
|
|
|
|
* @watcher_id: An identifier obtained from g_bus_watch_name()
|
|
|
|
|
*
|
|
|
|
|
* Stops watching a name.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
2020-02-21 16:12:08 +01:00
|
|
|
|
* Note that there may still be D-Bus traffic to process (relating to watching
|
|
|
|
|
* and unwatching the name) in the current thread-default #GMainContext after
|
|
|
|
|
* this function has returned. You should continue to iterate the #GMainContext
|
|
|
|
|
* until the #GDestroyNotify function passed to g_bus_watch_name() is called, in
|
|
|
|
|
* order to avoid memory leaks through callbacks queued on the #GMainContext
|
|
|
|
|
* after it’s stopped being iterated.
|
|
|
|
|
*
|
2010-05-06 22:02:08 +02:00
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
void
|
|
|
|
|
g_bus_unwatch_name (guint watcher_id)
|
|
|
|
|
{
|
|
|
|
|
Client *client;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (watcher_id > 0);
|
|
|
|
|
|
|
|
|
|
client = NULL;
|
|
|
|
|
|
|
|
|
|
G_LOCK (lock);
|
|
|
|
|
if (watcher_id == 0 ||
|
|
|
|
|
map_id_to_client == NULL ||
|
|
|
|
|
(client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
|
|
|
|
|
{
|
|
|
|
|
g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client->cancelled = TRUE;
|
|
|
|
|
g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
G_UNLOCK (lock);
|
|
|
|
|
|
|
|
|
|
/* do callback without holding lock */
|
|
|
|
|
if (client != NULL)
|
|
|
|
|
{
|
|
|
|
|
client_unref (client);
|
|
|
|
|
}
|
|
|
|
|
}
|