[glib/wip/task: 9/12] gio: port TLS classes from GSimpleAsyncResult to GTask



commit 4ac0f6d757d3e4b8a060800dd83f1087a404c59a
Author: Dan Winship <danw gnome org>
Date:   Thu Aug 2 15:49:06 2012 -0400

    gio: port TLS classes from GSimpleAsyncResult to GTask
    
    NOTE: Needs maintainer sanity-checking before being merged to master.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=661767

 gio/gtlsdatabase.c    |  247 +++++++++++++++++++++----------------------------
 gio/gtlsinteraction.c |   27 +++---
 2 files changed, 116 insertions(+), 158 deletions(-)
---
diff --git a/gio/gtlsdatabase.c b/gio/gtlsdatabase.c
index caab282..9e2b5b5 100644
--- a/gio/gtlsdatabase.c
+++ b/gio/gtlsdatabase.c
@@ -27,8 +27,8 @@
 #include "gasyncresult.h"
 #include "gcancellable.h"
 #include "glibintl.h"
-#include "gsimpleasyncresult.h"
 #include "gsocketconnectable.h"
+#include "gtask.h"
 #include "gtlscertificate.h"
 #include "gtlsinteraction.h"
 
@@ -89,7 +89,6 @@ typedef struct _AsyncVerifyChain {
   GSocketConnectable *identity;
   GTlsInteraction *interaction;
   GTlsDatabaseVerifyFlags flags;
-  GTlsCertificateFlags verify_result;
 } AsyncVerifyChain;
 
 static void
@@ -104,24 +103,27 @@ async_verify_chain_free (gpointer data)
 }
 
 static void
-async_verify_chain_thread (GSimpleAsyncResult *res,
-                           GObject            *object,
-                           GCancellable       *cancellable)
+async_verify_chain_thread (GTask         *task,
+                           gpointer       object,
+                           gpointer       task_data,
+                           GCancellable  *cancellable)
 {
-  AsyncVerifyChain *args = g_simple_async_result_get_op_res_gpointer (res);
+  AsyncVerifyChain *args = task_data;
+  GTlsCertificateFlags verify_result;
   GError *error = NULL;
 
-  args->verify_result = g_tls_database_verify_chain (G_TLS_DATABASE (object),
-                                                     args->chain,
-                                                     args->purpose,
-                                                     args->identity,
-                                                     args->interaction,
-                                                     args->flags,
-                                                     cancellable,
-                                                     &error);
-
+  verify_result = g_tls_database_verify_chain (G_TLS_DATABASE (object),
+                                               args->chain,
+                                               args->purpose,
+                                               args->identity,
+                                               args->interaction,
+                                               args->flags,
+                                               cancellable,
+                                               &error);
   if (error)
-      g_simple_async_result_take_error (res, error);
+    g_task_return_error (task, error);
+  else
+    g_task_return_int (task, (gssize)verify_result);
 }
 
 static void
@@ -135,7 +137,7 @@ g_tls_database_real_verify_chain_async (GTlsDatabase           *self,
                                         GAsyncReadyCallback     callback,
                                         gpointer                user_data)
 {
-  GSimpleAsyncResult *res;
+  GTask *task;
   AsyncVerifyChain *args;
 
   args = g_slice_new0 (AsyncVerifyChain);
@@ -145,12 +147,10 @@ g_tls_database_real_verify_chain_async (GTlsDatabase           *self,
   args->interaction = interaction ? g_object_ref (interaction) : NULL;
   args->flags = flags;
 
-  res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
-                                   g_tls_database_real_verify_chain_async);
-  g_simple_async_result_set_op_res_gpointer (res, args, async_verify_chain_free);
-  g_simple_async_result_run_in_thread (res, async_verify_chain_thread,
-                                       G_PRIORITY_DEFAULT, cancellable);
-  g_object_unref (res);
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_task_data (task, args, async_verify_chain_free);
+  g_task_run_in_thread (task, async_verify_chain_thread);
+  g_object_unref (task);
 }
 
 static GTlsCertificateFlags
@@ -158,24 +158,15 @@ g_tls_database_real_verify_chain_finish (GTlsDatabase          *self,
                                          GAsyncResult          *result,
                                          GError               **error)
 {
-  AsyncVerifyChain *args;
-
-  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), G_TLS_CERTIFICATE_GENERIC_ERROR);
-  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self),
-                        g_tls_database_real_verify_chain_async), FALSE);
+  g_return_val_if_fail (g_task_is_valid (result, self), G_TLS_CERTIFICATE_GENERIC_ERROR);
 
-  if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
-    return G_TLS_CERTIFICATE_GENERIC_ERROR;
-
-  args = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result));
-  return args->verify_result;
+  return (GTlsCertificateFlags)g_task_propagate_int (G_TASK (result), error);
 }
 
 typedef struct {
   gchar *handle;
   GTlsInteraction *interaction;
   GTlsDatabaseLookupFlags flags;
-  GTlsCertificate *result;
 } AsyncLookupCertificateForHandle;
 
 static void
