[epiphany] Move the adblock filter list to gsettings



commit 10cf854c6d8f8a1821245e9094ecfb7036f47c39
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Wed Jan 18 13:36:15 2017 +0100

    Move the adblock filter list to gsettings
    
    It used to be a file in the adblock data dir. Now that adblock dir is
    shared, it's much better to store the filters in gsettings. It will also
    make easier to handle in case we eventually add a UI to manage filters.
    For now it always uses the first filter in the list, since the support
    for multiple filters was recently removed.
    The profile migrator now removes the adblock folder from non-default
    profile dirs and reads the filters.list file from the default profile
    dir to set its contents in gsettings.

 data/org.gnome.epiphany.gschema.xml          |    7 ++-
 embed/ephy-embed-shell.c                     |    5 +-
 lib/ephy-prefs.h                             |    1 +
 lib/ephy-profile-utils.h                     |    2 +-
 lib/ephy-uri-tester-shared.c                 |   10 +++-
 lib/ephy-uri-tester-shared.h                 |    2 +-
 src/profile-migrator/ephy-profile-migrator.c |   83 ++++++++++++++++++++++++++
 7 files changed, 102 insertions(+), 8 deletions(-)
---
diff --git a/data/org.gnome.epiphany.gschema.xml b/data/org.gnome.epiphany.gschema.xml
index d694482..1a51888 100644
--- a/data/org.gnome.epiphany.gschema.xml
+++ b/data/org.gnome.epiphany.gschema.xml
@@ -182,9 +182,14 @@
                </key>
                <key type="b" name="enable-adblock">
                        <default>true</default>
-                       <summary>Enable Adblock</summary>
+                       <summary>Enable adblock</summary>
                        <description>Whether to block the embedded advertisements that web pages might want 
to show.</description>
                </key>
+                <key type="as" name="adblock-filters">
+                        <default>['https://easylist-downloads.adblockplus.org/easylist.txt']</default>
+                        <summary>List of adblock filters</summary>
+                        <description>List of URLs with filter rules to be used by the adblock.</description>
+                </key>
        </schema>
        <schema path="/org/gnome/epiphany/state/" id="org.gnome.Epiphany.state">
                <key type="s" name="download-dir">
diff --git a/embed/ephy-embed-shell.c b/embed/ephy-embed-shell.c
index 2778773..92b7b5c 100644
--- a/embed/ephy-embed-shell.c
+++ b/embed/ephy-embed-shell.c
@@ -664,9 +664,6 @@ ephy_embed_shell_ensure_adblock_data_dir (EphyEmbedShell *shell)
   if (priv->adblock_data_dir)
     return priv->adblock_data_dir;
 
