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 G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
99 g_dbus_method_invocation_get_property (GObject *object,
104 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
109 g_value_set_string (value, g_dbus_method_invocation_get_sender (invocation));
112 case PROP_OBJECT_PATH:
113 g_value_set_string (value, g_dbus_method_invocation_get_object_path (invocation));
116 case PROP_INTERFACE_NAME:
117 g_value_set_string (value, g_dbus_method_invocation_get_interface_name (invocation));
120 case PROP_METHOD_NAME:
121 g_value_set_string (value, g_dbus_method_invocation_get_method_name (invocation));
124 case PROP_METHOD_INFO:
125 g_value_set_boxed (value, g_dbus_method_invocation_get_method_info (invocation));
128 case PROP_CONNECTION:
129 g_value_set_object (value, g_dbus_method_invocation_get_connection (invocation));
132 case PROP_PARAMETERS:
133 g_value_set_boxed (value, g_dbus_method_invocation_get_parameters (invocation));
137 g_value_set_object (value, g_dbus_method_invocation_get_message (invocation));
141 g_value_set_pointer (value, g_dbus_method_invocation_get_user_data (invocation));
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 g_dbus_method_invocation_set_property (GObject *object,
156 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
161 invocation->priv->sender = g_value_dup_string (value);
164 case PROP_OBJECT_PATH:
165 invocation->priv->object_path = g_value_dup_string (value);
168 case PROP_INTERFACE_NAME:
169 invocation->priv->interface_name = g_value_dup_string (value);
172 case PROP_METHOD_NAME:
173 invocation->priv->method_name = g_value_dup_string (value);
176 case PROP_METHOD_INFO:
177 invocation->priv->method_info = g_value_dup_boxed (value);
180 case PROP_CONNECTION:
181 invocation->priv->connection = g_value_dup_object (value);
184 case PROP_PARAMETERS:
185 invocation->priv->parameters = g_value_dup_boxed (value);
189 invocation->priv->message = g_value_dup_object (value);
193 invocation->priv->user_data = g_value_get_pointer (value);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
204 g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
206 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
208 gobject_class->finalize = g_dbus_method_invocation_finalize;
209 gobject_class->set_property = g_dbus_method_invocation_set_property;
210 gobject_class->get_property = g_dbus_method_invocation_get_property;
213 * GDBusMethodInvocation:sender:
215 * The bus name that invoked the method or %NULL if the connection is not a bus connection.
219 g_object_class_install_property (gobject_class,
221 g_param_spec_string ("sender",
223 P_("The bus name that invoked the method."),
227 G_PARAM_CONSTRUCT_ONLY |
228 G_PARAM_STATIC_NAME |
229 G_PARAM_STATIC_BLURB |
230 G_PARAM_STATIC_NICK));
233 * GDBusMethodInvocation:object-path:
235 * The object path the method was invoked on.
239 g_object_class_install_property (gobject_class,
241 g_param_spec_string ("object-path",
243 P_("The object path the method was invoked on."),
247 G_PARAM_CONSTRUCT_ONLY |
248 G_PARAM_STATIC_NAME |
249 G_PARAM_STATIC_BLURB |
250 G_PARAM_STATIC_NICK));
253 * GDBusMethodInvocation:interface-name:
255 * The name of the D-Bus interface the method was invoked on.
259 g_object_class_install_property (gobject_class,
261 g_param_spec_string ("interface-name",
262 P_("Interface Name"),
263 P_("The name of the D-Bus interface the method was invoked on."),
267 G_PARAM_CONSTRUCT_ONLY |
268 G_PARAM_STATIC_NAME |
269 G_PARAM_STATIC_BLURB |
270 G_PARAM_STATIC_NICK));
273 * GDBusMethodInvocation:method-name:
275 * The name of the method that was invoked.
279 g_object_class_install_property (gobject_class,
281 g_param_spec_string ("method-name",
283 P_("The name of the method that was invoked."),
287 G_PARAM_CONSTRUCT_ONLY |
288 G_PARAM_STATIC_NAME |
289 G_PARAM_STATIC_BLURB |
290 G_PARAM_STATIC_NICK));
293 * GDBusMethodInvocation:method-info:
295 * Information about the method that was invoked, if any.
299 g_object_class_install_property (gobject_class,
301 g_param_spec_boxed ("method-info",
303 P_("Information about the method that was invoked, if any."),
304 G_TYPE_DBUS_METHOD_INFO,
307 G_PARAM_CONSTRUCT_ONLY |
308 G_PARAM_STATIC_NAME |
309 G_PARAM_STATIC_BLURB |
310 G_PARAM_STATIC_NICK));
313 * GDBusMethodInvocation:connection:
315 * The #GDBusConnection the method was invoked on.
319 g_object_class_install_property (gobject_class,
321 g_param_spec_object ("connection",
323 P_("The #GDBusConnection the method was invoked on."),
324 G_TYPE_DBUS_CONNECTION,
327 G_PARAM_CONSTRUCT_ONLY |
328 G_PARAM_STATIC_NAME |
329 G_PARAM_STATIC_BLURB |
330 G_PARAM_STATIC_NICK));
333 * GDBusMethodInvocation:message:
339 g_object_class_install_property (gobject_class,
341 g_param_spec_object ("message",
343 P_("The D-Bus Message."),
347 G_PARAM_CONSTRUCT_ONLY |
348 G_PARAM_STATIC_NAME |
349 G_PARAM_STATIC_BLURB |
350 G_PARAM_STATIC_NICK));
353 * GDBusMethodInvocation:parameters:
355 * The parameters as a #GVariant tuple.
359 g_object_class_install_property (gobject_class,
361 g_param_spec_boxed ("parameters",
363 P_("The parameters as a #GVariant tuple."),
367 G_PARAM_CONSTRUCT_ONLY |
368 G_PARAM_STATIC_NAME |
369 G_PARAM_STATIC_BLURB |
370 G_PARAM_STATIC_NICK));
373 * GDBusMethodInvocation:user-data:
375 * The @user_data #gpointer passed to g_dbus_connection_register_object().
379 g_object_class_install_property (gobject_class,
381 g_param_spec_pointer ("user-data",
383 P_("The gpointer passed to g_dbus_connection_register_object()."),
386 G_PARAM_CONSTRUCT_ONLY |
387 G_PARAM_STATIC_NAME |
388 G_PARAM_STATIC_BLURB |
389 G_PARAM_STATIC_NICK));
391 g_type_class_add_private (klass, sizeof (GDBusMethodInvocationPrivate));
395 g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
397 invocation->priv = G_TYPE_INSTANCE_GET_PRIVATE (invocation,
398 G_TYPE_DBUS_METHOD_INVOCATION,
399 GDBusMethodInvocationPrivate);
403 * g_dbus_method_invocation_get_sender:
404 * @invocation: A #GDBusMethodInvocation.
406 * Gets the bus name that invoked the method.
408 * Returns: A string. Do not free, it is owned by @invocation.
413 g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
415 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
416 return invocation->priv->sender;
420 * g_dbus_method_invocation_get_object_path:
421 * @invocation: A #GDBusMethodInvocation.
423 * Gets the object path the method was invoked on.
425 * Returns: A string. Do not free, it is owned by @invocation.
430 g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
432 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
433 return invocation->priv->object_path;
437 * g_dbus_method_invocation_get_interface_name:
438 * @invocation: A #GDBusMethodInvocation.
440 * Gets the name of the D-Bus interface the method was invoked on.
442 * Returns: A string. Do not free, it is owned by @invocation.
447 g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
449 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
450 return invocation->priv->interface_name;
454 * g_dbus_method_invocation_get_method_info:
455 * @invocation: A #GDBusMethodInvocation.
457 * Gets information about the method call, if any.
459 * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
463 const GDBusMethodInfo *
464 g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
466 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
467 return invocation->priv->method_info;
471 * g_dbus_method_invocation_get_method_name:
472 * @invocation: A #GDBusMethodInvocation.
474 * Gets the name of the method that was invoked.
476 * Returns: A string. Do not free, it is owned by @invocation.
481 g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
483 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
484 return invocation->priv->method_name;
488 * g_dbus_method_invocation_get_connection:
489 * @invocation: A #GDBusMethodInvocation.
491 * Gets the #GDBusConnection the method was invoked on.
493 * Returns: A #GDBusConnection. Do not free, it is owned by @invocation.
498 g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
500 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
501 return invocation->priv->connection;
505 * g_dbus_method_invocation_get_message:
506 * @invocation: A #GDBusMethodInvocation.
508 * Gets the #GDBusMessage for the method invocation. This is useful if
509 * you need to use low-level protocol features, such as UNIX file
510 * descriptor passing, that cannot be properly expressed in the
513 * See <xref linkend="gdbus-server"/> and <xref
514 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
515 * low-level API to send and receive UNIX file descriptors.
517 * Returns: A #GDBusMessage. Do not free, it is owned by @invocation.
522 g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
524 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
525 return invocation->priv->message;
529 * g_dbus_method_invocation_get_parameters:
530 * @invocation: A #GDBusMethodInvocation.
532 * Gets the parameters of the method invocation.
534 * Returns: A #GVariant. Do not free, it is owned by @invocation.
539 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
541 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
542 return invocation->priv->parameters;
546 * g_dbus_method_invocation_get_user_data:
547 * @invocation: A #GDBusMethodInvocation.
549 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
551 * Returns: A #gpointer.
556 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
558 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
559 return invocation->priv->user_data;
563 * g_dbus_method_invocation_new:
564 * @sender: The bus name that invoked the method or %NULL if @connection is not a bus connection.
565 * @object_path: The object path the method was invoked on.
566 * @interface_name: The name of the D-Bus interface the method was invoked on.
567 * @method_name: The name of the method that was invoked.
568 * @method_info: Information about the method call or %NULL.
569 * @connection: The #GDBusConnection the method was invoked on.
570 * @message: The D-Bus message as a #GDBusMessage.
571 * @parameters: The parameters as a #GVariant tuple.
572 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
574 * Creates a new #GDBusMethodInvocation object.
576 * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
580 GDBusMethodInvocation *
581 g_dbus_method_invocation_new (const gchar *sender,
582 const gchar *object_path,
583 const gchar *interface_name,
584 const gchar *method_name,
585 const GDBusMethodInfo *method_info,
586 GDBusConnection *connection,
587 GDBusMessage *message,
588 GVariant *parameters,
591 g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
592 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
593 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
594 g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
595 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
596 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
597 g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
599 return G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION,
601 "object-path", object_path,
602 "interface-name", interface_name,
603 "method-name", method_name,
604 "method-info", method_info,
605 "connection", connection,
607 "parameters", parameters,
608 "user-data", user_data,
612 /* ---------------------------------------------------------------------------------------------------- */
615 * g_dbus_method_invocation_return_value:
616 * @invocation: A #GDBusMethodInvocation.
617 * @parameters: A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
619 * Finishes handling a D-Bus method call by returning @parameters.
621 * It is an error if @parameters is not of the right format.
623 * This method will free @invocation, you cannot use it afterwards.
628 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
629 GVariant *parameters)
634 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
635 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
637 if (parameters != NULL)
638 g_variant_ref_sink (parameters);
640 /* if we have introspection data, check that the signature of @parameters is correct */
641 if (invocation->priv->method_info != NULL)
644 const gchar *type_string;
647 if (parameters != NULL)
648 type_string = g_variant_get_type_string (parameters);
649 signature = _g_dbus_compute_complete_signature (invocation->priv->method_info->out_args, TRUE);
651 if (g_strcmp0 (type_string, signature) != 0)
653 g_warning (_("Type of return value is incorrect, got `%s', expected `%s'"),
662 reply = g_dbus_message_new_method_reply (invocation->priv->message);
663 g_dbus_message_set_body (reply, parameters);
665 if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, &error))
667 g_warning (_("Error sending message: %s"), error->message);
668 g_error_free (error);
670 g_object_unref (reply);
673 g_object_unref (invocation);
674 if (parameters != NULL)
675 g_variant_unref (parameters);
678 /* ---------------------------------------------------------------------------------------------------- */
681 * g_dbus_method_invocation_return_error:
682 * @invocation: A #GDBusMethodInvocation.
683 * @domain: A #GQuark for the #GError error domain.
684 * @code: The error code.
685 * @format: printf()-style format.
686 * @...: Parameters for @format.
688 * Finishes handling a D-Bus method call by returning an error.
690 * See g_dbus_error_encode_gerror() for details about what error name
691 * will be returned on the wire. In a nutshell, if the given error is
692 * registered using g_dbus_error_register_error() the name given
693 * during registration is used. Otherwise, a name of the form
694 * <literal>org.gtk.GDBus.UnmappedGError.Quark...</literal> is
695 * used. This provides transparent mapping of #GError between
696 * applications using GDBus.
698 * If you are writing an application intended to be portable,
699 * <emphasis>always</emphasis> register errors with g_dbus_error_register_error()
700 * or use g_dbus_method_invocation_return_dbus_error().
702 * This method will free @invocation, you cannot use it afterwards.
707 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
715 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
716 g_return_if_fail (format != NULL);
718 va_start (var_args, format);
719 g_dbus_method_invocation_return_error_valist (invocation,
728 * g_dbus_method_invocation_return_error_valist:
729 * @invocation: A #GDBusMethodInvocation.
730 * @domain: A #GQuark for the #GError error domain.
731 * @code: The error code.
732 * @format: printf()-style format.
733 * @var_args: #va_list of parameters for @format.
735 * Like g_dbus_method_invocation_return_error() but intended for
738 * This method will free @invocation, you cannot use it afterwards.
743 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
749 gchar *literal_message;
751 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
752 g_return_if_fail (format != NULL);
754 literal_message = g_strdup_vprintf (format, var_args);
755 g_dbus_method_invocation_return_error_literal (invocation,
759 g_free (literal_message);
763 * g_dbus_method_invocation_return_error_literal:
764 * @invocation: A #GDBusMethodInvocation.
765 * @domain: A #GQuark for the #GError error domain.
766 * @code: The error code.
767 * @message: The error message.
769 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
771 * This method will free @invocation, you cannot use it afterwards.
776 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
779 const gchar *message)
783 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
784 g_return_if_fail (message != NULL);
786 error = g_error_new_literal (domain, code, message);
787 g_dbus_method_invocation_return_gerror (invocation, error);
788 g_error_free (error);
792 * g_dbus_method_invocation_return_gerror:
793 * @invocation: A #GDBusMethodInvocation.
796 * Like g_dbus_method_invocation_return_error() but takes a #GError
797 * instead of the error domain, error code and message.
799 * This method will free @invocation, you cannot use it afterwards.
804 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
807 gchar *dbus_error_name;
809 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
810 g_return_if_fail (error != NULL);
812 dbus_error_name = g_dbus_error_encode_gerror (error);
814 g_dbus_method_invocation_return_dbus_error (invocation,
817 g_free (dbus_error_name);
821 * g_dbus_method_invocation_return_dbus_error:
822 * @invocation: A #GDBusMethodInvocation.
823 * @error_name: A valid D-Bus error name.
824 * @error_message: A valid D-Bus error message.
826 * Finishes handling a D-Bus method call by returning an error.
828 * This method will free @invocation, you cannot use it afterwards.
833 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
834 const gchar *error_name,
835 const gchar *error_message)
839 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
840 g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
841 g_return_if_fail (error_message != NULL);
843 reply = g_dbus_message_new_method_error_literal (invocation->priv->message,
846 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, NULL);
847 g_object_unref (reply);
849 g_object_unref (invocation);
852 #define __G_DBUS_METHOD_INVOCATION_C__
853 #include "gioaliasdef.c"