[gvfs] client: Port GDaemonFileInputStream to GTask



commit 3a5f04ce55ff6bcee7e6061f6f69bb11f658597b
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 |  297 +++++++++++++--------------------------
 1 files changed, 95 insertions(+), 202 deletions(-)
---
diff --git a/client/gdaemonfileinputstream.c b/client/gdaemonfileinputstream.c
index 9fc8ae5..84504e9 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,19 @@ 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);
-         return;
+          g_task_return_new_error (iterator->task, G_IO_ERROR, G_IO_ERROR_FAILED,
+                                   _("Error in stream protocol: %s"), io_error->message);
+          g_object_unref (iterator->task);
+          g_free (iterator);
+          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"));
+      g_object_unref (iterator->task);
+      g_free (iterator);
       return;
     }
   else
@@ -1778,17 +1758,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 +1780,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
@@ -1823,72 +1805,38 @@ async_iterate (AsyncIterator *iterator)
 }
 
 static void
-run_async_state_machine (GDaemonFileInputStream *file,
+run_async_state_machine (GTask *task,
                         state_machine_iterator iterator_cb,
-                        gpointer iterator_data,
-                        int io_priority,
-                        GAsyncReadyCallback callback,
-                        gpointer data,
-                        GCancellable *cancellable,
                         AsyncIteratorDone done_cb)
 {
   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 = task;
 
   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;
+  op = g_task_get_task_data (task);
 
-  if (io_error)
-    {
-      count_read = -1;
-      error = io_error;
-    }
-  else
-    {
-      count_read = op->ret_val;
-      error = op->ret_error;
-    }
-
-  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);
+  else
+    g_task_return_int (task, count_read);
 
-  /* 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_object_unref (task);
 }
 
 static void
@@ -1900,11 +1848,13 @@ g_daemon_file_input_stream_read_async  (GInputStream        *stream,
                                        GAsyncReadyCallback callback,
                                        gpointer            user_data)
 {
-  GDaemonFileInputStream *file;
   ReadOperation *op;
+  GTask *task;
+
+  task = g_task_new (stream, cancellable, callback, user_data);
+  g_task_set_priority (task, io_priority);
+  g_task_set_source_tag (task, g_daemon_file_input_stream_read_async);
 
-  file = G_DAEMON_FILE_INPUT_STREAM (stream);
-  
   /* Limit for sanity and to avoid 32bit overflow */
   if (count > MAX_READ_SIZE)
     count = MAX_READ_SIZE;
@@ -1914,12 +1864,10 @@ g_daemon_file_input_stream_read_async  (GInputStream        *stream,
   op->buffer = buffer;
   op->buffer_size = count;
 
-  run_async_state_machine (file,
+  g_task_set_task_data (task, op, g_free);
+
+  run_async_state_machine (task,
                           (state_machine_iterator)iterate_read_state_machine,
-                          op,
-                          io_priority,
-                          callback, user_data,
-                          cancellable,
                           async_read_done);
 }
 
@@ -1928,14 +1876,10 @@ 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), -1);
+  g_return_val_if_fail (g_async_result_is_tagged (result, g_daemon_file_input_stream_read_finish), -1);
 
-  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,33 +1905,19 @@ 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);
@@ -1999,21 +1929,12 @@ async_close_done (GInputStream *stream,
   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);
+  else
+    g_task_return_boolean (task, TRUE);
 
-  /* 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_object_unref (task);
 }
 
 static void
@@ -2023,19 +1944,20 @@ g_daemon_file_input_stream_close_async (GInputStream       *stream,
                                        GAsyncReadyCallback callback,
                                        gpointer            data)
 {
-  GDaemonFileInputStream *file;
   CloseOperation *op;
+  GTask *task;
+
+  task = g_task_new (stream, cancellable, callback, data);
+  g_task_set_priority (task, io_priority);
+  g_task_set_source_tag (task, g_daemon_file_input_stream_close_async);
 
-  file = G_DAEMON_FILE_INPUT_STREAM (stream);
-  
   op = g_new0 (CloseOperation, 1);
   op->state = CLOSE_STATE_INIT;
 
-  run_async_state_machine (file,
+  g_task_set_task_data (task, op, g_free);
+
+  run_async_state_machine (task,
                           (state_machine_iterator)iterate_close_state_machine,
-                          op, io_priority,
-                          callback, data,
-                          cancellable,
                           async_close_done);
 }
 
@@ -2044,54 +1966,30 @@ 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);
+  g_return_val_if_fail (g_async_result_is_tagged (result, g_daemon_file_input_stream_close_finish), 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
@@ -2102,11 +2000,13 @@ g_daemon_file_input_stream_query_info_async  (GFileInputStream     *stream,
                                              GAsyncReadyCallback   callback,
                                              gpointer              user_data)
 {
-  GDaemonFileInputStream *file;
   QueryOperation *op;
+  GTask *task;
+
+  task = g_task_new (stream, cancellable, callback, user_data);
+  g_task_set_priority (task, io_priority);
+  g_task_set_source_tag (task, g_daemon_file_input_stream_query_info_async);
 
-  file = G_DAEMON_FILE_INPUT_STREAM (stream);
-  
   op = g_new0 (QueryOperation, 1);
   op->state = QUERY_STATE_INIT;
   if (attributes)
@@ -2114,11 +2014,10 @@ g_daemon_file_input_stream_query_info_async  (GFileInputStream     *stream,
   else
     op->attributes = g_strdup ("");
 
-  run_async_state_machine (file,
+  g_task_set_task_data (task, op, (GDestroyNotify)query_operation_free);
+
+  run_async_state_machine (task,
                           (state_machine_iterator)iterate_query_state_machine,
-                          op, io_priority,
-                          callback, user_data,
-                          cancellable,
                           async_query_done);
 }
 
@@ -2127,14 +2026,8 @@ 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), NULL);
+  g_return_val_if_fail (g_async_result_is_tagged (result, g_daemon_file_input_stream_query_info_async), 
NULL);
 
-  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]