Move markup collect tests to the test framework
[glib.git] / gio / gdbusmethodinvocation.c
blobaea0b6d66c619c6d4b89d81fcef1a437e3a272ea
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>
23 #include "config.h"
25 #include <stdlib.h>
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"
35 #include "glibintl.h"
37 /**
38 * SECTION:gdbusmethodinvocation
39 * @short_description: Object for handling remote calls
40 * @include: gio/gio.h
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;
53 /**
54 * GDBusMethodInvocationClass:
56 * Class structure for #GDBusMethodInvocation.
58 * Since: 2.26
60 struct _GDBusMethodInvocationClass
62 /*< private >*/
63 GObjectClass parent_class;
66 /**
67 * GDBusMethodInvocation:
69 * The #GDBusMethodInvocation structure contains only private data and
70 * should only be accessed using the provided API.
72 * Since: 2.26
74 struct _GDBusMethodInvocation
76 /*< private >*/
77 GObject parent_instance;
79 /* construct-only properties */
80 gchar *sender;
81 gchar *object_path;
82 gchar *interface_name;
83 gchar *method_name;
84 const GDBusMethodInfo *method_info;
85 GDBusConnection *connection;
86 GDBusMessage *message;
87 GVariant *parameters;
88 gpointer user_data;
91 G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT);
93 static void
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);
109 static void
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;
117 static void
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.
130 * Since: 2.26
132 const gchar *
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.
147 * Since: 2.26
149 const gchar *
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.
164 * Since: 2.26
166 const gchar *
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.
181 * Since: 2.26
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.
198 * Since: 2.26
200 const gchar *
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: A #GDBusConnection. Do not free, it is owned by @invocation.
215 * Since: 2.26
217 GDBusConnection *
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
231 * #GVariant API.
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: A #GDBusMessage. Do not free, it is owned by @invocation.
239 * Since: 2.26
241 GDBusMessage *
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.
256 * Since: 2.26
258 GVariant *
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:
267 * @invocation: A #GDBusMethodInvocation.
269 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
271 * Returns: A #gpointer.
273 * Since: 2.26
275 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().
298 * Since: 2.26
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,
309 gpointer user_data)
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;
332 return invocation;
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.
349 * Since: 2.26
351 void
352 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
353 GVariant *parameters)
355 GDBusMessage *reply;
356 GError *error;
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)
367 GVariantType *type;
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);
379 goto out;
381 g_variant_type_free (type);
384 reply = g_dbus_message_new_method_reply (invocation->message);
385 g_dbus_message_set_body (reply, parameters);
386 error = NULL;
387 if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error))
389 g_warning (_("Error sending message: %s"), error->message);
390 g_error_free (error);
392 g_object_unref (reply);
394 out:
395 g_object_unref (invocation);
398 /* ---------------------------------------------------------------------------------------------------- */
401 * g_dbus_method_invocation_return_error:
402 * @invocation: A #GDBusMethodInvocation.
403 * @domain: A #GQuark for the #GError error domain.
404 * @code: The error code.
405 * @format: printf()-style format.
406 * @...: Parameters for @format.
408 * Finishes handling a D-Bus method call by returning an error.
410 * See g_dbus_error_encode_gerror() for details about what error name
411 * will be returned on the wire. In a nutshell, if the given error is
412 * registered using g_dbus_error_register_error() the name given
413 * during registration is used. Otherwise, a name of the form
414 * <literal>org.gtk.GDBus.UnmappedGError.Quark...</literal> is
415 * used. This provides transparent mapping of #GError between
416 * applications using GDBus.
418 * If you are writing an application intended to be portable,
419 * <emphasis>always</emphasis> register errors with g_dbus_error_register_error()
420 * or use g_dbus_method_invocation_return_dbus_error().
422 * This method will free @invocation, you cannot use it afterwards.
424 * Since: 2.26
426 void
427 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
428 GQuark domain,
429 gint code,
430 const gchar *format,
431 ...)
433 va_list var_args;
435 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
436 g_return_if_fail (format != NULL);
438 va_start (var_args, format);
439 g_dbus_method_invocation_return_error_valist (invocation,
440 domain,
441 code,
442 format,
443 var_args);
444 va_end (var_args);
448 * g_dbus_method_invocation_return_error_valist:
449 * @invocation: A #GDBusMethodInvocation.
450 * @domain: A #GQuark for the #GError error domain.
451 * @code: The error code.
452 * @format: printf()-style format.
453 * @var_args: #va_list of parameters for @format.
455 * Like g_dbus_method_invocation_return_error() but intended for
456 * language bindings.
458 * This method will free @invocation, you cannot use it afterwards.
460 * Since: 2.26
462 void
463 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
464 GQuark domain,
465 gint code,
466 const gchar *format,
467 va_list var_args)
469 gchar *literal_message;
471 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
472 g_return_if_fail (format != NULL);
474 literal_message = g_strdup_vprintf (format, var_args);
475 g_dbus_method_invocation_return_error_literal (invocation,
476 domain,
477 code,
478 literal_message);
479 g_free (literal_message);
483 * g_dbus_method_invocation_return_error_literal:
484 * @invocation: A #GDBusMethodInvocation.
485 * @domain: A #GQuark for the #GError error domain.
486 * @code: The error code.
487 * @message: The error message.
489 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
491 * This method will free @invocation, you cannot use it afterwards.
493 * Since: 2.26
495 void
496 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
497 GQuark domain,
498 gint code,
499 const gchar *message)
501 GError *error;
503 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
504 g_return_if_fail (message != NULL);
506 error = g_error_new_literal (domain, code, message);
507 g_dbus_method_invocation_return_gerror (invocation, error);
508 g_error_free (error);
512 * g_dbus_method_invocation_return_gerror:
513 * @invocation: A #GDBusMethodInvocation.
514 * @error: A #GError.
516 * Like g_dbus_method_invocation_return_error() but takes a #GError
517 * instead of the error domain, error code and message.
519 * This method will free @invocation, you cannot use it afterwards.
521 * Since: 2.26
523 void
524 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
525 const GError *error)
527 gchar *dbus_error_name;
529 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
530 g_return_if_fail (error != NULL);
532 dbus_error_name = g_dbus_error_encode_gerror (error);
534 g_dbus_method_invocation_return_dbus_error (invocation,
535 dbus_error_name,
536 error->message);
537 g_free (dbus_error_name);
541 * g_dbus_method_invocation_return_dbus_error:
542 * @invocation: A #GDBusMethodInvocation.
543 * @error_name: A valid D-Bus error name.
544 * @error_message: A valid D-Bus error message.
546 * Finishes handling a D-Bus method call by returning an error.
548 * This method will free @invocation, you cannot use it afterwards.
550 * Since: 2.26
552 void
553 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
554 const gchar *error_name,
555 const gchar *error_message)
557 GDBusMessage *reply;
559 g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
560 g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
561 g_return_if_fail (error_message != NULL);
563 reply = g_dbus_message_new_method_error_literal (invocation->message,
564 error_name,
565 error_message);
566 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
567 g_object_unref (reply);
569 g_object_unref (invocation);