[gdm: 1/2] libgdm: use g_autoptr to manage objects lifecycle



commit 71c41056684763ca0fea3ec1db5b56c5ffe20f7d
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Mon Jun 4 19:11:25 2018 +0200

    libgdm: use g_autoptr to manage objects lifecycle
    
    Using auto pointers allows to manage things in a cleaner way without having
    to manually unref things before returning.

 libgdm/gdm-client.c | 264 ++++++++++++++++++++++------------------------------
 1 file changed, 111 insertions(+), 153 deletions(-)
---
diff --git a/libgdm/gdm-client.c b/libgdm/gdm-client.c
index 28cb7253..f9e3faee 100644
--- a/libgdm/gdm-client.c
+++ b/libgdm/gdm-client.c
@@ -94,27 +94,23 @@ gdm_client_get_open_connection (GdmClient *client)
 static void
 on_got_manager (GObject             *object,
                 GAsyncResult        *result,
-                GTask               *task)
+                gpointer             user_data)
 {
-        GdmClient *client;
-        GError    *error;
+        g_autoptr(GTask)      task = user_data;
+        g_autoptr(GdmClient)  client = NULL;
         g_autoptr(GdmManager) manager = NULL;
+        g_autoptr(GError)     error = NULL;
 
         client = GDM_CLIENT (g_async_result_get_source_object (G_ASYNC_RESULT (task)));
-
-        error = NULL;
         manager = gdm_manager_proxy_new_finish (result, &error);
 
         if (error != NULL) {
-                g_task_return_error (task, error);
+                g_task_return_error (task, g_steal_pointer (&error));
         } else {
                 g_task_return_pointer (task,
-                                       g_object_ref (manager),
+                                       g_steal_pointer (&manager),
                                        (GDestroyNotify) g_object_unref);
         }
-
-        g_object_unref (task);
-        g_object_unref (client);
 }
 
 static void
@@ -135,7 +131,6 @@ get_manager (GdmClient           *client,
                                        "org.gnome.DisplayManager",
                                        "/org/gnome/DisplayManager/Manager",
                                        cancellable,
-                                       (GAsyncReadyCallback)
                                        on_got_manager,
                                        task);
 }
