[gnome-online-accounts/wip/rishi/kerberos-task-data: 2/2] kerberos: Use GTask's task data slot to leverage the compiler



commit 5c8283cea168e309720a17a7b54b3722dd785fee
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Aug 1 20:02:40 2019 +0200

    kerberos: Use GTask's task data slot to leverage the compiler
    
    GObject's arbitrary named key to data mapping is prone to programmer
    errors that cannot be caught by the compiler while building. eg., a
    typo in the name of a key will only be detected at run-time.

 src/goabackend/goakerberosprovider.c | 83 +++++++++++++++++++++++-------------
 1 file changed, 53 insertions(+), 30 deletions(-)
---
diff --git a/src/goabackend/goakerberosprovider.c b/src/goabackend/goakerberosprovider.c
index 04a07329..168b77c8 100644
--- a/src/goabackend/goakerberosprovider.c
+++ b/src/goabackend/goakerberosprovider.c
@@ -643,20 +643,51 @@ refresh_account (GoaProvider    *provider,
   return got_ticket;
 }
 
+/* ---------------------------------------------------------------------------------------------------- */
+
+typedef struct
+{
+  GcrSecretExchange *secret_exchange;
+  GoaObject *object;
+  gboolean remember_password;
+  gchar *principal;
+} InitialSignInData;
+
+static InitialSignInData *
+initial_sign_in_data_new (GoaObject *object, const gchar *principal)
+{
+  InitialSignInData *data;
+
+  data = g_slice_new0 (InitialSignInData);
+
+  if (object != NULL)
+    data->object = g_object_ref (object);
+
+  data->principal = g_strdup (principal);
+
+  return data;
+}
+
+static void
+initial_sign_in_data_free (InitialSignInData *data)
+{
+  g_clear_object (&data->secret_exchange);
+  g_clear_object (&data->object);
+  g_free (data->principal);
+
+  g_slice_free (InitialSignInData, data);
+}
+
 static void
 on_initial_sign_in_done (GoaKerberosProvider *self,
                          GAsyncResult        *result,
                          GTask               *task)
 {
   GError     *error;
-  gboolean    remember_password;
-  GoaObject  *object;
+  InitialSignInData *data;
   char       *object_path;
 
-  object = g_task_get_source_tag (task);
-
-  remember_password = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task),
-                                                          "remember-password"));
+  data = (InitialSignInData *) g_task_get_task_data (task);
 
   error = NULL;
   object_path = sign_in_identity_finish (self, result, &error);
@@ -666,17 +697,15 @@ on_initial_sign_in_done (GoaKerberosProvider *self,
       goto out;
     }
 
-  if (remember_password)
+  if (data->remember_password)
     {
       GVariantBuilder  builder;
 
-      if (object_path != NULL && object != NULL)
+      if (object_path != NULL && data->object != NULL)
         {
-          GcrSecretExchange *secret_exchange;
-          const char        *password;
+          const char *password;
 
-          secret_exchange = g_object_get_data (G_OBJECT (task), "secret-exchange");
-          password = gcr_secret_exchange_get_secret (secret_exchange, NULL);
+          password = gcr_secret_exchange_get_secret (data->secret_exchange, NULL);
 
           /* FIXME: we go to great lengths to keep the password in non-pageable memory,
            * and then just duplicate it into a gvariant here
@@ -689,7 +718,7 @@ on_initial_sign_in_done (GoaKerberosProvider *self,
 
           error = NULL;
           goa_utils_store_credentials_for_object_sync (GOA_PROVIDER (self),
-                                                       object,
+                                                       data->object,
                                                        g_variant_builder_end (&builder),
                                                        NULL,
                                                        NULL);
@@ -711,14 +740,14 @@ on_system_prompt_answered_for_initial_sign_in (GcrPrompt          *prompt,
   GoaKerberosProvider *self;
   GCancellable        *cancellable;
   GError              *error;
-  const char          *principal;
+  InitialSignInData   *data;
   const char          *password;
   const char          *preauth_source;
   GcrSecretExchange   *secret_exchange;
 
   self = GOA_KERBEROS_PROVIDER (g_task_get_source_object (task));
-  principal = g_object_get_data (G_OBJECT (task), "principal");
   cancellable = g_task_get_cancellable (task);
+  data = (InitialSignInData *) g_task_get_task_data (task);
 
   /* We currently don't prompt the user to choose a preauthentication source during initial sign in
    * so we assume there's no preauthentication source
@@ -749,20 +778,14 @@ on_system_prompt_answered_for_initial_sign_in (GcrPrompt          *prompt,
     }
 
   secret_exchange = gcr_system_prompt_get_secret_exchange (GCR_SYSTEM_PROMPT (prompt));
-  g_object_set_data_full (G_OBJECT (task),
-                          "secret-exchange",
-                          g_object_ref (secret_exchange),
-                          (GDestroyNotify)
-                          g_object_unref);
+  data->secret_exchange = g_object_ref (secret_exchange);
 
-  g_object_set_data (G_OBJECT (task),
-                     "remember-password",
-                     GINT_TO_POINTER (gcr_prompt_get_choice_chosen (prompt)));
+  data->remember_password = gcr_prompt_get_choice_chosen (prompt);
 
   gcr_system_prompt_close (GCR_SYSTEM_PROMPT (prompt), NULL, NULL);
 
   sign_in_identity (self,
-                    principal,
+                    data->principal,
                     password,
                     preauth_source,
                     cancellable,
@@ -815,14 +838,12 @@ perform_initial_sign_in (GoaKerberosProvider *self,
 {
 
   GTask *task;
+  InitialSignInData *data;
 
-  task = g_task_new (self, cancellable, callback, user_data);
-  g_task_set_source_tag (task, object);
+  data = initial_sign_in_data_new (object, principal);
 
-  g_object_set_data (G_OBJECT (task),
-                     "principal",
-                     (gpointer)
-                     principal);
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_task_data (task, data, (GDestroyNotify) initial_sign_in_data_free);
 
   gcr_system_prompt_open_async (-1,
                                 cancellable,
@@ -848,6 +869,8 @@ perform_initial_sign_in_finish (GoaKerberosProvider  *self,
   return g_task_propagate_boolean (task, error);
 }
 
+/* ---------------------------------------------------------------------------------------------------- */
+
 static void
 on_account_signed_in (GoaKerberosProvider  *self,
                       GAsyncResult         *result,


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