[epiphany] Move EphyUriTester to the UI process and make it a D-Bus interface



commit cfab395a9749b3bde1ec267f081a7a87ce3eaf14
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sun Oct 23 22:29:36 2016 -0500

    Move EphyUriTester to the UI process and make it a D-Bus interface
    
    For one thing, it's silly to have n different EphyUriTesters in n
    different web processes, each one loading up adblock filters separately.
    Once upon a time, this used to cause big problems when the different web
    processes would stomp on the global filters file. For us to ever
    implement a real filters configuration dialog, this needs to be handled
    in one place, by the UI process, and web extensions must just query the
    UI process when they want to use the URI tester.
    
    For another: it's basically required to use libhttpseverywhere
    effectively. We're going to have to set up almost this same exact
    interface for libhttpseverywhere, so might as well do it for adblock
    too. Why is it needed? Because loading HTTPS Everywhere rulesets takes
    ~2 seconds apiece. It's much too long to do each time we open a new
    browser tab, so we should do it in the UI process instead.
    
    An unfortunate consequence of this is that our GDBusConnection use in
    the web process can no longer be asynchronous. This is because we must
    have an EphyUriTesterProxy completely ready to be used before the first
    URI request is ready. We must handle each URI request synchronously as a
    consequence of WebKit's signal-based API. What a shame!

 embed/Makefile.am                                  |    3 +-
 embed/ephy-embed-shell.c                           |   17 ++-
 embed/{web-extension => }/ephy-uri-tester.c        |  136 ++++++++++++++-
 embed/{web-extension => }/ephy-uri-tester.h        |   13 +-
 embed/ephy-web-extension-proxy.c                   |    2 +-
 embed/web-extension/Makefile.am                    |    5 +-
 embed/web-extension/ephy-uri-tester-proxy.c        |  119 +++++++++++++
 ...b-extension-names.h => ephy-uri-tester-proxy.h} |   16 ++-
 embed/web-extension/ephy-web-extension-main.c      |    1 -
 embed/web-extension/ephy-web-extension.c           |  184 +++++--------------
 embed/web-extension/ephy-web-extension.h           |    1 -
 lib/Makefile.am                                    |    2 +
 .../ephy-dbus-names.h                              |    4 +
 .../ephy-uri-tester-interface.h                    |   15 ++-
 14 files changed, 347 insertions(+), 171 deletions(-)
---
diff --git a/embed/Makefile.am b/embed/Makefile.am
index a46a051..c5261fb 100644
--- a/embed/Makefile.am
+++ b/embed/Makefile.am
@@ -42,6 +42,8 @@ libephyembed_la_SOURCES = \
        ephy-find-toolbar.h             \
        ephy-notification-container.c   \
        ephy-notification-container.h   \
+       ephy-uri-tester.c               \
+       ephy-uri-tester.h               \
        ephy-view-source-handler.c      \
        ephy-view-source-handler.h      \
        ephy-web-view.c                 \
@@ -58,7 +60,6 @@ libephyembed_la_CFLAGS = \
 libephyembed_la_CPPFLAGS = \
        -I$(top_builddir)/lib                   \
        -I$(top_builddir)/lib/widgets           \
-       -I$(top_srcdir)/embed/web-extension     \
        -I$(top_srcdir)/lib                     \
        -I$(top_srcdir)/lib/egg                 \
        -I$(top_srcdir)/lib/history             \
diff --git a/embed/ephy-embed-shell.c b/embed/ephy-embed-shell.c
index 732f4b6..e1886e5 100644
--- a/embed/ephy-embed-shell.c
+++ b/embed/ephy-embed-shell.c
@@ -35,10 +35,10 @@
 #include "ephy-profile-utils.h"
 #include "ephy-settings.h"
 #include "ephy-snapshot-service.h"
+#include "ephy-uri-tester.h"
 #include "ephy-view-source-handler.h"
 #include "ephy-web-app-utils.h"
 #include "ephy-web-extension-proxy.h"
-#include "ephy-web-extension-names.h"
 
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
@@ -62,6 +62,7 @@ typedef struct {
   EphyViewSourceHandler *source_handler;
   guint update_overview_timeout_id;
   guint hiding_overview_item;
+  EphyUriTester *uri_tester;
   GDBusServer *dbus_server;
   GList *web_extensions;
 } EphyEmbedShellPrivate;
@@ -110,6 +111,7 @@ ephy_embed_shell_dispose (GObject *object)
   g_clear_object (&priv->downloads_manager);
   g_clear_object (&priv->hosts_manager);
   g_clear_object (&priv->web_context);
