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 if (_g_dbus_connection_is_kdbus (client->connection))
479 g_error ("TODO: Implement StartServiceByName\n");
481 g_dbus_connection_call (client->connection,
482 "org.freedesktop.DBus", /* bus name */
483 "/org/freedesktop/DBus", /* object path */
484 "org.freedesktop.DBus", /* interface name */
485 "StartServiceByName", /* method name */
486 g_variant_new ("(su)", client->name, 0),
487 G_VARIANT_TYPE ("(u)"),
488 G_DBUS_CALL_FLAGS_NONE,
491 (GAsyncReadyCallback) start_service_by_name_cb,
492 client_ref (client));
497 invoke_get_name_owner (client);
503 connection_get_cb (GObject *source_object,
507 Client *client = user_data;
509 client->connection = g_bus_get_finish (res, NULL);
510 if (client->connection == NULL)
512 call_vanished_handler (client, FALSE);
516 has_connection (client);
519 client_unref (client);
522 /* ---------------------------------------------------------------------------------------------------- */
526 * @bus_type: The type of bus to watch a name on.
527 * @name: The name (well-known or unique) to watch.
528 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
529 * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
530 * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
531 * @user_data: User data to pass to handlers.
532 * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
534 * Starts watching @name on the bus specified by @bus_type and calls
535 * @name_appeared_handler and @name_vanished_handler when the name is
536 * known to have a owner respectively known to lose its
537 * owner. Callbacks will be invoked in the
538 * [thread-default main context][g-main-context-push-thread-default]
539 * of the thread you are calling this function from.
541 * You are guaranteed that one of the handlers will be invoked after
542 * calling this function. When you are done watching the name, just
543 * call g_bus_unwatch_name() with the watcher id this function
546 * If the name vanishes or appears (for example the application owning
547 * the name could restart), the handlers are also invoked. If the
548 * #GDBusConnection that is used for watching the name disconnects, then
549 * @name_vanished_handler is invoked since it is no longer
550 * possible to access the name.
552 * Another guarantee is that invocations of @name_appeared_handler
553 * and @name_vanished_handler are guaranteed to alternate; that
554 * is, if @name_appeared_handler is invoked then you are
555 * guaranteed that the next time one of the handlers is invoked, it
556 * will be @name_vanished_handler. The reverse is also true.
558 * This behavior makes it very simple to write applications that want
559 * to take action when a certain [name exists][gdbus-watching-names].
560 * Basically, the application should create object proxies in
561 * @name_appeared_handler and destroy them again (if any) in
562 * @name_vanished_handler.
564 * Returns: An identifier (never 0) that an be used with
565 * g_bus_unwatch_name() to stop watching the name.
570 g_bus_watch_name (GBusType bus_type,
572 GBusNameWatcherFlags flags,
573 GBusNameAppearedCallback name_appeared_handler,
574 GBusNameVanishedCallback name_vanished_handler,
576 GDestroyNotify user_data_free_func)
580 g_return_val_if_fail (g_dbus_is_name (name), 0);
584 client = g_new0 (Client, 1);
585 client->ref_count = 1;
586 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
587 client->name = g_strdup (name);
588 client->flags = flags;
589 client->name_appeared_handler = name_appeared_handler;
590 client->name_vanished_handler = name_vanished_handler;
591 client->user_data = user_data;
592 client->user_data_free_func = user_data_free_func;
593 client->main_context = g_main_context_ref_thread_default ();
595 if (map_id_to_client == NULL)
597 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
599 g_hash_table_insert (map_id_to_client,
600 GUINT_TO_POINTER (client->id),
606 client_ref (client));
614 * g_bus_watch_name_on_connection:
615 * @connection: A #GDBusConnection.
616 * @name: The name (well-known or unique) to watch.
617 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
618 * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL.
619 * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL.
620 * @user_data: User data to pass to handlers.
621 * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL.
623 * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
626 * Returns: An identifier (never 0) that an be used with
627 * g_bus_unwatch_name() to stop watching the name.
631 guint g_bus_watch_name_on_connection (GDBusConnection *connection,
633 GBusNameWatcherFlags flags,
634 GBusNameAppearedCallback name_appeared_handler,
635 GBusNameVanishedCallback name_vanished_handler,
637 GDestroyNotify user_data_free_func)
641 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
642 g_return_val_if_fail (g_dbus_is_name (name), 0);
646 client = g_new0 (Client, 1);
647 client->ref_count = 1;
648 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
649 client->name = g_strdup (name);
650 client->flags = flags;
651 client->name_appeared_handler = name_appeared_handler;
652 client->name_vanished_handler = name_vanished_handler;
653 client->user_data = user_data;
654 client->user_data_free_func = user_data_free_func;
655 client->main_context = g_main_context_ref_thread_default ();
657 if (map_id_to_client == NULL)
658 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
660 g_hash_table_insert (map_id_to_client,
661 GUINT_TO_POINTER (client->id),
664 client->connection = g_object_ref (connection);
667 has_connection (client);
673 GClosure *name_appeared_closure;
674 GClosure *name_vanished_closure;
677 static WatchNameData *
678 watch_name_data_new (GClosure *name_appeared_closure,
679 GClosure *name_vanished_closure)
683 data = g_new0 (WatchNameData, 1);
685 if (name_appeared_closure != NULL)
687 data->name_appeared_closure = g_closure_ref (name_appeared_closure);
688 g_closure_sink (name_appeared_closure);
689 if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure))
690 g_closure_set_marshal (name_appeared_closure, g_cclosure_marshal_generic);
693 if (name_vanished_closure != NULL)
695 data->name_vanished_closure = g_closure_ref (name_vanished_closure);
696 g_closure_sink (name_vanished_closure);
697 if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure))
698 g_closure_set_marshal (name_vanished_closure, g_cclosure_marshal_generic);
705 watch_with_closures_on_name_appeared (GDBusConnection *connection,
707 const gchar *name_owner,
710 WatchNameData *data = user_data;
711 GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
713 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
714 g_value_set_object (¶ms[0], connection);
716 g_value_init (¶ms[1], G_TYPE_STRING);
717 g_value_set_string (¶ms[1], name);
719 g_value_init (¶ms[2], G_TYPE_STRING);
720 g_value_set_string (¶ms[2], name_owner);
722 g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
724 g_value_unset (params + 0);
725 g_value_unset (params + 1);
726 g_value_unset (params + 2);
730 watch_with_closures_on_name_vanished (GDBusConnection *connection,
734 WatchNameData *data = user_data;
735 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
737 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
738 g_value_set_object (¶ms[0], connection);
740 g_value_init (¶ms[1], G_TYPE_STRING);
741 g_value_set_string (¶ms[1], name);
743 g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
745 g_value_unset (params + 0);
746 g_value_unset (params + 1);
750 bus_watch_name_free_func (gpointer user_data)
752 WatchNameData *data = user_data;
754 if (data->name_appeared_closure != NULL)
755 g_closure_unref (data->name_appeared_closure);
757 if (data->name_vanished_closure != NULL)
758 g_closure_unref (data->name_vanished_closure);
764 * g_bus_watch_name_with_closures:
765 * @bus_type: The type of bus to watch a name on.
766 * @name: The name (well-known or unique) to watch.
767 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
768 * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
770 * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
771 * to not exist or %NULL.
773 * Version of g_bus_watch_name() using closures instead of callbacks for
774 * easier binding in other languages.
776 * Returns: An identifier (never 0) that an be used with
777 * g_bus_unwatch_name() to stop watching the name.
779 * Rename to: g_bus_watch_name
784 g_bus_watch_name_with_closures (GBusType bus_type,
786 GBusNameWatcherFlags flags,
787 GClosure *name_appeared_closure,
788 GClosure *name_vanished_closure)
790 return g_bus_watch_name (bus_type,
793 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
794 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
795 watch_name_data_new (name_appeared_closure, name_vanished_closure),
796 bus_watch_name_free_func);
800 * g_bus_watch_name_on_connection_with_closures:
801 * @connection: A #GDBusConnection.
802 * @name: The name (well-known or unique) to watch.
803 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
804 * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
806 * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
807 * to not exist or %NULL.
809 * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
810 * easier binding in other languages.
812 * Returns: An identifier (never 0) that an be used with
813 * g_bus_unwatch_name() to stop watching the name.
815 * Rename to: g_bus_watch_name_on_connection
819 guint g_bus_watch_name_on_connection_with_closures (
820 GDBusConnection *connection,
822 GBusNameWatcherFlags flags,
823 GClosure *name_appeared_closure,
824 GClosure *name_vanished_closure)
826 return g_bus_watch_name_on_connection (connection,
829 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
830 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
831 watch_name_data_new (name_appeared_closure, name_vanished_closure),
832 bus_watch_name_free_func);
836 * g_bus_unwatch_name:
837 * @watcher_id: An identifier obtained from g_bus_watch_name()
839 * Stops watching a name.
844 g_bus_unwatch_name (guint watcher_id)
848 g_return_if_fail (watcher_id > 0);
853 if (watcher_id == 0 ||
854 map_id_to_client == NULL ||
855 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
857 g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
861 client->cancelled = TRUE;
862 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
867 /* do callback without holding lock */
870 client_unref (client);