@@ -185,27 +176,29 @@ async_lookup_certificate_for_handle_free (gpointer data)
 
   g_free (args->handle);
   g_clear_object (&args->interaction);
-  g_clear_object (&args->result);
   g_slice_free (AsyncLookupCertificateForHandle, args);
 }
 
 static void
-async_lookup_certificate_for_handle_thread (GSimpleAsyncResult *res,
-                                            GObject            *object,
-                                            GCancellable       *cancellable)
+async_lookup_certificate_for_handle_thread (GTask         *task,
+                                            gpointer       object,
+                                            gpointer       task_data,
+                                            GCancellable  *cancellable)
 {
-  AsyncLookupCertificateForHandle *args = g_simple_async_result_get_op_res_gpointer (res);
+  AsyncLookupCertificateForHandle *args = task_data;
+  GTlsCertificate *result;
   GError *error = NULL;
 
-  args->result = g_tls_database_lookup_certificate_for_handle (G_TLS_DATABASE (object),
-                                                               args->handle,
-                                                               args->interaction,
-                                                               args->flags,
-                                                               cancellable,
-                                                               &error);
-
-  if (error)
-      g_simple_async_result_take_error (res, error);
+  result = g_tls_database_lookup_certificate_for_handle (G_TLS_DATABASE (object),
+                                                         args->handle,
+                                                         args->interaction,
+                                                         args->flags,
+                                                         cancellable,
+                                                         &error);
+  if (result)
+    g_task_return_pointer (task, result, g_object_unref);
+  else
+    g_task_return_error (task, error);
 }
 
 static void
@@ -217,19 +210,17 @@ g_tls_database_real_lookup_certificate_for_handle_async (GTlsDatabase
                                                          GAsyncReadyCallback     callback,
                                                          gpointer                user_data)
 {
-  GSimpleAsyncResult *res;
+  GTask *task;
   AsyncLookupCertificateForHandle *args;
 
   args = g_slice_new0 (AsyncLookupCertificateForHandle);
   args->handle = g_strdup (handle);
   args->interaction = interaction ? g_object_ref (interaction) : NULL;
 
-  res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
-                                   g_tls_database_real_lookup_certificate_for_handle_async);
-  g_simple_async_result_set_op_res_gpointer (res, args, async_lookup_certificate_for_handle_free);
-  g_simple_async_result_run_in_thread (res, async_lookup_certificate_for_handle_thread,
-                                       G_PRIORITY_DEFAULT, cancellable);
-  g_object_unref (res);
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_task_data (task, args, async_lookup_certificate_for_handle_free);
+  g_task_run_in_thread (task, async_lookup_certificate_for_handle_thread);
+  g_object_unref (task);
 }
 
 static GTlsCertificate*
@@ -237,20 +228,9 @@ g_tls_database_real_lookup_certificate_for_handle_finish (GTlsDatabase
                                                           GAsyncResult          *result,
                                                           GError               **error)
 {
-  AsyncLookupCertificateForHandle *args;
-  GTlsCertificate *certificate;
-
-  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
-  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self),
-                        g_tls_database_real_lookup_certificate_for_handle_async), FALSE);
+  g_return_val_if_fail (g_task_is_valid (result, self), NULL);
 
-  if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
-    return NULL;
-
-  args = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result));
-  certificate = args->result;
-  args->result = NULL;
-  return certificate;
+  return g_task_propagate_pointer (G_TASK (result), error);
 }
 
 
@@ -258,7 +238,6 @@ typedef struct {
   GTlsCertificate *certificate;
   GTlsInteraction *interaction;
   GTlsDatabaseLookupFlags flags;
-  GTlsCertificate *issuer;
 } AsyncLookupCertificateIssuer;
 
 static void
@@ -268,27 +247,29 @@ async_lookup_certificate_issuer_free (gpointer data)
 
   g_clear_object (&args->certificate);
   g_clear_object (&args->interaction);
-  g_clear_object (&args->issuer);
   g_slice_free (AsyncLookupCertificateIssuer, args);
 }
 
 static void
