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_set_name (idle_source, "[gio] call_in_idle_cb");
202 g_source_attach (idle_source, client->main_context);
203 g_source_unref (idle_source);
207 do_call (Client *client, CallType call_type)
209 GMainContext *current_context;
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);
216 actually_do_call (client, client->connection, client->name_owner, call_type);
217 g_main_context_unref (current_context);
221 call_appeared_handler (Client *client)
223 if (client->previous_call != PREVIOUS_CALL_APPEARED)
225 client->previous_call = PREVIOUS_CALL_APPEARED;
226 if (!client->cancelled && client->name_appeared_handler != NULL)
228 do_call (client, CALL_TYPE_NAME_APPEARED);
234 call_vanished_handler (Client *client,
235 gboolean ignore_cancelled)
237 if (client->previous_call != PREVIOUS_CALL_VANISHED)
239 client->previous_call = PREVIOUS_CALL_VANISHED;
240 if (((!client->cancelled) || ignore_cancelled) && client->name_vanished_handler != NULL)
242 do_call (client, CALL_TYPE_NAME_VANISHED);
247 /* ---------------------------------------------------------------------------------------------------- */
250 on_connection_disconnected (GDBusConnection *connection,
251 gboolean remote_peer_vanished,
255 Client *client = user_data;
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;
266 call_vanished_handler (client, FALSE);
269 /* ---------------------------------------------------------------------------------------------------- */
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,
280 Client *client = user_data;
282 const gchar *old_owner;
283 const gchar *new_owner;
285 if (!client->initialized)
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)
293 g_variant_get (parameters,
299 /* we only care about a specific name */
300 if (g_strcmp0 (name, client->name) != 0)
303 if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
305 g_free (client->name_owner);
306 client->name_owner = NULL;
307 call_vanished_handler (client, FALSE);
310 if (new_owner != NULL && strlen (new_owner) > 0)
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);
322 /* ---------------------------------------------------------------------------------------------------- */
325 process_get_name_owner (Client *client,
326 const char *name_owner)
328 if (name_owner != NULL)
330 g_warn_if_fail (client->name_owner == NULL);
331 client->name_owner = g_strdup (name_owner);
332 call_appeared_handler (client);
335 call_vanished_handler (client, FALSE);
337 client->initialized = TRUE;
341 get_name_owner_cb (GObject *source_object,
345 Client *client = user_data;
347 const char *name_owner;
352 result = g_dbus_connection_call_finish (client->connection,
357 g_variant_get (result, "(&s)", &name_owner);
360 process_get_name_owner (client, name_owner);
363 g_variant_unref (result);
364 client_unref (client);
367 /* ---------------------------------------------------------------------------------------------------- */
370 invoke_get_name_owner (Client *client)
372 if (_g_dbus_connection_is_kdbus (client->connection))
376 name_owner = g_dbus_get_name_owner (client->connection,
379 process_get_name_owner (client, name_owner);
384 g_dbus_connection_call (client->connection,
385 "org.freedesktop.DBus", /* bus name */
386 "/org/freedesktop/DBus", /* object path */
387 "org.freedesktop.DBus", /* interface name */
388 "GetNameOwner", /* method name */
389 g_variant_new ("(s)", client->name),
390 G_VARIANT_TYPE ("(s)"),
391 G_DBUS_CALL_FLAGS_NONE,
394 (GAsyncReadyCallback) get_name_owner_cb,
395 client_ref (client));
399 /* ---------------------------------------------------------------------------------------------------- */
402 start_service_by_name_cb (GObject *source_object,
406 Client *client = user_data;
411 result = g_dbus_connection_call_finish (client->connection,
416 guint32 start_service_result;
417 g_variant_get (result, "(u)", &start_service_result);
419 if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
421 invoke_get_name_owner (client);
423 else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
425 invoke_get_name_owner (client);
429 g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
430 call_vanished_handler (client, FALSE);
431 client->initialized = TRUE;
436 /* Errors are not unexpected; the bus will reply e.g.
438 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
439 * was not provided by any .service files
441 * This doesn't mean that the name doesn't have an owner, just
442 * that it's not provided by a .service file. So proceed to
443 * invoke GetNameOwner().
445 invoke_get_name_owner (client);
449 g_variant_unref (result);
450 client_unref (client);
453 /* ---------------------------------------------------------------------------------------------------- */
456 has_connection (Client *client)
458 /* listen for disconnection */
459 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
461 G_CALLBACK (on_connection_disconnected),
464 /* start listening to NameOwnerChanged messages immediately */
465 client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
466 "org.freedesktop.DBus", /* name */
467 "org.freedesktop.DBus", /* if */
468 "NameOwnerChanged", /* signal */
469 "/org/freedesktop/DBus", /* path */
471 G_DBUS_SIGNAL_FLAGS_NONE,
472 on_name_owner_changed,
476 if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
478 g_dbus_connection_call (client->connection,
479 "org.freedesktop.DBus", /* bus name */
480 "/org/freedesktop/DBus", /* object path */
481 "org.freedesktop.DBus", /* interface name */
482 "StartServiceByName", /* method name */
483 g_variant_new ("(su)", client->name, 0),
484 G_VARIANT_TYPE ("(u)"),
485 G_DBUS_CALL_FLAGS_NONE,
488 (GAsyncReadyCallback) start_service_by_name_cb,
489 client_ref (client));
494 invoke_get_name_owner (client);
500 connection_get_cb (GObject *source_object,
504 Client *client = user_data;
506 client->connection = g_bus_get_finish (res, NULL);
507 if (client->connection == NULL)
509 call_vanished_handler (client, FALSE);
513 has_connection (client);
516 client_unref (client);
519 /* ---------------------------------------------------------------------------------------------------- */
523 * @bus_type: The type of bus to watch a name on.
524 * @name: The name (well-known or unique) to watch.
525 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
526 * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
527 * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
528 * @user_data: User data to pass to handlers.
529 * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
531 * Starts watching @name on the bus specified by @bus_type and calls
532 * @name_appeared_handler and @name_vanished_handler when the name is
533 * known to have a owner respectively known to lose its
534 * owner. Callbacks will be invoked in the
535 * [thread-default main context][g-main-context-push-thread-default]
536 * of the thread you are calling this function from.
538 * You are guaranteed that one of the handlers will be invoked after
539 * calling this function. When you are done watching the name, just
540 * call g_bus_unwatch_name() with the watcher id this function
543 * If the name vanishes or appears (for example the application owning
544 * the name could restart), the handlers are also invoked. If the
545 * #GDBusConnection that is used for watching the name disconnects, then
546 * @name_vanished_handler is invoked since it is no longer
547 * possible to access the name.
549 * Another guarantee is that invocations of @name_appeared_handler
550 * and @name_vanished_handler are guaranteed to alternate; that
551 * is, if @name_appeared_handler is invoked then you are
552 * guaranteed that the next time one of the handlers is invoked, it
553 * will be @name_vanished_handler. The reverse is also true.
555 * This behavior makes it very simple to write applications that want
556 * to take action when a certain [name exists][gdbus-watching-names].
557 * Basically, the application should create object proxies in
558 * @name_appeared_handler and destroy them again (if any) in
559 * @name_vanished_handler.
561 * Returns: An identifier (never 0) that an be used with
562 * g_bus_unwatch_name() to stop watching the name.
567 g_bus_watch_name (GBusType bus_type,
569 GBusNameWatcherFlags flags,
570 GBusNameAppearedCallback name_appeared_handler,
571 GBusNameVanishedCallback name_vanished_handler,
573 GDestroyNotify user_data_free_func)
577 g_return_val_if_fail (g_dbus_is_name (name), 0);
581 client = g_new0 (Client, 1);
582 client->ref_count = 1;
583 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
584 client->name = g_strdup (name);
585 client->flags = flags;
586 client->name_appeared_handler = name_appeared_handler;
587 client->name_vanished_handler = name_vanished_handler;
588 client->user_data = user_data;
589 client->user_data_free_func = user_data_free_func;
590 client->main_context = g_main_context_ref_thread_default ();
592 if (map_id_to_client == NULL)
594 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
596 g_hash_table_insert (map_id_to_client,
597 GUINT_TO_POINTER (client->id),
603 client_ref (client));
611 * g_bus_watch_name_on_connection:
612 * @connection: A #GDBusConnection.
613 * @name: The name (well-known or unique) to watch.
614 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
615 * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
616 * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
617 * @user_data: User data to pass to handlers.
618 * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
620 * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
623 * Returns: An identifier (never 0) that an be used with
624 * g_bus_unwatch_name() to stop watching the name.
628 guint g_bus_watch_name_on_connection (GDBusConnection *connection,
630 GBusNameWatcherFlags flags,
631 GBusNameAppearedCallback name_appeared_handler,
632 GBusNameVanishedCallback name_vanished_handler,
634 GDestroyNotify user_data_free_func)
638 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
639 g_return_val_if_fail (g_dbus_is_name (name), 0);
643 client = g_new0 (Client, 1);
644 client->ref_count = 1;
645 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
646 client->name = g_strdup (name);
647 client->flags = flags;
648 client->name_appeared_handler = name_appeared_handler;
649 client->name_vanished_handler = name_vanished_handler;
650 client->user_data = user_data;
651 client->user_data_free_func = user_data_free_func;
652 client->main_context = g_main_context_ref_thread_default ();
654 if (map_id_to_client == NULL)
655 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
657 g_hash_table_insert (map_id_to_client,
658 GUINT_TO_POINTER (client->id),
661 client->connection = g_object_ref (connection);
664 has_connection (client);
670 GClosure *name_appeared_closure;
671 GClosure *name_vanished_closure;
674 static WatchNameData *
675 watch_name_data_new (GClosure *name_appeared_closure,
676 GClosure *name_vanished_closure)
680 data = g_new0 (WatchNameData, 1);
682 if (name_appeared_closure != NULL)
684 data->name_appeared_closure = g_closure_ref (name_appeared_closure);
685 g_closure_sink (name_appeared_closure);
686 if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure))
687 g_closure_set_marshal (name_appeared_closure, g_cclosure_marshal_generic);
690 if (name_vanished_closure != NULL)
692 data->name_vanished_closure = g_closure_ref (name_vanished_closure);
693 g_closure_sink (name_vanished_closure);
694 if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure))
695 g_closure_set_marshal (name_vanished_closure, g_cclosure_marshal_generic);
702 watch_with_closures_on_name_appeared (GDBusConnection *connection,
704 const gchar *name_owner,
707 WatchNameData *data = user_data;
708 GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
710 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
711 g_value_set_object (¶ms[0], connection);
713 g_value_init (¶ms[1], G_TYPE_STRING);
714 g_value_set_string (¶ms[1], name);
716 g_value_init (¶ms[2], G_TYPE_STRING);
717 g_value_set_string (¶ms[2], name_owner);
719 g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
721 g_value_unset (params + 0);
722 g_value_unset (params + 1);
723 g_value_unset (params + 2);
727 watch_with_closures_on_name_vanished (GDBusConnection *connection,
731 WatchNameData *data = user_data;
732 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
734 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
735 g_value_set_object (¶ms[0], connection);
737 g_value_init (¶ms[1], G_TYPE_STRING);
738 g_value_set_string (¶ms[1], name);
740 g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
742 g_value_unset (params + 0);
743 g_value_unset (params + 1);
747 bus_watch_name_free_func (gpointer user_data)
749 WatchNameData *data = user_data;
751 if (data->name_appeared_closure != NULL)
752 g_closure_unref (data->name_appeared_closure);
754 if (data->name_vanished_closure != NULL)
755 g_closure_unref (data->name_vanished_closure);
761 * g_bus_watch_name_with_closures:
762 * @bus_type: The type of bus to watch a name on.
763 * @name: The name (well-known or unique) to watch.
764 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
765 * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
767 * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
768 * to not exist or %NULL.
770 * Version of g_bus_watch_name() using closures instead of callbacks for
771 * easier binding in other languages.
773 * Returns: An identifier (never 0) that an be used with
774 * g_bus_unwatch_name() to stop watching the name.
776 * Rename to: g_bus_watch_name
781 g_bus_watch_name_with_closures (GBusType bus_type,
783 GBusNameWatcherFlags flags,
784 GClosure *name_appeared_closure,
785 GClosure *name_vanished_closure)
787 return g_bus_watch_name (bus_type,
790 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
791 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
792 watch_name_data_new (name_appeared_closure, name_vanished_closure),
793 bus_watch_name_free_func);
797 * g_bus_watch_name_on_connection_with_closures:
798 * @connection: A #GDBusConnection.
799 * @name: The name (well-known or unique) to watch.
800 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
801 * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
803 * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
804 * to not exist or %NULL.
806 * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
807 * easier binding in other languages.
809 * Returns: An identifier (never 0) that an be used with
810 * g_bus_unwatch_name() to stop watching the name.
812 * Rename to: g_bus_watch_name_on_connection
816 guint g_bus_watch_name_on_connection_with_closures (
817 GDBusConnection *connection,
819 GBusNameWatcherFlags flags,
820 GClosure *name_appeared_closure,
821 GClosure *name_vanished_closure)
823 return g_bus_watch_name_on_connection (connection,
826 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
827 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
828 watch_name_data_new (name_appeared_closure, name_vanished_closure),
829 bus_watch_name_free_func);
833 * g_bus_unwatch_name:
834 * @watcher_id: An identifier obtained from g_bus_watch_name()
836 * Stops watching a name.
841 g_bus_unwatch_name (guint watcher_id)
845 g_return_if_fail (watcher_id > 0);
850 if (watcher_id == 0 ||
851 map_id_to_client == NULL ||
852 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
854 g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
858 client->cancelled = TRUE;
859 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
864 /* do callback without holding lock */
867 client_unref (client);