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>
26 #include <glib/gi18n.h>
28 #include "gdbusutils.h"
29 #include "gdbusconnection.h"
30 #include "gdbusmessage.h"
31 #include "gdbusmethodinvocation.h"
32 #include "gdbusintrospection.h"
33 #include "gdbuserror.h"
34 #include "gdbusprivate.h"
37 * SECTION:gdbusmethodinvocation
38 * @short_description: Object for handling remote calls
41 * Instances of the #GDBusMethodInvocation class are used when
42 * handling D-Bus method calls. It provides a way to asynchronously
43 * return results and errors.
46 struct _GDBusMethodInvocationPrivate
48 /* construct-only properties */
51 gchar *interface_name;
53 const GDBusMethodInfo *method_info;
54 GDBusConnection *connection;
55 GDBusMessage *message;
74 G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT);
77 g_dbus_method_invocation_finalize (GObject *object)
79 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
81 g_free (invocation->priv->sender);
82 g_free (invocation->priv->object_path);
83 g_free (invocation->priv->interface_name);
84 g_free (invocation->priv->method_name);
85 g_object_unref (invocation->priv->connection);
86 g_object_unref (invocation->priv->message);
87 g_variant_unref (invocation->priv->parameters);
89 if (G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize != NULL)
90 G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
94 g_dbus_method_invocation_get_property (GObject *object,
99 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
104 g_value_set_string (value, g_dbus_method_invocation_get_sender (invocation));
107 case PROP_OBJECT_PATH:
108 g_value_set_string (value, g_dbus_method_invocation_get_object_path (invocation));
111 case PROP_INTERFACE_NAME:
112 g_value_set_string (value, g_dbus_method_invocation_get_interface_name (invocation));
115 case PROP_METHOD_NAME:
116 g_value_set_string (value, g_dbus_method_invocation_get_method_name (invocation));
119 case PROP_METHOD_INFO:
120 g_value_set_boxed (value, g_dbus_method_invocation_get_method_info (invocation));
123 case PROP_CONNECTION:
124 g_value_set_object (value, g_dbus_method_invocation_get_connection (invocation));
127 case PROP_PARAMETERS:
128 g_value_set_boxed (value, g_dbus_method_invocation_get_parameters (invocation));
132 g_value_set_object (value, g_dbus_method_invocation_get_message (invocation));
136 g_value_set_pointer (value, g_dbus_method_invocation_get_user_data (invocation));
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146 g_dbus_method_invocation_set_property (GObject *object,
151 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
156 invocation->priv->sender = g_value_dup_string (value);
159 case PROP_OBJECT_PATH:
160 invocation->priv->object_path = g_value_dup_string (value);
163 case PROP_INTERFACE_NAME:
164 invocation->priv->interface_name = g_value_dup_string (value);
167 case PROP_METHOD_NAME:
168 invocation->priv->method_name = g_value_dup_string (value);
171 case PROP_METHOD_INFO:
172 invocation->priv->method_info = g_value_dup_boxed (value);
175 case PROP_CONNECTION:
176 invocation->priv->connection = g_value_dup_object (value);
179 case PROP_PARAMETERS:
180 invocation->priv->parameters = g_value_dup_boxed (value);
184 invocation->priv->message = g_value_dup_object (value);
188 invocation->priv->user_data = g_value_get_pointer (value);
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
201 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
203 gobject_class->finalize = g_dbus_method_invocation_finalize;
204 gobject_class->set_property = g_dbus_method_invocation_set_property;
205 gobject_class->get_property = g_dbus_method_invocation_get_property;
208 * GDBusMethodInvocation:sender:
210 * The bus name that invoked the method or %NULL if the connection is not a bus connection.
212 g_object_class_install_property (gobject_class,
214 g_param_spec_string ("sender",
216 _("The bus name that invoked the method."),
220 G_PARAM_CONSTRUCT_ONLY |
221 G_PARAM_STATIC_NAME |
222 G_PARAM_STATIC_BLURB |
223 G_PARAM_STATIC_NICK));
226 * GDBusMethodInvocation:object-path:
228 * The object path the method was invoked on.
230 g_object_class_install_property (gobject_class,
232 g_param_spec_string ("object-path",
234 _("The object path the method was invoked on."),
238 G_PARAM_CONSTRUCT_ONLY |
239 G_PARAM_STATIC_NAME |
240 G_PARAM_STATIC_BLURB |
241 G_PARAM_STATIC_NICK));
244 * GDBusMethodInvocation:interface-name:
246 * The name of the D-Bus interface the method was invoked on.
248 g_object_class_install_property (gobject_class,
250 g_param_spec_string ("interface-name",
252 _("The name of the D-Bus interface the method was invoked on."),
256 G_PARAM_CONSTRUCT_ONLY |
257 G_PARAM_STATIC_NAME |
258 G_PARAM_STATIC_BLURB |
259 G_PARAM_STATIC_NICK));
262 * GDBusMethodInvocation:method-name:
264 * The name of the method that was invoked.
266 g_object_class_install_property (gobject_class,
268 g_param_spec_string ("method-name",
270 _("The name of the method that was invoked."),
274 G_PARAM_CONSTRUCT_ONLY |
275 G_PARAM_STATIC_NAME |
276 G_PARAM_STATIC_BLURB |
277 G_PARAM_STATIC_NICK));
280 * GDBusMethodInvocation:method-info:
282 * Information about the method that was invoked, if any.
284 g_object_class_install_property (gobject_class,
286 g_param_spec_boxed ("method-info",
288 _("Information about the method that was invoked, if any."),
289 G_TYPE_DBUS_METHOD_INFO,
292 G_PARAM_CONSTRUCT_ONLY |
293 G_PARAM_STATIC_NAME |
294 G_PARAM_STATIC_BLURB |
295 G_PARAM_STATIC_NICK));
298 * GDBusMethodInvocation:connection:
300 * The #GDBusConnection the method was invoked on.
302 g_object_class_install_property (gobject_class,
304 g_param_spec_object ("connection",
306 _("The #GDBusConnection the method was invoked on."),
307 G_TYPE_DBUS_CONNECTION,
310 G_PARAM_CONSTRUCT_ONLY |
311 G_PARAM_STATIC_NAME |
312 G_PARAM_STATIC_BLURB |
313 G_PARAM_STATIC_NICK));
316 * GDBusMethodInvocation:message:
320 g_object_class_install_property (gobject_class,
322 g_param_spec_object ("message",
324 _("The D-Bus Message."),
328 G_PARAM_CONSTRUCT_ONLY |
329 G_PARAM_STATIC_NAME |
330 G_PARAM_STATIC_BLURB |
331 G_PARAM_STATIC_NICK));
334 * GDBusMethodInvocation:parameters:
336 * The parameters as a #GVariant tuple.
338 g_object_class_install_property (gobject_class,
340 g_param_spec_boxed ("parameters",
342 _("The parameters as a #GVariant tuple."),
346 G_PARAM_CONSTRUCT_ONLY |
347 G_PARAM_STATIC_NAME |
348 G_PARAM_STATIC_BLURB |
349 G_PARAM_STATIC_NICK));
352 * GDBusMethodInvocation:user-data:
354 * The @user_data #gpointer passed to g_dbus_connection_register_object().
356 g_object_class_install_property (gobject_class,
358 g_param_spec_pointer ("user-data",
360 _("The gpointer passed to g_dbus_connection_register_object()."),
363 G_PARAM_CONSTRUCT_ONLY |
364 G_PARAM_STATIC_NAME |
365 G_PARAM_STATIC_BLURB |
366 G_PARAM_STATIC_NICK));
368 g_type_class_add_private (klass, sizeof (GDBusMethodInvocationPrivate));
372 g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
374 invocation->priv = G_TYPE_INSTANCE_GET_PRIVATE (invocation,
375 G_TYPE_DBUS_METHOD_INVOCATION,
376 GDBusMethodInvocationPrivate);
380 * g_dbus_method_invocation_get_sender:
381 * @invocation: A #GDBusMethodInvocation.
383 * Gets the bus name that invoked the method.
385 * Returns: A string. Do not free, it is owned by @invocation.
388 g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
390 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
391 return invocation->priv->sender;
395 * g_dbus_method_invocation_get_object_path:
396 * @invocation: A #GDBusMethodInvocation.
398 * Gets the object path the method was invoked on.
400 * Returns: A string. Do not free, it is owned by @invocation.
403 g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
405 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
406 return invocation->priv->object_path;
410 * g_dbus_method_invocation_get_interface_name:
411 * @invocation: A #GDBusMethodInvocation.
413 * Gets the name of the D-Bus interface the method was invoked on.
415 * Returns: A string. Do not free, it is owned by @invocation.
418 g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
420 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
421 return invocation->priv->interface_name;
425 * g_dbus_method_invocation_get_method_info:
426 * @invocation: A #GDBusMethodInvocation.
428 * Gets information about the method call, if any.
430 * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
432 const GDBusMethodInfo *
433 g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
435 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
436 return invocation->priv->method_info;
440 * g_dbus_method_invocation_get_method_name:
441 * @invocation: A #GDBusMethodInvocation.
443 * Gets the name of the method that was invoked.
445 * Returns: A string. Do not free, it is owned by @invocation.
448 g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
450 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
451 return invocation->priv->method_name;
455 * g_dbus_method_invocation_get_connection:
456 * @invocation: A #GDBusMethodInvocation.
458 * Gets the #GDBusConnection the method was invoked on.
460 * Returns: A #GDBusConnection. Do not free, it is owned by @invocation.
463 g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
465 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
466 return invocation->priv->connection;
470 * g_dbus_method_invocation_get_message:
471 * @invocation: A #GDBusMethodInvocation.
473 * Gets the #GDBusMessage for the method invocation. This is useful if
474 * you need to use low-level protocol features, such as UNIX file
475 * descriptor passing, that cannot be properly expressed in the
478 * See <xref linkend="gdbus-server"/> and <xref
479 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
480 * low-level API to send and receive UNIX file descriptors.
482 * Returns: A #GDBusMessage. Do not free, it is owned by @invocation.
485 g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
487 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
488 return invocation->priv->message;
492 * g_dbus_method_invocation_get_parameters:
493 * @invocation: A #GDBusMethodInvocation.
495 * Gets the parameters of the method invocation.
497 * Returns: A #GVariant. Do not free, it is owned by @invocation.
500 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
502 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
503 return invocation->priv->parameters;
507 * g_dbus_method_invocation_get_user_data:
508 * @invocation: A #GDBusMethodInvocation.
510 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
512 * Returns: A #gpointer.
515 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
517 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
518 return invocation->priv->user_data;
522 * g_dbus_method_invocation_new:
523 * @sender: The bus name that invoked the method or %NULL if @connection is not a bus connection.
524 * @object_path: The object path the method was invoked on.
525 * @interface_name: The name of the D-Bus interface the method was invoked on.
526 * @method_name: The name of the method that was invoked.
527 * @method_info: Information about the method call or %NULL.
528 * @connection: The #GDBusConnection the method was invoked on.
529 * @message: The D-Bus message as a #GDBusMessage.
530 * @parameters: The parameters as a #GVariant tuple.
531 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
533 * Creates a new #GDBusMethodInvocation object.
535 * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
537 GDBusMethodInvocation *
538 g_dbus_method_invocation_new (const gchar *sender,
539 const gchar *object_path,
540 const gchar *interface_name,
541 const gchar *method_name,
542 const GDBusMethodInfo *method_info,
543 GDBusConnection *connection,
544 GDBusMessage *message,
545 GVariant *parameters,
548 g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
549 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
550 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
551 g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
552 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
553 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
554 g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
556 return G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION,
558 "object-path", object_path,
559 "interface-name", interface_name,
560 "method-name", method_name,
561 "method-info", method_info,
562 "connection", connection,
564 "parameters", parameters,
565 "user-data", user_data,
569 /* ---------------------------------------------------------------------------------------------------- */
572 * g_dbus_method_invocation_return_value:
573 * @invocation: A #GDBusMethodInvocation.
574 * @parameters: A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
576 * Finishes handling a D-Bus method call by returning @parameters.
578 * It is an error if @parameters is not of the right format.
580 * This method will free @invocation, you cannot use it afterwards.
583 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
584 GVariant *parameters)
589 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
590 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
592 if (parameters != NULL)
593 g_variant_ref_sink (parameters);
595 /* if we have introspection data, check that the signature of @parameters is correct */
596 if (invocation->priv->method_info != NULL)
599 const gchar *type_string;
602 if (parameters != NULL)
603 type_string = g_variant_get_type_string (parameters);
604 signature = _g_dbus_compute_complete_signature (invocation->priv->method_info->out_args, TRUE);
606 if (g_strcmp0 (type_string, signature) != 0)
608 g_warning (_("Type of return value is incorrect, got `%s', expected `%s'"),
617 reply = g_dbus_message_new_method_reply (invocation->priv->message);
618 g_dbus_message_set_body (reply, parameters);
620 if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, &error))
622 g_warning (_("Error sending message: %s"), error->message);
623 g_error_free (error);
625 g_object_unref (reply);
628 g_object_unref (invocation);
629 if (parameters != NULL)
630 g_variant_unref (parameters);
633 /* ---------------------------------------------------------------------------------------------------- */
636 * g_dbus_method_invocation_return_error:
637 * @invocation: A #GDBusMethodInvocation.
638 * @domain: A #GQuark for the #GError error domain.
639 * @code: The error code.
640 * @format: printf()-style format.
641 * @...: Parameters for @format.
643 * Finishes handling a D-Bus method call by returning an error.
645 * See g_dbus_error_encode_gerror() for details about what error name
646 * will be returned on the wire. In a nutshell, if the given error is
647 * registered using g_dbus_error_register_error() the name given
648 * during registration is used. Otherwise, a name of the form
649 * <literal>org.gtk.GDBus.UnmappedGError.Quark...</literal> is
650 * used. This provides transparent mapping of #GError between
651 * applications using GDBus.
653 * If you are writing an application intended to be portable,
654 * <emphasis>always</emphasis> register errors with g_dbus_error_register_error()
655 * or use g_dbus_method_invocation_return_dbus_error().
657 * This method will free @invocation, you cannot use it afterwards.
660 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
668 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
669 g_return_if_fail (format != NULL);
671 va_start (var_args, format);
672 g_dbus_method_invocation_return_error_valist (invocation,
681 * g_dbus_method_invocation_return_error_valist:
682 * @invocation: A #GDBusMethodInvocation.
683 * @domain: A #GQuark for the #GError error domain.
684 * @code: The error code.
685 * @format: printf()-style format.
686 * @var_args: #va_list of parameters for @format.
688 * Like g_dbus_method_invocation_return_error() but intended for
691 * This method will free @invocation, you cannot use it afterwards.
694 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
700 gchar *literal_message;
702 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
703 g_return_if_fail (format != NULL);
705 literal_message = g_strdup_vprintf (format, var_args);
706 g_dbus_method_invocation_return_error_literal (invocation,
710 g_free (literal_message);
714 * g_dbus_method_invocation_return_error_literal:
715 * @invocation: A #GDBusMethodInvocation.
716 * @domain: A #GQuark for the #GError error domain.
717 * @code: The error code.
718 * @message: The error message.
720 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
722 * This method will free @invocation, you cannot use it afterwards.
725 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
728 const gchar *message)
732 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
733 g_return_if_fail (message != NULL);
735 error = g_error_new_literal (domain, code, message);
736 g_dbus_method_invocation_return_gerror (invocation, error);
737 g_error_free (error);
741 * g_dbus_method_invocation_return_gerror:
742 * @invocation: A #GDBusMethodInvocation.
745 * Like g_dbus_method_invocation_return_error() but takes a #GError
746 * instead of the error domain, error code and message.
748 * This method will free @invocation, you cannot use it afterwards.
751 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
754 gchar *dbus_error_name;
756 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
757 g_return_if_fail (error != NULL);
759 dbus_error_name = g_dbus_error_encode_gerror (error);
761 g_dbus_method_invocation_return_dbus_error (invocation,
764 g_free (dbus_error_name);
768 * g_dbus_method_invocation_return_dbus_error:
769 * @invocation: A #GDBusMethodInvocation.
770 * @error_name: A valid D-Bus error name.
771 * @error_message: A valid D-Bus error message.
773 * Finishes handling a D-Bus method call by returning an error.
775 * This method will free @invocation, you cannot use it afterwards.
778 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
779 const gchar *error_name,
780 const gchar *error_message)
784 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
785 g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
786 g_return_if_fail (error_message != NULL);
788 reply = g_dbus_message_new_method_error_literal (invocation->priv->message,
791 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, NULL);
792 g_object_unref (reply);
794 g_object_unref (invocation);