[kdbus] Use new API insted of direct call to org.freedesktop.DBus
[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       if (_g_dbus_connection_is_kdbus (client->connection))
479         g_error ("TODO: Implement StartServiceByName\n");
480       else
481         g_dbus_connection_call (client->connection,
482                                 "org.freedesktop.DBus",  /* bus name */
483                                 "/org/freedesktop/DBus", /* object path */
484                                 "org.freedesktop.DBus",  /* interface name */
485                                 "StartServiceByName",    /* method name */
486                                 g_variant_new ("(su)", client->name, 0),
487                                 G_VARIANT_TYPE ("(u)"),
488                                 G_DBUS_CALL_FLAGS_NONE,
489                                 -1,
490                                 NULL,
491                                 (GAsyncReadyCallback) start_service_by_name_cb,
492                                 client_ref (client));
493     }
494   else
495     {
496       /* check owner */
497       invoke_get_name_owner (client);
498     }
499 }
500
501
502 static void
503 connection_get_cb (GObject      *source_object,
504                    GAsyncResult *res,
505                    gpointer      user_data)
506 {
507   Client *client = user_data;
508
509   client->connection = g_bus_get_finish (res, NULL);
510   if (client->connection == NULL)
511     {
512       call_vanished_handler (client, FALSE);
513       goto out;
514     }
515
516   has_connection (client);
517
518  out:
519   client_unref (client);
520 }
521
522 /* ---------------------------------------------------------------------------------------------------- */
523
524 /**
525  * g_bus_watch_name:
526  * @bus_type: The type of bus to watch a name on.
527  * @name: The name (well-known or unique) to watch.
528  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
529  * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
530  * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
531  * @user_data: User data to pass to handlers.
532  * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
533  *
534  * Starts watching @name on the bus specified by @bus_type and calls
535  * @name_appeared_handler and @name_vanished_handler when the name is
536  * known to have a owner respectively known to lose its
537  * owner. Callbacks will be invoked in the
538  * [thread-default main context][g-main-context-push-thread-default]
539  * of the thread you are calling this function from.
540  *
541  * You are guaranteed that one of the handlers will be invoked after
542  * calling this function. When you are done watching the name, just
543  * call g_bus_unwatch_name() with the watcher id this function
544  * returns.
545  *
546  * If the name vanishes or appears (for example the application owning
547  * the name could restart), the handlers are also invoked. If the
548  * #GDBusConnection that is used for watching the name disconnects, then
549  * @name_vanished_handler is invoked since it is no longer
550  * possible to access the name.
551  *
552  * Another guarantee is that invocations of @name_appeared_handler
553  * and @name_vanished_handler are guaranteed to alternate; that
554  * is, if @name_appeared_handler is invoked then you are
555  * guaranteed that the next time one of the handlers is invoked, it
556  * will be @name_vanished_handler. The reverse is also true.
557  *
558  * This behavior makes it very simple to write applications that want
559  * to take action when a certain [name exists][gdbus-watching-names].
560  * Basically, the application should create object proxies in
561  * @name_appeared_handler and destroy them again (if any) in
562  * @name_vanished_handler.
563  *
564  * Returns: An identifier (never 0) that an be used with
565  * g_bus_unwatch_name() to stop watching the name.
566  *
567  * Since: 2.26
568  */
569 guint
570 g_bus_watch_name (GBusType                  bus_type,
571                   const gchar              *name,
572                   GBusNameWatcherFlags      flags,
573                   GBusNameAppearedCallback  name_appeared_handler,
574                   GBusNameVanishedCallback  name_vanished_handler,
575                   gpointer                  user_data,
576                   GDestroyNotify            user_data_free_func)
577 {
578   Client *client;
579
580   g_return_val_if_fail (g_dbus_is_name (name), 0);
581
582   G_LOCK (lock);
583
584   client = g_new0 (Client, 1);
585   client->ref_count = 1;
586   client->id = next_global_id++; /* TODO: uh oh, handle overflow */
587   client->name = g_strdup (name);
588   client->flags = flags;
589   client->name_appeared_handler = name_appeared_handler;
590   client->name_vanished_handler = name_vanished_handler;
591   client->user_data = user_data;
592   client->user_data_free_func = user_data_free_func;
593   client->main_context = g_main_context_ref_thread_default ();
594
595   if (map_id_to_client == NULL)
596     {
597       map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
598     }
599   g_hash_table_insert (map_id_to_client,
600                        GUINT_TO_POINTER (client->id),
601                        client);
602
603   g_bus_get (bus_type,
604              NULL,
605              connection_get_cb,
606              client_ref (client));
607
608   G_UNLOCK (lock);
609
610   return client->id;
611 }
612
613 /**
614  * g_bus_watch_name_on_connection:
615  * @connection: A #GDBusConnection.
616  * @name: The name (well-known or unique) to watch.
617  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
618  * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
619  * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
620  * @user_data: User data to pass to handlers.
621  * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
622  *
623  * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
624  * #GBusType.
625  *
626  * Returns: An identifier (never 0) that an be used with
627  * g_bus_unwatch_name() to stop watching the name.
628  *
629  * Since: 2.26
630  */
631 guint g_bus_watch_name_on_connection (GDBusConnection          *connection,
632                                       const gchar              *name,
633                                       GBusNameWatcherFlags      flags,
634                                       GBusNameAppearedCallback  name_appeared_handler,
635                                       GBusNameVanishedCallback  name_vanished_handler,
636                                       gpointer                  user_data,
637                                       GDestroyNotify            user_data_free_func)
638 {
639   Client *client;
640
641   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
642   g_return_val_if_fail (g_dbus_is_name (name), 0);
643
644   G_LOCK (lock);
645
646   client = g_new0 (Client, 1);
647   client->ref_count = 1;
648   client->id = next_global_id++; /* TODO: uh oh, handle overflow */
649   client->name = g_strdup (name);
650   client->flags = flags;
651   client->name_appeared_handler = name_appeared_handler;
652   client->name_vanished_handler = name_vanished_handler;
653   client->user_data = user_data;
654   client->user_data_free_func = user_data_free_func;
655   client->main_context = g_main_context_ref_thread_default ();
656
657   if (map_id_to_client == NULL)
658     map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
659
660   g_hash_table_insert (map_id_to_client,
661                        GUINT_TO_POINTER (client->id),
662                        client);
663
664   client->connection = g_object_ref (connection);
665   G_UNLOCK (lock);
666
667   has_connection (client);
668
669   return client->id;
670 }
671
672 typedef struct {
673   GClosure *name_appeared_closure;
674   GClosure *name_vanished_closure;
675 } WatchNameData;
676
677 static WatchNameData *
678 watch_name_data_new (GClosure *name_appeared_closure,
679                      GClosure *name_vanished_closure)
680 {
681   WatchNameData *data;
682
683   data = g_new0 (WatchNameData, 1);
684
685   if (name_appeared_closure != NULL)
686     {
687       data->name_appeared_closure = g_closure_ref (name_appeared_closure);
688       g_closure_sink (name_appeared_closure);
689       if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure))
690         g_closure_set_marshal (name_appeared_closure, g_cclosure_marshal_generic);
691     }
692
693   if (name_vanished_closure != NULL)
694     {
695       data->name_vanished_closure = g_closure_ref (name_vanished_closure);
696       g_closure_sink (name_vanished_closure);
697       if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure))
698         g_closure_set_marshal (name_vanished_closure, g_cclosure_marshal_generic);
699     }
700
701   return data;
702 }
703
704 static void
705 watch_with_closures_on_name_appeared (GDBusConnection *connection,
706                                       const gchar     *name,
707                                       const gchar     *name_owner,
708                                       gpointer         user_data)
709 {
710   WatchNameData *data = user_data;
711   GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
712
713   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
714   g_value_set_object (&params[0], connection);
715
716   g_value_init (&params[1], G_TYPE_STRING);
717   g_value_set_string (&params[1], name);
718
719   g_value_init (&params[2], G_TYPE_STRING);
720   g_value_set_string (&params[2], name_owner);
721
722   g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
723
724   g_value_unset (params + 0);
725   g_value_unset (params + 1);
726   g_value_unset (params + 2);
727 }
728
729 static void
730 watch_with_closures_on_name_vanished (GDBusConnection *connection,
731                                       const gchar     *name,
732                                       gpointer         user_data)
733 {
734   WatchNameData *data = user_data;
735   GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
736
737   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
738   g_value_set_object (&params[0], connection);
739
740   g_value_init (&params[1], G_TYPE_STRING);
741   g_value_set_string (&params[1], name);
742
743   g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
744
745   g_value_unset (params + 0);
746   g_value_unset (params + 1);
747 }
748
749 static void
750 bus_watch_name_free_func (gpointer user_data)
751 {
752   WatchNameData *data = user_data;
753
754   if (data->name_appeared_closure != NULL)
755     g_closure_unref (data->name_appeared_closure);
756
757   if (data->name_vanished_closure != NULL)
758     g_closure_unref (data->name_vanished_closure);
759
760   g_free (data);
761 }
762
763 /**
764  * g_bus_watch_name_with_closures:
765  * @bus_type: The type of bus to watch a name on.
766  * @name: The name (well-known or unique) to watch.
767  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
768  * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
769  * to exist or %NULL.
770  * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
771  * to not exist or %NULL.
772  *
773  * Version of g_bus_watch_name() using closures instead of callbacks for
774  * easier binding in other languages.
775  *
776  * Returns: An identifier (never 0) that an be used with
777  * g_bus_unwatch_name() to stop watching the name.
778  *
779  * Rename to: g_bus_watch_name
780  *
781  * Since: 2.26
782  */
783 guint
784 g_bus_watch_name_with_closures (GBusType                 bus_type,
785                                 const gchar             *name,
786                                 GBusNameWatcherFlags     flags,
787                                 GClosure                *name_appeared_closure,
788                                 GClosure                *name_vanished_closure)
789 {
790   return g_bus_watch_name (bus_type,
791           name,
792           flags,
793           name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
794           name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
795           watch_name_data_new (name_appeared_closure, name_vanished_closure),
796           bus_watch_name_free_func);
797 }
798
799 /**
800  * g_bus_watch_name_on_connection_with_closures:
801  * @connection: A #GDBusConnection.
802  * @name: The name (well-known or unique) to watch.
803  * @flags: Flags from the #GBusNameWatcherFlags enumeration.
804  * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
805  * to exist or %NULL.
806  * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
807  * to not exist or %NULL.
808  *
809  * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
810  * easier binding in other languages.
811  *
812  * Returns: An identifier (never 0) that an be used with
813  * g_bus_unwatch_name() to stop watching the name.
814  *
815  * Rename to: g_bus_watch_name_on_connection
816  *
817  * Since: 2.26
818  */
819 guint g_bus_watch_name_on_connection_with_closures (
820                                       GDBusConnection          *connection,
821                                       const gchar              *name,
822                                       GBusNameWatcherFlags      flags,
823                                       GClosure                 *name_appeared_closure,
824                                       GClosure                 *name_vanished_closure)
825 {
826   return g_bus_watch_name_on_connection (connection,
827           name,
828           flags,
829           name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
830           name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
831           watch_name_data_new (name_appeared_closure, name_vanished_closure),
832           bus_watch_name_free_func);
833 }
834
835 /**
836  * g_bus_unwatch_name:
837  * @watcher_id: An identifier obtained from g_bus_watch_name()
838  *
839  * Stops watching a name.
840  *
841  * Since: 2.26
842  */
843 void
844 g_bus_unwatch_name (guint watcher_id)
845 {
846   Client *client;
847
848   g_return_if_fail (watcher_id > 0);
849
850   client = NULL;
851
852   G_LOCK (lock);
853   if (watcher_id == 0 ||
854       map_id_to_client == NULL ||
855       (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
856     {
857       g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
858       goto out;
859     }
860
861   client->cancelled = TRUE;
862   g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
863
864  out:
865   G_UNLOCK (lock);
866
867   /* do callback without holding lock */
868   if (client != NULL)
869     {
870       client_unref (client);
871     }
872 }