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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
25 #include "gdbusutils.h"
26 #include "gdbusconnection.h"
27 #include "gdbusmessage.h"
28 #include "gdbusmethodinvocation.h"
29 #include "gdbusintrospection.h"
30 #include "gdbuserror.h"
31 #include "gdbusprivate.h"
34 #include "gunixfdlist.h"
40 * SECTION:gdbusmethodinvocation
41 * @short_description: Object for handling remote calls
44 * Instances of the #GDBusMethodInvocation class are used when
45 * handling D-Bus method calls. It provides a way to asynchronously
46 * return results and errors.
48 * The normal way to obtain a #GDBusMethodInvocation object is to receive
49 * it as an argument to the handle_method_call() function in a
50 * #GDBusInterfaceVTable that was passed to g_dbus_connection_register_object().
53 typedef struct _GDBusMethodInvocationClass GDBusMethodInvocationClass;
56 * GDBusMethodInvocationClass:
58 * Class structure for #GDBusMethodInvocation.
62 struct _GDBusMethodInvocationClass
65 GObjectClass parent_class;
69 * GDBusMethodInvocation:
71 * The #GDBusMethodInvocation structure contains only private data and
72 * should only be accessed using the provided API.
76 struct _GDBusMethodInvocation
79 GObject parent_instance;
81 /* construct-only properties */
84 gchar *interface_name;
86 GDBusMethodInfo *method_info;
87 GDBusPropertyInfo *property_info;
88 GDBusConnection *connection;
89 GDBusMessage *message;
94 G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT);
97 g_dbus_method_invocation_finalize (GObject *object)
99 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
101 g_free (invocation->sender);
102 g_free (invocation->object_path);
103 g_free (invocation->interface_name);
104 g_free (invocation->method_name);
105 if (invocation->method_info)
106 g_dbus_method_info_unref (invocation->method_info);
107 g_object_unref (invocation->connection);
108 g_object_unref (invocation->message);
109 g_variant_unref (invocation->parameters);
111 G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
115 g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
117 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119 gobject_class->finalize = g_dbus_method_invocation_finalize;
123 g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
128 * g_dbus_method_invocation_get_sender:
129 * @invocation: A #GDBusMethodInvocation.
131 * Gets the bus name that invoked the method.
133 * Returns: A string. Do not free, it is owned by @invocation.
138 g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
140 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
141 return invocation->sender;
145 * g_dbus_method_invocation_get_object_path:
146 * @invocation: A #GDBusMethodInvocation.
148 * Gets the object path the method was invoked on.
150 * Returns: A string. Do not free, it is owned by @invocation.
155 g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
157 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
158 return invocation->object_path;
162 * g_dbus_method_invocation_get_interface_name:
163 * @invocation: A #GDBusMethodInvocation.
165 * Gets the name of the D-Bus interface the method was invoked on.
167 * If this method call is a property Get, Set or GetAll call that has
168 * been redirected to the method call handler then
169 * "org.freedesktop.DBus.Properties" will be returned. See
170 * #GDBusInterfaceVTable for more information.
172 * Returns: A string. Do not free, it is owned by @invocation.
177 g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
179 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
180 return invocation->interface_name;
184 * g_dbus_method_invocation_get_method_info:
185 * @invocation: A #GDBusMethodInvocation.
187 * Gets information about the method call, if any.
189 * If this method invocation is a property Get, Set or GetAll call that
190 * has been redirected to the method call handler then %NULL will be
191 * returned. See g_dbus_method_invocation_get_property_info() and
192 * #GDBusInterfaceVTable for more information.
194 * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
198 const GDBusMethodInfo *
199 g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
201 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
202 return invocation->method_info;
206 * g_dbus_method_invocation_get_property_info:
207 * @invocation: A #GDBusMethodInvocation
209 * Gets information about the property that this method call is for, if
212 * This will only be set in the case of an invocation in response to a
213 * property Get or Set call that has been directed to the method call
214 * handler for an object on account of its property_get() or
215 * property_set() vtable pointers being unset.
217 * See #GDBusInterfaceVTable for more information.
219 * If the call was GetAll, %NULL will be returned.
221 * Returns: (transfer none): a #GDBusPropertyInfo or %NULL
225 const GDBusPropertyInfo *
226 g_dbus_method_invocation_get_property_info (GDBusMethodInvocation *invocation)
228 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
229 return invocation->property_info;
233 * g_dbus_method_invocation_get_method_name:
234 * @invocation: A #GDBusMethodInvocation.
236 * Gets the name of the method that was invoked.
238 * Returns: A string. Do not free, it is owned by @invocation.
243 g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
245 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
246 return invocation->method_name;
250 * g_dbus_method_invocation_get_connection:
251 * @invocation: A #GDBusMethodInvocation.
253 * Gets the #GDBusConnection the method was invoked on.
255 * Returns: (transfer none):A #GDBusConnection. Do not free, it is owned by @invocation.
260 g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
262 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
263 return invocation->connection;
267 * g_dbus_method_invocation_get_message:
268 * @invocation: A #GDBusMethodInvocation.
270 * Gets the #GDBusMessage for the method invocation. This is useful if
271 * you need to use low-level protocol features, such as UNIX file
272 * descriptor passing, that cannot be properly expressed in the
275 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
276 * for an example of how to use this low-level API to send and receive
277 * UNIX file descriptors.
279 * Returns: (transfer none): #GDBusMessage. Do not free, it is owned by @invocation.
284 g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
286 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
287 return invocation->message;
291 * g_dbus_method_invocation_get_parameters:
292 * @invocation: A #GDBusMethodInvocation.
294 * Gets the parameters of the method invocation. If there are no input
295 * parameters then this will return a GVariant with 0 children rather than NULL.
297 * Returns: (transfer none): A #GVariant tuple. Do not unref this because it is owned by @invocation.
302 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
304 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
305 return invocation->parameters;
309 * g_dbus_method_invocation_get_user_data: (skip)
310 * @invocation: A #GDBusMethodInvocation.
312 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
314 * Returns: A #gpointer.
319 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
321 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
322 return invocation->user_data;
326 * _g_dbus_method_invocation_new:
327 * @sender: (allow-none): The bus name that invoked the method or %NULL if @connection is not a bus connection.
328 * @object_path: The object path the method was invoked on.
329 * @interface_name: The name of the D-Bus interface the method was invoked on.
330 * @method_name: The name of the method that was invoked.
331 * @method_info: (allow-none): Information about the method call or %NULL.
332 * @property_info: (allow-none): Information about the property or %NULL.
333 * @connection: The #GDBusConnection the method was invoked on.
334 * @message: The D-Bus message as a #GDBusMessage.
335 * @parameters: The parameters as a #GVariant tuple.
336 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
338 * Creates a new #GDBusMethodInvocation object.
340 * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
344 GDBusMethodInvocation *
345 _g_dbus_method_invocation_new (const gchar *sender,
346 const gchar *object_path,
347 const gchar *interface_name,
348 const gchar *method_name,
349 const GDBusMethodInfo *method_info,
350 const GDBusPropertyInfo *property_info,
351 GDBusConnection *connection,
352 GDBusMessage *message,
353 GVariant *parameters,
356 GDBusMethodInvocation *invocation;
358 g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
359 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
360 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
361 g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
362 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
363 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
364 g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
366 invocation = G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION, NULL));
367 invocation->sender = g_strdup (sender);
368 invocation->object_path = g_strdup (object_path);
369 invocation->interface_name = g_strdup (interface_name);
370 invocation->method_name = g_strdup (method_name);
372 invocation->method_info = g_dbus_method_info_ref ((GDBusMethodInfo *)method_info);
374 invocation->property_info = g_dbus_property_info_ref ((GDBusPropertyInfo *)property_info);
375 invocation->connection = g_object_ref (connection);
376 invocation->message = g_object_ref (message);
377 invocation->parameters = g_variant_ref (parameters);
378 invocation->user_data = user_data;
383 /* ---------------------------------------------------------------------------------------------------- */
386 g_dbus_method_invocation_return_value_internal (GDBusMethodInvocation *invocation,
387 GVariant *parameters,
388 GUnixFDList *fd_list)
393 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
394 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
396 if (parameters == NULL)
397 parameters = g_variant_new_tuple (NULL, 0);
399 /* if we have introspection data, check that the signature of @parameters is correct */
400 if (invocation->method_info != NULL)
404 type = _g_dbus_compute_complete_signature (invocation->method_info->out_args);
406 if (!g_variant_is_of_type (parameters, type))
408 gchar *type_string = g_variant_type_dup_string (type);
410 g_warning ("Type of return value is incorrect: expected '%s', got '%s''",
411 type_string, g_variant_get_type_string (parameters));
412 g_variant_type_free (type);
413 g_free (type_string);
416 g_variant_type_free (type);
419 /* property_info is only non-NULL if set that way from
420 * GDBusConnection, so this must be the case of async property
421 * handling on either 'Get', 'Set' or 'GetAll'.
423 if (invocation->property_info != NULL)
425 if (g_str_equal (invocation->method_name, "Get"))
429 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(v)")))
431 g_warning ("Type of return value for property 'Get' call should be '(v)' but got '%s'",
432 g_variant_get_type_string (parameters));
436 /* Go deeper and make sure that the value inside of the
437 * variant matches the property type.
439 g_variant_get (parameters, "(v)", &nested);
440 if (!g_str_equal (g_variant_get_type_string (nested), invocation->property_info->signature))
442 g_warning ("Value returned from property 'Get' call for '%s' should be '%s' but is '%s'",
443 invocation->property_info->name, invocation->property_info->signature,
444 g_variant_get_type_string (nested));
445 g_variant_unref (nested);
448 g_variant_unref (nested);
451 else if (g_str_equal (invocation->method_name, "GetAll"))
453 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a{sv})")))
455 g_warning ("Type of return value for property 'GetAll' call should be '(a{sv})' but got '%s'",
456 g_variant_get_type_string (parameters));
460 /* Could iterate the list of properties and make sure that all
461 * of them are actually on the interface and with the correct
462 * types, but let's not do that for now...
466 else if (g_str_equal (invocation->method_name, "Set"))
468 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE_UNIT))
470 g_warning ("Type of return value for property 'Set' call should be '()' but got '%s'",
471 g_variant_get_type_string (parameters));
477 g_assert_not_reached ();
480 if (G_UNLIKELY (_g_dbus_debug_return ()))
482 _g_dbus_debug_print_lock ();
483 g_print ("========================================================================\n"
484 "GDBus-debug:Return:\n"
485 " >>>> METHOD RETURN\n"
486 " in response to %s.%s()\n"
489 " reply-serial %d\n",
490 invocation->interface_name, invocation->method_name,
491 invocation->object_path,
493 g_dbus_message_get_serial (invocation->message));
494 _g_dbus_debug_print_unlock ();
497 reply = g_dbus_message_new_method_reply (invocation->message);
498 g_dbus_message_set_body (reply, parameters);
502 g_dbus_message_set_unix_fd_list (reply, fd_list);
506 if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error))
508 g_warning ("Error sending message: %s", error->message);
509 g_error_free (error);
511 g_object_unref (reply);
514 g_object_unref (invocation);
518 * g_dbus_method_invocation_return_value:
519 * @invocation: (transfer full): A #GDBusMethodInvocation.
520 * @parameters: (allow-none): A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
522 * Finishes handling a D-Bus method call by returning @parameters.
523 * If the @parameters GVariant is floating, it is consumed.
525 * It is an error if @parameters is not of the right format.
527 * This method will free @invocation, you cannot use it afterwards.
532 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
533 GVariant *parameters)
535 g_dbus_method_invocation_return_value_internal (invocation, parameters, NULL);
540 * g_dbus_method_invocation_return_value_with_unix_fd_list:
541 * @invocation: (transfer full): A #GDBusMethodInvocation.
542 * @parameters: (allow-none): A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
543 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
545 * Like g_dbus_method_invocation_return_value() but also takes a #GUnixFDList.
547 * This method is only available on UNIX.
549 * This method will free @invocation, you cannot use it afterwards.
554 g_dbus_method_invocation_return_value_with_unix_fd_list (GDBusMethodInvocation *invocation,
555 GVariant *parameters,
556 GUnixFDList *fd_list)
558 g_dbus_method_invocation_return_value_internal (invocation, parameters, fd_list);
562 /* ---------------------------------------------------------------------------------------------------- */
565 * g_dbus_method_invocation_return_error:
566 * @invocation: (transfer full): A #GDBusMethodInvocation.
567 * @domain: A #GQuark for the #GError error domain.
568 * @code: The error code.
569 * @format: printf()-style format.
570 * @...: Parameters for @format.
572 * Finishes handling a D-Bus method call by returning an error.
574 * See g_dbus_error_encode_gerror() for details about what error name
575 * will be returned on the wire. In a nutshell, if the given error is
576 * registered using g_dbus_error_register_error() the name given
577 * during registration is used. Otherwise, a name of the form
578 * `org.gtk.GDBus.UnmappedGError.Quark...` is used. This provides
579 * transparent mapping of #GError between applications using GDBus.
581 * If you are writing an application intended to be portable,
582 * always register errors with g_dbus_error_register_error()
583 * or use g_dbus_method_invocation_return_dbus_error().
585 * This method will free @invocation, you cannot use it afterwards.
590 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
598 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
599 g_return_if_fail (format != NULL);
601 va_start (var_args, format);
602 g_dbus_method_invocation_return_error_valist (invocation,
611 * g_dbus_method_invocation_return_error_valist:
612 * @invocation: (transfer full): A #GDBusMethodInvocation.
613 * @domain: A #GQuark for the #GError error domain.
614 * @code: The error code.
615 * @format: printf()-style format.
616 * @var_args: #va_list of parameters for @format.
618 * Like g_dbus_method_invocation_return_error() but intended for
621 * This method will free @invocation, you cannot use it afterwards.
626 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
632 gchar *literal_message;
634 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
635 g_return_if_fail (format != NULL);
637 literal_message = g_strdup_vprintf (format, var_args);
638 g_dbus_method_invocation_return_error_literal (invocation,
642 g_free (literal_message);
646 * g_dbus_method_invocation_return_error_literal:
647 * @invocation: (transfer full): A #GDBusMethodInvocation.
648 * @domain: A #GQuark for the #GError error domain.
649 * @code: The error code.
650 * @message: The error message.
652 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
654 * This method will free @invocation, you cannot use it afterwards.
659 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
662 const gchar *message)
666 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
667 g_return_if_fail (message != NULL);
669 error = g_error_new_literal (domain, code, message);
670 g_dbus_method_invocation_return_gerror (invocation, error);
671 g_error_free (error);
675 * g_dbus_method_invocation_return_gerror:
676 * @invocation: (transfer full): A #GDBusMethodInvocation.
679 * Like g_dbus_method_invocation_return_error() but takes a #GError
680 * instead of the error domain, error code and message.
682 * This method will free @invocation, you cannot use it afterwards.
687 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
690 gchar *dbus_error_name;
692 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
693 g_return_if_fail (error != NULL);
695 dbus_error_name = g_dbus_error_encode_gerror (error);
697 g_dbus_method_invocation_return_dbus_error (invocation,
700 g_free (dbus_error_name);
704 * g_dbus_method_invocation_take_error: (skip)
705 * @invocation: (transfer full): A #GDBusMethodInvocation.
706 * @error: (transfer full): A #GError.
708 * Like g_dbus_method_invocation_return_gerror() but takes ownership
709 * of @error so the caller does not need to free it.
711 * This method will free @invocation, you cannot use it afterwards.
716 g_dbus_method_invocation_take_error (GDBusMethodInvocation *invocation,
719 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
720 g_return_if_fail (error != NULL);
721 g_dbus_method_invocation_return_gerror (invocation, error);
722 g_error_free (error);
726 * g_dbus_method_invocation_return_dbus_error:
727 * @invocation: (transfer full): A #GDBusMethodInvocation.
728 * @error_name: A valid D-Bus error name.
729 * @error_message: A valid D-Bus error message.
731 * Finishes handling a D-Bus method call by returning an error.
733 * This method will free @invocation, you cannot use it afterwards.
738 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
739 const gchar *error_name,
740 const gchar *error_message)
744 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
745 g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
746 g_return_if_fail (error_message != NULL);
748 if (G_UNLIKELY (_g_dbus_debug_return ()))
750 _g_dbus_debug_print_lock ();
751 g_print ("========================================================================\n"
752 "GDBus-debug:Return:\n"
753 " >>>> METHOD ERROR %s\n"
755 " in response to %s.%s()\n"
758 " reply-serial %d\n",
761 invocation->interface_name, invocation->method_name,
762 invocation->object_path,
764 g_dbus_message_get_serial (invocation->message));
765 _g_dbus_debug_print_unlock ();
768 reply = g_dbus_message_new_method_error_literal (invocation->message,
771 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
772 g_object_unref (reply);
774 g_object_unref (invocation);