Blame gio/gdbusmethodinvocation.c

Packit ae235b
/* GDBus - GLib D-Bus Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2008-2010 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: David Zeuthen <davidz@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#include "gdbusutils.h"
Packit ae235b
#include "gdbusconnection.h"
Packit ae235b
#include "gdbusmessage.h"
Packit ae235b
#include "gdbusmethodinvocation.h"
Packit ae235b
#include "gdbusintrospection.h"
Packit ae235b
#include "gdbuserror.h"
Packit ae235b
#include "gdbusprivate.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include "gunixfdlist.h"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gdbusmethodinvocation
Packit ae235b
 * @short_description: Object for handling remote calls
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * Instances of the #GDBusMethodInvocation class are used when
Packit ae235b
 * handling D-Bus method calls. It provides a way to asynchronously
Packit ae235b
 * return results and errors.
Packit ae235b
 *
Packit ae235b
 * The normal way to obtain a #GDBusMethodInvocation object is to receive
Packit ae235b
 * it as an argument to the handle_method_call() function in a
Packit ae235b
 * #GDBusInterfaceVTable that was passed to g_dbus_connection_register_object().
Packit ae235b
 */
Packit ae235b
Packit ae235b
typedef struct _GDBusMethodInvocationClass GDBusMethodInvocationClass;
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GDBusMethodInvocationClass:
Packit ae235b
 *
Packit ae235b
 * Class structure for #GDBusMethodInvocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
