gio: (belatedly) port gdbus from GSimpleAsyncResult to GTask

https://bugzilla.gnome.org/show_bug.cgi?id=661767
This commit is contained in:
Dan Winship
2012-08-02 15:46:32 -04:00
parent e2655cd455
commit f10b6550ff
5 changed files with 365 additions and 607 deletions

View File

@@ -32,8 +32,7 @@
#include "ginitable.h"
#include "gasyncinitable.h"
#include "gioerror.h"
#include "gasyncresult.h"
#include "gsimpleasyncresult.h"
#include "gtask.h"
#include "gcancellable.h"
#include "gdbusinterface.h"
@@ -1411,29 +1410,12 @@ on_name_owner_changed (GDBusConnection *connection,
/* ---------------------------------------------------------------------------------------------------- */
typedef struct
{
GDBusProxy *proxy;
GCancellable *cancellable;
GSimpleAsyncResult *simple;
} AsyncInitData;
static void
async_init_data_free (AsyncInitData *data)
{
g_object_unref (data->proxy);
if (data->cancellable != NULL)
g_object_unref (data->cancellable);
g_object_unref (data->simple);
g_free (data);
}
static void
async_init_get_all_cb (GDBusConnection *connection,
GAsyncResult *res,
gpointer user_data)
{
AsyncInitData *data = user_data;
GTask *task = user_data;
GVariant *result;
GError *error;
@@ -1456,40 +1438,35 @@ async_init_get_all_cb (GDBusConnection *connection,
//g_debug ("error: %d %d %s", error->domain, error->code, error->message);
g_error_free (error);
}
else
{
g_simple_async_result_set_op_res_gpointer (data->simple,
result,
(GDestroyNotify) g_variant_unref);
}
g_simple_async_result_complete_in_idle (data->simple);
async_init_data_free (data);
g_task_return_pointer (task, result,
(GDestroyNotify) g_variant_unref);
g_object_unref (task);
}
static void
async_init_data_set_name_owner (AsyncInitData *data,
const gchar *name_owner)
async_init_data_set_name_owner (GTask *task,
const gchar *name_owner)
{
GDBusProxy *proxy = g_task_get_source_object (task);
gboolean get_all;
if (name_owner != NULL)
{
/* it starts as NULL anyway */
G_LOCK (properties_lock);
data->proxy->priv->name_owner = g_strdup (name_owner);
proxy->priv->name_owner = g_strdup (name_owner);
G_UNLOCK (properties_lock);
}
get_all = TRUE;
if (data->proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
if (proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES)
{
/* Don't load properties if the API user doesn't want them */
get_all = FALSE;
}
else if (name_owner == NULL && data->proxy->priv->name != NULL)
else if (name_owner == NULL && proxy->priv->name != NULL)
{
/* Don't attempt to load properties if the name_owner is NULL (which
* usually means the name isn't owned), unless name is also NULL (which
@@ -1502,23 +1479,23 @@ async_init_data_set_name_owner (AsyncInitData *data,
if (get_all)
{
/* load all properties asynchronously */
g_dbus_connection_call (data->proxy->priv->connection,
g_dbus_connection_call (proxy->priv->connection,
name_owner,
data->proxy->priv->object_path,
proxy->priv->object_path,
"org.freedesktop.DBus.Properties",
"GetAll",
g_variant_new ("(s)", data->proxy->priv->interface_name),
g_variant_new ("(s)", proxy->priv->interface_name),
G_VARIANT_TYPE ("(a{sv})"),
G_DBUS_CALL_FLAGS_NONE,
-1, /* timeout */
data->cancellable,
g_task_get_cancellable (task),
(GAsyncReadyCallback) async_init_get_all_cb,
data);
task);
}
else
{
g_simple_async_result_complete_in_idle (data->simple);
async_init_data_free (data);
g_task_return_pointer (task, NULL, NULL);
g_object_unref (task);
}
}
@@ -1527,7 +1504,7 @@ async_init_get_name_owner_cb (GDBusConnection *connection,
GAsyncResult *res,
gpointer user_data)
{
AsyncInitData *data = user_data;
GTask *task = user_data;
GError *error;
GVariant *result;
@@ -1541,13 +1518,12 @@ async_init_get_name_owner_cb (GDBusConnection *connection,
error->code == G_DBUS_ERROR_NAME_HAS_NO_OWNER)
{
g_error_free (error);
async_init_data_set_name_owner (data, NULL);
async_init_data_set_name_owner (task, NULL);
}
else
{
g_simple_async_result_take_error (data->simple, error);
g_simple_async_result_complete_in_idle (data->simple);
async_init_data_free (data);
g_task_return_error (task, error);
g_object_unref (task);
}
}
else
@@ -1556,27 +1532,29 @@ async_init_get_name_owner_cb (GDBusConnection *connection,
const gchar *name_owner;
g_variant_get (result, "(&s)", &name_owner);
async_init_data_set_name_owner (data, name_owner);
async_init_data_set_name_owner (task, name_owner);
g_variant_unref (result);
}
}
static void
async_init_call_get_name_owner (AsyncInitData *data)
async_init_call_get_name_owner (GTask *task)
{
g_dbus_connection_call (data->proxy->priv->connection,
GDBusProxy *proxy = g_task_get_source_object (task);
g_dbus_connection_call (proxy->priv->connection,
"org.freedesktop.DBus", /* name */
"/org/freedesktop/DBus", /* object path */
"org.freedesktop.DBus", /* interface */
"GetNameOwner",
g_variant_new ("(s)",
data->proxy->priv->name),
proxy->priv->name),
G_VARIANT_TYPE ("(s)"),
G_DBUS_CALL_FLAGS_NONE,
-1, /* timeout */
data->cancellable,
g_task_get_cancellable (task),
(GAsyncReadyCallback) async_init_get_name_owner_cb,
data);
task);
}
static void
@@ -1584,7 +1562,8 @@ async_init_start_service_by_name_cb (GDBusConnection *connection,
GAsyncResult *res,
gpointer user_data)
{
AsyncInitData *data = user_data;
GTask *task = user_data;
GDBusProxy *proxy = g_task_get_source_object (task);
GError *error;
GVariant *result;
@@ -1628,7 +1607,7 @@ async_init_start_service_by_name_cb (GDBusConnection *connection,
{
g_prefix_error (&error,
_("Error calling StartServiceByName for %s: "),
data->proxy->priv->name);
proxy->priv->name);
g_free (remote_error);
goto failed;
}
@@ -1652,38 +1631,39 @@ async_init_start_service_by_name_cb (GDBusConnection *connection,
G_IO_ERROR_FAILED,
_("Unexpected reply %d from StartServiceByName(\"%s\") method"),
start_service_result,
data->proxy->priv->name);
proxy->priv->name);
goto failed;
}
}
async_init_call_get_name_owner (data);
async_init_call_get_name_owner (task);
return;
failed:
g_warn_if_fail (error != NULL);
g_simple_async_result_take_error (data->simple, error);
g_simple_async_result_complete_in_idle (data->simple);
async_init_data_free (data);
g_task_return_error (task, error);
g_object_unref (task);
}
static void
async_init_call_start_service_by_name (AsyncInitData *data)
async_init_call_start_service_by_name (GTask *task)
{
g_dbus_connection_call (data->proxy->priv->connection,
GDBusProxy *proxy = g_task_get_source_object (task);
g_dbus_connection_call (proxy->priv->connection,
"org.freedesktop.DBus", /* name */
"/org/freedesktop/DBus", /* object path */
"org.freedesktop.DBus", /* interface */
"StartServiceByName",
g_variant_new ("(su)",
data->proxy->priv->name,
proxy->priv->name,
0),
G_VARIANT_TYPE ("(u)"),
G_DBUS_CALL_FLAGS_NONE,
-1, /* timeout */
data->cancellable,
g_task_get_cancellable (task),
(GAsyncReadyCallback) async_init_start_service_by_name_cb,
data);
task);
}
static void
@@ -1694,37 +1674,31 @@ async_initable_init_second_async (GAsyncInitable *initable,
gpointer user_data)
{
GDBusProxy *proxy = G_DBUS_PROXY (initable);
AsyncInitData *data;
GTask *task;
data = g_new0 (AsyncInitData, 1);
data->proxy = g_object_ref (proxy);
data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
data->simple = g_simple_async_result_new (G_OBJECT (proxy),
callback,
user_data,
NULL);
g_simple_async_result_set_check_cancellable (data->simple, cancellable);
task = g_task_new (proxy, cancellable, callback, user_data);
g_task_set_priority (task, io_priority);
/* Check name ownership asynchronously - possibly also start the service */
if (proxy->priv->name == NULL)
{
/* Do nothing */
async_init_data_set_name_owner (data, NULL);
async_init_data_set_name_owner (task, NULL);
}
else if (g_dbus_is_unique_name (proxy->priv->name))
{
async_init_data_set_name_owner (data, proxy->priv->name);
async_init_data_set_name_owner (task, proxy->priv->name);
}
else
{
if ((proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START) ||
(proxy->priv->flags & G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START_AT_CONSTRUCTION))
{
async_init_call_get_name_owner (data);
async_init_call_get_name_owner (task);
}
else
{
async_init_call_start_service_by_name (data);
async_init_call_start_service_by_name (task);
}
}
}
@@ -1735,24 +1709,18 @@ async_initable_init_second_finish (GAsyncInitable *initable,
GError **error)
{
GDBusProxy *proxy = G_DBUS_PROXY (initable);
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
GTask *task = G_TASK (res);
GVariant *result;
gboolean ret;
ret = FALSE;
ret = !g_task_had_error (task);
if (g_simple_async_result_propagate_error (simple, error))
goto out;
result = g_simple_async_result_get_op_res_gpointer (simple);
result = g_task_propagate_pointer (task, error);
if (result != NULL)
{
process_get_all_reply (proxy, result);
}
ret = TRUE;
out:
proxy->priv->initialized = TRUE;
return ret;
}
@@ -1815,7 +1783,7 @@ async_initable_init_first (GAsyncInitable *initable)
/* ---------------------------------------------------------------------------------------------------- */
/* initialization is split into two parts - the first is the
* non-blocing part that requires the callers GMainContext - the
* non-blocking part that requires the callers GMainContext - the
* second is a blocking part async part that doesn't require the
* callers GMainContext.. we do this split so the code can be reused
* in the GInitable implementation below.
@@ -1824,52 +1792,46 @@ async_initable_init_first (GAsyncInitable *initable)
* paths.
*/
typedef struct
static void
init_second_async_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GDBusProxy *proxy;
gint io_priority;
GCancellable *cancellable;
GAsyncReadyCallback callback;
gpointer user_data;
} GetConnectionData;
GTask *task = user_data;
GError *error = NULL;
if (async_initable_init_second_finish (G_ASYNC_INITABLE (source_object), res, &error))
g_task_return_boolean (task, TRUE);
else
g_task_return_error (task, error);
g_object_unref (task);
}
static void
get_connection_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GetConnectionData *data = user_data;
GTask *task = user_data;
GDBusProxy *proxy = g_task_get_source_object (task);
GError *error;
error = NULL;
data->proxy->priv->connection = g_bus_get_finish (res, &error);
if (data->proxy->priv->connection == NULL)
proxy->priv->connection = g_bus_get_finish (res, &error);
if (proxy->priv->connection == NULL)
{
GSimpleAsyncResult *simple;
simple = g_simple_async_result_new (G_OBJECT (data->proxy),
data->callback,
data->user_data,
NULL);
g_simple_async_result_set_check_cancellable (simple, data->cancellable);
g_simple_async_result_take_error (simple, error);
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
g_task_return_error (task, error);
g_object_unref (task);
}
else
{
async_initable_init_first (G_ASYNC_INITABLE (data->proxy));
async_initable_init_second_async (G_ASYNC_INITABLE (data->proxy),
data->io_priority,
data->cancellable,
data->callback,
data->user_data);
async_initable_init_first (G_ASYNC_INITABLE (proxy));
async_initable_init_second_async (G_ASYNC_INITABLE (proxy),
g_task_get_priority (task),
g_task_get_cancellable (task),
init_second_async_cb,
task);
}
if (data->cancellable != NULL)
g_object_unref (data->cancellable);
g_object_unref (data->proxy);
g_free (data);
}
static void
@@ -1880,28 +1842,25 @@ async_initable_init_async (GAsyncInitable *initable,
gpointer user_data)
{
GDBusProxy *proxy = G_DBUS_PROXY (initable);
GTask *task;
task = g_task_new (proxy, cancellable, callback, user_data);
g_task_set_priority (task, io_priority);
if (proxy->priv->bus_type != G_BUS_TYPE_NONE)
{
GetConnectionData *data;
g_assert (proxy->priv->connection == NULL);
data = g_new0 (GetConnectionData, 1);
data->proxy = g_object_ref (proxy);
data->io_priority = io_priority;
data->cancellable = cancellable != NULL ? g_object_ref (cancellable) : NULL;
data->callback = callback;
data->user_data = user_data;
g_bus_get (proxy->priv->bus_type,
cancellable,
get_connection_cb,
data);
task);
}
else
{
async_initable_init_first (initable);
async_initable_init_second_async (initable, io_priority, cancellable, callback, user_data);
async_initable_init_second_async (initable, io_priority, cancellable,
init_second_async_cb, task);
}
}
@@ -1910,7 +1869,7 @@ async_initable_init_finish (GAsyncInitable *initable,
GAsyncResult *res,
GError **error)
{
return async_initable_init_second_finish (initable, res, error);
return g_task_propagate_boolean (G_TASK (res), error);
}
static void
@@ -2586,7 +2545,7 @@ reply_cb (GDBusConnection *connection,
GAsyncResult *res,
gpointer user_data)
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
GTask *task = user_data;
GVariant *value;
GError *error;
#ifdef G_OS_UNIX
@@ -2606,7 +2565,7 @@ reply_cb (GDBusConnection *connection,
#endif
if (error != NULL)
{
g_simple_async_result_take_error (simple, error);
g_task_return_error (task, error);
}
else
{
@@ -2616,12 +2575,10 @@ reply_cb (GDBusConnection *connection,
#ifdef G_OS_UNIX
data->fd_list = fd_list;
#endif
g_simple_async_result_set_op_res_gpointer (simple, data, (GDestroyNotify) reply_data_free);
g_task_return_pointer (task, data, (GDestroyNotify) reply_data_free);
}
/* no need to complete in idle since the method GDBusConnection already does */
g_simple_async_result_complete (simple);
g_object_unref (simple);
g_object_unref (task);
}
/* properties_lock must be held for as long as you will keep the
@@ -2683,7 +2640,7 @@ g_dbus_proxy_call_internal (GDBusProxy *proxy,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *simple;
GTask *task;
gboolean was_split;
gchar *split_interface_name;
const gchar *split_method_name;
@@ -2713,16 +2670,12 @@ g_dbus_proxy_call_internal (GDBusProxy *proxy,
if (callback != NULL)
{
my_callback = (GAsyncReadyCallback) reply_cb;
simple = g_simple_async_result_new (G_OBJECT (proxy),
callback,
user_data,
g_dbus_proxy_call_internal);
g_simple_async_result_set_check_cancellable (simple, cancellable);
task = g_task_new (proxy, cancellable, callback, user_data);
}
else
{
my_callback = NULL;
simple = NULL;
task = NULL;
}
G_LOCK (properties_lock);
@@ -2746,14 +2699,13 @@ g_dbus_proxy_call_internal (GDBusProxy *proxy,
destination = g_strdup (get_destination_for_call (proxy));
if (destination == NULL)
{
if (simple != NULL)
if (task != NULL)
{
g_simple_async_result_set_error (simple,
G_IO_ERROR,
G_IO_ERROR_FAILED,
_("Cannot invoke method; proxy is for a well-known name without an owner and proxy was constructed with the G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag"));
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
g_task_return_new_error (task,
G_IO_ERROR,
G_IO_ERROR_FAILED,
_("Cannot invoke method; proxy is for a well-known name without an owner and proxy was constructed with the G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START flag"));
g_object_unref (task);
}
G_UNLOCK (properties_lock);
goto out;
@@ -2775,7 +2727,7 @@ g_dbus_proxy_call_internal (GDBusProxy *proxy,
fd_list,
cancellable,
my_callback,
simple);
task);
#else
g_dbus_connection_call (proxy->priv->connection,
destination,
@@ -2788,7 +2740,7 @@ g_dbus_proxy_call_internal (GDBusProxy *proxy,
timeout_msec == -1 ? proxy->priv->timeout_msec : timeout_msec,
cancellable,
my_callback,
simple);
task);
#endif
out:
@@ -2805,27 +2757,25 @@ g_dbus_proxy_call_finish_internal (GDBusProxy *proxy,
GAsyncResult *res,
GError **error)
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
GVariant *value;
ReplyData *data;
g_return_val_if_fail (G_IS_DBUS_PROXY (proxy), NULL);
g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
g_return_val_if_fail (g_task_is_valid (res, proxy), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_dbus_proxy_call_internal);
value = NULL;
if (g_simple_async_result_propagate_error (simple, error))
data = g_task_propagate_pointer (G_TASK (res), error);
if (!data)
goto out;
data = g_simple_async_result_get_op_res_gpointer (simple);
value = g_variant_ref (data->value);
#ifdef G_OS_UNIX
if (out_fd_list != NULL)
*out_fd_list = data->fd_list != NULL ? g_object_ref (data->fd_list) : NULL;
#endif
reply_data_free (data);
out:
return value;