[epiphany] Bring back the support for multiple adblock filter files



commit f77c643a580fd59793ab1b96a95f38f5716eec6f
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Wed Jan 18 15:11:03 2017 +0100

    Bring back the support for multiple adblock filter files

 embed/ephy-embed-shell.c              |   22 ++++++++-----
 embed/web-extension/ephy-uri-tester.c |   57 ++++++++++++++++++++-------------
 lib/ephy-uri-tester-shared.c          |   13 ++------
 lib/ephy-uri-tester-shared.h          |    3 +-
 4 files changed, 54 insertions(+), 41 deletions(-)
---
diff --git a/embed/ephy-embed-shell.c b/embed/ephy-embed-shell.c
index 92b7b5c..30b4c3c 100644
--- a/embed/ephy-embed-shell.c
+++ b/embed/ephy-embed-shell.c
@@ -770,10 +770,11 @@ ephy_embed_shell_retrieve_filter_file_finished (GFile                     *src,
 
 static void
 ephy_embed_shell_retrieve_filter_file (EphyEmbedShell *shell,
+                                       const char     *filter_url,
                                        GFile          *file)
 {
   EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
-  GFile *src = g_file_new_for_uri (ADBLOCK_DEFAULT_FILTER_URL);
+  GFile *src = g_file_new_for_uri (filter_url);
   AdblockFilterRetrieveData *data;
 
   if (!priv->uri_tester_update_cancellable)
@@ -793,16 +794,21 @@ ephy_embed_shell_retrieve_filter_file (EphyEmbedShell *shell,
 }
 
 static void
-ephy_embed_shell_update_adblock_filter_file (EphyEmbedShell *shell)
+ephy_embed_shell_update_adblock_filter_files (EphyEmbedShell *shell)
 {
-  GFile *filter_file;
+  char **filters;
 
-  filter_file = ephy_uri_tester_get_adblock_filter_file (ephy_embed_shell_ensure_adblock_data_dir (shell));
+  filters = g_settings_get_strv (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_ADBLOCK_FILTERS);
+  for (guint i = 0; filters[i]; i++) {
+    GFile *filter_file;
 
-  if (!adblock_filter_file_is_valid (filter_file))
-    ephy_embed_shell_retrieve_filter_file (shell, filter_file);
+    filter_file = ephy_uri_tester_get_adblock_filter_file (ephy_embed_shell_ensure_adblock_data_dir (shell), 
filters[i]);
+    if (!adblock_filter_file_is_valid (filter_file))
+      ephy_embed_shell_retrieve_filter_file (shell, filters[i], filter_file);
+    g_object_unref (filter_file);
+  }
 
-  g_object_unref (filter_file);
+  g_strfreev (filters);
 }
 
 static void
@@ -810,7 +816,7 @@ ephy_embed_shell_update_uri_tester (EphyEmbedShell *shell)
 {
   EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
 
-  ephy_embed_shell_update_adblock_filter_file (shell);
+  ephy_embed_shell_update_adblock_filter_files (shell);
 
   if (priv->mode != EPHY_EMBED_SHELL_MODE_TEST &&
       priv->mode != EPHY_EMBED_SHELL_MODE_SEARCH_PROVIDER) {
diff --git a/embed/web-extension/ephy-uri-tester.c b/embed/web-extension/ephy-uri-tester.c
index 41b5b23..073bd87 100644
--- a/embed/web-extension/ephy-uri-tester.c
+++ b/embed/web-extension/ephy-uri-tester.c
@@ -28,6 +28,8 @@
 #include "ephy-uri-tester.h"
 
 #include "ephy-debug.h"
+#include "ephy-prefs.h"
+#include "ephy-settings.h"
 #include "ephy-uri-tester-shared.h"
 
 #include <gio/gio.h>
@@ -65,6 +67,7 @@ struct _EphyUriTester {
   GRegex *regex_frame_add;
 
   GMainLoop *load_loop;
+  int adblock_filters_to_load;
   gboolean adblock_loaded;
 #ifdef HAVE_LIBHTTPSEVERYWHERE
   gboolean https_everywhere_loaded;
@@ -509,13 +512,15 @@ ephy_uri_tester_parse_line (EphyUriTester *tester,
 static void
 ephy_uri_tester_adblock_loaded (EphyUriTester *tester)
 {
-  tester->adblock_loaded = TRUE;
+  if (g_atomic_int_dec_and_test (&tester->adblock_filters_to_load)) {
+    tester->adblock_loaded = TRUE;
 #ifdef HAVE_LIBHTTPSEVERYWHERE
-  if (tester->https_everywhere_loaded)
-    g_main_loop_quit (tester->load_loop);
+    if (tester->https_everywhere_loaded)
+      g_main_loop_quit (tester->load_loop);
 #else
-  g_main_loop_quit (tester->load_loop);
+    g_main_loop_quit (tester->load_loop);
 #endif
+  }
 }
 
 #ifdef HAVE_LIBHTTPSEVERYWHERE
@@ -653,8 +658,8 @@ ephy_uri_tester_load_sync (GTask         *task,
                            EphyUriTester *tester)
 {
   GMainContext *context;
-  GFile *filter_file;
-  GFileMonitor *monitor = NULL;
+  char **filters;
+  GList *monitors = NULL;
 
   context = g_main_context_new ();
   tester->load_loop = g_main_loop_new (context, FALSE);
@@ -667,29 +672,37 @@ ephy_uri_tester_load_sync (GTask         *task,
                                  tester);
 #endif
 
-  filter_file = ephy_uri_tester_get_adblock_filter_file (tester->adblock_data_dir);
-  if (!g_file_query_exists (filter_file, NULL)) {
-    GError *error = NULL;
+  filters = g_settings_get_strv (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_ADBLOCK_FILTERS);
+  tester->adblock_filters_to_load = g_strv_length (filters);
+  for (guint i = 0; filters[i]; i++) {
+    GFile *filter_file;
+
+    filter_file = ephy_uri_tester_get_adblock_filter_file (tester->adblock_data_dir, filters[i]);
+    if (!g_file_query_exists (filter_file, NULL)) {
+      GFileMonitor *monitor;
+      GError *error = NULL;
 
-    monitor = g_file_monitor_file (filter_file, G_FILE_MONITOR_WATCH_MOVES, NULL, &error);
-    if (monitor) {
-       g_signal_connect (monitor, "changed", G_CALLBACK (adblock_file_monitor_changed), tester);
+      monitor = g_file_monitor_file (filter_file, G_FILE_MONITOR_WATCH_MOVES, NULL, &error);
+      if (monitor) {
+        monitors = g_list_prepend (monitors, monitor);
+        g_signal_connect (monitor, "changed", G_CALLBACK (adblock_file_monitor_changed), tester);
+      } else {
+        g_warning ("Failed to monitor adblock file: %s\n", error->message);
+        g_error_free (error);
+        ephy_uri_tester_adblock_loaded (tester);
+      }
     } else {
-      g_warning ("Failed to monitor adblock file: %s\n", error->message);
-      g_error_free (error);
-      ephy_uri_tester_adblock_loaded (tester);
+      g_file_read_async (filter_file, G_PRIORITY_DEFAULT_IDLE, NULL,
+                         (GAsyncReadyCallback)file_read_cb,
+                         tester);
     }
-  } else {
-    g_file_read_async (filter_file, G_PRIORITY_DEFAULT_IDLE, NULL,
-                       (GAsyncReadyCallback)file_read_cb,
-                       tester);
+    g_object_unref (filter_file);
   }
-  g_object_unref (filter_file);
+  g_strfreev (filters);
 
   g_main_loop_run (tester->load_loop);
 
-  if (monitor)
-    g_object_unref (monitor);
+  g_list_free_full (monitors, g_object_unref);
   g_main_context_pop_thread_default (context);
   g_main_context_unref (context);
 
diff --git a/lib/ephy-uri-tester-shared.c b/lib/ephy-uri-tester-shared.c
index edee1d4..323b360 100644
--- a/lib/ephy-uri-tester-shared.c
+++ b/lib/ephy-uri-tester-shared.c
@@ -21,21 +21,14 @@
 #include "config.h"
 #include "ephy-uri-tester-shared.h"
 
-#include "ephy-prefs.h"
-#include "ephy-settings.h"
-
 GFile *
-ephy_uri_tester_get_adblock_filter_file (const char *adblock_data_dir)
+ephy_uri_tester_get_adblock_filter_file (const char *adblock_data_dir,
+                                         const char *filter_url)
 {
-  char **filters;
   char *filter_filename, *filter_path;
   GFile *filter_file;
 
-  filters = g_settings_get_strv (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_ADBLOCK_FILTERS);
-
-  /* FIXME: really support multiple filters */
-  filter_filename = g_compute_checksum_for_string (G_CHECKSUM_MD5, filters[0] ? filters[0] : 
ADBLOCK_DEFAULT_FILTER_URL, -1);
-  g_strfreev (filters);
+  filter_filename = g_compute_checksum_for_string (G_CHECKSUM_MD5, filter_url, -1);
   filter_path = g_build_filename (adblock_data_dir, filter_filename, NULL);
   g_free (filter_filename);
   filter_file = g_file_new_for_path (filter_path);
diff --git a/lib/ephy-uri-tester-shared.h b/lib/ephy-uri-tester-shared.h
index bbb8fd9..1badc03 100644
--- a/lib/ephy-uri-tester-shared.h
+++ b/lib/ephy-uri-tester-shared.h
@@ -26,6 +26,7 @@ G_BEGIN_DECLS
 
 #define ADBLOCK_DEFAULT_FILTER_URL "https://easylist-downloads.adblockplus.org/easylist.txt";
 
-GFile *ephy_uri_tester_get_adblock_filter_file (const char *adblock_data_dir);
+GFile *ephy_uri_tester_get_adblock_filter_file (const char *adblock_data_dir,
+                                                const char *filter_url);
 
 G_END_DECLS


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