-async_lookup_certificate_issuer_thread (GSimpleAsyncResult *res,
-                            GObject            *object,
-                            GCancellable       *cancellable)
+async_lookup_certificate_issuer_thread (GTask         *task,
+                                        gpointer       object,
+                                        gpointer       task_data,
+                                        GCancellable  *cancellable)
 {
-  AsyncLookupCertificateIssuer *args = g_simple_async_result_get_op_res_gpointer (res);
+  AsyncLookupCertificateIssuer *args = task_data;
+  GTlsCertificate *issuer;
   GError *error = NULL;
 
-  args->issuer = g_tls_database_lookup_certificate_issuer (G_TLS_DATABASE (object),
-                                                           args->certificate,
-                                                           args->interaction,
-                                                           args->flags,
-                                                           cancellable,
-                                                           &error);
-
-  if (error)
-      g_simple_async_result_take_error (res, error);
+  issuer = g_tls_database_lookup_certificate_issuer (G_TLS_DATABASE (object),
+                                                     args->certificate,
+                                                     args->interaction,
+                                                     args->flags,
+                                                     cancellable,
+                                                     &error);
+  if (issuer)
+    g_task_return_pointer (task, issuer, g_object_unref);
+  else
+    g_task_return_error (task, error);
 }
 
 static void
@@ -300,7 +281,7 @@ g_tls_database_real_lookup_certificate_issuer_async (GTlsDatabase           *sel
                                                      GAsyncReadyCallback     callback,
                                                      gpointer                user_data)
 {
-  GSimpleAsyncResult *res;
+  GTask *task;
   AsyncLookupCertificateIssuer *args;
 
   args = g_slice_new0 (AsyncLookupCertificateIssuer);
@@ -308,73 +289,66 @@ g_tls_database_real_lookup_certificate_issuer_async (GTlsDatabase           *sel
   args->flags = flags;
   args->interaction = interaction ? g_object_ref (interaction) : NULL;
 
-  res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
-                                   g_tls_database_real_lookup_certificate_issuer_async);
-  g_simple_async_result_set_op_res_gpointer (res, args, async_lookup_certificate_issuer_free);
-  g_simple_async_result_run_in_thread (res, async_lookup_certificate_issuer_thread,
-                                       G_PRIORITY_DEFAULT, cancellable);
-  g_object_unref (res);
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_task_data (task, args, async_lookup_certificate_issuer_free);
+  g_task_run_in_thread (task, async_lookup_certificate_issuer_thread);
+  g_object_unref (task);
 }
 
-static GTlsCertificate*
+static GTlsCertificate *
 g_tls_database_real_lookup_certificate_issuer_finish (GTlsDatabase          *self,
                                                       GAsyncResult          *result,
                                                       GError               **error)
 {
-  AsyncLookupCertificateIssuer *args;
-  GTlsCertificate *issuer;
-
-  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
-  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self),
-                        g_tls_database_real_lookup_certificate_issuer_async), FALSE);
-
-  if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
-    return NULL;
+  g_return_val_if_fail (g_task_is_valid (result, self), NULL);
 
-  args = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result));
-  issuer = args->issuer;
-  args->issuer = NULL;
-  return issuer;
+  return g_task_propagate_pointer (G_TASK (result), error);
 }
 
 typedef struct {
   GByteArray *issuer;
   GTlsInteraction *interaction;
   GTlsDatabaseLookupFlags flags;
-  GList *results;
 } AsyncLookupCertificatesIssuedBy;
 
 static void
 async_lookup_certificates_issued_by_free (gpointer data)
 {
   AsyncLookupCertificatesIssuedBy *args = data;
-  GList *l;
 
   g_byte_array_unref (args->issuer);
   g_clear_object (&args->interaction);
-  for (l = args->results; l; l = g_list_next (l))
-    g_object_unref (l->data);
-  g_list_free (args->results);
   g_slice_free (AsyncLookupCertificatesIssuedBy, args);
 }
 
 static void
-async_lookup_certificates_issued_by_thread (GSimpleAsyncResult *res,
-                                            GObject            *object,
-                                            GCancellable       *cancellable)
+async_lookup_certificates_free_certificates (gpointer data)
 {
-  AsyncLookupCertificatesIssuedBy *args = g_simple_async_result_get_op_res_gpointer (res);
-  GError *error = NULL;
+  GList *list = data;
 
-  args->results = g_tls_database_lookup_certificates_issued_by (G_TLS_DATABASE (object),
-                                                                args->issuer,
-                                                                args->interaction,
-                                                                args->flags,
-                                                                cancellable,
-                                                                &error);
+  g_list_free_full (list, g_object_unref);
+}
 
-  if (error)
-      g_simple_async_result_take_error (res, error);
+static void
+async_lookup_certificates_issued_by_thread (GTask         *task,
+                                            gpointer       object,
+                                            gpointer       task_data,
+                                            GCancellable  *cancellable)
+{
+  AsyncLookupCertificatesIssuedBy *args = task_data;
+  GList *results;
+  GError *error = NULL;
+
+  results = g_tls_database_lookup_certificates_issued_by (G_TLS_DATABASE (object),
+                                                          args->issuer,
+                                                          args->interaction,
+                                                          args->flags,
+                                                          cancellable,
+                                                          &error);
+  if (results)
+    g_task_return_pointer (task, results, async_lookup_certificates_free_certificates);
+  else
+    g_task_return_error (task, error);
 }
 
 static void