struct _GDBusMethodInvocationClass
Packit ae235b
{
Packit ae235b
  /*< private >*/
Packit ae235b
  GObjectClass parent_class;
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GDBusMethodInvocation:
Packit ae235b
 *
Packit ae235b
 * The #GDBusMethodInvocation structure contains only private data and
Packit ae235b
 * should only be accessed using the provided API.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
struct _GDBusMethodInvocation
Packit ae235b
{
Packit ae235b
  /*< private >*/
Packit ae235b
  GObject parent_instance;
Packit ae235b
Packit ae235b
  /* construct-only properties */
Packit ae235b
  gchar           *sender;
Packit ae235b
  gchar           *object_path;
Packit ae235b
  gchar           *interface_name;
Packit ae235b
  gchar           *method_name;
Packit ae235b
  GDBusMethodInfo *method_info;
Packit ae235b
  GDBusPropertyInfo *property_info;
Packit ae235b
  GDBusConnection *connection;
Packit ae235b
  GDBusMessage    *message;
Packit ae235b
  GVariant        *parameters;
Packit ae235b
  gpointer         user_data;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_method_invocation_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
Packit ae235b
Packit ae235b
  g_free (invocation->sender);
Packit ae235b
  g_free (invocation->object_path);
Packit ae235b
  g_free (invocation->interface_name);
Packit ae235b
  g_free (invocation->method_name);
Packit ae235b
  if (invocation->method_info)
Packit ae235b
      g_dbus_method_info_unref (invocation->method_info);
Packit ae235b
  if (invocation->property_info)
Packit ae235b
      g_dbus_property_info_unref (invocation->property_info);
Packit ae235b
  g_object_unref (invocation->connection);
Packit ae235b
  g_object_unref (invocation->message);
Packit ae235b
  g_variant_unref (invocation->parameters);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_dbus_method_invocation_finalize;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_get_sender:
Packit ae235b
 * @invocation: A #GDBusMethodInvocation.
Packit ae235b
 *
Packit ae235b
 * Gets the bus name that invoked the method.
Packit ae235b
 *
Packit ae235b
 * Returns: A string. Do not free, it is owned by @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
Packit ae235b
  return invocation->sender;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_get_object_path:
Packit ae235b
 * @invocation: A #GDBusMethodInvocation.
Packit ae235b
 *
Packit ae235b
 * Gets the object path the method was invoked on.
Packit ae235b
 *
Packit ae235b
 * Returns: A string. Do not free, it is owned by @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
Packit ae235b
  return invocation->object_path;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_get_interface_name:
Packit ae235b
 * @invocation: A #GDBusMethodInvocation.
Packit ae235b
 *
Packit ae235b
 * Gets the name of the D-Bus interface the method was invoked on.
Packit ae235b
 *
Packit ae235b
 * If this method call is a property Get, Set or GetAll call that has
Packit ae235b
 * been redirected to the method call handler then
Packit ae235b
 * "org.freedesktop.DBus.Properties" will be returned.  See
Packit ae235b
 * #GDBusInterfaceVTable for more information.
Packit ae235b
 *
Packit ae235b
 * Returns: A string. Do not free, it is owned by @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
Packit ae235b
  return invocation->interface_name;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_get_method_info:
Packit ae235b
 * @invocation: A #GDBusMethodInvocation.
Packit ae235b
 *
Packit ae235b
 * Gets information about the method call, if any.
Packit ae235b
 *
Packit ae235b
 * If this method invocation is a property Get, Set or GetAll call that
Packit ae235b
 * has been redirected to the method call handler then %NULL will be
Packit ae235b
 * returned.  See g_dbus_method_invocation_get_property_info() and
Packit ae235b
 * #GDBusInterfaceVTable for more information.
Packit ae235b
 *
Packit ae235b
 * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const GDBusMethodInfo *
Packit ae235b
g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
Packit ae235b
  return invocation->method_info;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_get_property_info:
Packit ae235b
 * @invocation: A #GDBusMethodInvocation
Packit ae235b
 *
Packit ae235b
 * Gets information about the property that this method call is for, if
Packit ae235b
 * any.
Packit ae235b
 *
Packit ae235b
 * This will only be set in the case of an invocation in response to a
Packit ae235b
 * property Get or Set call that has been directed to the method call
Packit ae235b
 * handler for an object on account of its property_get() or
Packit ae235b
 * property_set() vtable pointers being unset.
Packit ae235b
 *
Packit ae235b
 * See #GDBusInterfaceVTable for more information.
Packit ae235b
 *
Packit ae235b
 * If the call was GetAll, %NULL will be returned.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): a #GDBusPropertyInfo or %NULL
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
const GDBusPropertyInfo *
Packit ae235b
g_dbus_method_invocation_get_property_info (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
Packit ae235b
  return invocation->property_info;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_get_method_name:
Packit ae235b
 * @invocation: A #GDBusMethodInvocation.
Packit ae235b
 *
Packit ae235b
 * Gets the name of the method that was invoked.
Packit ae235b
 *
Packit ae235b
 * Returns: A string. Do not free, it is owned by @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
const gchar *
Packit ae235b
g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
Packit ae235b
  return invocation->method_name;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_get_connection:
Packit ae235b
 * @invocation: A #GDBusMethodInvocation.
Packit ae235b
 *
Packit ae235b
 * Gets the #GDBusConnection the method was invoked on.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none):A #GDBusConnection. Do not free, it is owned by @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GDBusConnection *
Packit ae235b
g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
Packit ae235b
  return invocation->connection;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_get_message:
Packit ae235b
 * @invocation: A #GDBusMethodInvocation.
Packit ae235b
 *
Packit ae235b
 * Gets the #GDBusMessage for the method invocation. This is useful if
Packit ae235b
 * you need to use low-level protocol features, such as UNIX file
Packit ae235b
 * descriptor passing, that cannot be properly expressed in the
Packit ae235b
 * #GVariant API.
Packit ae235b
 *
Packit ae235b
 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
Packit ae235b
 * for an example of how to use this low-level API to send and receive
Packit ae235b
 * UNIX file descriptors.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): #GDBusMessage. Do not free, it is owned by @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GDBusMessage *
Packit ae235b
g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
Packit ae235b
  return invocation->message;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_get_parameters:
Packit ae235b
 * @invocation: A #GDBusMethodInvocation.
Packit ae235b
 *
Packit ae235b
 * Gets the parameters of the method invocation. If there are no input
Packit ae235b
 * parameters then this will return a GVariant with 0 children rather than NULL.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): A #GVariant tuple. Do not unref this because it is owned by @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GVariant *
Packit ae235b
g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
Packit ae235b
  return invocation->parameters;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_get_user_data: (skip)
Packit ae235b
 * @invocation: A #GDBusMethodInvocation.
Packit ae235b
 *
Packit ae235b
 * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
