Convert remaining examples to links
[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 static guint next_global_id = 1;
81 static GHashTable *map_id_to_client = NULL;
82
83 static Client *
84 client_ref (Client *client)
85 {
86   g_atomic_int_inc (&client->ref_count);
87   return client;
88 }
89
90 static void
91 client_unref (Client *client)
92 {
93   if (g_atomic_int_dec_and_test (&client->ref_count))
94     {
95       if (client->connection != NULL)
96         {
97           if (client->name_owner_changed_subscription_id > 0)
98             g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
99           if (client->disconnected_signal_handler_id > 0)
100             g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
101           g_object_unref (client->connection);
102         }
103       g_free (client->name);
104       g_free (client->name_owner);
105       g_main_context_unref (client->main_context);
106       if (client->user_data_free_func != NULL)
107         client->user_data_free_func (client->user_data);
108       g_free (client);
109     }
110 }
111
112 /* ---------------------------------------------------------------------------------------------------- */
113
114 typedef enum
115 {
116   CALL_TYPE_NAME_APPEARED,
117   CALL_TYPE_NAME_VANISHED
118 } CallType;
119
120 typedef struct
121 {
122   Client *client;
123
124   /* keep this separate because client->connection may
125    * be set to NULL after scheduling the call
126    */
127   GDBusConnection *connection;
128
129   /* ditto */
130   gchar *name_owner;
131
132   CallType call_type;
133 } CallHandlerData;
134
135 static void
136 call_handler_data_free (CallHandlerData *data)
137 {
138   if (data->connection != NULL)
139     g_object_unref (data->connection);
140   g_free (data->name_owner);
141   client_unref (data->client);
142   g_free (data);
143 }
144
145 static void
146 actually_do_call (Client *client, GDBusConnection *connection, const gchar *name_owner, CallType call_type)
147 {
148   switch (call_type)
149     {
150     case CALL_TYPE_NAME_APPEARED:
151       if (client->name_appeared_handler != NULL)
152         {
153           client->name_appeared_handler (connection,
154                                          client->name,
155                                          name_owner,
156                                          client->user_data);
157         }
158       break;
159
160     case CALL_TYPE_NAME_VANISHED:
161       if (client->name_vanished_handler != NULL)
162         {
163           client->name_vanished_handler (connection,
164                                          client->name,
165                                          client->user_data);
166         }
167       break;
168
169     default:
170       g_assert_not_reached ();
171       break;
172     }
173 }
174
175 static gboolean
176 call_in_idle_cb (gpointer _data)
177 {
178   CallHandlerData *data = _data;
179   actually_do_call (data->client, data->connection, data->name_owner, data->call_type);
180   return FALSE;
181 }
182
183 static void
184 schedule_call_in_idle (Client *client, CallType call_type)
185 {
186   CallHandlerData *data;
187   GSource *idle_source;
188
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->name_owner = g_strdup (client->name_owner);
193   data->call_type = call_type;
194
195   idle_source = g_idle_source_new ();
196   g_source_set_priority (idle_source, G_PRIORITY_HIGH);
197   g_source_set_callback (idle_source,
198                          call_in_idle_cb,
199                          data,
200                          (GDestroyNotify) call_handler_data_free);
201   g_source_attach (idle_source, client->main_context);
202   g_source_unref (idle_source);
203 }
204
205 static void
206 do_call (Client *client, CallType call_type)
207 {
208   GMainContext *current_context;
209
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, client->name_owner, call_type);
216   g_main_context_unref (current_context);
217 }
218
219 static void
220 call_appeared_handler (Client *client)
221 {
222   if (client->previous_call != PREVIOUS_CALL_APPEARED)
223     {
224       client->previous_call = PREVIOUS_CALL_APPEARED;
225       if (!client->cancelled && client->name_appeared_handler != NULL)
226         {
227           do_call (client, CALL_TYPE_NAME_APPEARED);
228         }
229     }
230 }
231
232 static void
233 call_vanished_handler (Client  *client,
234                        gboolean ignore_cancelled)
235 {
236   if (client->previous_call != PREVIOUS_CALL_VANISHED)
237     {
238       client->previous_call = PREVIOUS_CALL_VANISHED;
239       if (((!client->cancelled) || ignore_cancelled) && client->name_vanished_handler != NULL)
240         {
241           do_call (client, CALL_TYPE_NAME_VANISHED);
242         }
243     }
244 }
245
246 /* ---------------------------------------------------------------------------------------------------- */
247
248 static void
249 on_connection_disconnected (GDBusConnection *connection,
250                             gboolean         remote_peer_vanished,
251                             GError          *error,
252                             gpointer         user_data)
253 {
254   Client *client = user_data;
255
256   if (client->name_owner_changed_subscription_id > 0)
257     g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
258   if (client->disconnected_signal_handler_id > 0)
259     g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
260   g_object_unref (client->connection);
261   client->disconnected_signal_handler_id = 0;
262   client->name_owner_changed_subscription_id = 0;
263   client->connection = NULL;
264
265   call_vanished_handler (client, FALSE);
266 }
267
268 /* ---------------------------------------------------------------------------------------------------- */
269
270 static void
271 on_name_owner_changed (GDBusConnection *connection,
272                        const gchar      *sender_name,
273                        const gchar      *object_path,
274                        const gchar      *interface_name,
275                        const gchar      *signal_name,
276                        GVariant         *parameters,
277                        gpointer          user_data)
278 {
279   Client *client = user_data;
280   const gchar *name;
281   const gchar *old_owner;
282   const gchar *new_owner;
283
284   if (!client->initialized)
285     goto out;
286
287   if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
288       g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
289       g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
290     goto out;
291
292   g_variant_get (parameters,
293                  "(&s&s&s)",
294                  &name,
295                  &old_owner,
296                  &new_owner);
297
298   /* we only care about a specific name */
299   if (g_strcmp0 (name, client->name) != 0)
300     goto out;
301
302   if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
303     {
304       g_free (client->name_owner);
305       client->name_owner = NULL;
306       call_vanished_handler (client, FALSE);
307     }
308
309   if (new_owner != NULL && strlen (new_owner) > 0)
310     {
311       g_warn_if_fail (client->name_owner == NULL);
312       g_free (client->name_owner);
313       client->name_owner = g_strdup (new_owner);
314       call_appeared_handler (client);
315     }
316
317  out:
318   ;
319 }
320
321 /* ---------------------------------------------------------------------------------------------------- */
322
323 static void
324 get_name_owner_cb (GObject      *source_object,
325                    GAsyncResult *res,
326                    gpointer      user_data)
327 {
328   Client *client = user_data;
329   GVariant *result;
330   const char *name_owner;
331
332   name_owner = NULL;
333   result = NULL;
334
335   result = g_dbus_connection_call_finish (client->connection,
336                                           res,
337                                           NULL);
338   if (result != NULL)
339     {
340       g_variant_get (result, "(&s)", &name_owner);
341     }
342
343   if (name_owner != NULL)
344     {
345       g_warn_if_fail (client->name_owner == NULL);
346       client->name_owner = g_strdup (name_owner);
347       call_appeared_handler (client);
348     }
349   else
350     {
351       call_vanished_handler (client, FALSE);
352     }
353
354   client->initialized = TRUE;
355
356   if (result != NULL)
357     g_variant_unref (result);
358   client_unref (client);
359 }
360
361 /* ---------------------------------------------------------------------------------------------------- */
362
363 static void
364 invoke_get_name_owner (Client *client)
365 {
366   g_dbus_connection_call (client->connection,
367                           "org.freedesktop.DBus",  /* bus name */
368                           "/org/freedesktop/DBus", /* object path */
369                           "org.freedesktop.DBus",  /* interface name */
370                           "GetNameOwner",          /* method name */
371                           g_variant_new ("(s)", client->name),
372                           G_VARIANT_TYPE ("(s)"),
373                           G_DBUS_CALL_FLAGS_NONE,
374                           -1,
375                           NULL,
376                           (GAsyncReadyCallback) get_name_owner_cb,
377                           client_ref (client));
378 }
379
380 /* ---------------------------------------------------------------------------------------------------- */
381
382 static void
383 start_service_by_name_cb (GObject      *source_object,
384                           GAsyncResult *res,
385                           gpointer      user_data)
386 {
387   Client *client = user_data;
388   GVariant *result;
389
390   result = NULL;
391
392   result = g_dbus_connection_call_finish (client->connection,
393                                           res,
394                                           NULL);
395   if (result != NULL)
396     {
397       guint32 start_service_result;
398       g_variant_get (result, "(u)", &start_service_result);
399
400       if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
401         {
402           invoke_get_name_owner (client);
403         }
404       else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
405         {
406           invoke_get_name_owner (client);
407         }
408       else
409         {
410           g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
411           call_vanished_handler (client, FALSE);
412           client->initialized = TRUE;
413         }
414     }
415   else
416     {
417       /* Errors are not unexpected; the bus will reply e.g.
418        *
419        *   org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
420        *   was not provided by any .service files
421        *
422        * This doesn't mean that the name doesn't have an owner, just
423        * that it's not provided by a .service file. So proceed to
424        * invoke GetNameOwner().
425        */
426       invoke_get_name_owner (client);
427     }
428
429   if (result != NULL)
430     g_variant_unref (result);
431   client_unref (client);
432 }
433
434 /* ---------------------------------------------------------------------------------------------------- */
435
436 static void
437 has_connection (Client *client)
438 {
439   /* listen for disconnection */
440   client->disconnected_signal_handler_id = g_signal_connect (client->connection,
441                                                              "closed",
442                                                              G_CALLBACK (on_connection_disconnected),
443                                                              client);
444
445   /* start listening to NameOwnerChanged messages immediately */
446   client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
447                                                                                    "org.freedesktop.DBus",  /* name */
448                                                                                    "org.freedesktop.DBus",  /* if */
449                                                                                    "NameOwnerChanged",      /* signal */
450                                                                                    "/org/freedesktop/DBus", /* path */
451                                                                                    client->name,
452                                                                                    G_DBUS_SIGNAL_FLAGS_NONE,
453                                                                                    on_name_owner_changed,
454                                                                                    client,
455                                                                                    NULL);
456
457   if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
458     {
459       g_dbus_connection_call (client->connection,
460                               "org.freedesktop.DBus",  /* bus name */
461                               "/org/freedesktop/DBus", /* object path */
462                               "org.freedesktop.DBus",  /* interface name */
463                               "StartServiceByName",    /* method name */
464                               g_variant_new ("(su)", client->name, 0),
465                               G_VARIANT_TYPE ("(u)"),
466                               G_DBUS_CALL_FLAGS_NONE,
467                               -1,
468                               NULL,
469                               (GAsyncReadyCallback) start_service_by_name_cb,
470                               client_ref (client));
471     }
472   else
473     {
474       /* check owner */
475       invoke_get_name_owner (client);
476     }
477 }
478
479
480 static void
481 connection_get_cb (GObject      *source_object,
482                    GAsyncResult *res,
483                    gpointer      user_data)
484 {
485   Client *client = user_data;
486
487   client->connection = g_bus_get_finish (res, NULL);
488   if (client->connection == NULL)
489     {
490       call_vanished_handler (client, FALSE);
491       goto out;
492     }
493
494   has_connection (client);
495
496  out:
497   client_unref (client);
498 }
499
500 /* ---------------------------------------------------------------------------------------------------- */
501
502 /**
503  * g_bus_watch_name:
504  * @bus_type: The type of bus to watch a name on.
505  * @name: The name (well-known or unique) to watch.
506  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
507  * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
508  * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
509  * @user_data: User data to pass to handlers.
510  * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
511  *
512  * Starts watching @name on the bus specified by @bus_type and calls
513  * @name_appeared_handler and @name_vanished_handler when the name is
514  * known to have a owner respectively known to lose its
515  * owner. Callbacks will be invoked in the
516  * [thread-default main context][g-main-context-push-thread-default]
517  * of the thread you are calling this function from.
518  *
519  * You are guaranteed that one of the handlers will be invoked after
520  * calling this function. When you are done watching the name, just
521  * call g_bus_unwatch_name() with the watcher id this function
522  * returns.
523  *
524  * If the name vanishes or appears (for example the application owning
525  * the name could restart), the handlers are also invoked. If the
526  * #GDBusConnection that is used for watching the name disconnects, then
527  * @name_vanished_handler is invoked since it is no longer
528  * possible to access the name.
529  *
530  * Another guarantee is that invocations of @name_appeared_handler
531  * and @name_vanished_handler are guaranteed to alternate; that
532  * is, if @name_appeared_handler is invoked then you are
533  * guaranteed that the next time one of the handlers is invoked, it
534  * will be @name_vanished_handler. The reverse is also true.
535  *
536  * This behavior makes it very simple to write applications that want
537  * to take action when a certain [name exists][gdbus-watching-names].
538  * Basically, the application should create object proxies in
539  * @name_appeared_handler and destroy them again (if any) in
540  * @name_vanished_handler.
541  *
542  * Returns: An identifier (never 0) that an be used with
543  * g_bus_unwatch_name() to stop watching the name.
544  *
545  * Since: 2.26
546  */
547 guint
548 g_bus_watch_name (GBusType                  bus_type,
549                   const gchar              *name,
550                   GBusNameWatcherFlags      flags,
551                   GBusNameAppearedCallback  name_appeared_handler,
552                   GBusNameVanishedCallback  name_vanished_handler,
553                   gpointer                  user_data,
554                   GDestroyNotify            user_data_free_func)
555 {
556   Client *client;
557
558   g_return_val_if_fail (g_dbus_is_name (name), 0);
559
560   G_LOCK (lock);
561
562   client = g_new0 (Client, 1);
563   client->ref_count = 1;
564   client->id = next_global_id++; /* TODO: uh oh, handle overflow */
565   client->name = g_strdup (name);
566   client->flags = flags;
567   client->name_appeared_handler = name_appeared_handler;
568   client->name_vanished_handler = name_vanished_handler;
569   client->user_data = user_data;
570   client->user_data_free_func = user_data_free_func;
571   client->main_context = g_main_context_ref_thread_default ();
572
573   if (map_id_to_client == NULL)
574     {
575       map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
576     }
577   g_hash_table_insert (map_id_to_client,
578                        GUINT_TO_POINTER (client->id),
579                        client);
580
581   g_bus_get (bus_type,
582              NULL,
583              connection_get_cb,
584              client_ref (client));
585
586   G_UNLOCK (lock);
587
588   return client->id;
589 }
590
591 /**
592  * g_bus_watch_name_on_connection:
593  * @connection: A #GDBusConnection.
594  * @name: The name (well-known or unique) to watch.
595  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
596  * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
597  * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
598  * @user_data: User data to pass to handlers.
599  * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
600  *
601  * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
602  * #GBusType.
603  *
604  * Returns: An identifier (never 0) that an be used with
605  * g_bus_unwatch_name() to stop watching the name.
606  *
607  * Since: 2.26
608  */
609 guint g_bus_watch_name_on_connection (GDBusConnection          *connection,
610                                       const gchar              *name,
611                                       GBusNameWatcherFlags      flags,
612                                       GBusNameAppearedCallback  name_appeared_handler,
613                                       GBusNameVanishedCallback  name_vanished_handler,
614                                       gpointer                  user_data,
615                                       GDestroyNotify            user_data_free_func)
616 {
617   Client *client;
618
619   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
620   g_return_val_if_fail (g_dbus_is_name (name), 0);
621
622   G_LOCK (lock);
623
624   client = g_new0 (Client, 1);
625   client->ref_count = 1;
626   client->id = next_global_id++; /* TODO: uh oh, handle overflow */
627   client->name = g_strdup (name);
628   client->flags = flags;
629   client->name_appeared_handler = name_appeared_handler;
630   client->name_vanished_handler = name_vanished_handler;
631   client->user_data = user_data;
632   client->user_data_free_func = user_data_free_func;
633   client->main_context = g_main_context_ref_thread_default ();
634
635   if (map_id_to_client == NULL)
636     map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
637
638   g_hash_table_insert (map_id_to_client,
639                        GUINT_TO_POINTER (client->id),
640                        client);
641
642   client->connection = g_object_ref (connection);
643   G_UNLOCK (lock);
644
645   has_connection (client);
646
647   return client->id;
648 }
649
650 typedef struct {
651   GClosure *name_appeared_closure;
652   GClosure *name_vanished_closure;
653 } WatchNameData;
654
655 static WatchNameData *
656 watch_name_data_new (GClosure *name_appeared_closure,
657                      GClosure *name_vanished_closure)
658 {
659   WatchNameData *data;
660
661   data = g_new0 (WatchNameData, 1);
662
663   if (name_appeared_closure != NULL)
664     {
665       data->name_appeared_closure = g_closure_ref (name_appeared_closure);
666       g_closure_sink (name_appeared_closure);
667       if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure))
668         g_closure_set_marshal (name_appeared_closure, g_cclosure_marshal_generic);
669     }
670
671   if (name_vanished_closure != NULL)
672     {
673       data->name_vanished_closure = g_closure_ref (name_vanished_closure);
674       g_closure_sink (name_vanished_closure);
675       if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure))
676         g_closure_set_marshal (name_vanished_closure, g_cclosure_marshal_generic);
677     }
678
679   return data;
680 }
681
682 static void
683 watch_with_closures_on_name_appeared (GDBusConnection *connection,
684                                       const gchar     *name,
685                                       const gchar     *name_owner,
686                                       gpointer         user_data)
687 {
688   WatchNameData *data = user_data;
689   GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
690
691   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
692   g_value_set_object (&params[0], connection);
693
694   g_value_init (&params[1], G_TYPE_STRING);
695   g_value_set_string (&params[1], name);
696
697   g_value_init (&params[2], G_TYPE_STRING);
698   g_value_set_string (&params[2], name_owner);
699
700   g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
701
702   g_value_unset (params + 0);
703   g_value_unset (params + 1);
704   g_value_unset (params + 2);
705 }
706
707 static void
708 watch_with_closures_on_name_vanished (GDBusConnection *connection,
709                                       const gchar     *name,
710                                       gpointer         user_data)
711 {
712   WatchNameData *data = user_data;
713   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
714
715   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
716   g_value_set_object (&params[0], connection);
717
718   g_value_init (&params[1], G_TYPE_STRING);
719   g_value_set_string (&params[1], name);
720
721   g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
722
723   g_value_unset (params + 0);
724   g_value_unset (params + 1);
725 }
726
727 static void
728 bus_watch_name_free_func (gpointer user_data)
729 {
730   WatchNameData *data = user_data;
731
732   if (data->name_appeared_closure != NULL)
733     g_closure_unref (data->name_appeared_closure);
734
735   if (data->name_vanished_closure != NULL)
736     g_closure_unref (data->name_vanished_closure);
737
738   g_free (data);
739 }
740
741 /**
742  * g_bus_watch_name_with_closures:
743  * @bus_type: The type of bus to watch a name on.
744  * @name: The name (well-known or unique) to watch.
745  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
746  * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
747  * to exist or %NULL.
748  * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
749  * to not exist or %NULL.
750  *
751  * Version of g_bus_watch_name() using closures instead of callbacks for
752  * easier binding in other languages.
753  *
754  * Returns: An identifier (never 0) that an be used with
755  * g_bus_unwatch_name() to stop watching the name.
756  *
757  * Rename to: g_bus_watch_name
758  *
759  * Since: 2.26
760  */
761 guint
762 g_bus_watch_name_with_closures (GBusType                 bus_type,
763                                 const gchar             *name,
764                                 GBusNameWatcherFlags     flags,
765                                 GClosure                *name_appeared_closure,
766                                 GClosure                *name_vanished_closure)
767 {
768   return g_bus_watch_name (bus_type,
769           name,
770           flags,
771           name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
772           name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
773           watch_name_data_new (name_appeared_closure, name_vanished_closure),
774           bus_watch_name_free_func);
775 }
776
777 /**
778  * g_bus_watch_name_on_connection_with_closures:
779  * @connection: A #GDBusConnection.
780  * @name: The name (well-known or unique) to watch.
781  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
782  * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
783  * to exist or %NULL.
784  * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
785  * to not exist or %NULL.
786  *
787  * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
788  * easier binding in other languages.
789  *
790  * Returns: An identifier (never 0) that an be used with
791  * g_bus_unwatch_name() to stop watching the name.
792  *
793  * Rename to: g_bus_watch_name_on_connection
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 }