[epiphany] embed-utils: Add ephy_embed_utils_address_is_valid



commit 2de9626b1506a2cfa9a2567e733f432805c4b2c4
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Mon Feb 24 15:15:33 2014 +0100

    embed-utils: Add ephy_embed_utils_address_is_valid
    
    Helper method used to check if a url is valid to decide whether to load
    it or perform a search. It's mostly the same expression used by
    EphyWebView, but moved to a helper function. The regular expressions used
    in that expression are also moved to embed-utils and they are only
    compiled once instead of everytime a web view is created.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=725081

 embed/ephy-embed-shell.c |    2 +
 embed/ephy-embed-utils.c |   91 ++++++++++++++++++++++++++++++++++++++++++++++
 embed/ephy-embed-utils.h |    2 +
 embed/ephy-web-view.c    |   58 +-----------------------------
 4 files changed, 96 insertions(+), 57 deletions(-)
---
diff --git a/embed/ephy-embed-shell.c b/embed/ephy-embed-shell.c
index c919c19..99bc61d 100644
--- a/embed/ephy-embed-shell.c
+++ b/embed/ephy-embed-shell.c
@@ -27,6 +27,7 @@
 #include "ephy-embed-prefs.h"
 #include "ephy-embed-private.h"
 #include "ephy-embed-type-builtins.h"
+#include "ephy-embed-utils.h"
 #include "ephy-encodings.h"
 #include "ephy-file-helpers.h"
 #include "ephy-history-service.h"
@@ -638,6 +639,7 @@ ephy_embed_shell_shutdown (GApplication* application)
   g_list_foreach (priv->web_extensions, (GFunc)ephy_embed_shell_unwatch_web_extension, application);
 
   g_object_unref (ephy_embed_prefs_get_web_view_group ());
+  ephy_embed_utils_shutdown ();
 }
 
 static void
diff --git a/embed/ephy-embed-utils.c b/embed/ephy-embed-utils.c
index 6eb5c4a..26a0a62 100644
--- a/embed/ephy-embed-utils.c
+++ b/embed/ephy-embed-utils.c
@@ -25,12 +25,16 @@
 #include "ephy-embed-utils.h"
 
 #include "ephy-about-handler.h"
+#include "ephy-embed-private.h"
 #include "ephy-string.h"
 
 #include <string.h>
 #include <glib/gi18n.h>
 #include <libsoup/soup.h>
 
+static GRegex *non_search_regex;
+static GRegex *domain_regex;
+
 char *
 ephy_embed_utils_link_message_parse (const char *message)
 {
@@ -70,6 +74,38 @@ ephy_embed_utils_link_message_parse (const char *message)
   return g_string_free (tmp, FALSE);
 }
 
+static gpointer
+create_non_search_regex (gpointer user_data)
+{
+  non_search_regex = g_regex_new (EPHY_WEB_VIEW_NON_SEARCH_REGEX,
+                                  G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, NULL);
+  return non_search_regex;
+}
+
+static GRegex *
+get_non_search_regex (void)
+{
+  static GOnce once_init = G_ONCE_INIT;
+
+  return g_once (&once_init, create_non_search_regex, NULL);
+}
+
+static gpointer
+create_domain_regex (gpointer user_data)
+{
+  domain_regex = g_regex_new (EPHY_WEB_VIEW_DOMAIN_REGEX,
+                              G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, NULL);
+  return domain_regex;
+}
+
+static GRegex *
+get_domain_regex (void)
+{
+  static GOnce once_init = G_ONCE_INIT;
+
+  return g_once (&once_init, create_domain_regex, NULL);
+}
+
 gboolean
 ephy_embed_utils_address_has_web_scheme (const char *address)
 {
@@ -104,6 +140,54 @@ ephy_embed_utils_address_is_existing_absolute_filename (const char *address)
     g_file_test (address, G_FILE_TEST_EXISTS);
 }
 
+static gboolean
+is_public_domain (const char *address)
+{
+  char *host;
+  gboolean retval = FALSE;
+
+  host = ephy_string_get_host_name (address);
+  if (!host)
+    return FALSE;
+
+  if (g_regex_match (get_domain_regex (), host, 0, NULL)) {
+    if (g_str_equal (host, "localhost"))
+      retval = TRUE;
+    else {
+      const char *end;
+
+      end = g_strrstr (host, ".");
+      if (end && *end != '\0')
+        retval = soup_tld_domain_is_public_suffix (end);
+    }
+  }
+
+  g_free (host);
+
+  return retval;
+}
+
+gboolean
+ephy_embed_utils_address_is_valid (const char *address)
+{
+  char *scheme;
+  gboolean retval;
+
+  if (!address)
+    return FALSE;
+
+  scheme = g_uri_parse_scheme (address);
+
+  retval = scheme ||
+    ephy_embed_utils_address_is_existing_absolute_filename (address) ||
+    g_regex_match (get_non_search_regex (), address, 0, NULL) ||
+    is_public_domain (address);
+
+  g_free (scheme);
+
+  return retval;
+}
+
 char*
 ephy_embed_utils_normalize_address (const char *address)
 {
@@ -186,3 +270,10 @@ ephy_embed_utils_is_no_show_address (const char *address)
 
   return FALSE;
 }
