[epiphany] Move find_username_and_password_elements to ephy-web-dom-utils



commit 78b70aa84bb94f5097d40ffbb461aefcbbaeb979
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Sat Mar 9 13:04:02 2013 +0100

    Move find_username_and_password_elements to ephy-web-dom-utils
    
    Renamed as ephy_web_dom_utils_find_form_auth_elements(). Also fixed
    possible memory leaks in case of multiple auth input fields found. It
    now returns a boolean indicating whther form auth elements were found.

 embed/ephy-web-view.c    |   70 ++------------------------------------------
 lib/ephy-web-dom-utils.c |   72 ++++++++++++++++++++++++++++++++++++++++++----
 lib/ephy-web-dom-utils.h |   11 +++----
 3 files changed, 75 insertions(+), 78 deletions(-)
---
diff --git a/embed/ephy-web-view.c b/embed/ephy-web-view.c
index 16e1d83..c5dc5a9 100644
--- a/embed/ephy-web-view.c
+++ b/embed/ephy-web-view.c
@@ -548,66 +548,6 @@ fill_form_cb (const char *username,
                 "value", password, NULL);
 }
 
-static void
-find_username_and_password_elements (WebKitDOMNode *form_node,
-                                     WebKitDOMNode **username_node,
-                                     WebKitDOMNode **password_node)
-{
-  WebKitDOMHTMLCollection *elements;
-  WebKitDOMHTMLFormElement *form = WEBKIT_DOM_HTML_FORM_ELEMENT (form_node);
-  gulong elements_n;
-  int j;
-
-  elements = webkit_dom_html_form_element_get_elements (form);
-  elements_n = webkit_dom_html_collection_get_length (elements);
-
-  if (elements_n == 0) {
-    LOG ("No elements found for this form.");
-    return;
-  }
-
-  for (j = 0; j < elements_n; j++) {
-    WebKitDOMNode *element;
-
-    element = webkit_dom_html_collection_item (elements, j);
-
-    if (WEBKIT_DOM_IS_HTML_INPUT_ELEMENT (element)) {
-      char *element_type;
-
-      g_object_get (element, "type", &element_type, NULL);
-
-      if (g_str_equal ("text", element_type) || g_str_equal ("email", element_type)) {
-        /* We found more than one inputs of type text; we won't be
-         * saving here */
-        if (*username_node) {
-          g_object_unref (*username_node);
-          *username_node = NULL;
-          g_free (element_type);
-
-          break;
-        }
-
-        *username_node = g_object_ref (element);
-      }
-      else if (g_str_equal ("password", element_type)) {
-        if (*password_node) {
-          g_object_unref (*password_node);
-          *password_node = NULL;
-          g_free (element_type);
-
-          break;
-        }
-
-        *password_node = g_object_ref (element);
-      }
-
-      g_free (element_type);
-    }
-  }
-
-  g_object_unref(elements);
-}
-
 typedef struct {
   EphyEmbed *embed;
   char *uri;
@@ -771,8 +711,7 @@ form_submitted_cb (WebKitDOMHTMLFormElement *dom_form,
 
   soup_uri_set_query (uri, NULL);
 
-  find_username_and_password_elements (WEBKIT_DOM_NODE (dom_form),
-                                       &username_node, &password_node);
+  ephy_web_dom_utils_find_form_auth_elements (dom_form, &username_node, &password_node);
 
   store_data = g_slice_new (StorePasswordData);
 
@@ -872,15 +811,14 @@ _ephy_web_view_hook_into_forms (EphyWebView *web_view)
   }
 
   for (i = 0; i < forms_n; i++) {
-    WebKitDOMNode *form;
+    WebKitDOMHTMLFormElement *form;
     WebKitDOMNode *username_node = NULL;
     WebKitDOMNode *password_node = NULL;
 
-    form = webkit_dom_html_collection_item (forms, i);
-    find_username_and_password_elements (form, &username_node, &password_node);
+    form = WEBKIT_DOM_HTML_FORM_ELEMENT (webkit_dom_html_collection_item (forms, i));
 
     /* We have a field that may be the user, and one for a password. */
-    if (username_node && password_node) {
+    if (ephy_web_dom_utils_find_form_auth_elements (form, &username_node, &password_node)) {
       LOG ("Hooking and pre-filling a form");
       webkit_dom_event_target_add_event_listener (WEBKIT_DOM_EVENT_TARGET (form), "submit",
                                                   G_CALLBACK (form_submitted_cb), false,
diff --git a/lib/ephy-web-dom-utils.c b/lib/ephy-web-dom-utils.c
index 375a7be..1224c56 100644
--- a/lib/ephy-web-dom-utils.c
+++ b/lib/ephy-web-dom-utils.c
@@ -23,12 +23,6 @@
 #include "ephy-web-dom-utils.h"
 
 #include <libsoup/soup.h>
-#ifdef HAVE_WEBKIT2
-#include <webkit2/webkit2.h>
-#include <webkit2/webkit-web-extension.h>
-#else
-#include <webkit/webkit.h>
-#endif
 
 /**
  * ephy_web_dom_utils_has_modified_forms:
@@ -354,3 +348,69 @@ ephy_web_dom_utils_get_best_icon (WebKitDOMDocument *document,
 
   return ret;
 }
+
+gboolean
+ephy_web_dom_utils_find_form_auth_elements (WebKitDOMHTMLFormElement *form,
+                                            WebKitDOMNode **username,
+                                            WebKitDOMNode **password)
+{
+  WebKitDOMHTMLCollection *elements;
+  WebKitDOMNode *username_node = NULL;
+  WebKitDOMNode *password_node = NULL;
+  guint i, n_elements;
+  gboolean found_auth_elements = FALSE;
+
+  elements = webkit_dom_html_form_element_get_elements (form);
+  n_elements = webkit_dom_html_collection_get_length (elements);
+
+  for (i = 0; i < n_elements; i++) {
+    WebKitDOMNode *element;
+    char *element_type;
+
+    element = webkit_dom_html_collection_item (elements, i);
+    if (!WEBKIT_DOM_IS_HTML_INPUT_ELEMENT (element))
+      continue;
+
+    g_object_get (element, "type", &element_type, NULL);
+
+    if (g_str_equal (element_type, "text") || g_str_equal (element_type, "email")) {
+      /* We found more than one inputs of type text; we won't be saving here. */
+      if (username_node) {
+        g_free (element_type);
+        found_auth_elements = FALSE;
+        break;
+      }
+
+      username_node = g_object_ref (element);
+      found_auth_elements = TRUE;
+    } else if (g_str_equal (element_type, "password")) {
+      /* We found more than one inputs of type password; we won't be saving here. */
+      if (password_node) {
+        g_free (element_type);
+        found_auth_elements = FALSE;
+        break;
+      }
+
+      password_node = g_object_ref (element);
+      found_auth_elements = TRUE;
+    }
+
+    g_free (element_type);
+  }
+
+  g_object_unref(elements);
+
+  if (found_auth_elements && username_node && password_node) {
+    *username = username_node;
+    *password = password_node;
+
+    return TRUE;
+  }
+
+  if (username_node)
+    g_object_unref (username_node);
+  if (password_node)
+    g_object_unref (password_node);
+
+  return FALSE;
+}
diff --git a/lib/ephy-web-dom-utils.h b/lib/ephy-web-dom-utils.h
index a998d4a..aa052d2 100644
--- a/lib/ephy-web-dom-utils.h
+++ b/lib/ephy-web-dom-utils.h
@@ -24,12 +24,7 @@
 #ifndef EPHY_WEB_DOM_UTILS_H
 #define EPHY_WEB_DOM_UTILS_H
 
-#ifdef HAVE_WEBKIT2
-#include <webkit2/webkit2.h>
-#include <webkit2/webkit-web-extension.h>
-#else
-#include <webkit/webkit.h>
-#endif
+#include <webkitdom/webkitdom.h>
 
 G_BEGIN_DECLS
 
@@ -42,6 +37,10 @@ gboolean ephy_web_dom_utils_get_best_icon (WebKitDOMDocument *document,
                                            char             **uri_out,
                                            char             **color_out);
 
+gboolean ephy_web_dom_utils_find_form_auth_elements (WebKitDOMHTMLFormElement *form,
+                                                     WebKitDOMNode           **username,
+                                                     WebKitDOMNode           **password);
+
 G_END_DECLS
 
 #endif


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