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 /* Must be accessed atomically. */
81 static volatile guint next_global_id
= 1;
83 /* Must be accessed with @lock held. */
84 static GHashTable
*map_id_to_client
= NULL
;
87 client_ref (Client
*client
)
89 g_atomic_int_inc (&client
->ref_count
);
94 client_unref (Client
*client
)
96 if (g_atomic_int_dec_and_test (&client
->ref_count
))
98 if (client
->connection
!= NULL
)
100 if (client
->name_owner_changed_subscription_id
> 0)
101 g_dbus_connection_signal_unsubscribe (client
->connection
, client
->name_owner_changed_subscription_id
);
102 if (client
->disconnected_signal_handler_id
> 0)
103 g_signal_handler_disconnect (client
->connection
, client
->disconnected_signal_handler_id
);
104 g_object_unref (client
->connection
);
106 g_free (client
->name
);
107 g_free (client
->name_owner
);
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_set_name (idle_source
, "[gio] call_in_idle_cb");
205 g_source_attach (idle_source
, client
->main_context
);
206 g_source_unref (idle_source
);
210 do_call (Client
*client
, CallType call_type
)
212 GMainContext
*current_context
;
214 /* only schedule in idle if we're not in the right thread */
215 current_context
= g_main_context_ref_thread_default ();
216 if (current_context
!= client
->main_context
)
217 schedule_call_in_idle (client
, call_type
);
219 actually_do_call (client
, client
->connection
, client
->name_owner
, call_type
);
220 g_main_context_unref (current_context
);
224 call_appeared_handler (Client
*client
)
226 if (client
->previous_call
!= PREVIOUS_CALL_APPEARED
)
228 client
->previous_call
= PREVIOUS_CALL_APPEARED
;
229 if (!client
->cancelled
&& client
->name_appeared_handler
!= NULL
)
231 do_call (client
, CALL_TYPE_NAME_APPEARED
);
237 call_vanished_handler (Client
*client
,
238 gboolean ignore_cancelled
)
240 if (client
->previous_call
!= PREVIOUS_CALL_VANISHED
)
242 client
->previous_call
= PREVIOUS_CALL_VANISHED
;
243 if (((!client
->cancelled
) || ignore_cancelled
) && client
->name_vanished_handler
!= NULL
)
245 do_call (client
, CALL_TYPE_NAME_VANISHED
);
250 /* ---------------------------------------------------------------------------------------------------- */
253 on_connection_disconnected (GDBusConnection
*connection
,
254 gboolean remote_peer_vanished
,
258 Client
*client
= user_data
;
260 if (client
->name_owner_changed_subscription_id
> 0)
261 g_dbus_connection_signal_unsubscribe (client
->connection
, client
->name_owner_changed_subscription_id
);
262 if (client
->disconnected_signal_handler_id
> 0)
263 g_signal_handler_disconnect (client
->connection
, client
->disconnected_signal_handler_id
);
264 g_object_unref (client
->connection
);
265 client
->disconnected_signal_handler_id
= 0;
266 client
->name_owner_changed_subscription_id
= 0;
267 client
->connection
= NULL
;
269 call_vanished_handler (client
, FALSE
);
272 /* ---------------------------------------------------------------------------------------------------- */
275 on_name_owner_changed (GDBusConnection
*connection
,
276 const gchar
*sender_name
,
277 const gchar
*object_path
,
278 const gchar
*interface_name
,
279 const gchar
*signal_name
,
280 GVariant
*parameters
,
283 Client
*client
= user_data
;
285 const gchar
*old_owner
;
286 const gchar
*new_owner
;
288 if (!client
->initialized
)
291 if (g_strcmp0 (object_path
, "/org/freedesktop/DBus") != 0 ||
292 g_strcmp0 (interface_name
, "org.freedesktop.DBus") != 0 ||
293 g_strcmp0 (sender_name
, "org.freedesktop.DBus") != 0)
296 g_variant_get (parameters
,
302 /* we only care about a specific name */
303 if (g_strcmp0 (name
, client
->name
) != 0)
306 if ((old_owner
!= NULL
&& strlen (old_owner
) > 0) && client
->name_owner
!= NULL
)
308 g_free (client
->name_owner
);
309 client
->name_owner
= NULL
;
310 call_vanished_handler (client
, FALSE
);
313 if (new_owner
!= NULL
&& strlen (new_owner
) > 0)
315 g_warn_if_fail (client
->name_owner
== NULL
);
316 g_free (client
->name_owner
);
317 client
->name_owner
= g_strdup (new_owner
);
318 call_appeared_handler (client
);
325 /* ---------------------------------------------------------------------------------------------------- */
328 get_name_owner_cb (GObject
*source_object
,
332 Client
*client
= user_data
;
334 const char *name_owner
;
339 result
= g_dbus_connection_call_finish (client
->connection
,
344 g_variant_get (result
, "(&s)", &name_owner
);
347 if (name_owner
!= NULL
)
349 g_warn_if_fail (client
->name_owner
== NULL
);
350 client
->name_owner
= g_strdup (name_owner
);
351 call_appeared_handler (client
);
355 call_vanished_handler (client
, FALSE
);
358 client
->initialized
= TRUE
;
361 g_variant_unref (result
);
362 client_unref (client
);
365 /* ---------------------------------------------------------------------------------------------------- */
368 invoke_get_name_owner (Client
*client
)
370 g_dbus_connection_call (client
->connection
,
371 "org.freedesktop.DBus", /* bus name */
372 "/org/freedesktop/DBus", /* object path */
373 "org.freedesktop.DBus", /* interface name */
374 "GetNameOwner", /* method name */
375 g_variant_new ("(s)", client
->name
),
376 G_VARIANT_TYPE ("(s)"),
377 G_DBUS_CALL_FLAGS_NONE
,
380 (GAsyncReadyCallback
) get_name_owner_cb
,
381 client_ref (client
));
384 /* ---------------------------------------------------------------------------------------------------- */
387 start_service_by_name_cb (GObject
*source_object
,
391 Client
*client
= user_data
;
396 result
= g_dbus_connection_call_finish (client
->connection
,
401 guint32 start_service_result
;
402 g_variant_get (result
, "(u)", &start_service_result
);
404 if (start_service_result
== 1) /* DBUS_START_REPLY_SUCCESS */
406 invoke_get_name_owner (client
);
408 else if (start_service_result
== 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
410 invoke_get_name_owner (client
);
414 g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result
);
415 call_vanished_handler (client
, FALSE
);
416 client
->initialized
= TRUE
;
421 /* Errors are not unexpected; the bus will reply e.g.
423 * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
424 * was not provided by any .service files
426 * This doesn't mean that the name doesn't have an owner, just
427 * that it's not provided by a .service file. So proceed to
428 * invoke GetNameOwner().
430 invoke_get_name_owner (client
);
434 g_variant_unref (result
);
435 client_unref (client
);
438 /* ---------------------------------------------------------------------------------------------------- */
441 has_connection (Client
*client
)
443 /* listen for disconnection */
444 client
->disconnected_signal_handler_id
= g_signal_connect (client
->connection
,
446 G_CALLBACK (on_connection_disconnected
),
449 /* start listening to NameOwnerChanged messages immediately */
450 client
->name_owner_changed_subscription_id
= g_dbus_connection_signal_subscribe (client
->connection
,
451 "org.freedesktop.DBus", /* name */
452 "org.freedesktop.DBus", /* if */
453 "NameOwnerChanged", /* signal */
454 "/org/freedesktop/DBus", /* path */
456 G_DBUS_SIGNAL_FLAGS_NONE
,
457 on_name_owner_changed
,
461 if (client
->flags
& G_BUS_NAME_WATCHER_FLAGS_AUTO_START
)
463 g_dbus_connection_call (client
->connection
,
464 "org.freedesktop.DBus", /* bus name */
465 "/org/freedesktop/DBus", /* object path */
466 "org.freedesktop.DBus", /* interface name */
467 "StartServiceByName", /* method name */
468 g_variant_new ("(su)", client
->name
, 0),
469 G_VARIANT_TYPE ("(u)"),
470 G_DBUS_CALL_FLAGS_NONE
,
473 (GAsyncReadyCallback
) start_service_by_name_cb
,
474 client_ref (client
));
479 invoke_get_name_owner (client
);
485 connection_get_cb (GObject
*source_object
,
489 Client
*client
= user_data
;
491 client
->connection
= g_bus_get_finish (res
, NULL
);
492 if (client
->connection
== NULL
)
494 call_vanished_handler (client
, FALSE
);
498 has_connection (client
);
501 client_unref (client
);
504 /* ---------------------------------------------------------------------------------------------------- */
508 * @bus_type: The type of bus to watch a name on.
509 * @name: The name (well-known or unique) to watch.
510 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
511 * @name_appeared_handler: (nullable): Handler to invoke when @name is known to exist or %NULL.
512 * @name_vanished_handler: (nullable): Handler to invoke when @name is known to not exist or %NULL.
513 * @user_data: User data to pass to handlers.
514 * @user_data_free_func: (nullable): Function for freeing @user_data or %NULL.
516 * Starts watching @name on the bus specified by @bus_type and calls
517 * @name_appeared_handler and @name_vanished_handler when the name is
518 * known to have a owner respectively known to lose its
519 * owner. Callbacks will be invoked in the
520 * [thread-default main context][g-main-context-push-thread-default]
521 * of the thread you are calling this function from.
523 * You are guaranteed that one of the handlers will be invoked after
524 * calling this function. When you are done watching the name, just
525 * call g_bus_unwatch_name() with the watcher id this function
528 * If the name vanishes or appears (for example the application owning
529 * the name could restart), the handlers are also invoked. If the
530 * #GDBusConnection that is used for watching the name disconnects, then
531 * @name_vanished_handler is invoked since it is no longer
532 * possible to access the name.
534 * Another guarantee is that invocations of @name_appeared_handler
535 * and @name_vanished_handler are guaranteed to alternate; that
536 * is, if @name_appeared_handler is invoked then you are
537 * guaranteed that the next time one of the handlers is invoked, it
538 * will be @name_vanished_handler. The reverse is also true.
540 * This behavior makes it very simple to write applications that want
541 * to take action when a certain [name exists][gdbus-watching-names].
542 * Basically, the application should create object proxies in
543 * @name_appeared_handler and destroy them again (if any) in
544 * @name_vanished_handler.
546 * Returns: An identifier (never 0) that an be used with
547 * g_bus_unwatch_name() to stop watching the name.
552 g_bus_watch_name (GBusType bus_type
,
554 GBusNameWatcherFlags flags
,
555 GBusNameAppearedCallback name_appeared_handler
,
556 GBusNameVanishedCallback name_vanished_handler
,
558 GDestroyNotify user_data_free_func
)
562 g_return_val_if_fail (g_dbus_is_name (name
), 0);
566 client
= g_new0 (Client
, 1);
567 client
->ref_count
= 1;
568 client
->id
= g_atomic_int_add (&next_global_id
, 1); /* TODO: uh oh, handle overflow */
569 client
->name
= g_strdup (name
);
570 client
->flags
= flags
;
571 client
->name_appeared_handler
= name_appeared_handler
;
572 client
->name_vanished_handler
= name_vanished_handler
;
573 client
->user_data
= user_data
;
574 client
->user_data_free_func
= user_data_free_func
;
575 client
->main_context
= g_main_context_ref_thread_default ();
577 if (map_id_to_client
== NULL
)
579 map_id_to_client
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
581 g_hash_table_insert (map_id_to_client
,
582 GUINT_TO_POINTER (client
->id
),
588 client_ref (client
));
596 * g_bus_watch_name_on_connection:
597 * @connection: A #GDBusConnection.
598 * @name: The name (well-known or unique) to watch.
599 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
600 * @name_appeared_handler: (nullable): Handler to invoke when @name is known to exist or %NULL.
601 * @name_vanished_handler: (nullable): Handler to invoke when @name is known to not exist or %NULL.
602 * @user_data: User data to pass to handlers.
603 * @user_data_free_func: (nullable): Function for freeing @user_data or %NULL.
605 * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
608 * Returns: An identifier (never 0) that an be used with
609 * g_bus_unwatch_name() to stop watching the name.
613 guint
g_bus_watch_name_on_connection (GDBusConnection
*connection
,
615 GBusNameWatcherFlags flags
,
616 GBusNameAppearedCallback name_appeared_handler
,
617 GBusNameVanishedCallback name_vanished_handler
,
619 GDestroyNotify user_data_free_func
)
623 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
624 g_return_val_if_fail (g_dbus_is_name (name
), 0);
628 client
= g_new0 (Client
, 1);
629 client
->ref_count
= 1;
630 client
->id
= g_atomic_int_add (&next_global_id
, 1); /* TODO: uh oh, handle overflow */
631 client
->name
= g_strdup (name
);
632 client
->flags
= flags
;
633 client
->name_appeared_handler
= name_appeared_handler
;
634 client
->name_vanished_handler
= name_vanished_handler
;
635 client
->user_data
= user_data
;
636 client
->user_data_free_func
= user_data_free_func
;
637 client
->main_context
= g_main_context_ref_thread_default ();
639 if (map_id_to_client
== NULL
)
640 map_id_to_client
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
642 g_hash_table_insert (map_id_to_client
,
643 GUINT_TO_POINTER (client
->id
),
646 client
->connection
= g_object_ref (connection
);
649 has_connection (client
);
655 GClosure
*name_appeared_closure
;
656 GClosure
*name_vanished_closure
;
659 static WatchNameData
*
660 watch_name_data_new (GClosure
*name_appeared_closure
,
661 GClosure
*name_vanished_closure
)
665 data
= g_new0 (WatchNameData
, 1);
667 if (name_appeared_closure
!= NULL
)
669 data
->name_appeared_closure
= g_closure_ref (name_appeared_closure
);
670 g_closure_sink (name_appeared_closure
);
671 if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure
))
672 g_closure_set_marshal (name_appeared_closure
, g_cclosure_marshal_generic
);
675 if (name_vanished_closure
!= NULL
)
677 data
->name_vanished_closure
= g_closure_ref (name_vanished_closure
);
678 g_closure_sink (name_vanished_closure
);
679 if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure
))
680 g_closure_set_marshal (name_vanished_closure
, g_cclosure_marshal_generic
);
687 watch_with_closures_on_name_appeared (GDBusConnection
*connection
,
689 const gchar
*name_owner
,
692 WatchNameData
*data
= user_data
;
693 GValue params
[3] = { G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
};
695 g_value_init (¶ms
[0], G_TYPE_DBUS_CONNECTION
);
696 g_value_set_object (¶ms
[0], connection
);
698 g_value_init (¶ms
[1], G_TYPE_STRING
);
699 g_value_set_string (¶ms
[1], name
);
701 g_value_init (¶ms
[2], G_TYPE_STRING
);
702 g_value_set_string (¶ms
[2], name_owner
);
704 g_closure_invoke (data
->name_appeared_closure
, NULL
, 3, params
, NULL
);
706 g_value_unset (params
+ 0);
707 g_value_unset (params
+ 1);
708 g_value_unset (params
+ 2);
712 watch_with_closures_on_name_vanished (GDBusConnection
*connection
,
716 WatchNameData
*data
= user_data
;
717 GValue params
[2] = { G_VALUE_INIT
, G_VALUE_INIT
};
719 g_value_init (¶ms
[0], G_TYPE_DBUS_CONNECTION
);
720 g_value_set_object (¶ms
[0], connection
);
722 g_value_init (¶ms
[1], G_TYPE_STRING
);
723 g_value_set_string (¶ms
[1], name
);
725 g_closure_invoke (data
->name_vanished_closure
, NULL
, 2, params
, NULL
);
727 g_value_unset (params
+ 0);
728 g_value_unset (params
+ 1);
732 bus_watch_name_free_func (gpointer user_data
)
734 WatchNameData
*data
= user_data
;
736 if (data
->name_appeared_closure
!= NULL
)
737 g_closure_unref (data
->name_appeared_closure
);
739 if (data
->name_vanished_closure
!= NULL
)
740 g_closure_unref (data
->name_vanished_closure
);
746 * g_bus_watch_name_with_closures: (rename-to g_bus_watch_name)
747 * @bus_type: The type of bus to watch a name on.
748 * @name: The name (well-known or unique) to watch.
749 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
750 * @name_appeared_closure: (nullable): #GClosure to invoke when @name is known
752 * @name_vanished_closure: (nullable): #GClosure to invoke when @name is known
753 * to not exist or %NULL.
755 * Version of g_bus_watch_name() using closures instead of callbacks for
756 * easier binding in other languages.
758 * Returns: An identifier (never 0) that an be used with
759 * g_bus_unwatch_name() to stop watching the name.
764 g_bus_watch_name_with_closures (GBusType bus_type
,
766 GBusNameWatcherFlags flags
,
767 GClosure
*name_appeared_closure
,
768 GClosure
*name_vanished_closure
)
770 return g_bus_watch_name (bus_type
,
773 name_appeared_closure
!= NULL
? watch_with_closures_on_name_appeared
: NULL
,
774 name_vanished_closure
!= NULL
? watch_with_closures_on_name_vanished
: NULL
,
775 watch_name_data_new (name_appeared_closure
, name_vanished_closure
),
776 bus_watch_name_free_func
);
780 * g_bus_watch_name_on_connection_with_closures: (rename-to g_bus_watch_name_on_connection)
781 * @connection: A #GDBusConnection.
782 * @name: The name (well-known or unique) to watch.
783 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
784 * @name_appeared_closure: (nullable): #GClosure to invoke when @name is known
786 * @name_vanished_closure: (nullable): #GClosure to invoke when @name is known
787 * to not exist or %NULL.
789 * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
790 * easier binding in other languages.
792 * Returns: An identifier (never 0) that an be used with
793 * g_bus_unwatch_name() to stop watching the name.
797 guint
g_bus_watch_name_on_connection_with_closures (
798 GDBusConnection
*connection
,
800 GBusNameWatcherFlags flags
,
801 GClosure
*name_appeared_closure
,
802 GClosure
*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
,
809 watch_name_data_new (name_appeared_closure
, name_vanished_closure
),
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
);