[gvfs/wip/oholy/gtask: 5/16] daemon: Port GVfsBackend to GTask



commit ece484bcdca1a845baf8d39f42abb6f01edfa0b1
Author: Ondrej Holy <oholy redhat com>
Date:   Mon May 11 19:18:34 2015 +0200

    daemon: Port GVfsBackend to GTask
    
    GSimpleAsyncResult is deprecated in favour of GTask and should be replaced.
    
    This patch also introduce g_vfs_backend_register_mount_finish() and
    g_vfs_backend_unregister_mount_finish() functions to make the code
    nicer.
    
    Based on patch from Dan Winship.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=747412

 daemon/gvfsbackend.c    |  265 +++++++++++++++++++++++------------------------
 daemon/gvfsbackend.h    |    6 +
 daemon/gvfsdaemon.c     |    6 +-
 daemon/gvfsjobmount.c   |    5 +-
 daemon/gvfsjobunmount.c |    9 +--
 5 files changed, 142 insertions(+), 149 deletions(-)
---
diff --git a/daemon/gvfsbackend.c b/daemon/gvfsbackend.c
index e22659a..0ba1988 100644
--- a/daemon/gvfsbackend.c
+++ b/daemon/gvfsbackend.c
@@ -629,40 +629,38 @@ g_vfs_backend_invocation_first_handler (GVfsDBusMount *object,
   return FALSE;
 }
 
-
-typedef struct {
-  GVfsBackend *backend;
-  GAsyncReadyCallback callback;
-  gpointer callback_data;
-} AsyncProxyCreate;
-
-static void
-async_proxy_create_free (AsyncProxyCreate *data)
-{
-  g_clear_object (&data->backend);
-  g_free (data);
-}
-
 static void
-create_mount_tracker_proxy (GVfsBackend *backend,
-                            GAsyncReadyCallback op_callback,
-                            gpointer op_callback_data,
+create_mount_tracker_proxy (GTask *task,
                             GAsyncReadyCallback callback)
 {
-  AsyncProxyCreate *data;
-
-  data = g_new0 (AsyncProxyCreate, 1);
-  data->callback = op_callback;
-  data->callback_data = op_callback_data;
-  data->backend = g_object_ref (backend);
-
   gvfs_dbus_mount_tracker_proxy_new_for_bus (G_BUS_TYPE_SESSION,
                                              G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS | 
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
                                              G_VFS_DBUS_DAEMON_NAME,
                                              G_VFS_DBUS_MOUNTTRACKER_PATH,
                                              NULL,
                                              callback,
-                                             data);
+                                             task);
+}
+
+static void
+register_mount_cb (GVfsDBusMountTracker *proxy,
+                   GAsyncResult *res,
+                   gpointer user_data)
+{
+  GTask *task = G_TASK (user_data);
+  GError *error = NULL;
+
+  if (!gvfs_dbus_mount_tracker_call_register_mount_finish (proxy, res, &error))
+    {
+      g_dbus_error_strip_remote_error (error);
+      g_task_return_error (task, error);
+    }
+  else
+    {
+      g_task_return_boolean (task, TRUE);
+    }
+
+  g_object_unref (task);
 }
 
 static void
@@ -670,11 +668,10 @@ register_mount_got_proxy_cb (GObject *source_object,
                              GAsyncResult *res,
                              gpointer user_data)
 {
-  AsyncProxyCreate *data = user_data;
+  GTask *task = G_TASK (user_data);
   GVfsDBusMountTracker *proxy;
   GError *error = NULL;
-  GSimpleAsyncResult *result;
-  GVfsBackend *backend;
+  GVfsBackend *backend = G_VFS_BACKEND (g_task_get_source_object (task));
   char *stable_name;
   char *x_content_types_string;
   char *icon_str;
@@ -684,16 +681,11 @@ register_mount_got_proxy_cb (GObject *source_object,
   if (proxy == NULL)
     {
       g_dbus_error_strip_remote_error (error);
-      result = g_simple_async_result_new_take_error (source_object,
-                                                     data->callback, data->callback_data,
-                                                     error);
-      g_simple_async_result_complete_in_idle (result);
-      g_object_unref (result);
-      async_proxy_create_free (data);
+      g_task_return_error (task, error);
+      g_object_unref (task);
       return;
     }
 
-  backend = data->backend;
   backend->priv->is_mounted = TRUE;
 
   if (backend->priv->x_content_types != NULL && g_strv_length (backend->priv->x_content_types) > 0)
@@ -725,14 +717,14 @@ register_mount_got_proxy_cb (GObject *source_object,
                                                g_mount_spec_to_dbus (backend->priv->mount_spec),
                                                backend->priv->default_location ? 
backend->priv->default_location : "",
                                                NULL,
-                                               data->callback, data->callback_data);
+                                               (GAsyncReadyCallback) register_mount_cb,
+                                               task);
 
   g_free (stable_name);
   g_free (x_content_types_string);
   g_free (icon_str);
   g_free (symbolic_icon_str);
   g_object_unref (proxy);
-  async_proxy_create_free (data);
 }
 
 void