+  g_clear_object (&priv->uri_tester);
   g_clear_object (&priv->dbus_server);
 
   G_OBJECT_CLASS (ephy_embed_shell_parent_class)->dispose (object);
@@ -605,9 +607,12 @@ new_connection_cb (GDBusServer     *server,
                    GDBusConnection *connection,
                    EphyEmbedShell  *shell)
 {
+  EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
   EphyWebExtensionProxy *extension = ephy_web_extension_proxy_new (connection);
   ephy_embed_shell_watch_web_extension (shell, extension);
 
+  ephy_uri_tester_register_dbus_object (priv->uri_tester, connection);
+
   g_signal_connect_object (extension, "page-created",
                            G_CALLBACK (web_extension_page_created), shell, 0);
 
@@ -883,19 +888,25 @@ ephy_embed_shell_constructed (GObject *object)
 {
   EphyEmbedShell *shell;
   EphyEmbedShellPrivate *priv;
+  EphyEmbedShellMode mode;
 
   G_OBJECT_CLASS (ephy_embed_shell_parent_class)->constructed (object);
 
   shell = EPHY_EMBED_SHELL (object);
   priv = ephy_embed_shell_get_instance_private (shell);
+  mode = ephy_embed_shell_get_mode (shell);
+
   /* These do not run the EmbedShell application instance, so make sure that
      there is a web context and a user content manager for them. */
-  if (ephy_embed_shell_get_mode (shell) == EPHY_EMBED_SHELL_MODE_TEST ||
-      ephy_embed_shell_get_mode (shell) == EPHY_EMBED_SHELL_MODE_SEARCH_PROVIDER) {
+  if (mode == EPHY_EMBED_SHELL_MODE_TEST ||
+      mode == EPHY_EMBED_SHELL_MODE_SEARCH_PROVIDER) {
     ephy_embed_shell_create_web_context (shell);
     priv->user_content = webkit_user_content_manager_new ();
   }
 
+  if (mode != EPHY_EMBED_SHELL_MODE_SEARCH_PROVIDER)
+    priv->uri_tester = ephy_uri_tester_new ();
+
   g_signal_connect_object (ephy_snapshot_service_get_default (),
                            "snapshot-saved", G_CALLBACK (snapshot_saved_cb),
                            shell, 0);
diff --git a/embed/web-extension/ephy-uri-tester.c b/embed/ephy-uri-tester.c
similarity index 86%
rename from embed/web-extension/ephy-uri-tester.c
rename to embed/ephy-uri-tester.c
index 785e632..860ff32 100644
--- a/embed/web-extension/ephy-uri-tester.c
+++ b/embed/ephy-uri-tester.c
@@ -27,10 +27,16 @@
 #include "config.h"
 #include "ephy-uri-tester.h"
 
+#include "ephy-dbus-names.h"
 #include "ephy-debug.h"
+#include "ephy-file-helpers.h"
+#include "ephy-settings.h"
+#include "ephy-uri-helpers.h"
+#include "ephy-uri-tester-interface.h"
 
 #include <gio/gio.h>
 #include <glib/gstdio.h>
+#include <libsoup/soup.h>
 #include <string.h>
 
 #define DEFAULT_FILTER_URL "https://easylist-downloads.adblockplus.org/easylist.txt";
@@ -226,6 +232,15 @@ ephy_uri_tester_load_patterns (EphyUriTester *tester)
 }
 
 static void
+ephy_uri_tester_set_filters (EphyUriTester *tester, GSList *filters)
+{
+  if (tester->filters)
+    g_slist_free_full (tester->filters, g_free);
+
+  tester->filters = filters;
+}
+
+static void
 ephy_uri_tester_load_filters (EphyUriTester *tester)
 {
   GSList *list = NULL;
@@ -920,29 +935,132 @@ ephy_uri_tester_class_init (EphyUriTesterClass *klass)
 }
 
 EphyUriTester *
-ephy_uri_tester_new (const char *base_data_dir)
+ephy_uri_tester_new (void)
 {
-  g_return_val_if_fail (base_data_dir != NULL, NULL);
-
-  return g_object_new (EPHY_TYPE_URI_TESTER, "base-data-dir", base_data_dir, NULL);
+  return g_object_new (EPHY_TYPE_URI_TESTER, "base-data-dir", ephy_dot_dir (), NULL);
 }
 
-gboolean
+static gboolean
 ephy_uri_tester_test_uri (EphyUriTester *tester,
                           const char    *req_uri,
                           const char    *page_uri)
 {
+  /* Always load the main resource. */
+  if (g_strcmp0 (req_uri, page_uri) == 0)
+    return FALSE;
+
+  /* Always load data requests, as uri_tester won't do any good here. */
+  if (g_str_has_prefix (req_uri, SOUP_URI_SCHEME_DATA))
+    return FALSE;
+
   /* check whitelisting rules before the normal ones */
   if (ephy_uri_tester_is_matched (tester, NULL, req_uri, page_uri, TRUE))
     return FALSE;
   return ephy_uri_tester_is_matched (tester, NULL, req_uri, page_uri, FALSE);
 }
 
+static char *
+ephy_uri_tester_rewrite_uri (EphyUriTester *uri_tester,
+                             const char    *request_uri,
+                             const char    *page_uri)
+{
+  char *modified_uri;
+
+  /* Should we block the URL outright? */
+  if (g_settings_get_boolean (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_ENABLE_ADBLOCK) &&
+      ephy_uri_tester_test_uri (uri_tester, request_uri, page_uri)) {
+    g_debug ("Request '%s' blocked (page: '%s')", request_uri, page_uri);
+    return g_strdup ("");
+  }
+
+  if (g_settings_get_boolean (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_DO_NOT_TRACK)) {
+    /* Remove analytics from URL. Note that this function is a bit annoying to
+     * use: it returns NULL if it doesn't remove any query parameters. */
+    modified_uri = ephy_remove_tracking_from_uri (request_uri);
+  }
+
+  if (!modified_uri)
+    modified_uri = g_strdup (request_uri);
+
+  /* FIXME: Rewrite URL to use HTTPS if directed by HTTPS Everywhere */
+
+  return modified_uri;
+}
+
+static void
+handle_method_call (GDBusConnection       *connection,
+                    const char            *sender,
+                    const char            *object_path,
+                    const char            *interface_name,
+                    const char            *method_name,
+                    GVariant              *parameters,
+                    GDBusMethodInvocation *invocation,
+                    gpointer               user_data)
+{
+  EphyUriTester *uri_tester = EPHY_URI_TESTER (user_data);
+
+  if (g_strcmp0 (interface_name, EPHY_URI_TESTER_INTERFACE) != 0)
+    return;
+
+  if (g_strcmp0 (method_name, "MaybeRewriteUri") == 0) {
+    const char *request_uri;
+    const char *page_uri;
+    char *rewritten_uri;
+
+    g_variant_get (parameters, "(&s&s)", &request_uri, &page_uri);
+
+    if (request_uri == NULL || request_uri == '\0') {
+      g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
+                                             "Request URI cannot be NULL or empty");
+      return;
+    }
+
+    if (page_uri == NULL || page_uri == '\0') {
+      g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
+                                             "Page URI cannot be NULL or empty");
+      return;
+    }
+
+    rewritten_uri = ephy_uri_tester_rewrite_uri (uri_tester, request_uri, page_uri);
+
+    if (!rewritten_uri)
+      rewritten_uri = g_strdup ("");
+
+    g_dbus_method_invocation_return_value (invocation,
+                                           g_variant_new ("(s)", rewritten_uri));
+
+    g_free (rewritten_uri);
+  }
+}
+
+static const GDBusInterfaceVTable interface_vtable = {
+  handle_method_call,
+  NULL,
+  NULL
+};
+
 void