+
+void
+ephy_embed_utils_shutdown (void)
+{
+  g_clear_pointer (&non_search_regex, (GDestroyNotify)g_regex_unref);
+  g_clear_pointer (&domain_regex, (GDestroyNotify)g_regex_unref);
+}
diff --git a/embed/ephy-embed-utils.h b/embed/ephy-embed-utils.h
index 0b9af55..2cb6a7e 100644
--- a/embed/ephy-embed-utils.h
+++ b/embed/ephy-embed-utils.h
@@ -40,9 +40,11 @@ G_BEGIN_DECLS
 char*    ephy_embed_utils_link_message_parse                    (const char *message);
 gboolean ephy_embed_utils_address_has_web_scheme                (const char *address);
 gboolean ephy_embed_utils_address_is_existing_absolute_filename (const char *address);
+gboolean ephy_embed_utils_address_is_valid                      (const char *address);
 char*    ephy_embed_utils_normalize_address                     (const char *address);
 gboolean ephy_embed_utils_url_is_empty                          (const char *location);
 gboolean ephy_embed_utils_is_no_show_address                    (const char *address);
+void     ephy_embed_utils_shutdown                              (void);
 
 G_END_DECLS
 
diff --git a/embed/ephy-web-view.c b/embed/ephy-web-view.c
index 4ef4007..f8228b9 100644
--- a/embed/ephy-web-view.c
+++ b/embed/ephy-web-view.c
@@ -709,16 +709,6 @@ ephy_web_view_finalize (GObject *object)
 {
   EphyWebViewPrivate *priv = EPHY_WEB_VIEW (object)->priv;
 
-  if (priv->non_search_regex) {
-    g_regex_unref (priv->non_search_regex);
-    priv->non_search_regex = NULL;
-  }
-
-  if (priv->domain_regex) {
-    g_regex_unref (priv->domain_regex);
-    priv->domain_regex = NULL;
-  }
-
   ephy_web_view_popups_manager_reset (EPHY_WEB_VIEW (object));
 
   g_free (priv->address);
@@ -1955,12 +1945,6 @@ ephy_web_view_init (EphyWebView *web_view)
 
   priv->file_monitor = ephy_file_monitor_new (web_view);
 
-  priv->non_search_regex = g_regex_new (EPHY_WEB_VIEW_NON_SEARCH_REGEX,
-                                        G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, NULL);
-
-  priv->domain_regex = g_regex_new (EPHY_WEB_VIEW_DOMAIN_REGEX,
-                                    G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, NULL);
-
   priv->history_service = EPHY_HISTORY_SERVICE (ephy_embed_shell_get_global_history_service 
(ephy_embed_shell_get_default ()));
   priv->history_service_cancellable = g_cancellable_new ();
 
@@ -2045,34 +2029,6 @@ ephy_web_view_new_with_related_view (WebKitWebView *related_view)
                        NULL);
 }
 
-static gboolean
-is_public_domain (EphyWebView *view, const char *url)
-{
-  gboolean retval = FALSE;
-  char *host = NULL;
-  EphyWebViewPrivate *priv = view->priv;
-
-  host = ephy_string_get_host_name (url);
-  g_return_val_if_fail (host, FALSE);
-
-  if (g_regex_match (priv->domain_regex, host, 0, NULL)) {
-    if (g_str_equal (host, "localhost"))
-      retval = TRUE;
-    else {
-      const char *end;
-
-      end = g_strrstr (host, ".");
-
-      if (end && *end != '\0')
-        retval = soup_tld_domain_is_public_suffix (end);
-    }
-  }
-
-  g_free (host);
-
-  return retval;
-}
-
 /**
  * ephy_web_view_normalize_or_autosearch_url:
  * @view: an %EphyWebView
@@ -2087,21 +2043,12 @@ char*
 ephy_web_view_normalize_or_autosearch_url (EphyWebView *view, const char *url)
 {
   char *effective_url;
-  char *scheme;
-  EphyWebViewPrivate *priv = view->priv;
 
   g_return_val_if_fail (EPHY_IS_WEB_VIEW (view), NULL);
   g_return_val_if_fail (url, NULL);
 
-  scheme = g_uri_parse_scheme (url);
-
   /* If the string doesn't look like an URI, let's search it; */
-  if (!ephy_embed_utils_address_has_web_scheme (url) &&
-      scheme == NULL &&
-      !ephy_embed_utils_address_is_existing_absolute_filename (url) &&
-      priv->non_search_regex &&
-      !g_regex_match (priv->non_search_regex, url, 0, NULL) &&
-      !is_public_domain (view, url)) {
+  if (!ephy_embed_utils_address_is_valid (url)) {
     char *query_param, *url_search;
 
     url_search = g_settings_get_string (EPHY_SETTINGS_MAIN,
@@ -2121,9 +2068,6 @@ ephy_web_view_normalize_or_autosearch_url (EphyWebView *view, const char *url)
   } else
     effective_url = ephy_embed_utils_normalize_address (url);
 
-  if (scheme)
-    g_free (scheme);
-
   return effective_url;
 }
 


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