@@ -740,7 +732,44 @@ g_vfs_backend_register_mount (GVfsBackend *backend,
                               GAsyncReadyCallback callback,
                              gpointer user_data)
 {
-  create_mount_tracker_proxy (backend, callback, user_data, register_mount_got_proxy_cb);
+  GTask *task;
+
+  task = g_task_new (backend, NULL, callback, user_data);
+  g_task_set_source_tag (task, g_vfs_backend_register_mount);
+
+  create_mount_tracker_proxy (task, register_mount_got_proxy_cb);
+}
+
+gboolean
+g_vfs_backend_register_mount_finish (GVfsBackend *backend,
+                                     GAsyncResult *res,
+                                     GError **error)
+{
+  g_return_val_if_fail (g_task_is_valid (res, backend), FALSE);
+  g_return_val_if_fail (g_async_result_is_tagged (res, g_vfs_backend_register_mount), FALSE);
+
+  return g_task_propagate_boolean (G_TASK (res), error);
+}
+
+static void
+unregister_mount_cb (GVfsDBusMountTracker *proxy,
+                     GAsyncResult *res,
+                     gpointer user_data)
+{
+  GTask *task = G_TASK (user_data);
+  GError *error = NULL;
+
+  if (!gvfs_dbus_mount_tracker_call_unregister_mount_finish (proxy, res, &error))
+    {
+      g_dbus_error_strip_remote_error (error);
+      g_task_return_error (task, error);
+    }
+  else
+    {
+      g_task_return_boolean (task, TRUE);
+    }
+
+  g_object_unref (task);
 }
 
 static void
@@ -748,34 +777,27 @@ unregister_mount_got_proxy_cb (GObject *source_object,
                                GAsyncResult *res,
                                gpointer user_data)
 {
-  AsyncProxyCreate *data = user_data;
+  GTask *task = G_TASK (user_data);
   GVfsDBusMountTracker *proxy;
   GError *error = NULL;
-  GSimpleAsyncResult *result;
-  GVfsBackend *backend;
+  GVfsBackend *backend = G_VFS_BACKEND (g_task_get_source_object (task));
 
   proxy = gvfs_dbus_mount_tracker_proxy_new_for_bus_finish (res, &error);
   if (proxy == NULL)
     {
       g_dbus_error_strip_remote_error (error);
-      result = g_simple_async_result_new_take_error (source_object,
-                                                     data->callback, data->callback_data,
-                                                     error);
-      g_simple_async_result_complete_in_idle (result);
-      g_object_unref (result);
-      async_proxy_create_free (data);
+      g_task_return_error (task, error);
+      g_object_unref (task);
       return;
     }
 
-  backend = data->backend;
-  
   gvfs_dbus_mount_tracker_call_unregister_mount (proxy,
                                                  backend->priv->object_path,
                                                  NULL,
-                                                 data->callback, data->callback_data);
+                                                 (GAsyncReadyCallback) unregister_mount_cb,
+                                                 task);
 
   g_object_unref (proxy);
-  async_proxy_create_free (data);
 }
 
 void
@@ -783,14 +805,29 @@ g_vfs_backend_unregister_mount (GVfsBackend *backend,
                                 GAsyncReadyCallback callback,
                                gpointer user_data)
 {
-  create_mount_tracker_proxy (backend, callback, user_data, unregister_mount_got_proxy_cb);
+  GTask *task;
+
+  task = g_task_new (backend, NULL, callback, user_data);
+  g_task_set_source_tag (task, g_vfs_backend_unregister_mount);
+
+  create_mount_tracker_proxy (task, unregister_mount_got_proxy_cb);
+}
+
+gboolean
+g_vfs_backend_unregister_mount_finish (GVfsBackend *backend,
+                                       GAsyncResult *res,
+                                       GError **error)
+{
+  g_return_val_if_fail (g_task_is_valid (res, backend), FALSE);
+  g_return_val_if_fail (g_async_result_is_tagged (res, g_vfs_backend_unregister_mount), FALSE);
+
+  return g_task_propagate_boolean (G_TASK (res), error);
 }
 
 /* ------------------------------------------------------------------------------------------------- */
 
 typedef struct
 {
-  GVfsBackend *backend;
   GMountSource *mount_source;
 
   gboolean ret;
@@ -802,52 +839,34 @@ typedef struct
 
   gboolean completed;
 
-  GAsyncReadyCallback callback;
-  gpointer            user_data;
-
   guint timeout_id;
 } UnmountWithOpData;
 
 static void