-ephy_uri_tester_set_filters (EphyUriTester *tester, GSList *filters)
+ephy_uri_tester_register_dbus_object (EphyUriTester   *tester,
+                                      GDBusConnection *connection)
 {
-  if (tester->filters)
-    g_slist_free_full (tester->filters, g_free);
+  static GDBusNodeInfo *introspection_data = NULL;
+  guint registration_id;
+  GError *error = NULL;
 
-  tester->filters = filters;
+  if (!introspection_data)
+    introspection_data = g_dbus_node_info_new_for_xml (ephy_uri_tester_introspection_xml, NULL);
+
+  registration_id =
+    g_dbus_connection_register_object (connection,
+                                       EPHY_URI_TESTER_OBJECT_PATH,
+                                       introspection_data->interfaces[0],
+                                       &interface_vtable,
+                                       g_object_ref (tester),
+                                       g_object_unref,
+                                       &error);
+  if (!registration_id) {
+    g_warning ("Failed to register URI tester object: %s\n", error->message);
+    g_error_free (error);
+    return;
+  }
 }
diff --git a/embed/web-extension/ephy-uri-tester.h b/embed/ephy-uri-tester.h
similarity index 69%
rename from embed/web-extension/ephy-uri-tester.h
rename to embed/ephy-uri-tester.h
index b8091da..0791721 100644
--- a/embed/web-extension/ephy-uri-tester.h
+++ b/embed/ephy-uri-tester.h
@@ -20,8 +20,7 @@
 
 #pragma once
 
