Imported Upstream version 2.51.2
[platform/upstream/glib.git] / gio / gdbusnamewatching.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  * Author: David Zeuthen <davidz@redhat.com>
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "gdbusutils.h"
27 #include "gdbusnamewatching.h"
28 #include "gdbuserror.h"
29 #include "gdbusprivate.h"
30 #include "gdbusconnection.h"
31
32 #include "glibintl.h"
33
34 /**
35  * SECTION:gdbusnamewatching
36  * @title: Watching Bus Names
37  * @short_description: Simple API for watching bus names
38  * @include: gio/gio.h
39  *
40  * Convenience API for watching bus names.
41  *
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)
44  */
45
46 G_LOCK_DEFINE_STATIC (lock);
47
48 /* ---------------------------------------------------------------------------------------------------- */
49
50 typedef enum
51 {
52   PREVIOUS_CALL_NONE = 0,
53   PREVIOUS_CALL_APPEARED,
54   PREVIOUS_CALL_VANISHED,
55 } PreviousCall;
56
57 typedef struct
58 {
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;
69
70   GDBusConnection          *connection;
71   gulong                    disconnected_signal_handler_id;
72   guint                     name_owner_changed_subscription_id;
73
74   PreviousCall              previous_call;
75
76   gboolean                  cancelled;
77   gboolean                  initialized;
78 } Client;
79
80 /* Must be accessed atomically. */
81 static volatile guint next_global_id = 1;
82
83 /* Must be accessed with @lock held. */
84 static GHashTable *map_id_to_client = NULL;
85
86 static Client *
87 client_ref (Client *client)
88 {
89   g_atomic_int_inc (&client->ref_count);
90   return client;
91 }
92
93 static void
94 client_unref (Client *client)
95 {
96   if (g_atomic_int_dec_and_test (&client->ref_count))
97     {
98       if (client->connection != NULL)
99         {
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);
105         }
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);
112     }
113 }
114
115 /* ---------------------------------------------------------------------------------------------------- */
116
117 typedef enum
118 {
119   CALL_TYPE_NAME_APPEARED,
120   CALL_TYPE_NAME_VANISHED
121 } CallType;
122
123 typedef struct
124 {
125   Client *client;
126
127   /* keep this separate because client->connection may
128    * be set to NULL after scheduling the call
129    */
130   GDBusConnection *connection;
131
132   /* ditto */
133   gchar *name_owner;
134
135   CallType call_type;
136 } CallHandlerData;
137
138 static void
139 call_handler_data_free (CallHandlerData *data)
140 {
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);
146 }
147
148 static void
149 actually_do_call (Client *client, GDBusConnection *connection, const gchar *name_owner, CallType call_type)
150 {
151   switch (call_type)
152     {
153     case CALL_TYPE_NAME_APPEARED:
154       if (client->name_appeared_handler != NULL)
155         {
156           client->name_appeared_handler (connection,
157                                          client->name,
158                                          name_owner,
159                                          client->user_data);
160         }
161       break;
162
163     case CALL_TYPE_NAME_VANISHED:
164       if (client->name_vanished_handler != NULL)
165         {
166           client->name_vanished_handler (connection,
167                                          client->name,
168                                          client->user_data);
169         }
170       break;
171
172     default:
173       g_assert_not_reached ();
174       break;
175     }
176 }
177
178 static gboolean
179 call_in_idle_cb (gpointer _data)
180 {
181   CallHandlerData *data = _data;
182   actually_do_call (data->client, data->connection, data->name_owner, data->call_type);
183   return FALSE;
184 }
185
186 static void
187 schedule_call_in_idle (Client *client, CallType call_type)
188 {
189   CallHandlerData *data;
190   GSource *idle_source;
191
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;
197
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);
207 }
208
209 static void
210 do_call (Client *client, CallType call_type)
211 {
212   GMainContext *current_context;
213
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);
221 }
222
223 static void
224 call_appeared_handler (Client *client)
225 {
226   if (client->previous_call != PREVIOUS_CALL_APPEARED)
227     {
228       client->previous_call = PREVIOUS_CALL_APPEARED;
229       if (!client->cancelled && client->name_appeared_handler != NULL)
230         {
231           do_call (client, CALL_TYPE_NAME_APPEARED);
232         }
233     }
234 }
235
236 static void
237 call_vanished_handler (Client  *client,
238                        gboolean ignore_cancelled)
239 {
240   if (client->previous_call != PREVIOUS_CALL_VANISHED)
241     {
242       client->previous_call = PREVIOUS_CALL_VANISHED;
243       if (((!client->cancelled) || ignore_cancelled) && client->name_vanished_handler != NULL)
244         {
245           do_call (client, CALL_TYPE_NAME_VANISHED);
246         }
247     }
248 }
249
250 /* ---------------------------------------------------------------------------------------------------- */
251
252 static void
253 on_connection_disconnected (GDBusConnection *connection,
254                             gboolean         remote_peer_vanished,
255                             GError          *error,
256                             gpointer         user_data)
257 {
258   Client *client = user_data;
259
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;
268
269   call_vanished_handler (client, FALSE);
270 }
271
272 /* ---------------------------------------------------------------------------------------------------- */
273
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)
282 {
283   Client *client = user_data;
284   const gchar *name;
285   const gchar *old_owner;
286   const gchar *new_owner;
287
288   if (!client->initialized)
289     goto out;
290
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;
295
296   g_variant_get (parameters,
297                  "(&s&s&s)",
298                  &name,
299                  &old_owner,
300                  &new_owner);
301
302   /* we only care about a specific name */
303   if (g_strcmp0 (name, client->name) != 0)
304     goto out;
305
306   if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
307     {
308       g_free (client->name_owner);
309       client->name_owner = NULL;
310       call_vanished_handler (client, FALSE);
311     }
312
313   if (new_owner != NULL && strlen (new_owner) > 0)
314     {
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);
319     }
320
321  out:
322   ;
323 }
324
325 /* ---------------------------------------------------------------------------------------------------- */
326
327 static void
328 get_name_owner_cb (GObject      *source_object,
329                    GAsyncResult *res,
330                    gpointer      user_data)
331 {
332   Client *client = user_data;
333   GVariant *result;
334   const char *name_owner;
335
336   name_owner = NULL;
337   result = NULL;
338
339   result = g_dbus_connection_call_finish (client->connection,
340                                           res,
341                                           NULL);
342   if (result != NULL)
343     {
344       g_variant_get (result, "(&s)", &name_owner);
345     }
346
347   if (name_owner != NULL)
348     {
349       g_warn_if_fail (client->name_owner == NULL);
350       client->name_owner = g_strdup (name_owner);
351       call_appeared_handler (client);
352     }
353   else
354     {
355       call_vanished_handler (client, FALSE);
356     }
357
358   client->initialized = TRUE;
359
360   if (result != NULL)
361     g_variant_unref (result);
362   client_unref (client);
363 }
364
365 /* ---------------------------------------------------------------------------------------------------- */
366
367 static void
368 invoke_get_name_owner (Client *client)
369 {
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,
378                           -1,
379                           NULL,
380                           (GAsyncReadyCallback) get_name_owner_cb,
381                           client_ref (client));
382 }
383
384 /* ---------------------------------------------------------------------------------------------------- */
385
386 static void
387 start_service_by_name_cb (GObject      *source_object,
388                           GAsyncResult *res,
389                           gpointer      user_data)
390 {
391   Client *client = user_data;
392   GVariant *result;
393
394   result = NULL;
395
396   result = g_dbus_connection_call_finish (client->connection,
397                                           res,
398                                           NULL);
399   if (result != NULL)
400     {
401       guint32 start_service_result;
402       g_variant_get (result, "(u)", &start_service_result);
403
404       if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
405         {
406           invoke_get_name_owner (client);
407         }
408       else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
409         {
410           invoke_get_name_owner (client);
411         }
412       else
413         {
414           g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
415           call_vanished_handler (client, FALSE);
416           client->initialized = TRUE;
417         }
418     }
419   else
420     {
421       /* Errors are not unexpected; the bus will reply e.g.
422        *
423        *   org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
424        *   was not provided by any .service files
425        *
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().
429        */
430       invoke_get_name_owner (client);
431     }
432
433   if (result != NULL)
434     g_variant_unref (result);
435   client_unref (client);
436 }
437
438 /* ---------------------------------------------------------------------------------------------------- */
439
440 static void
441 has_connection (Client *client)
442 {
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);
448
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);
460
461   if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
462     {
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,
471                               -1,
472                               NULL,
473                               (GAsyncReadyCallback) start_service_by_name_cb,
474                               client_ref (client));
475     }
476   else
477     {
478       /* check owner */
479       invoke_get_name_owner (client);
480     }
481 }
482
483
484 static void
485 connection_get_cb (GObject      *source_object,
486                    GAsyncResult *res,
487                    gpointer      user_data)
488 {
489   Client *client = user_data;
490
491   client->connection = g_bus_get_finish (res, NULL);
492   if (client->connection == NULL)
493     {
494       call_vanished_handler (client, FALSE);
495       goto out;
496     }
497
498   has_connection (client);
499
500  out:
501   client_unref (client);
502 }
503
504 /* ---------------------------------------------------------------------------------------------------- */
505
506 /**
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.
515  *
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.
522  *
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.
527  *
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.
533  *
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.
539  *
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.
545  *
546  * Returns: An identifier (never 0) that an be used with
547  * g_bus_unwatch_name() to stop watching the name.
548  *
549  * Since: 2.26
550  */
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)
559 {
560   Client *client;
561
562   g_return_val_if_fail (g_dbus_is_name (name), 0);
563
564   G_LOCK (lock);
565
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 ();
576
577   if (map_id_to_client == NULL)
578     {
579       map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
580     }
581   g_hash_table_insert (map_id_to_client,
582                        GUINT_TO_POINTER (client->id),
583                        client);
584
585   g_bus_get (bus_type,
586              NULL,
587              connection_get_cb,
588              client_ref (client));
589
590   G_UNLOCK (lock);
591
592   return client->id;
593 }
594
595 /**
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.
604  *
605  * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
606  * #GBusType.
607  *
608  * Returns: An identifier (never 0) that an be used with
609  * g_bus_unwatch_name() to stop watching the name.
610  *
611  * Since: 2.26
612  */
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)
620 {
621   Client *client;
622
623   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
624   g_return_val_if_fail (g_dbus_is_name (name), 0);
625
626   G_LOCK (lock);
627
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 ();
638
639   if (map_id_to_client == NULL)
640     map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
641
642   g_hash_table_insert (map_id_to_client,
643                        GUINT_TO_POINTER (client->id),
644                        client);
645
646   client->connection = g_object_ref (connection);
647   G_UNLOCK (lock);
648
649   has_connection (client);
650
651   return client->id;
652 }
653
654 typedef struct {
655   GClosure *name_appeared_closure;
656   GClosure *name_vanished_closure;
657 } WatchNameData;
658
659 static WatchNameData *
660 watch_name_data_new (GClosure *name_appeared_closure,
661                      GClosure *name_vanished_closure)
662 {
663   WatchNameData *data;
664
665   data = g_new0 (WatchNameData, 1);
666
667   if (name_appeared_closure != NULL)
668     {
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);
673     }
674
675   if (name_vanished_closure != NULL)
676     {
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);
681     }
682
683   return data;
684 }
685
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)
691 {
692   WatchNameData *data = user_data;
693   GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
694
695   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
696   g_value_set_object (&params[0], connection);
697
698   g_value_init (&params[1], G_TYPE_STRING);
699   g_value_set_string (&params[1], name);
700
701   g_value_init (&params[2], G_TYPE_STRING);
702   g_value_set_string (&params[2], name_owner);
703
704   g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
705
706   g_value_unset (params + 0);
707   g_value_unset (params + 1);
708   g_value_unset (params + 2);
709 }
710
711 static void
712 watch_with_closures_on_name_vanished (GDBusConnection *connection,
713                                       const gchar     *name,
714                                       gpointer         user_data)
715 {
716   WatchNameData *data = user_data;
717   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
718
719   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
720   g_value_set_object (&params[0], connection);
721
722   g_value_init (&params[1], G_TYPE_STRING);
723   g_value_set_string (&params[1], name);
724
725   g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
726
727   g_value_unset (params + 0);
728   g_value_unset (params + 1);
729 }
730
731 static void
732 bus_watch_name_free_func (gpointer user_data)
733 {
734   WatchNameData *data = user_data;
735
736   if (data->name_appeared_closure != NULL)
737     g_closure_unref (data->name_appeared_closure);
738
739   if (data->name_vanished_closure != NULL)
740     g_closure_unref (data->name_vanished_closure);
741
742   g_free (data);
743 }
744
745 /**
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.
754  *
755  * Version of g_bus_watch_name() using closures instead of callbacks for
756  * easier binding in other languages.
757  *
758  * Returns: An identifier (never 0) that an be used with
759  * g_bus_unwatch_name() to stop watching the name.
760  *
761  * Since: 2.26
762  */
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)
769 {
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);
777 }
778
779 /**
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.
788  *
789  * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
790  * easier binding in other languages.
791  *
792  * Returns: An identifier (never 0) that an be used with
793  * g_bus_unwatch_name() to stop watching the name.
794  *
795  * Since: 2.26
796  */
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)
803 {
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);
811 }
812
813 /**
814  * g_bus_unwatch_name:
815  * @watcher_id: An identifier obtained from g_bus_watch_name()
816  *
817  * Stops watching a name.
818  *
819  * Since: 2.26
820  */
821 void
822 g_bus_unwatch_name (guint watcher_id)
823 {
824   Client *client;
825
826   g_return_if_fail (watcher_id > 0);
827
828   client = NULL;
829
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)
834     {
835       g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
836       goto out;
837     }
838
839   client->cancelled = TRUE;
840   g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
841
842  out:
843   G_UNLOCK (lock);
844
845   /* do callback without holding lock */
846   if (client != NULL)
847     {
848       client_unref (client);
849     }
850 }