-complete_unmount_with_op (UnmountWithOpData *data, gboolean no_more_processes)
+complete_unmount_with_op (GTask *task, gboolean no_more_processes)
 {
-  gboolean ret;
-  GSimpleAsyncResult *simple;
+  UnmountWithOpData *data = g_task_get_task_data (task);
 
   g_source_remove (data->timeout_id);
 
-  ret = TRUE;
-
-  simple = g_simple_async_result_new (G_OBJECT (data->backend),
-                                      data->callback,
-                                      data->user_data,
-                                      NULL);
-
-  if (no_more_processes)
+  if (!no_more_processes && !data->ret)
     {
-      /* do nothing, e.g. return TRUE to signal we should unmount */
+      /*  If the "show-processes" signal wasn't handled */
+      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_BUSY,
+                               _("File system is busy"));
     }
-  else if (!data->ret)
+  else if (!no_more_processes && (data->aborted || data->choice == 1))
     {
-      /*  If the "show-processes" signal wasn't handled */
-      g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_BUSY,
-                                       _("File system is busy"));
-      ret = FALSE;
+      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED,
+                               "GMountOperation aborted");
     }
   else
     {
-      if (data->aborted || data->choice == 1)
-        {
-          g_simple_async_result_set_error (simple, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED,
-                                           "GMountOperation aborted");
-          ret = FALSE;
-        }
+      g_task_return_boolean (task, TRUE);
     }
 
   data->completed = TRUE;
-  g_simple_async_result_set_op_res_gboolean (simple, ret);
-  g_simple_async_result_complete (simple);
-  g_object_unref (simple);
+  g_object_unref (task);
 }
 
 static void
@@ -855,7 +874,8 @@ on_show_processes_reply (GMountSource  *mount_source,
                          GAsyncResult  *res,
                          gpointer       user_data)
 {
-  UnmountWithOpData *data = user_data;
+  GTask *task = G_TASK (user_data);
+  UnmountWithOpData *data = g_task_get_task_data (task);
 
   /* Do nothing if we've handled this already */
   if (data->completed)
@@ -866,21 +886,23 @@ on_show_processes_reply (GMountSource  *mount_source,
                                                     &data->aborted,
                                                     &data->choice);
 
-  complete_unmount_with_op (data, FALSE);
+  complete_unmount_with_op (task, FALSE);
 }
 
 static gboolean
 on_update_processes_timeout (gpointer user_data)
 {
-  UnmountWithOpData *data = user_data;
+  GTask *task = G_TASK (user_data);
+  UnmountWithOpData *data = g_task_get_task_data (task);
   GArray *processes;
-  GVfsDaemon *daemon = g_vfs_backend_get_daemon (data->backend);
+  GVfsBackend *backend = G_VFS_BACKEND (g_task_get_source_object (task));
+  GVfsDaemon *daemon = g_vfs_backend_get_daemon (backend);
 
   if (!g_vfs_daemon_has_blocking_processes (daemon))
     {
       /* no more processes, abort mount op */
       g_mount_source_abort (data->mount_source);
-      complete_unmount_with_op (data, TRUE);
+      complete_unmount_with_op (task, TRUE);
     }
   else
     {
@@ -890,7 +912,7 @@ on_update_processes_timeout (gpointer user_data)
                                            processes,
                                            data->choices,
                                            (GAsyncReadyCallback) on_show_processes_reply,
-                                           data);
+                                           task);
       g_array_unref (processes);
     }
 
