[libsecret/wip/dueno/local-file: 7/8] storage: Allow NULL for self argument



commit dfe7299d76a370c56b297c38fc462f372066b953
Author: Daiki Ueno <dueno src gnome org>
Date:   Sun Sep 23 10:14:36 2018 +0200

    storage: Allow NULL for self argument

 libsecret/secret-storage.c | 207 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 204 insertions(+), 3 deletions(-)
---
diff --git a/libsecret/secret-storage.c b/libsecret/secret-storage.c
index 9f66e88..9670fd0 100644
--- a/libsecret/secret-storage.c
+++ b/libsecret/secret-storage.c
@@ -591,6 +591,72 @@ on_store_default_collection (GObject *source_object,
        g_object_unref (task);
 }
 
+typedef struct {
+       SecretSchema *schema;
+       GHashTable *attributes;
+       gchar *collection;
+       gchar *label;
+       SecretValue *value;
+} StorageClosure;
+
+static void
+storage_closure_free (StorageClosure *closure)
+{
+       g_clear_pointer (&closure->schema, secret_schema_unref);
+       g_clear_pointer (&closure->attributes, g_hash_table_unref);
+       g_clear_pointer (&closure->collection, g_free);
+       g_clear_pointer (&closure->label, g_free);
+       g_clear_pointer (&closure->value, secret_value_unref);
+       g_free (closure);
+}
+
+static void
+on_store (GObject *source_object,
+         GAsyncResult *result,
+         gpointer user_data)
+{
+       SecretStorage *self = SECRET_STORAGE (source_object);
+       GTask *task = G_TASK (user_data);
+       GError *error = NULL;
+
+       if (!secret_storage_store_finish (self, result, &error)) {
+               g_task_return_error (task, error);
+               g_object_unref (task);
+               return;
+       }
+
+       g_task_return_boolean (task, TRUE);
+       g_object_unref (task);
+}
+
+static void
+on_get_default_store (GObject *source_object,
+                     GAsyncResult *result,
+                     gpointer user_data)
+{
+       SecretStorage *storage;
+       GTask *task = G_TASK (user_data);
+       StorageClosure *closure = g_task_get_task_data (task);
+       GError *error = NULL;
+
+       storage = secret_storage_get_default_finish (result, &error);
+       if (!storage) {
+               g_task_return_error (task, error);
+               g_object_unref (task);
+               return;
+       }
+
+       secret_storage_store (storage,
+                             closure->schema,
+                             closure->attributes,
+                             closure->collection,
+                             closure->label,
+                             closure->value,
+                             g_task_get_cancellable (task),
+                             on_store,
+                             task);
+}
+
 void
 secret_storage_store (SecretStorage       *self,
                       const SecretSchema  *schema,
@@ -610,7 +676,7 @@ secret_storage_store (SecretStorage       *self,
        gchar *encoded;
        GTask *task;
 
-       g_return_if_fail (SECRET_IS_STORAGE (self));
+       g_return_if_fail (self == NULL || SECRET_IS_STORAGE (self));
        g_return_if_fail (attributes != NULL);
        g_return_if_fail (label != NULL);
        g_return_if_fail (value != NULL);
@@ -618,6 +684,22 @@ secret_storage_store (SecretStorage       *self,
 
        task = g_task_new (self, cancellable, callback, user_data);
 
+       if (self == NULL) {
+               StorageClosure *closure = g_new0 (StorageClosure, 1);
+               closure->schema = secret_schema_ref ((SecretSchema *) schema);
+               closure->attributes = g_hash_table_ref (attributes);
+               closure->collection = g_strdup (collection);
+               closure->label = g_strdup (label);
+               closure->value = secret_value_ref (value);
+               g_task_set_task_data (task, closure,
+                                     (GDestroyNotify) storage_closure_free);
+               secret_storage_get_default (G_PRIORITY_DEFAULT,
+                                           cancellable,
+                                           on_get_default_store,
+                                           task);
+               return;
+       }
+
        /* Warnings raised already */
        if (schema != NULL &&
            !_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE)) {
@@ -719,6 +801,55 @@ json_to_secret_value (JsonNode *node)
        return secret_value_new_full ((gchar *) value, n_value, content_type, g_free);
 }
 
+static void
+on_lookup (GObject *source_object,
+          GAsyncResult *result,
+          gpointer user_data)
+{
+       SecretStorage *self = SECRET_STORAGE (source_object);
+       GTask *task = G_TASK (user_data);
+       SecretValue *value;
+       GError *error = NULL;
+
+       value = secret_storage_lookup_finish (self, result, &error);
+       if (error) {
+               g_task_return_error (task, error);
+               g_object_unref (task);
+               return;
+       }
+
+       if (value)
+               g_task_return_pointer (task, value, secret_value_unref);
+       else
+               g_task_return_pointer (task, NULL, NULL);
+       g_object_unref (task);
+}
+
+static void
+on_get_default_lookup (GObject *source_object,
+                      GAsyncResult *result,
+                      gpointer user_data)
+{
+       SecretStorage *storage;
+       GTask *task = G_TASK (user_data);
+       StorageClosure *closure = g_task_get_task_data (task);
+       GError *error = NULL;
+
+       storage = secret_storage_get_default_finish (result, &error);
+       if (!storage) {
+               g_task_return_error (task, error);
+               g_object_unref (task);
+               return;
+       }
+
+       secret_storage_lookup (storage,
+                              closure->schema,
+                              closure->attributes,
+                              g_task_get_cancellable (task),
+                              on_lookup,
+                              task);
+}
+
 void
 secret_storage_lookup (SecretStorage *self,
                        const SecretSchema *schema,
@@ -731,12 +862,25 @@ secret_storage_lookup (SecretStorage *self,
        const gchar *schema_name = NULL;
        GTask *task;
 
-       g_return_if_fail (SECRET_IS_STORAGE (self));
+       g_return_if_fail (self == NULL || SECRET_IS_STORAGE (self));
        g_return_if_fail (attributes != NULL);
        g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
 
        task = g_task_new (self, cancellable, callback, user_data);
 
+       if (self == NULL) {
+               StorageClosure *closure = g_new0 (StorageClosure, 1);
+               closure->schema = secret_schema_ref ((SecretSchema *) schema);
+               closure->attributes = g_hash_table_ref (attributes);
+               g_task_set_task_data (task, closure,
+                                     (GDestroyNotify) storage_closure_free);
+               secret_storage_get_default (G_PRIORITY_DEFAULT,
+                                           cancellable,
+                                           on_get_default_lookup,
+                                           task);
+               return;
+       }
+
        /* Warnings raised already */
        if (schema != NULL &&
            !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE)) {
@@ -788,6 +932,50 @@ secret_storage_lookup_finish (SecretStorage *self,
        return g_task_propagate_pointer (G_TASK (result), error);
 }
 
+static void
+on_clear (GObject *source_object,
+         GAsyncResult *result,
+         gpointer user_data)
+{
+       SecretStorage *self = SECRET_STORAGE (source_object);
+       GTask *task = G_TASK (user_data);
+       GError *error = NULL;
+
+       if (!secret_storage_clear_finish (self, result, &error)) {
+               g_task_return_error (task, error);
+               g_object_unref (task);
+               return;
+       }
+
+       g_task_return_boolean (task, TRUE);
+       g_object_unref (task);
+}
+
+static void
+on_get_default_clear (GObject *source_object,
+                     GAsyncResult *result,
+                     gpointer user_data)
+{
+       SecretStorage *storage;
+       GTask *task = G_TASK (user_data);
+       StorageClosure *closure = g_task_get_task_data (task);
+       GError *error = NULL;
+
+       storage = secret_storage_get_default_finish (result, &error);
+       if (!storage) {
+               g_task_return_error (task, error);
+               g_object_unref (task);
+               return;
+       }
+
+       secret_storage_clear (storage,
+                             closure->schema,
+                             closure->attributes,
+                             g_task_get_cancellable (task),
+                             on_clear,
+                             task);
+}
+
 void
 secret_storage_clear (SecretStorage *self,
                       const SecretSchema *schema,
@@ -799,12 +987,25 @@ secret_storage_clear (SecretStorage *self,
        const gchar *schema_name = NULL;
        GTask *task;
 
-       g_return_if_fail (SECRET_STORAGE (self));
+       g_return_if_fail (self == NULL || SECRET_STORAGE (self));
        g_return_if_fail (attributes != NULL);
        g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
 
        task = g_task_new (self, cancellable, callback, user_data);
 
+       if (self == NULL) {
+               StorageClosure *closure = g_new0 (StorageClosure, 1);
+               closure->schema = secret_schema_ref ((SecretSchema *) schema);
+               closure->attributes = g_hash_table_ref (attributes);
+               g_task_set_task_data (task, closure,
+                                     (GDestroyNotify) storage_closure_free);
+               secret_storage_get_default (G_PRIORITY_DEFAULT,
+                                           cancellable,
+                                           on_get_default_clear,
+                                           task);
+               return;
+       }
+
        /* Warnings raised already */
        if (schema != NULL &&
            !_secret_attributes_validate (schema, attributes, G_STRFUNC, TRUE)) {


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