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