[epiphany] password-manager: Query by ID when handling single records



commit e4668a99f6c4fb4806afc5cb7f4b82052e1efba7
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Sat Aug 19 10:33:23 2017 +0300

    password-manager: Query by ID when handling single records
    
    IDs are unique so the query should always return a single item.

 embed/web-extension/ephy-web-extension.c |    2 +
 lib/sync/ephy-password-manager.c         |   53 +++++++++++------------------
 lib/sync/ephy-password-manager.h         |    4 +-
 src/passwords-dialog.c                   |    6 +--
 4 files changed, 26 insertions(+), 39 deletions(-)
---
diff --git a/embed/web-extension/ephy-web-extension.c b/embed/web-extension/ephy-web-extension.c
index df984b5..a9709ae 100644
--- a/embed/web-extension/ephy-web-extension.c
+++ b/embed/web-extension/ephy-web-extension.c
@@ -483,6 +483,7 @@ form_submitted_cb (WebKitDOMHTMLFormElement *dom_form,
   uri_str = soup_uri_to_string (uri, FALSE);
 
   ephy_password_manager_query (extension->password_manager,
+                               NULL,
                                uri_str,
                                username_field_value,
                                username_field_name,
@@ -563,6 +564,7 @@ pre_fill_form (EphyEmbedFormAuth *form_auth)
     g_clear_pointer (&username, g_free);
 
   ephy_password_manager_query (extension->password_manager,
+                               NULL,
                                uri_str,
                                username,
                                username_field_name,
diff --git a/lib/sync/ephy-password-manager.c b/lib/sync/ephy-password-manager.c
index 71555c8..c145a8c 100644
--- a/lib/sync/ephy-password-manager.c
+++ b/lib/sync/ephy-password-manager.c
@@ -324,7 +324,7 @@ ephy_password_manager_init (EphyPasswordManager *self)
 {
   LOG ("Loading usernames into internal cache...");
   self->cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-  ephy_password_manager_query (self, NULL, NULL, NULL, NULL,
+  ephy_password_manager_query (self, NULL, NULL, NULL, NULL, NULL,
                                populate_cache_cb, self);
 }
 
@@ -446,7 +446,7 @@ update_password_cb (GList    *records,
   UpdatePasswordAsyncData *data = (UpdatePasswordAsyncData *)user_data;
   EphyPasswordRecord *record;
 
-  /* We only expect one matching record here. */
+  /* We expect only one matching record here. */
   g_assert (g_list_length (records) == 1);
 
   record = EPHY_PASSWORD_RECORD (records->data);
@@ -482,7 +482,8 @@ ephy_password_manager_save (EphyPasswordManager *self,
   if (!is_new) {
     LOG ("Updating password for (%s, %s, %s, %s)",
          uri, username, username_field, password_field);
-    ephy_password_manager_query (self, uri, username,
+    ephy_password_manager_query (self, NULL,
+                                 uri, username,
                                  username_field, password_field,
                                  update_password_cb,
                                  update_password_async_data_new (self, password));
@@ -562,6 +563,7 @@ out:
 
 void
 ephy_password_manager_query (EphyPasswordManager              *self,
+                             const char                       *id,
                              const char                       *uri,
                              const char                       *username,
                              const char                       *username_field,
@@ -577,7 +579,7 @@ ephy_password_manager_query (EphyPasswordManager              *self,
   LOG ("Querying password records for (%s, %s, %s, %s)",
        uri, username, username_field, password_field);
 
-  attributes = get_attributes_table (NULL, uri, username,
+  attributes = get_attributes_table (id, uri, username,
                                      username_field, password_field, -1);
   data = query_async_data_new (callback, user_data);
 
@@ -686,7 +688,7 @@ forget_cb (GList    *records,
   EphyPasswordManager *self = EPHY_PASSWORD_MANAGER (user_data);
   EphyPasswordRecord *record;
 
-  /* We only expect one matching record here. */
+  /* We expect only one matching record here. */
   g_assert (g_list_length (records) == 1);
 
   record = EPHY_PASSWORD_RECORD (records->data);
@@ -698,18 +700,16 @@ forget_cb (GList    *records,
 
 void
 ephy_password_manager_forget (EphyPasswordManager *self,
-                              const char          *hostname,
-                              const char          *username)
+                              const char          *id)
 {
   g_return_if_fail (EPHY_IS_PASSWORD_MANAGER (self));
-  g_return_if_fail (hostname);
-  g_return_if_fail (username);
+  g_return_if_fail (id);
 
   /* synchronizable-deleted signal needs an EphySynchronizable object,
    * therefore we need to obtain the password record first and then emit
    * the signal before clearing the password from the secret schema. */
-  ephy_password_manager_query (self, hostname, username,
-                               NULL, NULL,
+  ephy_password_manager_query (self, id,
+                               NULL, NULL, NULL, NULL,
                                forget_cb, self);
 }
 
@@ -741,7 +741,7 @@ ephy_password_manager_forget_all (EphyPasswordManager *self)
   /* synchronizable-deleted signal needs an EphySynchronizable object, therefore
    * we need to obtain the password records first and emit the signal for each
    * one before clearing the secret schema. */
-  ephy_password_manager_query (self, NULL, NULL, NULL, NULL,
+  ephy_password_manager_query (self, NULL, NULL, NULL, NULL, NULL,
                                forget_all_cb, self);
 }
 
@@ -809,22 +809,12 @@ replace_existing_cb (GList    *records,
 {
   ReplaceRecordAsyncData *data = (ReplaceRecordAsyncData *)user_data;
 
-  for (GList *l = records; l && l->data; l = l->next) {
-    /* NULL fields can cause the query to match other records too,
-     * so we need to make sure we have the record we've been looking for. */
-    if (!g_strcmp0 (ephy_password_record_get_hostname (records->data),
-                    ephy_password_record_get_hostname (data->record)) &&
-        !g_strcmp0 (ephy_password_record_get_username (records->data),
-                    ephy_password_record_get_username (data->record)) &&
-        !g_strcmp0 (ephy_password_record_get_username_field (records->data),
-                    ephy_password_record_get_username_field (data->record)) &&
-        !g_strcmp0 (ephy_password_record_get_password_field (records->data),
-                    ephy_password_record_get_password_field (data->record))) {
-      ephy_password_manager_forget_record (data->manager, records->data, data->record);
-      break;
-    }
-  }
+  /* We expect only one matching record here. */
+  g_assert (g_list_length (records) == 1);
+
+  ephy_password_manager_forget_record (data->manager, records->data, data->record);
 
+  g_list_free_full (records, g_object_unref);
   replace_record_async_data_free (data);
 }
 
@@ -835,11 +825,8 @@ ephy_password_manager_replace_existing (EphyPasswordManager *self,
   g_assert (EPHY_IS_PASSWORD_MANAGER (self));
   g_assert (EPHY_IS_PASSWORD_RECORD (record));
 
-  ephy_password_manager_query (self,
-                               ephy_password_record_get_hostname (record),
-                               ephy_password_record_get_username (record),
-                               ephy_password_record_get_username_field (record),
-                               ephy_password_record_get_password_field (record),
+  ephy_password_manager_query (self, ephy_password_record_get_id (record),
+                               NULL, NULL, NULL, NULL,
                                replace_existing_cb,
                                replace_record_async_data_new (self, record));
 }
@@ -1101,7 +1088,7 @@ synchronizable_manager_merge (EphySynchronizableManager              *manager,
 {
   EphyPasswordManager *self = EPHY_PASSWORD_MANAGER (manager);
 
-  ephy_password_manager_query (self, NULL, NULL, NULL, NULL,
+  ephy_password_manager_query (self, NULL, NULL, NULL, NULL, NULL,
                                merge_cb,
                                merge_passwords_async_data_new (self,
                                                                is_initial,
diff --git a/lib/sync/ephy-password-manager.h b/lib/sync/ephy-password-manager.h
index 9b027df..5afd4e6 100644
--- a/lib/sync/ephy-password-manager.h
+++ b/lib/sync/ephy-password-manager.h
@@ -55,6 +55,7 @@ void                 ephy_password_manager_save                     (EphyPasswor
                                                                      const char          *password_field,
                                                                      gboolean             is_new);
 void                 ephy_password_manager_query                    (EphyPasswordManager              *self,
+                                                                     const char                       *id,
                                                                      const char                       *uri,
                                                                      const char                       
*username,
                                                                      const char                       
*username_field,
@@ -62,8 +63,7 @@ void                 ephy_password_manager_query                    (EphyPasswor
                                                                      EphyPasswordManagerQueryCallback  
callback,
                                                                      gpointer                          
user_data);
 void                 ephy_password_manager_forget                    (EphyPasswordManager *self,
-                                                                      const char          *hostname,
-                                                                      const char          *username);
+                                                                      const char          *id);
 void                 ephy_password_manager_forget_all                (EphyPasswordManager *self);
 /* Note: Below functions are deprecated and should not be used in newly written code.
  * The only reason they still exist is that the profile migrator expects them. */
diff --git a/src/passwords-dialog.c b/src/passwords-dialog.c
index cdc6cf7..953cc78 100644
--- a/src/passwords-dialog.c
+++ b/src/passwords-dialog.c
@@ -176,9 +176,7 @@ forget (GSimpleAction *action,
     gtk_tree_model_get_iter (model, &iter, path);
     gtk_tree_model_get_value (model, &iter, COL_PASSWORDS_DATA, &val);
     record = g_value_get_object (&val);
-    ephy_password_manager_forget (dialog->manager,
-                                  ephy_password_record_get_hostname (record),
-                                  ephy_password_record_get_username (record));
+    ephy_password_manager_forget (dialog->manager, ephy_password_record_get_id (record));
     dialog->records = g_list_remove (dialog->records, record);
     g_object_unref (record);
     g_value_unset (&val);
@@ -437,7 +435,7 @@ populate_model (EphyPasswordsDialog *dialog)
 
   /* Ask for all password records. */
   ephy_password_manager_query (dialog->manager,
-                               NULL, NULL, NULL, NULL,
+                               NULL, NULL, NULL, NULL, NULL,
                                populate_model_cb, dialog);
 }
 


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