@@ -200,7 +195,7 @@ on_user_verifier_choice_list_proxy_created (GObject            *source,
 {
         GdmClient                 *client;
         GdmUserVerifierChoiceList *choice_list;
-        GError                    *error = NULL;
+        g_autoptr(GError)          error = NULL;
 
         client = GDM_CLIENT (g_async_result_get_source_object (G_ASYNC_RESULT (data->task)));
 
@@ -208,7 +203,6 @@ on_user_verifier_choice_list_proxy_created (GObject            *source,
 
         if (choice_list == NULL) {
                 g_debug ("Couldn't create UserVerifier ChoiceList proxy: %s", error->message);
-                g_clear_error (&error);
                 g_hash_table_remove (client->priv->user_verifier_extensions, 
gdm_user_verifier_choice_list_interface_info ()->name);
         } else {
                 g_hash_table_replace (client->priv->user_verifier_extensions, 
gdm_user_verifier_choice_list_interface_info ()->name, choice_list);
@@ -225,7 +219,7 @@ on_user_verifier_extensions_enabled (GdmUserVerifier    *user_verifier,
         GdmClient *client;
         GCancellable *cancellable;
         GDBusConnection *connection;
-        GError    *error = NULL;
+        g_autoptr(GError) error = NULL;
         size_t     i;
 
         client = GDM_CLIENT (g_async_result_get_source_object (G_ASYNC_RESULT (data->task)));
@@ -236,7 +230,6 @@ on_user_verifier_extensions_enabled (GdmUserVerifier    *user_verifier,
         if (error != NULL) {
                 g_debug ("Couldn't enable user verifier extensions: %s",
                          error->message);
-                g_clear_error (&error);
                 complete_user_verifier_proxy_operation (client, data);
                 return;
         }
@@ -284,17 +277,17 @@ free_interface_skeleton (GDBusInterfaceSkeleton *interface)
 static void
 on_user_verifier_proxy_created (GObject            *source,
                                 GAsyncResult       *result,
-                                GTask              *task)
+                                gpointer            user_data)
 {
         GdmClient       *self;
-        GdmUserVerifier *user_verifier;
         GCancellable    *cancellable = NULL;
-        GError          *error = NULL;
+        g_autoptr(GdmUserVerifier) user_verifier = NULL;
+        g_autoptr(GTask)           task = user_data;
+        g_autoptr(GError)          error = NULL;
 
         user_verifier = gdm_user_verifier_proxy_new_finish (result, &error);
         if (user_verifier == NULL) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
@@ -304,9 +297,8 @@ on_user_verifier_proxy_created (GObject            *source,
         if (self->priv->enabled_extensions == NULL) {
                 g_debug ("no enabled extensions");
                 g_task_return_pointer (task,
-                                       user_verifier,
+                                       g_steal_pointer (&user_verifier),
                                        (GDestroyNotify) g_object_unref);
-                g_object_unref (task);
                 return;
         }
 
@@ -323,23 +315,21 @@ on_user_verifier_proxy_created (GObject            *source,
                                                   (GAsyncReadyCallback)
                                                   on_user_verifier_extensions_enabled,
                                                   user_verifier_data_new (task, user_verifier));
-        g_object_unref (user_verifier);
 }
 
 static void
 on_reauthentication_channel_connected (GObject            *source_object,
                                        GAsyncResult       *result,
-                                       GTask              *task)
+                                       gpointer            user_data)
 {
-        GDBusConnection *connection;
         GCancellable *cancellable;
-        GError       *error;
+        g_autoptr(GTask)           task = user_data;
+        g_autoptr(GDBusConnection) connection = NULL;
+        g_autoptr(GError)          error = NULL;
 
-        error = NULL;
         connection = g_dbus_connection_new_for_address_finish (result, &error);
         if (!connection) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
@@ -349,28 +339,25 @@ on_reauthentication_channel_connected (GObject            *source_object,
                                      NULL,
                                      SESSION_DBUS_PATH,
                                      cancellable,
-                                     (GAsyncReadyCallback)
                                      on_user_verifier_proxy_created,
-                                     task);
-        g_object_unref (connection);
+                                     g_steal_pointer (&task));
 }
 
 static void
 on_reauthentication_channel_opened (GdmManager         *manager,
                                     GAsyncResult       *result,
-                                    GTask              *task)
+                                    gpointer            user_data)
 {
         GCancellable *cancellable;
-        char         *address;
-        GError       *error;
+        g_autoptr(GTask)  task = user_data;
+        g_autoptr(GError) error = NULL;
+        g_autofree char  *address = NULL;
 
-        error = NULL;
         if (!gdm_manager_call_open_reauthentication_channel_finish (manager,
                                                                     &address,
                                                                     result,
                                                                     &error)) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
@@ -379,26 +366,24 @@ on_reauthentication_channel_opened (GdmManager         *manager,
                                            G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
                                            NULL,
                                            cancellable,
-                                           (GAsyncReadyCallback)
                                            on_reauthentication_channel_connected,
-                                           task);
+                                           g_steal_pointer (&task));
 }
 
 static void
 on_got_manager_for_reauthentication (GdmClient           *client,
                                      GAsyncResult        *result,
-                                     GTask               *task)
+                                     gpointer             user_data)
 {
         GCancellable *cancellable;
-        GdmManager   *manager;
-        char         *username;
-        GError       *error;
+        const char   *username;
+        g_autoptr(GTask)      task = user_data;
+        g_autoptr(GdmManager) manager = NULL;
+        g_autoptr(GError)     error = NULL;
 
-        error = NULL;
         manager = g_task_propagate_pointer (G_TASK (result), &error);
         if (manager == NULL) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
@@ -409,8 +394,7 @@ on_got_manager_for_reauthentication (GdmClient           *client,
                                                         cancellable,
                                                         (GAsyncReadyCallback)
                                                         on_reauthentication_channel_opened,
-                                                        task);
-        g_object_unref (manager);
+                                                        g_steal_pointer (&task));
 }
 
 static GDBusConnection *
