X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gio%2Fgapplicationimpl-dbus.c;h=15e66f2990b3e35ed9cb60db661ccc2fa1c41ae4;hb=e608ec7b2e47d29fa189fca6e97f484f41c115a4;hp=ffbb114e1e672fd94b2109456985669dad9886b6;hpb=eefd08996f7657488cb8afed23667030d6bd45f7;p=platform%2Fupstream%2Fglib.git diff --git a/gio/gapplicationimpl-dbus.c b/gio/gapplicationimpl-dbus.c index ffbb114..15e66f2 100644 --- a/gio/gapplicationimpl-dbus.c +++ b/gio/gapplicationimpl-dbus.c @@ -12,25 +12,25 @@ * 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 . * * Authors: Ryan Lortie */ +#include "config.h" + #include "gapplicationimpl.h" #include "gactiongroup.h" #include "gactiongroupexporter.h" #include "gremoteactiongroup.h" -#include "gdbusactiongroup.h" +#include "gdbusactiongroup-private.h" #include "gapplication.h" #include "gfile.h" #include "gdbusconnection.h" #include "gdbusintrospection.h" #include "gdbuserror.h" -#include "gmenuexporter.h" +#include "glib/gstdio.h" #include #include @@ -38,47 +38,69 @@ #include "gapplicationcommandline.h" #include "gdbusmethodinvocation.h" -G_GNUC_INTERNAL gboolean -g_dbus_action_group_sync (GDBusActionGroup *group, - GCancellable *cancellable, - GError **error); - +#ifdef G_OS_UNIX +#include "gunixinputstream.h" +#include "gunixfdlist.h" +#endif /* DBus Interface definition {{{1 */ + +/* For documentation of these interfaces, see + * https://wiki.gnome.org/Projects/GLib/GApplication/DBusAPI + */ static const gchar org_gtk_Application_xml[] = "" - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" ""; static GDBusInterfaceInfo *org_gtk_Application; +static const gchar org_freedesktop_Application_xml[] = + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + ""; + +static GDBusInterfaceInfo *org_freedesktop_Application; + static const gchar org_gtk_private_CommandLine_xml[] = "" - " " - " " - " " - " " - " " - " " - " " - " " + "" + "" + "" + "" + "" + "" + "" + "" ""; static GDBusInterfaceInfo *org_gtk_private_CommandLine; @@ -87,27 +109,64 @@ static GDBusInterfaceInfo *org_gtk_private_CommandLine; struct _GApplicationImpl { GDBusConnection *session_bus; + GActionGroup *exported_actions; const gchar *bus_name; gchar *object_path; guint object_id; + guint fdo_object_id; guint actions_id; - gchar *app_menu_path; - guint app_menu_id; - - gchar *menubar_path; - guint menubar_id; - gboolean properties_live; gboolean primary; - gpointer app; + gboolean busy; + GApplication *app; }; static GApplicationCommandLine * g_dbus_command_line_new (GDBusMethodInvocation *invocation); +static GVariant * +g_application_impl_get_property (GDBusConnection *connection, + const gchar *sender, + const gchar *object_path, + const gchar *interface_name, + const gchar *property_name, + GError **error, + gpointer user_data) +{ + GApplicationImpl *impl = user_data; + + if (strcmp (property_name, "Busy") == 0) + return g_variant_new_boolean (impl->busy); + + g_assert_not_reached (); + + return NULL; +} + +static void +send_property_change (GApplicationImpl *impl) +{ + GVariantBuilder builder; + + g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); + g_variant_builder_add (&builder, + "{sv}", + "Busy", g_variant_new_boolean (impl->busy)); + + g_dbus_connection_emit_signal (impl->session_bus, + NULL, + impl->object_path, + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + g_variant_new ("(sa{sv}as)", + "org.gtk.Application", + &builder, + NULL), + NULL); +} static void g_application_impl_method_call (GDBusConnection *connection, @@ -128,7 +187,10 @@ g_application_impl_method_call (GDBusConnection *connection, { GVariant *platform_data; + /* Completely the same for both freedesktop and gtk interfaces */ + g_variant_get (parameters, "(@a{sv})", &platform_data); + class->before_emit (impl->app, platform_data); g_signal_emit_by_name (impl->app, "activate"); class->after_emit (impl->app, platform_data); @@ -139,14 +201,28 @@ g_application_impl_method_call (GDBusConnection *connection, else if (strcmp (method_name, "Open") == 0) { + GApplicationFlags flags; GVariant *platform_data; const gchar *hint; GVariant *array; GFile **files; gint n, i; - g_variant_get (parameters, "(@ass@a{sv})", - &array, &hint, &platform_data); + flags = g_application_get_flags (impl->app); + if ((flags & G_APPLICATION_HANDLES_OPEN) == 0) + { + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED, "Application does not open files"); + return; + } + + /* freedesktop interface has no hint parameter */ + if (g_str_equal (interface_name, "org.freedesktop.Application")) + { + g_variant_get (parameters, "(@as@a{sv})", &array, &platform_data); + hint = ""; + } + else + g_variant_get (parameters, "(@as&s@a{sv})", &array, &hint, &platform_data); n = g_variant_n_children (array); files = g_new (GFile *, n + 1); @@ -176,10 +252,21 @@ g_application_impl_method_call (GDBusConnection *connection, else if (strcmp (method_name, "CommandLine") == 0) { + GApplicationFlags flags; GApplicationCommandLine *cmdline; GVariant *platform_data; int status; + flags = g_application_get_flags (impl->app); + if ((flags & G_APPLICATION_HANDLES_COMMAND_LINE) == 0) + { + g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED, + "Application does not handle command line arguments"); + return; + } + + /* Only on the GtkApplication interface */ + cmdline = g_dbus_command_line_new (invocation); platform_data = g_variant_get_child_value (parameters, 2); class->before_emit (impl->app, platform_data); @@ -189,46 +276,32 @@ g_application_impl_method_call (GDBusConnection *connection, g_variant_unref (platform_data); g_object_unref (cmdline); } - else - g_assert_not_reached (); -} + else if (g_str_equal (method_name, "ActivateAction")) + { + GVariant *parameter = NULL; + GVariant *platform_data; + GVariantIter *iter; + const gchar *name; -static GVariant * -g_application_impl_get_property (GDBusConnection *connection, - const gchar *sender, - const gchar *object_path, - const gchar *interface_name, - const gchar *property_name, - GError **error, - gpointer user_data) -{ - GApplicationImpl *impl = user_data; - GVariantBuilder builder; + /* Only on the freedesktop interface */ - /* We use this boolean to detect if the properties have ever been - * queried before. If they have not been queried, then there is no - * point emitting change signals since nobody is watching anyway. - */ - impl->properties_live = TRUE; + g_variant_get (parameters, "(&sav@a{sv})", &name, &iter, &platform_data); + g_variant_iter_next (iter, "v", ¶meter); + g_variant_iter_free (iter); - g_variant_builder_init (&builder, G_VARIANT_TYPE_OBJECT_PATH_ARRAY); + class->before_emit (impl->app, platform_data); + g_action_group_activate_action (impl->exported_actions, name, parameter); + class->after_emit (impl->app, platform_data); - if (g_str_equal (property_name, "AppMenu")) - { - if (impl->app_menu_path != NULL) - g_variant_builder_add (&builder, "o", impl->app_menu_path); - } + if (parameter) + g_variant_unref (parameter); - else if (g_str_equal (property_name, "MenuBar")) - { - if (impl->menubar_path != NULL) - g_variant_builder_add (&builder, "o", impl->menubar_path); - } + g_variant_unref (platform_data); + g_dbus_method_invocation_return_value (invocation, NULL); + } else g_assert_not_reached (); - - return g_variant_builder_end (&builder); } static gchar * @@ -236,6 +309,10 @@ application_path_from_appid (const gchar *appid) { gchar *appid_path, *iter; + if (appid == NULL) + /* this is a private implementation detail */ + return g_strdup ("/org/gtk/Application/anonymous"); + appid_path = g_strconcat ("/", appid, NULL); for (iter = appid_path; *iter; iter++) { @@ -249,79 +326,6 @@ application_path_from_appid (const gchar *appid) return appid_path; } -static void -g_application_impl_publish_menu (GApplicationImpl *impl, - const gchar *type, - GMenuModel *model, - guint *id, - gchar **path) -{ - gint i; - - /* unexport any existing menu */ - if (*id) - { - g_dbus_connection_unexport_menu_model (impl->session_bus, *id); - g_free (*path); - *path = NULL; - *id = 0; - } - - /* export the new menu, if there is one */ - if (model != NULL) - { - /* try getting the preferred name */ - *path = g_strconcat (impl->object_path, "/menus/", type, NULL); - *id = g_dbus_connection_export_menu_model (impl->session_bus, *path, model, NULL); - - /* keep trying until we get a working name... */ - for (i = 0; *id == 0; i++) - { - g_free (*path); - *path = g_strdup_printf ("%s/menus/%s%d", impl->object_path, type, i); - *id = g_dbus_connection_export_menu_model (impl->session_bus, *path, model, NULL); - } - } - - /* notify for changes, if needed */ - if (impl->properties_live) - { - GVariantBuilder builder; - - g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT); - g_variant_builder_add (&builder, "{sv}", type, g_variant_new_objv ((const gchar **) path, *path != NULL)); - g_dbus_connection_emit_signal (impl->session_bus, NULL, impl->object_path, - "org.freedesktop.DBus.Properties", "PropertiesChanged", - g_variant_new ("(sa{sv}as)", "org.gtk.Actions", &builder, NULL), NULL); - } -} - -static void -g_application_impl_app_menu_changed (GObject *source, - GParamSpec *pspec, - gpointer user_data) -{ - GApplicationImpl *impl = user_data; - - g_assert (source == impl->app); - - g_application_impl_publish_menu (impl, "AppMenu", g_application_get_app_menu (impl->app), - &impl->app_menu_id, &impl->app_menu_path); -} - -static void -g_application_impl_menubar_changed (GObject *source, - GParamSpec *pspec, - gpointer user_data) -{ - GApplicationImpl *impl = user_data; - - g_assert (source == impl->app); - - g_application_impl_publish_menu (impl, "MenuBar", g_application_get_menubar (impl->app), - &impl->menubar_id, &impl->menubar_path); -} - /* Attempt to become the primary instance. * * Returns %TRUE if everything went OK, regardless of if we became the @@ -338,8 +342,10 @@ g_application_impl_attempt_primary (GApplicationImpl *impl, { const static GDBusInterfaceVTable vtable = { g_application_impl_method_call, - g_application_impl_get_property + g_application_impl_get_property, + NULL /* set_property */ }; + GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app); GVariant *reply; guint32 rval; @@ -355,6 +361,14 @@ g_application_impl_attempt_primary (GApplicationImpl *impl, g_assert (org_gtk_Application != NULL); g_dbus_interface_info_ref (org_gtk_Application); g_dbus_node_info_unref (info); + + info = g_dbus_node_info_new_for_xml (org_freedesktop_Application_xml, &error); + if G_UNLIKELY (info == NULL) + g_error ("%s", error->message); + org_freedesktop_Application = g_dbus_node_info_lookup_interface (info, "org.freedesktop.Application"); + g_assert (org_freedesktop_Application != NULL); + g_dbus_interface_info_ref (org_freedesktop_Application); + g_dbus_node_info_unref (info); } /* We could possibly have been D-Bus activated as a result of incoming @@ -377,11 +391,39 @@ g_application_impl_attempt_primary (GApplicationImpl *impl, if (impl->object_id == 0) return FALSE; - impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path, impl->app, error); + impl->fdo_object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path, + org_freedesktop_Application, &vtable, impl, NULL, error); + + if (impl->fdo_object_id == 0) + return FALSE; + + impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path, + impl->exported_actions, error); if (impl->actions_id == 0) return FALSE; + if (!app_class->dbus_register (impl->app, + impl->session_bus, + impl->object_path, + error)) + return FALSE; + + if (impl->bus_name == NULL) + { + /* If this is a non-unique application then it is sufficient to + * have our object paths registered. We can return now. + * + * Note: non-unique applications always act as primary-instance. + */ + impl->primary = TRUE; + return TRUE; + } + + /* If this is a unique application then we need to attempt to own + * the well-known name and fall back to remote mode (!is_primary) + * in the case that we can't do that. + */ /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */ reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "RequestName", @@ -397,14 +439,6 @@ g_application_impl_attempt_primary (GApplicationImpl *impl, /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */ impl->primary = (rval != 3); - if (impl->primary) - { - g_signal_connect (impl->app, "notify::app-menu", G_CALLBACK (g_application_impl_app_menu_changed), impl); - g_signal_connect (impl->app, "notify::menubar", G_CALLBACK (g_application_impl_menubar_changed), impl); - g_application_impl_app_menu_changed (impl->app, NULL, impl); - g_application_impl_menubar_changed (impl->app, NULL, impl); - } - return TRUE; } @@ -419,44 +453,48 @@ g_application_impl_attempt_primary (GApplicationImpl *impl, static void g_application_impl_stop_primary (GApplicationImpl *impl) { + GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app); + + app_class->dbus_unregister (impl->app, + impl->session_bus, + impl->object_path); + if (impl->object_id) { g_dbus_connection_unregister_object (impl->session_bus, impl->object_id); impl->object_id = 0; } + if (impl->fdo_object_id) + { + g_dbus_connection_unregister_object (impl->session_bus, impl->fdo_object_id); + impl->fdo_object_id = 0; + } + if (impl->actions_id) { g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id); impl->actions_id = 0; } - if (impl->primary) + if (impl->primary && impl->bus_name) { - g_signal_handlers_disconnect_by_func (impl->app, g_application_impl_app_menu_changed, impl); - g_signal_handlers_disconnect_by_func (impl->app, g_application_impl_menubar_changed, impl); - g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "ReleaseName", g_variant_new ("(s)", impl->bus_name), NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); impl->primary = FALSE; } +} - if (impl->app_menu_id) - { - g_dbus_connection_unexport_menu_model (impl->session_bus, impl->app_menu_id); - g_free (impl->app_menu_path); - impl->app_menu_path = NULL; - impl->app_menu_id = 0; - } - - if (impl->menubar_id) +void +g_application_impl_set_busy_state (GApplicationImpl *impl, + gboolean busy) +{ + if (impl->busy != busy) { - g_dbus_connection_unexport_menu_model (impl->session_bus, impl->menubar_id); - g_free (impl->menubar_path); - impl->menubar_path = NULL; - impl->menubar_id = 0; + impl->busy = busy; + send_property_change (impl); } } @@ -477,6 +515,7 @@ GApplicationImpl * g_application_impl_register (GApplication *application, const gchar *appid, GApplicationFlags flags, + GActionGroup *exported_actions, GRemoteActionGroup **remote_actions, GCancellable *cancellable, GError **error) @@ -484,10 +523,16 @@ g_application_impl_register (GApplication *application, GDBusActionGroup *actions; GApplicationImpl *impl; + g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL); + impl = g_slice_new0 (GApplicationImpl); impl->app = application; - impl->bus_name = appid; + impl->exported_actions = exported_actions; + + /* non-unique applications do not attempt to acquire a bus name */ + if (~flags & G_APPLICATION_NON_UNIQUE) + impl->bus_name = appid; impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL); @@ -522,7 +567,7 @@ g_application_impl_register (GApplication *application, if (flags & G_APPLICATION_IS_SERVICE) { g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, - "Unable to acquire bus name `%s'", appid); + "Unable to acquire bus name '%s'", appid); g_application_impl_destroy (impl); return NULL; @@ -631,8 +676,12 @@ g_application_impl_cmdline_done (GObject *source, GError *error = NULL; GVariant *reply; - reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), - result, &error); +#ifdef G_OS_UNIX + reply = g_dbus_connection_call_with_unix_fd_list_finish (G_DBUS_CONNECTION (source), NULL, result, &error); +#else + reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), result, &error); +#endif + if (reply != NULL) { @@ -651,9 +700,9 @@ g_application_impl_cmdline_done (GObject *source, } int -g_application_impl_command_line (GApplicationImpl *impl, - gchar **arguments, - GVariant *platform_data) +g_application_impl_command_line (GApplicationImpl *impl, + const gchar * const *arguments, + GVariant *platform_data) { const static GDBusInterfaceVTable vtable = { g_application_impl_cmdline_method_call @@ -687,15 +736,31 @@ g_application_impl_command_line (GApplicationImpl *impl, /* In theory we should try other paths... */ g_assert (object_id != 0); - g_dbus_connection_call (impl->session_bus, - impl->bus_name, - impl->object_path, - "org.gtk.Application", - "CommandLine", - g_variant_new ("(o^aay@a{sv})", object_path, - arguments, platform_data), +#ifdef G_OS_UNIX + { + GError *error = NULL; + GUnixFDList *fd_list; + + /* send along the stdin in case + * g_application_command_line_get_stdin_data() is called + */ + fd_list = g_unix_fd_list_new (); + g_unix_fd_list_append (fd_list, 0, &error); + g_assert_no_error (error); + + g_dbus_connection_call_with_unix_fd_list (impl->session_bus, impl->bus_name, impl->object_path, + "org.gtk.Application", "CommandLine", + g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data), + G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, fd_list, NULL, + g_application_impl_cmdline_done, &data); + } +#else + g_dbus_connection_call (impl->session_bus, impl->bus_name, impl->object_path, + "org.gtk.Application", "CommandLine", + g_variant_new ("(o^aay@a{sv})", object_path, arguments, platform_data), G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL, g_application_impl_cmdline_done, &data); +#endif g_main_loop_run (data.loop); @@ -713,6 +778,17 @@ g_application_impl_flush (GApplicationImpl *impl) g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL); } +GDBusConnection * +g_application_impl_get_dbus_connection (GApplicationImpl *impl) +{ + return impl->session_bus; +} + +const gchar * +g_application_impl_get_dbus_object_path (GApplicationImpl *impl) +{ + return impl->object_path; +} /* GDBusCommandLine implementation {{{1 */ @@ -761,6 +837,35 @@ g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline, NULL, 0, -1, NULL, NULL, NULL); } +static GInputStream * +g_dbus_command_line_get_stdin (GApplicationCommandLine *cmdline) +{ +#ifdef G_OS_UNIX + GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline; + GInputStream *result = NULL; + GDBusMessage *message; + GUnixFDList *fd_list; + + message = g_dbus_method_invocation_get_message (gdbcl->invocation); + fd_list = g_dbus_message_get_unix_fd_list (message); + + if (fd_list && g_unix_fd_list_get_length (fd_list)) + { + gint *fds, n_fds, i; + + fds = g_unix_fd_list_steal_fds (fd_list, &n_fds); + result = g_unix_input_stream_new (fds[0], TRUE); + for (i = 1; i < n_fds; i++) + (void) g_close (fds[i], NULL); + g_free (fds); + } + + return result; +#else + return NULL; +#endif +} + static void g_dbus_command_line_finalize (GObject *object) { @@ -791,6 +896,7 @@ g_dbus_command_line_class_init (GApplicationCommandLineClass *class) object_class->finalize = g_dbus_command_line_finalize; class->printerr_literal = g_dbus_command_line_printerr_literal; class->print_literal = g_dbus_command_line_print_literal; + class->get_stdin = g_dbus_command_line_get_stdin; } static GApplicationCommandLine * @@ -798,13 +904,19 @@ g_dbus_command_line_new (GDBusMethodInvocation *invocation) { GDBusCommandLine *gdbcl; GVariant *args; + GVariant *arguments, *platform_data; args = g_dbus_method_invocation_get_parameters (invocation); + arguments = g_variant_get_child_value (args, 1); + platform_data = g_variant_get_child_value (args, 2); gdbcl = g_object_new (g_dbus_command_line_get_type (), - "arguments", g_variant_get_child_value (args, 1), - "platform-data", g_variant_get_child_value (args, 2), + "arguments", arguments, + "platform-data", platform_data, NULL); + g_variant_unref (arguments); + g_variant_unref (platform_data); + gdbcl->connection = g_dbus_method_invocation_get_connection (invocation); gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation); g_variant_get_child (args, 0, "&o", &gdbcl->object_path);