[gnome-online-accounts/wip/rishi/gtask: 3/4] ewsclient: Port goa_ews_client_autodiscover() to GTask



commit f68c8e777b60bd8e0f19fcb1c42f85b365814ec0
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri Nov 23 18:37:34 2018 +0100

    ewsclient: Port goa_ews_client_autodiscover() to GTask
    
    https://bugzilla.gnome.org/show_bug.cgi?id=764157

 src/goabackend/goaewsclient.c | 82 +++++++++++++++++++++++++++----------------
 1 file changed, 52 insertions(+), 30 deletions(-)
---
diff --git a/src/goabackend/goaewsclient.c b/src/goabackend/goaewsclient.c
index f3721592..4cb576b3 100644
--- a/src/goabackend/goaewsclient.c
+++ b/src/goabackend/goaewsclient.c
@@ -1,6 +1,6 @@
 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 /*
- * Copyright © 2012 – 2017 Red Hat, Inc.
+ * Copyright © 2012 – 2018 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -64,7 +64,6 @@ goa_ews_client_new (void)
 typedef struct
 {
   GCancellable *cancellable;
-  GSimpleAsyncResult *res;
   SoupMessage *msgs[2];
   SoupSession *session;
   gboolean accept_ssl_errors;
@@ -92,7 +91,6 @@ ews_client_autodiscover_data_free (gpointer user_data)
 
   /* soup_session_queue_message stole the references to data->msgs */
   xmlOutputBufferClose (data->buf);
-  g_object_unref (data->res);
   g_object_unref (data->session);
   g_slice_free (AutodiscoverData, data);
 }
@@ -132,10 +130,12 @@ ews_client_authenticate (SoupSession *session,
 static void
 ews_client_request_started (SoupSession *session, SoupMessage *msg, SoupSocket *socket, gpointer user_data)
 {
-  AutodiscoverData *data = user_data;
+  AutodiscoverData *data;
   GError *error;
+  GTask *task = G_TASK (data);
   GTlsCertificateFlags cert_flags;
 
+  data = (AutodiscoverData *) g_task_get_task_data (task);
   error = NULL;
 
   if (!data->accept_ssl_errors
@@ -143,7 +143,7 @@ ews_client_request_started (SoupSession *session, SoupMessage *msg, SoupSocket *
       && cert_flags != 0)
     {
       goa_utils_set_error_ssl (&error, cert_flags);
-      g_simple_async_result_take_error (data->res, error);
+      g_task_return_error (task, error);
       soup_session_abort (data->session);
     }
 }
@@ -151,18 +151,24 @@ ews_client_request_started (SoupSession *session, SoupMessage *msg, SoupSocket *
 static void
 ews_client_autodiscover_cancelled_cb (GCancellable *cancellable, gpointer user_data)
 {
-  AutodiscoverData *data = user_data;
+  AutodiscoverData *data;
+  GTask *task = G_TASK (user_data);
+  gboolean cancelled;
+
+  data = g_task_get_task_data (task);
+
+  cancelled = g_task_return_error_if_cancelled (task);
   soup_session_abort (data->session);
+
+  g_return_if_fail (cancelled);
 }
 
 static gboolean
-ews_client_autodiscover_complete_and_free_in_idle (gpointer user_data)
+ews_client_autodiscover_free_in_idle (gpointer user_data)
 {
-  AutodiscoverData *data = user_data;
-
-  g_simple_async_result_complete_in_idle (data->res);
-  ews_client_autodiscover_data_free (data);
+  GTask *task = G_TASK (user_data);
 
+  g_object_unref (task);
   return G_SOURCE_REMOVE;
 }
 
@@ -191,6 +197,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
 {
   GError *error = NULL;
   AutodiscoverData *data = user_data;
+  GTask *task = G_TASK (user_data);
   gboolean op_res = FALSE;
   guint idx;
   guint status;
@@ -213,7 +220,8 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
 
   /* status == SOUP_STATUS_CANCELLED, if we are being aborted by the
    * GCancellable, an SSL error or another message that was
-   * successful.
+   * successful. The GTask was already 'returned' by the respective
+   * callbacks.
    */
   if (status == SOUP_STATUS_CANCELLED)
     {
@@ -228,6 +236,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
     {
       g_warning ("goa_ews_client_autodiscover() failed: %u — %s", msg->status_code, msg->reason_phrase);
       goa_utils_set_error_soup (&error, msg);
+      g_task_return_error (task, error);
       goto out;
     }
 
@@ -243,6 +252,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
                    GOA_ERROR,
                    GOA_ERROR_FAILED, /* TODO: more specific */
                    _("Failed to parse autodiscover response XML"));
+      g_task_return_error (task, error);
       goto out;
     }
 
@@ -254,6 +264,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
                    GOA_ERROR_FAILED, /* TODO: more specific */
                    /* Translators: the parameter is an XML element name. */
                    _("Failed to find “%s” element"), "Autodiscover");
+      g_task_return_error (task, error);
       goto out;
     }
 
@@ -269,6 +280,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
                    GOA_ERROR_FAILED, /* TODO: more specific */
                    /* Translators: the parameter is an XML element name. */
                    _("Failed to find “%s” element"), "Response");