@@ -465,46 +449,41 @@ gdm_client_get_connection_sync (GdmClient      *client,
 static void
 on_connected (GObject            *source_object,
               GAsyncResult       *result,
-              GTask              *task)
+              gpointer            user_data)
 {
-        GDBusConnection *connection;
-        GError *error;
+        g_autoptr(GTask)           task = user_data;
+        g_autoptr(GDBusConnection) connection = NULL;
+        g_autoptr(GError)          error = NULL;
 
-        error = NULL;
         connection = g_dbus_connection_new_for_address_finish (result, &error);
         if (!connection) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
         g_task_return_pointer (task,
-                               g_object_ref (connection),
+                               g_steal_pointer (&connection),
                                (GDestroyNotify) g_object_unref);
-        g_object_unref (task);
-        g_object_unref (connection);
 }
 
 static void
 on_session_opened (GdmManager         *manager,
                    GAsyncResult       *result,
-                   GTask              *task)
+                   gpointer            user_data)
 {
-        GdmClient *client;
-        g_autofree char  *address = NULL;
         GCancellable     *cancellable;
-        GError           *error;
+        g_autoptr(GTask)     task = user_data;
+        g_autoptr(GdmClient) client = NULL;
+        g_autoptr(GError)    error = NULL;
+        g_autofree char     *address = NULL;
 
         client = GDM_CLIENT (g_async_result_get_source_object (G_ASYNC_RESULT (task)));
 
-        error = NULL;
         if (!gdm_manager_call_open_session_finish (manager,
                                                    &address,
                                                    result,
                                                    &error)) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
-                g_object_unref (client);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
@@ -513,26 +492,23 @@ on_session_opened (GdmManager         *manager,
                                            G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
                                            NULL,
                                            cancellable,
-                                           (GAsyncReadyCallback)
                                            on_connected,
-                                           task);
-        g_object_unref (client);
+                                           g_steal_pointer (&task));
 }
 
 static void
 on_got_manager_for_opening_connection (GdmClient           *client,
                                        GAsyncResult        *result,
-                                       GTask               *task)
+                                       gpointer             user_data)
 {
         GCancellable *cancellable;
-        GdmManager   *manager;
-        GError       *error;
+        g_autoptr(GTask)      task = user_data;
+        g_autoptr(GdmManager) manager = NULL;
+        g_autoptr(GError)     error = NULL;
 
-        error = NULL;
         manager = g_task_propagate_pointer (G_TASK (result), &error);
         if (manager == NULL) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
@@ -541,9 +517,7 @@ on_got_manager_for_opening_connection (GdmClient           *client,
                                        cancellable,
                                        (GAsyncReadyCallback)
                                        on_session_opened,
-                                       task);
-
-        g_object_unref (manager);
+                                       g_steal_pointer (&task));
 }
 
 static GDBusConnection *