-#include <glib-object.h>
-#include <glib.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
@@ -29,13 +28,9 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (EphyUriTester, ephy_uri_tester, EPHY, URI_TESTER, GObject)
 
-EphyUriTester *ephy_uri_tester_new         (const char *base_data_dir);
+EphyUriTester *ephy_uri_tester_new                  (void);
 
-gboolean       ephy_uri_tester_test_uri    (EphyUriTester *tester,
-                                            const char *req_uri,
-                                            const char *page_uri);
-
-void           ephy_uri_tester_set_filters (EphyUriTester *tester,
-                                            GSList *filters);
+void           ephy_uri_tester_register_dbus_object (EphyUriTester   *tester,
+                                                     GDBusConnection *connection);
 
 G_END_DECLS
diff --git a/embed/ephy-web-extension-proxy.c b/embed/ephy-web-extension-proxy.c
index 0c8d3aa..c35332e 100644
--- a/embed/ephy-web-extension-proxy.c
+++ b/embed/ephy-web-extension-proxy.c
@@ -21,7 +21,7 @@
 #include <config.h>
 #include "ephy-web-extension-proxy.h"
 
-#include "ephy-web-extension-names.h"
+#include "ephy-dbus-names.h"
 #include "ephy-history-service.h"
 
 struct _EphyWebExtensionProxy {
diff --git a/embed/web-extension/Makefile.am b/embed/web-extension/Makefile.am
index e0eaf3d..3699bcc 100644
--- a/embed/web-extension/Makefile.am
+++ b/embed/web-extension/Makefile.am
@@ -5,14 +5,13 @@ webextensiondir = $(pkglibdir)/web-extensions
 libephywebextension_la_SOURCES = \
        ephy-embed-form-auth.c          \
        ephy-embed-form-auth.h          \
-       ephy-uri-tester.c               \
-       ephy-uri-tester.h               \
+       ephy-uri-tester-proxy.c         \
+       ephy-uri-tester-proxy.h         \
        ephy-web-dom-utils.c            \
        ephy-web-dom-utils.h            \
        ephy-web-extension.c            \
        ephy-web-extension.h            \
        ephy-web-extension-main.c       \
-       ephy-web-extension-names.h      \
        ephy-web-overview.h             \
        ephy-web-overview.c             \
        ephy-web-overview-model.h       \
diff --git a/embed/web-extension/ephy-uri-tester-proxy.c b/embed/web-extension/ephy-uri-tester-proxy.c
new file mode 100644
index 0000000..7a156ce
--- /dev/null
+++ b/embed/web-extension/ephy-uri-tester-proxy.c
@@ -0,0 +1,119 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ *  Copyright © 2016 Igalia S.L.
+ *
+ *  This file is part of Epiphany.
+ *
+ *  Epiphany is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  Epiphany is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include "ephy-uri-tester-proxy.h"
+
+#include "ephy-dbus-names.h"
+
+struct _EphyUriTesterProxy {
+  GObject parent_instance;
+
+  GDBusProxy *proxy;
+  GDBusConnection *connection;
+};
+
+G_DEFINE_TYPE (EphyUriTesterProxy, ephy_uri_tester_proxy, G_TYPE_OBJECT)
+
+static void
+ephy_uri_tester_proxy_dispose (GObject *object)
+{
+  EphyUriTesterProxy *uri_tester = EPHY_URI_TESTER_PROXY (object);
+
+  g_clear_object (&uri_tester->proxy);
+  g_clear_object (&uri_tester->connection);
+
+  G_OBJECT_CLASS (ephy_uri_tester_proxy_parent_class)->dispose (object);
+}
+
+static void
+ephy_uri_tester_proxy_init (EphyUriTesterProxy *uri_tester)
+{
+}
+
+static void
+ephy_uri_tester_proxy_class_init (EphyUriTesterProxyClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = ephy_uri_tester_proxy_dispose;
+}
+
+EphyUriTesterProxy *
+ephy_uri_tester_proxy_new (GDBusConnection *connection)
+{
+  EphyUriTesterProxy *uri_tester;
+  GError *error = NULL;
+
+  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
+
+  uri_tester = g_object_new (EPHY_TYPE_URI_TESTER_PROXY, NULL);
+
+  uri_tester->connection = g_object_ref (connection);
+
+  /* It has to be sync because it must be guaranteed to be ready before the
+   * first request is sent. We have to handle requests synchronously anyway.
+   */
+  uri_tester->proxy = g_dbus_proxy_new_sync (connection,
+                                             G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | 
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+                                             NULL,
+                                             NULL,
+                                             EPHY_URI_TESTER_OBJECT_PATH,
+                                             EPHY_URI_TESTER_INTERFACE,
+                                             NULL,
+                                             &error);
+
+  /* This is fatal. */
+  if (error)
+    g_error ("Failed to initialize URI tester: %s", error->message);
+
+  return uri_tester;
+}
+
+char *
+ephy_uri_tester_proxy_maybe_rewrite_uri (EphyUriTesterProxy *uri_tester,
+                                         const char         *request_uri,
+                                         const char         *page_uri)
+{
+  GVariant *variant;
+  char *modified_uri;
+  GError *error = NULL;
+
+  g_return_val_if_fail (EPHY_IS_URI_TESTER_PROXY (uri_tester), g_strdup (""));
+
+  variant = g_dbus_proxy_call_sync (uri_tester->proxy,
+                                    "MaybeRewriteUri",
+                                    g_variant_new ("(ss)", request_uri, page_uri),
+                                    G_DBUS_CALL_FLAGS_NONE,
+                                    -1,
+                                    NULL,
+                                    &error);
+
+  if (error) {
+    g_warning ("Failed to query EphyUriTester for %s: %s", request_uri, error->message);
+    g_error_free (error);
+    return g_strdup (request_uri);
+  }
+
+  g_variant_get (variant, "(s)", &modified_uri);
+
+  g_variant_unref (variant);
+  return modified_uri;
+}
diff --git a/embed/web-extension/ephy-web-extension-names.h b/embed/web-extension/ephy-uri-tester-proxy.h
similarity index 58%
copy from embed/web-extension/ephy-web-extension-names.h
copy to embed/web-extension/ephy-uri-tester-proxy.h
index b051f44..958eaa2 100644
--- a/embed/web-extension/ephy-web-extension-names.h
+++ b/embed/web-extension/ephy-uri-tester-proxy.h
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /*
- *  Copyright © 2014 Igalia S.L.
+ *  Copyright © 2016 Igalia S.L.
  *
  *  This file is part of Epiphany.
  *
@@ -20,10 +20,18 @@
 
 #pragma once
 
+#include <gio/gio.h>
+
 G_BEGIN_DECLS
 
-#define EPHY_WEB_EXTENSION_SERVICE_NAME "org.gnome.Epiphany.WebExtension"
-#define EPHY_WEB_EXTENSION_OBJECT_PATH  "/org/gnome/Epiphany/WebExtension"
-#define EPHY_WEB_EXTENSION_INTERFACE    "org.gnome.Epiphany.WebExtension"
+#define EPHY_TYPE_URI_TESTER_PROXY (ephy_uri_tester_proxy_get_type ())
+
+G_DECLARE_FINAL_TYPE (EphyUriTesterProxy, ephy_uri_tester_proxy, EPHY, URI_TESTER_PROXY, GObject)
+
+EphyUriTesterProxy *ephy_uri_tester_proxy_new (GDBusConnection *connection);
+
+char               *ephy_uri_tester_proxy_maybe_rewrite_uri (EphyUriTesterProxy *uri_tester,
+                                                             const char         *request_uri,
+                                                             const char         *page_uri);
 
 G_END_DECLS
diff --git a/embed/web-extension/ephy-web-extension-main.c b/embed/web-extension/ephy-web-extension-main.c
index b6b885a..13f029a 100644
--- a/embed/web-extension/ephy-web-extension-main.c
+++ b/embed/web-extension/ephy-web-extension-main.c
@@ -57,7 +57,6 @@ webkit_web_extension_initialize_with_user_data (WebKitWebExtension *webkit_exten
   ephy_web_extension_initialize (extension,
                                  webkit_extension,
                                  server_address,
-                                 dot_dir,
                                  private_profile);
 }
 
diff --git a/embed/web-extension/ephy-web-extension.c b/embed/web-extension/ephy-web-extension.c
index 5c138ec..df46948 100644
--- a/embed/web-extension/ephy-web-extension.c
+++ b/embed/web-extension/ephy-web-extension.c
@@ -21,6 +21,7 @@
 #include "config.h"
 #include "ephy-web-extension.h"
 
+#include "ephy-dbus-names.h"
 #include "ephy-dbus-util.h"
 #include "ephy-debug.h"
 #include "ephy-embed-form-auth.h"
@@ -29,11 +30,9 @@
 #include "ephy-hosts-manager.h"
 #include "ephy-prefs.h"
 #include "ephy-settings.h"
+#include "ephy-uri-tester-proxy.h"
 #include "ephy-web-dom-utils.h"
-#include "ephy-uri-helpers.h"
-#include "ephy-uri-tester.h"
 #include "ephy-web-overview.h"
-#include "ephy-web-extension-names.h"
 
 #include <gio/gio.h>
 #include <gtk/gtk.h>
@@ -58,13 +57,12 @@ struct _EphyWebExtension {
 
   GDBusConnection *dbus_connection;
   GCancellable *cancellable;
-  GArray *page_created_signals_pending;
 
-  EphyUriTester *uri_tester;
   EphyFormAuthDataCache *form_auth_data_cache;
   GHashTable *form_auth_data_save_requests;
   EphyWebOverviewModel *overview_model;
   EphyHostsManager *hosts_manager;
+  EphyUriTesterProxy *uri_tester;
 };
 
 static const char introspection_xml[] =
@@ -122,59 +120,30 @@ web_page_send_request (WebKitWebPage     *web_page,
 {
   const char *request_uri;
   const char *page_uri;
-  char *new_uri;
-  gboolean ret;
-
-  request_uri = webkit_uri_request_get_uri (request);
+  char *modified_uri;
 
   if (g_settings_get_boolean (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_DO_NOT_TRACK)) {
-    SoupMessageHeaders *headers;
-
-    headers = webkit_uri_request_get_http_headers (request);
+    SoupMessageHeaders *headers = webkit_uri_request_get_http_headers (request);
     if (headers) {
       /* Do Not Track header. '1' means 'opt-out'. See:
        * http://tools.ietf.org/id/draft-mayer-do-not-track-00.txt */
       soup_message_headers_append (headers, "DNT", "1");
     }
