From d973101a1efc06579b110c4de89360919280da2e Mon Sep 17 00:00:00 2001 From: Carlos Garcia Campos Date: Sat, 9 Mar 2013 16:10:36 +0100 Subject: Add EphyFormAuthDataCache to ephy-form-auth-data And use it in EphyEmbedSingle to cache the form auth data. --- embed/ephy-embed-single.c | 131 +++------------------------------------- embed/ephy-embed-single.h | 6 -- embed/ephy-web-view.c | 2 +- lib/ephy-form-auth-data.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++ lib/ephy-form-auth-data.h | 18 ++++++ 5 files changed, 176 insertions(+), 129 deletions(-) diff --git a/embed/ephy-embed-single.c b/embed/ephy-embed-single.c index 0c01a7361..289fa162a 100644 --- a/embed/ephy-embed-single.c +++ b/embed/ephy-embed-single.c @@ -47,7 +47,7 @@ #define NSPLUGINWRAPPER_SETUP "/usr/bin/mozilla-plugin-config" struct _EphyEmbedSinglePrivate { - GHashTable *form_auth_data; + EphyFormAuthDataCache *form_auth_data_cache; #ifndef HAVE_WEBKIT2 SoupCache *cache; #endif @@ -55,98 +55,6 @@ struct _EphyEmbedSinglePrivate { G_DEFINE_TYPE (EphyEmbedSingle, ephy_embed_single, G_TYPE_OBJECT) -static void -form_auth_data_free (EphyEmbedSingleFormAuthData *data) -{ - g_free (data->form_username); - g_free (data->form_password); - g_free (data->username); - - g_slice_free (EphyEmbedSingleFormAuthData, data); -} - -static EphyEmbedSingleFormAuthData* -form_auth_data_new (const char *form_username, - const char *form_password, - const char *username) -{ - EphyEmbedSingleFormAuthData *data; - - data = g_slice_new (EphyEmbedSingleFormAuthData); - data->form_username = g_strdup (form_username); - data->form_password = g_strdup (form_password); - data->username = g_strdup (username); - - return data; -} - -static void -store_form_data_cb (SecretService *service, GAsyncResult *result, EphyEmbedSingle *single) -{ - GList *results, *p; - SecretItem *item; - GHashTable *attributes; - char *host, *form_username, *form_password, *username; - - GError *error = NULL; - - results = secret_service_search_finish (service, result, &error); - if (error != NULL) { - g_warning ("Error caching form data: %s", error->message); - g_error_free (error); - return; - } - - for (p = results; p; p = p->next) { - item = (SecretItem *)p->data; - attributes = secret_item_get_attributes (item); - host = ephy_string_get_host_name (g_hash_table_lookup (attributes, "uri")); - form_username = g_hash_table_lookup (attributes, FORM_USERNAME_KEY); - form_password = g_hash_table_lookup (attributes, FORM_PASSWORD_KEY); - username = g_hash_table_lookup (attributes, "username"); - - ephy_embed_single_add_form_auth (single, host, form_username, form_password, username); - - g_free (host); - g_hash_table_unref (attributes); - } - g_list_free_full (results, (GDestroyNotify)g_object_unref); -} - -static void -cache_secret_form_data (EphyEmbedSingle *single) -{ - GHashTable *attributes; - - attributes = secret_attributes_build (EPHY_FORM_PASSWORD_SCHEMA, NULL); - secret_service_search (NULL, - EPHY_FORM_PASSWORD_SCHEMA, - attributes, - SECRET_SEARCH_UNLOCK | SECRET_SEARCH_ALL, - NULL, - (GAsyncReadyCallback)store_form_data_cb, - single); - g_hash_table_unref (attributes); -} - -static void -free_form_auth_data_list (gpointer data) -{ - GSList *p, *l = (GSList*)data; - - for (p = l; p; p = p->next) - form_auth_data_free ((EphyEmbedSingleFormAuthData*)p->data); - - g_slist_free (l); -} - -static void -remove_form_auth_data (gpointer key, gpointer value, gpointer user_data) -{ - if (value) - free_form_auth_data_list ((GSList*)value); -} - static void ephy_embed_single_dispose (GObject *object) { @@ -169,12 +77,7 @@ ephy_embed_single_finalize (GObject *object) { EphyEmbedSinglePrivate *priv = EPHY_EMBED_SINGLE (object)->priv; - if (priv->form_auth_data) { - g_hash_table_foreach (priv->form_auth_data, - (GHFunc)remove_form_auth_data, - NULL); - g_hash_table_destroy (priv->form_auth_data); - } + ephy_form_auth_data_cache_free (priv->form_auth_data_cache); G_OBJECT_CLASS (ephy_embed_single_parent_class)->finalize (object); } @@ -186,11 +89,7 @@ ephy_embed_single_init (EphyEmbedSingle *single) single->priv = priv = EPHY_EMBED_SINGLE_GET_PRIVATE (single); - priv->form_auth_data = g_hash_table_new_full (g_str_hash, - g_str_equal, - g_free, - NULL); - cache_secret_form_data (single); + priv->form_auth_data_cache = ephy_form_auth_data_cache_new (); } static void @@ -396,13 +295,13 @@ ephy_embed_single_clear_cache (EphyEmbedSingle *single) * @uri: the URI of a web page * * Gets a #GSList of all stored login/passwords, in - * #EphyEmbedSingleFormAuthData format, for any form in @uri, or %NULL + * #EphyFormAuthData format, for any form in @uri, or %NULL * if we have none. * - * The #EphyEmbedSingleFormAuthData structs and the #GSList are owned + * The #EphyFormAuthData structs and the #GSList are owned * by @single and should not be freed by the user. * - * Returns: (transfer none) (element-type EphyEmbedSingleFormAuthData): #GSList with the possible auto-fills for the forms + * Returns: (transfer none) (element-type EphyFormAuthData): #GSList with the possible auto-fills for the forms * in @uri, or %NULL **/ GSList * @@ -412,11 +311,10 @@ ephy_embed_single_get_form_auth (EphyEmbedSingle *single, EphyEmbedSinglePrivate *priv; g_return_val_if_fail (EPHY_IS_EMBED_SINGLE (single), NULL); - g_return_val_if_fail (uri, NULL); priv = single->priv; - return g_hash_table_lookup (priv->form_auth_data, uri); + return ephy_form_auth_data_cache_get_list (priv->form_auth_data_cache, uri); } /** @@ -438,25 +336,14 @@ ephy_embed_single_add_form_auth (EphyEmbedSingle *single, const char *form_password, const char *username) { - EphyEmbedSingleFormAuthData *form_data; EphyEmbedSinglePrivate *priv; - GSList *l; g_return_if_fail (EPHY_IS_EMBED_SINGLE (single)); - g_return_if_fail (uri); - g_return_if_fail (form_username); - g_return_if_fail (form_password); - g_return_if_fail (username); priv = single->priv; LOG ("Appending: name field: %s / pass field: %s / username: %s / uri: %s", form_username, form_password, username, uri); - form_data = form_auth_data_new (form_username, form_password, username); - l = g_hash_table_lookup (priv->form_auth_data, - uri); - l = g_slist_append (l, form_data); - g_hash_table_replace (priv->form_auth_data, - g_strdup (uri), - l); + ephy_form_auth_data_cache_add (priv->form_auth_data_cache, + uri, form_username, form_password, username); } diff --git a/embed/ephy-embed-single.h b/embed/ephy-embed-single.h index f620cdb9c..79f411649 100644 --- a/embed/ephy-embed-single.h +++ b/embed/ephy-embed-single.h @@ -41,12 +41,6 @@ typedef struct _EphyEmbedSingle EphyEmbedSingle; typedef struct _EphyEmbedSingleClass EphyEmbedSingleClass; typedef struct _EphyEmbedSinglePrivate EphyEmbedSinglePrivate; -typedef struct { - char *form_username; - char *form_password; - char *username; -} EphyEmbedSingleFormAuthData; - struct _EphyEmbedSingle { GObject parent; diff --git a/embed/ephy-web-view.c b/embed/ephy-web-view.c index d86233330..f5627d8dc 100644 --- a/embed/ephy-web-view.c +++ b/embed/ephy-web-view.c @@ -762,7 +762,7 @@ pre_fill_form (WebKitDOMNode *username_node, for (p = l; p; p = p->next) { char *username_field_name; char *password_field_name; - EphyEmbedSingleFormAuthData *data = (EphyEmbedSingleFormAuthData*)p->data; + EphyFormAuthData *data = (EphyFormAuthData *)p->data; g_object_get (username_node, "name", &username_field_name, NULL); diff --git a/lib/ephy-form-auth-data.c b/lib/ephy-form-auth-data.c index b1bf50cce..ea0580247 100644 --- a/lib/ephy-form-auth-data.c +++ b/lib/ephy-form-auth-data.c @@ -22,6 +22,8 @@ #include "config.h" #include "ephy-form-auth-data.h" +#include "ephy-string.h" + #include #include @@ -249,3 +251,149 @@ ephy_form_auth_data_query (const char *uri, g_free (key_str); } +static EphyFormAuthData * +ephy_form_auth_data_new (const char *form_username, + const char *form_password, + const char *username) +{ + EphyFormAuthData *data; + + data = g_slice_new (EphyFormAuthData); + data->form_username = g_strdup (form_username); + data->form_password = g_strdup (form_password); + data->username = g_strdup (username); + + return data; +} + +static void +ephy_form_auth_data_free (EphyFormAuthData *data) +{ + g_free (data->form_username); + g_free (data->form_password); + g_free (data->username); + + g_slice_free (EphyFormAuthData, data); +} + +static void +screcet_service_search_finished (SecretService *service, + GAsyncResult *result, + EphyFormAuthDataCache *cache) +{ + GList *results, *p; + GError *error = NULL; + + results = secret_service_search_finish (service, result, &error); + if (error != NULL) { + g_warning ("Error caching form data: %s", error->message); + g_error_free (error); + return; + } + + for (p = results; p; p = p->next) { + SecretItem *item = (SecretItem *)p->data; + GHashTable *attributes; + char *host; + + attributes = secret_item_get_attributes (item); + host = ephy_string_get_host_name (g_hash_table_lookup (attributes, URI_KEY)); + ephy_form_auth_data_cache_add (cache, host, + g_hash_table_lookup (attributes, FORM_USERNAME_KEY), + g_hash_table_lookup (attributes, FORM_PASSWORD_KEY), + g_hash_table_lookup (attributes, USERNAME_KEY)); + + g_free (host); + g_hash_table_unref (attributes); + } + + g_list_free_full (results, g_object_unref); +} + +static void +ephy_form_auth_data_cache_init (EphyFormAuthDataCache *cache) +{ + GHashTable *attributes; + + attributes = secret_attributes_build (EPHY_FORM_PASSWORD_SCHEMA, NULL); + secret_service_search (NULL, + EPHY_FORM_PASSWORD_SCHEMA, + attributes, + SECRET_SEARCH_UNLOCK | SECRET_SEARCH_ALL, + NULL, + (GAsyncReadyCallback)screcet_service_search_finished, + cache); + g_hash_table_unref (attributes); +} + +struct _EphyFormAuthDataCache { + GHashTable *form_auth_data_map; +}; + +EphyFormAuthDataCache * +ephy_form_auth_data_cache_new (void) +{ + EphyFormAuthDataCache *cache = g_slice_new (EphyFormAuthDataCache); + + cache->form_auth_data_map = g_hash_table_new_full (g_str_hash, + g_str_equal, + g_free, + NULL); + ephy_form_auth_data_cache_init (cache); + + return cache; +} + +static void +form_auth_data_map_free_value (gpointer key, + gpointer value, + gpointer user_data) +{ + g_slist_free_full ((GSList *)value, (GDestroyNotify)ephy_form_auth_data_free); +} + +void +ephy_form_auth_data_cache_free (EphyFormAuthDataCache *cache) +{ + g_return_if_fail (cache); + + g_hash_table_foreach (cache->form_auth_data_map, + (GHFunc)form_auth_data_map_free_value, + NULL); + g_hash_table_destroy (cache->form_auth_data_map); + + g_slice_free (EphyFormAuthDataCache, cache); +} + +void +ephy_form_auth_data_cache_add (EphyFormAuthDataCache *cache, + const char *uri, + const char *form_username, + const char *form_password, + const char *username) +{ + EphyFormAuthData *data; + GSList *l; + + g_return_if_fail (cache); + g_return_if_fail (uri); + g_return_if_fail (form_username); + g_return_if_fail (form_password); + g_return_if_fail (username); + + data = ephy_form_auth_data_new (form_username, form_password, username); + l = g_hash_table_lookup (cache->form_auth_data_map, uri); + l = g_slist_append (l, data); + g_hash_table_replace (cache->form_auth_data_map, + g_strdup (uri), l); +} + +GSList * +ephy_form_auth_data_cache_get_list (EphyFormAuthDataCache *cache, + const char *uri) +{ + g_return_val_if_fail (cache, NULL); + g_return_val_if_fail (uri, NULL); + + return g_hash_table_lookup (cache->form_auth_data_map, uri); +} diff --git a/lib/ephy-form-auth-data.h b/lib/ephy-form-auth-data.h index 5bd3bfdf3..33333719c 100644 --- a/lib/ephy-form-auth-data.h +++ b/lib/ephy-form-auth-data.h @@ -56,4 +56,22 @@ const SecretSchema *ephy_form_auth_data_get_password_schema (void) G_GNUC_CONST; #define EPHY_FORM_PASSWORD_SCHEMA ephy_form_auth_data_get_password_schema () +typedef struct { + char *form_username; + char *form_password; + char *username; +} EphyFormAuthData; + +typedef struct _EphyFormAuthDataCache EphyFormAuthDataCache; + +EphyFormAuthDataCache *ephy_form_auth_data_cache_new (void); +void ephy_form_auth_data_cache_free (EphyFormAuthDataCache *cache); +void ephy_form_auth_data_cache_add (EphyFormAuthDataCache *cache, + const char *uri, + const char *form_username, + const char *form_password, + const char *username); +GSList *ephy_form_auth_data_cache_get_list (EphyFormAuthDataCache *cache, + const char *uri); + #endif -- cgit v1.2.3