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 const GDBusMethodInfo *method_info;
89 GDBusConnection *connection;
90 GDBusMessage *message;
95 G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT);
98 g_dbus_method_invocation_finalize (GObject *object)
100 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
102 g_free (invocation->sender);
103 g_free (invocation->object_path);
104 g_free (invocation->interface_name);
105 g_free (invocation->method_name);
106 g_object_unref (invocation->connection);
107 g_object_unref (invocation->message);
108 g_variant_unref (invocation->parameters);
110 G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
114 g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
116 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118 gobject_class->finalize = g_dbus_method_invocation_finalize;
122 g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
127 * g_dbus_method_invocation_get_sender:
128 * @invocation: A #GDBusMethodInvocation.
130 * Gets the bus name that invoked the method.
132 * Returns: A string. Do not free, it is owned by @invocation.
137 g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
139 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
140 return invocation->sender;
144 * g_dbus_method_invocation_get_object_path:
145 * @invocation: A #GDBusMethodInvocation.
147 * Gets the object path the method was invoked on.
149 * Returns: A string. Do not free, it is owned by @invocation.
154 g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
156 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
157 return invocation->object_path;
161 * g_dbus_method_invocation_get_interface_name:
162 * @invocation: A #GDBusMethodInvocation.
164 * Gets the name of the D-Bus interface the method was invoked on.
166 * Returns: A string. Do not free, it is owned by @invocation.
171 g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
173 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
174 return invocation->interface_name;
178 * g_dbus_method_invocation_get_method_info:
179 * @invocation: A #GDBusMethodInvocation.
181 * Gets information about the method call, if any.
183 * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
187 const GDBusMethodInfo *
188 g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
190 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
191 return invocation->method_info;
195 * g_dbus_method_invocation_get_method_name:
196 * @invocation: A #GDBusMethodInvocation.
198 * Gets the name of the method that was invoked.
200 * Returns: A string. Do not free, it is owned by @invocation.
205 g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
207 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
208 return invocation->method_name;
212 * g_dbus_method_invocation_get_connection:
213 * @invocation: A #GDBusMethodInvocation.
215 * Gets the #GDBusConnection the method was invoked on.
217 * Returns: (transfer none):A #GDBusConnection. Do not free, it is owned by @invocation.
222 g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
224 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
225 return invocation->connection;
229 * g_dbus_method_invocation_get_message:
230 * @invocation: A #GDBusMethodInvocation.
232 * Gets the #GDBusMessage for the method invocation. This is useful if
233 * you need to use low-level protocol features, such as UNIX file
234 * descriptor passing, that cannot be properly expressed in the
237 * See <xref linkend="gdbus-server"/> and <xref
238 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
239 * low-level API to send and receive UNIX file descriptors.
241 * Returns: (transfer none): #GDBusMessage. Do not free, it is owned by @invocation.
246 g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
248 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
249 return invocation->message;
253 * g_dbus_method_invocation_get_parameters:
254 * @invocation: A #GDBusMethodInvocation.
256 * Gets the parameters of the method invocation. If there are no input
257 * parameters then this will return a GVariant with 0 children rather than NULL.
259 * Returns: (transfer none): A #GVariant tuple. Do not unref this because it is owned by @invocation.
264 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
266 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
267 return invocation->parameters;
271 * g_dbus_method_invocation_get_user_data: (skip)
272 * @invocation: A #GDBusMethodInvocation.
274 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
276 * Returns: A #gpointer.
281 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
283 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
284 return invocation->user_data;
288 * _g_dbus_method_invocation_new:
289 * @sender: The bus name that invoked the method or %NULL if @connection is not a bus connection.
290 * @object_path: The object path the method was invoked on.
291 * @interface_name: The name of the D-Bus interface the method was invoked on.
292 * @method_name: The name of the method that was invoked.
293 * @method_info: Information about the method call or %NULL.
294 * @connection: The #GDBusConnection the method was invoked on.
295 * @message: The D-Bus message as a #GDBusMessage.
296 * @parameters: The parameters as a #GVariant tuple.
297 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
299 * Creates a new #GDBusMethodInvocation object.
301 * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
305 GDBusMethodInvocation *
306 _g_dbus_method_invocation_new (const gchar *sender,
307 const gchar *object_path,
308 const gchar *interface_name,
309 const gchar *method_name,
310 const GDBusMethodInfo *method_info,
311 GDBusConnection *connection,
312 GDBusMessage *message,
313 GVariant *parameters,
316 GDBusMethodInvocation *invocation;
318 g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
319 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
320 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
321 g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
322 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
323 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
324 g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
326 invocation = G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION, NULL));
327 invocation->sender = g_strdup (sender);
328 invocation->object_path = g_strdup (object_path);
329 invocation->interface_name = g_strdup (interface_name);
330 invocation->method_name = g_strdup (method_name);
331 invocation->method_info = g_dbus_method_info_ref ((GDBusMethodInfo *)method_info);
332 invocation->connection = g_object_ref (connection);
333 invocation->message = g_object_ref (message);
334 invocation->parameters = g_variant_ref (parameters);
335 invocation->user_data = user_data;
340 /* ---------------------------------------------------------------------------------------------------- */
343 g_dbus_method_invocation_return_value_internal (GDBusMethodInvocation *invocation,
344 GVariant *parameters,
345 GUnixFDList *fd_list)
350 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
351 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
353 if (parameters == NULL)
354 parameters = g_variant_new_tuple (NULL, 0);
356 /* if we have introspection data, check that the signature of @parameters is correct */
357 if (invocation->method_info != NULL)
361 type = _g_dbus_compute_complete_signature (invocation->method_info->out_args);
363 if (!g_variant_is_of_type (parameters, type))
365 gchar *type_string = g_variant_type_dup_string (type);
367 g_warning ("Type of return value is incorrect, got `%s', expected `%s'",
368 g_variant_get_type_string (parameters), type_string);
369 g_variant_type_free (type);
370 g_free (type_string);
373 g_variant_type_free (type);
376 if (G_UNLIKELY (_g_dbus_debug_return ()))
378 _g_dbus_debug_print_lock ();
379 g_print ("========================================================================\n"
380 "GDBus-debug:Return:\n"
381 " >>>> METHOD RETURN\n"
382 " in response to %s.%s()\n"
385 " reply-serial %d\n",
386 invocation->interface_name, invocation->method_name,
387 invocation->object_path,
389 g_dbus_message_get_serial (invocation->message));
390 _g_dbus_debug_print_unlock ();
393 reply = g_dbus_message_new_method_reply (invocation->message);
394 g_dbus_message_set_body (reply, parameters);
398 g_dbus_message_set_unix_fd_list (reply, fd_list);
402 if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error))
404 g_warning ("Error sending message: %s", error->message);
405 g_error_free (error);
407 g_object_unref (reply);
410 g_object_unref (invocation);
414 * g_dbus_method_invocation_return_value:
415 * @invocation: (transfer full): A #GDBusMethodInvocation.
416 * @parameters: (allow-none): A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
418 * Finishes handling a D-Bus method call by returning @parameters.
419 * If the @parameters GVariant is floating, it is consumed.
421 * It is an error if @parameters is not of the right format.
423 * This method will free @invocation, you cannot use it afterwards.
428 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
429 GVariant *parameters)
431 g_dbus_method_invocation_return_value_internal (invocation, parameters, NULL);
436 * g_dbus_method_invocation_return_value_with_unix_fd_list:
437 * @invocation: (transfer full): A #GDBusMethodInvocation.
438 * @parameters: (allow-none): A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
439 * @fd_list: (allow-none): A #GUnixFDList or %NULL.
441 * Like g_dbus_method_invocation_return_value() but also takes a #GUnixFDList.
443 * This method is only available on UNIX.
445 * This method will free @invocation, you cannot use it afterwards.
450 g_dbus_method_invocation_return_value_with_unix_fd_list (GDBusMethodInvocation *invocation,
451 GVariant *parameters,
452 GUnixFDList *fd_list)
454 g_dbus_method_invocation_return_value_internal (invocation, parameters, fd_list);
458 /* ---------------------------------------------------------------------------------------------------- */
461 * g_dbus_method_invocation_return_error:
462 * @invocation: (transfer full): A #GDBusMethodInvocation.
463 * @domain: A #GQuark for the #GError error domain.
464 * @code: The error code.
465 * @format: printf()-style format.
466 * @...: Parameters for @format.
468 * Finishes handling a D-Bus method call by returning an error.
470 * See g_dbus_error_encode_gerror() for details about what error name
471 * will be returned on the wire. In a nutshell, if the given error is
472 * registered using g_dbus_error_register_error() the name given
473 * during registration is used. Otherwise, a name of the form
474 * <literal>org.gtk.GDBus.UnmappedGError.Quark...</literal> is
475 * used. This provides transparent mapping of #GError between
476 * applications using GDBus.
478 * If you are writing an application intended to be portable,
479 * <emphasis>always</emphasis> register errors with g_dbus_error_register_error()
480 * or use g_dbus_method_invocation_return_dbus_error().
482 * This method will free @invocation, you cannot use it afterwards.
487 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
495 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
496 g_return_if_fail (format != NULL);
498 va_start (var_args, format);
499 g_dbus_method_invocation_return_error_valist (invocation,
508 * g_dbus_method_invocation_return_error_valist:
509 * @invocation: (transfer full): A #GDBusMethodInvocation.
510 * @domain: A #GQuark for the #GError error domain.
511 * @code: The error code.
512 * @format: printf()-style format.
513 * @var_args: #va_list of parameters for @format.
515 * Like g_dbus_method_invocation_return_error() but intended for
518 * This method will free @invocation, you cannot use it afterwards.
523 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
529 gchar *literal_message;
531 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
532 g_return_if_fail (format != NULL);
534 literal_message = g_strdup_vprintf (format, var_args);
535 g_dbus_method_invocation_return_error_literal (invocation,
539 g_free (literal_message);
543 * g_dbus_method_invocation_return_error_literal:
544 * @invocation: (transfer full): A #GDBusMethodInvocation.
545 * @domain: A #GQuark for the #GError error domain.
546 * @code: The error code.
547 * @message: The error message.
549 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
551 * This method will free @invocation, you cannot use it afterwards.
556 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
559 const gchar *message)
563 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
564 g_return_if_fail (message != NULL);
566 error = g_error_new_literal (domain, code, message);
567 g_dbus_method_invocation_return_gerror (invocation, error);
568 g_error_free (error);
572 * g_dbus_method_invocation_return_gerror:
573 * @invocation: (transfer full): A #GDBusMethodInvocation.
576 * Like g_dbus_method_invocation_return_error() but takes a #GError
577 * instead of the error domain, error code and message.
579 * This method will free @invocation, you cannot use it afterwards.
584 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
587 gchar *dbus_error_name;
589 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
590 g_return_if_fail (error != NULL);
592 dbus_error_name = g_dbus_error_encode_gerror (error);
594 g_dbus_method_invocation_return_dbus_error (invocation,
597 g_free (dbus_error_name);
601 * g_dbus_method_invocation_take_error: (skip)
602 * @invocation: (transfer full): A #GDBusMethodInvocation.
603 * @error: (transfer full): A #GError.
605 * Like g_dbus_method_invocation_return_gerror() but takes ownership
606 * of @error so the caller does not need to free it.
608 * This method will free @invocation, you cannot use it afterwards.
613 g_dbus_method_invocation_take_error (GDBusMethodInvocation *invocation,
616 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
617 g_return_if_fail (error != NULL);
618 g_dbus_method_invocation_return_gerror (invocation, error);
619 g_error_free (error);
623 * g_dbus_method_invocation_return_dbus_error:
624 * @invocation: (transfer full): A #GDBusMethodInvocation.
625 * @error_name: A valid D-Bus error name.
626 * @error_message: A valid D-Bus error message.
628 * Finishes handling a D-Bus method call by returning an error.
630 * This method will free @invocation, you cannot use it afterwards.
635 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
636 const gchar *error_name,
637 const gchar *error_message)
641 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
642 g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
643 g_return_if_fail (error_message != NULL);
645 if (G_UNLIKELY (_g_dbus_debug_return ()))
647 _g_dbus_debug_print_lock ();
648 g_print ("========================================================================\n"
649 "GDBus-debug:Return:\n"
650 " >>>> METHOD ERROR %s\n"
652 " in response to %s.%s()\n"
655 " reply-serial %d\n",
658 invocation->interface_name, invocation->method_name,
659 invocation->object_path,
661 g_dbus_message_get_serial (invocation->message));
662 _g_dbus_debug_print_unlock ();
665 reply = g_dbus_message_new_method_error_literal (invocation->message,
668 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
669 g_object_unref (reply);
671 g_object_unref (invocation);