]> spindle.queued.net Git - midori/commitdiff
Move DNS prefetching into the core
authorChristian Dywan <christian@twotoasts.de>
Fri, 1 Jan 2010 16:53:58 +0000 (17:53 +0100)
committerChristian Dywan <christian@twotoasts.de>
Fri, 1 Jan 2010 17:16:55 +0000 (18:16 +0100)
The feature is going to be included with WebKitGTK+ and
having it in the core allows us to prefetch bookmarks as well.

extensions/dnsprefetch.c [deleted file]
midori/midori-browser.c
midori/midori-view.c
midori/sokoke.c
midori/sokoke.h
tests/magic-uri.c
wscript

diff --git a/extensions/dnsprefetch.c b/extensions/dnsprefetch.c
deleted file mode 100644 (file)
index 0667534..0000000
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- Copyright (C) 2009 Alexander Butenko <a.butenka@gmail.com>
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
-*/
-
-#include <midori/midori.h>
-
-#include "config.h"
-
-#include <glib/gstdio.h>
-#if HAVE_UNISTD_H
-    #include <unistd.h>
-#endif
-
-#define MAXHOSTS 50
-static gchar* hosts = NULL;
-static int host_count;
-
-static bool
-dnsprefetch_do_prefetch (const char* uri)
-{
-     SoupURI* s_uri;
-
-     if (!uri)
-        return FALSE;
-     s_uri = soup_uri_new (uri);
-     if (!s_uri || !s_uri->host)
-         return FALSE;
-
-     #if GLIB_CHECK_VERSION (2, 22, 0)
-     if (g_hostname_is_ip_address (s_uri->host))
-     #else
-     if (g_ascii_isdigit (s_uri->host[0]) && g_strstr_len (s_uri->host, 4, "."))
-     #endif
-     {
-         soup_uri_free (s_uri);
-         return FALSE;
-     }
-     if (!g_str_has_prefix (uri, "http"))
-     {
-         soup_uri_free (s_uri);
-         return FALSE;
-     }
-
-     if (!g_regex_match_simple (s_uri->host, hosts,
-                                G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY))
-     {
-         SoupAddress* address;
-         gchar* new_hosts;
-
-         address = soup_address_new (s_uri->host, SOUP_ADDRESS_ANY_PORT);
-         soup_address_resolve_async (address, 0, 0, 0, 0);
-         g_object_unref (address);
-
-         if (host_count > MAXHOSTS)
-         {
-             katze_assign (hosts, g_strdup (""));
-             host_count = 0;
-         }
-         host_count++;
-         new_hosts = g_strdup_printf ("%s|%s", hosts, s_uri->host);
-         katze_assign (hosts, new_hosts);
-     }
-     soup_uri_free (s_uri);
-     return TRUE;
-}
-
-static void
-dnsprefetch_prefetch_cb (WebKitWebView* web_view,
-                         const gchar*   title,
-                         const char*    uri,
-                         gpointer       user_data)
-{
-     dnsprefetch_do_prefetch (uri);
-}
-
-static void
-dnsprefetch_add_tab_cb (MidoriBrowser* browser,
-                        MidoriView*    view)
-{
-    GtkWidget* web_view = gtk_bin_get_child (GTK_BIN (view));
-    g_signal_connect (web_view, "hovering-over-link",
-            G_CALLBACK (dnsprefetch_prefetch_cb), 0);
-}
-
-static void
-dnsprefetch_deactivate_cb (MidoriExtension* extension,
-                           MidoriBrowser*   browser);
-
-static void
-dnsprefetch_add_tab_foreach_cb (MidoriView*    view,
-                                MidoriBrowser* browser)
-{
-    dnsprefetch_add_tab_cb (browser, view);
-}
-
-static void
-dnsprefetch_app_add_browser_cb (MidoriApp*       app,
-                                MidoriBrowser*   browser,
-                                MidoriExtension* extension)
-{
-    midori_browser_foreach (browser,
-        (GtkCallback)dnsprefetch_add_tab_foreach_cb, browser);
-    g_signal_connect (browser, "add-tab",
-        G_CALLBACK (dnsprefetch_add_tab_cb), 0);
-    g_signal_connect (extension, "deactivate",
-        G_CALLBACK (dnsprefetch_deactivate_cb), browser);
-}
-
-static void
-dnsprefetch_deactivate_tabs (MidoriView*    view,
-                             MidoriBrowser* browser)
-{
-    GtkWidget* web_view = gtk_bin_get_child (GTK_BIN (view));
-    g_signal_handlers_disconnect_by_func (
-       browser, dnsprefetch_add_tab_cb, 0);
-    g_signal_handlers_disconnect_by_func (
-       web_view, dnsprefetch_do_prefetch, 0);
-}
-
-static void
-dnsprefetch_deactivate_cb (MidoriExtension* extension,
-                       MidoriBrowser*   browser)
-{
-    MidoriApp* app = midori_extension_get_app (extension);
-
-    katze_assign (hosts, g_strdup (""));
-    host_count = 0;
-
-    g_signal_handlers_disconnect_by_func (
-        extension, dnsprefetch_deactivate_cb, browser);
-    g_signal_handlers_disconnect_by_func (
-        app, dnsprefetch_app_add_browser_cb, extension);
-    midori_browser_foreach (browser, (GtkCallback)dnsprefetch_deactivate_tabs, browser);
-
-}
-
-static void
-dnsprefetch_activate_cb (MidoriExtension* extension,
-                         MidoriApp*       app)
-{
-    KatzeArray* browsers;
-    MidoriBrowser* browser;
-    guint i;
-
-    katze_assign (hosts, g_strdup (""));
-    host_count = 0;
-
-    browsers = katze_object_get_object (app, "browsers");
-    i = 0;
-    while ((browser = katze_array_get_nth_item (browsers, i++)))
-        dnsprefetch_app_add_browser_cb (app, browser, extension);
-    g_signal_connect (app, "add-browser",
-        G_CALLBACK (dnsprefetch_app_add_browser_cb), extension);
-
-    g_object_unref (browsers);
-}
-
-#if G_ENABLE_DEBUG
-static void
-dnsprefetch_parse (void)
-{
-    g_assert (!dnsprefetch_do_prefetch (NULL));
-    g_assert (dnsprefetch_do_prefetch ("http://google.com"));
-    g_assert (dnsprefetch_do_prefetch ("http://google.com"));
-    g_assert (dnsprefetch_do_prefetch ("http://googlecom"));
-    g_assert (dnsprefetch_do_prefetch ("http://1kino.com"));
-    g_assert (dnsprefetch_do_prefetch ("http://"));
-    g_assert (!dnsprefetch_do_prefetch ("http:/"));
-    g_assert (!dnsprefetch_do_prefetch ("http"));
-    g_assert (!dnsprefetch_do_prefetch ("ftp://ftphost.org"));
-    g_assert (!dnsprefetch_do_prefetch ("http://10.0.0.1"));
-    g_assert (!dnsprefetch_do_prefetch ("about:blank"));
-    g_assert (!dnsprefetch_do_prefetch ("javascript: alert()"));
-}
-
-void
-extension_test (void)
-{
-    katze_assign (hosts, g_strdup (""));
-    host_count = 0;
-    g_test_add_func ("/extensions/dnsprefetch/parse", dnsprefetch_parse);
-}
-#endif
-
-MidoriExtension*
-extension_init (void)
-{
-    MidoriExtension* extension = g_object_new (MIDORI_TYPE_EXTENSION,
-        "name", _("DNS prefetching"),
-        "description", _("Prefetch IP addresses of hovered links"),
-        "version", "0.1",
-        "authors", "Alexander V. Butenko <a.butenka@gmail.com>",
-        NULL);
-    g_signal_connect (extension, "activate",
-         G_CALLBACK (dnsprefetch_activate_cb), NULL);
-
-    return extension;
-}
index 77c42c673723b5c13cc456e27724cfff963515eb..13f069675ddf097d8e310934a5c8966d0803a614 100644 (file)
@@ -2760,7 +2760,10 @@ midori_browser_menu_item_select_cb (GtkWidget*     menuitem,
         /* This is undocumented object data, used by KatzeArrayAction. */
         KatzeItem* item = g_object_get_data (G_OBJECT (menuitem), "KatzeItem");
         if (item)
+        {
             tooltip = g_strdup (katze_item_get_uri (item));
+            sokoke_prefetch_uri (tooltip);
+        }
     }
     _midori_browser_set_statusbar_text (browser, tooltip);
     g_free (tooltip);