@@ -386,7 +360,7 @@ g_tls_database_real_lookup_certificates_issued_by_async (GTlsDatabase
                                                          GAsyncReadyCallback     callback,
                                                          gpointer                user_data)
 {
-  GSimpleAsyncResult *res;
+  GTask *task;
   AsyncLookupCertificatesIssuedBy *args;
 
   args = g_slice_new0 (AsyncLookupCertificatesIssuedBy);
@@ -394,33 +368,20 @@ g_tls_database_real_lookup_certificates_issued_by_async (GTlsDatabase
   args->flags = flags;
   args->interaction = interaction ? g_object_ref (interaction) : NULL;
 
-  res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
-                                   g_tls_database_real_lookup_certificates_issued_by_async);
-  g_simple_async_result_set_op_res_gpointer (res, args, async_lookup_certificates_issued_by_free);
-  g_simple_async_result_run_in_thread (res, async_lookup_certificates_issued_by_thread,
-                                       G_PRIORITY_DEFAULT, cancellable);
-  g_object_unref (res);
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_task_data (task, args, async_lookup_certificates_issued_by_free);
+  g_task_run_in_thread (task, async_lookup_certificates_issued_by_thread);
+  g_object_unref (task);
 }
 
-static GList*
+static GList *
 g_tls_database_real_lookup_certificates_issued_by_finish (GTlsDatabase          *self,
                                                           GAsyncResult          *result,
                                                           GError               **error)
 {
-  AsyncLookupCertificatesIssuedBy *args;
-  GList *results;
-
-  g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
-  g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self),
-                        g_tls_database_real_lookup_certificates_issued_by_async), FALSE);
-
-  if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error))
-    return NULL;
+  g_return_val_if_fail (g_task_is_valid (result, self), NULL);
 
-  args = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result));
-  results = args->results;
-  args->results = NULL;
-  return results;
+  return g_task_propagate_pointer (G_TASK (result), error);
 }
 
 static void
diff --git a/gio/gtlsinteraction.c b/gio/gtlsinteraction.c
index e921b81..f9fd265 100644
--- a/gio/gtlsinteraction.c
+++ b/gio/gtlsinteraction.c
@@ -28,7 +28,7 @@
 #include "gtlspassword.h"
 #include "gasyncresult.h"
 #include "gcancellable.h"
-#include "gsimpleasyncresult.h"
+#include "gtask.h"
 #include "gioenumtypes.h"
 #include "glibintl.h"
 
@@ -468,7 +468,7 @@ g_tls_interaction_ask_password_async (GTlsInteraction    *interaction,
                                       gpointer            user_data)
 {
   GTlsInteractionClass *klass;
-  GSimpleAsyncResult *res;
+  GTask *task;
 
   g_return_if_fail (G_IS_TLS_INTERACTION (interaction));
   g_return_if_fail (G_IS_TLS_PASSWORD (password));
@@ -483,10 +483,9 @@ g_tls_interaction_ask_password_async (GTlsInteraction    *interaction,
     }
   else
     {
-      res = g_simple_async_result_new (G_OBJECT (interaction), callback, user_data,
-                                       g_tls_interaction_ask_password_async);
-      g_simple_async_result_complete_in_idle (res);
-      g_object_unref (res);
+      task = g_task_new (interaction, cancellable, callback, user_data);
+      g_task_return_int (task, G_TLS_INTERACTION_UNHANDLED);
+      g_object_unref (task);
     }
 }
 
@@ -520,18 +519,16 @@ g_tls_interaction_ask_password_finish (GTlsInteraction    *interaction,
   g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), G_TLS_INTERACTION_UNHANDLED);
 
-  /* If it's one of our simple unhandled async results, handle here */
-  if (g_simple_async_result_is_valid (result, G_OBJECT (interaction),
-                                      g_tls_interaction_ask_password_async))
+  klass = G_TLS_INTERACTION_GET_CLASS (interaction);
+  if (klass->ask_password_finish)
     {
-      return G_TLS_INTERACTION_UNHANDLED;
-    }
+      g_return_val_if_fail (klass->ask_password_async != NULL, G_TLS_INTERACTION_UNHANDLED);
 
-  /* Invoke finish of derived class */
+      return (klass->ask_password_finish) (interaction, result, error);
+    }
   else
     {
-      klass = G_TLS_INTERACTION_GET_CLASS (interaction);
-      g_return_val_if_fail (klass->ask_password_finish, G_TLS_INTERACTION_UNHANDLED);
-      return (klass->ask_password_finish) (interaction, result, error);
+      g_return_val_if_fail (g_task_is_valid (result, interaction), G_TLS_INTERACTION_UNHANDLED);
+      return g_task_propagate_int (G_TASK (result), error);
     }
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]