-
-    /* Remove analytics from URL before loading */
-    new_uri = ephy_remove_tracking_from_uri (request_uri);
-    if (new_uri) {
-      webkit_uri_request_set_uri (request, new_uri);
-      request_uri = webkit_uri_request_get_uri (request);
-    }
-    g_free (new_uri);
   }
 
-#if 0
-  /* Rewrite URL to use HTTPS if directed by HTTPS Everywhere */
-  new_uri = https_everywhere_rewrite (request_uri);
-  if (g_strcmp0 (request_uri, new_uri) != 0) {
-    LOG ("HTTPS Everywhere: rewrote %s to %s", request_uri, new_uri);
-    webkit_uri_request_set_uri (request, new_uri);
-    request_uri = webkit_uri_request_get_uri (request);
-  }
-  g_free (new_uri);
-#endif
-
-  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;
-
-  /* Always load data requests, as uri_tester won't do any good here. */
-  if (g_str_has_prefix (request_uri, SOUP_URI_SCHEME_DATA))
-    return FALSE;
-
-  ret = ephy_uri_tester_test_uri (extension->uri_tester, request_uri, page_uri);
-  if (ret)
-    g_debug ("Request '%s' blocked (page: '%s')", request_uri, page_uri);
+  modified_uri = ephy_uri_tester_proxy_maybe_rewrite_uri (extension->uri_tester,
+                                                          request_uri,
+                                                          page_uri);
+  if (g_strcmp0 (request_uri, modified_uri) != 0) {
+    LOG ("Rewrote %s to %s", request_uri, modified_uri);
+    webkit_uri_request_set_uri (request, modified_uri);
+  }
+  g_free (modified_uri);
 
