Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / gdbusmethodinvocation.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26
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"
34
35 #ifdef G_OS_UNIX
36 #include "gunixfdlist.h"
37 #endif
38
39 #include "glibintl.h"
40
41 /**
42  * SECTION:gdbusmethodinvocation
43  * @short_description: Object for handling remote calls
44  * @include: gio/gio.h
45  *
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.
49  *
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().
53  */
54
55 typedef struct _GDBusMethodInvocationClass GDBusMethodInvocationClass;
56
57 /**
58  * GDBusMethodInvocationClass:
59  *
60  * Class structure for #GDBusMethodInvocation.
61  *
62  * Since: 2.26
63  */
64 struct _GDBusMethodInvocationClass
65 {
66   /*< private >*/
67   GObjectClass parent_class;
68 };
69
70 /**
71  * GDBusMethodInvocation:
72  *
73  * The #GDBusMethodInvocation structure contains only private data and
74  * should only be accessed using the provided API.
75  *
76  * Since: 2.26
77  */
78 struct _GDBusMethodInvocation
79 {
80   /*< private >*/
81   GObject parent_instance;
82
83   /* construct-only properties */
84   gchar           *sender;
85   gchar           *object_path;
86   gchar           *interface_name;
87   gchar           *method_name;
88   const GDBusMethodInfo *method_info;
89   GDBusConnection *connection;
90   GDBusMessage    *message;
91   GVariant        *parameters;
92   gpointer         user_data;
93 };
94
95 G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT);
96
97 static void
98 g_dbus_method_invocation_finalize (GObject *object)
99 {
100   GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
101
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);
109
110   G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
111 }
112
113 static void
114 g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
115 {
116   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
117
118   gobject_class->finalize = g_dbus_method_invocation_finalize;
119 }
120
121 static void
122 g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
123 {
124 }
125
126 /**
127  * g_dbus_method_invocation_get_sender:
128  * @invocation: A #GDBusMethodInvocation.
129  *
130  * Gets the bus name that invoked the method.
131  *
132  * Returns: A string. Do not free, it is owned by @invocation.
133  *
134  * Since: 2.26
135  */
136 const gchar *
137 g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
138 {
139   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
140   return invocation->sender;
141 }
142
143 /**
144  * g_dbus_method_invocation_get_object_path:
145  * @invocation: A #GDBusMethodInvocation.
146  *
147  * Gets the object path the method was invoked on.
148  *
149  * Returns: A string. Do not free, it is owned by @invocation.
150  *
151  * Since: 2.26
152  */
153 const gchar *
154 g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
155 {
156   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
157   return invocation->object_path;
158 }
159
160 /**
161  * g_dbus_method_invocation_get_interface_name:
162  * @invocation: A #GDBusMethodInvocation.
163  *
164  * Gets the name of the D-Bus interface the method was invoked on.
165  *
166  * Returns: A string. Do not free, it is owned by @invocation.
167  *
168  * Since: 2.26
169  */
170 const gchar *
171 g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
172 {
173   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
174   return invocation->interface_name;
175 }
176
177 /**
178  * g_dbus_method_invocation_get_method_info:
179  * @invocation: A #GDBusMethodInvocation.
180  *
181  * Gets information about the method call, if any.
182  *
183  * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
184  *
185  * Since: 2.26
186  */
187 const GDBusMethodInfo *
188 g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
189 {
190   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
191   return invocation->method_info;
192 }
193
194 /**
195  * g_dbus_method_invocation_get_method_name:
196  * @invocation: A #GDBusMethodInvocation.
197  *
198  * Gets the name of the method that was invoked.
199  *
200  * Returns: A string. Do not free, it is owned by @invocation.
201  *
202  * Since: 2.26
203  */
204 const gchar *
205 g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
206 {
207   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
208   return invocation->method_name;
209 }
210
211 /**
212  * g_dbus_method_invocation_get_connection:
213  * @invocation: A #GDBusMethodInvocation.
214  *
215  * Gets the #GDBusConnection the method was invoked on.
216  *
217  * Returns: (transfer none):A #GDBusConnection. Do not free, it is owned by @invocation.
218  *
219  * Since: 2.26
220  */
221 GDBusConnection *
222 g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
223 {
224   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
225   return invocation->connection;
226 }
227
228 /**
229  * g_dbus_method_invocation_get_message:
230  * @invocation: A #GDBusMethodInvocation.
231  *
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
235  * #GVariant API.
236  *
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.
240  *
241  * Returns: (transfer none): #GDBusMessage. Do not free, it is owned by @invocation.
242  *
243  * Since: 2.26
244  */
245 GDBusMessage *
246 g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
247 {
248   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
249   return invocation->message;
250 }
251
252 /**
253  * g_dbus_method_invocation_get_parameters:
254  * @invocation: A #GDBusMethodInvocation.
255  *
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.
258  *
259  * Returns: (transfer none): A #GVariant tuple. Do not unref this because it is owned by @invocation.
260  *
261  * Since: 2.26
262  */
263 GVariant *
264 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
265 {
266   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
267   return invocation->parameters;
268 }
269
270 /**
271  * g_dbus_method_invocation_get_user_data: (skip)
272  * @invocation: A #GDBusMethodInvocation.
273  *
274  * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
275  *
276  * Returns: A #gpointer.
277  *
278  * Since: 2.26
279  */
280 gpointer
281 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
282 {
283   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
284   return invocation->user_data;
285 }
286
287 /* < internal >
288  * _g_dbus_method_invocation_new:
289  * @sender: (allow-none): 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: (allow-none): 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().
298  *
299  * Creates a new #GDBusMethodInvocation object.
300  *
301  * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
302  *
303  * Since: 2.26
304  */
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,
314                                gpointer               user_data)
315 {
316   GDBusMethodInvocation *invocation;
317
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);
325
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;
336
337   return invocation;
338 }
339
340 /* ---------------------------------------------------------------------------------------------------- */
341
342 static void
343 g_dbus_method_invocation_return_value_internal (GDBusMethodInvocation *invocation,
344                                                 GVariant              *parameters,
345                                                 GUnixFDList           *fd_list)
346 {
347   GDBusMessage *reply;
348   GError *error;
349
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));
352
353   if (parameters == NULL)
354     parameters = g_variant_new_tuple (NULL, 0);
355
356   /* if we have introspection data, check that the signature of @parameters is correct */
357   if (invocation->method_info != NULL)
358     {
359       GVariantType *type;
360
361       type = _g_dbus_compute_complete_signature (invocation->method_info->out_args);
362
363       if (!g_variant_is_of_type (parameters, type))
364         {
365           gchar *type_string = g_variant_type_dup_string (type);
366
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);
371           goto out;
372         }
373       g_variant_type_free (type);
374     }
375
376   if (G_UNLIKELY (_g_dbus_debug_return ()))
377     {
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"
383                "      on object %s\n"
384                "      to name %s\n"
385                "      reply-serial %d\n",
386                invocation->interface_name, invocation->method_name,
387                invocation->object_path,
388                invocation->sender,
389                g_dbus_message_get_serial (invocation->message));
390       _g_dbus_debug_print_unlock ();
391     }
392
393   reply = g_dbus_message_new_method_reply (invocation->message);
394   g_dbus_message_set_body (reply, parameters);
395
396 #ifdef G_OS_UNIX
397   if (fd_list != NULL)
398     g_dbus_message_set_unix_fd_list (reply, fd_list);
399 #endif
400
401   error = NULL;
402   if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error))
403     {
404       g_warning ("Error sending message: %s", error->message);
405       g_error_free (error);
406     }
407   g_object_unref (reply);
408
409  out:
410   g_object_unref (invocation);
411 }
412
413 /**
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.
417  *
418  * Finishes handling a D-Bus method call by returning @parameters.
419  * If the @parameters GVariant is floating, it is consumed.
420  *
421  * It is an error if @parameters is not of the right format.
422  *
423  * This method will free @invocation, you cannot use it afterwards.
424  *
425  * Since: 2.26
426  */
427 void
428 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
429                                        GVariant              *parameters)
430 {
431   g_dbus_method_invocation_return_value_internal (invocation, parameters, NULL);
432 }
433
434 #ifdef G_OS_UNIX
435 /**
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.
440  *
441  * Like g_dbus_method_invocation_return_value() but also takes a #GUnixFDList.
442  *
443  * This method is only available on UNIX.
444  *
445  * This method will free @invocation, you cannot use it afterwards.
446  *
447  * Since: 2.30
448  */
449 void
450 g_dbus_method_invocation_return_value_with_unix_fd_list (GDBusMethodInvocation *invocation,
451                                                          GVariant              *parameters,
452                                                          GUnixFDList           *fd_list)
453 {
454   g_dbus_method_invocation_return_value_internal (invocation, parameters, fd_list);
455 }
456 #endif
457
458 /* ---------------------------------------------------------------------------------------------------- */
459
460 /**
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.
467  *
468  * Finishes handling a D-Bus method call by returning an error.
469  *
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.
477  *
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().
481  *
482  * This method will free @invocation, you cannot use it afterwards.
483  *
484  * Since: 2.26
485  */
486 void
487 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
488                                        GQuark                 domain,
489                                        gint                   code,
490                                        const gchar           *format,
491                                        ...)
492 {
493   va_list var_args;
494
495   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
496   g_return_if_fail (format != NULL);
497
498   va_start (var_args, format);
499   g_dbus_method_invocation_return_error_valist (invocation,
500                                                 domain,
501                                                 code,
502                                                 format,
503                                                 var_args);
504   va_end (var_args);
505 }
506
507 /**
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.
514  *
515  * Like g_dbus_method_invocation_return_error() but intended for
516  * language bindings.
517  *
518  * This method will free @invocation, you cannot use it afterwards.
519  *
520  * Since: 2.26
521  */
522 void
523 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
524                                               GQuark                 domain,
525                                               gint                   code,
526                                               const gchar           *format,
527                                               va_list                var_args)
528 {
529   gchar *literal_message;
530
531   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
532   g_return_if_fail (format != NULL);
533
534   literal_message = g_strdup_vprintf (format, var_args);
535   g_dbus_method_invocation_return_error_literal (invocation,
536                                                  domain,
537                                                  code,
538                                                  literal_message);
539   g_free (literal_message);
540 }
541
542 /**
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.
548  *
549  * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
550  *
551  * This method will free @invocation, you cannot use it afterwards.
552  *
553  * Since: 2.26
554  */
555 void
556 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
557                                                GQuark                 domain,
558                                                gint                   code,
559                                                const gchar           *message)
560 {
561   GError *error;
562
563   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
564   g_return_if_fail (message != NULL);
565
566   error = g_error_new_literal (domain, code, message);
567   g_dbus_method_invocation_return_gerror (invocation, error);
568   g_error_free (error);
569 }
570
571 /**
572  * g_dbus_method_invocation_return_gerror:
573  * @invocation: (transfer full): A #GDBusMethodInvocation.
574  * @error: A #GError.
575  *
576  * Like g_dbus_method_invocation_return_error() but takes a #GError
577  * instead of the error domain, error code and message.
578  *
579  * This method will free @invocation, you cannot use it afterwards.
580  *
581  * Since: 2.26
582  */
583 void
584 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
585                                         const GError          *error)
586 {
587   gchar *dbus_error_name;
588
589   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
590   g_return_if_fail (error != NULL);
591
592   dbus_error_name = g_dbus_error_encode_gerror (error);
593
594   g_dbus_method_invocation_return_dbus_error (invocation,
595                                               dbus_error_name,
596                                               error->message);
597   g_free (dbus_error_name);
598 }
599
600 /**
601  * g_dbus_method_invocation_take_error: (skip)
602  * @invocation: (transfer full): A #GDBusMethodInvocation.
603  * @error: (transfer full): A #GError.
604  *
605  * Like g_dbus_method_invocation_return_gerror() but takes ownership
606  * of @error so the caller does not need to free it.
607  *
608  * This method will free @invocation, you cannot use it afterwards.
609  *
610  * Since: 2.30
611  */
612 void
613 g_dbus_method_invocation_take_error (GDBusMethodInvocation *invocation,
614                                      GError                *error)
615 {
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);
620 }
621
622 /**
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.
627  *
628  * Finishes handling a D-Bus method call by returning an error.
629  *
630  * This method will free @invocation, you cannot use it afterwards.
631  *
632  * Since: 2.26
633  */
634 void
635 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
636                                             const gchar           *error_name,
637                                             const gchar           *error_message)
638 {
639   GDBusMessage *reply;
640
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);
644
645   if (G_UNLIKELY (_g_dbus_debug_return ()))
646     {
647       _g_dbus_debug_print_lock ();
648       g_print ("========================================================================\n"
649                "GDBus-debug:Return:\n"
650                " >>>> METHOD ERROR %s\n"
651                "      message `%s'\n"
652                "      in response to %s.%s()\n"
653                "      on object %s\n"
654                "      to name %s\n"
655                "      reply-serial %d\n",
656                error_name,
657                error_message,
658                invocation->interface_name, invocation->method_name,
659                invocation->object_path,
660                invocation->sender,
661                g_dbus_message_get_serial (invocation->message));
662       _g_dbus_debug_print_unlock ();
663     }
664
665   reply = g_dbus_message_new_method_error_literal (invocation->message,
666                                                    error_name,
667                                                    error_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);
670
671   g_object_unref (invocation);
672 }