Add some more test about gdbus_error apis
[glib.git] / gio / gdbusnameowning.c
blob6a5dc84620df2c21d6c8618fbb44c205090a40d3
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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
23 #include "config.h"
25 #include <stdlib.h>
27 #include "gdbusutils.h"
28 #include "gdbusnameowning.h"
29 #include "gdbuserror.h"
30 #include "gdbusprivate.h"
31 #include "gdbusconnection.h"
32 #include "gio-marshal.h"
34 #include "glibintl.h"
36 /**
37 * SECTION:gdbusnameowning
38 * @title: Owning Bus Names
39 * @short_description: Simple API for owning bus names
40 * @include: gio/gio.h
42 * Convenience API for owning bus names.
44 * <example id="gdbus-owning-names"><title>Simple application owning a name</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-own-name.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
47 G_LOCK_DEFINE_STATIC (lock);
49 /* ---------------------------------------------------------------------------------------------------- */
51 typedef enum
53 PREVIOUS_CALL_NONE = 0,
54 PREVIOUS_CALL_ACQUIRED,
55 PREVIOUS_CALL_LOST,
56 } PreviousCall;
58 typedef struct
60 volatile gint ref_count;
61 guint id;
62 GBusNameOwnerFlags flags;
63 gchar *name;
64 GBusAcquiredCallback bus_acquired_handler;
65 GBusNameAcquiredCallback name_acquired_handler;
66 GBusNameLostCallback name_lost_handler;
67 gpointer user_data;
68 GDestroyNotify user_data_free_func;
69 GMainContext *main_context;
71 PreviousCall previous_call;
73 GDBusConnection *connection;
74 gulong disconnected_signal_handler_id;
75 guint name_acquired_subscription_id;
76 guint name_lost_subscription_id;
78 gboolean cancelled;
80 gboolean needs_release;
81 } Client;
83 static guint next_global_id = 1;
84 static GHashTable *map_id_to_client = NULL;
87 static Client *
88 client_ref (Client *client)
90 g_atomic_int_inc (&client->ref_count);
91 return client;
94 static void
95 client_unref (Client *client)
97 if (g_atomic_int_dec_and_test (&client->ref_count))
99 if (client->connection != NULL)
101 if (client->disconnected_signal_handler_id > 0)
102 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
103 if (client->name_acquired_subscription_id > 0)
104 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
105 if (client->name_lost_subscription_id > 0)
106 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
107 g_object_unref (client->connection);
109 if (client->main_context != NULL)
110 g_main_context_unref (client->main_context);
111 g_free (client->name);
112 if (client->user_data_free_func != NULL)
113 client->user_data_free_func (client->user_data);
114 g_free (client);
118 /* ---------------------------------------------------------------------------------------------------- */
121 typedef enum
123 CALL_TYPE_NAME_ACQUIRED,
124 CALL_TYPE_NAME_LOST
125 } CallType;
127 typedef struct
129 Client *client;
131 /* keep this separate because client->connection may
132 * be set to NULL after scheduling the call
134 GDBusConnection *connection;
136 /* set to TRUE to call acquired */
137 CallType call_type;
138 } CallHandlerData;
140 static void
141 call_handler_data_free (CallHandlerData *data)
143 if (data->connection != NULL)
144 g_object_unref (data->connection);
145 client_unref (data->client);
146 g_free (data);
149 static void
150 actually_do_call (Client *client, GDBusConnection *connection, CallType call_type)
152 switch (call_type)
154 case CALL_TYPE_NAME_ACQUIRED:
155 if (client->name_acquired_handler != NULL)
157 client->name_acquired_handler (connection,
158 client->name,
159 client->user_data);
161 break;
163 case CALL_TYPE_NAME_LOST:
164 if (client->name_lost_handler != NULL)
166 client->name_lost_handler (connection,
167 client->name,
168 client->user_data);
170 break;
172 default:
173 g_assert_not_reached ();
174 break;
178 static gboolean
179 call_in_idle_cb (gpointer _data)
181 CallHandlerData *data = _data;
182 actually_do_call (data->client, data->connection, data->call_type);
183 return FALSE;
186 static void
187 schedule_call_in_idle (Client *client, CallType call_type)
189 CallHandlerData *data;
190 GSource *idle_source;
192 data = g_new0 (CallHandlerData, 1);
193 data->client = client_ref (client);
194 data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
195 data->call_type = call_type;
197 idle_source = g_idle_source_new ();
198 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
199 g_source_set_callback (idle_source,
200 call_in_idle_cb,
201 data,
202 (GDestroyNotify) call_handler_data_free);
203 g_source_attach (idle_source, client->main_context);
204 g_source_unref (idle_source);
207 static void
208 do_call (Client *client, CallType call_type)
210 /* only schedule in idle if we're not in the right thread */
211 if (g_main_context_get_thread_default () != client->main_context)
212 schedule_call_in_idle (client, call_type);
213 else
214 actually_do_call (client, client->connection, call_type);
217 static void
218 call_acquired_handler (Client *client)
220 if (client->previous_call != PREVIOUS_CALL_ACQUIRED)
222 client->previous_call = PREVIOUS_CALL_ACQUIRED;
223 if (!client->cancelled)
225 do_call (client, CALL_TYPE_NAME_ACQUIRED);
230 static void
231 call_lost_handler (Client *client)
233 if (client->previous_call != PREVIOUS_CALL_LOST)
235 client->previous_call = PREVIOUS_CALL_LOST;
236 if (!client->cancelled)
238 do_call (client, CALL_TYPE_NAME_LOST);
243 /* ---------------------------------------------------------------------------------------------------- */
245 static void
246 on_name_lost_or_acquired (GDBusConnection *connection,
247 const gchar *sender_name,
248 const gchar *object_path,
249 const gchar *interface_name,
250 const gchar *signal_name,
251 GVariant *parameters,
252 gpointer user_data)
254 Client *client = user_data;
255 const gchar *name;
257 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
258 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
259 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
260 goto out;
262 if (g_strcmp0 (signal_name, "NameLost") == 0)
264 g_variant_get (parameters, "(&s)", &name);
265 if (g_strcmp0 (name, client->name) == 0)
267 call_lost_handler (client);
270 else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
272 g_variant_get (parameters, "(&s)", &name);
273 if (g_strcmp0 (name, client->name) == 0)
275 call_acquired_handler (client);
278 out:
282 /* ---------------------------------------------------------------------------------------------------- */
284 static void
285 request_name_cb (GObject *source_object,
286 GAsyncResult *res,
287 gpointer user_data)
289 Client *client = user_data;
290 GVariant *result;
291 guint32 request_name_reply;
292 gboolean subscribe;
294 request_name_reply = 0;
295 result = NULL;
297 result = g_dbus_connection_call_finish (client->connection,
298 res,
299 NULL);
300 if (result != NULL)
302 g_variant_get (result, "(u)", &request_name_reply);
303 g_variant_unref (result);
306 subscribe = FALSE;
308 switch (request_name_reply)
310 case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
311 /* We got the name - now listen for NameLost and NameAcquired */
312 call_acquired_handler (client);
313 subscribe = TRUE;
314 client->needs_release = TRUE;
315 break;
317 case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
318 /* Waiting in line - listen for NameLost and NameAcquired */
319 call_lost_handler (client);
320 subscribe = TRUE;
321 client->needs_release = TRUE;
322 break;
324 default:
325 /* assume we couldn't get the name - explicit fallthrough */
326 case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
327 case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
328 /* Some other part of the process is already owning the name */
329 call_lost_handler (client);
330 break;
333 if (subscribe)
335 /* start listening to NameLost and NameAcquired messages */
336 client->name_lost_subscription_id =
337 g_dbus_connection_signal_subscribe (client->connection,
338 "org.freedesktop.DBus",
339 "org.freedesktop.DBus",
340 "NameLost",
341 "/org/freedesktop/DBus",
342 client->name,
343 G_DBUS_SIGNAL_FLAGS_NONE,
344 on_name_lost_or_acquired,
345 client,
346 NULL);
347 client->name_acquired_subscription_id =
348 g_dbus_connection_signal_subscribe (client->connection,
349 "org.freedesktop.DBus",
350 "org.freedesktop.DBus",
351 "NameAcquired",
352 "/org/freedesktop/DBus",
353 client->name,
354 G_DBUS_SIGNAL_FLAGS_NONE,
355 on_name_lost_or_acquired,
356 client,
357 NULL);
360 client_unref (client);
363 /* ---------------------------------------------------------------------------------------------------- */
365 static void
366 on_connection_disconnected (GDBusConnection *connection,
367 gboolean remote_peer_vanished,
368 GError *error,
369 gpointer user_data)
371 Client *client = user_data;
373 if (client->disconnected_signal_handler_id > 0)
374 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
375 if (client->name_acquired_subscription_id > 0)
376 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
377 if (client->name_lost_subscription_id > 0)
378 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
379 g_object_unref (client->connection);
380 client->disconnected_signal_handler_id = 0;
381 client->name_acquired_subscription_id = 0;
382 client->name_lost_subscription_id = 0;
383 client->connection = NULL;
385 call_lost_handler (client);
388 /* ---------------------------------------------------------------------------------------------------- */
390 static void
391 has_connection (Client *client)
393 /* listen for disconnection */
394 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
395 "closed",
396 G_CALLBACK (on_connection_disconnected),
397 client);
399 /* attempt to acquire the name */
400 g_dbus_connection_call (client->connection,
401 "org.freedesktop.DBus", /* bus name */
402 "/org/freedesktop/DBus", /* object path */
403 "org.freedesktop.DBus", /* interface name */
404 "RequestName", /* method name */
405 g_variant_new ("(su)",
406 client->name,
407 client->flags),
408 G_VARIANT_TYPE ("(u)"),
409 G_DBUS_CALL_FLAGS_NONE,
411 NULL,
412 (GAsyncReadyCallback) request_name_cb,
413 client_ref (client));
417 static void
418 connection_get_cb (GObject *source_object,
419 GAsyncResult *res,
420 gpointer user_data)
422 Client *client = user_data;
424 client->connection = g_bus_get_finish (res, NULL);
425 if (client->connection == NULL)
427 call_lost_handler (client);
428 goto out;
431 /* No need to schedule this in idle as we're already in the thread
432 * that the user called g_bus_own_name() from. This is because
433 * g_bus_get() guarantees that.
435 * Also, we need to ensure that the handler is invoked *before*
436 * we call RequestName(). Otherwise there is a race.
438 if (client->bus_acquired_handler != NULL)
440 client->bus_acquired_handler (client->connection,
441 client->name,
442 client->user_data);
445 has_connection (client);
447 out:
448 client_unref (client);
451 /* ---------------------------------------------------------------------------------------------------- */
454 * g_bus_own_name_on_connection:
455 * @connection: A #GDBusConnection.
456 * @name: The well-known name to own.
457 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
458 * @name_acquired_handler: Handler to invoke when @name is acquired or %NULL.
459 * @name_lost_handler: Handler to invoke when @name is lost or %NULL.
460 * @user_data: User data to pass to handlers.
461 * @user_data_free_func: Function for freeing @user_data or %NULL.
463 * Like g_bus_own_name() but takes a #GDBusConnection instead of a
464 * #GBusType.
466 * Returns: An identifier (never 0) that an be used with
467 * g_bus_unown_name() to stop owning the name.
469 * Since: 2.26
471 guint
472 g_bus_own_name_on_connection (GDBusConnection *connection,
473 const gchar *name,
474 GBusNameOwnerFlags flags,
475 GBusNameAcquiredCallback name_acquired_handler,
476 GBusNameLostCallback name_lost_handler,
477 gpointer user_data,
478 GDestroyNotify user_data_free_func)
480 Client *client;
482 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
483 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
485 G_LOCK (lock);
487 client = g_new0 (Client, 1);
488 client->ref_count = 1;
489 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
490 client->name = g_strdup (name);
491 client->flags = flags;
492 client->name_acquired_handler = name_acquired_handler;
493 client->name_lost_handler = name_lost_handler;
494 client->user_data = user_data;
495 client->user_data_free_func = user_data_free_func;
496 client->main_context = g_main_context_get_thread_default ();
497 if (client->main_context != NULL)
498 g_main_context_ref (client->main_context);
500 client->connection = g_object_ref (connection);
502 if (map_id_to_client == NULL)
504 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
506 g_hash_table_insert (map_id_to_client,
507 GUINT_TO_POINTER (client->id),
508 client);
510 G_UNLOCK (lock);
512 has_connection (client);
514 return client->id;
518 * g_bus_own_name:
519 * @bus_type: The type of bus to own a name on.
520 * @name: The well-known name to own.
521 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
522 * @bus_acquired_handler: Handler to invoke when connected to the bus of type @bus_type or %NULL.
523 * @name_acquired_handler: Handler to invoke when @name is acquired or %NULL.
524 * @name_lost_handler: Handler to invoke when @name is lost or %NULL.
525 * @user_data: User data to pass to handlers.
526 * @user_data_free_func: Function for freeing @user_data or %NULL.
528 * Starts acquiring @name on the bus specified by @bus_type and calls
529 * @name_acquired_handler and @name_lost_handler when the name is
530 * acquired respectively lost. Callbacks will be invoked in the <link
531 * linkend="g-main-context-push-thread-default">thread-default main
532 * loop</link> of the thread you are calling this function from.
534 * You are guaranteed that one of the @name_acquired_handler and @name_lost_handler
535 * callbacks will be invoked after calling this function - there are three
536 * possible cases:
537 * <itemizedlist>
538 * <listitem><para>
539 * @name_lost_handler with a %NULL connection (if a connection to the bus can't be made).
540 * </para></listitem>
541 * <listitem><para>
542 * @bus_acquired_handler then @name_lost_handler (if the name can't be obtained)
543 * </para></listitem>
544 * <listitem><para>
545 * @bus_acquired_handler then @name_acquired_handler (if the name was obtained).
546 * </para></listitem>
547 * </itemizedlist>
548 * When you are done owning the name, just call g_bus_unown_name()
549 * with the owner id this function returns.
551 * If the name is acquired or lost (for example another application
552 * could acquire the name if you allow replacement or the application
553 * currently owning the name exits), the handlers are also invoked. If the
554 * #GDBusConnection that is used for attempting to own the name
555 * closes, then @name_lost_handler is invoked since it is no
556 * longer possible for other processes to access the process.
558 * You cannot use g_bus_own_name() several times for the same name (unless
559 * interleaved with calls to g_bus_unown_name()) - only the first call
560 * will work.
562 * Another guarantee is that invocations of @name_acquired_handler
563 * and @name_lost_handler are guaranteed to alternate; that
564 * is, if @name_acquired_handler is invoked then you are
565 * guaranteed that the next time one of the handlers is invoked, it
566 * will be @name_lost_handler. The reverse is also true.
568 * If you plan on exporting objects (using e.g.
569 * g_dbus_connection_register_object()), note that it is generally too late
570 * to export the objects in @name_acquired_handler. Instead, you can do this
571 * in @bus_acquired_handler since you are guaranteed that this will run
572 * before @name is requested from the bus.
574 * This behavior makes it very simple to write applications that wants
575 * to own names and export objects, see <xref linkend="gdbus-owning-names"/>.
576 * Simply register objects to be exported in @bus_acquired_handler and
577 * unregister the objects (if any) in @name_lost_handler.
579 * Returns: An identifier (never 0) that an be used with
580 * g_bus_unown_name() to stop owning the name.
582 * Since: 2.26
584 guint
585 g_bus_own_name (GBusType bus_type,
586 const gchar *name,
587 GBusNameOwnerFlags flags,
588 GBusAcquiredCallback bus_acquired_handler,
589 GBusNameAcquiredCallback name_acquired_handler,
590 GBusNameLostCallback name_lost_handler,
591 gpointer user_data,
592 GDestroyNotify user_data_free_func)
594 Client *client;
596 g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
598 G_LOCK (lock);
600 client = g_new0 (Client, 1);
601 client->ref_count = 1;
602 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
603 client->name = g_strdup (name);
604 client->flags = flags;
605 client->bus_acquired_handler = bus_acquired_handler;
606 client->name_acquired_handler = name_acquired_handler;
607 client->name_lost_handler = name_lost_handler;
608 client->user_data = user_data;
609 client->user_data_free_func = user_data_free_func;
610 client->main_context = g_main_context_get_thread_default ();
611 if (client->main_context != NULL)
612 g_main_context_ref (client->main_context);
614 if (map_id_to_client == NULL)
616 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
618 g_hash_table_insert (map_id_to_client,
619 GUINT_TO_POINTER (client->id),
620 client);
622 g_bus_get (bus_type,
623 NULL,
624 connection_get_cb,
625 client_ref (client));
627 G_UNLOCK (lock);
629 return client->id;
632 typedef struct {
633 GClosure *bus_acquired_closure;
634 GClosure *name_acquired_closure;
635 GClosure *name_lost_closure;
636 } OwnNameData;
638 static OwnNameData *
639 own_name_data_new (GClosure *bus_acquired_closure,
640 GClosure *name_acquired_closure,
641 GClosure *name_lost_closure)
643 OwnNameData *data;
645 data = g_new0 (OwnNameData, 1);
647 if (bus_acquired_closure != NULL)
649 data->bus_acquired_closure = g_closure_ref (bus_acquired_closure);
650 g_closure_sink (bus_acquired_closure);
651 if (G_CLOSURE_NEEDS_MARSHAL (bus_acquired_closure))
652 g_closure_set_marshal (bus_acquired_closure, _gio_marshal_VOID__STRING);
655 if (name_acquired_closure != NULL)
657 data->name_acquired_closure = g_closure_ref (name_acquired_closure);
658 g_closure_sink (name_acquired_closure);
659 if (G_CLOSURE_NEEDS_MARSHAL (name_acquired_closure))
660 g_closure_set_marshal (name_acquired_closure, _gio_marshal_VOID__STRING);
663 if (name_lost_closure != NULL)
665 data->name_lost_closure = g_closure_ref (name_lost_closure);
666 g_closure_sink (name_lost_closure);
667 if (G_CLOSURE_NEEDS_MARSHAL (name_lost_closure))
668 g_closure_set_marshal (name_lost_closure, _gio_marshal_VOID__STRING);
671 return data;
674 static void
675 own_with_closures_on_bus_acquired (GDBusConnection *connection,
676 const gchar *name,
677 gpointer user_data)
679 OwnNameData *data = user_data;
680 GValue params[2] = { { 0, }, { 0, } };
682 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
683 g_value_set_object (&params[0], connection);
685 g_value_init (&params[1], G_TYPE_STRING);
686 g_value_set_string (&params[1], name);
688 g_closure_invoke (data->bus_acquired_closure, NULL, 2, params, NULL);
690 g_value_unset (params + 0);
691 g_value_unset (params + 1);
694 static void
695 own_with_closures_on_name_acquired (GDBusConnection *connection,
696 const gchar *name,
697 gpointer user_data)
699 OwnNameData *data = user_data;
700 GValue params[2] = { { 0, }, { 0, } };
702 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
703 g_value_set_object (&params[0], connection);
705 g_value_init (&params[1], G_TYPE_STRING);
706 g_value_set_string (&params[1], name);
708 g_closure_invoke (data->name_acquired_closure, NULL, 2, params, NULL);
710 g_value_unset (params + 0);
711 g_value_unset (params + 1);
714 static void
715 own_with_closures_on_name_lost (GDBusConnection *connection,
716 const gchar *name,
717 gpointer user_data)
719 OwnNameData *data = user_data;
720 GValue params[2] = { { 0, }, { 0, } };
722 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
723 g_value_set_object (&params[0], connection);
725 g_value_init (&params[1], G_TYPE_STRING);
726 g_value_set_string (&params[1], name);
728 g_closure_invoke (data->name_lost_closure, NULL, 2, params, NULL);
730 g_value_unset (params + 0);
731 g_value_unset (params + 1);
734 static void
735 bus_own_name_free_func (gpointer user_data)
737 OwnNameData *data = user_data;
739 if (data->bus_acquired_closure != NULL)
740 g_closure_unref (data->bus_acquired_closure);
742 if (data->name_acquired_closure != NULL)
743 g_closure_unref (data->name_acquired_closure);
745 if (data->name_lost_closure != NULL)
746 g_closure_unref (data->name_lost_closure);
748 g_free (data);
752 * g_bus_own_name_with_closures:
753 * @bus_type: The type of bus to own a name on.
754 * @name: The well-known name to own.
755 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
756 * @bus_acquired_closure: (allow-none): #GClosure to invoke when connected to
757 * the bus of type @bus_type or %NULL.
758 * @name_acquired_closure: (allow-none): #GClosure to invoke when @name is
759 * acquired or %NULL.
760 * @name_lost_closure: (allow-none): #GClosure to invoke when @name is lost or
761 * %NULL.
763 * Version of g_bus_own_name() using closures instead of callbacks for
764 * easier binding in other languages.
766 * Returns: An identifier (never 0) that an be used with
767 * g_bus_unown_name() to stop owning the name.
769 * Rename to: g_bus_own_name
771 * Since: 2.26
773 guint
774 g_bus_own_name_with_closures (GBusType bus_type,
775 const gchar *name,
776 GBusNameOwnerFlags flags,
777 GClosure *bus_acquired_closure,
778 GClosure *name_acquired_closure,
779 GClosure *name_lost_closure)
781 return g_bus_own_name (bus_type,
782 name,
783 flags,
784 bus_acquired_closure != NULL ? own_with_closures_on_bus_acquired : NULL,
785 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
786 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
787 own_name_data_new (bus_acquired_closure,
788 name_acquired_closure,
789 name_lost_closure),
790 bus_own_name_free_func);
794 * g_bus_own_name_on_connection_with_closures:
795 * @connection: A #GDBusConnection.
796 * @name: The well-known name to own.
797 * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
798 * @name_acquired_closure: (allow-none): #GClosure to invoke when @name is
799 * acquired or %NULL.
800 * @name_lost_closure: (allow-none): #GClosure to invoke when @name is lost or
801 * %NULL.
803 * Version of g_bus_own_name_on_connection() using closures instead of callbacks for
804 * easier binding in other languages.
806 * Returns: An identifier (never 0) that an be used with
807 * g_bus_unown_name() to stop owning the name.
809 * Rename to: g_bus_own_name_on_connection
811 * Since: 2.26
813 guint
814 g_bus_own_name_on_connection_with_closures (GDBusConnection *connection,
815 const gchar *name,
816 GBusNameOwnerFlags flags,
817 GClosure *name_acquired_closure,
818 GClosure *name_lost_closure)
820 return g_bus_own_name_on_connection (connection,
821 name,
822 flags,
823 name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
824 name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
825 own_name_data_new (NULL,
826 name_acquired_closure,
827 name_lost_closure),
828 bus_own_name_free_func);
832 * g_bus_unown_name:
833 * @owner_id: An identifier obtained from g_bus_own_name()
835 * Stops owning a name.
837 * Since: 2.26
839 void
840 g_bus_unown_name (guint owner_id)
842 Client *client;
844 g_return_if_fail (owner_id > 0);
846 client = NULL;
848 G_LOCK (lock);
849 if (owner_id == 0 || map_id_to_client == NULL ||
850 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (owner_id))) == NULL)
852 g_warning ("Invalid id %d passed to g_bus_unown_name()", owner_id);
853 goto out;
856 client->cancelled = TRUE;
857 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (owner_id)));
859 out:
860 G_UNLOCK (lock);
862 /* do callback without holding lock */
863 if (client != NULL)
865 /* Release the name if needed */
866 if (client->needs_release && client->connection != NULL)
868 GVariant *result;
869 GError *error;
870 guint32 release_name_reply;
872 /* TODO: it kinda sucks having to do a sync call to release the name - but if
873 * we don't, then a subsequent grab of the name will make the bus daemon return
874 * IN_QUEUE which will trigger name_lost().
876 * I believe this is a bug in the bus daemon.
878 error = NULL;
879 result = g_dbus_connection_call_sync (client->connection,
880 "org.freedesktop.DBus", /* bus name */
881 "/org/freedesktop/DBus", /* object path */
882 "org.freedesktop.DBus", /* interface name */
883 "ReleaseName", /* method name */
884 g_variant_new ("(s)", client->name),
885 G_VARIANT_TYPE ("(u)"),
886 G_DBUS_CALL_FLAGS_NONE,
888 NULL,
889 &error);
890 if (result == NULL)
892 g_warning ("Error releasing name %s: %s", client->name, error->message);
893 g_error_free (error);
895 else
897 g_variant_get (result, "(u)", &release_name_reply);
898 if (release_name_reply != 1 /* DBUS_RELEASE_NAME_REPLY_RELEASED */)
900 g_warning ("Unexpected reply %d when releasing name %s", release_name_reply, client->name);
902 g_variant_unref (result);
906 if (client->disconnected_signal_handler_id > 0)
907 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
908 if (client->name_acquired_subscription_id > 0)
909 g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
910 if (client->name_lost_subscription_id > 0)
911 g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
912 client->disconnected_signal_handler_id = 0;
913 client->name_acquired_subscription_id = 0;
914 client->name_lost_subscription_id = 0;
915 if (client->connection != NULL)
917 g_object_unref (client->connection);
918 client->connection = NULL;
921 client_unref (client);