[gvfs/wip/oholy/gtask: 10/25] client: Port GDaemonFileInputStream to GTask



commit 65df592133ce46b637d00e5f444512da63fd82c3
Author: Ondrej Holy <oholy redhat com>
Date:   Mon Jun 13 14:13:20 2016 +0200

    client: Port GDaemonFileInputStream to GTask
    
    GSimpleAsyncResult is deprecated in favour of GTask and should be replaced.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=747412

 client/gdaemonfileinputstream.c |  250 +++++++++++----------------------------
 1 files changed, 70 insertions(+), 180 deletions(-)
---
diff --git a/client/gdaemonfileinputstream.c b/client/gdaemonfileinputstream.c
index 9fc8ae5..459e345 100644
--- a/client/gdaemonfileinputstream.c
+++ b/client/gdaemonfileinputstream.c
@@ -293,6 +293,13 @@ pre_read_free (PreRead *pre)
 }
 
 static void
+query_operation_free (QueryOperation *op)
+{
+  g_free (op->attributes);
+  g_free (op);
+}
+
+static void
 g_string_remove_in_front (GString *string,
                          gsize bytes)
 {
@@ -1648,49 +1655,24 @@ g_daemon_file_input_stream_query_info (GFileInputStream     *stream,
 
 typedef struct AsyncIterator AsyncIterator;
 
-typedef void (*AsyncIteratorDone) (GInputStream *stream,
-                                  gpointer op_data,
-                                  GAsyncReadyCallback callback,
-                                  gpointer callback_data,
-                                   GCancellable *cancellable,
-                                  GError *io_error);
+typedef void (*AsyncIteratorDone) (GTask *task);
 
 struct AsyncIterator {
   AsyncIteratorDone done_cb;
-  GDaemonFileInputStream *file;
-  GCancellable *cancellable;
   IOOperationData io_data;
   state_machine_iterator iterator;
-  gpointer iterator_data;
-  int io_priority;
-  GAsyncReadyCallback callback;
-  gpointer callback_data;
+  GTask *task;
 };
 
 static void async_iterate (AsyncIterator *iterator);
 
 static void
-async_iterator_done (AsyncIterator *iterator, GError *io_error)
-{
-  iterator->done_cb (G_INPUT_STREAM (iterator->file),
-                    iterator->iterator_data,
-                    iterator->callback,
-                    iterator->callback_data,
-                     iterator->cancellable,
-                    io_error);
-
-  g_free (iterator);
-  
-}
-
-static void
 async_op_handle (AsyncIterator *iterator,
                 gssize res,
                 GError *io_error)
 {
   IOOperationData *io_data = &iterator->io_data;
-  GError *error;
-  
+
   if (io_error != NULL)
     {
       if (error_is_cancel (io_error))
@@ -1700,21 +1682,15 @@ async_op_handle (AsyncIterator *iterator,
        }
       else
        {
-         error = NULL;
-         g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                      _("Error in stream protocol: %s"), io_error->message);
-         async_iterator_done (iterator, error);
-         g_error_free (error);
+         g_task_return_new_error (iterator->task, G_IO_ERROR, G_IO_ERROR_FAILED,
+                                  _("Error in stream protocol: %s"), io_error->message);
          return;
        }
     }
   else if (res == 0 && io_data->io_size != 0)
     {
-      error = NULL;
-      g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
-                  _("Error in stream protocol: %s"), _("End of stream"));
-      async_iterator_done (iterator, error);
-      g_error_free (error);
+       g_task_return_new_error (iterator->task, G_IO_ERROR, G_IO_ERROR_FAILED,
+                                 _("Error in stream protocol: %s"), _("End of stream"));
       return;
     }
   else
@@ -1778,17 +1754,19 @@ static void
 async_iterate (AsyncIterator *iterator)
 {
   IOOperationData *io_data = &iterator->io_data;
-  GDaemonFileInputStream *file = iterator->file;
+  GDaemonFileInputStream *file;
+  GCancellable *cancellable = g_task_get_cancellable (iterator->task);
   StateOp io_op;
-  
-  io_data->cancelled =
-    g_cancellable_is_cancelled (iterator->cancellable);
 
-  io_op = iterator->iterator (file, io_data, iterator->iterator_data);
+  io_data->cancelled = g_cancellable_is_cancelled (cancellable);
+
+  file = G_DAEMON_FILE_INPUT_STREAM (g_task_get_source_object (iterator->task));
+  io_op = iterator->iterator (file, io_data, g_task_get_task_data (iterator->task));
 
   if (io_op == STATE_OP_DONE)
     {
-      async_iterator_done (iterator, NULL);
+      iterator->done_cb (iterator->task);
+      g_free (iterator);
       return;
     }
 
@@ -1798,24 +1776,24 @@ async_iterate (AsyncIterator *iterator)
     {
       g_input_stream_read_async (file->data_stream,
                                 io_data->io_buffer, io_data->io_size,
-                                iterator->io_priority,
-                                io_data->io_allow_cancel ? iterator->cancellable : NULL,
+                                g_task_get_priority (iterator->task),
+                                io_data->io_allow_cancel ? cancellable : NULL,
                                 async_read_op_callback, iterator);
     }
   else if (io_op == STATE_OP_SKIP)
     {
       g_input_stream_skip_async (file->data_stream,
                                 io_data->io_size,
-                                iterator->io_priority,
-                                io_data->io_allow_cancel ? iterator->cancellable : NULL,
+                                g_task_get_priority (iterator->task),
+                                io_data->io_allow_cancel ? cancellable : NULL,
                                 async_skip_op_callback, iterator);
     }
   else if (io_op == STATE_OP_WRITE)
     {
       g_output_stream_write_async (file->command_stream,
                                   io_data->io_buffer, io_data->io_size,
-                                  iterator->io_priority,
-                                  io_data->io_allow_cancel ? iterator->cancellable : NULL,
+                                  g_task_get_priority (iterator->task),
+                                  io_data->io_allow_cancel ? cancellable : NULL,
                                   async_write_op_callback, iterator);
     }
   else
@@ -1826,6 +1804,7 @@ static void
 run_async_state_machine (GDaemonFileInputStream *file,
                         state_machine_iterator iterator_cb,
                         gpointer iterator_data,
+                        GDestroyNotify notify,
                         int io_priority,
                         GAsyncReadyCallback callback,
                         gpointer data,
@@ -1835,60 +1814,32 @@ run_async_state_machine (GDaemonFileInputStream *file,
   AsyncIterator *iterator;
 
   iterator = g_new0 (AsyncIterator, 1);
-  iterator->file = file;
   iterator->iterator = iterator_cb;
-  iterator->iterator_data = iterator_data;
-  iterator->io_priority = io_priority;
-  iterator->cancellable = cancellable;
-  iterator->callback = callback;
-  iterator->callback_data = data;
   iterator->done_cb = done_cb;
+  iterator->task = g_task_new (file, cancellable, callback, data);
+  g_task_set_priority (iterator->task, io_priority);
+  g_task_set_task_data (iterator->task, iterator_data, notify);
 
   async_iterate (iterator);
 }
 
 static void
-async_read_done (GInputStream *stream,
-                gpointer op_data,
-                GAsyncReadyCallback callback,
-                gpointer user_data,
-                 GCancellable *cancellable,
-                GError *io_error)
+async_read_done (GTask *task)
 {
   ReadOperation *op;
   gssize count_read;
   GError *error;
-  GSimpleAsyncResult *simple;
-
-  op = op_data;
 
-  if (io_error)
-    {
-      count_read = -1;
-      error = io_error;
-    }
-  else
-    {
-      count_read = op->ret_val;
-      error = op->ret_error;
-    }
+  op = g_task_get_task_data (task);
 
-  simple = g_simple_async_result_new (G_OBJECT (stream),
-                                     callback, user_data,
-                                     g_daemon_file_input_stream_read_async);
-  
-  g_simple_async_result_set_op_res_gssize (simple, count_read);
+  count_read = op->ret_val;
+  error = op->ret_error;
 
   if (count_read == -1)
-    g_simple_async_result_set_from_error (simple, error);
+    g_task_return_error (task, error);
 
-  /* Complete immediately, not in idle, since we're already in a mainloop callout */
-  _g_simple_async_result_complete_with_cancellable (simple, cancellable);
-  g_object_unref (simple);
-  
-  if (op->ret_error)
-    g_error_free (op->ret_error);
-  g_free (op);
+  g_task_return_int (task, count_read);
+  g_object_unref (task);
 }
 
 static void
@@ -1904,7 +1855,7 @@ g_daemon_file_input_stream_read_async  (GInputStream        *stream,
   ReadOperation *op;
 
   file = G_DAEMON_FILE_INPUT_STREAM (stream);
-  
+
   /* Limit for sanity and to avoid 32bit overflow */
   if (count > MAX_READ_SIZE)
     count = MAX_READ_SIZE;
@@ -1916,7 +1867,7 @@ g_daemon_file_input_stream_read_async  (GInputStream        *stream,
 
   run_async_state_machine (file,
                           (state_machine_iterator)iterate_read_state_machine,
-                          op,
+                          op, g_free,
                           io_priority,
                           callback, user_data,
                           cancellable,
@@ -1928,14 +1879,9 @@ g_daemon_file_input_stream_read_finish (GInputStream              *stream,
                                        GAsyncResult              *result,
                                        GError                   **error)
 {
-  GSimpleAsyncResult *simple;
-  gssize nread;
+  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
 
-  simple = G_SIMPLE_ASYNC_RESULT (result);
-  g_assert (g_simple_async_result_get_source_tag (simple) == g_daemon_file_input_stream_read_async);
-  
-  nread = g_simple_async_result_get_op_res_gssize (simple);
-  return nread;
+  return g_task_propagate_int (G_TASK (result), error);
 }
 
 
@@ -1961,59 +1907,35 @@ g_daemon_file_input_stream_skip_finish  (GInputStream              *stream,
 }
 
 static void
-async_close_done (GInputStream *stream,
-                 gpointer op_data,
-                 GAsyncReadyCallback callback,
-                 gpointer user_data,
-                  GCancellable *cancellable,
-                 GError *io_error)
+async_close_done (GTask *task)
 {
   GDaemonFileInputStream *file;
-  GSimpleAsyncResult *simple;
   CloseOperation *op;
   gboolean result;
   GError *error;
+  GCancellable *cancellable = g_task_get_cancellable (task);
 
-  file = G_DAEMON_FILE_INPUT_STREAM (stream);
-  
-  op = op_data;
+  file = G_DAEMON_FILE_INPUT_STREAM (g_task_get_source_object (task));
+  op = g_task_get_task_data (task);
 
-  if (io_error)
-    {
-      result = FALSE;
-      error = io_error;
-    }
-  else
-    {
-      result = op->ret_val;
-      error = op->ret_error;
-    }
+  result = op->ret_val;
+  error = op->ret_error;
 
   if (result)
     result = g_output_stream_close (file->command_stream, cancellable, &error);
   else
     g_output_stream_close (file->command_stream, cancellable, NULL);
-    
+
   if (result)
     result = g_input_stream_close (file->data_stream, cancellable, &error);
   else
     g_input_stream_close (file->data_stream, cancellable, NULL);
 
-
-  simple = g_simple_async_result_new (G_OBJECT (stream),
-                                     callback, user_data,
-                                     g_daemon_file_input_stream_read_async);
-
   if (!result)
-    g_simple_async_result_set_from_error (simple, error);
+    g_task_return_error (task, error);
 
-  /* Complete immediately, not in idle, since we're already in a mainloop callout */
-  _g_simple_async_result_complete_with_cancellable (simple, cancellable);
-  g_object_unref (simple);
-  
-  if (op->ret_error)
-    g_error_free (op->ret_error);
-  g_free (op);
+  g_task_return_boolean (task, TRUE);
+  g_object_unref (task);
 }
 
 static void
@@ -2027,13 +1949,13 @@ g_daemon_file_input_stream_close_async (GInputStream       *stream,
   CloseOperation *op;
 
   file = G_DAEMON_FILE_INPUT_STREAM (stream);
-  
+
   op = g_new0 (CloseOperation, 1);
   op->state = CLOSE_STATE_INIT;
 
   run_async_state_machine (file,
                           (state_machine_iterator)iterate_close_state_machine,
-                          op, io_priority,
+                          op, g_free, io_priority,
                           callback, data,
                           cancellable,
                           async_close_done);
@@ -2044,54 +1966,29 @@ g_daemon_file_input_stream_close_finish (GInputStream              *stream,
                                         GAsyncResult              *result,
                                         GError                   **error)
 {
-  /* Failures handled in generic close_finish code */
-  return TRUE;
+  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
+
+  return g_task_propagate_boolean (G_TASK (result), error);
 }
 
 static void
-async_query_done (GInputStream *stream,
-                 gpointer op_data,
-                 GAsyncReadyCallback callback,
-                 gpointer user_data,
-                  GCancellable *cancellable,
-                 GError *io_error)
+async_query_done (GTask *task)
 {
-  GSimpleAsyncResult *simple;
   QueryOperation *op;
   GFileInfo *info;
   GError *error;
 
-  op = op_data;
-
-  if (io_error)
-    {
-      info = NULL;
-      error = io_error;
-    }
-  else
-    {
-      info = op->info;
-      error = op->ret_error;
-    }
+  op = g_task_get_task_data (task);
 
-  simple = g_simple_async_result_new (G_OBJECT (stream),
-                                     callback, user_data,
-                                     g_daemon_file_input_stream_query_info_async);
+  info = op->info;
+  error = op->ret_error;
 
   if (info == NULL)
-    g_simple_async_result_set_from_error (simple, error);
+    g_task_return_error (task, error);
   else
-    g_simple_async_result_set_op_res_gpointer (simple, info,
-                                              g_object_unref);
-  
-  /* Complete immediately, not in idle, since we're already in a mainloop callout */
-  _g_simple_async_result_complete_with_cancellable (simple, cancellable);
-  g_object_unref (simple);
-  
-  if (op->ret_error)
-    g_error_free (op->ret_error);
-  g_free (op->attributes);
-  g_free (op);
+    g_task_return_pointer (task, info, g_object_unref);
+
+  g_object_unref (task);
 }
 
 static void
@@ -2106,7 +2003,7 @@ g_daemon_file_input_stream_query_info_async  (GFileInputStream     *stream,
   QueryOperation *op;
 
   file = G_DAEMON_FILE_INPUT_STREAM (stream);
-  
+
   op = g_new0 (QueryOperation, 1);
   op->state = QUERY_STATE_INIT;
   if (attributes)
@@ -2116,7 +2013,7 @@ g_daemon_file_input_stream_query_info_async  (GFileInputStream     *stream,
 
   run_async_state_machine (file,
                           (state_machine_iterator)iterate_query_state_machine,
-                          op, io_priority,
+                          op, (GDestroyNotify)query_operation_free, io_priority,
                           callback, user_data,
                           cancellable,
                           async_query_done);
@@ -2127,14 +2024,7 @@ g_daemon_file_input_stream_query_info_finish (GFileInputStream     *stream,
                                              GAsyncResult         *result,
                                              GError              **error)
 {
-  GSimpleAsyncResult *simple;
-  GFileInfo *info;
+  g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
 
-  simple = G_SIMPLE_ASYNC_RESULT (result);
-  g_assert (g_simple_async_result_get_source_tag (simple) == g_daemon_file_input_stream_query_info_async);
-  
-  info = g_simple_async_result_get_op_res_gpointer (simple);
-  
-  return g_object_ref (info);
-  
+  return g_task_propagate_pointer (G_TASK (result), error);
 }


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