@@ -569,7 +543,7 @@ gdm_client_get_connection (GdmClient           *client,
                             GAsyncReadyCallback  callback,
                             gpointer             user_data)
 {
-        GTask *task;
+        g_autoptr(GTask) task = NULL;
         GDBusConnection *connection;
 
         g_return_if_fail (GDM_IS_CLIENT (client));
@@ -584,7 +558,6 @@ gdm_client_get_connection (GdmClient           *client,
             g_task_return_pointer (task,
                                    g_object_ref (connection),
                                    (GDestroyNotify) g_object_unref);
-            g_object_unref (task);
             return;
         }
 
@@ -592,7 +565,7 @@ gdm_client_get_connection (GdmClient           *client,
                      cancellable,
                      (GAsyncReadyCallback)
                      on_got_manager_for_opening_connection,
-                     task);
+                     g_steal_pointer (&task));
 }
 
 /**
@@ -616,7 +589,7 @@ gdm_client_open_reauthentication_channel_sync (GdmClient     *client,
                                                GError       **error)
 {
         g_autoptr(GDBusConnection) connection = NULL;
-        g_autoptr(GdmManager) manager = NULL;
+        g_autoptr(GdmManager)      manager = NULL;
         g_autofree char *address = NULL;
         GdmUserVerifier *user_verifier = NULL;
         gboolean         ret;
@@ -806,18 +779,16 @@ gdm_client_get_user_verifier_sync (GdmClient     *client,
 static void
 on_connection_for_user_verifier (GdmClient          *client,
                                  GAsyncResult       *result,
-                                 GTask              *task)
+                                 gpointer            user_data)
 {
-        g_autoptr(GDBusConnection) connection = NULL;
         GCancellable *cancellable;
-        GError       *error;
+        g_autoptr(GTask)           task = user_data;
+        g_autoptr(GDBusConnection) connection = NULL;
+        g_autoptr(GError)          error = NULL;
 
-        error = NULL;
         connection = gdm_client_get_connection_finish (client, result, &error);
-
         if (connection == NULL) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
@@ -827,9 +798,8 @@ on_connection_for_user_verifier (GdmClient          *client,
                                      NULL,
                                      SESSION_DBUS_PATH,
                                      cancellable,
-                                     (GAsyncReadyCallback)
                                      on_user_verifier_proxy_created,
-                                     task);
+                                     g_steal_pointer (&task));
 }
 
 /**
@@ -848,7 +818,7 @@ gdm_client_get_user_verifier (GdmClient           *client,
                               GAsyncReadyCallback  callback,
                               gpointer             user_data)
 {
-        GTask *task;
+        g_autoptr(GTask) task = NULL;
 
         g_return_if_fail (GDM_IS_CLIENT (client));
 
@@ -861,15 +831,14 @@ gdm_client_get_user_verifier (GdmClient           *client,
                 g_task_return_pointer (task,
                                        g_object_ref (client->priv->user_verifier),
                                        (GDestroyNotify) g_object_unref);
-                g_object_unref (task);
                 return;
         }
 
         gdm_client_get_connection (client,
-                                    cancellable,
-                                    (GAsyncReadyCallback)
-                                    on_connection_for_user_verifier,
-                                    task);
+                                   cancellable,
+                                   (GAsyncReadyCallback)
+                                   on_connection_for_user_verifier,
+                                   g_steal_pointer (&task));
 }
 
 /**
@@ -951,22 +920,21 @@ query_for_timed_login_requested_signal (GdmGreeter *greeter)
 static void
 on_greeter_proxy_created (GObject            *source,
                           GAsyncResult       *result,
-                          GTask              *task)
+                          gpointer            user_data)
 {
+        g_autoptr(GTask)  task = user_data;
+        g_autoptr(GError) error = NULL;
         GdmGreeter   *greeter;
-        GError       *error = NULL;
 
         greeter = gdm_greeter_proxy_new_finish (result, &error);
         if (greeter == NULL) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
         g_task_return_pointer (task,
                                greeter,
                                (GDestroyNotify) g_object_unref);
-        g_object_unref (task);
 
         query_for_timed_login_requested_signal (greeter);
 }
@@ -974,18 +942,17 @@ on_greeter_proxy_created (GObject            *source,
 static void
 on_connection_for_greeter (GdmClient          *client,
                            GAsyncResult       *result,
-                           GTask              *task)
+                           gpointer            user_data)
 {
-        g_autoptr(GDBusConnection) connection = NULL;
         GCancellable *cancellable;
-        GError       *error;
+        g_autoptr(GTask)           task = user_data;
+        g_autoptr(GDBusConnection) connection = NULL;
+        g_autoptr(GError)          error = NULL;
 
-        error = NULL;
         connection = gdm_client_get_connection_finish (client, result, &error);
 
         if (connection == NULL) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
@@ -995,9 +962,8 @@ on_connection_for_greeter (GdmClient          *client,
                                NULL,
                                SESSION_DBUS_PATH,
                                cancellable,
-                               (GAsyncReadyCallback)
                                on_greeter_proxy_created,
-                               task);
+                               g_steal_pointer (&task));
 }
 
 /**
@@ -1016,7 +982,7 @@ gdm_client_get_greeter (GdmClient           *client,
                         GAsyncReadyCallback  callback,
                         gpointer             user_data)
 {
-        GTask *task;
+        g_autoptr(GTask) task = NULL;
 
         g_return_if_fail (GDM_IS_CLIENT (client));
 
@@ -1029,15 +995,14 @@ gdm_client_get_greeter (GdmClient           *client,
                 g_task_return_pointer (task,
                                        g_object_ref (client->priv->greeter),
                                        (GDestroyNotify) g_object_unref);
-                g_object_unref (task);
                 return;
         }
 
         gdm_client_get_connection (client,
-                                    cancellable,
-                                    (GAsyncReadyCallback)
-                                    on_connection_for_greeter,
-                                    task);
+                                   cancellable,
+                                   (GAsyncReadyCallback)
+                                   on_connection_for_greeter,
+                                   g_steal_pointer (&task));
 }
 
 /**
@@ -1127,39 +1092,37 @@ gdm_client_get_greeter_sync (GdmClient     *client,
 static void
 on_remote_greeter_proxy_created (GObject            *object,
                                  GAsyncResult       *result,
-                                 GTask              *task)
+                                 gpointer            user_data)
 {
+        g_autoptr(GTask)  task = user_data;
+        g_autoptr(GError) error = NULL;
         GdmRemoteGreeter *remote_greeter;
-        GError           *error = NULL;
 
         remote_greeter = gdm_remote_greeter_proxy_new_finish (result, &error);
         if (remote_greeter == NULL) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
         g_task_return_pointer (task,
                                remote_greeter,
                                (GDestroyNotify) g_object_unref);
-        g_object_unref (task);
 }
 
 static void
 on_connection_for_remote_greeter (GdmClient          *client,
                                   GAsyncResult       *result,
-                                  GTask              *task)
+                                  gpointer            user_data)
 {
-        g_autoptr(GDBusConnection) connection = NULL;
         GCancellable *cancellable;
-        GError       *error;
+        g_autoptr(GTask)           task = user_data;
+        g_autoptr(GDBusConnection) connection = NULL;
+        g_autoptr(GError)          error = NULL;
 
-        error = NULL;
         connection = gdm_client_get_connection_finish (client, result, &error);
 
         if (connection == NULL) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
@@ -1169,9 +1132,8 @@ on_connection_for_remote_greeter (GdmClient          *client,
                                       NULL,
                                       SESSION_DBUS_PATH,
                                       cancellable,
-                                      (GAsyncReadyCallback)
                                       on_remote_greeter_proxy_created,
-                                      task);
+                                      g_steal_pointer (&task));
 }
 
 /**
@@ -1190,7 +1152,7 @@ gdm_client_get_remote_greeter (GdmClient           *client,
                                GAsyncReadyCallback  callback,
                                gpointer             user_data)
 {
-        GTask *task;
+        g_autoptr (GTask) task = NULL;
 
         g_return_if_fail (GDM_IS_CLIENT (client));
 
@@ -1203,15 +1165,14 @@ gdm_client_get_remote_greeter (GdmClient           *client,
                 g_task_return_pointer (task,
                                        g_object_ref (client->priv->remote_greeter),
                                        (GDestroyNotify) g_object_unref);
-                g_object_unref (task);
                 return;
         }
 
         gdm_client_get_connection (client,
-                                    cancellable,
-                                    (GAsyncReadyCallback)
-                                    on_connection_for_remote_greeter,
-                                    task);
+                                   cancellable,
+                                   (GAsyncReadyCallback)
+                                   on_connection_for_remote_greeter,
+                                   g_steal_pointer (&task));
 }
 
 /**
@@ -1298,39 +1259,37 @@ gdm_client_get_remote_greeter_sync (GdmClient     *client,
 static void
 on_chooser_proxy_created (GObject            *source,
                           GAsyncResult       *result,
-                          GTask              *task)
+                          gpointer            user_data)
 {
         GdmChooser   *chooser;
-        GError       *error = NULL;
+        g_autoptr(GTask)  task = user_data;
+        g_autoptr(GError) error = NULL;
 
         chooser = gdm_chooser_proxy_new_finish (result, &error);
         if (chooser == NULL) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
         g_task_return_pointer (task,
                                chooser,
                                (GDestroyNotify) g_object_unref);
-        g_object_unref (task);
 }
 
 static void
 on_connection_for_chooser (GdmClient          *client,
                            GAsyncResult       *result,
-                           GTask              *task)
+                           gpointer            user_data)
 {
-        g_autoptr(GDBusConnection) connection = NULL;
         GCancellable *cancellable;
-        GError       *error;
+        g_autoptr(GTask)           task = user_data;
+        g_autoptr(GDBusConnection) connection = NULL;
+        g_autoptr(GError)          error = NULL;
 
-        error = NULL;
         connection = gdm_client_get_connection_finish (client, result, &error);
 
         if (connection == NULL) {
-                g_task_return_error (task, error);
-                g_object_unref (task);
+                g_task_return_error (task, g_steal_pointer (&error));
                 return;
         }
 
@@ -1342,7 +1301,7 @@ on_connection_for_chooser (GdmClient          *client,
                                cancellable,
                                (GAsyncReadyCallback)
                                on_chooser_proxy_created,
-                               task);
+                               g_steal_pointer (&task));
 }
 
 /**
@@ -1361,7 +1320,7 @@ gdm_client_get_chooser (GdmClient           *client,
                         GAsyncReadyCallback  callback,
                         gpointer             user_data)
 {
-        GTask *task;
+        g_autoptr(GTask) task = NULL;
 
         g_return_if_fail (GDM_IS_CLIENT (client));
 
@@ -1374,15 +1333,14 @@ gdm_client_get_chooser (GdmClient           *client,
                 g_task_return_pointer (task,
                                        g_object_ref (client->priv->chooser),
                                        (GDestroyNotify) g_object_unref);
-                g_object_unref (task);
                 return;
         }
 
         gdm_client_get_connection (client,
-                                    cancellable,
-                                    (GAsyncReadyCallback)
-                                    on_connection_for_chooser,
-                                    task);
+                                   cancellable,
+                                   (GAsyncReadyCallback)
+                                   on_connection_for_chooser,
+                                   g_steal_pointer (&task));
 }
 
 /**


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