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