index 379fda8d2c5c44b0e3360a1ad24ae0304b0d0afc..4abd75c6c5b5f53be14e4927de2fc9fac546593e 100644 (file)
@@ -1194,6 +1194,10 @@ webkit_web_view_hovering_over_link_cb (WebKitWebView* web_view,
                                        const gchar*   link_uri,
                                        MidoriView*    view)
 {
+    #if !(WEBKIT_CHECK_VERSION (2, 18, 0) && defined (HAVE_LIBSOUP_2_29_3))
+    sokoke_prefetch_uri (link_uri);
+    #endif
+
     katze_assign (view->link_uri, g_strdup (link_uri));
     if (link_uri && g_str_has_prefix (link_uri, "mailto:"))
     {
index d0f6152948aabee25446c877c0c4ab452f35dd06..89dd0b3ae27eaa7881c68bf968d882c57376d97d 100644 (file)
@@ -1,6 +1,7 @@
 /*
  Copyright (C) 2007-2009 Christian Dywan <christian@twotoasts.de>
  Copyright (C) 2009 Dale Whittaker <dayul@users.sf.net>
+ Copyright (C) 2009 Alexander Butenko <a.butenka@gmail.com>
 
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
@@ -1521,3 +1522,66 @@ sokoke_file_chooser_dialog_new (const gchar*         title,
     #endif
     return dialog;
 }
+
+/**
+ * sokoke_prefetch_uri:
+ * @uri: an URI string
+ *
+ * Attempts to prefetch the specified URI, that is
+ * it tries to resolve the hostname in advance.
+ *
+ * Return value: %TRUE on success
+ **/
+gboolean
+sokoke_prefetch_uri (const char* uri)
+{
+    #define MAXHOSTS 50
+    static gchar* hosts = NULL;
+    static gint host_count = G_MAXINT;
+
+    SoupURI* s_uri;
+
+    if (!uri)
+        return FALSE;
+    s_uri = soup_uri_new (uri);
+    if (!s_uri || !s_uri->host)
+        return FALSE;
+
+    #if GLIB_CHECK_VERSION (2, 22, 0)
+    if (g_hostname_is_ip_address (s_uri->host))
+    #else
+    if (g_ascii_isdigit (s_uri->host[0]) && g_strstr_len (s_uri->host, 4, "."))
+    #endif
+    {
+        soup_uri_free (s_uri);
+        return FALSE;
+    }
+    if (!g_str_has_prefix (uri, "http"))
+    {
+        soup_uri_free (s_uri);
+        return FALSE;
+    }
+
+    if (!hosts ||
+        !g_regex_match_simple (s_uri->host, hosts,
+                               G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY))
+    {
+        SoupAddress* address;
+        gchar* new_hosts;
+
+        address = soup_address_new (s_uri->host, SOUP_ADDRESS_ANY_PORT);
+        soup_address_resolve_async (address, 0, 0, 0, 0);
+        g_object_unref (address);
+
+        if (host_count > MAXHOSTS)
+        {
+            katze_assign (hosts, g_strdup (""));
+            host_count = 0;
+        }
+        host_count++;
+        new_hosts = g_strdup_printf ("%s|%s", hosts, s_uri->host);
+        katze_assign (hosts, new_hosts);
+    }
+    soup_uri_free (s_uri);
+    return TRUE;
+}
index 5f805a335b6f4c087ffbebb487409f5ade1e781d..acf91ef7763d24aa7d8daa6ee859f5827052a98b 100644 (file)
@@ -179,4 +179,7 @@ sokoke_file_chooser_dialog_new          (const gchar*         title,
                                          GtkWindow*           window,
                                          GtkFileChooserAction action);
 
+gboolean
+sokoke_prefetch_uri                     (const char* uri);
+
 #endif /* !__SOKOKE_H__ */
index a4e58c95c0a43c12d6279d988d12129b2b412b8f..5733927e1293436e93e0ae8e0d77f6e35765f148 100644 (file)
@@ -1,5 +1,6 @@
 /*
  Copyright (C) 2008-2009 Christian Dywan <christian@twotoasts.de>
+ Copyright (C) 2009 Alexander Butenko <a.butenka@gmail.com>
 
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
@@ -225,10 +226,30 @@ magic_uri_format (void)
     }
 }
 
+static void
+magic_uri_prefetch (void)
+{
+    g_assert (!sokoke_prefetch_uri (NULL));
+    g_assert (sokoke_prefetch_uri ("http://google.com"));
+    g_assert (sokoke_prefetch_uri ("http://google.com"));
+    g_assert (sokoke_prefetch_uri ("http://googlecom"));
+    g_assert (sokoke_prefetch_uri ("http://1kino.com"));
+    g_assert (sokoke_prefetch_uri ("http://"));
+    g_assert (!sokoke_prefetch_uri ("http:/"));
+    g_assert (!sokoke_prefetch_uri ("http"));
+    g_assert (!sokoke_prefetch_uri ("ftp://ftphost.org"));
+    g_assert (!sokoke_prefetch_uri ("http://10.0.0.1"));
+    g_assert (!sokoke_prefetch_uri ("about:blank"));
+    g_assert (!sokoke_prefetch_uri ("javascript: alert()"));
+}
+
 int
 main (int    argc,
       char** argv)
 {
+    /* libSoup uses threads, therefore if WebKit is built with libSoup
+       or Midori is using it, we need to initialize threads. */
+    if (!g_thread_supported ()) g_thread_init (NULL);
     g_test_init (&argc, &argv, NULL);
     gtk_init_check (&argc, &argv);
 
@@ -238,6 +259,7 @@ main (int    argc,
     g_test_add_func ("/magic-uri/pseudo", magic_uri_pseudo);
     g_test_add_func ("/magic-uri/performance", magic_uri_performance);
     g_test_add_func ("/magic-uri/format", magic_uri_format);
+    g_test_add_func ("/magic-uri/prefetch", magic_uri_prefetch);
 
     return g_test_run ();
 }
diff --git a/wscript b/wscript
index b0e7127493222e8dc91a0488abd7c8d57a015d9c..e1b1b1a9832e3283425bd6a2789f4db5a39ab071 100644 (file)
--- a/wscript
+++ b/wscript
@@ -213,6 +213,7 @@ def configure (conf):
     check_pkg ('libsoup-2.4', '2.25.2')
     conf.define ('HAVE_LIBSOUP_2_25_2', 1)
     check_pkg ('libsoup-2.4', '2.27.90', False, var='LIBSOUP_2_27_90')
+    check_pkg ('libsoup-2.4', '2.29.3', False, var='LIBSOUP_2_29_3')
     check_pkg ('libxml-2.0', '2.6')
 
     if conf.env['HAVE_LIBSOUP_2_27_90']: