nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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 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
517 * [thread-default main context][g-main-context-push-thread-default]
518 * 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 want
538 * to take action when a certain [name exists][gdbus-watching-names].
539 * Basically, the application should create object proxies in
540 * @name_appeared_handler and destroy them again (if any) in
541 * @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: (rename-to g_bus_watch_name)
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 * 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: (rename-to g_bus_watch_name_on_connection)
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 * Since: 2.26
793 */
794 guint g_bus_watch_name_on_connection_with_closures (
795 GDBusConnection *connection,
796 const gchar *name,
797 GBusNameWatcherFlags flags,
798 GClosure *name_appeared_closure,
799 GClosure *name_vanished_closure)
800 {
801 return g_bus_watch_name_on_connection (connection,
802 name,
803 flags,
804 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
805 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
806 watch_name_data_new (name_appeared_closure, name_vanished_closure),
807 bus_watch_name_free_func);
808 }
809  
810 /**
811 * g_bus_unwatch_name:
812 * @watcher_id: An identifier obtained from g_bus_watch_name()
813 *
814 * Stops watching a name.
815 *
816 * Since: 2.26
817 */
818 void
819 g_bus_unwatch_name (guint watcher_id)
820 {
821 Client *client;
822  
823 g_return_if_fail (watcher_id > 0);
824  
825 client = NULL;
826  
827 G_LOCK (lock);
828 if (watcher_id == 0 ||
829 map_id_to_client == NULL ||
830 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
831 {
832 g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
833 goto out;
834 }
835  
836 client->cancelled = TRUE;
837 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
838  
839 out:
840 G_UNLOCK (lock);
841  
842 /* do callback without holding lock */
843 if (client != NULL)
844 {
845 client_unref (client);
846 }
847 }