@@ -928,19 +950,10 @@ g_vfs_backend_unmount_with_operation_finish (GVfsBackend *backend,
                                              GAsyncResult *res,
                                              GError **error)
 {
-  gboolean ret;
-  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
-
-  if (g_simple_async_result_propagate_error (simple, error))
-    {
-      ret = FALSE;
-    }
-  else
-    {
-      ret = g_simple_async_result_get_op_res_gboolean (simple);
-    }
+  g_return_val_if_fail (g_task_is_valid (res, backend), FALSE);
+  g_return_val_if_fail (g_async_result_is_tagged (res, g_vfs_backend_unmount_with_operation), FALSE);
 
-  return ret;
+  return g_task_propagate_boolean (G_TASK (res), error);
 }
 
 /**
@@ -971,31 +984,28 @@ g_vfs_backend_unmount_with_operation (GVfsBackend        *backend,
 {
   GArray *processes;
   UnmountWithOpData *data;
-  GVfsDaemon *daemon = g_vfs_backend_get_daemon (backend);
+  GVfsDaemon *daemon;
+  GTask *task;
 
   g_return_if_fail (G_VFS_IS_BACKEND (backend));
   g_return_if_fail (G_IS_MOUNT_SOURCE (mount_source));
   g_return_if_fail (callback != NULL);
 
+  task = g_task_new (backend, NULL, callback, user_data);
+  g_task_set_source_tag (task, g_vfs_backend_unmount_with_operation);
+
+  daemon = g_vfs_backend_get_daemon (backend);
+
   /* if no processes are blocking, complete immediately */
   if (!g_vfs_daemon_has_blocking_processes (daemon))
     {
-      GSimpleAsyncResult *simple;
-      simple = g_simple_async_result_new (G_OBJECT (backend),
-                                          callback,
-                                          user_data,
-                                          NULL);
-      g_simple_async_result_set_op_res_gboolean (simple, TRUE);
-      g_simple_async_result_complete (simple);
-      g_object_unref (simple);
+      g_task_return_boolean (task, TRUE);
+      g_object_unref (task);
       return;
     }
 
   data = g_new0 (UnmountWithOpData, 1);
-  data->backend = backend;
   data->mount_source = mount_source;
-  data->callback = callback;
-  data->user_data = user_data;
 
   data->choices[0] = _("Unmount Anyway");
   data->choices[1] = _("Cancel");
@@ -1003,11 +1013,7 @@ g_vfs_backend_unmount_with_operation (GVfsBackend        *backend,
   data->message = _("Volume is busy\n"
                     "One or more applications are keeping the volume busy.");
 
-  /* free data when the mount source goes away */
-  g_object_set_data_full (G_OBJECT (mount_source),
-                          "unmount-op-data",
-                          data,
-                          (GDestroyNotify) unmount_with_op_data_free);
+  g_task_set_task_data (task, data, (GDestroyNotify) unmount_with_op_data_free);
 
   /* show processes */
   processes = g_vfs_daemon_get_blocking_processes (daemon);
@@ -1016,30 +1022,23 @@ g_vfs_backend_unmount_with_operation (GVfsBackend        *backend,
                                        processes,
                                        data->choices,
                                        (GAsyncReadyCallback) on_show_processes_reply,
-                                       data);
+                                       task);
   g_array_unref (processes);
 
   /* update these processes every two secs */
-  data->timeout_id = g_timeout_add_seconds (2,
-                                            on_update_processes_timeout,
-                                            data);
+  data->timeout_id = g_timeout_add_seconds (2, on_update_processes_timeout, task);
 }
 
 static void
-forced_unregister_mount_callback (GVfsDBusMountTracker *proxy,
-                                 GAsyncResult *res,
-                                 gpointer user_data)
+forced_unregister_mount_callback (GVfsBackend *backend,
+                                  GAsyncResult *res,
+                                  gpointer user_data)
 {
   GVfsDaemon *daemon;
-  GVfsBackend *backend;
   GError *error = NULL;
 
-  g_return_if_fail (G_VFS_IS_BACKEND (user_data));
-
   g_debug ("forced_unregister_mount_callback\n");
-  if (! gvfs_dbus_mount_tracker_call_unregister_mount_finish (proxy,
-                                                              res,
-                                                              &error))
+  if (!g_vfs_backend_unregister_mount_finish (backend, res, &error))
     {
       g_dbus_error_strip_remote_error (error);
       g_warning ("Error unregistering mount: %s (%s, %d)\n",
@@ -1048,12 +1047,9 @@ forced_unregister_mount_callback (GVfsDBusMountTracker *proxy,
     }
 
   /* Unlink job source from daemon */
-  backend = G_VFS_BACKEND (user_data);
   daemon = g_vfs_backend_get_daemon (backend);
   g_vfs_daemon_close_active_channels (daemon, backend);
   g_vfs_job_source_closed (G_VFS_JOB_SOURCE (backend));
-
-  g_object_unref (backend);
 }
 
 void
@@ -1062,6 +1058,5 @@ g_vfs_backend_force_unmount (GVfsBackend *backend)
   g_vfs_backend_set_block_requests (backend, TRUE);
   g_vfs_backend_unregister_mount (backend,
                                   (GAsyncReadyCallback) forced_unregister_mount_callback,
-                                  g_object_ref (backend));
+                                  NULL);
 }
