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"
38 * SECTION:gdbusmethodinvocation
39 * @short_description: Object for handling remote calls
42 * Instances of the #GDBusMethodInvocation class are used when
43 * handling D-Bus method calls. It provides a way to asynchronously
44 * return results and errors.
46 * The normal way to obtain a #GDBusMethodInvocation object is to receive
47 * it as an argument to the handle_method_call() function in a
48 * #GDBusInterfaceVTable that was passed to g_dbus_connection_register_object().
51 typedef struct _GDBusMethodInvocationClass GDBusMethodInvocationClass;
54 * GDBusMethodInvocationClass:
56 * Class structure for #GDBusMethodInvocation.
60 struct _GDBusMethodInvocationClass
63 GObjectClass parent_class;
67 * GDBusMethodInvocation:
69 * The #GDBusMethodInvocation structure contains only private data and
70 * should only be accessed using the provided API.
74 struct _GDBusMethodInvocation
77 GObject parent_instance;
79 /* construct-only properties */
82 gchar *interface_name;
84 const GDBusMethodInfo *method_info;
85 GDBusConnection *connection;
86 GDBusMessage *message;
91 G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT);
94 g_dbus_method_invocation_finalize (GObject *object)
96 GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
98 g_free (invocation->sender);
99 g_free (invocation->object_path);
100 g_free (invocation->interface_name);
101 g_free (invocation->method_name);
102 g_object_unref (invocation->connection);
103 g_object_unref (invocation->message);
104 g_variant_unref (invocation->parameters);
106 G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
110 g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114 gobject_class->finalize = g_dbus_method_invocation_finalize;
118 g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
123 * g_dbus_method_invocation_get_sender:
124 * @invocation: A #GDBusMethodInvocation.
126 * Gets the bus name that invoked the method.
128 * Returns: A string. Do not free, it is owned by @invocation.
133 g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
135 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
136 return invocation->sender;
140 * g_dbus_method_invocation_get_object_path:
141 * @invocation: A #GDBusMethodInvocation.
143 * Gets the object path the method was invoked on.
145 * Returns: A string. Do not free, it is owned by @invocation.
150 g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
152 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
153 return invocation->object_path;
157 * g_dbus_method_invocation_get_interface_name:
158 * @invocation: A #GDBusMethodInvocation.
160 * Gets the name of the D-Bus interface the method was invoked on.
162 * Returns: A string. Do not free, it is owned by @invocation.
167 g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
169 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
170 return invocation->interface_name;
174 * g_dbus_method_invocation_get_method_info:
175 * @invocation: A #GDBusMethodInvocation.
177 * Gets information about the method call, if any.
179 * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
183 const GDBusMethodInfo *
184 g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
186 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
187 return invocation->method_info;
191 * g_dbus_method_invocation_get_method_name:
192 * @invocation: A #GDBusMethodInvocation.
194 * Gets the name of the method that was invoked.
196 * Returns: A string. Do not free, it is owned by @invocation.
201 g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
203 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
204 return invocation->method_name;
208 * g_dbus_method_invocation_get_connection:
209 * @invocation: A #GDBusMethodInvocation.
211 * Gets the #GDBusConnection the method was invoked on.
213 * Returns: (transfer none):A #GDBusConnection. Do not free, it is owned by @invocation.
218 g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
220 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
221 return invocation->connection;
225 * g_dbus_method_invocation_get_message:
226 * @invocation: A #GDBusMethodInvocation.
228 * Gets the #GDBusMessage for the method invocation. This is useful if
229 * you need to use low-level protocol features, such as UNIX file
230 * descriptor passing, that cannot be properly expressed in the
233 * See <xref linkend="gdbus-server"/> and <xref
234 * linkend="gdbus-unix-fd-client"/> for an example of how to use this
235 * low-level API to send and receive UNIX file descriptors.
237 * Returns: (transfer none): #GDBusMessage. Do not free, it is owned by @invocation.
242 g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
244 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
245 return invocation->message;
249 * g_dbus_method_invocation_get_parameters:
250 * @invocation: A #GDBusMethodInvocation.
252 * Gets the parameters of the method invocation. If there are no input
253 * parameters then this will return a GVariant with 0 children rather than NULL.
255 * Returns: (transfer none): A #GVariant tuple. Do not unref this because it is owned by @invocation.
260 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
262 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
263 return invocation->parameters;
267 * g_dbus_method_invocation_get_user_data: (skip)
268 * @invocation: A #GDBusMethodInvocation.
270 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
272 * Returns: A #gpointer.
277 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
279 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
280 return invocation->user_data;
284 * _g_dbus_method_invocation_new:
285 * @sender: The bus name that invoked the method or %NULL if @connection is not a bus connection.
286 * @object_path: The object path the method was invoked on.
287 * @interface_name: The name of the D-Bus interface the method was invoked on.
288 * @method_name: The name of the method that was invoked.
289 * @method_info: Information about the method call or %NULL.
290 * @connection: The #GDBusConnection the method was invoked on.
291 * @message: The D-Bus message as a #GDBusMessage.
292 * @parameters: The parameters as a #GVariant tuple.
293 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
295 * Creates a new #GDBusMethodInvocation object.
297 * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
301 GDBusMethodInvocation *
302 _g_dbus_method_invocation_new (const gchar *sender,
303 const gchar *object_path,
304 const gchar *interface_name,
305 const gchar *method_name,
306 const GDBusMethodInfo *method_info,
307 GDBusConnection *connection,
308 GDBusMessage *message,
309 GVariant *parameters,
312 GDBusMethodInvocation *invocation;
314 g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
315 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
316 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
317 g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
318 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
319 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
320 g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
322 invocation = G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION, NULL));
323 invocation->sender = g_strdup (sender);
324 invocation->object_path = g_strdup (object_path);
325 invocation->interface_name = g_strdup (interface_name);
326 invocation->method_name = g_strdup (method_name);
327 invocation->method_info = g_dbus_method_info_ref ((GDBusMethodInfo *)method_info);
328 invocation->connection = g_object_ref (connection);
329 invocation->message = g_object_ref (message);
330 invocation->parameters = g_variant_ref (parameters);
331 invocation->user_data = user_data;
336 /* ---------------------------------------------------------------------------------------------------- */
339 * g_dbus_method_invocation_return_value:
340 * @invocation: A #GDBusMethodInvocation.
341 * @parameters: A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
343 * Finishes handling a D-Bus method call by returning @parameters.
344 * If the @parameters GVariant is floating, it is consumed.
346 * It is an error if @parameters is not of the right format.
348 * This method will free @invocation, you cannot use it afterwards.
353 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
354 GVariant *parameters)
359 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
360 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
362 if (parameters == NULL)
363 parameters = g_variant_new_tuple (NULL, 0);
365 /* if we have introspection data, check that the signature of @parameters is correct */
366 if (invocation->method_info != NULL)
370 type = _g_dbus_compute_complete_signature (invocation->method_info->out_args);
372 if (!g_variant_is_of_type (parameters, type))
374 gchar *type_string = g_variant_type_dup_string (type);
376 g_warning (_("Type of return value is incorrect, got `%s', expected `%s'"),
377 g_variant_get_type_string (parameters), type_string);
378 g_variant_type_free (type);
379 g_free (type_string);
382 g_variant_type_free (type);
385 if (G_UNLIKELY (_g_dbus_debug_return ()))
387 _g_dbus_debug_print_lock ();
388 g_print ("========================================================================\n"
389 "GDBus-debug:Return:\n"
390 " >>>> METHOD RETURN\n"
391 " in response to %s.%s()\n"
394 " reply-serial %d\n",
395 invocation->interface_name, invocation->method_name,
396 invocation->object_path,
398 g_dbus_message_get_serial (invocation->message));
399 _g_dbus_debug_print_unlock ();
402 reply = g_dbus_message_new_method_reply (invocation->message);
403 g_dbus_message_set_body (reply, parameters);
405 if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error))
407 g_warning (_("Error sending message: %s"), error->message);
408 g_error_free (error);
410 g_object_unref (reply);
413 g_object_unref (invocation);
416 /* ---------------------------------------------------------------------------------------------------- */
419 * g_dbus_method_invocation_return_error:
420 * @invocation: A #GDBusMethodInvocation.
421 * @domain: A #GQuark for the #GError error domain.
422 * @code: The error code.
423 * @format: printf()-style format.
424 * @...: Parameters for @format.
426 * Finishes handling a D-Bus method call by returning an error.
428 * See g_dbus_error_encode_gerror() for details about what error name
429 * will be returned on the wire. In a nutshell, if the given error is
430 * registered using g_dbus_error_register_error() the name given
431 * during registration is used. Otherwise, a name of the form
432 * <literal>org.gtk.GDBus.UnmappedGError.Quark...</literal> is
433 * used. This provides transparent mapping of #GError between
434 * applications using GDBus.
436 * If you are writing an application intended to be portable,
437 * <emphasis>always</emphasis> register errors with g_dbus_error_register_error()
438 * or use g_dbus_method_invocation_return_dbus_error().
440 * This method will free @invocation, you cannot use it afterwards.
445 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
453 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
454 g_return_if_fail (format != NULL);
456 va_start (var_args, format);
457 g_dbus_method_invocation_return_error_valist (invocation,
466 * g_dbus_method_invocation_return_error_valist:
467 * @invocation: A #GDBusMethodInvocation.
468 * @domain: A #GQuark for the #GError error domain.
469 * @code: The error code.
470 * @format: printf()-style format.
471 * @var_args: #va_list of parameters for @format.
473 * Like g_dbus_method_invocation_return_error() but intended for
476 * This method will free @invocation, you cannot use it afterwards.
481 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
487 gchar *literal_message;
489 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
490 g_return_if_fail (format != NULL);
492 literal_message = g_strdup_vprintf (format, var_args);
493 g_dbus_method_invocation_return_error_literal (invocation,
497 g_free (literal_message);
501 * g_dbus_method_invocation_return_error_literal:
502 * @invocation: A #GDBusMethodInvocation.
503 * @domain: A #GQuark for the #GError error domain.
504 * @code: The error code.
505 * @message: The error message.
507 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
509 * This method will free @invocation, you cannot use it afterwards.
514 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
517 const gchar *message)
521 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
522 g_return_if_fail (message != NULL);
524 error = g_error_new_literal (domain, code, message);
525 g_dbus_method_invocation_return_gerror (invocation, error);
526 g_error_free (error);
530 * g_dbus_method_invocation_return_gerror:
531 * @invocation: A #GDBusMethodInvocation.
534 * Like g_dbus_method_invocation_return_error() but takes a #GError
535 * instead of the error domain, error code and message.
537 * This method will free @invocation, you cannot use it afterwards.
542 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
545 gchar *dbus_error_name;
547 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
548 g_return_if_fail (error != NULL);
550 dbus_error_name = g_dbus_error_encode_gerror (error);
552 g_dbus_method_invocation_return_dbus_error (invocation,
555 g_free (dbus_error_name);
559 * g_dbus_method_invocation_return_dbus_error:
560 * @invocation: A #GDBusMethodInvocation.
561 * @error_name: A valid D-Bus error name.
562 * @error_message: A valid D-Bus error message.
564 * Finishes handling a D-Bus method call by returning an error.
566 * This method will free @invocation, you cannot use it afterwards.
571 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
572 const gchar *error_name,
573 const gchar *error_message)
577 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
578 g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
579 g_return_if_fail (error_message != NULL);
581 if (G_UNLIKELY (_g_dbus_debug_return ()))
583 _g_dbus_debug_print_lock ();
584 g_print ("========================================================================\n"
585 "GDBus-debug:Return:\n"
586 " >>>> METHOD ERROR %s\n"
588 " in response to %s.%s()\n"
591 " reply-serial %d\n",
594 invocation->interface_name, invocation->method_name,
595 invocation->object_path,
597 g_dbus_message_get_serial (invocation->message));
598 _g_dbus_debug_print_unlock ();
601 reply = g_dbus_message_new_method_error_literal (invocation->message,
604 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
605 g_object_unref (reply);
607 g_object_unref (invocation);