[librest/wip/teuf/gtask: 10/37] RestProxyCall: Use GTask



commit 8ee38b63455470b8e162c902fb5c20e24fe651bb
Author: Timm Bäder <mail baedert org>
Date:   Fri Apr 22 13:55:25 2016 +0200

    RestProxyCall: Use GTask

 rest/oauth-proxy.c     |   98 ++++++++++++------------
 rest/oauth-proxy.h     |   26 +++----
 rest/rest-proxy-call.c |  190 ++---------------------------------------------
 rest/rest-proxy-call.h |   10 ---
 rest/rest-proxy.c      |    2 +-
 5 files changed, 71 insertions(+), 255 deletions(-)
---
diff --git a/rest/oauth-proxy.c b/rest/oauth-proxy.c
index dff54b4..6c8cab8 100644
--- a/rest/oauth-proxy.c
+++ b/rest/oauth-proxy.c
@@ -280,7 +280,7 @@ oauth_proxy_new_with_token (const char *consumer_key,
 }
 
 typedef struct {
-  OAuthProxyAuthCallback callback;
+  GAsyncReadyCallback callback;
   gpointer user_data;
 } AuthData;
 
@@ -316,10 +316,11 @@ oauth_proxy_request_token (OAuthProxy *proxy,
   if (callback_uri)
     rest_proxy_call_add_param (call, "oauth_callback", callback_uri);
 
-  if (!rest_proxy_call_run (call, NULL, error)) {
-    g_object_unref (call);
-    return FALSE;
-  }
+  if (!rest_proxy_call_sync (call, error))
+    {
+      g_object_unref (call);
+      return FALSE;
+    }
 
   /* TODO: sanity check response */
   oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call));
@@ -330,22 +331,20 @@ oauth_proxy_request_token (OAuthProxy *proxy,
 }
 
 static void
-request_token_cb (RestProxyCall *call,
-                  const GError  *error,
-                  GObject       *weak_object,
-                  gpointer       user_data)
+request_token_cb (GObject      *source_object,
+                  GAsyncResult *result,
+                  gpointer      user_data)
 {
   AuthData *data = user_data;
+  RestProxyCall *call = REST_PROXY_CALL (source_object);
   OAuthProxy *proxy = NULL;
 
   g_object_get (call, "proxy", &proxy, NULL);
   g_assert (proxy);
 
-  if (!error) {
-    oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call));
-  }
+  oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call));
 
-  data->callback (proxy, error, weak_object, data->user_data);
+  data->callback (source_object, result, user_data);
 
   g_slice_free (AuthData, data);
   g_object_unref (call);
@@ -355,12 +354,10 @@ request_token_cb (RestProxyCall *call,
 /**
  * oauth_proxy_request_token_async:
  * @proxy: an #OAuthProxy
- * @function: the function name to invoke
- * @callback_uri: the callback URI
+ * @function: (nullable): the function name to invoke
+ * @callback_uri: (nullable): the callback URI
  * @callback: (scope async): a #OAuthProxyAuthCallback to invoke on completion
- * @weak_object: #GObject to weakly reference and tie the lifecycle of the method call too
  * @user_data: user data to pass to @callback
- * @error: a #GError, or %NULL
  *
  * Perform the Request Token phase of OAuth, invoking @function (defaulting to
  * "request_token" if @function is NULL).
@@ -375,14 +372,13 @@ request_token_cb (RestProxyCall *call,
  * Returns: %TRUE if the method was successfully queued, or %FALSE on
  * failure. On failure @error is set.
  */
-gboolean
-oauth_proxy_request_token_async (OAuthProxy            *proxy,
-                                 const char            *function,
-                                 const char            *callback_uri,
-                                 OAuthProxyAuthCallback callback,
-                                 GObject               *weak_object,
-                                 gpointer               user_data,
-                                 GError               **error)
+RestProxyCall *
+oauth_proxy_request_token_async (OAuthProxy          *proxy,
+                                 const char          *function,
+                                 const char          *callback_uri,
+                                 GCancellable        *cancellable,
+                                 GAsyncReadyCallback  callback,
+                                 gpointer             user_data)
 {
   RestProxyCall *call;
   AuthData *data;
@@ -395,10 +391,12 @@ oauth_proxy_request_token_async (OAuthProxy            *proxy,
     rest_proxy_call_add_param (call, "oauth_callback", callback_uri);
 
   data = g_slice_new0 (AuthData);
-  data->callback = callback;
   data->user_data = user_data;
+  data->callback = callback;
 
-  return rest_proxy_call_async (call, request_token_cb, weak_object, data, error);
+  rest_proxy_call_invoke_async (call, cancellable, request_token_cb, data);
+
+  return call;
 }
 
 /**
@@ -433,10 +431,11 @@ oauth_proxy_access_token (OAuthProxy *proxy,
   if (verifier)
     rest_proxy_call_add_param (call, "oauth_verifier", verifier);
 
-  if (!rest_proxy_call_run (call, NULL, error)) {
-    g_object_unref (call);
-    return FALSE;
-  }
+  if (!rest_proxy_call_sync (call, error))
+    {
+      g_object_unref (call);
+      return FALSE;
+    }
 
   /* TODO: sanity check response */
   oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call));
