feb09ddc5376ac3a93364ca7bb5f9b8303e93f73
[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_set_name (idle_source, "[gio] call_in_idle_cb");
202   g_source_attach (idle_source, client->main_context);
203   g_source_unref (idle_source);
204 }
205
206 static void
207 do_call (Client *client, CallType call_type)
208 {
209   GMainContext *current_context;
210
211   /* only schedule in idle if we're not in the right thread */
212   current_context = g_main_context_ref_thread_default ();
213   if (current_context != client->main_context)
214     schedule_call_in_idle (client, call_type);
215   else
216     actually_do_call (client, client->connection, client->name_owner, call_type);
217   g_main_context_unref (current_context);
218 }
219
220 static void
221 call_appeared_handler (Client *client)
222 {
223   if (client->previous_call != PREVIOUS_CALL_APPEARED)
224     {
225       client->previous_call = PREVIOUS_CALL_APPEARED;
226       if (!client->cancelled && client->name_appeared_handler != NULL)
227         {
228           do_call (client, CALL_TYPE_NAME_APPEARED);
229         }
230     }
231 }
232
233 static void
234 call_vanished_handler (Client  *client,
235                        gboolean ignore_cancelled)
236 {
237   if (client->previous_call != PREVIOUS_CALL_VANISHED)
238     {
239       client->previous_call = PREVIOUS_CALL_VANISHED;
240       if (((!client->cancelled) || ignore_cancelled) && client->name_vanished_handler != NULL)
241         {
242           do_call (client, CALL_TYPE_NAME_VANISHED);
243         }
244     }
245 }
246
247 /* ---------------------------------------------------------------------------------------------------- */
248
249 static void
250 on_connection_disconnected (GDBusConnection *connection,
251                             gboolean         remote_peer_vanished,
252                             GError          *error,
253                             gpointer         user_data)
254 {
255   Client *client = user_data;
256
257   if (client->name_owner_changed_subscription_id > 0)
258     g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
259   if (client->disconnected_signal_handler_id > 0)
260     g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
261   g_object_unref (client->connection);
262   client->disconnected_signal_handler_id = 0;
263   client->name_owner_changed_subscription_id = 0;
264   client->connection = NULL;
265
266   call_vanished_handler (client, FALSE);
267 }
268
269 /* ---------------------------------------------------------------------------------------------------- */
270
271 static void
272 on_name_owner_changed (GDBusConnection *connection,
273                        const gchar      *sender_name,
274                        const gchar      *object_path,
275                        const gchar      *interface_name,
276                        const gchar      *signal_name,
277                        GVariant         *parameters,
278                        gpointer          user_data)
279 {
280   Client *client = user_data;
281   const gchar *name;
282   const gchar *old_owner;
283   const gchar *new_owner;
284
285   if (!client->initialized)
286     goto out;
287
288   if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
289       g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
290       g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
291     goto out;
292
293   g_variant_get (parameters,
294                  "(&s&s&s)",
295                  &name,
296                  &old_owner,
297                  &new_owner);
298
299   /* we only care about a specific name */
300   if (g_strcmp0 (name, client->name) != 0)
301     goto out;
302
303   if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
304     {
305       g_free (client->name_owner);
306       client->name_owner = NULL;
307       call_vanished_handler (client, FALSE);
308     }
309
310   if (new_owner != NULL && strlen (new_owner) > 0)
311     {
312       g_warn_if_fail (client->name_owner == NULL);
313       g_free (client->name_owner);
314       client->name_owner = g_strdup (new_owner);
315       call_appeared_handler (client);
316     }
317
318  out:
319   ;
320 }
321
322 /* ---------------------------------------------------------------------------------------------------- */
323
324 static void
325 process_get_name_owner (Client      *client,
326                         const char  *name_owner)
327 {
328   if (name_owner != NULL)
329     {
330       g_warn_if_fail (client->name_owner == NULL);
331       client->name_owner = g_strdup (name_owner);
332       call_appeared_handler (client);
333     }
334   else
335     call_vanished_handler (client, FALSE);
336
337   client->initialized = TRUE;
338 }
339
340 static void
341 get_name_owner_cb (GObject      *source_object,
342                    GAsyncResult *res,
343                    gpointer      user_data)
344 {
345   Client *client = user_data;
346   GVariant *result;
347   const char *name_owner;
348
349   name_owner = NULL;
350   result = NULL;
351
352   result = g_dbus_connection_call_finish (client->connection,
353                                           res,
354                                           NULL);
355   if (result != NULL)
356     {
357       g_variant_get (result, "(&s)", &name_owner);
358     }
359
360   process_get_name_owner (client, name_owner);
361
362   if (result != NULL)
363     g_variant_unref (result);
364   client_unref (client);
365 }
366
367 /* ---------------------------------------------------------------------------------------------------- */
368
369 static void
370 invoke_get_name_owner (Client *client)
371 {
372   if (_g_dbus_connection_is_kdbus (client->connection))
373     {
374       char *name_owner;
375
376       name_owner = g_dbus_get_name_owner (client->connection,
377                                           client->name,
378                                           NULL);
379       process_get_name_owner (client, name_owner);
380       g_free (name_owner);
381     }
382   else
383     {
384       g_dbus_connection_call (client->connection,
385                               "org.freedesktop.DBus",  /* bus name */
386                               "/org/freedesktop/DBus", /* object path */
387                               "org.freedesktop.DBus",  /* interface name */
388                               "GetNameOwner",          /* method name */
389                               g_variant_new ("(s)", client->name),
390                               G_VARIANT_TYPE ("(s)"),
391                               G_DBUS_CALL_FLAGS_NONE,
392                               -1,
393                               NULL,
394                               (GAsyncReadyCallback) get_name_owner_cb,
395                               client_ref (client));
396     }
397 }
398
399 /* ---------------------------------------------------------------------------------------------------- */
400
401 static void
402 start_service_by_name_cb (GObject      *source_object,
403                           GAsyncResult *res,
404                           gpointer      user_data)
405 {
406   Client *client = user_data;
407   GVariant *result;
408
409   result = NULL;
410
411   result = g_dbus_connection_call_finish (client->connection,
412                                           res,
413                                           NULL);
414   if (result != NULL)
415     {
416       guint32 start_service_result;
417       g_variant_get (result, "(u)", &start_service_result);
418
419       if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
420         {
421           invoke_get_name_owner (client);
422         }
423       else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
424         {
425           invoke_get_name_owner (client);
426         }
427       else
428         {
429           g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
430           call_vanished_handler (client, FALSE);
431           client->initialized = TRUE;
432         }
433     }
434   else
435     {
436       /* Errors are not unexpected; the bus will reply e.g.
437        *
438        *   org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
439        *   was not provided by any .service files
440        *
441        * This doesn't mean that the name doesn't have an owner, just
442        * that it's not provided by a .service file. So proceed to
443        * invoke GetNameOwner().
444        */
445       invoke_get_name_owner (client);
446     }
447
448   if (result != NULL)
449     g_variant_unref (result);
450   client_unref (client);
451 }
452
453 /* ---------------------------------------------------------------------------------------------------- */
454
455 static void
456 has_connection (Client *client)
457 {
458   /* listen for disconnection */
459   client->disconnected_signal_handler_id = g_signal_connect (client->connection,
460                                                              "closed",
461                                                              G_CALLBACK (on_connection_disconnected),
462                                                              client);
463
464   /* start listening to NameOwnerChanged messages immediately */
465   client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
466                                                                                    "org.freedesktop.DBus",  /* name */
467                                                                                    "org.freedesktop.DBus",  /* if */
468                                                                                    "NameOwnerChanged",      /* signal */
469                                                                                    "/org/freedesktop/DBus", /* path */
470                                                                                    client->name,
471                                                                                    G_DBUS_SIGNAL_FLAGS_NONE,
472                                                                                    on_name_owner_changed,
473                                                                                    client,
474                                                                                    NULL);
475
476   if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
477     {
478       g_dbus_connection_call (client->connection,
479                               "org.freedesktop.DBus",  /* bus name */
480                               "/org/freedesktop/DBus", /* object path */
481                               "org.freedesktop.DBus",  /* interface name */
482                               "StartServiceByName",    /* method name */
483                               g_variant_new ("(su)", client->name, 0),
484                               G_VARIANT_TYPE ("(u)"),
485                               G_DBUS_CALL_FLAGS_NONE,
486                               -1,
487                               NULL,
488                               (GAsyncReadyCallback) start_service_by_name_cb,
489                               client_ref (client));
490     }
491   else
492     {
493       /* check owner */
494       invoke_get_name_owner (client);
495     }
496 }
497
498
499 static void
500 connection_get_cb (GObject      *source_object,
501                    GAsyncResult *res,
502                    gpointer      user_data)
503 {
504   Client *client = user_data;
505
506   client->connection = g_bus_get_finish (res, NULL);
507   if (client->connection == NULL)
508     {
509       call_vanished_handler (client, FALSE);
510       goto out;
511     }
512
513   has_connection (client);
514
515  out:
516   client_unref (client);
517 }
518
519 /* ---------------------------------------------------------------------------------------------------- */
520
521 /**
522  * g_bus_watch_name:
523  * @bus_type: The type of bus to watch a name on.
524  * @name: The name (well-known or unique) to watch.
525  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
526  * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
527  * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
528  * @user_data: User data to pass to handlers.
529  * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
530  *
531  * Starts watching @name on the bus specified by @bus_type and calls
532  * @name_appeared_handler and @name_vanished_handler when the name is
533  * known to have a owner respectively known to lose its
534  * owner. Callbacks will be invoked in the
535  * [thread-default main context][g-main-context-push-thread-default]
536  * of the thread you are calling this function from.
537  *
538  * You are guaranteed that one of the handlers will be invoked after
539  * calling this function. When you are done watching the name, just
540  * call g_bus_unwatch_name() with the watcher id this function
541  * returns.
542  *
543  * If the name vanishes or appears (for example the application owning
544  * the name could restart), the handlers are also invoked. If the
545  * #GDBusConnection that is used for watching the name disconnects, then
546  * @name_vanished_handler is invoked since it is no longer
547  * possible to access the name.
548  *
549  * Another guarantee is that invocations of @name_appeared_handler
550  * and @name_vanished_handler are guaranteed to alternate; that
551  * is, if @name_appeared_handler is invoked then you are
552  * guaranteed that the next time one of the handlers is invoked, it
553  * will be @name_vanished_handler. The reverse is also true.
554  *
555  * This behavior makes it very simple to write applications that want
556  * to take action when a certain [name exists][gdbus-watching-names].
557  * Basically, the application should create object proxies in
558  * @name_appeared_handler and destroy them again (if any) in
559  * @name_vanished_handler.
560  *
561  * Returns: An identifier (never 0) that an be used with
562  * g_bus_unwatch_name() to stop watching the name.
563  *
564  * Since: 2.26
565  */
566 guint
567 g_bus_watch_name (GBusType                  bus_type,
568                   const gchar              *name,
569                   GBusNameWatcherFlags      flags,
570                   GBusNameAppearedCallback  name_appeared_handler,
571                   GBusNameVanishedCallback  name_vanished_handler,
572                   gpointer                  user_data,
573                   GDestroyNotify            user_data_free_func)
574 {
575   Client *client;
576
577   g_return_val_if_fail (g_dbus_is_name (name), 0);
578
579   G_LOCK (lock);
580
581   client = g_new0 (Client, 1);
582   client->ref_count = 1;
583   client->id = next_global_id++; /* TODO: uh oh, handle overflow */
584   client->name = g_strdup (name);
585   client->flags = flags;
586   client->name_appeared_handler = name_appeared_handler;
587   client->name_vanished_handler = name_vanished_handler;
588   client->user_data = user_data;
589   client->user_data_free_func = user_data_free_func;
590   client->main_context = g_main_context_ref_thread_default ();
591
592   if (map_id_to_client == NULL)
593     {
594       map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
595     }
596   g_hash_table_insert (map_id_to_client,
597                        GUINT_TO_POINTER (client->id),
598                        client);
599
600   g_bus_get (bus_type,
601              NULL,
602              connection_get_cb,
603              client_ref (client));
604
605   G_UNLOCK (lock);
606
607   return client->id;
608 }
609
610 /**
611  * g_bus_watch_name_on_connection:
612  * @connection: A #GDBusConnection.
613  * @name: The name (well-known or unique) to watch.
614  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
615  * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
616  * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
617  * @user_data: User data to pass to handlers.
618  * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
619  *
620  * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
621  * #GBusType.
622  *
623  * Returns: An identifier (never 0) that an be used with
624  * g_bus_unwatch_name() to stop watching the name.
625  *
626  * Since: 2.26
627  */
628 guint g_bus_watch_name_on_connection (GDBusConnection          *connection,
629                                       const gchar              *name,
630                                       GBusNameWatcherFlags      flags,
631                                       GBusNameAppearedCallback  name_appeared_handler,
632                                       GBusNameVanishedCallback  name_vanished_handler,
633                                       gpointer                  user_data,
634                                       GDestroyNotify            user_data_free_func)
635 {
636   Client *client;
637
638   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
639   g_return_val_if_fail (g_dbus_is_name (name), 0);
640
641   G_LOCK (lock);
642
643   client = g_new0 (Client, 1);
644   client->ref_count = 1;
645   client->id = next_global_id++; /* TODO: uh oh, handle overflow */
646   client->name = g_strdup (name);
647   client->flags = flags;
648   client->name_appeared_handler = name_appeared_handler;
649   client->name_vanished_handler = name_vanished_handler;
650   client->user_data = user_data;
651   client->user_data_free_func = user_data_free_func;
652   client->main_context = g_main_context_ref_thread_default ();
653
654   if (map_id_to_client == NULL)
655     map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
656
657   g_hash_table_insert (map_id_to_client,
658                        GUINT_TO_POINTER (client->id),
659                        client);
660
661   client->connection = g_object_ref (connection);
662   G_UNLOCK (lock);
663
664   has_connection (client);
665
666   return client->id;
667 }
668
669 typedef struct {
670   GClosure *name_appeared_closure;
671   GClosure *name_vanished_closure;
672 } WatchNameData;
673
674 static WatchNameData *
675 watch_name_data_new (GClosure *name_appeared_closure,
676                      GClosure *name_vanished_closure)
677 {
678   WatchNameData *data;
679
680   data = g_new0 (WatchNameData, 1);
681
682   if (name_appeared_closure != NULL)
683     {
684       data->name_appeared_closure = g_closure_ref (name_appeared_closure);
685       g_closure_sink (name_appeared_closure);
686       if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure))
687         g_closure_set_marshal (name_appeared_closure, g_cclosure_marshal_generic);
688     }
689
690   if (name_vanished_closure != NULL)
691     {
692       data->name_vanished_closure = g_closure_ref (name_vanished_closure);
693       g_closure_sink (name_vanished_closure);
694       if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure))
695         g_closure_set_marshal (name_vanished_closure, g_cclosure_marshal_generic);
696     }
697
698   return data;
699 }
700
701 static void
702 watch_with_closures_on_name_appeared (GDBusConnection *connection,
703                                       const gchar     *name,
704                                       const gchar     *name_owner,
705                                       gpointer         user_data)
706 {
707   WatchNameData *data = user_data;
708   GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
709
710   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
711   g_value_set_object (&params[0], connection);
712
713   g_value_init (&params[1], G_TYPE_STRING);
714   g_value_set_string (&params[1], name);
715
716   g_value_init (&params[2], G_TYPE_STRING);
717   g_value_set_string (&params[2], name_owner);
718
719   g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
720
721   g_value_unset (params + 0);
722   g_value_unset (params + 1);
723   g_value_unset (params + 2);
724 }
725
726 static void
727 watch_with_closures_on_name_vanished (GDBusConnection *connection,
728                                       const gchar     *name,
729                                       gpointer         user_data)
730 {
731   WatchNameData *data = user_data;
732   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
733
734   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
735   g_value_set_object (&params[0], connection);
736
737   g_value_init (&params[1], G_TYPE_STRING);
738   g_value_set_string (&params[1], name);
739
740   g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
741
742   g_value_unset (params + 0);
743   g_value_unset (params + 1);
744 }
745
746 static void
747 bus_watch_name_free_func (gpointer user_data)
748 {
749   WatchNameData *data = user_data;
750
751   if (data->name_appeared_closure != NULL)
752     g_closure_unref (data->name_appeared_closure);
753
754   if (data->name_vanished_closure != NULL)
755     g_closure_unref (data->name_vanished_closure);
756
757   g_free (data);
758 }
759
760 /**
761  * g_bus_watch_name_with_closures:
762  * @bus_type: The type of bus to watch a name on.
763  * @name: The name (well-known or unique) to watch.
764  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
765  * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
766  * to exist or %NULL.
767  * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
768  * to not exist or %NULL.
769  *
770  * Version of g_bus_watch_name() using closures instead of callbacks for
771  * easier binding in other languages.
772  *
773  * Returns: An identifier (never 0) that an be used with
774  * g_bus_unwatch_name() to stop watching the name.
775  *
776  * Rename to: g_bus_watch_name
777  *
778  * Since: 2.26
779  */
780 guint
781 g_bus_watch_name_with_closures (GBusType                 bus_type,
782                                 const gchar             *name,
783                                 GBusNameWatcherFlags     flags,
784                                 GClosure                *name_appeared_closure,
785                                 GClosure                *name_vanished_closure)
786 {
787   return g_bus_watch_name (bus_type,
788           name,
789           flags,
790           name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
791           name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
792           watch_name_data_new (name_appeared_closure, name_vanished_closure),
793           bus_watch_name_free_func);
794 }
795
796 /**
797  * g_bus_watch_name_on_connection_with_closures:
798  * @connection: A #GDBusConnection.
799  * @name: The name (well-known or unique) to watch.
800  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
801  * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
802  * to exist or %NULL.
803  * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
804  * to not exist or %NULL.
805  *
806  * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
807  * easier binding in other languages.
808  *
809  * Returns: An identifier (never 0) that an be used with
810  * g_bus_unwatch_name() to stop watching the name.
811  *
812  * Rename to: g_bus_watch_name_on_connection
813  *
814  * Since: 2.26
815  */
816 guint g_bus_watch_name_on_connection_with_closures (
817                                       GDBusConnection          *connection,
818                                       const gchar              *name,
819                                       GBusNameWatcherFlags      flags,
820                                       GClosure                 *name_appeared_closure,
821                                       GClosure                 *name_vanished_closure)
822 {
823   return g_bus_watch_name_on_connection (connection,
824           name,
825           flags,
826           name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
827           name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
828           watch_name_data_new (name_appeared_closure, name_vanished_closure),
829           bus_watch_name_free_func);
830 }
831
832 /**
833  * g_bus_unwatch_name:
834  * @watcher_id: An identifier obtained from g_bus_watch_name()
835  *
836  * Stops watching a name.
837  *
838  * Since: 2.26
839  */
840 void
841 g_bus_unwatch_name (guint watcher_id)
842 {
843   Client *client;
844
845   g_return_if_fail (watcher_id > 0);
846
847   client = NULL;
848
849   G_LOCK (lock);
850   if (watcher_id == 0 ||
851       map_id_to_client == NULL ||
852       (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
853     {
854       g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
855       goto out;
856     }
857
858   client->cancelled = TRUE;
859   g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
860
861  out:
862   G_UNLOCK (lock);
863
864   /* do callback without holding lock */
865   if (client != NULL)
866     {
867       client_unref (client);
868     }
869 }