gresourcefile: simplify path canonicalization
[glib.git] / gio / gdbusnameowning.c
blob2c2714db22d89831d3d001df34d1cd45ac8ed61a
1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include "config.h"
23 #include <stdlib.h>
25 #include "gdbusutils.h"
26 #include "gdbusnameowning.h"
27 #include "gdbuserror.h"
28 #include "gdbusprivate.h"
29 #include "gdbusconnection.h"
31 #include "glibintl.h"
33 /**
34 * SECTION:gdbusnameowning
35 * @title: Owning Bus Names
36 * @short_description: Simple API for owning bus names
37 * @include: gio/gio.h
39 * Convenience API for owning bus names.
41 * A simple example for owning a name can be found in
42 * [gdbus-example-own-name.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-own-name.c)
45 G_LOCK_DEFINE_STATIC (lock);
47 /* ---------------------------------------------------------------------------------------------------- */
49 typedef enum
51 PREVIOUS_CALL_NONE = 0,
52 PREVIOUS_CALL_ACQUIRED,
53 PREVIOUS_CALL_LOST,
54 } PreviousCall;
56 typedef struct
58 volatile gint ref_count;
59 guint id;
60 GBusNameOwnerFlags flags;
61 gchar *name;
62 GBusAcquiredCallback bus_acquired_handler;
63 GBusNameAcquiredCallback name_acquired_handler;
64 GBusNameLostCallback name_lost_handler;
65 gpointer user_data;
66 GDestroyNotify user_data_free_func;
67 GMainContext *main_context;
69 PreviousCall previous_call;
71 GDBusConnection *connection;
72 gulong disconnected_signal_handler_id;
73 guint name_acquired_subscription_id;
74 guint name_lost_subscription_id;
76 volatile gboolean cancelled; /* must hold lock when reading or modifying */
78 gboolean needs_release;
79 } Client;
81 static guint next_global_id = 1;
82 static GHashTable *map_id_to_client = NULL;
85 static Client *
86 client_ref (Client *client)
88 g_atomic_int_inc (&client->ref_count);
89 return client;
92 static void
93 client_unref (Client *client)
95 if (g_atomic_int_dec_and_test (&client->ref_count))
97 if (client->connection != NULL)
99 if (client->disconnected_signal_handler_id > 0)
100 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
101 if (client->name_acquired_subscription_id > 0)
102 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
103 if (client->name_lost_subscription_id > 0)
104 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
105 g_object_unref (client->connection);
107 g_main_context_unref (client->main_context);
108 g_free (client->name);
109 if (client->user_data_free_func != NULL)
110 client->user_data_free_func (client->user_data);
111 g_free (client);
115 /* ---------------------------------------------------------------------------------------------------- */
118 typedef enum
120 CALL_TYPE_NAME_ACQUIRED,
121 CALL_TYPE_NAME_LOST
122 } CallType;
124 typedef struct
126 Client *client;
128 /* keep this separate because client->connection may
129 * be set to NULL after scheduling the call
131 GDBusConnection *connection;
133 /* set to TRUE to call acquired */
134 CallType call_type;
135 } CallHandlerData;
137 static void
138 call_handler_data_free (CallHandlerData *data)
140 if (data->connection != NULL)
141 g_object_unref (data->connection);
142 client_unref (data->client);
143 g_free (data);
146 static void
147 actually_do_call (Client *client, GDBusConnection *connection, CallType call_type)
149 switch (call_type)
151 case CALL_TYPE_NAME_ACQUIRED:
152 if (client->name_acquired_handler != NULL)
154 client->name_acquired_handler (connection,
155 client->name,
156 client->user_data);
158 break;
160 case CALL_TYPE_NAME_LOST:
161 if (client->name_lost_handler != NULL)
163 client->name_lost_handler (connection,
164 client->name,
165 client->user_data);
167 break;
169 default:
170 g_assert_not_reached ();
171 break;
175 static gboolean
176 call_in_idle_cb (gpointer _data)
178 CallHandlerData *data = _data;
179 actually_do_call (data->client, data->connection, data->call_type);
180 return FALSE;
183 static void
184 schedule_call_in_idle (Client *client, CallType call_type)
186 CallHandlerData *data;
187 GSource *idle_source;
189 data = g_new0 (CallHandlerData, 1);
190 data->client = client_ref (client);
191 data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
192 data->call_type = call_type;
194 idle_source = g_idle_source_new ();
195 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
196 g_source_set_callback (idle_source,
197 call_in_idle_cb,
198 data,
199 (GDestroyNotify) call_handler_data_free);
200 g_source_set_name (idle_source, "[gio, gdbusnameowning.c] call_in_idle_cb");
201 g_source_attach (idle_source, client->main_context);
202 g_source_unref (idle_source);
205 static void
206 do_call (Client *client, CallType call_type)
208 GMainContext *current_context;
210 /* only schedule in idle if we're not in the right thread */
211 current_context = g_main_context_ref_thread_default ();
212 if (current_context != client->main_context)
213 schedule_call_in_idle (client, call_type);
214 else
215 actually_do_call (client, client->connection, call_type);
216 g_main_context_unref (current_context);
219 static void
220 call_acquired_handler (Client *client)
222 G_LOCK (lock);
223 if (client->previous_call != PREVIOUS_CALL_ACQUIRED)
225 client->previous_call = PREVIOUS_CALL_ACQUIRED;
226 if (!client->cancelled)
228 G_UNLOCK (lock);
229 do_call (client, CALL_TYPE_NAME_ACQUIRED);
230 goto out;
233 G_UNLOCK (lock);
234 out:
238 static void
239 call_lost_handler (Client *client)
241 G_LOCK (lock);
242 if (client->previous_call != PREVIOUS_CALL_LOST)
244 client->previous_call = PREVIOUS_CALL_LOST;
245 if (!client->cancelled)
247 G_UNLOCK (lock);
248 do_call (client, CALL_TYPE_NAME_LOST);
249 goto out;
252 G_UNLOCK (lock);
253 out:
257 /* ---------------------------------------------------------------------------------------------------- */
259 static void
260 on_name_lost_or_acquired (GDBusConnection *connection,
261 const gchar *sender_name,
262 const gchar *object_path,
263 const gchar *interface_name,
264 const gchar *signal_name,
265 GVariant *parameters,
266 gpointer user_data)
268 Client *client = user_data;
269 const gchar *name;
271 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
272 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
273 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
274 goto out;
276 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
278 g_warning ("%s signal had unexpected signature %s", signal_name,
279 g_variant_get_type_string (parameters));
280 goto out;
283 if (g_strcmp0 (signal_name, "NameLost") == 0)
285 g_variant_get (parameters, "(&s)", &name);
286 if (g_strcmp0 (name, client->name) == 0)
288 call_lost_handler (client);
291 else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
293 g_variant_get (parameters, "(&s)", &name);
294 if (g_strcmp0 (name, client->name) == 0)
296 call_acquired_handler (client);
299 out:
303 /* ---------------------------------------------------------------------------------------------------- */
305 static void
306 request_name_cb (GObject *source_object,
307 GAsyncResult *res,
308 gpointer user_data)
310 Client *client = user_data;
311 GVariant *result;
312 guint32 request_name_reply;
313 gboolean subscribe;
315 request_name_reply = 0;
316 result = NULL;
318 /* don't use client->connection - it may be NULL already */
319 result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
320 res,
321 NULL);
322 if (result != NULL)
324 g_variant_get (result, "(u)", &request_name_reply);
325 g_variant_unref (result);
328 subscribe = FALSE;
330 switch (request_name_reply)
332 case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
333 /* We got the name - now listen for NameLost and NameAcquired */
334 call_acquired_handler (client);
335 subscribe = TRUE;
336 client->needs_release = TRUE;
337 break;
339 case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
340 /* Waiting in line - listen for NameLost and NameAcquired */
341 call_lost_handler (client);
342 subscribe = TRUE;
343 client->needs_release = TRUE;
344 break;
346 default:
347 /* assume we couldn't get the name - explicit fallthrough */
348 case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
349 case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
350 /* Some other part of the process is already owning the name */
351 call_lost_handler (client);
352 break;
356 if (subscribe)
358 GDBusConnection *connection = NULL;
360 /* if cancelled, there is no point in subscribing to signals - if not, make sure
361 * we use a known good Connection object since it may be set to NULL at any point
362 * after being cancelled
364 G_LOCK (lock);
365 if (!client->cancelled)
366 connection = g_object_ref (client->connection);
367 G_UNLOCK (lock);
369 /* start listening to NameLost and NameAcquired messages */
370 if (connection != NULL)
372 client->name_lost_subscription_id =
373 g_dbus_connection_signal_subscribe (connection,
374 "org.freedesktop.DBus",
375 "org.freedesktop.DBus",
376 "NameLost",
377 "/org/freedesktop/DBus",
378 client->name,
379 G_DBUS_SIGNAL_FLAGS_NONE,
380 on_name_lost_or_acquired,
381 client,
382 NULL);
383 client->name_acquired_subscription_id =
384 g_dbus_connection_signal_subscribe (connection,
385 "org.freedesktop.DBus",
386 "org.freedesktop.DBus",
387 "NameAcquired",
388 "/org/freedesktop/DBus",
389 client->name,
390 G_DBUS_SIGNAL_FLAGS_NONE,
391 on_name_lost_or_acquired,
392 client,
393 NULL);
394 g_object_unref (connection);
398 client_unref (client);
401 /* ---------------------------------------------------------------------------------------------------- */
403 static void
404 on_connection_disconnected (GDBusConnection *connection,
405 gboolean remote_peer_vanished,
406 GError *error,
407 gpointer user_data)
409 Client *client = user_data;
411 if (client->disconnected_signal_handler_id > 0)
412 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
413 if (client->name_acquired_subscription_id > 0)
414 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
415 if (client->name_lost_subscription_id > 0)
416 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
417 g_object_unref (client->connection);
418 client->disconnected_signal_handler_id = 0;
419 client->name_acquired_subscription_id = 0;
420 client->name_lost_subscription_id = 0;
421 client->connection = NULL;
423 call_lost_handler (client);
426 /* ---------------------------------------------------------------------------------------------------- */
428 static void
429 has_connection (Client *client)
431 /* listen for disconnection */
432 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
433 "closed",
434 G_CALLBACK (on_connection_disconnected),
435 client);
437 /* attempt to acquire the name */
438 g_dbus_connection_call (client->connection,
439 "org.freedesktop.DBus", /* bus name */
440 "/org/freedesktop/DBus", /* object path */
441 "org.freedesktop.DBus", /* interface name */
442 "RequestName", /* method name */
443 g_variant_new ("(su)",
444 client->name,
445 client->flags),
446 G_VARIANT_TYPE ("(u)"),
447 G_DBUS_CALL_FLAGS_NONE,
449 NULL,
450 (GAsyncReadyCallback) request_name_cb,
451 client_ref (client));
455 static void
456 connection_get_cb (GObject *source_object,
457 GAsyncResult *res,
458 gpointer user_data)
460 Client *client = user_data;
462 /* must not do anything if already cancelled */
463 G_LOCK (lock);
464 if (client->cancelled)
466 G_UNLOCK (lock);
467 goto out;
469 G_UNLOCK (lock);
471 client->connection = g_bus_get_finish (res, NULL);
472 if (client->connection == NULL)
474 call_lost_handler (client);
475 goto out;
478 /* No need to schedule this in idle as we're already in the thread
479 * that the user called g_bus_own_name() from. This is because
480 * g_bus_get() guarantees that.
482 * Also, we need to ensure that the handler is invoked *before*
483 * we call RequestName(). Otherwise there is a race.
485 if (client->bus_acquired_handler != NULL)
487 client->bus_acquired_handler (client->connection,
488 client->name,
489 client->user_data);
492 has_connection (client);
494 out:
495 client_unref (client);
498 /* ---------------------------------------------------------------------------------------------------- */
501 * g_bus_own_name_on_connection:
502 * @connection: a #GDBusConnection
503 * @name: the well-known name to own
504 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
505 * @name_acquired_handler: (nullable): handler to invoke when @name is acquired or %NULL
506 * @name_lost_handler: (nullable): handler to invoke when @name is lost or %NULL
507 * @user_data: user data to pass to handlers
508 * @user_data_free_func: (nullable): function for freeing @user_data or %NULL
510 * Like g_bus_own_name() but takes a #GDBusConnection instead of a
511 * #GBusType.
513 * Returns: an identifier (never 0) that an be used with
514 * g_bus_unown_name() to stop owning the name
516 * Since: 2.26
518 guint
519 g_bus_own_name_on_connection (GDBusConnection *connection,
520 const gchar *name,
521 GBusNameOwnerFlags flags,
522 GBusNameAcquiredCallback name_acquired_handler,
523 GBusNameLostCallback name_lost_handler,
524 gpointer user_data,
525 GDestroyNotify user_data_free_func)
527 Client *client;
529 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
530 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
532 G_LOCK (lock);
534 client = g_new0 (Client, 1);
535 client->ref_count = 1;
536 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
537 client->name = g_strdup (name);
538 client->flags = flags;
539 client->name_acquired_handler = name_acquired_handler;
540 client->name_lost_handler = name_lost_handler;
541 client->user_data = user_data;
542 client->user_data_free_func = user_data_free_func;
543 client->main_context = g_main_context_ref_thread_default ();
545 client->connection = g_object_ref (connection);
547 if (map_id_to_client == NULL)
549 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
551 g_hash_table_insert (map_id_to_client,
552 GUINT_TO_POINTER (client->id),
553 client);
555 G_UNLOCK (lock);
557 has_connection (client);
559 return client->id;
563 * g_bus_own_name:
564 * @bus_type: the type of bus to own a name on
565 * @name: the well-known name to own
566 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
567 * @bus_acquired_handler: (nullable): handler to invoke when connected to the bus of type @bus_type or %NULL
568 * @name_acquired_handler: (nullable): handler to invoke when @name is acquired or %NULL
569 * @name_lost_handler: (nullable): handler to invoke when @name is lost or %NULL
570 * @user_data: user data to pass to handlers
571 * @user_data_free_func: (nullable): function for freeing @user_data or %NULL
573 * Starts acquiring @name on the bus specified by @bus_type and calls
574 * @name_acquired_handler and @name_lost_handler when the name is
575 * acquired respectively lost. Callbacks will be invoked in the
576 * [thread-default main context][g-main-context-push-thread-default]
577 * of the thread you are calling this function from.
579 * You are guaranteed that one of the @name_acquired_handler and @name_lost_handler
580 * callbacks will be invoked after calling this function - there are three
581 * possible cases:
583 * - @name_lost_handler with a %NULL connection (if a connection to the bus
584 * can't be made).
586 * - @bus_acquired_handler then @name_lost_handler (if the name can't be
587 * obtained)
589 * - @bus_acquired_handler then @name_acquired_handler (if the name was
590 * obtained).
592 * When you are done owning the name, just call g_bus_unown_name()
593 * with the owner id this function returns.
595 * If the name is acquired or lost (for example another application
596 * could acquire the name if you allow replacement or the application
597 * currently owning the name exits), the handlers are also invoked.
598 * If the #GDBusConnection that is used for attempting to own the name
599 * closes, then @name_lost_handler is invoked since it is no longer
600 * possible for other processes to access the process.
602 * You cannot use g_bus_own_name() several times for the same name (unless
603 * interleaved with calls to g_bus_unown_name()) - only the first call
604 * will work.
606 * Another guarantee is that invocations of @name_acquired_handler
607 * and @name_lost_handler are guaranteed to alternate; that
608 * is, if @name_acquired_handler is invoked then you are
609 * guaranteed that the next time one of the handlers is invoked, it
610 * will be @name_lost_handler. The reverse is also true.
612 * If you plan on exporting objects (using e.g.
613 * g_dbus_connection_register_object()), note that it is generally too late
614 * to export the objects in @name_acquired_handler. Instead, you can do this
615 * in @bus_acquired_handler since you are guaranteed that this will run
616 * before @name is requested from the bus.
618 * This behavior makes it very simple to write applications that wants
619 * to [own names][gdbus-owning-names] and export objects.
620 * Simply register objects to be exported in @bus_acquired_handler and
621 * unregister the objects (if any) in @name_lost_handler.
623 * Returns: an identifier (never 0) that an be used with
624 * g_bus_unown_name() to stop owning the name.
626 * Since: 2.26
628 guint
629 g_bus_own_name (GBusType bus_type,
630 const gchar *name,
631 GBusNameOwnerFlags flags,
632 GBusAcquiredCallback bus_acquired_handler,
633 GBusNameAcquiredCallback name_acquired_handler,
634 GBusNameLostCallback name_lost_handler,
635 gpointer user_data,
636 GDestroyNotify user_data_free_func)
638 Client *client;
640 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
642 G_LOCK (lock);
644 client = g_new0 (Client, 1);
645 client->ref_count = 1;
646 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
647 client->name = g_strdup (name);
648 client->flags = flags;
649 client->bus_acquired_handler = bus_acquired_handler;
650 client->name_acquired_handler = name_acquired_handler;
651 client->name_lost_handler = name_lost_handler;
652 client->user_data = user_data;
653 client->user_data_free_func = user_data_free_func;
654 client->main_context = g_main_context_ref_thread_default ();
656 if (map_id_to_client == NULL)
658 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
660 g_hash_table_insert (map_id_to_client,
661 GUINT_TO_POINTER (client->id),
662 client);
664 g_bus_get (bus_type,
665 NULL,
666 connection_get_cb,
667 client_ref (client));
669 G_UNLOCK (lock);
671 return client->id;
674 typedef struct {
675 GClosure *bus_acquired_closure;
676 GClosure *name_acquired_closure;
677 GClosure *name_lost_closure;
678 } OwnNameData;
680 static OwnNameData *
681 own_name_data_new (GClosure *bus_acquired_closure,
682 GClosure *name_acquired_closure,
683 GClosure *name_lost_closure)
685 OwnNameData *data;
687 data = g_new0 (OwnNameData, 1);
689 if (bus_acquired_closure != NULL)
691 data->bus_acquired_closure = g_closure_ref (bus_acquired_closure);
692 g_closure_sink (bus_acquired_closure);
693 if (G_CLOSURE_NEEDS_MARSHAL (bus_acquired_closure))
694 g_closure_set_marshal (bus_acquired_closure, g_cclosure_marshal_generic);
697 if (name_acquired_closure != NULL)
699 data->name_acquired_closure = g_closure_ref (name_acquired_closure);
700 g_closure_sink (name_acquired_closure);
701 if (G_CLOSURE_NEEDS_MARSHAL (name_acquired_closure))
702 g_closure_set_marshal (name_acquired_closure, g_cclosure_marshal_generic);
705 if (name_lost_closure != NULL)
707 data->name_lost_closure = g_closure_ref (name_lost_closure);
708 g_closure_sink (name_lost_closure);
709 if (G_CLOSURE_NEEDS_MARSHAL (name_lost_closure))
710 g_closure_set_marshal (name_lost_closure, g_cclosure_marshal_generic);
713 return data;
716 static void
717 own_with_closures_on_bus_acquired (GDBusConnection *connection,
718 const gchar *name,
719 gpointer user_data)
721 OwnNameData *data = user_data;
722 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
724 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
725 g_value_set_object (&params[0], connection);
727 g_value_init (&params[1], G_TYPE_STRING);
728 g_value_set_string (&params[1], name);
730 g_closure_invoke (data->bus_acquired_closure, NULL, 2, params, NULL);
732 g_value_unset (params + 0);
733 g_value_unset (params + 1);
736 static void
737 own_with_closures_on_name_acquired (GDBusConnection *connection,
738 const gchar *name,
739 gpointer user_data)
741 OwnNameData *data = user_data;
742 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
744 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
745 g_value_set_object (&params[0], connection);
747 g_value_init (&params[1], G_TYPE_STRING);
748 g_value_set_string (&params[1], name);
750 g_closure_invoke (data->name_acquired_closure, NULL, 2, params, NULL);
752 g_value_unset (params + 0);
753 g_value_unset (params + 1);
756 static void
757 own_with_closures_on_name_lost (GDBusConnection *connection,
758 const gchar *name,
759 gpointer user_data)
761 OwnNameData *data = user_data;
762 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
764 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
765 g_value_set_object (&params[0], connection);
767 g_value_init (&params[1], G_TYPE_STRING);
768 g_value_set_string (&params[1], name);
770 g_closure_invoke (data->name_lost_closure, NULL, 2, params, NULL);
772 g_value_unset (params + 0);
773 g_value_unset (params + 1);
776 static void
777 bus_own_name_free_func (gpointer user_data)
779 OwnNameData *data = user_data;
781 if (data->bus_acquired_closure != NULL)
782 g_closure_unref (data->bus_acquired_closure);
784 if (data->name_acquired_closure != NULL)
785 g_closure_unref (data->name_acquired_closure);
787 if (data->name_lost_closure != NULL)
788 g_closure_unref (data->name_lost_closure);
790 g_free (data);
794 * g_bus_own_name_with_closures: (rename-to g_bus_own_name)
795 * @bus_type: the type of bus to own a name on
796 * @name: the well-known name to own
797 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
798 * @bus_acquired_closure: (nullable): #GClosure to invoke when connected to
799 * the bus of type @bus_type or %NULL
800 * @name_acquired_closure: (nullable): #GClosure to invoke when @name is
801 * acquired or %NULL
802 * @name_lost_closure: (nullable): #GClosure to invoke when @name is lost or
803 * %NULL
805 * Version of g_bus_own_name() using closures instead of callbacks for
806 * easier binding in other languages.
808 * Returns: an identifier (never 0) that an be used with
809 * g_bus_unown_name() to stop owning the name.
811 * Since: 2.26
813 guint
814 g_bus_own_name_with_closures (GBusType bus_type,
815 const gchar *name,
816 GBusNameOwnerFlags flags,
817 GClosure *bus_acquired_closure,
818 GClosure *name_acquired_closure,
819 GClosure *name_lost_closure)
821 return g_bus_own_name (bus_type,
822 name,
823 flags,
824 bus_acquired_closure != NULL ? own_with_closures_on_bus_acquired : NULL,
825 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
826 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
827 own_name_data_new (bus_acquired_closure,
828 name_acquired_closure,
829 name_lost_closure),
830 bus_own_name_free_func);
834 * g_bus_own_name_on_connection_with_closures: (rename-to g_bus_own_name_on_connection)
835 * @connection: a #GDBusConnection
836 * @name: the well-known name to own
837 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
838 * @name_acquired_closure: (nullable): #GClosure to invoke when @name is
839 * acquired or %NULL
840 * @name_lost_closure: (nullable): #GClosure to invoke when @name is lost
841 * or %NULL
843 * Version of g_bus_own_name_on_connection() using closures instead of
844 * callbacks for easier binding in other languages.
846 * Returns: an identifier (never 0) that an be used with
847 * g_bus_unown_name() to stop owning the name.
849 * Since: 2.26
851 guint
852 g_bus_own_name_on_connection_with_closures (GDBusConnection *connection,
853 const gchar *name,
854 GBusNameOwnerFlags flags,
855 GClosure *name_acquired_closure,
856 GClosure *name_lost_closure)
858 return g_bus_own_name_on_connection (connection,
859 name,
860 flags,
861 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
862 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
863 own_name_data_new (NULL,
864 name_acquired_closure,
865 name_lost_closure),
866 bus_own_name_free_func);
870 * g_bus_unown_name:
871 * @owner_id: an identifier obtained from g_bus_own_name()
873 * Stops owning a name.
875 * Since: 2.26
877 void
878 g_bus_unown_name (guint owner_id)
880 Client *client;
882 g_return_if_fail (owner_id > 0);
884 client = NULL;
886 G_LOCK (lock);
887 if (owner_id == 0 || map_id_to_client == NULL ||
888 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (owner_id))) == NULL)
890 g_warning ("Invalid id %d passed to g_bus_unown_name()", owner_id);
891 goto out;
894 client->cancelled = TRUE;
895 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (owner_id)));
897 out:
898 G_UNLOCK (lock);
900 /* do callback without holding lock */
901 if (client != NULL)
903 /* Release the name if needed */
904 if (client->needs_release &&
905 client->connection != NULL &&
906 !g_dbus_connection_is_closed (client->connection))
908 GVariant *result;
909 GError *error;
910 guint32 release_name_reply;
912 /* TODO: it kinda sucks having to do a sync call to release the name - but if
913 * we don't, then a subsequent grab of the name will make the bus daemon return
914 * IN_QUEUE which will trigger name_lost().
916 * I believe this is a bug in the bus daemon.
918 error = NULL;
919 result = g_dbus_connection_call_sync (client->connection,
920 "org.freedesktop.DBus", /* bus name */
921 "/org/freedesktop/DBus", /* object path */
922 "org.freedesktop.DBus", /* interface name */
923 "ReleaseName", /* method name */
924 g_variant_new ("(s)", client->name),
925 G_VARIANT_TYPE ("(u)"),
926 G_DBUS_CALL_FLAGS_NONE,
928 NULL,
929 &error);
930 if (result == NULL)
932 g_warning ("Error releasing name %s: %s", client->name, error->message);
933 g_error_free (error);
935 else
937 g_variant_get (result, "(u)", &release_name_reply);
938 if (release_name_reply != 1 /* DBUS_RELEASE_NAME_REPLY_RELEASED */)
940 g_warning ("Unexpected reply %d when releasing name %s", release_name_reply, client->name);
942 g_variant_unref (result);
946 if (client->disconnected_signal_handler_id > 0)
947 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
948 if (client->name_acquired_subscription_id > 0)
949 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
950 if (client->name_lost_subscription_id > 0)
951 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
952 client->disconnected_signal_handler_id = 0;
953 client->name_acquired_subscription_id = 0;
954 client->name_lost_subscription_id = 0;
955 if (client->connection != NULL)
957 g_object_unref (client->connection);
958 client->connection = NULL;
961 client_unref (client);