+      g_task_return_error (task, error);
       goto out;
     }
 
@@ -312,7 +324,7 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
    * that it won't get lost when we hear from another autodiscover
    * attempt for the same GAsyncResult.
    */
-  g_simple_async_result_set_op_res_gboolean (data->res, op_res);
+  g_task_return_boolean (task, TRUE);
 
   for (idx = 0; idx < size; idx++)
     {
@@ -356,8 +368,8 @@ ews_client_autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpo
 
       source = g_idle_source_new ();
       g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
-      g_source_set_callback (source, ews_client_autodiscover_complete_and_free_in_idle, data, NULL);
-      g_source_set_name (source, "[goa] ews_client_autodiscover_complete_and_free_in_idle");
+      g_source_set_callback (source, ews_client_autodiscover_free_in_idle, task, NULL);
+      g_source_set_name (source, "[goa] ews_client_autodiscover_free_in_idle");
 
       context = g_main_context_get_thread_default ();
       g_source_attach (source, context);
@@ -457,6 +469,7 @@ goa_ews_client_autodiscover (GoaEwsClient        *self,
 {
   AutodiscoverData *data;
   AutodiscoverAuthData *auth;
+  GTask *task = NULL;
   gchar *url1;
   gchar *url2;
   xmlDoc *doc;
@@ -469,6 +482,12 @@ goa_ews_client_autodiscover (GoaEwsClient        *self,
   g_return_if_fail (server != NULL && server[0] != '\0');
   g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
 
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, goa_ews_client_autodiscover);
+
+  data = g_slice_new0 (AutodiscoverData);
+  g_task_set_task_data (task, data, ews_client_autodiscover_data_free);
+
   doc = ews_client_create_autodiscover_xml (email);
   buf = xmlAllocOutputBuffer (NULL);
   xmlNodeDumpOutput (buf, doc, xmlDocGetRootElement (doc), 0, 1, NULL);
@@ -484,9 +503,8 @@ goa_ews_client_autodiscover (GoaEwsClient        *self,
    * to time out. So run both queries in parallel and let the fastest
    * (successful) one win.
    */
-  data = g_slice_new0 (AutodiscoverData);
+
   data->buf = buf;
-  data->res = g_simple_async_result_new (G_OBJECT (self), callback, user_data, goa_ews_client_autodiscover);
   data->msgs[0] = ews_client_create_msg_for_url (url1, buf);
   data->msgs[1] = ews_client_create_msg_for_url (url2, buf);
   data->pending = sizeof (data->msgs) / sizeof (data->msgs[0]);
@@ -498,11 +516,10 @@ goa_ews_client_autodiscover (GoaEwsClient        *self,
   if (cancellable != NULL)
     {
       data->cancellable = g_object_ref (cancellable);
-      data->cancellable_id = g_cancellable_connect (data->cancellable,
+      data->cancellable_id = g_cancellable_connect (cancellable,
                                                     G_CALLBACK (ews_client_autodiscover_cancelled_cb),
-                                                    data,
+                                                    task,
                                                     NULL);
-      g_simple_async_result_set_check_cancellable (data->res, data->cancellable);
     }
 
   auth = g_slice_new0 (AutodiscoverAuthData);
@@ -515,31 +532,36 @@ goa_ews_client_autodiscover (GoaEwsClient        *self,
                          ews_client_autodiscover_auth_data_free,
                          0);
 
-  g_signal_connect (data->session, "request-started", G_CALLBACK (ews_client_request_started), data);
+  g_signal_connect (data->session, "request-started", G_CALLBACK (ews_client_request_started), task);
 
-  soup_session_queue_message (data->session, data->msgs[0], ews_client_autodiscover_response_cb, data);
-  soup_session_queue_message (data->session, data->msgs[1], ews_client_autodiscover_response_cb, data);
+  soup_session_queue_message (data->session,
+                              data->msgs[0],
+                              ews_client_autodiscover_response_cb,
+                              g_object_ref (task));
+  soup_session_queue_message (data->session,
+                              data->msgs[1],
+                              ews_client_autodiscover_response_cb,
+                              g_object_ref (task));
 
   g_free (url2);
   g_free (url1);
+  g_object_unref (task);
   xmlFreeDoc (doc);
 }
 
 gboolean
 goa_ews_client_autodiscover_finish (GoaEwsClient *self, GAsyncResult *res, GError **error)
 {
-  GSimpleAsyncResult *simple;
+  GTask *task;
 
-  g_return_val_if_fail (g_simple_async_result_is_valid (res, G_OBJECT (self), goa_ews_client_autodiscover),
-                        FALSE);
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
-  simple = G_SIMPLE_ASYNC_RESULT (res);
+  g_return_val_if_fail (g_task_is_valid (res, self), FALSE);
+  task = G_TASK (res);
 
-  if (g_simple_async_result_propagate_error (simple, error))
-    return FALSE;
+  g_return_val_if_fail (g_task_get_source_tag (task) == goa_ews_client_autodiscover, FALSE);
 
-  return g_simple_async_result_get_op_res_gboolean (simple);
+  return g_task_propagate_boolean (task, error);
 }
 
 /* ---------------------------------------------------------------------------------------------------- */


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