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