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"
36 #include "gunixfdlist.h"
42 * SECTION:gdbusmethodinvocation
43 * @short_description: Object for handling remote calls
46 * Instances of the #GDBusMethodInvocation class are used when
47 * handling D-Bus method calls. It provides a way to asynchronously
48 * return results and errors.
50 * The normal way to obtain a #GDBusMethodInvocation object is to receive
51 * it as an argument to the handle_method_call() function in a
52 * #GDBusInterfaceVTable that was passed to g_dbus_connection_register_object().
55 typedef struct _GDBusMethodInvocationClass GDBusMethodInvocationClass;
58 * GDBusMethodInvocationClass:
60 * Class structure for #GDBusMethodInvocation.
64 struct _GDBusMethodInvocationClass
67 GObjectClass parent_class;
71 * GDBusMethodInvocation:
73 * The #GDBusMethodInvocation structure contains only private data and
74 * should only be accessed using the provided API.
78 struct _GDBusMethodInvocation
81 GObject parent_instance;
83 /* construct-only properties */
86 gchar *interface_name;
88 GDBusMethodInfo *method_info;
89 GDBusPropertyInfo *property_info;
90 GDBusConnection *connection;
91 GDBusMessage *message;
96 G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT);
99 g_dbus_method_invocation_finalize (GObject *object)
101 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
103 g_free (invocation->sender);
104 g_free (invocation->object_path);
105 g_free (invocation->interface_name);
106 g_free (invocation->method_name);
107 if (invocation->method_info)
108 g_dbus_method_info_unref (invocation->method_info);
109 g_object_unref (invocation->connection);
110 g_object_unref (invocation->message);
111 g_variant_unref (invocation->parameters);
113 G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
117 g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
119 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
121 gobject_class->finalize = g_dbus_method_invocation_finalize;
125 g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
130 * g_dbus_method_invocation_get_sender:
131 * @invocation: A #GDBusMethodInvocation.
133 * Gets the bus name that invoked the method.
135 * Returns: A string. Do not free, it is owned by @invocation.
140 g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
142 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
143 return invocation->sender;
147 * g_dbus_method_invocation_get_object_path:
148 * @invocation: A #GDBusMethodInvocation.
150 * Gets the object path the method was invoked on.
152 * Returns: A string. Do not free, it is owned by @invocation.
157 g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
159 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
160 return invocation->object_path;
164 * g_dbus_method_invocation_get_interface_name:
165 * @invocation: A #GDBusMethodInvocation.
167 * Gets the name of the D-Bus interface the method was invoked on.
169 * If this method call is a property Get, Set or GetAll call that has
170 * been redirected to the method call handler then
171 * "org.freedesktop.DBus.Properties" will be returned. See
172 * #GDBusInterfaceVTable for more information.
174 * Returns: A string. Do not free, it is owned by @invocation.
179 g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
181 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
182 return invocation->interface_name;
186 * g_dbus_method_invocation_get_method_info:
187 * @invocation: A #GDBusMethodInvocation.
189 * Gets information about the method call, if any.
191 * If this method invocation is a property Get, Set or GetAll call that
192 * has been redirected to the method call handler then %NULL will be
193 * returned. See g_dbus_method_invocation_get_property_info() and
194 * #GDBusInterfaceVTable for more information.
196 * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
200 const GDBusMethodInfo *
201 g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
203 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
204 return invocation->method_info;
208 * g_dbus_method_invocation_get_property_info:
209 * @invocation: A #GDBusMethodInvocation
211 * Gets information about the property that this method call is for, if
214 * This will only be set in the case of an invocation in response to a
215 * property Get or Set call that has been directed to the method call
216 * handler for an object on account of its property_get() or
217 * property_set() vtable pointers being unset.
219 * See #GDBusInterfaceVTable for more information.
221 * If the call was GetAll, %NULL will be returned.
223 * Returns: (transfer none): a #GDBusPropertyInfo or %NULL
227 const GDBusPropertyInfo *
228 g_dbus_method_invocation_get_property_info (GDBusMethodInvocation *invocation)
230 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
231 return invocation->property_info;
235 * g_dbus_method_invocation_get_method_name:
236 * @invocation: A #GDBusMethodInvocation.
238 * Gets the name of the method that was invoked.
240 * Returns: A string. Do not free, it is owned by @invocation.
245 g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
247 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
248 return invocation->method_name;
252 * g_dbus_method_invocation_get_connection:
253 * @invocation: A #GDBusMethodInvocation.
255 * Gets the #GDBusConnection the method was invoked on.
257 * Returns: (transfer none):A #GDBusConnection. Do not free, it is owned by @invocation.
262 g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
264 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
265 return invocation->connection;
269 * g_dbus_method_invocation_get_message:
270 * @invocation: A #GDBusMethodInvocation.
272 * Gets the #GDBusMessage for the method invocation. This is useful if
273 * you need to use low-level protocol features, such as UNIX file
274 * descriptor passing, that cannot be properly expressed in the
277 * See <xref linkend="gdbus-server"/> and <xref
278 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
279 * low-level API to send and receive UNIX file descriptors.
281 * Returns: (transfer none): #GDBusMessage. Do not free, it is owned by @invocation.
286 g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
288 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
289 return invocation->message;
293 * g_dbus_method_invocation_get_parameters:
294 * @invocation: A #GDBusMethodInvocation.
296 * Gets the parameters of the method invocation. If there are no input
297 * parameters then this will return a GVariant with 0 children rather than NULL.
299 * Returns: (transfer none): A #GVariant tuple. Do not unref this because it is owned by @invocation.
304 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
306 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
307 return invocation->parameters;
311 * g_dbus_method_invocation_get_user_data: (skip)
312 * @invocation: A #GDBusMethodInvocation.
314 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
316 * Returns: A #gpointer.
321 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
323 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
324 return invocation->user_data;
328 * _g_dbus_method_invocation_new:
329 * @sender: (allow-none): The bus name that invoked the method or %NULL if @connection is not a bus connection.
330 * @object_path: The object path the method was invoked on.
331 * @interface_name: The name of the D-Bus interface the method was invoked on.
332 * @method_name: The name of the method that was invoked.
333 * @method_info: (allow-none): Information about the method call or %NULL.
334 * @property_info: (allow-none): Information about the property or %NULL.
335 * @connection: The #GDBusConnection the method was invoked on.
336 * @message: The D-Bus message as a #GDBusMessage.
337 * @parameters: The parameters as a #GVariant tuple.
338 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
340 * Creates a new #GDBusMethodInvocation object.
342 * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
346 GDBusMethodInvocation *
347 _g_dbus_method_invocation_new (const gchar *sender,
348 const gchar *object_path,
349 const gchar *interface_name,
350 const gchar *method_name,
351 const GDBusMethodInfo *method_info,
352 const GDBusPropertyInfo *property_info,
353 GDBusConnection *connection,
354 GDBusMessage *message,
355 GVariant *parameters,
358 GDBusMethodInvocation *invocation;
360 g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
361 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
362 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
363 g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
364 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
365 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
366 g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
368 invocation = G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION, NULL));
369 invocation->sender = g_strdup (sender);
370 invocation->object_path = g_strdup (object_path);
371 invocation->interface_name = g_strdup (interface_name);
372 invocation->method_name = g_strdup (method_name);
374 invocation->method_info = g_dbus_method_info_ref ((GDBusMethodInfo *)method_info);
376 invocation->property_info = g_dbus_property_info_ref ((GDBusPropertyInfo *)property_info);
377 invocation->connection = g_object_ref (connection);
378 invocation->message = g_object_ref (message);
379 invocation->parameters = g_variant_ref (parameters);
380 invocation->user_data = user_data;
385 /* ---------------------------------------------------------------------------------------------------- */
388 g_dbus_method_invocation_return_value_internal (GDBusMethodInvocation *invocation,
389 GVariant *parameters,
390 GUnixFDList *fd_list)
395 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
396 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
398 if (parameters == NULL)
399 parameters = g_variant_new_tuple (NULL, 0);
401 /* if we have introspection data, check that the signature of @parameters is correct */
402 if (invocation->method_info != NULL)
406 type = _g_dbus_compute_complete_signature (invocation->method_info->out_args);
408 if (!g_variant_is_of_type (parameters, type))
410 gchar *type_string = g_variant_type_dup_string (type);
412 g_warning ("Type of return value is incorrect: expected '%s', got '%s''",
413 type_string, g_variant_get_type_string (parameters));
414 g_variant_type_free (type);
415 g_free (type_string);
418 g_variant_type_free (type);
421 if (G_UNLIKELY (_g_dbus_debug_return ()))
423 _g_dbus_debug_print_lock ();
424 g_print ("========================================================================\n"
425 "GDBus-debug:Return:\n"
426 " >>>> METHOD RETURN\n"
427 " in response to %s.%s()\n"
430 " reply-serial %d\n",
431 invocation->interface_name, invocation->method_name,
432 invocation->object_path,
434 g_dbus_message_get_serial (invocation->message));
435 _g_dbus_debug_print_unlock ();
438 reply = g_dbus_message_new_method_reply (invocation->message);
439 g_dbus_message_set_body (reply, parameters);
443 g_dbus_message_set_unix_fd_list (reply, fd_list);
447 if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error))
449 g_warning ("Error sending message: %s", error->message);
450 g_error_free (error);
452 g_object_unref (reply);
455 g_object_unref (invocation);
459 * g_dbus_method_invocation_return_value:
460 * @invocation: (transfer full): A #GDBusMethodInvocation.
461 * @parameters: (allow-none): A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
463 * Finishes handling a D-Bus method call by returning @parameters.
464 * If the @parameters GVariant is floating, it is consumed.
466 * It is an error if @parameters is not of the right format.
468 * This method will free @invocation, you cannot use it afterwards.
473 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
474 GVariant *parameters)
476 g_dbus_method_invocation_return_value_internal (invocation, parameters, NULL);
481 * g_dbus_method_invocation_return_value_with_unix_fd_list:
482 * @invocation: (transfer full): A #GDBusMethodInvocation.
483 * @parameters: (allow-none): A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
484 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
486 * Like g_dbus_method_invocation_return_value() but also takes a #GUnixFDList.
488 * This method is only available on UNIX.
490 * This method will free @invocation, you cannot use it afterwards.
495 g_dbus_method_invocation_return_value_with_unix_fd_list (GDBusMethodInvocation *invocation,
496 GVariant *parameters,
497 GUnixFDList *fd_list)
499 g_dbus_method_invocation_return_value_internal (invocation, parameters, fd_list);
503 /* ---------------------------------------------------------------------------------------------------- */
506 * g_dbus_method_invocation_return_error:
507 * @invocation: (transfer full): A #GDBusMethodInvocation.
508 * @domain: A #GQuark for the #GError error domain.
509 * @code: The error code.
510 * @format: printf()-style format.
511 * @...: Parameters for @format.
513 * Finishes handling a D-Bus method call by returning an error.
515 * See g_dbus_error_encode_gerror() for details about what error name
516 * will be returned on the wire. In a nutshell, if the given error is
517 * registered using g_dbus_error_register_error() the name given
518 * during registration is used. Otherwise, a name of the form
519 * <literal>org.gtk.GDBus.UnmappedGError.Quark...</literal> is
520 * used. This provides transparent mapping of #GError between
521 * applications using GDBus.
523 * If you are writing an application intended to be portable,
524 * <emphasis>always</emphasis> register errors with g_dbus_error_register_error()
525 * or use g_dbus_method_invocation_return_dbus_error().
527 * This method will free @invocation, you cannot use it afterwards.
532 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
540 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
541 g_return_if_fail (format != NULL);
543 va_start (var_args, format);
544 g_dbus_method_invocation_return_error_valist (invocation,
553 * g_dbus_method_invocation_return_error_valist:
554 * @invocation: (transfer full): A #GDBusMethodInvocation.
555 * @domain: A #GQuark for the #GError error domain.
556 * @code: The error code.
557 * @format: printf()-style format.
558 * @var_args: #va_list of parameters for @format.
560 * Like g_dbus_method_invocation_return_error() but intended for
563 * This method will free @invocation, you cannot use it afterwards.
568 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
574 gchar *literal_message;
576 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
577 g_return_if_fail (format != NULL);
579 literal_message = g_strdup_vprintf (format, var_args);
580 g_dbus_method_invocation_return_error_literal (invocation,
584 g_free (literal_message);
588 * g_dbus_method_invocation_return_error_literal:
589 * @invocation: (transfer full): A #GDBusMethodInvocation.
590 * @domain: A #GQuark for the #GError error domain.
591 * @code: The error code.
592 * @message: The error message.
594 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
596 * This method will free @invocation, you cannot use it afterwards.
601 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
604 const gchar *message)
608 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
609 g_return_if_fail (message != NULL);
611 error = g_error_new_literal (domain, code, message);
612 g_dbus_method_invocation_return_gerror (invocation, error);
613 g_error_free (error);
617 * g_dbus_method_invocation_return_gerror:
618 * @invocation: (transfer full): A #GDBusMethodInvocation.
621 * Like g_dbus_method_invocation_return_error() but takes a #GError
622 * instead of the error domain, error code and message.
624 * This method will free @invocation, you cannot use it afterwards.
629 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
632 gchar *dbus_error_name;
634 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
635 g_return_if_fail (error != NULL);
637 dbus_error_name = g_dbus_error_encode_gerror (error);
639 g_dbus_method_invocation_return_dbus_error (invocation,
642 g_free (dbus_error_name);
646 * g_dbus_method_invocation_take_error: (skip)
647 * @invocation: (transfer full): A #GDBusMethodInvocation.
648 * @error: (transfer full): A #GError.
650 * Like g_dbus_method_invocation_return_gerror() but takes ownership
651 * of @error so the caller does not need to free it.
653 * This method will free @invocation, you cannot use it afterwards.
658 g_dbus_method_invocation_take_error (GDBusMethodInvocation *invocation,
661 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
662 g_return_if_fail (error != NULL);
663 g_dbus_method_invocation_return_gerror (invocation, error);
664 g_error_free (error);
668 * g_dbus_method_invocation_return_dbus_error:
669 * @invocation: (transfer full): A #GDBusMethodInvocation.
670 * @error_name: A valid D-Bus error name.
671 * @error_message: A valid D-Bus error message.
673 * Finishes handling a D-Bus method call by returning an error.
675 * This method will free @invocation, you cannot use it afterwards.
680 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
681 const gchar *error_name,
682 const gchar *error_message)
686 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
687 g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
688 g_return_if_fail (error_message != NULL);
690 if (G_UNLIKELY (_g_dbus_debug_return ()))
692 _g_dbus_debug_print_lock ();
693 g_print ("========================================================================\n"
694 "GDBus-debug:Return:\n"
695 " >>>> METHOD ERROR %s\n"
697 " in response to %s.%s()\n"
700 " reply-serial %d\n",
703 invocation->interface_name, invocation->method_name,
704 invocation->object_path,
706 g_dbus_message_get_serial (invocation->message));
707 _g_dbus_debug_print_unlock ();
710 reply = g_dbus_message_new_method_error_literal (invocation->message,
713 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
714 g_object_unref (reply);
716 g_object_unref (invocation);