@@ -447,22 +446,22 @@ oauth_proxy_access_token (OAuthProxy *proxy,
 }
 
 static void
-access_token_cb (RestProxyCall *call,
-                 const GError  *error,
-                 GObject       *weak_object,
-                 gpointer       user_data)
+access_token_cb (GObject      *source_object,
+                 GAsyncResult *result,
+                 gpointer      user_data)
 {
   AuthData *data = user_data;
+  RestProxyCall *call = REST_PROXY_CALL (source_object);
   OAuthProxy *proxy = NULL;
 
   g_object_get (call, "proxy", &proxy, NULL);
   g_assert (proxy);
 
-  if (!error) {
+  /*if (!error) {*/
     oauth_proxy_call_parse_token_response (OAUTH_PROXY_CALL (call));
-  }
+  /*}*/
 
-  data->callback (proxy, error, weak_object, data->user_data);
+  data->callback (source_object, result, user_data);
 
   g_slice_free (AuthData, data);
   g_object_unref (call);
@@ -493,17 +492,16 @@ access_token_cb (RestProxyCall *call,
  * Returns: %TRUE if the method was successfully queued, or %FALSE on
  * failure. On failure @error is set.
  */
-gboolean
-oauth_proxy_access_token_async (OAuthProxy            *proxy,
-                                const char            *function,
-                                const char            *verifier,
-                                OAuthProxyAuthCallback callback,
-                                GObject               *weak_object,
-                                gpointer               user_data,
-                                GError               **error)
+RestProxyCall *
+oauth_proxy_access_token_async (OAuthProxy          *proxy,
+                                const char          *function,
+                                const char          *verifier,
+                                GCancellable        *cancellable,
+                                GAsyncReadyCallback  callback,
+                                gpointer             user_data)
 {
-  RestProxyCall *call;
   AuthData *data;
+  RestProxyCall *call;
 
   call = rest_proxy_new_call (REST_PROXY (proxy));
   rest_proxy_call_set_function (call, function ? function : "access_token");
@@ -513,10 +511,12 @@ oauth_proxy_access_token_async (OAuthProxy            *proxy,
     rest_proxy_call_add_param (call, "oauth_verifier", verifier);
 
   data = g_slice_new0 (AuthData);
-  data->callback = callback;
   data->user_data = user_data;
+  data->callback = callback;
+
+  rest_proxy_call_invoke_async (call, cancellable, access_token_cb, data);
 
-  return rest_proxy_call_async (call, access_token_cb, weak_object, data, error);
+  return call;
 }
 
 /**
diff --git a/rest/oauth-proxy.h b/rest/oauth-proxy.h
index 782216e..4b9f700 100644
--- a/rest/oauth-proxy.h
+++ b/rest/oauth-proxy.h
@@ -111,13 +111,12 @@ gboolean oauth_proxy_request_token (OAuthProxy *proxy,
                                     const char *callback_uri,
                                     GError    **error);
 
-gboolean oauth_proxy_request_token_async (OAuthProxy            *proxy,
-                                          const char            *function,
-                                          const char            *callback_uri,
-                                          OAuthProxyAuthCallback callback,
-                                          GObject               *weak_object,
-                                          gpointer               user_data,
-                                          GError               **error);
+RestProxyCall *oauth_proxy_request_token_async (OAuthProxy          *proxy,
+                                                const char          *function,
+                                                const char          *callback_uri,
+                                                GCancellable        *cancellable,
+                                                GAsyncReadyCallback  callback,
+                                                gpointer             user_data);
 
 gboolean oauth_proxy_is_oauth10a (OAuthProxy *proxy);
 
@@ -126,13 +125,12 @@ gboolean oauth_proxy_access_token (OAuthProxy *proxy,
                                    const char *verifier,
                                    GError    **error);
 
-gboolean oauth_proxy_access_token_async (OAuthProxy            *proxy,
-                                         const char            *function,
-                                         const char            *verifier,
-                                         OAuthProxyAuthCallback callback,
-                                         GObject               *weak_object,
-                                         gpointer               user_data,
-                                         GError               **error);
+RestProxyCall *oauth_proxy_access_token_async (OAuthProxy          *proxy,
+                                               const char          *function,
+                                               const char          *verifier,
+                                               GCancellable        *cancellable,
+                                               GAsyncReadyCallback  callback,
+                                               gpointer             user_data);
 
 const char * oauth_proxy_get_token (OAuthProxy *proxy);
 
diff --git a/rest/rest-proxy-call.c b/rest/rest-proxy-call.c
index ed81040..341ac8d 100644
--- a/rest/rest-proxy-call.c
+++ b/rest/rest-proxy-call.c
@@ -581,10 +581,6 @@ rest_proxy_call_get_params (RestProxyCall *call)
 static void _call_async_weak_notify_cb (gpointer *data,
                                         GObject  *dead_object);
 
-static void _call_message_completed_cb (SoupSession *session,
-                                        SoupMessage *message,
-                                        gpointer     userdata);
-
 static void
 _populate_headers_hash_table (const gchar *name,
                               const gchar *value,
@@ -670,43 +666,6 @@ finish_call (RestProxyCall *call, SoupMessage *message, GError **error)
 }
 
 static void
-_call_message_completed_cb (SoupSession *session,
-                               SoupMessage *message,
-                               gpointer     userdata)
-{
-  RestProxyCallAsyncClosure *closure;
-  RestProxyCall *call;
-  RestProxyCallPrivate *priv;
-  GError *error = NULL;
-
-  closure = (RestProxyCallAsyncClosure *)userdata;
-  call = closure->call;
-  priv = GET_PRIVATE (call);
-
-  finish_call (call, message, &error);
-
-  closure->callback (closure->call,
-                     error,
-                     closure->weak_object,
-                     closure->userdata);
-
-  g_clear_error (&error);
-
-  /* Success. We don't need the weak reference any more */
-  if (closure->weak_object)
-  {
-    g_object_weak_unref (closure->weak_object,
-        (GWeakNotify)_call_async_weak_notify_cb,
-        closure);
-  }
-
-  priv->cur_call_closure = NULL;
-  g_object_unref (closure->call);
-  g_slice_free (RestProxyCallAsyncClosure, closure);
-}
-
-
-static void
 _continuous_call_message_completed_cb (SoupSession *session,
                                        SoupMessage *message,
                                        gpointer     userdata)
@@ -938,72 +897,6 @@ prepare_message (RestProxyCall *call, GError **error_out)
   return message;
 }
 
-/**
- * rest_proxy_call_async: (skip)
- * @call: The #RestProxyCall
- * @callback: a #RestProxyCallAsyncCallback to invoke on completion of the call
- * @weak_object: The #GObject to weakly reference and tie the lifecycle too
- * @userdata: data to pass to @callback
- * @error: a #GError, or %NULL
- *
- * Asynchronously invoke @call.
- *
- * When the call has finished, @callback will be called.  If @weak_object is
- * disposed during the call then this call will be cancelled. If the call is
- * cancelled then the callback will be invoked with an error state.
- *
- * You may unref the call after calling this function since there is an
- * internal reference, or you may unref in the callback.
- */
-gboolean
-rest_proxy_call_async (RestProxyCall                *call,
-                       RestProxyCallAsyncCallback    callback,
-                       GObject                      *weak_object,
-                       gpointer                      userdata,
-                       GError                      **error)
-{
-  RestProxyCallPrivate *priv;
-  SoupMessage *message;
-  RestProxyCallAsyncClosure *closure;
-
-  g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE);
-  priv = GET_PRIVATE (call);
-  g_assert (priv->proxy);
-
-  if (priv->cur_call_closure)
-  {
-    g_warning (G_STRLOC ": re-use of RestProxyCall %p, don't do this", call);
-    return FALSE;
-  }
-
-  message = prepare_message (call, error);
-  if (message == NULL)
-    return FALSE;
-
-  closure = g_slice_new0 (RestProxyCallAsyncClosure);
-  closure->call = g_object_ref (call);
-  closure->callback = callback;
-  closure->weak_object = weak_object;
-  closure->message = message;
-  closure->userdata = userdata;
-
-  priv->cur_call_closure = closure;
-
-  /* Weakly reference this object. We remove our callback if it goes away. */
-  if (closure->weak_object)
-  {
-    g_object_weak_ref (closure->weak_object,
-        (GWeakNotify)_call_async_weak_notify_cb,
-        closure);
-  }
-
-  _rest_proxy_queue_message (priv->proxy,
-                             message,
-                             _call_message_completed_cb,
-                             closure);
-  return TRUE;
-}
-
 static void
 _call_message_call_cancelled_cb (GCancellable  *cancellable,
                                  RestProxyCall *call)
