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.
254 * Returns: A #GVariant. Do not free, it is owned by @invocation.
259 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
261 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
262 return invocation->parameters;
266 * g_dbus_method_invocation_get_user_data: (skip)
267 * @invocation: A #GDBusMethodInvocation.
269 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
271 * Returns: A #gpointer.
276 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
278 g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
279 return invocation->user_data;
283 * _g_dbus_method_invocation_new:
284 * @sender: The bus name that invoked the method or %NULL if @connection is not a bus connection.
285 * @object_path: The object path the method was invoked on.
286 * @interface_name: The name of the D-Bus interface the method was invoked on.
287 * @method_name: The name of the method that was invoked.
288 * @method_info: Information about the method call or %NULL.
289 * @connection: The #GDBusConnection the method was invoked on.
290 * @message: The D-Bus message as a #GDBusMessage.
291 * @parameters: The parameters as a #GVariant tuple.
292 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
294 * Creates a new #GDBusMethodInvocation object.
296 * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
300 GDBusMethodInvocation *
301 _g_dbus_method_invocation_new (const gchar *sender,
302 const gchar *object_path,
303 const gchar *interface_name,
304 const gchar *method_name,
305 const GDBusMethodInfo *method_info,
306 GDBusConnection *connection,
307 GDBusMessage *message,
308 GVariant *parameters,
311 GDBusMethodInvocation *invocation;
313 g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
314 g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
315 g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
316 g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
317 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
318 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
319 g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
321 invocation = G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION, NULL));
322 invocation->sender = g_strdup (sender);
323 invocation->object_path = g_strdup (object_path);
324 invocation->interface_name = g_strdup (interface_name);
325 invocation->method_name = g_strdup (method_name);
326 invocation->method_info = g_dbus_method_info_ref ((GDBusMethodInfo *)method_info);
327 invocation->connection = g_object_ref (connection);
328 invocation->message = g_object_ref (message);
329 invocation->parameters = g_variant_ref (parameters);
330 invocation->user_data = user_data;
335 /* ---------------------------------------------------------------------------------------------------- */
338 * g_dbus_method_invocation_return_value:
339 * @invocation: A #GDBusMethodInvocation.
340 * @parameters: A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
342 * Finishes handling a D-Bus method call by returning @parameters.
343 * If the @parameters GVariant is floating, it is consumed.
345 * It is an error if @parameters is not of the right format.
347 * This method will free @invocation, you cannot use it afterwards.
352 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
353 GVariant *parameters)
358 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
359 g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
361 if (parameters == NULL)
362 parameters = g_variant_new_tuple (NULL, 0);
364 /* if we have introspection data, check that the signature of @parameters is correct */
365 if (invocation->method_info != NULL)
369 type = _g_dbus_compute_complete_signature (invocation->method_info->out_args);
371 if (!g_variant_is_of_type (parameters, type))
373 gchar *type_string = g_variant_type_dup_string (type);
375 g_warning (_("Type of return value is incorrect, got `%s', expected `%s'"),
376 g_variant_get_type_string (parameters), type_string);
377 g_variant_type_free (type);
378 g_free (type_string);
381 g_variant_type_free (type);
384 if (G_UNLIKELY (_g_dbus_debug_return ()))
386 _g_dbus_debug_print_lock ();
387 g_print ("========================================================================\n"
388 "GDBus-debug:Return:\n"
389 " >>>> METHOD RETURN\n"
390 " in response to %s.%s()\n"
393 " reply-serial %d\n",
394 invocation->interface_name, invocation->method_name,
395 invocation->object_path,
397 g_dbus_message_get_serial (invocation->message));
398 _g_dbus_debug_print_unlock ();
401 reply = g_dbus_message_new_method_reply (invocation->message);
402 g_dbus_message_set_body (reply, parameters);
404 if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error))
406 g_warning (_("Error sending message: %s"), error->message);
407 g_error_free (error);
409 g_object_unref (reply);
412 g_object_unref (invocation);
415 /* ---------------------------------------------------------------------------------------------------- */
418 * g_dbus_method_invocation_return_error:
419 * @invocation: A #GDBusMethodInvocation.
420 * @domain: A #GQuark for the #GError error domain.
421 * @code: The error code.
422 * @format: printf()-style format.
423 * @...: Parameters for @format.
425 * Finishes handling a D-Bus method call by returning an error.
427 * See g_dbus_error_encode_gerror() for details about what error name
428 * will be returned on the wire. In a nutshell, if the given error is
429 * registered using g_dbus_error_register_error() the name given
430 * during registration is used. Otherwise, a name of the form
431 * <literal>org.gtk.GDBus.UnmappedGError.Quark...</literal> is
432 * used. This provides transparent mapping of #GError between
433 * applications using GDBus.
435 * If you are writing an application intended to be portable,
436 * <emphasis>always</emphasis> register errors with g_dbus_error_register_error()
437 * or use g_dbus_method_invocation_return_dbus_error().
439 * This method will free @invocation, you cannot use it afterwards.
444 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
452 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
453 g_return_if_fail (format != NULL);
455 va_start (var_args, format);
456 g_dbus_method_invocation_return_error_valist (invocation,
465 * g_dbus_method_invocation_return_error_valist:
466 * @invocation: A #GDBusMethodInvocation.
467 * @domain: A #GQuark for the #GError error domain.
468 * @code: The error code.
469 * @format: printf()-style format.
470 * @var_args: #va_list of parameters for @format.
472 * Like g_dbus_method_invocation_return_error() but intended for
475 * This method will free @invocation, you cannot use it afterwards.
480 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
486 gchar *literal_message;
488 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
489 g_return_if_fail (format != NULL);
491 literal_message = g_strdup_vprintf (format, var_args);
492 g_dbus_method_invocation_return_error_literal (invocation,
496 g_free (literal_message);
500 * g_dbus_method_invocation_return_error_literal:
501 * @invocation: A #GDBusMethodInvocation.
502 * @domain: A #GQuark for the #GError error domain.
503 * @code: The error code.
504 * @message: The error message.
506 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
508 * This method will free @invocation, you cannot use it afterwards.
513 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
516 const gchar *message)
520 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
521 g_return_if_fail (message != NULL);
523 error = g_error_new_literal (domain, code, message);
524 g_dbus_method_invocation_return_gerror (invocation, error);
525 g_error_free (error);
529 * g_dbus_method_invocation_return_gerror:
530 * @invocation: A #GDBusMethodInvocation.
533 * Like g_dbus_method_invocation_return_error() but takes a #GError
534 * instead of the error domain, error code and message.
536 * This method will free @invocation, you cannot use it afterwards.
541 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
544 gchar *dbus_error_name;
546 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
547 g_return_if_fail (error != NULL);
549 dbus_error_name = g_dbus_error_encode_gerror (error);
551 g_dbus_method_invocation_return_dbus_error (invocation,
554 g_free (dbus_error_name);
558 * g_dbus_method_invocation_return_dbus_error:
559 * @invocation: A #GDBusMethodInvocation.
560 * @error_name: A valid D-Bus error name.
561 * @error_message: A valid D-Bus error message.
563 * Finishes handling a D-Bus method call by returning an error.
565 * This method will free @invocation, you cannot use it afterwards.
570 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
571 const gchar *error_name,
572 const gchar *error_message)
576 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
577 g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
578 g_return_if_fail (error_message != NULL);
580 if (G_UNLIKELY (_g_dbus_debug_return ()))
582 _g_dbus_debug_print_lock ();
583 g_print ("========================================================================\n"
584 "GDBus-debug:Return:\n"
585 " >>>> METHOD ERROR %s\n"
587 " in response to %s.%s()\n"
590 " reply-serial %d\n",
593 invocation->interface_name, invocation->method_name,
594 invocation->object_path,
596 g_dbus_message_get_serial (invocation->message));
597 _g_dbus_debug_print_unlock ();
600 reply = g_dbus_message_new_method_error_literal (invocation->message,
603 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
604 g_object_unref (reply);
606 g_object_unref (invocation);