Commit d2130946 authored by Dan Winship's avatar Dan Winship

gio: port networking classes from GSimpleAsyncResult to GTask

https://bugzilla.gnome.org/show_bug.cgi?id=661767
parent 130d0fda
......@@ -29,7 +29,7 @@
#include "gasyncresult.h"
#include "gcancellable.h"
#include "gproxyresolver.h"
#include "gsimpleasyncresult.h"
#include "gtask.h"
#include "giomodule.h"
#include "giomodule-priv.h"
......@@ -93,29 +93,17 @@ g_dummy_proxy_resolver_lookup_async (GProxyResolver *resolver,
gpointer user_data)
{
GError *error = NULL;
GSimpleAsyncResult *simple;
GTask *task;
gchar **proxies;
proxies = g_dummy_proxy_resolver_lookup (resolver, uri, cancellable, &error);
task = g_task_new (resolver, cancellable, callback, user_data);
simple = g_simple_async_result_new (G_OBJECT (resolver),
callback, user_data,
g_dummy_proxy_resolver_lookup_async);
if (proxies == NULL)
{
g_simple_async_result_take_error (simple, error);
}
proxies = g_dummy_proxy_resolver_lookup (resolver, uri, cancellable, &error);
if (proxies)
g_task_return_pointer (task, proxies, (GDestroyNotify) g_strfreev);
else
{
g_simple_async_result_set_op_res_gpointer (simple,
proxies,
NULL);
}
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
g_task_return_error (task, error);
g_object_unref (task);
}
static gchar **
......@@ -123,17 +111,9 @@ g_dummy_proxy_resolver_lookup_finish (GProxyResolver *resolver,
GAsyncResult *result,
GError **error)
{
if (G_IS_SIMPLE_ASYNC_RESULT (result))
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
if (g_simple_async_result_propagate_error (simple, error))
return NULL;
return g_simple_async_result_get_op_res_gpointer (simple);
}
g_return_val_if_fail (g_task_is_valid (result, resolver), NULL);
return NULL;
return g_task_propagate_pointer (G_TASK (result), error);
}
static void
......
......@@ -32,7 +32,7 @@
#include "gnetworkingprivate.h"
#include "gproxyaddressenumerator.h"
#include "gresolver.h"
#include "gsimpleasyncresult.h"
#include "gtask.h"
#include "gsocketaddressenumerator.h"
#include "gioerror.h"
#include "gsocketconnectable.h"
......@@ -852,19 +852,19 @@ got_addresses (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GSimpleAsyncResult *simple = user_data;
GNetworkAddressAddressEnumerator *addr_enum =
g_simple_async_result_get_op_res_gpointer (simple);
GTask *task = user_data;
GNetworkAddressAddressEnumerator *addr_enum = g_task_get_source_object (task);
GResolver *resolver = G_RESOLVER (source_object);
GList *addresses;
GError *error = NULL;
GSocketAddress *sockaddr;
if (!addr_enum->addr->priv->sockaddrs)
{
addresses = g_resolver_lookup_by_name_finish (resolver, result, &error);
if (error)
g_simple_async_result_take_error (simple, error);
g_task_return_error (task, error);
else
g_network_address_set_addresses (addr_enum->addr, addresses);
}
......@@ -874,8 +874,17 @@ got_addresses (GObject *source_object,
addr_enum->addresses = addr_enum->addr->priv->sockaddrs;
addr_enum->next = addr_enum->addresses;
g_simple_async_result_complete (simple);
g_object_unref (simple);
if (addr_enum->next)
{
sockaddr = g_object_ref (addr_enum->next->data);
addr_enum->next = addr_enum->next->next;
}
else
sockaddr = NULL;
if (!error)
g_task_return_pointer (task, sockaddr, g_object_unref);
g_object_unref (task);
}
static void
......@@ -886,11 +895,10 @@ g_network_address_address_enumerator_next_async (GSocketAddressEnumerator *enum
{
GNetworkAddressAddressEnumerator *addr_enum =
G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
GSimpleAsyncResult *simple;
GSocketAddress *sockaddr;
GTask *task;
simple = g_simple_async_result_new (G_OBJECT (enumerator),
callback, user_data,
g_network_address_address_enumerator_next_async);
task = g_task_new (addr_enum, cancellable, callback, user_data);
if (addr_enum->addresses == NULL)
{
......@@ -898,11 +906,10 @@ g_network_address_address_enumerator_next_async (GSocketAddressEnumerator *enum
{
GResolver *resolver = g_resolver_get_default ();
g_simple_async_result_set_op_res_gpointer (simple, g_object_ref (addr_enum), g_object_unref);
g_resolver_lookup_by_name_async (resolver,
addr_enum->addr->priv->hostname,
cancellable,
got_addresses, simple);
got_addresses, task);
return;
}
......@@ -910,8 +917,16 @@ g_network_address_address_enumerator_next_async (GSocketAddressEnumerator *enum
addr_enum->next = addr_enum->addresses;
}
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
if (addr_enum->next)
{
sockaddr = g_object_ref (addr_enum->next->data);
addr_enum->next = addr_enum->next->next;
}
else
sockaddr = NULL;
g_task_return_pointer (task, sockaddr, g_object_unref);
g_object_unref (task);
}
static GSocketAddress *
......@@ -919,21 +934,9 @@ g_network_address_address_enumerator_next_finish (GSocketAddressEnumerator *enu
GAsyncResult *result,
GError **error)
{
GNetworkAddressAddressEnumerator *addr_enum =
G_NETWORK_ADDRESS_ADDRESS_ENUMERATOR (enumerator);
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
GSocketAddress *sockaddr;
g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL);
if (g_simple_async_result_propagate_error (simple, error))
return NULL;
else if (!addr_enum->next)
return NULL;
else
{
sockaddr = addr_enum->next->data;
addr_enum->next = addr_enum->next->next;
return g_object_ref (sockaddr);
}
return g_task_propagate_pointer (G_TASK (result), error);
}
static void
......
......@@ -28,7 +28,7 @@
#include "ginitable.h"
#include "gioenumtypes.h"
#include "giomodule-priv.h"
#include "gsimpleasyncresult.h"
#include "gtask.h"
/**
* SECTION:gnetworkmonitor
......@@ -148,18 +148,15 @@ g_network_monitor_real_can_reach_async (GNetworkMonitor *monitor,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *simple;
GTask *task;
GError *error = NULL;
simple = g_simple_async_result_new (G_OBJECT (monitor),
callback, user_data,
g_network_monitor_real_can_reach_async);
task = g_task_new (monitor, cancellable, callback, user_data);
if (g_network_monitor_can_reach (monitor, connectable, cancellable, &error))
g_simple_async_result_set_op_res_gboolean (simple, TRUE);
g_task_return_boolean (task, TRUE);
else
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);
}
/**
......@@ -199,15 +196,9 @@ g_network_monitor_real_can_reach_finish (GNetworkMonitor *monitor,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
g_return_val_if_fail (g_task_is_valid (result, monitor), FALSE);
g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (monitor), g_network_monitor_real_can_reach_async), FALSE);
simple = G_SIMPLE_ASYNC_RESULT (result);
if (g_simple_async_result_propagate_error (simple, error))
return FALSE;
else
return g_simple_async_result_get_op_res_gboolean (simple);
return g_task_propagate_boolean (G_TASK (result), error);
}
/**
......
......@@ -33,7 +33,7 @@
#include "gnetworkaddress.h"
#include "gnetworkingprivate.h"
#include "gresolver.h"
#include "gsimpleasyncresult.h"
#include "gtask.h"
#include "gsocketaddressenumerator.h"
#include "gsocketconnectable.h"
#include "gsrvtarget.h"
......@@ -395,9 +395,6 @@ typedef struct {
GError *error;
/* For async operation */
GCancellable *cancellable;
GSimpleAsyncResult *result;
} GNetworkServiceAddressEnumerator;
typedef struct {
......@@ -530,13 +527,13 @@ g_network_service_address_enumerator_next (GSocketAddressEnumerator *enumerator
return ret;
}
static void next_async_resolved_targets (GObject *source_object,
GAsyncResult *result,
gpointer user_data);
static void next_async_have_targets (GNetworkServiceAddressEnumerator *srv_enum);
static void next_async_have_address (GObject *source_object,
GAsyncResult *result,
gpointer user_data);
static void next_async_resolved_targets (GObject *source_object,
GAsyncResult *result,
gpointer user_data);
static void next_async_have_targets (GTask *srv_enum);
static void next_async_have_address (GObject *source_object,
GAsyncResult *result,
gpointer user_data);
static void
g_network_service_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
......@@ -546,15 +543,9 @@ g_network_service_address_enumerator_next_async (GSocketAddressEnumerator *enum
{
GNetworkServiceAddressEnumerator *srv_enum =
G_NETWORK_SERVICE_ADDRESS_ENUMERATOR (enumerator);
GTask *task;
g_return_if_fail (srv_enum->result == NULL);
srv_enum->result = g_simple_async_result_new (G_OBJECT (enumerator),
callback, user_data,
g_network_service_address_enumerator_next_async);
if (cancellable)
srv_enum->cancellable = g_object_ref (cancellable);
task = g_task_new (enumerator, cancellable, callback, user_data);
/* If we haven't yet resolved srv, do that */
if (!srv_enum->srv->priv->targets)
......@@ -565,10 +556,10 @@ g_network_service_address_enumerator_next_async (GSocketAddressEnumerator *enum
srv_enum->srv->priv->domain,
cancellable,
next_async_resolved_targets,
srv_enum);
task);
}
else
next_async_have_targets (srv_enum);
next_async_have_targets (task);
}
static void
......@@ -576,7 +567,8 @@ next_async_resolved_targets (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GNetworkServiceAddressEnumerator *srv_enum = user_data;
GTask *task = user_data;
GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
GError *error = NULL;
GList *targets;
......@@ -593,23 +585,21 @@ next_async_resolved_targets (GObject *source_object,
if (error)
{
GSimpleAsyncResult *simple = srv_enum->result;
srv_enum->result = NULL;
g_simple_async_result_take_error (simple, error);
g_simple_async_result_complete (simple);
g_object_unref (simple);
g_task_return_error (task, error);
g_object_unref (task);
}
else
{
srv_enum->t = srv_enum->srv->priv->targets = targets;
next_async_have_targets (srv_enum);
next_async_have_targets (task);
}
}
static void
next_async_have_targets (GNetworkServiceAddressEnumerator *srv_enum)
next_async_have_targets (GTask *task)
{
GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
/* Delegate to GNetworkAddress */
if (srv_enum->addr_enum == NULL && srv_enum->t)
{
......@@ -631,23 +621,21 @@ next_async_have_targets (GNetworkServiceAddressEnumerator *srv_enum)
if (srv_enum->addr_enum)
{
g_socket_address_enumerator_next_async (srv_enum->addr_enum,
srv_enum->cancellable,
g_task_get_cancellable (task),
next_async_have_address,
srv_enum);
task);
}
else
{
GSimpleAsyncResult *simple = srv_enum->result;
srv_enum->result = NULL;
if (srv_enum->error)
{
g_simple_async_result_take_error (simple, srv_enum->error);
g_task_return_error (task, srv_enum->error);
srv_enum->error = NULL;
}
else
g_task_return_pointer (task, NULL, NULL);
g_simple_async_result_complete (simple);
g_object_unref (simple);
g_object_unref (task);
}
}
......@@ -656,7 +644,8 @@ next_async_have_address (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GNetworkServiceAddressEnumerator *srv_enum = user_data;
GTask *task = user_data;
GNetworkServiceAddressEnumerator *srv_enum = g_task_get_source_object (task);
GSocketAddress *address;
GError *error = NULL;
......@@ -677,20 +666,12 @@ next_async_have_address (GObject *source_object,
g_object_unref (srv_enum->addr_enum);
srv_enum->addr_enum = NULL;
next_async_have_targets (srv_enum);
next_async_have_targets (task);
}
else
{
GSimpleAsyncResult *simple = srv_enum->result;
srv_enum->result = NULL;
if (address)
g_simple_async_result_set_op_res_gpointer (simple,
address, g_object_unref);
g_simple_async_result_complete (simple);
g_object_unref (simple);
g_task_return_pointer (task, address, g_object_unref);
g_object_unref (task);
}
}
......@@ -699,14 +680,7 @@ g_network_service_address_enumerator_next_finish (GSocketAddressEnumerator *enu
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
GSocketAddress *sockaddr;
if (g_simple_async_result_propagate_error (simple, error))
return NULL;
sockaddr = g_simple_async_result_get_op_res_gpointer (simple);
return sockaddr ? g_object_ref (sockaddr) : NULL;
return g_task_propagate_pointer (G_TASK (result), error);
}
static void
......@@ -729,12 +703,6 @@ g_network_service_address_enumerator_finalize (GObject *object)
if (srv_enum->resolver)
g_object_unref (srv_enum->resolver);
if (srv_enum->result)
g_object_unref (srv_enum->result);
if (srv_enum->cancellable)
g_object_unref (srv_enum->cancellable);
if (srv_enum->error)
g_error_free (srv_enum->error);
......
......@@ -33,7 +33,7 @@
#include "gproxy.h"
#include "gproxyaddress.h"
#include "gproxyresolver.h"
#include "gsimpleasyncresult.h"
#include "gtask.h"
#include "gresolver.h"
#include "gsocketaddress.h"
#include "gsocketaddressenumerator.h"
......@@ -71,10 +71,6 @@ struct _GProxyAddressEnumeratorPrivate
gboolean supports_hostname;
GList *next_dest_ip;
GError *last_error;
/* Async attributes */
GSimpleAsyncResult *simple;
GCancellable *cancellable;
};
static void
......@@ -313,31 +309,25 @@ g_proxy_address_enumerator_next (GSocketAddressEnumerator *enumerator,
static void
complete_async (GProxyAddressEnumeratorPrivate *priv)
complete_async (GTask *task)
{
GSimpleAsyncResult *simple = priv->simple;
if (priv->cancellable)
{
g_object_unref (priv->cancellable);
priv->cancellable = NULL;
}
priv->simple = NULL;
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
if (priv->last_error)
{
g_simple_async_result_take_error (simple, priv->last_error);
g_task_return_error (task, priv->last_error);
priv->last_error = NULL;
}
else
g_task_return_pointer (task, NULL, NULL);
g_simple_async_result_complete (simple);
g_object_unref (simple);
g_object_unref (task);
}
static void
save_result (GProxyAddressEnumeratorPrivate *priv)
return_result (GTask *task)
{
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
GSocketAddress *result;
if (strcmp ("direct", priv->proxy_type) == 0)
......@@ -397,9 +387,8 @@ save_result (GProxyAddressEnumeratorPrivate *priv)
}
}
g_simple_async_result_set_op_res_gpointer (priv->simple,
result,
g_object_unref);
g_task_return_pointer (task, result, g_object_unref);
g_object_unref (task);
}
static void address_enumerate_cb (GObject *object,
......@@ -407,8 +396,10 @@ static void address_enumerate_cb (GObject *object,
gpointer user_data);
static void
next_proxy (GProxyAddressEnumeratorPrivate *priv)
next_proxy (GTask *task)
{
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
if (*priv->next_proxy)
{
g_object_unref (priv->addr_enum);
......@@ -425,14 +416,14 @@ next_proxy (GProxyAddressEnumeratorPrivate *priv)
if (priv->addr_enum)
{
g_socket_address_enumerator_next_async (priv->addr_enum,
priv->cancellable,
g_task_get_cancellable (task),
address_enumerate_cb,
priv);
task);
return;
}
}
complete_async (priv);
complete_async (task);
}
static void
......@@ -440,21 +431,19 @@ dest_hostname_lookup_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
GProxyAddressEnumeratorPrivate *priv = user_data;
GTask *task = user_data;
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
g_clear_error (&priv->last_error);
priv->dest_ips = g_resolver_lookup_by_name_finish (G_RESOLVER (object),
result,
&priv->last_error);
if (priv->dest_ips)
{
save_result (priv);
complete_async (priv);
}
return_result (task);
else
{
g_clear_object (&priv->proxy_address);
next_proxy (priv);
next_proxy (task);
}
}
......@@ -463,7 +452,8 @@ address_enumerate_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
GProxyAddressEnumeratorPrivate *priv = user_data;
GTask *task = user_data;
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
g_clear_error (&priv->last_error);
priv->proxy_address =
......@@ -478,18 +468,17 @@ address_enumerate_cb (GObject *object,
resolver = g_resolver_get_default();
g_resolver_lookup_by_name_async (resolver,
priv->dest_hostname,
priv->cancellable,
g_task_get_cancellable (task),
dest_hostname_lookup_cb,
priv);
task);
g_object_unref (resolver);
return;
}
save_result (priv);
complete_async (priv);
return_result (task);
}
else
next_proxy (priv);
next_proxy (task);
}
static void
......@@ -497,18 +486,19 @@ proxy_lookup_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
GError *error = NULL;
GProxyAddressEnumeratorPrivate *priv = user_data;
GSimpleAsyncResult *simple = priv->simple;
GTask *task = user_data;
GProxyAddressEnumeratorPrivate *priv = g_task_get_task_data (task);
g_clear_error (&priv->last_error);
priv->proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (object),
result,
&error);
&priv->last_error);
priv->next_proxy = priv->proxies;
if (error)
if (priv->last_error)
{
g_simple_async_result_take_error (simple, error);
complete_async (task);
return;
}
else
{
......@@ -516,14 +506,14 @@ proxy_lookup_cb (GObject *object,
if (priv->addr_enum)
{
g_socket_address_enumerator_next_async (priv->addr_enum,
priv->cancellable,
g_task_get_cancellable (task),
address_enumerate_cb,
priv);
task);
return;
}
}
complete_async (priv);
complete_async (task);
}
static void
......@@ -533,15 +523,10 @@ g_proxy_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
gpointer user_data)
{
GProxyAddressEnumeratorPrivate *priv = GET_PRIVATE (enumerator);
GTask *task;
g_return_if_fail (priv->simple == NULL);
g_return_if_fail (priv->cancellable == NULL);
priv->simple = g_simple_async_result_new (G_OBJECT (enumerator),
callback, user_data,
g_proxy_address_enumerator_next_async);
priv->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
task = g_task_new (enumerator, cancellable, callback, user_data);
g_task_set_task_data (task, priv, NULL);
if (priv->proxies == NULL)
{
......@@ -550,7 +535,7 @@ g_proxy_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
priv->dest_uri,
cancellable,
proxy_lookup_cb,
priv);
task);
return;
}
......@@ -558,28 +543,20 @@ g_proxy_address_enumerator_next_async (GSocketAddressEnumerator *enumerator,
{
if (priv->proxy_address)
{
save_result (priv);
return_result (task);
return;
}
else
{
g_socket_address_enumerator_next_async (priv->addr_enum,
cancellable,
address_enumerate_cb,
priv);
task);
return;
}
}
g_simple_async_result_complete_in_idle (priv->simple);
g_object_unref (priv->simple);
priv->simple = NULL;
if (priv->cancellable)
{
g_object_unref (priv->cancellable);
priv->cancellable = NULL;
}
complete_async (task);
}
static GSocketAddress *
......@@ -587,17 +564,9 @@ g_proxy_address_enumerator_next_finish (GSocketAddressEnumerator *enumerator,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
GSocketAddress *address;
if (g_simple_async_result_propagate_error (simple, error))
return NULL;
g_return_val_if_fail (g_task_is_valid (result, enumerator), NULL);
address = g_simple_async_result_get_op_res_gpointer (simple);
if (address)
g_object_ref (address);
return address;
return g_task_propagate_pointer (G_TASK (result), error);
}
static void
......@@ -702,9 +671,6 @@ g_proxy_address_enumerator_finalize (GObject *object)
g_free (priv->proxy_username);
g_free (priv->proxy_password);
if (priv->cancellable)
g_object_unref (priv->cancellable);
g_clear_error (&priv->last_error);
G_OBJECT_CLASS (g_proxy_address_enumerator_parent_class)->finalize (object);
......
......@@ -30,6 +30,7 @@
#include "ginetaddress.h"
#include "ginetsocketaddress.h"
#include "gsimpleasyncresult.h"
#include "gtask.h"
#include "gsrvtarget.h"
#include "gthreadedresolver.h"
......@@ -395,15 +396,13 @@ g_resolver_lookup_by_name_async (GResolver *resolver,
addr = g_inet_address_new_from_string (hostname);
if (addr)
{
GSimpleAsyncResult *simple;
GTask *task;
simple = g_simple_async_result_new (G_OBJECT (resolver),
callback, user_data,
g_resolver_lookup_by_name_async);
g_simple_async_result_set_op_res_gpointer (simple, addr, g_object_unref);
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
task = g_task_new (resolver, cancellable, callback, user_data);