1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
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/>.
18 * Author: David Zeuthen <davidz@redhat.com>
26 #include "gdbusutils.h"
27 #include "gdbusnamewatching.h"
28 #include "gdbuserror.h"
29 #include "gdbusprivate.h"
30 #include "gdbusconnection.h"
35 * SECTION:gdbusnamewatching
36 * @title: Watching Bus Names
37 * @short_description: Simple API for watching bus names
40 * Convenience API for watching bus names.
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)
46 G_LOCK_DEFINE_STATIC (lock);
48 /* ---------------------------------------------------------------------------------------------------- */
52 PREVIOUS_CALL_NONE = 0,
53 PREVIOUS_CALL_APPEARED,
54 PREVIOUS_CALL_VANISHED,
59 volatile gint ref_count;
62 GBusNameWatcherFlags flags;
64 GBusNameAppearedCallback name_appeared_handler;
65 GBusNameVanishedCallback name_vanished_handler;
67 GDestroyNotify user_data_free_func;
68 GMainContext *main_context;
70 GDBusConnection *connection;
71 gulong disconnected_signal_handler_id;
72 guint name_owner_changed_subscription_id;
74 PreviousCall previous_call;
80 static guint next_global_id = 1;
81 static GHashTable *map_id_to_client = NULL;
84 client_ref (Client *client)
86 g_atomic_int_inc (&client->ref_count);
91 client_unref (Client *client)
93 if (g_atomic_int_dec_and_test (&client->ref_count))
95 if (client->connection != NULL)
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);
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);
112 /* ---------------------------------------------------------------------------------------------------- */
116 CALL_TYPE_NAME_APPEARED,
117 CALL_TYPE_NAME_VANISHED
124 /* keep this separate because client->connection may
125 * be set to NULL after scheduling the call
127 GDBusConnection *connection;
136 call_handler_data_free (CallHandlerData *data)
138 if (data->connection != NULL)
139 g_object_unref (data->connection);
140 g_free (data->name_owner);
141 client_unref (data->client);
146 actually_do_call (Client *client, GDBusConnection *connection, const gchar *name_owner, CallType call_type)
150 case CALL_TYPE_NAME_APPEARED:
151 if (client->name_appeared_handler != NULL)
153 client->name_appeared_handler (connection,
160 case CALL_TYPE_NAME_VANISHED:
161 if (client->name_vanished_handler != NULL)
163 client->name_vanished_handler (connection,
170 g_assert_not_reached ();
176 call_in_idle_cb (gpointer _data)
178 CallHandlerData *data = _data;
179 actually_do_call (data->client, data->connection, data->name_owner, data->call_type);
184 schedule_call_in_idle (Client *client, CallType call_type)
186 CallHandlerData *data;
187 GSource *idle_source;
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;
195 idle_source = g_idle_source_new ();
196 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
197 g_source_set_callback (idle_source,
200 (GDestroyNotify) call_handler_data_free);
201 g_source_attach (idle_source, client->main_context);
202 g_source_unref (idle_source);
206 do_call (Client *client, CallType call_type)
208 GMainContext *current_context;
210 /* only schedule in idle if we're not in the right thread */
211 current_context = g_main_context_ref_thread_default ();
212 if (current_context != client->main_context)
213 schedule_call_in_idle (client, call_type);
215 actually_do_call (client, client->connection, client->name_owner, call_type);
216 g_main_context_unref (current_context);
220 call_appeared_handler (Client *client)
222 if (client->previous_call != PREVIOUS_CALL_APPEARED)
224 client->previous_call = PREVIOUS_CALL_APPEARED;
225 if (!client->cancelled && client->name_appeared_handler != NULL)
227 do_call (client, CALL_TYPE_NAME_APPEARED);
233 call_vanished_handler (Client *client,
234 gboolean ignore_cancelled)
236 if (client->previous_call != PREVIOUS_CALL_VANISHED)
238 client->previous_call = PREVIOUS_CALL_VANISHED;
239 if (((!client->cancelled) || ignore_cancelled) && client->name_vanished_handler != NULL)
241 do_call (client, CALL_TYPE_NAME_VANISHED);
246 /* ---------------------------------------------------------------------------------------------------- */
249 on_connection_disconnected (GDBusConnection *connection,
250 gboolean remote_peer_vanished,
254 Client *client = user_data;
256 if (client->name_owner_changed_subscription_id > 0)
257 g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
258 if (client->disconnected_signal_handler_id > 0)
259 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
260 g_object_unref (client->connection);
261 client->disconnected_signal_handler_id = 0;
262 client->name_owner_changed_subscription_id = 0;
263 client->connection = NULL;
265 call_vanished_handler (client, FALSE);
268 /* ---------------------------------------------------------------------------------------------------- */
271 on_name_owner_changed (GDBusConnection *connection,
272 const gchar *sender_name,
273 const gchar *object_path,
274 const gchar *interface_name,
275 const gchar *signal_name,
276 GVariant *parameters,
279 Client *client = user_data;
281 const gchar *old_owner;
282 const gchar *new_owner;
284 if (!client->initialized)
287 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
288 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
289 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
292 g_variant_get (parameters,
298 /* we only care about a specific name */
299 if (g_strcmp0 (name, client->name) != 0)
302 if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
304 g_free (client->name_owner);
305 client->name_owner = NULL;
306 call_vanished_handler (client, FALSE);
309 if (new_owner != NULL && strlen (new_owner) > 0)
311 g_warn_if_fail (client->name_owner == NULL);
312 g_free (client->name_owner);
313 client->name_owner = g_strdup (new_owner);
314 call_appeared_handler (client);
321 /* ---------------------------------------------------------------------------------------------------- */
324 get_name_owner_cb (GObject *source_object,
328 Client *client = user_data;
330 const char *name_owner;
335 result = g_dbus_connection_call_finish (client->connection,
340 g_variant_get (result, "(&s)", &name_owner);
343 if (name_owner != NULL)
345 g_warn_if_fail (client->name_owner == NULL);
346 client->name_owner = g_strdup (name_owner);
347 call_appeared_handler (client);
351 call_vanished_handler (client, FALSE);
354 client->initialized = TRUE;
357 g_variant_unref (result);
358 client_unref (client);
361 /* ---------------------------------------------------------------------------------------------------- */
364 invoke_get_name_owner (Client *client)
366 g_dbus_connection_call (client->connection,
367 "org.freedesktop.DBus", /* bus name */
368 "/org/freedesktop/DBus", /* object path */
369 "org.freedesktop.DBus", /* interface name */
370 "GetNameOwner", /* method name */
371 g_variant_new ("(s)", client->name),
372 G_VARIANT_TYPE ("(s)"),
373 G_DBUS_CALL_FLAGS_NONE,
376 (GAsyncReadyCallback) get_name_owner_cb,
377 client_ref (client));
380 /* ---------------------------------------------------------------------------------------------------- */
383 start_service_by_name_cb (GObject *source_object,
387 Client *client = user_data;
392 result = g_dbus_connection_call_finish (client->connection,
397 guint32 start_service_result;
398 g_variant_get (result, "(u)", &start_service_result);
400 if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
402 invoke_get_name_owner (client);
404 else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
406 invoke_get_name_owner (client);
410 g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
411 call_vanished_handler (client, FALSE);
412 client->initialized = TRUE;
417 /* Errors are not unexpected; the bus will reply e.g.
419 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
420 * was not provided by any .service files
422 * This doesn't mean that the name doesn't have an owner, just
423 * that it's not provided by a .service file. So proceed to
424 * invoke GetNameOwner().
426 invoke_get_name_owner (client);
430 g_variant_unref (result);
431 client_unref (client);
434 /* ---------------------------------------------------------------------------------------------------- */
437 has_connection (Client *client)
439 /* listen for disconnection */
440 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
442 G_CALLBACK (on_connection_disconnected),
445 /* start listening to NameOwnerChanged messages immediately */
446 client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
447 "org.freedesktop.DBus", /* name */
448 "org.freedesktop.DBus", /* if */
449 "NameOwnerChanged", /* signal */
450 "/org/freedesktop/DBus", /* path */
452 G_DBUS_SIGNAL_FLAGS_NONE,
453 on_name_owner_changed,
457 if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
459 g_dbus_connection_call (client->connection,
460 "org.freedesktop.DBus", /* bus name */
461 "/org/freedesktop/DBus", /* object path */
462 "org.freedesktop.DBus", /* interface name */
463 "StartServiceByName", /* method name */
464 g_variant_new ("(su)", client->name, 0),
465 G_VARIANT_TYPE ("(u)"),
466 G_DBUS_CALL_FLAGS_NONE,
469 (GAsyncReadyCallback) start_service_by_name_cb,
470 client_ref (client));
475 invoke_get_name_owner (client);
481 connection_get_cb (GObject *source_object,
485 Client *client = user_data;
487 client->connection = g_bus_get_finish (res, NULL);
488 if (client->connection == NULL)
490 call_vanished_handler (client, FALSE);
494 has_connection (client);
497 client_unref (client);
500 /* ---------------------------------------------------------------------------------------------------- */
504 * @bus_type: The type of bus to watch a name on.
505 * @name: The name (well-known or unique) to watch.
506 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
507 * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
508 * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
509 * @user_data: User data to pass to handlers.
510 * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
512 * Starts watching @name on the bus specified by @bus_type and calls
513 * @name_appeared_handler and @name_vanished_handler when the name is
514 * known to have a owner respectively known to lose its
515 * owner. Callbacks will be invoked in the
516 * [thread-default main context][g-main-context-push-thread-default]
517 * of the thread you are calling this function from.
519 * You are guaranteed that one of the handlers will be invoked after
520 * calling this function. When you are done watching the name, just
521 * call g_bus_unwatch_name() with the watcher id this function
524 * If the name vanishes or appears (for example the application owning
525 * the name could restart), the handlers are also invoked. If the
526 * #GDBusConnection that is used for watching the name disconnects, then
527 * @name_vanished_handler is invoked since it is no longer
528 * possible to access the name.
530 * Another guarantee is that invocations of @name_appeared_handler
531 * and @name_vanished_handler are guaranteed to alternate; that
532 * is, if @name_appeared_handler is invoked then you are
533 * guaranteed that the next time one of the handlers is invoked, it
534 * will be @name_vanished_handler. The reverse is also true.
536 * This behavior makes it very simple to write applications that want
537 * to take action when a certain [name exists][gdbus-watching-names].
538 * Basically, the application should create object proxies in
539 * @name_appeared_handler and destroy them again (if any) in
540 * @name_vanished_handler.
542 * Returns: An identifier (never 0) that an be used with
543 * g_bus_unwatch_name() to stop watching the name.
548 g_bus_watch_name (GBusType bus_type,
550 GBusNameWatcherFlags flags,
551 GBusNameAppearedCallback name_appeared_handler,
552 GBusNameVanishedCallback name_vanished_handler,
554 GDestroyNotify user_data_free_func)
558 g_return_val_if_fail (g_dbus_is_name (name), 0);
562 client = g_new0 (Client, 1);
563 client->ref_count = 1;
564 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
565 client->name = g_strdup (name);
566 client->flags = flags;
567 client->name_appeared_handler = name_appeared_handler;
568 client->name_vanished_handler = name_vanished_handler;
569 client->user_data = user_data;
570 client->user_data_free_func = user_data_free_func;
571 client->main_context = g_main_context_ref_thread_default ();
573 if (map_id_to_client == NULL)
575 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
577 g_hash_table_insert (map_id_to_client,
578 GUINT_TO_POINTER (client->id),
584 client_ref (client));
592 * g_bus_watch_name_on_connection:
593 * @connection: A #GDBusConnection.
594 * @name: The name (well-known or unique) to watch.
595 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
596 * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
597 * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
598 * @user_data: User data to pass to handlers.
599 * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
601 * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
604 * Returns: An identifier (never 0) that an be used with
605 * g_bus_unwatch_name() to stop watching the name.
609 guint g_bus_watch_name_on_connection (GDBusConnection *connection,
611 GBusNameWatcherFlags flags,
612 GBusNameAppearedCallback name_appeared_handler,
613 GBusNameVanishedCallback name_vanished_handler,
615 GDestroyNotify user_data_free_func)
619 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
620 g_return_val_if_fail (g_dbus_is_name (name), 0);
624 client = g_new0 (Client, 1);
625 client->ref_count = 1;
626 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
627 client->name = g_strdup (name);
628 client->flags = flags;
629 client->name_appeared_handler = name_appeared_handler;
630 client->name_vanished_handler = name_vanished_handler;
631 client->user_data = user_data;
632 client->user_data_free_func = user_data_free_func;
633 client->main_context = g_main_context_ref_thread_default ();
635 if (map_id_to_client == NULL)
636 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
638 g_hash_table_insert (map_id_to_client,
639 GUINT_TO_POINTER (client->id),
642 client->connection = g_object_ref (connection);
645 has_connection (client);
651 GClosure *name_appeared_closure;
652 GClosure *name_vanished_closure;
655 static WatchNameData *
656 watch_name_data_new (GClosure *name_appeared_closure,
657 GClosure *name_vanished_closure)
661 data = g_new0 (WatchNameData, 1);
663 if (name_appeared_closure != NULL)
665 data->name_appeared_closure = g_closure_ref (name_appeared_closure);
666 g_closure_sink (name_appeared_closure);
667 if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure))
668 g_closure_set_marshal (name_appeared_closure, g_cclosure_marshal_generic);
671 if (name_vanished_closure != NULL)
673 data->name_vanished_closure = g_closure_ref (name_vanished_closure);
674 g_closure_sink (name_vanished_closure);
675 if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure))
676 g_closure_set_marshal (name_vanished_closure, g_cclosure_marshal_generic);
683 watch_with_closures_on_name_appeared (GDBusConnection *connection,
685 const gchar *name_owner,
688 WatchNameData *data = user_data;
689 GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
691 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
692 g_value_set_object (¶ms[0], connection);
694 g_value_init (¶ms[1], G_TYPE_STRING);
695 g_value_set_string (¶ms[1], name);
697 g_value_init (¶ms[2], G_TYPE_STRING);
698 g_value_set_string (¶ms[2], name_owner);
700 g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
702 g_value_unset (params + 0);
703 g_value_unset (params + 1);
704 g_value_unset (params + 2);
708 watch_with_closures_on_name_vanished (GDBusConnection *connection,
712 WatchNameData *data = user_data;
713 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
715 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
716 g_value_set_object (¶ms[0], connection);
718 g_value_init (¶ms[1], G_TYPE_STRING);
719 g_value_set_string (¶ms[1], name);
721 g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
723 g_value_unset (params + 0);
724 g_value_unset (params + 1);
728 bus_watch_name_free_func (gpointer user_data)
730 WatchNameData *data = user_data;
732 if (data->name_appeared_closure != NULL)
733 g_closure_unref (data->name_appeared_closure);
735 if (data->name_vanished_closure != NULL)
736 g_closure_unref (data->name_vanished_closure);
742 * g_bus_watch_name_with_closures:
743 * @bus_type: The type of bus to watch a name on.
744 * @name: The name (well-known or unique) to watch.
745 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
746 * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
748 * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
749 * to not exist or %NULL.
751 * Version of g_bus_watch_name() using closures instead of callbacks for
752 * easier binding in other languages.
754 * Returns: An identifier (never 0) that an be used with
755 * g_bus_unwatch_name() to stop watching the name.
757 * Rename to: g_bus_watch_name
762 g_bus_watch_name_with_closures (GBusType bus_type,
764 GBusNameWatcherFlags flags,
765 GClosure *name_appeared_closure,
766 GClosure *name_vanished_closure)
768 return g_bus_watch_name (bus_type,
771 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
772 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
773 watch_name_data_new (name_appeared_closure, name_vanished_closure),
774 bus_watch_name_free_func);
778 * g_bus_watch_name_on_connection_with_closures:
779 * @connection: A #GDBusConnection.
780 * @name: The name (well-known or unique) to watch.
781 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
782 * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
784 * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
785 * to not exist or %NULL.
787 * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
788 * easier binding in other languages.
790 * Returns: An identifier (never 0) that an be used with
791 * g_bus_unwatch_name() to stop watching the name.
793 * Rename to: g_bus_watch_name_on_connection
797 guint g_bus_watch_name_on_connection_with_closures (
798 GDBusConnection *connection,
800 GBusNameWatcherFlags flags,
801 GClosure *name_appeared_closure,
802 GClosure *name_vanished_closure)
804 return g_bus_watch_name_on_connection (connection,
807 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
808 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
809 watch_name_data_new (name_appeared_closure, name_vanished_closure),
810 bus_watch_name_free_func);
814 * g_bus_unwatch_name:
815 * @watcher_id: An identifier obtained from g_bus_watch_name()
817 * Stops watching a name.
822 g_bus_unwatch_name (guint watcher_id)
826 g_return_if_fail (watcher_id > 0);
831 if (watcher_id == 0 ||
832 map_id_to_client == NULL ||
833 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
835 g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
839 client->cancelled = TRUE;
840 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
845 /* do callback without holding lock */
848 client_unref (client);