1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2009 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>
27 #include <glib/gi18n.h>
29 #include "gdbusutils.h"
30 #include "gdbusnamewatching.h"
31 #include "gdbuserror.h"
32 #include "gdbusprivate.h"
33 #include "gdbusconnection.h"
36 * SECTION:gdbusnamewatching
37 * @title: Watching Bus Names
38 * @short_description: Simple API for watching bus names
41 * Convenience API for watching bus names.
43 * <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>
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 if (client->main_context != NULL)
106 g_main_context_unref (client->main_context);
107 if (client->user_data_free_func != NULL)
108 client->user_data_free_func (client->user_data);
113 /* ---------------------------------------------------------------------------------------------------- */
117 CALL_TYPE_NAME_APPEARED,
118 CALL_TYPE_NAME_VANISHED
125 /* keep this separate because client->connection may
126 * be set to NULL after scheduling the call
128 GDBusConnection *connection;
137 call_handler_data_free (CallHandlerData *data)
139 if (data->connection != NULL)
140 g_object_unref (data->connection);
141 g_free (data->name_owner);
142 client_unref (data->client);
147 actually_do_call (Client *client, GDBusConnection *connection, const gchar *name_owner, CallType call_type)
151 case CALL_TYPE_NAME_APPEARED:
152 if (client->name_appeared_handler != NULL)
154 client->name_appeared_handler (connection,
161 case CALL_TYPE_NAME_VANISHED:
162 if (client->name_vanished_handler != NULL)
164 client->name_vanished_handler (connection,
171 g_assert_not_reached ();
177 call_in_idle_cb (gpointer _data)
179 CallHandlerData *data = _data;
180 actually_do_call (data->client, data->connection, data->name_owner, data->call_type);
185 schedule_call_in_idle (Client *client, CallType call_type)
187 CallHandlerData *data;
188 GSource *idle_source;
190 data = g_new0 (CallHandlerData, 1);
191 data->client = client_ref (client);
192 data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
193 data->name_owner = g_strdup (client->name_owner);
194 data->call_type = call_type;
196 idle_source = g_idle_source_new ();
197 g_source_set_priority (idle_source, G_PRIORITY_HIGH);
198 g_source_set_callback (idle_source,
201 (GDestroyNotify) call_handler_data_free);
202 g_source_attach (idle_source, client->main_context);
203 g_source_unref (idle_source);
207 do_call (Client *client, CallType call_type)
209 /* only schedule in idle if we're not in the right thread */
210 if (g_main_context_get_thread_default () != client->main_context)
211 schedule_call_in_idle (client, call_type);
213 actually_do_call (client, client->connection, client->name_owner, call_type);
217 call_appeared_handler (Client *client)
219 if (client->previous_call != PREVIOUS_CALL_APPEARED)
221 client->previous_call = PREVIOUS_CALL_APPEARED;
222 if (!client->cancelled && client->name_appeared_handler != NULL)
224 do_call (client, CALL_TYPE_NAME_APPEARED);
230 call_vanished_handler (Client *client,
231 gboolean ignore_cancelled)
233 if (client->previous_call != PREVIOUS_CALL_VANISHED)
235 client->previous_call = PREVIOUS_CALL_VANISHED;
236 if (((!client->cancelled) || ignore_cancelled) && client->name_vanished_handler != NULL)
238 do_call (client, CALL_TYPE_NAME_VANISHED);
243 /* ---------------------------------------------------------------------------------------------------- */
246 on_connection_disconnected (GDBusConnection *connection,
247 gboolean remote_peer_vanished,
251 Client *client = user_data;
253 if (client->name_owner_changed_subscription_id > 0)
254 g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
255 if (client->disconnected_signal_handler_id > 0)
256 g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
257 g_object_unref (client->connection);
258 client->disconnected_signal_handler_id = 0;
259 client->name_owner_changed_subscription_id = 0;
260 client->connection = NULL;
262 call_vanished_handler (client, FALSE);
265 /* ---------------------------------------------------------------------------------------------------- */
268 on_name_owner_changed (GDBusConnection *connection,
269 const gchar *sender_name,
270 const gchar *object_path,
271 const gchar *interface_name,
272 const gchar *signal_name,
273 GVariant *parameters,
276 Client *client = user_data;
278 const gchar *old_owner;
279 const gchar *new_owner;
281 if (!client->initialized)
284 if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
285 g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
286 g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
289 g_variant_get (parameters,
295 /* we only care about a specific name */
296 if (g_strcmp0 (name, client->name) != 0)
299 if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
301 g_free (client->name_owner);
302 client->name_owner = NULL;
303 call_vanished_handler (client, FALSE);
306 if (new_owner != NULL && strlen (new_owner) > 0)
308 g_warn_if_fail (client->name_owner == NULL);
309 g_free (client->name_owner);
310 client->name_owner = g_strdup (new_owner);
311 call_appeared_handler (client);
318 /* ---------------------------------------------------------------------------------------------------- */
321 get_name_owner_cb (GObject *source_object,
325 Client *client = user_data;
327 const char *name_owner;
332 result = g_dbus_connection_invoke_method_finish (client->connection,
337 g_variant_get (result, "(s)", &name_owner);
340 if (name_owner != NULL)
342 g_warn_if_fail (client->name_owner == NULL);
343 client->name_owner = g_strdup (name_owner);
344 call_appeared_handler (client);
348 call_vanished_handler (client, FALSE);
351 client->initialized = TRUE;
354 g_variant_unref (result);
355 client_unref (client);
358 /* ---------------------------------------------------------------------------------------------------- */
361 invoke_get_name_owner (Client *client)
363 g_dbus_connection_invoke_method (client->connection,
364 "org.freedesktop.DBus", /* bus name */
365 "/org/freedesktop/DBus", /* object path */
366 "org.freedesktop.DBus", /* interface name */
367 "GetNameOwner", /* method name */
368 g_variant_new ("(s)", client->name),
369 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
372 (GAsyncReadyCallback) get_name_owner_cb,
373 client_ref (client));
376 /* ---------------------------------------------------------------------------------------------------- */
379 start_service_by_name_cb (GObject *source_object,
383 Client *client = user_data;
388 result = g_dbus_connection_invoke_method_finish (client->connection,
393 guint32 start_service_result;
394 g_variant_get (result, "(u)", &start_service_result);
396 if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
398 invoke_get_name_owner (client);
400 else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
402 invoke_get_name_owner (client);
406 g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
407 call_vanished_handler (client, FALSE);
408 client->initialized = TRUE;
413 /* Errors are not unexpected; the bus will reply e.g.
415 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
416 * was not provided by any .service files
418 * so just report vanished.
420 call_vanished_handler (client, FALSE);
421 client->initialized = TRUE;
425 g_variant_unref (result);
426 client_unref (client);
429 /* ---------------------------------------------------------------------------------------------------- */
432 has_connection (Client *client)
434 /* listen for disconnection */
435 client->disconnected_signal_handler_id = g_signal_connect (client->connection,
437 G_CALLBACK (on_connection_disconnected),
440 /* start listening to NameOwnerChanged messages immediately */
441 client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
442 "org.freedesktop.DBus", /* name */
443 "org.freedesktop.DBus", /* if */
444 "NameOwnerChanged", /* signal */
445 "/org/freedesktop/DBus", /* path */
447 on_name_owner_changed,
451 if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
453 g_dbus_connection_invoke_method (client->connection,
454 "org.freedesktop.DBus", /* bus name */
455 "/org/freedesktop/DBus", /* object path */
456 "org.freedesktop.DBus", /* interface name */
457 "StartServiceByName", /* method name */
458 g_variant_new ("(su)", client->name, 0),
459 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
462 (GAsyncReadyCallback) start_service_by_name_cb,
463 client_ref (client));
468 invoke_get_name_owner (client);
474 connection_get_cb (GObject *source_object,
478 Client *client = user_data;
480 client->connection = g_bus_get_finish (res, NULL);
481 if (client->connection == NULL)
483 call_vanished_handler (client, FALSE);
487 has_connection (client);
490 client_unref (client);
493 /* ---------------------------------------------------------------------------------------------------- */
497 * @bus_type: The type of bus to watch a name on.
498 * @name: The name (well-known or unique) to watch.
499 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
500 * @name_appeared_handler: Handler to invoke when @name is known to exist or %NULL.
501 * @name_vanished_handler: Handler to invoke when @name is known to not exist or %NULL.
502 * @user_data: User data to pass to handlers.
503 * @user_data_free_func: Function for freeing @user_data or %NULL.
505 * Starts watching @name on the bus specified by @bus_type and calls
506 * @name_appeared_handler and @name_vanished_handler when the name is
507 * known to have a owner respectively known to lose its
508 * owner. Callbacks will be invoked in the <link
509 * linkend="g-main-context-push-thread-default">thread-default main
510 * loop</link> of the thread you are calling this function from.
512 * You are guaranteed that one of the handlers will be invoked after
513 * calling this function. When you are done watching the name, just
514 * call g_bus_unwatch_name() with the watcher id this function
517 * If the name vanishes or appears (for example the application owning
518 * the name could restart), the handlers are also invoked. If the
519 * #GDBusConnection that is used for watching the name disconnects, then
520 * @name_vanished_handler is invoked since it is no longer
521 * possible to access the name.
523 * Another guarantee is that invocations of @name_appeared_handler
524 * and @name_vanished_handler are guaranteed to alternate; that
525 * is, if @name_appeared_handler is invoked then you are
526 * guaranteed that the next time one of the handlers is invoked, it
527 * will be @name_vanished_handler. The reverse is also true.
529 * This behavior makes it very simple to write applications that wants
530 * to take action when a certain name exists, see <xref
531 * linkend="gdbus-watching-names"/>. Basically, the application
532 * should create object proxies in @name_appeared_handler and destroy
533 * them again (if any) in @name_vanished_handler.
535 * Returns: An identifier (never 0) that an be used with
536 * g_bus_unwatch_name() to stop watching the name.
541 g_bus_watch_name (GBusType bus_type,
543 GBusNameWatcherFlags flags,
544 GBusNameAppearedCallback name_appeared_handler,
545 GBusNameVanishedCallback name_vanished_handler,
547 GDestroyNotify user_data_free_func)
551 g_return_val_if_fail (bus_type != G_BUS_TYPE_NONE, 0);
552 g_return_val_if_fail (g_dbus_is_name (name), 0);
556 client = g_new0 (Client, 1);
557 client->ref_count = 1;
558 client->id = next_global_id++; /* TODO: uh oh, handle overflow */
559 client->name = g_strdup (name);
560 client->flags = flags;
561 client->name_appeared_handler = name_appeared_handler;
562 client->name_vanished_handler = name_vanished_handler;
563 client->user_data = user_data;
564 client->user_data_free_func = user_data_free_func;
565 client->main_context = g_main_context_get_thread_default ();
566 if (client->main_context != NULL)
567 g_main_context_ref (client->main_context);
569 if (map_id_to_client == NULL)
571 map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
573 g_hash_table_insert (map_id_to_client,
574 GUINT_TO_POINTER (client->id),
580 client_ref (client));
588 * g_bus_unwatch_name:
589 * @watcher_id: An identifier obtained from g_bus_watch_name()
591 * Stops watching a name.
596 g_bus_unwatch_name (guint watcher_id)
600 g_return_if_fail (watcher_id > 0);
605 if (watcher_id == 0 ||
606 map_id_to_client == NULL ||
607 (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
609 g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
613 client->cancelled = TRUE;
614 g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
619 /* do callback without holding lock */
622 client_unref (client);