Add some more cases to the app-id unit tests
[glib.git] / gio / gdbusnamewatching.c
blob14603c5e494e283a845b8ca150ed1610ca1e0593
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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
26 #include "gdbusutils.h"
27 #include "gdbusnamewatching.h"
28 #include "gdbuserror.h"
29 #include "gdbusprivate.h"
30 #include "gdbusconnection.h"
32 #include "glibintl.h"
34 /**
35 * SECTION:gdbusnamewatching
36 * @title: Watching Bus Names
37 * @short_description: Simple API for watching bus names
38 * @include: gio/gio.h
40 * Convenience API for watching bus names.
42 * A simple example for watching a name can be found in
43 * [gdbus-example-watch-name.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-watch-name.c)
46 G_LOCK_DEFINE_STATIC (lock);
48 /* ---------------------------------------------------------------------------------------------------- */
50 typedef enum
52 PREVIOUS_CALL_NONE = 0,
53 PREVIOUS_CALL_APPEARED,
54 PREVIOUS_CALL_VANISHED,
55 } PreviousCall;
57 typedef struct
59 volatile gint ref_count;
60 guint id;
61 gchar *name;
62 GBusNameWatcherFlags flags;
63 gchar *name_owner;
64 GBusNameAppearedCallback name_appeared_handler;
65 GBusNameVanishedCallback name_vanished_handler;
66 gpointer user_data;
67 GDestroyNotify user_data_free_func;
68 GMainContext *main_context;
70 GDBusConnection *connection;
71 gulong disconnected_signal_handler_id;
72 guint name_owner_changed_subscription_id;
74 PreviousCall previous_call;
76 gboolean cancelled;
77 gboolean initialized;
78 } Client;
80 /* Must be accessed atomically. */
81 static volatile guint next_global_id = 1;
83 /* Must be accessed with @lock held. */
84 static GHashTable *map_id_to_client = NULL;
86 static Client *
87 client_ref (Client *client)
89 g_atomic_int_inc (&client->ref_count);
90 return client;
93 static void
94 client_unref (Client *client)
96 if (g_atomic_int_dec_and_test (&client->ref_count))
98 if (client->connection != NULL)
100 if (client->name_owner_changed_subscription_id > 0)
101 g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
102 if (client->disconnected_signal_handler_id > 0)
103 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
104 g_object_unref (client->connection);
106 g_free (client->name);
107 g_free (client->name_owner);
108 g_main_context_unref (client->main_context);
109 if (client->user_data_free_func != NULL)
110 client->user_data_free_func (client->user_data);
111 g_free (client);
115 /* ---------------------------------------------------------------------------------------------------- */
117 typedef enum
119 CALL_TYPE_NAME_APPEARED,
120 CALL_TYPE_NAME_VANISHED
121 } CallType;
123 typedef struct
125 Client *client;
127 /* keep this separate because client->connection may
128 * be set to NULL after scheduling the call
130 GDBusConnection *connection;
132 /* ditto */
133 gchar *name_owner;
135 CallType call_type;
136 } CallHandlerData;
138 static void
139 call_handler_data_free (CallHandlerData *data)
141 if (data->connection != NULL)
142 g_object_unref (data->connection);
143 g_free (data->name_owner);
144 client_unref (data->client);
145 g_free (data);
148 static void
149 actually_do_call (Client *client, GDBusConnection *connection, const gchar *name_owner, CallType call_type)
151 switch (call_type)
153 case CALL_TYPE_NAME_APPEARED:
154 if (client->name_appeared_handler != NULL)
156 client->name_appeared_handler (connection,
157 client->name,
158 name_owner,
159 client->user_data);
161 break;
163 case CALL_TYPE_NAME_VANISHED:
164 if (client->name_vanished_handler != NULL)
166 client->name_vanished_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->name_owner, 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->name_owner = g_strdup (client->name_owner);
196 data->call_type = call_type;
198 idle_source = g_idle_source_new ();
199 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
200 g_source_set_callback (idle_source,
201 call_in_idle_cb,
202 data,
203 (GDestroyNotify) call_handler_data_free);
204 g_source_set_name (idle_source, "[gio] call_in_idle_cb");
205 g_source_attach (idle_source, client->main_context);
206 g_source_unref (idle_source);
209 static void
210 do_call (Client *client, CallType call_type)
212 GMainContext *current_context;
214 /* only schedule in idle if we're not in the right thread */
215 current_context = g_main_context_ref_thread_default ();
216 if (current_context != client->main_context)
217 schedule_call_in_idle (client, call_type);
218 else
219 actually_do_call (client, client->connection, client->name_owner, call_type);
220 g_main_context_unref (current_context);
223 static void
224 call_appeared_handler (Client *client)
226 if (client->previous_call != PREVIOUS_CALL_APPEARED)
228 client->previous_call = PREVIOUS_CALL_APPEARED;
229 if (!client->cancelled && client->name_appeared_handler != NULL)
231 do_call (client, CALL_TYPE_NAME_APPEARED);
236 static void
237 call_vanished_handler (Client *client,
238 gboolean ignore_cancelled)
240 if (client->previous_call != PREVIOUS_CALL_VANISHED)
242 client->previous_call = PREVIOUS_CALL_VANISHED;
243 if (((!client->cancelled) || ignore_cancelled) && client->name_vanished_handler != NULL)
245 do_call (client, CALL_TYPE_NAME_VANISHED);
250 /* ---------------------------------------------------------------------------------------------------- */
252 static void
253 on_connection_disconnected (GDBusConnection *connection,
254 gboolean remote_peer_vanished,
255 GError *error,
256 gpointer user_data)
258 Client *client = user_data;
260 if (client->name_owner_changed_subscription_id > 0)
261 g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
262 if (client->disconnected_signal_handler_id > 0)
263 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
264 g_object_unref (client->connection);
265 client->disconnected_signal_handler_id = 0;
266 client->name_owner_changed_subscription_id = 0;
267 client->connection = NULL;
269 call_vanished_handler (client, FALSE);
272 /* ---------------------------------------------------------------------------------------------------- */
274 static void
275 on_name_owner_changed (GDBusConnection *connection,
276 const gchar *sender_name,
277 const gchar *object_path,
278 const gchar *interface_name,
279 const gchar *signal_name,
280 GVariant *parameters,
281 gpointer user_data)
283 Client *client = user_data;
284 const gchar *name;
285 const gchar *old_owner;
286 const gchar *new_owner;
288 if (!client->initialized)
289 goto out;
291 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
292 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
293 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
294 goto out;
296 g_variant_get (parameters,
297 "(&s&s&s)",
298 &name,
299 &old_owner,
300 &new_owner);
302 /* we only care about a specific name */
303 if (g_strcmp0 (name, client->name) != 0)
304 goto out;
306 if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
308 g_free (client->name_owner);
309 client->name_owner = NULL;
310 call_vanished_handler (client, FALSE);
313 if (new_owner != NULL && strlen (new_owner) > 0)
315 g_warn_if_fail (client->name_owner == NULL);
316 g_free (client->name_owner);
317 client->name_owner = g_strdup (new_owner);
318 call_appeared_handler (client);
321 out:
325 /* ---------------------------------------------------------------------------------------------------- */
327 static void
328 get_name_owner_cb (GObject *source_object,
329 GAsyncResult *res,
330 gpointer user_data)
332 Client *client = user_data;
333 GVariant *result;
334 const char *name_owner;
336 name_owner = NULL;
337 result = NULL;
339 result = g_dbus_connection_call_finish (client->connection,
340 res,
341 NULL);
342 if (result != NULL)
344 g_variant_get (result, "(&s)", &name_owner);
347 if (name_owner != NULL)
349 g_warn_if_fail (client->name_owner == NULL);
350 client->name_owner = g_strdup (name_owner);
351 call_appeared_handler (client);
353 else
355 call_vanished_handler (client, FALSE);
358 client->initialized = TRUE;
360 if (result != NULL)
361 g_variant_unref (result);
362 client_unref (client);
365 /* ---------------------------------------------------------------------------------------------------- */
367 static void
368 invoke_get_name_owner (Client *client)
370 g_dbus_connection_call (client->connection,
371 "org.freedesktop.DBus", /* bus name */
372 "/org/freedesktop/DBus", /* object path */
373 "org.freedesktop.DBus", /* interface name */
374 "GetNameOwner", /* method name */
375 g_variant_new ("(s)", client->name),
376 G_VARIANT_TYPE ("(s)"),
377 G_DBUS_CALL_FLAGS_NONE,
379 NULL,
380 (GAsyncReadyCallback) get_name_owner_cb,
381 client_ref (client));
384 /* ---------------------------------------------------------------------------------------------------- */
386 static void
387 start_service_by_name_cb (GObject *source_object,
388 GAsyncResult *res,
389 gpointer user_data)
391 Client *client = user_data;
392 GVariant *result;
394 result = NULL;
396 result = g_dbus_connection_call_finish (client->connection,
397 res,
398 NULL);
399 if (result != NULL)
401 guint32 start_service_result;
402 g_variant_get (result, "(u)", &start_service_result);
404 if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
406 invoke_get_name_owner (client);
408 else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
410 invoke_get_name_owner (client);
412 else
414 g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
415 call_vanished_handler (client, FALSE);
416 client->initialized = TRUE;
419 else
421 /* Errors are not unexpected; the bus will reply e.g.
423 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
424 * was not provided by any .service files
426 * This doesn't mean that the name doesn't have an owner, just
427 * that it's not provided by a .service file. So proceed to
428 * invoke GetNameOwner().
430 invoke_get_name_owner (client);
433 if (result != NULL)
434 g_variant_unref (result);
435 client_unref (client);
438 /* ---------------------------------------------------------------------------------------------------- */
440 static void
441 has_connection (Client *client)
443 /* listen for disconnection */
444 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
445 "closed",
446 G_CALLBACK (on_connection_disconnected),
447 client);
449 /* start listening to NameOwnerChanged messages immediately */
450 client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
451 "org.freedesktop.DBus", /* name */
452 "org.freedesktop.DBus", /* if */
453 "NameOwnerChanged", /* signal */
454 "/org/freedesktop/DBus", /* path */
455 client->name,
456 G_DBUS_SIGNAL_FLAGS_NONE,
457 on_name_owner_changed,
458 client,
459 NULL);
461 if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
463 g_dbus_connection_call (client->connection,
464 "org.freedesktop.DBus", /* bus name */
465 "/org/freedesktop/DBus", /* object path */
466 "org.freedesktop.DBus", /* interface name */
467 "StartServiceByName", /* method name */
468 g_variant_new ("(su)", client->name, 0),
469 G_VARIANT_TYPE ("(u)"),
470 G_DBUS_CALL_FLAGS_NONE,
472 NULL,
473 (GAsyncReadyCallback) start_service_by_name_cb,
474 client_ref (client));
476 else
478 /* check owner */
479 invoke_get_name_owner (client);
484 static void
485 connection_get_cb (GObject *source_object,
486 GAsyncResult *res,
487 gpointer user_data)
489 Client *client = user_data;
491 client->connection = g_bus_get_finish (res, NULL);
492 if (client->connection == NULL)
494 call_vanished_handler (client, FALSE);
495 goto out;
498 has_connection (client);
500 out:
501 client_unref (client);
504 /* ---------------------------------------------------------------------------------------------------- */
507 * g_bus_watch_name:
508 * @bus_type: The type of bus to watch a name on.
509 * @name: The name (well-known or unique) to watch.
510 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
511 * @name_appeared_handler: (nullable): Handler to invoke when @name is known to exist or %NULL.
512 * @name_vanished_handler: (nullable): Handler to invoke when @name is known to not exist or %NULL.
513 * @user_data: User data to pass to handlers.
514 * @user_data_free_func: (nullable): Function for freeing @user_data or %NULL.
516 * Starts watching @name on the bus specified by @bus_type and calls
517 * @name_appeared_handler and @name_vanished_handler when the name is
518 * known to have a owner respectively known to lose its
519 * owner. Callbacks will be invoked in the
520 * [thread-default main context][g-main-context-push-thread-default]
521 * of the thread you are calling this function from.
523 * You are guaranteed that one of the handlers will be invoked after
524 * calling this function. When you are done watching the name, just
525 * call g_bus_unwatch_name() with the watcher id this function
526 * returns.
528 * If the name vanishes or appears (for example the application owning
529 * the name could restart), the handlers are also invoked. If the
530 * #GDBusConnection that is used for watching the name disconnects, then
531 * @name_vanished_handler is invoked since it is no longer
532 * possible to access the name.
534 * Another guarantee is that invocations of @name_appeared_handler
535 * and @name_vanished_handler are guaranteed to alternate; that
536 * is, if @name_appeared_handler is invoked then you are
537 * guaranteed that the next time one of the handlers is invoked, it
538 * will be @name_vanished_handler. The reverse is also true.
540 * This behavior makes it very simple to write applications that want
541 * to take action when a certain [name exists][gdbus-watching-names].
542 * Basically, the application should create object proxies in
543 * @name_appeared_handler and destroy them again (if any) in
544 * @name_vanished_handler.
546 * Returns: An identifier (never 0) that an be used with
547 * g_bus_unwatch_name() to stop watching the name.
549 * Since: 2.26
551 guint
552 g_bus_watch_name (GBusType bus_type,
553 const gchar *name,
554 GBusNameWatcherFlags flags,
555 GBusNameAppearedCallback name_appeared_handler,
556 GBusNameVanishedCallback name_vanished_handler,
557 gpointer user_data,
558 GDestroyNotify user_data_free_func)
560 Client *client;
562 g_return_val_if_fail (g_dbus_is_name (name), 0);
564 G_LOCK (lock);
566 client = g_new0 (Client, 1);
567 client->ref_count = 1;
568 client->id = g_atomic_int_add (&next_global_id, 1); /* TODO: uh oh, handle overflow */
569 client->name = g_strdup (name);
570 client->flags = flags;
571 client->name_appeared_handler = name_appeared_handler;
572 client->name_vanished_handler = name_vanished_handler;
573 client->user_data = user_data;
574 client->user_data_free_func = user_data_free_func;
575 client->main_context = g_main_context_ref_thread_default ();
577 if (map_id_to_client == NULL)
579 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
581 g_hash_table_insert (map_id_to_client,
582 GUINT_TO_POINTER (client->id),
583 client);
585 g_bus_get (bus_type,
586 NULL,
587 connection_get_cb,
588 client_ref (client));
590 G_UNLOCK (lock);
592 return client->id;
596 * g_bus_watch_name_on_connection:
597 * @connection: A #GDBusConnection.
598 * @name: The name (well-known or unique) to watch.
599 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
600 * @name_appeared_handler: (nullable): Handler to invoke when @name is known to exist or %NULL.
601 * @name_vanished_handler: (nullable): Handler to invoke when @name is known to not exist or %NULL.
602 * @user_data: User data to pass to handlers.
603 * @user_data_free_func: (nullable): Function for freeing @user_data or %NULL.
605 * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
606 * #GBusType.
608 * Returns: An identifier (never 0) that an be used with
609 * g_bus_unwatch_name() to stop watching the name.
611 * Since: 2.26
613 guint g_bus_watch_name_on_connection (GDBusConnection *connection,
614 const gchar *name,
615 GBusNameWatcherFlags flags,
616 GBusNameAppearedCallback name_appeared_handler,
617 GBusNameVanishedCallback name_vanished_handler,
618 gpointer user_data,
619 GDestroyNotify user_data_free_func)
621 Client *client;
623 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
624 g_return_val_if_fail (g_dbus_is_name (name), 0);
626 G_LOCK (lock);
628 client = g_new0 (Client, 1);
629 client->ref_count = 1;
630 client->id = g_atomic_int_add (&next_global_id, 1); /* TODO: uh oh, handle overflow */
631 client->name = g_strdup (name);
632 client->flags = flags;
633 client->name_appeared_handler = name_appeared_handler;
634 client->name_vanished_handler = name_vanished_handler;
635 client->user_data = user_data;
636 client->user_data_free_func = user_data_free_func;
637 client->main_context = g_main_context_ref_thread_default ();
639 if (map_id_to_client == NULL)
640 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
642 g_hash_table_insert (map_id_to_client,
643 GUINT_TO_POINTER (client->id),
644 client);
646 client->connection = g_object_ref (connection);
647 G_UNLOCK (lock);
649 has_connection (client);
651 return client->id;
654 typedef struct {
655 GClosure *name_appeared_closure;
656 GClosure *name_vanished_closure;
657 } WatchNameData;
659 static WatchNameData *
660 watch_name_data_new (GClosure *name_appeared_closure,
661 GClosure *name_vanished_closure)
663 WatchNameData *data;
665 data = g_new0 (WatchNameData, 1);
667 if (name_appeared_closure != NULL)
669 data->name_appeared_closure = g_closure_ref (name_appeared_closure);
670 g_closure_sink (name_appeared_closure);
671 if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure))
672 g_closure_set_marshal (name_appeared_closure, g_cclosure_marshal_generic);
675 if (name_vanished_closure != NULL)
677 data->name_vanished_closure = g_closure_ref (name_vanished_closure);
678 g_closure_sink (name_vanished_closure);
679 if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure))
680 g_closure_set_marshal (name_vanished_closure, g_cclosure_marshal_generic);
683 return data;
686 static void
687 watch_with_closures_on_name_appeared (GDBusConnection *connection,
688 const gchar *name,
689 const gchar *name_owner,
690 gpointer user_data)
692 WatchNameData *data = user_data;
693 GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
695 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
696 g_value_set_object (&params[0], connection);
698 g_value_init (&params[1], G_TYPE_STRING);
699 g_value_set_string (&params[1], name);
701 g_value_init (&params[2], G_TYPE_STRING);
702 g_value_set_string (&params[2], name_owner);
704 g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
706 g_value_unset (params + 0);
707 g_value_unset (params + 1);
708 g_value_unset (params + 2);
711 static void
712 watch_with_closures_on_name_vanished (GDBusConnection *connection,
713 const gchar *name,
714 gpointer user_data)
716 WatchNameData *data = user_data;
717 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
719 g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
720 g_value_set_object (&params[0], connection);
722 g_value_init (&params[1], G_TYPE_STRING);
723 g_value_set_string (&params[1], name);
725 g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
727 g_value_unset (params + 0);
728 g_value_unset (params + 1);
731 static void
732 bus_watch_name_free_func (gpointer user_data)
734 WatchNameData *data = user_data;
736 if (data->name_appeared_closure != NULL)
737 g_closure_unref (data->name_appeared_closure);
739 if (data->name_vanished_closure != NULL)
740 g_closure_unref (data->name_vanished_closure);
742 g_free (data);
746 * g_bus_watch_name_with_closures: (rename-to g_bus_watch_name)
747 * @bus_type: The type of bus to watch a name on.
748 * @name: The name (well-known or unique) to watch.
749 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
750 * @name_appeared_closure: (nullable): #GClosure to invoke when @name is known
751 * to exist or %NULL.
752 * @name_vanished_closure: (nullable): #GClosure to invoke when @name is known
753 * to not exist or %NULL.
755 * Version of g_bus_watch_name() using closures instead of callbacks for
756 * easier binding in other languages.
758 * Returns: An identifier (never 0) that an be used with
759 * g_bus_unwatch_name() to stop watching the name.
761 * Since: 2.26
763 guint
764 g_bus_watch_name_with_closures (GBusType bus_type,
765 const gchar *name,
766 GBusNameWatcherFlags flags,
767 GClosure *name_appeared_closure,
768 GClosure *name_vanished_closure)
770 return g_bus_watch_name (bus_type,
771 name,
772 flags,
773 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
774 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
775 watch_name_data_new (name_appeared_closure, name_vanished_closure),
776 bus_watch_name_free_func);
780 * g_bus_watch_name_on_connection_with_closures: (rename-to g_bus_watch_name_on_connection)
781 * @connection: A #GDBusConnection.
782 * @name: The name (well-known or unique) to watch.
783 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
784 * @name_appeared_closure: (nullable): #GClosure to invoke when @name is known
785 * to exist or %NULL.
786 * @name_vanished_closure: (nullable): #GClosure to invoke when @name is known
787 * to not exist or %NULL.
789 * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
790 * easier binding in other languages.
792 * Returns: An identifier (never 0) that an be used with
793 * g_bus_unwatch_name() to stop watching the name.
795 * Since: 2.26
797 guint g_bus_watch_name_on_connection_with_closures (
798 GDBusConnection *connection,
799 const gchar *name,
800 GBusNameWatcherFlags flags,
801 GClosure *name_appeared_closure,
802 GClosure *name_vanished_closure)
804 return g_bus_watch_name_on_connection (connection,
805 name,
806 flags,
807 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
808 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
809 watch_name_data_new (name_appeared_closure, name_vanished_closure),
810 bus_watch_name_free_func);
814 * g_bus_unwatch_name:
815 * @watcher_id: An identifier obtained from g_bus_watch_name()
817 * Stops watching a name.
819 * Since: 2.26
821 void
822 g_bus_unwatch_name (guint watcher_id)
824 Client *client;
826 g_return_if_fail (watcher_id > 0);
828 client = NULL;
830 G_LOCK (lock);
831 if (watcher_id == 0 ||
832 map_id_to_client == NULL ||
833 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
835 g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
836 goto out;
839 client->cancelled = TRUE;
840 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
842 out:
843 G_UNLOCK (lock);
845 /* do callback without holding lock */
846 if (client != NULL)
848 client_unref (client);