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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include "gdbusutils.h"
29 #include "gdbusnamewatching.h"
30 #include "gdbuserror.h"
31 #include "gdbusprivate.h"
32 #include "gdbusconnection.h"
33 #include "gio-marshal.h"
38 * SECTION:gdbusnamewatching
39 * @title: Watching Bus Names
40 * @short_description: Simple API for watching bus names
43 * Convenience API for watching bus names.
45 * <example id="gdbus-watching-names"><title>Simple application watching a name</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-watch-name.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
48 G_LOCK_DEFINE_STATIC (lock);
50 /* ---------------------------------------------------------------------------------------------------- */
54 PREVIOUS_CALL_NONE = 0,
55 PREVIOUS_CALL_APPEARED,
56 PREVIOUS_CALL_VANISHED,
61 volatile gint ref_count;
64 GBusNameWatcherFlags flags;
66 GBusNameAppearedCallback name_appeared_handler;
67 GBusNameVanishedCallback name_vanished_handler;
69 GDestroyNotify user_data_free_func;
70 GMainContext *main_context;
72 GDBusConnection *connection;
73 gulong disconnected_signal_handler_id;
74 guint name_owner_changed_subscription_id;
76 PreviousCall previous_call;
82 static guint next_global_id = 1;
83 static GHashTable *map_id_to_client = NULL;
86 client_ref (Client *client)
88 g_atomic_int_inc (&client->ref_count);
93 client_unref (Client *client)
95 if (g_atomic_int_dec_and_test (&client->ref_count))
97 if (client->connection != NULL)
99 if (client->name_owner_changed_subscription_id > 0)
100 g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
101 if (client->disconnected_signal_handler_id > 0)
102 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
103 g_object_unref (client->connection);
105 g_free (client->name);
106 g_free (client->name_owner);
107 if (client->main_context != NULL)
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);
115 /* ---------------------------------------------------------------------------------------------------- */
119 CALL_TYPE_NAME_APPEARED,
120 CALL_TYPE_NAME_VANISHED
127 /* keep this separate because client->connection may
128 * be set to NULL after scheduling the call
130 GDBusConnection *connection;
139 call_handler_data_free (CallHandlerData *data)
141 if (data->connection != NULL)
142 g_object_unref (data->connection);
143 g_free (data->name_owner);
144 client_unref (data->client);
149 actually_do_call (Client *client, GDBusConnection *connection, const gchar *name_owner, CallType call_type)
153 case CALL_TYPE_NAME_APPEARED:
154 if (client->name_appeared_handler != NULL)
156 client->name_appeared_handler (connection,
163 case CALL_TYPE_NAME_VANISHED:
164 if (client->name_vanished_handler != NULL)
166 client->name_vanished_handler (connection,
173 g_assert_not_reached ();
179 call_in_idle_cb (gpointer _data)
181 CallHandlerData *data = _data;
182 actually_do_call (data->client, data->connection, data->name_owner, data->call_type);
187 schedule_call_in_idle (Client *client, CallType call_type)
189 CallHandlerData *data;
190 GSource *idle_source;
192 data = g_new0 (CallHandlerData, 1);
193 data->client = client_ref (client);
194 data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
195 data->name_owner = g_strdup (client->name_owner);
196 data->call_type = call_type;
198 idle_source = g_idle_source_new ();
199 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
200 g_source_set_callback (idle_source,
203 (GDestroyNotify) call_handler_data_free);
204 g_source_attach (idle_source, client->main_context);
205 g_source_unref (idle_source);
209 do_call (Client *client, CallType call_type)
211 /* only schedule in idle if we're not in the right thread */
212 if (g_main_context_get_thread_default () != client->main_context)
213 schedule_call_in_idle (client, call_type);
215 actually_do_call (client, client->connection, client->name_owner, call_type);
219 call_appeared_handler (Client *client)
221 if (client->previous_call != PREVIOUS_CALL_APPEARED)
223 client->previous_call = PREVIOUS_CALL_APPEARED;
224 if (!client->cancelled && client->name_appeared_handler != NULL)
226 do_call (client, CALL_TYPE_NAME_APPEARED);
232 call_vanished_handler (Client *client,
233 gboolean ignore_cancelled)
235 if (client->previous_call != PREVIOUS_CALL_VANISHED)
237 client->previous_call = PREVIOUS_CALL_VANISHED;
238 if (((!client->cancelled) || ignore_cancelled) && client->name_vanished_handler != NULL)
240 do_call (client, CALL_TYPE_NAME_VANISHED);
245 /* ---------------------------------------------------------------------------------------------------- */
248 on_connection_disconnected (GDBusConnection *connection,
249 gboolean remote_peer_vanished,
253 Client *client = user_data;
255 if (client->name_owner_changed_subscription_id > 0)
256 g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
257 if (client->disconnected_signal_handler_id > 0)
258 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
259 g_object_unref (client->connection);
260 client->disconnected_signal_handler_id = 0;
261 client->name_owner_changed_subscription_id = 0;
262 client->connection = NULL;
264 call_vanished_handler (client, FALSE);
267 /* ---------------------------------------------------------------------------------------------------- */
270 on_name_owner_changed (GDBusConnection *connection,
271 const gchar *sender_name,
272 const gchar *object_path,
273 const gchar *interface_name,
274 const gchar *signal_name,
275 GVariant *parameters,
278 Client *client = user_data;
280 const gchar *old_owner;
281 const gchar *new_owner;
283 if (!client->initialized)
286 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
287 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
288 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
291 g_variant_get (parameters,
297 /* we only care about a specific name */
298 if (g_strcmp0 (name, client->name) != 0)
301 if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
303 g_free (client->name_owner);
304 client->name_owner = NULL;
305 call_vanished_handler (client, FALSE);
308 if (new_owner != NULL && strlen (new_owner) > 0)
310 g_warn_if_fail (client->name_owner == NULL);
311 g_free (client->name_owner);
312 client->name_owner = g_strdup (new_owner);
313 call_appeared_handler (client);
320 /* ---------------------------------------------------------------------------------------------------- */
323 get_name_owner_cb (GObject *source_object,
327 Client *client = user_data;
329 const char *name_owner;
334 result = g_dbus_connection_call_finish (client->connection,
339 g_variant_get (result, "(&s)", &name_owner);
342 if (name_owner != NULL)
344 g_warn_if_fail (client->name_owner == NULL);
345 client->name_owner = g_strdup (name_owner);
346 call_appeared_handler (client);
350 call_vanished_handler (client, FALSE);
353 client->initialized = TRUE;
356 g_variant_unref (result);
357 client_unref (client);
360 /* ---------------------------------------------------------------------------------------------------- */
363 invoke_get_name_owner (Client *client)
365 g_dbus_connection_call (client->connection,
366 "org.freedesktop.DBus", /* bus name */
367 "/org/freedesktop/DBus", /* object path */
368 "org.freedesktop.DBus", /* interface name */
369 "GetNameOwner", /* method name */
370 g_variant_new ("(s)", client->name),
371 G_VARIANT_TYPE ("(s)"),
372 G_DBUS_CALL_FLAGS_NONE,
375 (GAsyncReadyCallback) get_name_owner_cb,
376 client_ref (client));
379 /* ---------------------------------------------------------------------------------------------------- */
382 start_service_by_name_cb (GObject *source_object,
386 Client *client = user_data;
391 result = g_dbus_connection_call_finish (client->connection,
396 guint32 start_service_result;
397 g_variant_get (result, "(u)", &start_service_result);
399 if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
401 invoke_get_name_owner (client);
403 else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
405 invoke_get_name_owner (client);
409 g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
410 call_vanished_handler (client, FALSE);
411 client->initialized = TRUE;
416 /* Errors are not unexpected; the bus will reply e.g.
418 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
419 * was not provided by any .service files
421 * This doesn't mean that the name doesn't have an owner, just
422 * that it's not provided by a .service file. So proceed to
423 * invoke GetNameOwner().
425 invoke_get_name_owner (client);
429 g_variant_unref (result);
430 client_unref (client);
433 /* ---------------------------------------------------------------------------------------------------- */
436 has_connection (Client *client)
438 /* listen for disconnection */
439 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
441 G_CALLBACK (on_connection_disconnected),
444 /* start listening to NameOwnerChanged messages immediately */
445 client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
446 "org.freedesktop.DBus", /* name */
447 "org.freedesktop.DBus", /* if */
448 "NameOwnerChanged", /* signal */
449 "/org/freedesktop/DBus", /* path */
451 G_DBUS_SIGNAL_FLAGS_NONE,
452 on_name_owner_changed,
456 if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
458 g_dbus_connection_call (client->connection,
459 "org.freedesktop.DBus", /* bus name */
460 "/org/freedesktop/DBus", /* object path */
461 "org.freedesktop.DBus", /* interface name */
462 "StartServiceByName", /* method name */
463 g_variant_new ("(su)", client->name, 0),
464 G_VARIANT_TYPE ("(u)"),
465 G_DBUS_CALL_FLAGS_NONE,
468 (GAsyncReadyCallback) start_service_by_name_cb,
469 client_ref (client));
474 invoke_get_name_owner (client);
480 connection_get_cb (GObject *source_object,
484 Client *client = user_data;
486 client->connection = g_bus_get_finish (res, NULL);
487 if (client->connection == NULL)
489 call_vanished_handler (client, FALSE);
493 has_connection (client);
496 client_unref (client);
499 /* ---------------------------------------------------------------------------------------------------- */
503 * @bus_type: The type of bus to watch a name on.
504 * @name: The name (well-known or unique) to watch.
505 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
506 * @name_appeared_handler: Handler to invoke when @name is known to exist or %NULL.
507 * @name_vanished_handler: Handler to invoke when @name is known to not exist or %NULL.
508 * @user_data: User data to pass to handlers.
509 * @user_data_free_func: Function for freeing @user_data or %NULL.
511 * Starts watching @name on the bus specified by @bus_type and calls
512 * @name_appeared_handler and @name_vanished_handler when the name is
513 * known to have a owner respectively known to lose its
514 * owner. Callbacks will be invoked in the <link
515 * linkend="g-main-context-push-thread-default">thread-default main
516 * loop</link> of the thread you are calling this function from.
518 * You are guaranteed that one of the handlers will be invoked after
519 * calling this function. When you are done watching the name, just
520 * call g_bus_unwatch_name() with the watcher id this function
523 * If the name vanishes or appears (for example the application owning
524 * the name could restart), the handlers are also invoked. If the
525 * #GDBusConnection that is used for watching the name disconnects, then
526 * @name_vanished_handler is invoked since it is no longer
527 * possible to access the name.
529 * Another guarantee is that invocations of @name_appeared_handler
530 * and @name_vanished_handler are guaranteed to alternate; that
531 * is, if @name_appeared_handler is invoked then you are
532 * guaranteed that the next time one of the handlers is invoked, it
533 * will be @name_vanished_handler. The reverse is also true.
535 * This behavior makes it very simple to write applications that wants
536 * to take action when a certain name exists, see <xref
537 * linkend="gdbus-watching-names"/>. Basically, the application
538 * should create object proxies in @name_appeared_handler and destroy
539 * them again (if any) in @name_vanished_handler.
541 * Returns: An identifier (never 0) that an be used with
542 * g_bus_unwatch_name() to stop watching the name.
547 g_bus_watch_name (GBusType bus_type,
549 GBusNameWatcherFlags flags,
550 GBusNameAppearedCallback name_appeared_handler,
551 GBusNameVanishedCallback name_vanished_handler,
553 GDestroyNotify user_data_free_func)
557 g_return_val_if_fail (g_dbus_is_name (name), 0);
561 client = g_new0 (Client, 1);
562 client->ref_count = 1;
563 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
564 client->name = g_strdup (name);
565 client->flags = flags;
566 client->name_appeared_handler = name_appeared_handler;
567 client->name_vanished_handler = name_vanished_handler;
568 client->user_data = user_data;
569 client->user_data_free_func = user_data_free_func;
570 client->main_context = g_main_context_get_thread_default ();
571 if (client->main_context != NULL)
572 g_main_context_ref (client->main_context);
574 if (map_id_to_client == NULL)
576 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
578 g_hash_table_insert (map_id_to_client,
579 GUINT_TO_POINTER (client->id),
585 client_ref (client));
593 * g_bus_watch_name_on_connection:
594 * @connection: A #GDBusConnection.
595 * @name: The name (well-known or unique) to watch.
596 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
597 * @name_appeared_handler: Handler to invoke when @name is known to exist or %NULL.
598 * @name_vanished_handler: Handler to invoke when @name is known to not exist or %NULL.
599 * @user_data: User data to pass to handlers.
600 * @user_data_free_func: Function for freeing @user_data or %NULL.
602 * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
605 * Returns: An identifier (never 0) that an be used with
606 * g_bus_unwatch_name() to stop watching the name.
610 guint g_bus_watch_name_on_connection (GDBusConnection *connection,
612 GBusNameWatcherFlags flags,
613 GBusNameAppearedCallback name_appeared_handler,
614 GBusNameVanishedCallback name_vanished_handler,
616 GDestroyNotify user_data_free_func)
620 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
621 g_return_val_if_fail (g_dbus_is_name (name), 0);
625 client = g_new0 (Client, 1);
626 client->ref_count = 1;
627 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
628 client->name = g_strdup (name);
629 client->flags = flags;
630 client->name_appeared_handler = name_appeared_handler;
631 client->name_vanished_handler = name_vanished_handler;
632 client->user_data = user_data;
633 client->user_data_free_func = user_data_free_func;
634 client->main_context = g_main_context_get_thread_default ();
635 if (client->main_context != NULL)
636 g_main_context_ref (client->main_context);
638 if (map_id_to_client == NULL)
639 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
641 g_hash_table_insert (map_id_to_client,
642 GUINT_TO_POINTER (client->id),
645 client->connection = g_object_ref (connection);
648 has_connection (client);
654 GClosure *name_appeared_closure;
655 GClosure *name_vanished_closure;
658 static WatchNameData *
659 watch_name_data_new (GClosure *name_appeared_closure,
660 GClosure *name_vanished_closure)
664 data = g_new0 (WatchNameData, 1);
666 if (name_appeared_closure != NULL)
668 data->name_appeared_closure = g_closure_ref (name_appeared_closure);
669 g_closure_sink (name_appeared_closure);
670 if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure))
671 g_closure_set_marshal (name_appeared_closure, _gio_marshal_VOID__STRING_STRING);
674 if (name_vanished_closure != NULL)
676 data->name_vanished_closure = g_closure_ref (name_vanished_closure);
677 g_closure_sink (name_vanished_closure);
678 if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure))
679 g_closure_set_marshal (name_vanished_closure, _gio_marshal_VOID__STRING);
686 watch_with_closures_on_name_appeared (GDBusConnection *connection,
688 const gchar *name_owner,
691 WatchNameData *data = user_data;
692 GValue params[3] = { { 0, }, { 0, }, { 0, } };
694 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
695 g_value_set_object (¶ms[0], connection);
697 g_value_init (¶ms[1], G_TYPE_STRING);
698 g_value_set_string (¶ms[1], name);
700 g_value_init (¶ms[2], G_TYPE_STRING);
701 g_value_set_string (¶ms[2], name_owner);
703 g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
705 g_value_unset (params + 0);
706 g_value_unset (params + 1);
707 g_value_unset (params + 2);
711 watch_with_closures_on_name_vanished (GDBusConnection *connection,
715 WatchNameData *data = user_data;
716 GValue params[2] = { { 0, }, { 0, } };
718 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
719 g_value_set_object (¶ms[0], connection);
721 g_value_init (¶ms[1], G_TYPE_STRING);
722 g_value_set_string (¶ms[1], name);
724 g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
726 g_value_unset (params + 0);
727 g_value_unset (params + 1);
731 bus_watch_name_free_func (gpointer user_data)
733 WatchNameData *data = user_data;
735 if (data->name_appeared_closure != NULL)
736 g_closure_unref (data->name_appeared_closure);
738 if (data->name_vanished_closure != NULL)
739 g_closure_unref (data->name_vanished_closure);
745 * g_bus_watch_name_with_closures:
746 * @bus_type: The type of bus to watch a name on.
747 * @name: The name (well-known or unique) to watch.
748 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
749 * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
751 * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
752 * to not exist or %NULL.
754 * Version of g_bus_watch_name() using closures instead of callbacks for
755 * easier binding in other languages.
757 * Returns: An identifier (never 0) that an be used with
758 * g_bus_unwatch_name() to stop watching the name.
760 * Rename to: g_bus_watch_name
765 g_bus_watch_name_with_closures (GBusType bus_type,
767 GBusNameWatcherFlags flags,
768 GClosure *name_appeared_closure,
769 GClosure *name_vanished_closure)
771 return g_bus_watch_name (bus_type,
774 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
775 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
776 watch_name_data_new (name_appeared_closure, name_vanished_closure),
777 bus_watch_name_free_func);
781 * g_bus_watch_name_on_connection_with_closures:
782 * @connection: A #GDBusConnection.
783 * @name: The name (well-known or unique) to watch.
784 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
785 * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
787 * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
788 * to not exist or %NULL.
790 * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
791 * easier binding in other languages.
793 * Returns: An identifier (never 0) that an be used with
794 * g_bus_unwatch_name() to stop watching the name.
796 * Rename to: g_bus_watch_name_on_connection
800 guint g_bus_watch_name_on_connection_with_closures (
801 GDBusConnection *connection,
803 GBusNameWatcherFlags flags,
804 GClosure *name_appeared_closure,
805 GClosure *name_vanished_closure)
807 return g_bus_watch_name_on_connection (connection,
810 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
811 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
812 watch_name_data_new (name_appeared_closure, name_vanished_closure),
813 bus_watch_name_free_func);
817 * g_bus_unwatch_name:
818 * @watcher_id: An identifier obtained from g_bus_watch_name()
820 * Stops watching a name.
825 g_bus_unwatch_name (guint watcher_id)
829 g_return_if_fail (watcher_id > 0);
834 if (watcher_id == 0 ||
835 map_id_to_client == NULL ||
836 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
838 g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
842 client->cancelled = TRUE;
843 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
848 /* do callback without holding lock */
851 client_unref (client);