[epiphany] web-extension: Implement adblocker in WebKit2



commit d9d8e23885d51a120c4e4c1c2ffd2d57921d0a78
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Mon Mar 4 20:27:25 2013 +0100

    web-extension: Implement adblocker in WebKit2
    
    https://bugzilla.gnome.org/show_bug.cgi?id=695307

 embed/web-extension/Makefile.am          |   13 +++++--
 embed/web-extension/ephy-web-extension.c |   48 ++++++++++++++++++++++++++++++
 src/ephy-main.c                          |    1 +
 3 files changed, 58 insertions(+), 4 deletions(-)
---
diff --git a/embed/web-extension/Makefile.am b/embed/web-extension/Makefile.am
index b11f406..f6fb603 100644
--- a/embed/web-extension/Makefile.am
+++ b/embed/web-extension/Makefile.am
@@ -1,18 +1,23 @@
 webextension_LTLIBRARIES = libephywebextension.la
 
-INCLUDES = \
-       -I$(top_srcdir)/lib
-
 webextensiondir = \
        $(libdir)/epiphany/$(EPIPHANY_MAJOR)/web-extensions
 
 libephywebextension_la_SOURCES = \
        ephy-web-extension.c \
        ephy-web-extension.h \
+       $(top_srcdir)/embed/uri-tester.c \
+       $(top_srcdir)/embed/uri-tester.h \
+       $(top_srcdir)/lib/ephy-debug.c \
+       $(top_srcdir)/lib/ephy-debug.h \
+       $(top_srcdir)/lib/ephy-settings.c \
+       $(top_srcdir)/lib/ephy-settings.h \
        $(top_srcdir)/lib/ephy-web-dom-utils.c \
        $(top_srcdir)/lib/ephy-web-dom-utils.h
 
-libephywebextension_la_CPPFLAG = \
+libephywebextension_la_CPPFLAGS = \
+       -I$(top_srcdir)/embed \
+       -I$(top_srcdir)/lib \
        $(AM_CPPFLAGS)
 
 libephywebextension_la_CFLAGS = \
diff --git a/embed/web-extension/ephy-web-extension.c b/embed/web-extension/ephy-web-extension.c
index 24e416e..fe5b63a 100644
--- a/embed/web-extension/ephy-web-extension.c
+++ b/embed/web-extension/ephy-web-extension.c
@@ -21,11 +21,17 @@
 #include "config.h"
 #include "ephy-web-extension.h"
 
+#include "ephy-debug.h"
+#include "ephy-prefs.h"
+#include "ephy-settings.h"
 #include "ephy-web-dom-utils.h"
+#include "uri-tester.h"
 
 #include <gio/gio.h>
 #include <webkit2/webkit-web-extension.h>
 
+static UriTester *uri_tester;
+
 static const char introspection_xml[] =
   "<node>"
   " <interface name='org.gnome.Epiphany.WebExtension'>"
@@ -164,11 +170,53 @@ bus_acquired_cb (GDBusConnection *connection,
   }
 }
 
+static gboolean
+web_page_send_request (WebKitWebPage *web_page,
+                       WebKitURIRequest *request,
+                       WebKitURIResponse *redirected_response,
+                       gpointer user_data)
+{
+  const char *request_uri;
+  const char *page_uri;
+
+  /* FIXME: Instead of checking the setting here, connect to the signal
+   * or not depending on the setting.
+   */
+  if (!g_settings_get_boolean (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_ENABLE_ADBLOCK))
+      return FALSE;
+
+  request_uri = webkit_uri_request_get_uri (request);
+  page_uri = webkit_web_page_get_uri (web_page);
+
+  /* Always load the main resource. */
+  if (g_strcmp0 (request_uri, page_uri) == 0)
+    return FALSE;
+
+  return uri_tester_test_uri (uri_tester, request_uri, page_uri, AD_URI_CHECK_TYPE_OTHER);
+}
+
+static void
+web_page_created_callback (WebKitWebExtension *extension,
+                           WebKitWebPage *web_page,
+                           gpointer user_data)
+{
+  g_signal_connect_object (web_page, "send-request",
+                           G_CALLBACK (web_page_send_request),
+                           NULL, 0);
+}
+
 G_MODULE_EXPORT void
 webkit_web_extension_initialize (WebKitWebExtension *extension)
 {
   char *service_name;
 
+  ephy_debug_init ();
+  uri_tester = uri_tester_new (g_getenv ("EPHY_DOT_DIR"));
+
+  g_signal_connect (extension, "page-created",
+                    G_CALLBACK (web_page_created_callback),
+                    NULL);
+
   service_name = g_strdup_printf ("%s-%s", EPHY_WEB_EXTENSION_SERVICE_NAME, g_getenv 
("EPHY_WEB_EXTENSION_ID"));
   g_bus_own_name (G_BUS_TYPE_SESSION,
                   service_name,
diff --git a/src/ephy-main.c b/src/ephy-main.c
index e27a5cc..6aa3b8c 100644
--- a/src/ephy-main.c
+++ b/src/ephy-main.c
@@ -464,6 +464,7 @@ main (int argc,
 #ifdef HAVE_WEBKIT2
   pid_str = g_strdup_printf ("%u", getpid ());
   g_setenv ("EPHY_WEB_EXTENSION_ID", pid_str, TRUE);
+  g_setenv ("EPHY_DOT_DIR", ephy_dot_dir (), TRUE);
   g_free (pid_str);
 
   /* Set the web extensions dir ASAP before the process is launched */


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