-  /* The filters list is large, so we don't want to store a separate copy per
-   * web app, but users should otherwise be able to configure different filters
-   * per profile directory. */
   if (priv->mode == EPHY_EMBED_SHELL_MODE_APPLICATION) {
     char *default_dot_dir = ephy_default_dot_dir ();
 
@@ -776,7 +773,7 @@ ephy_embed_shell_retrieve_filter_file (EphyEmbedShell *shell,
                                        GFile          *file)
 {
   EphyEmbedShellPrivate *priv = ephy_embed_shell_get_instance_private (shell);
-  GFile *src = g_file_new_for_uri (ADBLOCK_FILTER_URL);
+  GFile *src = g_file_new_for_uri (ADBLOCK_DEFAULT_FILTER_URL);
   AdblockFilterRetrieveData *data;
 
   if (!priv->uri_tester_update_cancellable)
diff --git a/lib/ephy-prefs.h b/lib/ephy-prefs.h
index 683732b..12e3fc2 100644
--- a/lib/ephy-prefs.h
+++ b/lib/ephy-prefs.h
@@ -84,6 +84,7 @@ typedef enum
 #define EPHY_PREFS_WEB_DEFAULT_ENCODING      "default-encoding"
 #define EPHY_PREFS_WEB_DO_NOT_TRACK          "do-not-track"
 #define EPHY_PREFS_WEB_ENABLE_ADBLOCK        "enable-adblock"
+#define EPHY_PREFS_WEB_ADBLOCK_FILTERS       "adblock-filters"
 
 #define EPHY_PREFS_SCHEMA                         "org.gnome.Epiphany"
 #define EPHY_PREFS_HOMEPAGE_URL                   "homepage-url"
diff --git a/lib/ephy-profile-utils.h b/lib/ephy-profile-utils.h
index aade307..956e7df 100644
--- a/lib/ephy-profile-utils.h
+++ b/lib/ephy-profile-utils.h
@@ -24,7 +24,7 @@
 
 G_BEGIN_DECLS
 
-#define EPHY_PROFILE_MIGRATION_VERSION 11
+#define EPHY_PROFILE_MIGRATION_VERSION 12
 
 #define EPHY_BOOKMARKS_FILE     "bookmarks.gvdb"
 #define EPHY_HISTORY_FILE       "ephy-history.db"
diff --git a/lib/ephy-uri-tester-shared.c b/lib/ephy-uri-tester-shared.c
index cf4ccee..edee1d4 100644
--- a/lib/ephy-uri-tester-shared.c
+++ b/lib/ephy-uri-tester-shared.c
@@ -21,13 +21,21 @@
 #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)
 {
+  char **filters;
   char *filter_filename, *filter_path;
   GFile *filter_file;
 
-  filter_filename = g_compute_checksum_for_string (G_CHECKSUM_MD5, ADBLOCK_FILTER_URL, -1);
+  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_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 abbbee1..bbb8fd9 100644
--- a/lib/ephy-uri-tester-shared.h
+++ b/lib/ephy-uri-tester-shared.h
@@ -24,7 +24,7 @@
 
 G_BEGIN_DECLS
 
-#define ADBLOCK_FILTER_URL "https://easylist-downloads.adblockplus.org/easylist.txt";
+#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);
 
diff --git a/src/profile-migrator/ephy-profile-migrator.c b/src/profile-migrator/ephy-profile-migrator.c
index f4bd2a8..7078900 100644
--- a/src/profile-migrator/ephy-profile-migrator.c
+++ b/src/profile-migrator/ephy-profile-migrator.c
@@ -39,6 +39,7 @@
 #include "ephy-profile-utils.h"
 #include "ephy-settings.h"
 #include "ephy-sqlite-connection.h"
+#include "ephy-uri-tester-shared.h"
 #include "ephy-web-app-utils.h"
 
 #include <fcntl.h>
@@ -605,6 +606,87 @@ migrate_bookmarks (void)
 }
 
 static void
+migrate_adblock_filters (void)
+{
+  char *adblock_dir;
+  char *filters_filename;
+  char *contents;
+  gsize content_size;
+  GPtrArray *filters_array = NULL;
+  GError *error = NULL;
+
+  adblock_dir = g_build_filename (ephy_dot_dir (), "adblock", NULL);
+  if (!g_file_test (adblock_dir, G_FILE_TEST_IS_DIR)) {
+    g_free (adblock_dir);
+    return;
+  }
+
+  if (!ephy_dot_dir_is_default ()) {
+    char *default_dot_dir;
+
+    /* Adblock filters rules are now shared to save space */
+    ephy_file_delete_dir_recursively (adblock_dir, NULL);
+    g_free (adblock_dir);
+
+    default_dot_dir = ephy_default_dot_dir ();
+    adblock_dir = g_build_filename (default_dot_dir, "adblock", NULL);
+    g_free (default_dot_dir);
+  }
+
+  filters_filename = g_build_filename (adblock_dir, "filters.list", NULL);
+  g_free (adblock_dir);
+
+  if (!g_file_get_contents (filters_filename, &contents, &content_size, &error)) {
+    if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+      g_warning ("Failed to read filters file: %s: %s", filters_filename, error->message);
+    g_free (filters_filename);
+    g_error_free (error);
+    return;
+  }
+
+  if (content_size > 0) {
+    char **filter_list;
+    guint  filter_list_length;
+    guint  i;
+
+    filter_list = g_strsplit (contents, ";", -1);
+    filter_list_length = g_strv_length (filter_list);
+    if (filter_list_length > 0) {
+      filters_array = g_ptr_array_sized_new (MAX (2, filter_list_length) + 1);
+      g_ptr_array_set_free_func (filters_array, g_free);
+      g_ptr_array_add (filters_array, g_strdup (ADBLOCK_DEFAULT_FILTER_URL));
+
+      for (i = 0; filter_list[i]; i++) {
+        char *url;
+
+        url = g_strstrip (filter_list[i]);
+        if (url[0] != '\0' && !g_str_equal (url, ADBLOCK_DEFAULT_FILTER_URL))
+          g_ptr_array_add (filters_array, g_strdup (url));
+      }
+
+      if (filters_array->len == 1) {
+        /* No additional filters, so do nothing. */
+        g_ptr_array_free (filters_array, TRUE);
+        filters_array = NULL;
+      } else {
+        g_ptr_array_add (filters_array, NULL);
+      }
+    }
+    g_strfreev (filter_list);
+  }
+
+  if (filters_array) {
+    g_settings_set_strv (EPHY_SETTINGS_WEB,
+                         EPHY_PREFS_WEB_ADBLOCK_FILTERS,
+                         (const gchar * const *)filters_array->pdata);
+    g_settings_sync ();
+    g_ptr_array_free (filters_array, TRUE);
+  }
+
+  g_unlink (filters_filename);
+}
+
+static void
 migrate_nothing (void)
 {
   /* Used to replace migrators that have been removed. Only remove migrators
@@ -627,6 +709,7 @@ const EphyProfileMigrator migrators[] = {
   migrate_form_passwords_to_libsecret,
   migrate_app_desktop_file_categories,
   migrate_bookmarks,
+  migrate_adblock_filters,
 };
 
 static gboolean


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