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 "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.
48 struct _GDBusMethodInvocationPrivate
50 /* construct-only properties */
53 gchar *interface_name;
55 const GDBusMethodInfo *method_info;
56 GDBusConnection *connection;
57 GDBusMessage *message;
76 G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT);
79 g_dbus_method_invocation_finalize (GObject *object)
81 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
83 g_free (invocation->priv->sender);
84 g_free (invocation->priv->object_path);
85 g_free (invocation->priv->interface_name);
86 g_free (invocation->priv->method_name);
87 g_object_unref (invocation->priv->connection);
88 g_object_unref (invocation->priv->message);
89 g_variant_unref (invocation->priv->parameters);
91 if (G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize != NULL)
92 G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
96 g_dbus_method_invocation_get_property (GObject *object,
101 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
106 g_value_set_string (value, g_dbus_method_invocation_get_sender (invocation));
109 case PROP_OBJECT_PATH:
110 g_value_set_string (value, g_dbus_method_invocation_get_object_path (invocation));
113 case PROP_INTERFACE_NAME:
114 g_value_set_string (value, g_dbus_method_invocation_get_interface_name (invocation));
117 case PROP_METHOD_NAME:
118 g_value_set_string (value, g_dbus_method_invocation_get_method_name (invocation));
121 case PROP_METHOD_INFO:
122 g_value_set_boxed (value, g_dbus_method_invocation_get_method_info (invocation));
125 case PROP_CONNECTION:
126 g_value_set_object (value, g_dbus_method_invocation_get_connection (invocation));
129 case PROP_PARAMETERS:
130 g_value_set_boxed (value, g_dbus_method_invocation_get_parameters (invocation));
134 g_value_set_object (value, g_dbus_method_invocation_get_message (invocation));
138 g_value_set_pointer (value, g_dbus_method_invocation_get_user_data (invocation));
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148 g_dbus_method_invocation_set_property (GObject *object,
153 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
158 invocation->priv->sender = g_value_dup_string (value);
161 case PROP_OBJECT_PATH:
162 invocation->priv->object_path = g_value_dup_string (value);
165 case PROP_INTERFACE_NAME:
166 invocation->priv->interface_name = g_value_dup_string (value);
169 case PROP_METHOD_NAME:
170 invocation->priv->method_name = g_value_dup_string (value);
173 case PROP_METHOD_INFO:
174 invocation->priv->method_info = g_value_dup_boxed (value);
177 case PROP_CONNECTION:
178 invocation->priv->connection = g_value_dup_object (value);
181 case PROP_PARAMETERS:
182 invocation->priv->parameters = g_value_dup_boxed (value);
186 invocation->priv->message = g_value_dup_object (value);
190 invocation->priv->user_data = g_value_get_pointer (value);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
203 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
205 gobject_class->finalize = g_dbus_method_invocation_finalize;
206 gobject_class->set_property = g_dbus_method_invocation_set_property;
207 gobject_class->get_property = g_dbus_method_invocation_get_property;
210 * GDBusMethodInvocation:sender:
212 * The bus name that invoked the method or %NULL if the connection is not a bus connection.
216 g_object_class_install_property (gobject_class,
218 g_param_spec_string ("sender",
220 _("The bus name that invoked the method."),
224 G_PARAM_CONSTRUCT_ONLY |
225 G_PARAM_STATIC_NAME |
226 G_PARAM_STATIC_BLURB |
227 G_PARAM_STATIC_NICK));
230 * GDBusMethodInvocation:object-path:
232 * The object path the method was invoked on.
236 g_object_class_install_property (gobject_class,
238 g_param_spec_string ("object-path",
240 _("The object path the method was invoked on."),
244 G_PARAM_CONSTRUCT_ONLY |
245 G_PARAM_STATIC_NAME |
246 G_PARAM_STATIC_BLURB |
247 G_PARAM_STATIC_NICK));
250 * GDBusMethodInvocation:interface-name:
252 * The name of the D-Bus interface the method was invoked on.
256 g_object_class_install_property (gobject_class,
258 g_param_spec_string ("interface-name",
260 _("The name of the D-Bus interface the method was invoked on."),
264 G_PARAM_CONSTRUCT_ONLY |
265 G_PARAM_STATIC_NAME |
266 G_PARAM_STATIC_BLURB |
267 G_PARAM_STATIC_NICK));
270 * GDBusMethodInvocation:method-name:
272 * The name of the method that was invoked.
276 g_object_class_install_property (gobject_class,
278 g_param_spec_string ("method-name",
280 _("The name of the method that was invoked."),
284 G_PARAM_CONSTRUCT_ONLY |
285 G_PARAM_STATIC_NAME |
286 G_PARAM_STATIC_BLURB |
287 G_PARAM_STATIC_NICK));
290 * GDBusMethodInvocation:method-info:
292 * Information about the method that was invoked, if any.
296 g_object_class_install_property (gobject_class,
298 g_param_spec_boxed ("method-info",
300 _("Information about the method that was invoked, if any."),
301 G_TYPE_DBUS_METHOD_INFO,
304 G_PARAM_CONSTRUCT_ONLY |
305 G_PARAM_STATIC_NAME |
306 G_PARAM_STATIC_BLURB |
307 G_PARAM_STATIC_NICK));
310 * GDBusMethodInvocation:connection:
312 * The #GDBusConnection the method was invoked on.
316 g_object_class_install_property (gobject_class,
318 g_param_spec_object ("connection",
320 _("The #GDBusConnection the method was invoked on."),
321 G_TYPE_DBUS_CONNECTION,
324 G_PARAM_CONSTRUCT_ONLY |
325 G_PARAM_STATIC_NAME |
326 G_PARAM_STATIC_BLURB |
327 G_PARAM_STATIC_NICK));
330 * GDBusMethodInvocation:message:
336 g_object_class_install_property (gobject_class,
338 g_param_spec_object ("message",
340 _("The D-Bus Message."),
344 G_PARAM_CONSTRUCT_ONLY |
345 G_PARAM_STATIC_NAME |
346 G_PARAM_STATIC_BLURB |
347 G_PARAM_STATIC_NICK));
350 * GDBusMethodInvocation:parameters:
352 * The parameters as a #GVariant tuple.
356 g_object_class_install_property (gobject_class,
358 g_param_spec_boxed ("parameters",
360 _("The parameters as a #GVariant tuple."),
364 G_PARAM_CONSTRUCT_ONLY |
365 G_PARAM_STATIC_NAME |
366 G_PARAM_STATIC_BLURB |
367 G_PARAM_STATIC_NICK));
370 * GDBusMethodInvocation:user-data:
372 * The @user_data #gpointer passed to g_dbus_connection_register_object().
376 g_object_class_install_property (gobject_class,
378 g_param_spec_pointer ("user-data",
380 _("The gpointer passed to g_dbus_connection_register_object()."),
383 G_PARAM_CONSTRUCT_ONLY |
384 G_PARAM_STATIC_NAME |
385 G_PARAM_STATIC_BLURB |
386 G_PARAM_STATIC_NICK));
388 g_type_class_add_private (klass, sizeof (GDBusMethodInvocationPrivate));
392 g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
394 invocation->priv = G_TYPE_INSTANCE_GET_PRIVATE (invocation,
395 G_TYPE_DBUS_METHOD_INVOCATION,
396 GDBusMethodInvocationPrivate);
400 * g_dbus_method_invocation_get_sender:
401 * @invocation: A #GDBusMethodInvocation.
403 * Gets the bus name that invoked the method.
405 * Returns: A string. Do not free, it is owned by @invocation.
410 g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
412 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
413 return invocation->priv->sender;
417 * g_dbus_method_invocation_get_object_path:
418 * @invocation: A #GDBusMethodInvocation.
420 * Gets the object path the method was invoked on.
422 * Returns: A string. Do not free, it is owned by @invocation.
427 g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
429 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
430 return invocation->priv->object_path;
434 * g_dbus_method_invocation_get_interface_name:
435 * @invocation: A #GDBusMethodInvocation.
437 * Gets the name of the D-Bus interface the method was invoked on.
439 * Returns: A string. Do not free, it is owned by @invocation.
444 g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
446 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
447 return invocation->priv->interface_name;
451 * g_dbus_method_invocation_get_method_info:
452 * @invocation: A #GDBusMethodInvocation.
454 * Gets information about the method call, if any.
456 * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
460 const GDBusMethodInfo *
461 g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
463 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
464 return invocation->priv->method_info;
468 * g_dbus_method_invocation_get_method_name:
469 * @invocation: A #GDBusMethodInvocation.
471 * Gets the name of the method that was invoked.
473 * Returns: A string. Do not free, it is owned by @invocation.
478 g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
480 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
481 return invocation->priv->method_name;
485 * g_dbus_method_invocation_get_connection:
486 * @invocation: A #GDBusMethodInvocation.
488 * Gets the #GDBusConnection the method was invoked on.
490 * Returns: A #GDBusConnection. Do not free, it is owned by @invocation.
495 g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
497 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
498 return invocation->priv->connection;
502 * g_dbus_method_invocation_get_message:
503 * @invocation: A #GDBusMethodInvocation.
505 * Gets the #GDBusMessage for the method invocation. This is useful if
506 * you need to use low-level protocol features, such as UNIX file
507 * descriptor passing, that cannot be properly expressed in the
510 * See <xref linkend="gdbus-server"/> and <xref
511 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
512 * low-level API to send and receive UNIX file descriptors.
514 * Returns: A #GDBusMessage. Do not free, it is owned by @invocation.
519 g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
521 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
522 return invocation->priv->message;
526 * g_dbus_method_invocation_get_parameters:
527 * @invocation: A #GDBusMethodInvocation.
529 * Gets the parameters of the method invocation.
531 * Returns: A #GVariant. Do not free, it is owned by @invocation.
536 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
538 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
539 return invocation->priv->parameters;
543 * g_dbus_method_invocation_get_user_data:
544 * @invocation: A #GDBusMethodInvocation.
546 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
548 * Returns: A #gpointer.
553 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
555 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
556 return invocation->priv->user_data;
560 * g_dbus_method_invocation_new:
561 * @sender: The bus name that invoked the method or %NULL if @connection is not a bus connection.
562 * @object_path: The object path the method was invoked on.
563 * @interface_name: The name of the D-Bus interface the method was invoked on.
564 * @method_name: The name of the method that was invoked.
565 * @method_info: Information about the method call or %NULL.
566 * @connection: The #GDBusConnection the method was invoked on.
567 * @message: The D-Bus message as a #GDBusMessage.
568 * @parameters: The parameters as a #GVariant tuple.
569 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
571 * Creates a new #GDBusMethodInvocation object.
573 * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
577 GDBusMethodInvocation *
578 g_dbus_method_invocation_new (const gchar *sender,
579 const gchar *object_path,
580 const gchar *interface_name,
581 const gchar *method_name,
582 const GDBusMethodInfo *method_info,
583 GDBusConnection *connection,
584 GDBusMessage *message,
585 GVariant *parameters,
588 g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
589 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
590 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
591 g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
592 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
593 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
594 g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
596 return G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION,
598 "object-path", object_path,
599 "interface-name", interface_name,
600 "method-name", method_name,
601 "method-info", method_info,
602 "connection", connection,
604 "parameters", parameters,
605 "user-data", user_data,
609 /* ---------------------------------------------------------------------------------------------------- */
612 * g_dbus_method_invocation_return_value:
613 * @invocation: A #GDBusMethodInvocation.
614 * @parameters: A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
616 * Finishes handling a D-Bus method call by returning @parameters.
618 * It is an error if @parameters is not of the right format.
620 * This method will free @invocation, you cannot use it afterwards.
625 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
626 GVariant *parameters)
631 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
632 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
634 if (parameters != NULL)
635 g_variant_ref_sink (parameters);
637 /* if we have introspection data, check that the signature of @parameters is correct */
638 if (invocation->priv->method_info != NULL)
641 const gchar *type_string;
644 if (parameters != NULL)
645 type_string = g_variant_get_type_string (parameters);
646 signature = _g_dbus_compute_complete_signature (invocation->priv->method_info->out_args, TRUE);
648 if (g_strcmp0 (type_string, signature) != 0)
650 g_warning (_("Type of return value is incorrect, got `%s', expected `%s'"),
659 reply = g_dbus_message_new_method_reply (invocation->priv->message);
660 g_dbus_message_set_body (reply, parameters);
662 if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, &error))
664 g_warning (_("Error sending message: %s"), error->message);
665 g_error_free (error);
667 g_object_unref (reply);
670 g_object_unref (invocation);
671 if (parameters != NULL)
672 g_variant_unref (parameters);
675 /* ---------------------------------------------------------------------------------------------------- */
678 * g_dbus_method_invocation_return_error:
679 * @invocation: A #GDBusMethodInvocation.
680 * @domain: A #GQuark for the #GError error domain.
681 * @code: The error code.
682 * @format: printf()-style format.
683 * @...: Parameters for @format.
685 * Finishes handling a D-Bus method call by returning an error.
687 * See g_dbus_error_encode_gerror() for details about what error name
688 * will be returned on the wire. In a nutshell, if the given error is
689 * registered using g_dbus_error_register_error() the name given
690 * during registration is used. Otherwise, a name of the form
691 * <literal>org.gtk.GDBus.UnmappedGError.Quark...</literal> is
692 * used. This provides transparent mapping of #GError between
693 * applications using GDBus.
695 * If you are writing an application intended to be portable,
696 * <emphasis>always</emphasis> register errors with g_dbus_error_register_error()
697 * or use g_dbus_method_invocation_return_dbus_error().
699 * This method will free @invocation, you cannot use it afterwards.
704 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
712 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
713 g_return_if_fail (format != NULL);
715 va_start (var_args, format);
716 g_dbus_method_invocation_return_error_valist (invocation,
725 * g_dbus_method_invocation_return_error_valist:
726 * @invocation: A #GDBusMethodInvocation.
727 * @domain: A #GQuark for the #GError error domain.
728 * @code: The error code.
729 * @format: printf()-style format.
730 * @var_args: #va_list of parameters for @format.
732 * Like g_dbus_method_invocation_return_error() but intended for
735 * This method will free @invocation, you cannot use it afterwards.
740 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
746 gchar *literal_message;
748 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
749 g_return_if_fail (format != NULL);
751 literal_message = g_strdup_vprintf (format, var_args);
752 g_dbus_method_invocation_return_error_literal (invocation,
756 g_free (literal_message);
760 * g_dbus_method_invocation_return_error_literal:
761 * @invocation: A #GDBusMethodInvocation.
762 * @domain: A #GQuark for the #GError error domain.
763 * @code: The error code.
764 * @message: The error message.
766 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
768 * This method will free @invocation, you cannot use it afterwards.
773 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
776 const gchar *message)
780 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
781 g_return_if_fail (message != NULL);
783 error = g_error_new_literal (domain, code, message);
784 g_dbus_method_invocation_return_gerror (invocation, error);
785 g_error_free (error);
789 * g_dbus_method_invocation_return_gerror:
790 * @invocation: A #GDBusMethodInvocation.
793 * Like g_dbus_method_invocation_return_error() but takes a #GError
794 * instead of the error domain, error code and message.
796 * This method will free @invocation, you cannot use it afterwards.
801 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
804 gchar *dbus_error_name;
806 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
807 g_return_if_fail (error != NULL);
809 dbus_error_name = g_dbus_error_encode_gerror (error);
811 g_dbus_method_invocation_return_dbus_error (invocation,
814 g_free (dbus_error_name);
818 * g_dbus_method_invocation_return_dbus_error:
819 * @invocation: A #GDBusMethodInvocation.
820 * @error_name: A valid D-Bus error name.
821 * @error_message: A valid D-Bus error message.
823 * Finishes handling a D-Bus method call by returning an error.
825 * This method will free @invocation, you cannot use it afterwards.
830 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
831 const gchar *error_name,
832 const gchar *error_message)
836 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
837 g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
838 g_return_if_fail (error_message != NULL);
840 reply = g_dbus_message_new_method_error_literal (invocation->priv->message,
843 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, NULL);
844 g_object_unref (reply);
846 g_object_unref (invocation);
849 #define __G_DBUS_METHOD_INVOCATION_C__
850 #include "gioaliasdef.c"