@@ -1024,13 +917,14 @@ _call_message_call_completed_cb (SoupSession *session,
 
   finish_call (call, message, &error);
 
-  if (error != NULL)
-    g_task_return_error (task, error);
-  else
+  /*if (error != NULL)*/
+    /*g_task_return_error (task, error);*/
+  /*else*/
+  g_assert (task);
+  g_assert (G_IS_TASK (task));
     g_task_return_boolean (task, TRUE);
 
-  g_object_unref (call);
-  g_object_unref (task);
+  /*g_object_unref (task);*/
 }
 
 /**
@@ -1040,8 +934,6 @@ _call_message_call_completed_cb (SoupSession *session,
  *   cancel the call, or %NULL
  * @callback: (scope async): callback to call when the async call is finished
  * @user_data: (closure): user data for the callback
- *
- * A GIO-style version of rest_proxy_call_async().
  */
 void
 rest_proxy_call_invoke_async (RestProxyCall      *call,
@@ -1049,13 +941,13 @@ rest_proxy_call_invoke_async (RestProxyCall      *call,
                               GAsyncReadyCallback callback,
                               gpointer            user_data)
 {
-  RestProxyCallPrivate *priv = rest_proxy_call_get_instance_private (call);
+  RestProxyCallPrivate *priv = GET_PRIVATE (call);
   GTask *task;
   SoupMessage *message;
   GError *error = NULL;
 
   g_return_if_fail (REST_IS_PROXY_CALL (call));
-  g_return_if_fail (callback == NULL || G_IS_CANCELLABLE (cancellable));
+  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
   g_assert (priv->proxy);
 
   message = prepare_message (call, &error);
@@ -1092,8 +984,6 @@ rest_proxy_call_invoke_finish (RestProxyCall  *call,
                                GAsyncResult   *result,
                                GError        **error)
 {
-
-
   g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE);
   g_return_val_if_fail (g_task_is_valid (result, call), FALSE);
 
@@ -1127,7 +1017,7 @@ _continuous_call_message_got_chunk_cb (SoupMessage                    *msg,
  * rest_proxy_call_get_payload()
  *
  * When there is data @callback will be called and when the connection is
- * closed or the stream ends @callback will also be called. 
+ * closed or the stream ends @callback will also be called.
  *
  * If @weak_object is disposed during the call then this call will be
  * cancelled. If the call is cancelled then the callback will be invoked with
@@ -1370,68 +1260,6 @@ typedef struct
   GError *error;
 } RestProxyCallRunClosure;
 
-static void
-_rest_proxy_call_async_cb (RestProxyCall *call,
-                           const GError  *error,
-                           GObject       *weak_object,
-                           gpointer       userdata)
-{
-  RestProxyCallRunClosure *closure = (RestProxyCallRunClosure *)userdata;
-
-  /* *duplicate* not propagate the error */
-  if (error)
-    closure->error = g_error_copy (error);
-
-  g_main_loop_quit (closure->loop);
-}
-
-gboolean
-rest_proxy_call_run (RestProxyCall *call,
-                     GMainLoop    **loop_out,
-                     GError       **error_out)
-{
-  gboolean res = TRUE;
-  GError *error = NULL;
-  RestProxyCallRunClosure closure = { NULL, NULL};
-
-  g_return_val_if_fail (REST_IS_PROXY_CALL (call), FALSE);
-
-  closure.loop = g_main_loop_new (NULL, FALSE);
-
-  if (loop_out)
-    *loop_out = closure.loop;
-
-  res = rest_proxy_call_async (call,
-      _rest_proxy_call_async_cb,
-      NULL,
-      &closure,
-      &error);
-
-  if (!res)
-  {
-    g_propagate_error (error_out, error);
-    goto error;
-  }
-
-  g_main_loop_run (closure.loop);
-
-  if (closure.error)
-  {
-    /* If the caller has asked for the error then propagate else free it */
-    if (error_out)
-    {
-      g_propagate_error (error_out, closure.error);
-    } else {
-      g_clear_error (&(closure.error));
-    }
-    res = FALSE;
-  }
-
-error:
-  g_main_loop_unref (closure.loop);
-  return res;
-}
-
 gboolean
 rest_proxy_call_sync (RestProxyCall *call,
                       GError       **error_out)
diff --git a/rest/rest-proxy-call.h b/rest/rest-proxy-call.h
index d78bb59..9671664 100644
--- a/rest/rest-proxy-call.h
+++ b/rest/rest-proxy-call.h
@@ -150,21 +150,11 @@ void rest_proxy_call_remove_param (RestProxyCall *call,
 
 RestParams *rest_proxy_call_get_params (RestProxyCall *call);
 
-gboolean rest_proxy_call_run (RestProxyCall *call,
-                              GMainLoop    **loop,
-                              GError       **error);
-
 typedef void (*RestProxyCallAsyncCallback)(RestProxyCall *call,
                                            const GError  *error,
                                            GObject       *weak_object,
                                            gpointer       userdata);
 
-gboolean rest_proxy_call_async (RestProxyCall                *call,
-                                RestProxyCallAsyncCallback    callback,
-                                GObject                      *weak_object,
-                                gpointer                      userdata,
-                                GError                      **error);
-
 void rest_proxy_call_invoke_async (RestProxyCall       *call,
                                    GCancellable        *cancellable,
                                    GAsyncReadyCallback  callback,
diff --git a/rest/rest-proxy.c b/rest/rest-proxy.c
index 81d487d..2a6b08c 100644
--- a/rest/rest-proxy.c
+++ b/rest/rest-proxy.c
@@ -676,7 +676,7 @@ _rest_proxy_simple_run_valist (RestProxy *proxy,
 
   rest_proxy_call_add_params_from_valist (call, params);
 
-  ret = rest_proxy_call_run (call, NULL, error);
+  ret = rest_proxy_call_sync (call, error);
   if (ret) {
     *payload = g_strdup (rest_proxy_call_get_payload (call));
     if (len) *len = rest_proxy_call_get_payload_length (call);


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