X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gio%2Fgdbusnamewatching.c;h=adf53c3815c4dd764bc11349c0b1e4e22a6f0694;hb=7a1aaaa1fa02679ecf335a19fffe3f55505921b5;hp=f6126e5aeab2bd4678aedcde33fbd52fdf521c7a;hpb=0fd6498cd89888023fb2161bfdde9339a4346986;p=platform%2Fupstream%2Fglib.git diff --git a/gio/gdbusnamewatching.c b/gio/gdbusnamewatching.c index f6126e5..adf53c3 100644 --- a/gio/gdbusnamewatching.c +++ b/gio/gdbusnamewatching.c @@ -1,6 +1,6 @@ /* GDBus - GLib D-Bus Library * - * Copyright (C) 2008-2009 Red Hat, Inc. + * Copyright (C) 2008-2010 Red Hat, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -13,9 +13,7 @@ * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General - * Public License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place, Suite 330, - * Boston, MA 02111-1307, USA. + * Public License along with this library; if not, see . * * Author: David Zeuthen */ @@ -23,8 +21,7 @@ #include "config.h" #include - -#include +#include #include "gdbusutils.h" #include "gdbusnamewatching.h" @@ -32,6 +29,8 @@ #include "gdbusprivate.h" #include "gdbusconnection.h" +#include "glibintl.h" + /** * SECTION:gdbusnamewatching * @title: Watching Bus Names @@ -40,7 +39,8 @@ * * Convenience API for watching bus names. * - * Simple application watching a nameFIXME: MISSING XINCLUDE CONTENT + * A simple example for watching a name can be found in + * [gdbus-example-watch-name.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-watch-name.c) */ G_LOCK_DEFINE_STATIC (lock); @@ -102,8 +102,7 @@ client_unref (Client *client) } g_free (client->name); g_free (client->name_owner); - if (client->main_context != NULL) - g_main_context_unref (client->main_context); + g_main_context_unref (client->main_context); if (client->user_data_free_func != NULL) client->user_data_free_func (client->user_data); g_free (client); @@ -199,6 +198,7 @@ schedule_call_in_idle (Client *client, CallType call_type) call_in_idle_cb, data, (GDestroyNotify) call_handler_data_free); + g_source_set_name (idle_source, "[gio] call_in_idle_cb"); g_source_attach (idle_source, client->main_context); g_source_unref (idle_source); } @@ -206,11 +206,15 @@ schedule_call_in_idle (Client *client, CallType call_type) static void do_call (Client *client, CallType call_type) { + GMainContext *current_context; + /* only schedule in idle if we're not in the right thread */ - if (g_main_context_get_thread_default () != client->main_context) + current_context = g_main_context_ref_thread_default (); + if (current_context != client->main_context) schedule_call_in_idle (client, call_type); else actually_do_call (client, client->connection, client->name_owner, call_type); + g_main_context_unref (current_context); } static void @@ -287,7 +291,7 @@ on_name_owner_changed (GDBusConnection *connection, goto out; g_variant_get (parameters, - "(sss)", + "(&s&s&s)", &name, &old_owner, &new_owner); @@ -329,12 +333,12 @@ get_name_owner_cb (GObject *source_object, name_owner = NULL; result = NULL; - result = g_dbus_connection_invoke_method_finish (client->connection, - res, - NULL); + result = g_dbus_connection_call_finish (client->connection, + res, + NULL); if (result != NULL) { - g_variant_get (result, "(s)", &name_owner); + g_variant_get (result, "(&s)", &name_owner); } if (name_owner != NULL) @@ -360,17 +364,18 @@ get_name_owner_cb (GObject *source_object, static void invoke_get_name_owner (Client *client) { - g_dbus_connection_invoke_method (client->connection, - "org.freedesktop.DBus", /* bus name */ - "/org/freedesktop/DBus", /* object path */ - "org.freedesktop.DBus", /* interface name */ - "GetNameOwner", /* method name */ - g_variant_new ("(s)", client->name), - G_DBUS_INVOKE_METHOD_FLAGS_NONE, - -1, - NULL, - (GAsyncReadyCallback) get_name_owner_cb, - client_ref (client)); + g_dbus_connection_call (client->connection, + "org.freedesktop.DBus", /* bus name */ + "/org/freedesktop/DBus", /* object path */ + "org.freedesktop.DBus", /* interface name */ + "GetNameOwner", /* method name */ + g_variant_new ("(s)", client->name), + G_VARIANT_TYPE ("(s)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + (GAsyncReadyCallback) get_name_owner_cb, + client_ref (client)); } /* ---------------------------------------------------------------------------------------------------- */ @@ -385,9 +390,9 @@ start_service_by_name_cb (GObject *source_object, result = NULL; - result = g_dbus_connection_invoke_method_finish (client->connection, - res, - NULL); + result = g_dbus_connection_call_finish (client->connection, + res, + NULL); if (result != NULL) { guint32 start_service_result; @@ -415,10 +420,11 @@ start_service_by_name_cb (GObject *source_object, * org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2 * was not provided by any .service files * - * so just report vanished. + * This doesn't mean that the name doesn't have an owner, just + * that it's not provided by a .service file. So proceed to + * invoke GetNameOwner(). */ - call_vanished_handler (client, FALSE); - client->initialized = TRUE; + invoke_get_name_owner (client); } if (result != NULL) @@ -444,23 +450,25 @@ has_connection (Client *client) "NameOwnerChanged", /* signal */ "/org/freedesktop/DBus", /* path */ client->name, + G_DBUS_SIGNAL_FLAGS_NONE, on_name_owner_changed, client, NULL); if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START) { - g_dbus_connection_invoke_method (client->connection, - "org.freedesktop.DBus", /* bus name */ - "/org/freedesktop/DBus", /* object path */ - "org.freedesktop.DBus", /* interface name */ - "StartServiceByName", /* method name */ - g_variant_new ("(su)", client->name, 0), - G_DBUS_INVOKE_METHOD_FLAGS_NONE, - -1, - NULL, - (GAsyncReadyCallback) start_service_by_name_cb, - client_ref (client)); + g_dbus_connection_call (client->connection, + "org.freedesktop.DBus", /* bus name */ + "/org/freedesktop/DBus", /* object path */ + "org.freedesktop.DBus", /* interface name */ + "StartServiceByName", /* method name */ + g_variant_new ("(su)", client->name, 0), + G_VARIANT_TYPE ("(u)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + (GAsyncReadyCallback) start_service_by_name_cb, + client_ref (client)); } else { @@ -497,17 +505,17 @@ connection_get_cb (GObject *source_object, * @bus_type: The type of bus to watch a name on. * @name: The name (well-known or unique) to watch. * @flags: Flags from the #GBusNameWatcherFlags enumeration. - * @name_appeared_handler: Handler to invoke when @name is known to exist or %NULL. - * @name_vanished_handler: Handler to invoke when @name is known to not exist or %NULL. + * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL. + * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL. * @user_data: User data to pass to handlers. - * @user_data_free_func: Function for freeing @user_data or %NULL. + * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL. * * Starts watching @name on the bus specified by @bus_type and calls * @name_appeared_handler and @name_vanished_handler when the name is * known to have a owner respectively known to lose its - * owner. Callbacks will be invoked in the thread-default main - * loop of the thread you are calling this function from. + * owner. Callbacks will be invoked in the + * [thread-default main context][g-main-context-push-thread-default] + * of the thread you are calling this function from. * * You are guaranteed that one of the handlers will be invoked after * calling this function. When you are done watching the name, just @@ -526,11 +534,11 @@ connection_get_cb (GObject *source_object, * guaranteed that the next time one of the handlers is invoked, it * will be @name_vanished_handler. The reverse is also true. * - * This behavior makes it very simple to write applications that wants - * to take action when a certain name exists, see . Basically, the application - * should create object proxies in @name_appeared_handler and destroy - * them again (if any) in @name_vanished_handler. + * This behavior makes it very simple to write applications that want + * to take action when a certain [name exists][gdbus-watching-names]. + * Basically, the application should create object proxies in + * @name_appeared_handler and destroy them again (if any) in + * @name_vanished_handler. * * Returns: An identifier (never 0) that an be used with * g_bus_unwatch_name() to stop watching the name. @@ -548,7 +556,6 @@ g_bus_watch_name (GBusType bus_type, { Client *client; - g_return_val_if_fail (bus_type != G_BUS_TYPE_NONE, 0); g_return_val_if_fail (g_dbus_is_name (name), 0); G_LOCK (lock); @@ -562,9 +569,7 @@ g_bus_watch_name (GBusType bus_type, client->name_vanished_handler = name_vanished_handler; client->user_data = user_data; client->user_data_free_func = user_data_free_func; - client->main_context = g_main_context_get_thread_default (); - if (client->main_context != NULL) - g_main_context_ref (client->main_context); + client->main_context = g_main_context_ref_thread_default (); if (map_id_to_client == NULL) { @@ -585,6 +590,228 @@ g_bus_watch_name (GBusType bus_type, } /** + * g_bus_watch_name_on_connection: + * @connection: A #GDBusConnection. + * @name: The name (well-known or unique) to watch. + * @flags: Flags from the #GBusNameWatcherFlags enumeration. + * @name_appeared_handler: (allow-none): Handler to invoke when @name is known to exist or %NULL. + * @name_vanished_handler: (allow-none): Handler to invoke when @name is known to not exist or %NULL. + * @user_data: User data to pass to handlers. + * @user_data_free_func: (allow-none): Function for freeing @user_data or %NULL. + * + * Like g_bus_watch_name() but takes a #GDBusConnection instead of a + * #GBusType. + * + * Returns: An identifier (never 0) that an be used with + * g_bus_unwatch_name() to stop watching the name. + * + * Since: 2.26 + */ +guint g_bus_watch_name_on_connection (GDBusConnection *connection, + const gchar *name, + GBusNameWatcherFlags flags, + GBusNameAppearedCallback name_appeared_handler, + GBusNameVanishedCallback name_vanished_handler, + gpointer user_data, + GDestroyNotify user_data_free_func) +{ + Client *client; + + g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0); + g_return_val_if_fail (g_dbus_is_name (name), 0); + + G_LOCK (lock); + + client = g_new0 (Client, 1); + client->ref_count = 1; + client->id = next_global_id++; /* TODO: uh oh, handle overflow */ + client->name = g_strdup (name); + client->flags = flags; + client->name_appeared_handler = name_appeared_handler; + client->name_vanished_handler = name_vanished_handler; + client->user_data = user_data; + client->user_data_free_func = user_data_free_func; + client->main_context = g_main_context_ref_thread_default (); + + if (map_id_to_client == NULL) + map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal); + + g_hash_table_insert (map_id_to_client, + GUINT_TO_POINTER (client->id), + client); + + client->connection = g_object_ref (connection); + G_UNLOCK (lock); + + has_connection (client); + + return client->id; +} + +typedef struct { + GClosure *name_appeared_closure; + GClosure *name_vanished_closure; +} WatchNameData; + +static WatchNameData * +watch_name_data_new (GClosure *name_appeared_closure, + GClosure *name_vanished_closure) +{ + WatchNameData *data; + + data = g_new0 (WatchNameData, 1); + + if (name_appeared_closure != NULL) + { + data->name_appeared_closure = g_closure_ref (name_appeared_closure); + g_closure_sink (name_appeared_closure); + if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure)) + g_closure_set_marshal (name_appeared_closure, g_cclosure_marshal_generic); + } + + if (name_vanished_closure != NULL) + { + data->name_vanished_closure = g_closure_ref (name_vanished_closure); + g_closure_sink (name_vanished_closure); + if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure)) + g_closure_set_marshal (name_vanished_closure, g_cclosure_marshal_generic); + } + + return data; +} + +static void +watch_with_closures_on_name_appeared (GDBusConnection *connection, + const gchar *name, + const gchar *name_owner, + gpointer user_data) +{ + WatchNameData *data = user_data; + GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT }; + + g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION); + g_value_set_object (¶ms[0], connection); + + g_value_init (¶ms[1], G_TYPE_STRING); + g_value_set_string (¶ms[1], name); + + g_value_init (¶ms[2], G_TYPE_STRING); + g_value_set_string (¶ms[2], name_owner); + + g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL); + + g_value_unset (params + 0); + g_value_unset (params + 1); + g_value_unset (params + 2); +} + +static void +watch_with_closures_on_name_vanished (GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + WatchNameData *data = user_data; + GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT }; + + g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION); + g_value_set_object (¶ms[0], connection); + + g_value_init (¶ms[1], G_TYPE_STRING); + g_value_set_string (¶ms[1], name); + + g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL); + + g_value_unset (params + 0); + g_value_unset (params + 1); +} + +static void +bus_watch_name_free_func (gpointer user_data) +{ + WatchNameData *data = user_data; + + if (data->name_appeared_closure != NULL) + g_closure_unref (data->name_appeared_closure); + + if (data->name_vanished_closure != NULL) + g_closure_unref (data->name_vanished_closure); + + g_free (data); +} + +/** + * g_bus_watch_name_with_closures: + * @bus_type: The type of bus to watch a name on. + * @name: The name (well-known or unique) to watch. + * @flags: Flags from the #GBusNameWatcherFlags enumeration. + * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known + * to exist or %NULL. + * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known + * to not exist or %NULL. + * + * Version of g_bus_watch_name() using closures instead of callbacks for + * easier binding in other languages. + * + * Returns: An identifier (never 0) that an be used with + * g_bus_unwatch_name() to stop watching the name. + * + * Rename to: g_bus_watch_name + * + * Since: 2.26 + */ +guint +g_bus_watch_name_with_closures (GBusType bus_type, + const gchar *name, + GBusNameWatcherFlags flags, + GClosure *name_appeared_closure, + GClosure *name_vanished_closure) +{ + return g_bus_watch_name (bus_type, + name, + flags, + name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL, + name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL, + watch_name_data_new (name_appeared_closure, name_vanished_closure), + bus_watch_name_free_func); +} + +/** + * g_bus_watch_name_on_connection_with_closures: + * @connection: A #GDBusConnection. + * @name: The name (well-known or unique) to watch. + * @flags: Flags from the #GBusNameWatcherFlags enumeration. + * @name_appeared_closure: (allow-none): #GClosure to invoke when @name is known + * to exist or %NULL. + * @name_vanished_closure: (allow-none): #GClosure to invoke when @name is known + * to not exist or %NULL. + * + * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for + * easier binding in other languages. + * + * Returns: An identifier (never 0) that an be used with + * g_bus_unwatch_name() to stop watching the name. + * + * Rename to: g_bus_watch_name_on_connection + * + * Since: 2.26 + */ +guint g_bus_watch_name_on_connection_with_closures ( + GDBusConnection *connection, + const gchar *name, + GBusNameWatcherFlags flags, + GClosure *name_appeared_closure, + GClosure *name_vanished_closure) +{ + return g_bus_watch_name_on_connection (connection, + name, + flags, + name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL, + name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL, + watch_name_data_new (name_appeared_closure, name_vanished_closure), + bus_watch_name_free_func); +} + +/** * g_bus_unwatch_name: * @watcher_id: An identifier obtained from g_bus_watch_name() *