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"
37 * SECTION:gdbusnamewatching
38 * @title: Watching Bus Names
39 * @short_description: Simple API for watching bus names
42 * Convenience API for watching bus names.
44 * <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>
47 G_LOCK_DEFINE_STATIC (lock);
49 /* ---------------------------------------------------------------------------------------------------- */
53 PREVIOUS_CALL_NONE = 0,
54 PREVIOUS_CALL_APPEARED,
55 PREVIOUS_CALL_VANISHED,
60 volatile gint ref_count;
63 GBusNameWatcherFlags flags;
65 GBusNameAppearedCallback name_appeared_handler;
66 GBusNameVanishedCallback name_vanished_handler;
68 GDestroyNotify user_data_free_func;
69 GMainContext *main_context;
71 GDBusConnection *connection;
72 gulong disconnected_signal_handler_id;
73 guint name_owner_changed_subscription_id;
75 PreviousCall previous_call;
81 static guint next_global_id = 1;
82 static GHashTable *map_id_to_client = NULL;
85 client_ref (Client *client)
87 g_atomic_int_inc (&client->ref_count);
92 client_unref (Client *client)
94 if (g_atomic_int_dec_and_test (&client->ref_count))
96 if (client->connection != NULL)
98 if (client->name_owner_changed_subscription_id > 0)
99 g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
100 if (client->disconnected_signal_handler_id > 0)
101 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
102 g_object_unref (client->connection);
104 g_free (client->name);
105 g_free (client->name_owner);
106 if (client->main_context != NULL)
107 g_main_context_unref (client->main_context);
108 if (client->user_data_free_func != NULL)
109 client->user_data_free_func (client->user_data);
114 /* ---------------------------------------------------------------------------------------------------- */
118 CALL_TYPE_NAME_APPEARED,
119 CALL_TYPE_NAME_VANISHED
126 /* keep this separate because client->connection may
127 * be set to NULL after scheduling the call
129 GDBusConnection *connection;
138 call_handler_data_free (CallHandlerData *data)
140 if (data->connection != NULL)
141 g_object_unref (data->connection);
142 g_free (data->name_owner);
143 client_unref (data->client);
148 actually_do_call (Client *client, GDBusConnection *connection, const gchar *name_owner, CallType call_type)
152 case CALL_TYPE_NAME_APPEARED:
153 if (client->name_appeared_handler != NULL)
155 client->name_appeared_handler (connection,
162 case CALL_TYPE_NAME_VANISHED:
163 if (client->name_vanished_handler != NULL)
165 client->name_vanished_handler (connection,
172 g_assert_not_reached ();
178 call_in_idle_cb (gpointer _data)
180 CallHandlerData *data = _data;
181 actually_do_call (data->client, data->connection, data->name_owner, data->call_type);
186 schedule_call_in_idle (Client *client, CallType call_type)
188 CallHandlerData *data;
189 GSource *idle_source;
191 data = g_new0 (CallHandlerData, 1);
192 data->client = client_ref (client);
193 data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
194 data->name_owner = g_strdup (client->name_owner);
195 data->call_type = call_type;
197 idle_source = g_idle_source_new ();
198 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
199 g_source_set_callback (idle_source,
202 (GDestroyNotify) call_handler_data_free);
203 g_source_attach (idle_source, client->main_context);
204 g_source_unref (idle_source);
208 do_call (Client *client, CallType call_type)
210 /* only schedule in idle if we're not in the right thread */
211 if (g_main_context_get_thread_default () != client->main_context)
212 schedule_call_in_idle (client, call_type);
214 actually_do_call (client, client->connection, client->name_owner, call_type);
218 call_appeared_handler (Client *client)
220 if (client->previous_call != PREVIOUS_CALL_APPEARED)
222 client->previous_call = PREVIOUS_CALL_APPEARED;
223 if (!client->cancelled && client->name_appeared_handler != NULL)
225 do_call (client, CALL_TYPE_NAME_APPEARED);
231 call_vanished_handler (Client *client,
232 gboolean ignore_cancelled)
234 if (client->previous_call != PREVIOUS_CALL_VANISHED)
236 client->previous_call = PREVIOUS_CALL_VANISHED;
237 if (((!client->cancelled) || ignore_cancelled) && client->name_vanished_handler != NULL)
239 do_call (client, CALL_TYPE_NAME_VANISHED);
244 /* ---------------------------------------------------------------------------------------------------- */
247 on_connection_disconnected (GDBusConnection *connection,
248 gboolean remote_peer_vanished,
252 Client *client = user_data;
254 if (client->name_owner_changed_subscription_id > 0)
255 g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
256 if (client->disconnected_signal_handler_id > 0)
257 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
258 g_object_unref (client->connection);
259 client->disconnected_signal_handler_id = 0;
260 client->name_owner_changed_subscription_id = 0;
261 client->connection = NULL;
263 call_vanished_handler (client, FALSE);
266 /* ---------------------------------------------------------------------------------------------------- */
269 on_name_owner_changed (GDBusConnection *connection,
270 const gchar *sender_name,
271 const gchar *object_path,
272 const gchar *interface_name,
273 const gchar *signal_name,
274 GVariant *parameters,
277 Client *client = user_data;
279 const gchar *old_owner;
280 const gchar *new_owner;
282 if (!client->initialized)
285 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
286 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
287 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
290 g_variant_get (parameters,
296 /* we only care about a specific name */
297 if (g_strcmp0 (name, client->name) != 0)
300 if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
302 g_free (client->name_owner);
303 client->name_owner = NULL;
304 call_vanished_handler (client, FALSE);
307 if (new_owner != NULL && strlen (new_owner) > 0)
309 g_warn_if_fail (client->name_owner == NULL);
310 g_free (client->name_owner);
311 client->name_owner = g_strdup (new_owner);
312 call_appeared_handler (client);
319 /* ---------------------------------------------------------------------------------------------------- */
322 get_name_owner_cb (GObject *source_object,
326 Client *client = user_data;
328 const char *name_owner;
333 result = g_dbus_connection_call_finish (client->connection,
338 g_variant_get (result, "(&s)", &name_owner);
341 if (name_owner != NULL)
343 g_warn_if_fail (client->name_owner == NULL);
344 client->name_owner = g_strdup (name_owner);
345 call_appeared_handler (client);
349 call_vanished_handler (client, FALSE);
352 client->initialized = TRUE;
355 g_variant_unref (result);
356 client_unref (client);
359 /* ---------------------------------------------------------------------------------------------------- */
362 invoke_get_name_owner (Client *client)
364 g_dbus_connection_call (client->connection,
365 "org.freedesktop.DBus", /* bus name */
366 "/org/freedesktop/DBus", /* object path */
367 "org.freedesktop.DBus", /* interface name */
368 "GetNameOwner", /* method name */
369 g_variant_new ("(s)", client->name),
370 G_VARIANT_TYPE ("(s)"),
371 G_DBUS_CALL_FLAGS_NONE,
374 (GAsyncReadyCallback) get_name_owner_cb,
375 client_ref (client));
378 /* ---------------------------------------------------------------------------------------------------- */
381 start_service_by_name_cb (GObject *source_object,
385 Client *client = user_data;
390 result = g_dbus_connection_call_finish (client->connection,
395 guint32 start_service_result;
396 g_variant_get (result, "(u)", &start_service_result);
398 if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
400 invoke_get_name_owner (client);
402 else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
404 invoke_get_name_owner (client);
408 g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
409 call_vanished_handler (client, FALSE);
410 client->initialized = TRUE;
415 /* Errors are not unexpected; the bus will reply e.g.
417 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
418 * was not provided by any .service files
420 * This doesn't mean that the name doesn't have an owner, just
421 * that it's not provided by a .service file. So proceed to
422 * invoke GetNameOwner().
424 invoke_get_name_owner (client);
428 g_variant_unref (result);
429 client_unref (client);
432 /* ---------------------------------------------------------------------------------------------------- */
435 has_connection (Client *client)
437 /* listen for disconnection */
438 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
440 G_CALLBACK (on_connection_disconnected),
443 /* start listening to NameOwnerChanged messages immediately */
444 client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
445 "org.freedesktop.DBus", /* name */
446 "org.freedesktop.DBus", /* if */
447 "NameOwnerChanged", /* signal */
448 "/org/freedesktop/DBus", /* path */
450 on_name_owner_changed,
454 if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
456 g_dbus_connection_call (client->connection,
457 "org.freedesktop.DBus", /* bus name */
458 "/org/freedesktop/DBus", /* object path */
459 "org.freedesktop.DBus", /* interface name */
460 "StartServiceByName", /* method name */
461 g_variant_new ("(su)", client->name, 0),
462 G_VARIANT_TYPE ("(u)"),
463 G_DBUS_CALL_FLAGS_NONE,
466 (GAsyncReadyCallback) start_service_by_name_cb,
467 client_ref (client));
472 invoke_get_name_owner (client);
478 connection_get_cb (GObject *source_object,
482 Client *client = user_data;
484 client->connection = g_bus_get_finish (res, NULL);
485 if (client->connection == NULL)
487 call_vanished_handler (client, FALSE);
491 has_connection (client);
494 client_unref (client);
497 /* ---------------------------------------------------------------------------------------------------- */
501 * @bus_type: The type of bus to watch a name on.
502 * @name: The name (well-known or unique) to watch.
503 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
504 * @name_appeared_handler: Handler to invoke when @name is known to exist or %NULL.
505 * @name_vanished_handler: Handler to invoke when @name is known to not exist or %NULL.
506 * @user_data: User data to pass to handlers.
507 * @user_data_free_func: Function for freeing @user_data or %NULL.
509 * Starts watching @name on the bus specified by @bus_type and calls
510 * @name_appeared_handler and @name_vanished_handler when the name is
511 * known to have a owner respectively known to lose its
512 * owner. Callbacks will be invoked in the <link
513 * linkend="g-main-context-push-thread-default">thread-default main
514 * loop</link> of the thread you are calling this function from.
516 * You are guaranteed that one of the handlers will be invoked after
517 * calling this function. When you are done watching the name, just
518 * call g_bus_unwatch_name() with the watcher id this function
521 * If the name vanishes or appears (for example the application owning
522 * the name could restart), the handlers are also invoked. If the
523 * #GDBusConnection that is used for watching the name disconnects, then
524 * @name_vanished_handler is invoked since it is no longer
525 * possible to access the name.
527 * Another guarantee is that invocations of @name_appeared_handler
528 * and @name_vanished_handler are guaranteed to alternate; that
529 * is, if @name_appeared_handler is invoked then you are
530 * guaranteed that the next time one of the handlers is invoked, it
531 * will be @name_vanished_handler. The reverse is also true.
533 * This behavior makes it very simple to write applications that wants
534 * to take action when a certain name exists, see <xref
535 * linkend="gdbus-watching-names"/>. Basically, the application
536 * should create object proxies in @name_appeared_handler and destroy
537 * them again (if any) in @name_vanished_handler.
539 * Returns: An identifier (never 0) that an be used with
540 * g_bus_unwatch_name() to stop watching the name.
545 g_bus_watch_name (GBusType bus_type,
547 GBusNameWatcherFlags flags,
548 GBusNameAppearedCallback name_appeared_handler,
549 GBusNameVanishedCallback name_vanished_handler,
551 GDestroyNotify user_data_free_func)
555 g_return_val_if_fail (g_dbus_is_name (name), 0);
559 client = g_new0 (Client, 1);
560 client->ref_count = 1;
561 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
562 client->name = g_strdup (name);
563 client->flags = flags;
564 client->name_appeared_handler = name_appeared_handler;
565 client->name_vanished_handler = name_vanished_handler;
566 client->user_data = user_data;
567 client->user_data_free_func = user_data_free_func;
568 client->main_context = g_main_context_get_thread_default ();
569 if (client->main_context != NULL)
570 g_main_context_ref (client->main_context);
572 if (map_id_to_client == NULL)
574 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
576 g_hash_table_insert (map_id_to_client,
577 GUINT_TO_POINTER (client->id),
583 client_ref (client));
591 * g_bus_watch_name_on_connection:
592 * @connection: A #GDBusConnection.
593 * @name: The name (well-known or unique) to watch.
594 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
595 * @name_appeared_handler: Handler to invoke when @name is known to exist or %NULL.
596 * @name_vanished_handler: Handler to invoke when @name is known to not exist or %NULL.
597 * @user_data: User data to pass to handlers.
598 * @user_data_free_func: Function for freeing @user_data or %NULL.
600 * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
603 * Returns: An identifier (never 0) that an be used with
604 * g_bus_unwatch_name() to stop watching the name.
608 guint g_bus_watch_name_on_connection (GDBusConnection *connection,
610 GBusNameWatcherFlags flags,
611 GBusNameAppearedCallback name_appeared_handler,
612 GBusNameVanishedCallback name_vanished_handler,
614 GDestroyNotify user_data_free_func)
618 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
619 g_return_val_if_fail (g_dbus_is_name (name), 0);
623 client = g_new0 (Client, 1);
624 client->ref_count = 1;
625 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
626 client->name = g_strdup (name);
627 client->flags = flags;
628 client->name_appeared_handler = name_appeared_handler;
629 client->name_vanished_handler = name_vanished_handler;
630 client->user_data = user_data;
631 client->user_data_free_func = user_data_free_func;
632 client->main_context = g_main_context_get_thread_default ();
633 if (client->main_context != NULL)
634 g_main_context_ref (client->main_context);
636 if (map_id_to_client == NULL)
638 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
640 g_hash_table_insert (map_id_to_client,
641 GUINT_TO_POINTER (client->id),
644 client->connection = g_object_ref (connection);
647 has_connection (client);
653 GClosure *name_appeared_closure;
654 GClosure *name_vanished_closure;
658 watch_with_closures_on_name_appeared (GDBusConnection *connection,
660 const gchar *name_owner,
663 WatchNameData *data = user_data;
664 GValue params[3] = { { 0, }, { 0, }, { 0, } };
666 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
667 g_value_set_object (¶ms[0], connection);
669 g_value_init (¶ms[1], G_TYPE_STRING);
670 g_value_set_string (¶ms[1], name);
672 g_value_init (¶ms[2], G_TYPE_STRING);
673 g_value_set_string (¶ms[2], name_owner);
675 g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
679 watch_with_closures_on_name_vanished (GDBusConnection *connection,
683 WatchNameData *data = user_data;
684 GValue params[2] = { { 0, }, { 0, } };
686 g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
687 g_value_set_object (¶ms[0], connection);
689 g_value_init (¶ms[1], G_TYPE_STRING);
690 g_value_set_string (¶ms[1], name);
692 g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
696 bus_watch_name_free_func (gpointer user_data)
698 WatchNameData *data = user_data;
700 if (data->name_appeared_closure != NULL)
701 g_closure_unref (data->name_appeared_closure);
703 if (data->name_vanished_closure != NULL)
704 g_closure_unref (data->name_vanished_closure);
710 * g_bus_watch_name_with_closures:
711 * @bus_type: The type of bus to watch a name on.
712 * @name: The name (well-known or unique) to watch.
713 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
714 * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
716 * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
717 * to not exist or %NULL.
719 * Version of g_bus_watch_name() using closures instead of callbacks for
720 * easier binding in other languages.
722 * Returns: An identifier (never 0) that an be used with
723 * g_bus_unwatch_name() to stop watching the name.
725 * Rename to: g_bus_watch_name
730 g_bus_watch_name_with_closures (GBusType bus_type,
732 GBusNameWatcherFlags flags,
733 GClosure *name_appeared_closure,
734 GClosure *name_vanished_closure)
738 data = g_new0 (WatchNameData, 1);
740 if (name_appeared_closure != NULL)
742 data->name_appeared_closure = g_closure_ref (name_appeared_closure);
743 g_closure_sink (name_appeared_closure);
746 if (name_vanished_closure != NULL)
748 data->name_vanished_closure = g_closure_ref (name_vanished_closure);
749 g_closure_sink (name_vanished_closure);
752 return g_bus_watch_name (bus_type,
755 name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
756 name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
758 bus_watch_name_free_func);
762 * g_bus_watch_name_on_connection_with_closures:
763 * @connection: A #GDBusConnection.
764 * @name: The name (well-known or unique) to watch.
765 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
766 * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known
768 * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known
769 * to not exist or %NULL.
771 * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
772 * easier binding in other languages.
774 * Returns: An identifier (never 0) that an be used with
775 * g_bus_unwatch_name() to stop watching the name.
777 * Rename to: g_bus_watch_name_on_connection
781 guint g_bus_watch_name_on_connection_with_closures (
782 GDBusConnection *connection,
784 GBusNameWatcherFlags flags,
785 GClosure *name_appeared_closure,
786 GClosure *name_vanished_closure)
790 data = g_new0 (WatchNameData, 1);
792 if (name_appeared_closure != NULL)
794 data->name_appeared_closure = g_closure_ref (name_appeared_closure);
795 g_closure_sink (name_appeared_closure);
798 if (name_vanished_closure != NULL)
800 data->name_vanished_closure = g_closure_ref (name_vanished_closure);
801 g_closure_sink (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,
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);