Packit ae235b
 *
Packit ae235b
 * Returns: A #gpointer.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gpointer
Packit ae235b
g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
Packit ae235b
  return invocation->user_data;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* < internal >
Packit ae235b
 * _g_dbus_method_invocation_new:
Packit ae235b
 * @sender: (nullable): The bus name that invoked the method or %NULL if @connection is not a bus connection.
Packit ae235b
 * @object_path: The object path the method was invoked on.
Packit ae235b
 * @interface_name: The name of the D-Bus interface the method was invoked on.
Packit ae235b
 * @method_name: The name of the method that was invoked.
Packit ae235b
 * @method_info: (nullable): Information about the method call or %NULL.
Packit ae235b
 * @property_info: (nullable): Information about the property or %NULL.
Packit ae235b
 * @connection: The #GDBusConnection the method was invoked on.
Packit ae235b
 * @message: The D-Bus message as a #GDBusMessage.
Packit ae235b
 * @parameters: The parameters as a #GVariant tuple.
Packit ae235b
 * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
Packit ae235b
 *
Packit ae235b
 * Creates a new #GDBusMethodInvocation object.
Packit ae235b
 *
Packit ae235b
 * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GDBusMethodInvocation *
Packit ae235b
_g_dbus_method_invocation_new (const gchar             *sender,
Packit ae235b
                               const gchar             *object_path,
Packit ae235b
                               const gchar             *interface_name,
Packit ae235b
                               const gchar             *method_name,
Packit ae235b
                               const GDBusMethodInfo   *method_info,
Packit ae235b
                               const GDBusPropertyInfo *property_info,
Packit ae235b
                               GDBusConnection         *connection,
Packit ae235b
                               GDBusMessage            *message,
Packit ae235b
                               GVariant                *parameters,
Packit ae235b
                               gpointer                 user_data)
Packit ae235b
{
Packit ae235b
  GDBusMethodInvocation *invocation;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
Packit ae235b
  g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
Packit ae235b
  g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
Packit ae235b
  g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
Packit ae235b
  g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
Packit ae235b
Packit ae235b
  invocation = G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION, NULL));
Packit ae235b
  invocation->sender = g_strdup (sender);
Packit ae235b
  invocation->object_path = g_strdup (object_path);
Packit ae235b
  invocation->interface_name = g_strdup (interface_name);
Packit ae235b
  invocation->method_name = g_strdup (method_name);
Packit ae235b
  if (method_info)
Packit ae235b
    invocation->method_info = g_dbus_method_info_ref ((GDBusMethodInfo *)method_info);
Packit ae235b
  if (property_info)
Packit ae235b
    invocation->property_info = g_dbus_property_info_ref ((GDBusPropertyInfo *)property_info);
Packit ae235b
  invocation->connection = g_object_ref (connection);
Packit ae235b
  invocation->message = g_object_ref (message);
Packit ae235b
  invocation->parameters = g_variant_ref (parameters);
Packit ae235b
  invocation->user_data = user_data;
Packit ae235b
Packit ae235b
  return invocation;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_dbus_method_invocation_return_value_internal (GDBusMethodInvocation *invocation,
Packit ae235b
                                                GVariant              *parameters,
Packit ae235b
                                                GUnixFDList           *fd_list)
