From d9d8e23885d51a120c4e4c1c2ffd2d57921d0a78 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Campos Date: Mon, 4 Mar 2013 20:27:25 +0100 Subject: 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 b11f40689..f6fb6036f 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 24e416eae..fe5b63a00 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 #include +static UriTester *uri_tester; + static const char introspection_xml[] = "" " " @@ -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 e27a5cc7f..6aa3b8c23 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 */ -- cgit v1.2.3