-
diff --git a/daemon/gvfsbackend.h b/daemon/gvfsbackend.h
index 6f77cf9..9c7476c 100644
--- a/daemon/gvfsbackend.h
+++ b/daemon/gvfsbackend.h
@@ -491,9 +491,15 @@ void        g_vfs_backend_set_mount_spec                 (GVfsBackend        *ba
 void        g_vfs_backend_register_mount                 (GVfsBackend        *backend,
                                                           GAsyncReadyCallback callback,
                                                          gpointer            user_data);
+gboolean    g_vfs_backend_register_mount_finish          (GVfsBackend        *backend,
+                                                          GAsyncResult       *res,
+                                                          GError            **error);
 void        g_vfs_backend_unregister_mount               (GVfsBackend        *backend,
                                                           GAsyncReadyCallback callback,
                                                          gpointer            user_data);
+gboolean    g_vfs_backend_unregister_mount_finish        (GVfsBackend        *backend,
+                                                          GAsyncResult       *res,
+                                                          GError            **error);
 const char *g_vfs_backend_get_backend_type               (GVfsBackend        *backend);
 const char *g_vfs_backend_get_display_name               (GVfsBackend        *backend);
 const char *g_vfs_backend_get_stable_name                (GVfsBackend        *backend);
diff --git a/daemon/gvfsdaemon.c b/daemon/gvfsdaemon.c
index b7a3b88..69e5687 100644
--- a/daemon/gvfsdaemon.c
+++ b/daemon/gvfsdaemon.c
@@ -425,15 +425,13 @@ job_source_closed_callback (GVfsJobSource *job_source,
 }
 
 static void
-re_register_jobs_cb (GVfsDBusMountTracker *proxy,
+re_register_jobs_cb (GVfsBackend *backend,
                      GAsyncResult *res,
                      gpointer user_data)
 {
   GError *error = NULL;
 
-  gvfs_dbus_mount_tracker_call_register_mount_finish (proxy,
-                                                      res,
-                                                      &error);
+  g_vfs_backend_register_mount_finish (backend, res, &error);
   g_debug ("re_register_jobs_cb, error: %p\n", error);
   g_clear_error (&error);
 }
diff --git a/daemon/gvfsjobmount.c b/daemon/gvfsjobmount.c
index d8f6db7..d2ae571 100644
--- a/daemon/gvfsjobmount.c
+++ b/daemon/gvfsjobmount.c
@@ -158,16 +158,15 @@ mount_failed (GVfsJobMount *op_job, GError *error)
 }
 
 static void
-register_mount_callback (GVfsDBusMountTracker *proxy,
+register_mount_callback (GVfsBackend *backend,
                          GAsyncResult *res,
                          gpointer user_data)
 {
   GVfsJobMount *op_job = G_VFS_JOB_MOUNT (user_data);
   GError *error = NULL;
   
-  if (! gvfs_dbus_mount_tracker_call_register_mount_finish (proxy, res, &error))
+  if (!g_vfs_backend_register_mount_finish (backend, res, &error))
     {
-      g_dbus_error_strip_remote_error (error);
       mount_failed (op_job, error);
     }
   else
diff --git a/daemon/gvfsjobunmount.c b/daemon/gvfsjobunmount.c
index af6db0d..7a58c34 100644
--- a/daemon/gvfsjobunmount.c
+++ b/daemon/gvfsjobunmount.c
@@ -287,27 +287,22 @@ try (GVfsJob *job)
 }
 
 static void
-unregister_mount_callback (GVfsDBusMountTracker *proxy,
+unregister_mount_callback (GVfsBackend *backend,
                            GAsyncResult *res,
                            gpointer user_data)
 {
-  GVfsBackend *backend;
   GVfsDaemon *daemon;
   GVfsJobUnmount *op_job = G_VFS_JOB_UNMOUNT (user_data);
   GError *error = NULL;
 
   g_debug ("unregister_mount_callback\n");
-  if (! gvfs_dbus_mount_tracker_call_unregister_mount_finish (proxy,
-                                                              res,
-                                                              &error))
+  if (!g_vfs_backend_unregister_mount_finish (backend, res, &error))
     {
-      g_dbus_error_strip_remote_error (error);
       g_warning ("Error unregistering mount: %s (%s, %d)\n",
                   error->message, g_quark_to_string (error->domain), error->code);
       g_error_free (error);
     }
   
-  backend = op_job->backend;
   (*G_VFS_JOB_CLASS (g_vfs_job_unmount_parent_class)->send_reply) (G_VFS_JOB (op_job));
 
   /* Unlink job source from daemon */


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