Packit ae235b
{
Packit ae235b
  GDBusMessage *reply;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
Packit ae235b
  g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
Packit ae235b
Packit ae235b
  if (g_dbus_message_get_flags (invocation->message) & G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED)
Packit ae235b
    {
Packit ae235b
      if (parameters != NULL)
Packit ae235b
        {
Packit ae235b
          g_variant_ref_sink (parameters);
Packit ae235b
          g_variant_unref (parameters);
Packit ae235b
        }
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (parameters == NULL)
Packit ae235b
    parameters = g_variant_new_tuple (NULL, 0);
Packit ae235b
Packit ae235b
  /* if we have introspection data, check that the signature of @parameters is correct */
Packit ae235b
  if (invocation->method_info != NULL)
Packit ae235b
    {
Packit ae235b
      GVariantType *type;
Packit ae235b
Packit ae235b
      type = _g_dbus_compute_complete_signature (invocation->method_info->out_args);
Packit ae235b
Packit ae235b
      if (!g_variant_is_of_type (parameters, type))
Packit ae235b
        {
Packit ae235b
          gchar *type_string = g_variant_type_dup_string (type);
Packit ae235b
Packit ae235b
          g_warning ("Type of return value is incorrect: expected '%s', got '%s''",
Packit ae235b
		     type_string, g_variant_get_type_string (parameters));
Packit ae235b
          g_variant_type_free (type);
Packit ae235b
          g_free (type_string);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
      g_variant_type_free (type);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* property_info is only non-NULL if set that way from
Packit ae235b
   * GDBusConnection, so this must be the case of async property
Packit ae235b
   * handling on either 'Get', 'Set' or 'GetAll'.
Packit ae235b
   */
Packit ae235b
  if (invocation->property_info != NULL)
Packit ae235b
    {
Packit ae235b
      if (g_str_equal (invocation->method_name, "Get"))
Packit ae235b
        {
Packit ae235b
          GVariant *nested;
Packit ae235b
Packit ae235b
          if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(v)")))
Packit ae235b
            {
Packit ae235b
              g_warning ("Type of return value for property 'Get' call should be '(v)' but got '%s'",
Packit ae235b
                         g_variant_get_type_string (parameters));
Packit ae235b
              goto out;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          /* Go deeper and make sure that the value inside of the
Packit ae235b
           * variant matches the property type.
Packit ae235b
           */
Packit ae235b
          g_variant_get (parameters, "(v)", &nested);
Packit ae235b
          if (!g_str_equal (g_variant_get_type_string (nested), invocation->property_info->signature))
Packit ae235b
            {
Packit ae235b
              g_warning ("Value returned from property 'Get' call for '%s' should be '%s' but is '%s'",
Packit ae235b
                         invocation->property_info->name, invocation->property_info->signature,
Packit ae235b
                         g_variant_get_type_string (nested));
Packit ae235b
              g_variant_unref (nested);
Packit ae235b
              goto out;
Packit ae235b
            }
Packit ae235b
          g_variant_unref (nested);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else if (g_str_equal (invocation->method_name, "GetAll"))
Packit ae235b
        {
Packit ae235b
          if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(a{sv})")))
Packit ae235b
            {
Packit ae235b
              g_warning ("Type of return value for property 'GetAll' call should be '(a{sv})' but got '%s'",
Packit ae235b
                         g_variant_get_type_string (parameters));
Packit ae235b
              goto out;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          /* Could iterate the list of properties and make sure that all
Packit ae235b
           * of them are actually on the interface and with the correct
Packit ae235b
           * types, but let's not do that for now...
Packit ae235b
           */
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else if (g_str_equal (invocation->method_name, "Set"))
Packit ae235b
        {
Packit ae235b
          if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE_UNIT))
Packit ae235b
            {
Packit ae235b
              g_warning ("Type of return value for property 'Set' call should be '()' but got '%s'",
Packit ae235b
                         g_variant_get_type_string (parameters));
Packit ae235b
              goto out;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      else
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (_g_dbus_debug_return ()))
Packit ae235b
    {
Packit ae235b
      _g_dbus_debug_print_lock ();
Packit ae235b
      g_print ("========================================================================\n"
Packit ae235b
               "GDBus-debug:Return:\n"
Packit ae235b
               " >>>> METHOD RETURN\n"
Packit ae235b
               "      in response to %s.%s()\n"
Packit ae235b
               "      on object %s\n"
Packit ae235b
               "      to name %s\n"
Packit ae235b
               "      reply-serial %d\n",
Packit ae235b
               invocation->interface_name, invocation->method_name,
Packit ae235b
               invocation->object_path,
Packit ae235b
               invocation->sender,
Packit ae235b
               g_dbus_message_get_serial (invocation->message));
Packit ae235b
      _g_dbus_debug_print_unlock ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  reply = g_dbus_message_new_method_reply (invocation->message);
Packit ae235b
  g_dbus_message_set_body (reply, parameters);
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  if (fd_list != NULL)
Packit ae235b
    g_dbus_message_set_unix_fd_list (reply, fd_list);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error))
Packit ae235b
    {
Packit ae235b
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CLOSED))
Packit ae235b
        g_warning ("Error sending message: %s", error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
    }
Packit ae235b
  g_object_unref (reply);
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  g_object_unref (invocation);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_return_value:
Packit ae235b
 * @invocation: (transfer full): A #GDBusMethodInvocation.
Packit ae235b
 * @parameters: (nullable): A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
Packit ae235b
 *
Packit ae235b
 * Finishes handling a D-Bus method call by returning @parameters.
Packit ae235b
 * If the @parameters GVariant is floating, it is consumed.
Packit ae235b
 *
Packit ae235b
 * It is an error if @parameters is not of the right format: it must be a tuple
Packit ae235b
 * containing the out-parameters of the D-Bus method. Even if the method has a
Packit ae235b
 * single out-parameter, it must be contained in a tuple. If the method has no
Packit ae235b
 * out-parameters, @parameters may be %NULL or an empty tuple.
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 * GDBusMethodInvocation *invocation = some_invocation;
Packit ae235b
 * g_autofree gchar *result_string = NULL;
Packit ae235b
 * g_autoptr (GError) error = NULL;
Packit ae235b
 *
Packit ae235b
 * result_string = calculate_result (&error);
Packit ae235b
 *
Packit ae235b
 * if (error != NULL)
Packit ae235b
 *   g_dbus_method_invocation_return_gerror (invocation, error);
Packit ae235b
 * else
Packit ae235b
 *   g_dbus_method_invocation_return_value (invocation,
Packit ae235b
 *                                          g_variant_new ("(s)", result_string));
Packit ae235b
 *
Packit ae235b
 * // Do not free @invocation here; returning a value does that
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * This method will take ownership of @invocation. See
Packit ae235b
 * #GDBusInterfaceVTable for more information about the ownership of
Packit ae235b
 * @invocation.
Packit ae235b
 *
Packit ae235b
 * Since 2.48, if the method call requested for a reply not to be sent
Packit ae235b
 * then this call will sink @parameters and free @invocation, but
Packit ae235b
 * otherwise do nothing (as per the recommendations of the D-Bus
Packit ae235b
 * specification).
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
Packit ae235b
                                       GVariant              *parameters)
Packit ae235b
{
Packit ae235b
  g_dbus_method_invocation_return_value_internal (invocation, parameters, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_return_value_with_unix_fd_list:
Packit ae235b
 * @invocation: (transfer full): A #GDBusMethodInvocation.
Packit ae235b
 * @parameters: (nullable): A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
Packit ae235b
 * @fd_list: (nullable): A #GUnixFDList or %NULL.
Packit ae235b
 *
Packit ae235b
 * Like g_dbus_method_invocation_return_value() but also takes a #GUnixFDList.
Packit ae235b
 *
Packit ae235b
 * This method is only available on UNIX.
Packit ae235b
 *
Packit ae235b
 * This method will take ownership of @invocation. See
Packit ae235b
 * #GDBusInterfaceVTable for more information about the ownership of
Packit ae235b
 * @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dbus_method_invocation_return_value_with_unix_fd_list (GDBusMethodInvocation *invocation,
Packit ae235b
                                                         GVariant              *parameters,
Packit ae235b
                                                         GUnixFDList           *fd_list)
Packit ae235b
{
Packit ae235b
  g_dbus_method_invocation_return_value_internal (invocation, parameters, fd_list);
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_return_error:
Packit ae235b
 * @invocation: (transfer full): A #GDBusMethodInvocation.
Packit ae235b
 * @domain: A #GQuark for the #GError error domain.
Packit ae235b
 * @code: The error code.
Packit ae235b
 * @format: printf()-style format.
Packit ae235b
 * @...: Parameters for @format.
Packit ae235b
 *
Packit ae235b
 * Finishes handling a D-Bus method call by returning an error.
Packit ae235b
 *
Packit ae235b
 * See g_dbus_error_encode_gerror() for details about what error name
Packit ae235b
 * will be returned on the wire. In a nutshell, if the given error is
Packit ae235b
 * registered using g_dbus_error_register_error() the name given
Packit ae235b
 * during registration is used. Otherwise, a name of the form
Packit ae235b
 * `org.gtk.GDBus.UnmappedGError.Quark...` is used. This provides
Packit ae235b
 * transparent mapping of #GError between applications using GDBus.
Packit ae235b
 *
Packit ae235b
 * If you are writing an application intended to be portable,
Packit ae235b
 * always register errors with g_dbus_error_register_error()
Packit ae235b
 * or use g_dbus_method_invocation_return_dbus_error().
Packit ae235b
 *
Packit ae235b
 * This method will take ownership of @invocation. See
Packit ae235b
 * #GDBusInterfaceVTable for more information about the ownership of
Packit ae235b
 * @invocation.
Packit ae235b
 *
Packit ae235b
 * Since 2.48, if the method call requested for a reply not to be sent
Packit ae235b
 * then this call will free @invocation but otherwise do nothing (as per
Packit ae235b
 * the recommendations of the D-Bus specification).
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
Packit ae235b
                                       GQuark                 domain,
Packit ae235b
                                       gint                   code,
Packit ae235b
                                       const gchar           *format,
Packit ae235b
                                       ...)
Packit ae235b
{
Packit ae235b
  va_list var_args;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
Packit ae235b
  g_return_if_fail (format != NULL);
Packit ae235b
Packit ae235b
  va_start (var_args, format);
Packit ae235b
  g_dbus_method_invocation_return_error_valist (invocation,
Packit ae235b
                                                domain,
Packit ae235b
                                                code,
Packit ae235b
                                                format,
Packit ae235b
                                                var_args);
Packit ae235b
  va_end (var_args);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_return_error_valist:
Packit ae235b
 * @invocation: (transfer full): A #GDBusMethodInvocation.
Packit ae235b
 * @domain: A #GQuark for the #GError error domain.
Packit ae235b
 * @code: The error code.
Packit ae235b
 * @format: printf()-style format.
Packit ae235b
 * @var_args: #va_list of parameters for @format.
Packit ae235b
 *
Packit ae235b
 * Like g_dbus_method_invocation_return_error() but intended for
Packit ae235b
 * language bindings.
Packit ae235b
 *
Packit ae235b
 * This method will take ownership of @invocation. See
Packit ae235b
 * #GDBusInterfaceVTable for more information about the ownership of
Packit ae235b
 * @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
Packit ae235b
                                              GQuark                 domain,
Packit ae235b
                                              gint                   code,
Packit ae235b
                                              const gchar           *format,
Packit ae235b
                                              va_list                var_args)
Packit ae235b
{
Packit ae235b
  gchar *literal_message;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
Packit ae235b
  g_return_if_fail (format != NULL);
Packit ae235b
Packit ae235b
  literal_message = g_strdup_vprintf (format, var_args);
Packit ae235b
  g_dbus_method_invocation_return_error_literal (invocation,
Packit ae235b
                                                 domain,
Packit ae235b
                                                 code,
Packit ae235b
                                                 literal_message);
Packit ae235b
  g_free (literal_message);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_return_error_literal:
Packit ae235b
 * @invocation: (transfer full): A #GDBusMethodInvocation.
Packit ae235b
 * @domain: A #GQuark for the #GError error domain.
Packit ae235b
 * @code: The error code.
Packit ae235b
 * @message: The error message.
Packit ae235b
 *
Packit ae235b
 * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
Packit ae235b
 *
Packit ae235b
 * This method will take ownership of @invocation. See
Packit ae235b
 * #GDBusInterfaceVTable for more information about the ownership of
Packit ae235b
 * @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
Packit ae235b
                                               GQuark                 domain,
Packit ae235b
                                               gint                   code,
Packit ae235b
                                               const gchar           *message)
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
Packit ae235b
  g_return_if_fail (message != NULL);
Packit ae235b
Packit ae235b
  error = g_error_new_literal (domain, code, message);
Packit ae235b
  g_dbus_method_invocation_return_gerror (invocation, error);
Packit ae235b
  g_error_free (error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_return_gerror:
Packit ae235b
 * @invocation: (transfer full): A #GDBusMethodInvocation.
Packit ae235b
 * @error: A #GError.
Packit ae235b
 *
Packit ae235b
 * Like g_dbus_method_invocation_return_error() but takes a #GError
Packit ae235b
 * instead of the error domain, error code and message.
Packit ae235b
 *
Packit ae235b
 * This method will take ownership of @invocation. See
Packit ae235b
 * #GDBusInterfaceVTable for more information about the ownership of
Packit ae235b
 * @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
Packit ae235b
                                        const GError          *error)
Packit ae235b
{
Packit ae235b
  gchar *dbus_error_name;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
Packit ae235b
  g_return_if_fail (error != NULL);
Packit ae235b
Packit ae235b
  dbus_error_name = g_dbus_error_encode_gerror (error);
Packit ae235b
Packit ae235b
  g_dbus_method_invocation_return_dbus_error (invocation,
Packit ae235b
                                              dbus_error_name,
Packit ae235b
                                              error->message);
Packit ae235b
  g_free (dbus_error_name);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_take_error: (skip)
Packit ae235b
 * @invocation: (transfer full): A #GDBusMethodInvocation.
Packit ae235b
 * @error: (transfer full): A #GError.
Packit ae235b
 *
Packit ae235b
 * Like g_dbus_method_invocation_return_gerror() but takes ownership
Packit ae235b
 * of @error so the caller does not need to free it.
Packit ae235b
 *
Packit ae235b
 * This method will take ownership of @invocation. See
Packit ae235b
 * #GDBusInterfaceVTable for more information about the ownership of
Packit ae235b
 * @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dbus_method_invocation_take_error (GDBusMethodInvocation *invocation,
Packit ae235b
                                     GError                *error)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
Packit ae235b
  g_return_if_fail (error != NULL);
Packit ae235b
  g_dbus_method_invocation_return_gerror (invocation, error);
Packit ae235b
  g_error_free (error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_method_invocation_return_dbus_error:
Packit ae235b
 * @invocation: (transfer full): A #GDBusMethodInvocation.
Packit ae235b
 * @error_name: A valid D-Bus error name.
Packit ae235b
 * @error_message: A valid D-Bus error message.
Packit ae235b
 *
Packit ae235b
 * Finishes handling a D-Bus method call by returning an error.
Packit ae235b
 *
Packit ae235b
 * This method will take ownership of @invocation. See
Packit ae235b
 * #GDBusInterfaceVTable for more information about the ownership of
Packit ae235b
 * @invocation.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
Packit ae235b
                                            const gchar           *error_name,
Packit ae235b
                                            const gchar           *error_message)
Packit ae235b
{
Packit ae235b
  GDBusMessage *reply;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
Packit ae235b
  g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
Packit ae235b
  g_return_if_fail (error_message != NULL);
Packit ae235b
Packit ae235b
  if (g_dbus_message_get_flags (invocation->message) & G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED)
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (_g_dbus_debug_return ()))
Packit ae235b
    {
Packit ae235b
      _g_dbus_debug_print_lock ();
Packit ae235b
      g_print ("========================================================================\n"
Packit ae235b
               "GDBus-debug:Return:\n"
Packit ae235b
               " >>>> METHOD ERROR %s\n"
Packit ae235b
               "      message '%s'\n"
Packit ae235b
               "      in response to %s.%s()\n"
Packit ae235b
               "      on object %s\n"
Packit ae235b
               "      to name %s\n"
Packit ae235b
               "      reply-serial %d\n",
Packit ae235b
               error_name,
Packit ae235b
               error_message,
Packit ae235b
               invocation->interface_name, invocation->method_name,
Packit ae235b
               invocation->object_path,
Packit ae235b
               invocation->sender,
Packit ae235b
               g_dbus_message_get_serial (invocation->message));
Packit ae235b
      _g_dbus_debug_print_unlock ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  reply = g_dbus_message_new_method_error_literal (invocation->message,
Packit ae235b
                                                   error_name,
Packit ae235b
                                                   error_message);
Packit ae235b
  g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
Packit ae235b
  g_object_unref (reply);
Packit ae235b
Packit ae235b
out:
Packit ae235b
  g_object_unref (invocation);
Packit ae235b
}