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>
27 #include "gdbusutils.h"
28 #include "gdbusconnection.h"
29 #include "gdbusmessage.h"
30 #include "gdbusmethodinvocation.h"
31 #include "gdbusintrospection.h"
32 #include "gdbuserror.h"
33 #include "gdbusprivate.h"
39 * SECTION:gdbusmethodinvocation
40 * @short_description: Object for handling remote calls
43 * Instances of the #GDBusMethodInvocation class are used when
44 * handling D-Bus method calls. It provides a way to asynchronously
45 * return results and errors.
47 * The normal way to obtain a #GDBusMethodInvocation object is to receive
48 * it as an argument to the handle_method_call() function in a
49 * #GDBusInterfaceVTable that was passed to g_dbus_connection_register_object().
52 struct _GDBusMethodInvocationPrivate
54 /* construct-only properties */
57 gchar *interface_name;
59 const GDBusMethodInfo *method_info;
60 GDBusConnection *connection;
61 GDBusMessage *message;
80 G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT);
83 g_dbus_method_invocation_finalize (GObject *object)
85 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
87 g_free (invocation->priv->sender);
88 g_free (invocation->priv->object_path);
89 g_free (invocation->priv->interface_name);
90 g_free (invocation->priv->method_name);
91 g_object_unref (invocation->priv->connection);
92 g_object_unref (invocation->priv->message);
93 g_variant_unref (invocation->priv->parameters);
95 if (G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize != NULL)
96 G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
100 g_dbus_method_invocation_get_property (GObject *object,
105 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
110 g_value_set_string (value, g_dbus_method_invocation_get_sender (invocation));
113 case PROP_OBJECT_PATH:
114 g_value_set_string (value, g_dbus_method_invocation_get_object_path (invocation));
117 case PROP_INTERFACE_NAME:
118 g_value_set_string (value, g_dbus_method_invocation_get_interface_name (invocation));
121 case PROP_METHOD_NAME:
122 g_value_set_string (value, g_dbus_method_invocation_get_method_name (invocation));
125 case PROP_METHOD_INFO:
126 g_value_set_boxed (value, g_dbus_method_invocation_get_method_info (invocation));
129 case PROP_CONNECTION:
130 g_value_set_object (value, g_dbus_method_invocation_get_connection (invocation));
133 case PROP_PARAMETERS:
134 g_value_set_boxed (value, g_dbus_method_invocation_get_parameters (invocation));
138 g_value_set_object (value, g_dbus_method_invocation_get_message (invocation));
142 g_value_set_pointer (value, g_dbus_method_invocation_get_user_data (invocation));
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152 g_dbus_method_invocation_set_property (GObject *object,
157 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
162 invocation->priv->sender = g_value_dup_string (value);
165 case PROP_OBJECT_PATH:
166 invocation->priv->object_path = g_value_dup_string (value);
169 case PROP_INTERFACE_NAME:
170 invocation->priv->interface_name = g_value_dup_string (value);
173 case PROP_METHOD_NAME:
174 invocation->priv->method_name = g_value_dup_string (value);
177 case PROP_METHOD_INFO:
178 invocation->priv->method_info = g_value_dup_boxed (value);
181 case PROP_CONNECTION:
182 invocation->priv->connection = g_value_dup_object (value);
185 case PROP_PARAMETERS:
186 invocation->priv->parameters = g_value_dup_boxed (value);
190 invocation->priv->message = g_value_dup_object (value);
194 invocation->priv->user_data = g_value_get_pointer (value);
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
207 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
209 gobject_class->finalize = g_dbus_method_invocation_finalize;
210 gobject_class->set_property = g_dbus_method_invocation_set_property;
211 gobject_class->get_property = g_dbus_method_invocation_get_property;
214 * GDBusMethodInvocation:sender:
216 * The bus name that invoked the method or %NULL if the connection is not a bus connection.
220 g_object_class_install_property (gobject_class,
222 g_param_spec_string ("sender",
224 _("The bus name that invoked the method."),
228 G_PARAM_CONSTRUCT_ONLY |
229 G_PARAM_STATIC_NAME |
230 G_PARAM_STATIC_BLURB |
231 G_PARAM_STATIC_NICK));
234 * GDBusMethodInvocation:object-path:
236 * The object path the method was invoked on.
240 g_object_class_install_property (gobject_class,
242 g_param_spec_string ("object-path",
244 _("The object path the method was invoked on."),
248 G_PARAM_CONSTRUCT_ONLY |
249 G_PARAM_STATIC_NAME |
250 G_PARAM_STATIC_BLURB |
251 G_PARAM_STATIC_NICK));
254 * GDBusMethodInvocation:interface-name:
256 * The name of the D-Bus interface the method was invoked on.
260 g_object_class_install_property (gobject_class,
262 g_param_spec_string ("interface-name",
264 _("The name of the D-Bus interface the method was invoked on."),
268 G_PARAM_CONSTRUCT_ONLY |
269 G_PARAM_STATIC_NAME |
270 G_PARAM_STATIC_BLURB |
271 G_PARAM_STATIC_NICK));
274 * GDBusMethodInvocation:method-name:
276 * The name of the method that was invoked.
280 g_object_class_install_property (gobject_class,
282 g_param_spec_string ("method-name",
284 _("The name of the method that was invoked."),
288 G_PARAM_CONSTRUCT_ONLY |
289 G_PARAM_STATIC_NAME |
290 G_PARAM_STATIC_BLURB |
291 G_PARAM_STATIC_NICK));
294 * GDBusMethodInvocation:method-info:
296 * Information about the method that was invoked, if any.
300 g_object_class_install_property (gobject_class,
302 g_param_spec_boxed ("method-info",
304 _("Information about the method that was invoked, if any."),
305 G_TYPE_DBUS_METHOD_INFO,
308 G_PARAM_CONSTRUCT_ONLY |
309 G_PARAM_STATIC_NAME |
310 G_PARAM_STATIC_BLURB |
311 G_PARAM_STATIC_NICK));
314 * GDBusMethodInvocation:connection:
316 * The #GDBusConnection the method was invoked on.
320 g_object_class_install_property (gobject_class,
322 g_param_spec_object ("connection",
324 _("The #GDBusConnection the method was invoked on."),
325 G_TYPE_DBUS_CONNECTION,
328 G_PARAM_CONSTRUCT_ONLY |
329 G_PARAM_STATIC_NAME |
330 G_PARAM_STATIC_BLURB |
331 G_PARAM_STATIC_NICK));
334 * GDBusMethodInvocation:message:
340 g_object_class_install_property (gobject_class,
342 g_param_spec_object ("message",
344 _("The D-Bus Message."),
348 G_PARAM_CONSTRUCT_ONLY |
349 G_PARAM_STATIC_NAME |
350 G_PARAM_STATIC_BLURB |
351 G_PARAM_STATIC_NICK));
354 * GDBusMethodInvocation:parameters:
356 * The parameters as a #GVariant tuple.
360 g_object_class_install_property (gobject_class,
362 g_param_spec_boxed ("parameters",
364 _("The parameters as a #GVariant tuple."),
368 G_PARAM_CONSTRUCT_ONLY |
369 G_PARAM_STATIC_NAME |
370 G_PARAM_STATIC_BLURB |
371 G_PARAM_STATIC_NICK));
374 * GDBusMethodInvocation:user-data:
376 * The @user_data #gpointer passed to g_dbus_connection_register_object().
380 g_object_class_install_property (gobject_class,
382 g_param_spec_pointer ("user-data",
384 _("The gpointer passed to g_dbus_connection_register_object()."),
387 G_PARAM_CONSTRUCT_ONLY |
388 G_PARAM_STATIC_NAME |
389 G_PARAM_STATIC_BLURB |
390 G_PARAM_STATIC_NICK));
392 g_type_class_add_private (klass, sizeof (GDBusMethodInvocationPrivate));
396 g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
398 invocation->priv = G_TYPE_INSTANCE_GET_PRIVATE (invocation,
399 G_TYPE_DBUS_METHOD_INVOCATION,
400 GDBusMethodInvocationPrivate);
404 * g_dbus_method_invocation_get_sender:
405 * @invocation: A #GDBusMethodInvocation.
407 * Gets the bus name that invoked the method.
409 * Returns: A string. Do not free, it is owned by @invocation.
414 g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
416 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
417 return invocation->priv->sender;
421 * g_dbus_method_invocation_get_object_path:
422 * @invocation: A #GDBusMethodInvocation.
424 * Gets the object path the method was invoked on.
426 * Returns: A string. Do not free, it is owned by @invocation.
431 g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
433 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
434 return invocation->priv->object_path;
438 * g_dbus_method_invocation_get_interface_name:
439 * @invocation: A #GDBusMethodInvocation.
441 * Gets the name of the D-Bus interface the method was invoked on.
443 * Returns: A string. Do not free, it is owned by @invocation.
448 g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
450 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
451 return invocation->priv->interface_name;
455 * g_dbus_method_invocation_get_method_info:
456 * @invocation: A #GDBusMethodInvocation.
458 * Gets information about the method call, if any.
460 * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
464 const GDBusMethodInfo *
465 g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
467 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
468 return invocation->priv->method_info;
472 * g_dbus_method_invocation_get_method_name:
473 * @invocation: A #GDBusMethodInvocation.
475 * Gets the name of the method that was invoked.
477 * Returns: A string. Do not free, it is owned by @invocation.
482 g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
484 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
485 return invocation->priv->method_name;
489 * g_dbus_method_invocation_get_connection:
490 * @invocation: A #GDBusMethodInvocation.
492 * Gets the #GDBusConnection the method was invoked on.
494 * Returns: A #GDBusConnection. Do not free, it is owned by @invocation.
499 g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
501 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
502 return invocation->priv->connection;
506 * g_dbus_method_invocation_get_message:
507 * @invocation: A #GDBusMethodInvocation.
509 * Gets the #GDBusMessage for the method invocation. This is useful if
510 * you need to use low-level protocol features, such as UNIX file
511 * descriptor passing, that cannot be properly expressed in the
514 * See <xref linkend="gdbus-server"/> and <xref
515 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
516 * low-level API to send and receive UNIX file descriptors.
518 * Returns: A #GDBusMessage. Do not free, it is owned by @invocation.
523 g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
525 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
526 return invocation->priv->message;
530 * g_dbus_method_invocation_get_parameters:
531 * @invocation: A #GDBusMethodInvocation.
533 * Gets the parameters of the method invocation.
535 * Returns: A #GVariant. Do not free, it is owned by @invocation.
540 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
542 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
543 return invocation->priv->parameters;
547 * g_dbus_method_invocation_get_user_data:
548 * @invocation: A #GDBusMethodInvocation.
550 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
552 * Returns: A #gpointer.
557 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
559 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
560 return invocation->priv->user_data;
564 * g_dbus_method_invocation_new:
565 * @sender: The bus name that invoked the method or %NULL if @connection is not a bus connection.
566 * @object_path: The object path the method was invoked on.
567 * @interface_name: The name of the D-Bus interface the method was invoked on.
568 * @method_name: The name of the method that was invoked.
569 * @method_info: Information about the method call or %NULL.
570 * @connection: The #GDBusConnection the method was invoked on.
571 * @message: The D-Bus message as a #GDBusMessage.
572 * @parameters: The parameters as a #GVariant tuple.
573 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
575 * Creates a new #GDBusMethodInvocation object.
577 * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
581 GDBusMethodInvocation *
582 g_dbus_method_invocation_new (const gchar *sender,
583 const gchar *object_path,
584 const gchar *interface_name,
585 const gchar *method_name,
586 const GDBusMethodInfo *method_info,
587 GDBusConnection *connection,
588 GDBusMessage *message,
589 GVariant *parameters,
592 g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
593 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
594 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
595 g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
596 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
597 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
598 g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
600 return G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION,
602 "object-path", object_path,
603 "interface-name", interface_name,
604 "method-name", method_name,
605 "method-info", method_info,
606 "connection", connection,
608 "parameters", parameters,
609 "user-data", user_data,
613 /* ---------------------------------------------------------------------------------------------------- */
616 * g_dbus_method_invocation_return_value:
617 * @invocation: A #GDBusMethodInvocation.
618 * @parameters: A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
620 * Finishes handling a D-Bus method call by returning @parameters.
622 * It is an error if @parameters is not of the right format.
624 * This method will free @invocation, you cannot use it afterwards.
629 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
630 GVariant *parameters)
635 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
636 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
638 if (parameters != NULL)
639 g_variant_ref_sink (parameters);
641 /* if we have introspection data, check that the signature of @parameters is correct */
642 if (invocation->priv->method_info != NULL)
645 const gchar *type_string;
648 if (parameters != NULL)
649 type_string = g_variant_get_type_string (parameters);
650 signature = _g_dbus_compute_complete_signature (invocation->priv->method_info->out_args, TRUE);
652 if (g_strcmp0 (type_string, signature) != 0)
654 g_warning (_("Type of return value is incorrect, got `%s', expected `%s'"),
663 reply = g_dbus_message_new_method_reply (invocation->priv->message);
664 g_dbus_message_set_body (reply, parameters);
666 if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, &error))
668 g_warning (_("Error sending message: %s"), error->message);
669 g_error_free (error);
671 g_object_unref (reply);
674 g_object_unref (invocation);
675 if (parameters != NULL)
676 g_variant_unref (parameters);
679 /* ---------------------------------------------------------------------------------------------------- */
682 * g_dbus_method_invocation_return_error:
683 * @invocation: A #GDBusMethodInvocation.
684 * @domain: A #GQuark for the #GError error domain.
685 * @code: The error code.
686 * @format: printf()-style format.
687 * @...: Parameters for @format.
689 * Finishes handling a D-Bus method call by returning an error.
691 * See g_dbus_error_encode_gerror() for details about what error name
692 * will be returned on the wire. In a nutshell, if the given error is
693 * registered using g_dbus_error_register_error() the name given
694 * during registration is used. Otherwise, a name of the form
695 * <literal>org.gtk.GDBus.UnmappedGError.Quark...</literal> is
696 * used. This provides transparent mapping of #GError between
697 * applications using GDBus.
699 * If you are writing an application intended to be portable,
700 * <emphasis>always</emphasis> register errors with g_dbus_error_register_error()
701 * or use g_dbus_method_invocation_return_dbus_error().
703 * This method will free @invocation, you cannot use it afterwards.
708 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
716 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
717 g_return_if_fail (format != NULL);
719 va_start (var_args, format);
720 g_dbus_method_invocation_return_error_valist (invocation,
729 * g_dbus_method_invocation_return_error_valist:
730 * @invocation: A #GDBusMethodInvocation.
731 * @domain: A #GQuark for the #GError error domain.
732 * @code: The error code.
733 * @format: printf()-style format.
734 * @var_args: #va_list of parameters for @format.
736 * Like g_dbus_method_invocation_return_error() but intended for
739 * This method will free @invocation, you cannot use it afterwards.
744 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
750 gchar *literal_message;
752 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
753 g_return_if_fail (format != NULL);
755 literal_message = g_strdup_vprintf (format, var_args);
756 g_dbus_method_invocation_return_error_literal (invocation,
760 g_free (literal_message);
764 * g_dbus_method_invocation_return_error_literal:
765 * @invocation: A #GDBusMethodInvocation.
766 * @domain: A #GQuark for the #GError error domain.
767 * @code: The error code.
768 * @message: The error message.
770 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
772 * This method will free @invocation, you cannot use it afterwards.
777 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
780 const gchar *message)
784 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
785 g_return_if_fail (message != NULL);
787 error = g_error_new_literal (domain, code, message);
788 g_dbus_method_invocation_return_gerror (invocation, error);
789 g_error_free (error);
793 * g_dbus_method_invocation_return_gerror:
794 * @invocation: A #GDBusMethodInvocation.
797 * Like g_dbus_method_invocation_return_error() but takes a #GError
798 * instead of the error domain, error code and message.
800 * This method will free @invocation, you cannot use it afterwards.
805 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
808 gchar *dbus_error_name;
810 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
811 g_return_if_fail (error != NULL);
813 dbus_error_name = g_dbus_error_encode_gerror (error);
815 g_dbus_method_invocation_return_dbus_error (invocation,
818 g_free (dbus_error_name);
822 * g_dbus_method_invocation_return_dbus_error:
823 * @invocation: A #GDBusMethodInvocation.
824 * @error_name: A valid D-Bus error name.
825 * @error_message: A valid D-Bus error message.
827 * Finishes handling a D-Bus method call by returning an error.
829 * This method will free @invocation, you cannot use it afterwards.
834 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
835 const gchar *error_name,
836 const gchar *error_message)
840 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
841 g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
842 g_return_if_fail (error_message != NULL);
844 reply = g_dbus_message_new_method_error_literal (invocation->priv->message,
847 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, NULL);
848 g_object_unref (reply);
850 g_object_unref (invocation);
853 #define __G_DBUS_METHOD_INVOCATION_C__
854 #include "gioaliasdef.c"