-  return ret;
+  return FALSE;
 }
 
 static GHashTable *
@@ -1115,44 +1084,13 @@ ephy_web_extension_emit_page_created (EphyWebExtension *extension,
 }
 
 static void
-ephy_web_extension_emit_page_created_signals_pending (EphyWebExtension *extension)
-{
-  guint i;
-
-  if (!extension->page_created_signals_pending)
-    return;
-
-  for (i = 0; i < extension->page_created_signals_pending->len; i++) {
-    guint64 page_id;
-
-    page_id = g_array_index (extension->page_created_signals_pending, guint64, i);
-    ephy_web_extension_emit_page_created (extension, page_id);
-  }
-
-  g_array_free (extension->page_created_signals_pending, TRUE);
-  extension->page_created_signals_pending = NULL;
-}
-
-static void
-ephy_web_extension_queue_page_created_signal_emission (EphyWebExtension *extension,
-                                                       guint64           page_id)
-{
-  if (!extension->page_created_signals_pending)
-    extension->page_created_signals_pending = g_array_new (FALSE, FALSE, sizeof (guint64));
-  extension->page_created_signals_pending = g_array_append_val (extension->page_created_signals_pending, 
page_id);
-}
-
-static void
 ephy_web_extension_page_created_cb (EphyWebExtension *extension,
                                     WebKitWebPage    *web_page)
 {
   guint64 page_id;
 
   page_id = webkit_web_page_get_id (web_page);
-  if (extension->dbus_connection)
-    ephy_web_extension_emit_page_created (extension, page_id);
-  else
-    ephy_web_extension_queue_page_created_signal_emission (extension, page_id);
+  ephy_web_extension_emit_page_created (extension, page_id);
 
   g_signal_connect (web_page, "send-request",
                     G_CALLBACK (web_page_send_request),
@@ -1337,7 +1275,6 @@ ephy_web_extension_dispose (GObject *object)
 {
   EphyWebExtension *extension = EPHY_WEB_EXTENSION (object);
 
-  g_clear_object (&extension->uri_tester);
   g_clear_object (&extension->overview_model);
   g_clear_object (&extension->hosts_manager);
 
@@ -1349,11 +1286,6 @@ ephy_web_extension_dispose (GObject *object)
     extension->form_auth_data_save_requests = NULL;
   }
 
-  if (extension->page_created_signals_pending) {
-    g_array_free (extension->page_created_signals_pending, TRUE);
-    extension->page_created_signals_pending = NULL;
-  }
-
   g_clear_object (&extension->cancellable);
   g_clear_object (&extension->dbus_connection);
 
@@ -1389,45 +1321,6 @@ ephy_web_extension_get (void)
   return EPHY_WEB_EXTENSION (g_once (&once_init, ephy_web_extension_create_instance, NULL));
 }
 
-static void
-dbus_connection_created_cb (GObject          *source_object,
-                            GAsyncResult     *result,
-                            EphyWebExtension *extension)
-{
-  static GDBusNodeInfo *introspection_data = NULL;
-  GDBusConnection *connection;
-  guint registration_id;
-  GError *error = NULL;
-
-  if (!introspection_data)
-    introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
-
-  connection = g_dbus_connection_new_for_address_finish (result, &error);
-  if (error) {
-    g_warning ("Failed to connect to UI process: %s", error->message);
-    g_error_free (error);
-    return;
-  }
-
-  registration_id =
-    g_dbus_connection_register_object (connection,
-                                       EPHY_WEB_EXTENSION_OBJECT_PATH,
-                                       introspection_data->interfaces[0],
-                                       &interface_vtable,
-                                       extension,
-                                       NULL,
-                                       &error);
-  if (!registration_id) {
-    g_warning ("Failed to register web extension object: %s\n", error->message);
-    g_error_free (error);
-    g_object_unref (connection);
-    return;
-  }
-
-  extension->dbus_connection = connection;
-  ephy_web_extension_emit_page_created_signals_pending (extension);
-}
-
 static gboolean
 authorize_authenticated_peer_cb (GDBusAuthObserver *observer,
                                  GIOStream         *stream,
@@ -1441,10 +1334,12 @@ void
 ephy_web_extension_initialize (EphyWebExtension   *extension,
                                WebKitWebExtension *wk_extension,
                                const char         *server_address,
-                               const char         *dot_dir,
                                gboolean            is_private_profile)
 {
+  static GDBusNodeInfo *introspection_data = NULL;
   GDBusAuthObserver *observer;
+  guint registration_id;
+  GError *error = NULL;
 
   g_return_if_fail (EPHY_IS_WEB_EXTENSION (extension));
 
@@ -1453,12 +1348,7 @@ ephy_web_extension_initialize (EphyWebExtension   *extension,
 
   extension->initialized = TRUE;
 
-#if 0
-  https_everywhere_init ();
-#endif
-
   extension->extension = g_object_ref (wk_extension);
-  extension->uri_tester = ephy_uri_tester_new (dot_dir);
   if (!is_private_profile)
     extension->form_auth_data_cache = ephy_form_auth_data_cache_new ();
 
@@ -1474,11 +1364,35 @@ ephy_web_extension_initialize (EphyWebExtension   *extension,
   g_signal_connect (observer, "authorize-authenticated-peer",
                     G_CALLBACK (authorize_authenticated_peer_cb), extension);
 
-  g_dbus_connection_new_for_address (server_address,
-                                     G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
-                                     observer,
-                                     extension->cancellable,
-                                     (GAsyncReadyCallback)dbus_connection_created_cb,
-                                     extension);
+  /* Sync because this has to be ready before the first URI request, since
+   * WebKit does not provide any async way to rewrite URI requests. */
+  extension->dbus_connection =
+      g_dbus_connection_new_for_address_sync (server_address,
+                                              G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
+                                              observer,
+                                              NULL,
+                                              &error);
   g_object_unref (observer);
+
+  /* Fatal. */
+  if (error)
+    g_error ("Failed to connect to UI process: %s", error->message);
+
+  if (!introspection_data)
+    introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
+
+  registration_id =
+    g_dbus_connection_register_object (extension->dbus_connection,
+                                       EPHY_WEB_EXTENSION_OBJECT_PATH,
+                                       introspection_data->interfaces[0],
+                                       &interface_vtable,
+                                       extension,
+                                       NULL,
+                                       &error);
+
+  /* Fatal. */
+  if (!registration_id)
+    g_error ("Failed to register web extension object: %s\n", error->message);
+
+  extension->uri_tester = ephy_uri_tester_proxy_new (extension->dbus_connection);
 }
diff --git a/embed/web-extension/ephy-web-extension.h b/embed/web-extension/ephy-web-extension.h
index d2ce855..3901967 100644
--- a/embed/web-extension/ephy-web-extension.h
+++ b/embed/web-extension/ephy-web-extension.h
@@ -33,7 +33,6 @@ EphyWebExtension *ephy_web_extension_get            (void);
 void              ephy_web_extension_initialize     (EphyWebExtension   *extension,
                                                      WebKitWebExtension *wk_extension,
                                                      const char         *server_address,
-                                                     const char         *dot_dir,
                                                      gboolean            is_private_profile);
 
 
diff --git a/lib/Makefile.am b/lib/Makefile.am
index be86da4..1acd150 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -6,6 +6,7 @@ TYPES_H_FILES = \
        ephy-security-levels.h
 
 libephymisc_la_SOURCES = \
+       ephy-dbus-names.h                       \
        ephy-dbus-util.c                        \
        ephy-dbus-util.h                        \
        ephy-debug.c                            \
@@ -48,6 +49,7 @@ libephymisc_la_SOURCES = \
        ephy-time-helpers.h                     \
        ephy-uri-helpers.c                      \
        ephy-uri-helpers.h                      \
+       ephy-uri-tester-interface.h             \
        ephy-web-app-utils.c                    \
        ephy-web-app-utils.h                    \
        ephy-zoom.c                             \
diff --git a/embed/web-extension/ephy-web-extension-names.h b/lib/ephy-dbus-names.h
similarity index 83%
copy from embed/web-extension/ephy-web-extension-names.h
copy to lib/ephy-dbus-names.h
index b051f44..0202777 100644
--- a/embed/web-extension/ephy-web-extension-names.h
+++ b/lib/ephy-dbus-names.h
@@ -26,4 +26,8 @@ G_BEGIN_DECLS
 #define EPHY_WEB_EXTENSION_OBJECT_PATH  "/org/gnome/Epiphany/WebExtension"
 #define EPHY_WEB_EXTENSION_INTERFACE    "org.gnome.Epiphany.WebExtension"
 
+#define EPHY_URI_TESTER_SERVICE_NAME    "org.gnome.Epiphany.UriTester"
+#define EPHY_URI_TESTER_OBJECT_PATH     "/org/gnome/Epiphany/UriTester"
+#define EPHY_URI_TESTER_INTERFACE       "org.gnome.Epiphany.UriTester"
+
 G_END_DECLS
diff --git a/embed/web-extension/ephy-web-extension-names.h b/lib/ephy-uri-tester-interface.h
similarity index 66%
rename from embed/web-extension/ephy-web-extension-names.h
rename to lib/ephy-uri-tester-interface.h
index b051f44..256277f 100644
--- a/embed/web-extension/ephy-web-extension-names.h
+++ b/lib/ephy-uri-tester-interface.h
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /*
- *  Copyright © 2014 Igalia S.L.
+ *  Copyright © 2016 Igalia S.L.
  *
  *  This file is part of Epiphany.
  *
@@ -22,8 +22,15 @@
 
 G_BEGIN_DECLS
 
-#define EPHY_WEB_EXTENSION_SERVICE_NAME "org.gnome.Epiphany.WebExtension"
-#define EPHY_WEB_EXTENSION_OBJECT_PATH  "/org/gnome/Epiphany/WebExtension"
-#define EPHY_WEB_EXTENSION_INTERFACE    "org.gnome.Epiphany.WebExtension"
+static const char ephy_uri_tester_introspection_xml[] =
+  "<node>"
+  " <interface name='org.gnome.Epiphany.UriTester'>"
+  "  <method name='MaybeRewriteUri'>"
+  "   <arg name='request_uri' type='s' direction='in'/>"
+  "   <arg name='page_uri' type='s' direction='in'/>"
+  "   <arg name='modified_request_uri' type='s' direction='out'/>"
+  "  </method>"
+  " </interface>"
+  "</node>";
 
 G_END_DECLS


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