[epiphany] adblock: Add profile migration to remove old filters



commit e5177caf0598b094b4d34c7498190495d378eacd
Author: Adrian Perez de Castro <aperez igalia com>
Date:   Wed Jul 10 00:47:24 2019 +0300

    adblock: Add profile migration to remove old filters
    
    This bumps the revision of the profile migration, and adds a migration
    function which removes the old ad blocker data from disk. Nothing else
    is needed because Epiphany will fetch the filters if needed at startup.
    
    Steps migrate_adblock_filters() and migrate_annoyance_list() can be
    turned into no-ops becuse the new migration steps will remove all the
    old filters anyway.

 embed/ephy-filters-manager.h                 |   3 -
 lib/ephy-profile-utils.h                     |   2 +-
 src/profile-migrator/ephy-profile-migrator.c | 128 +++++----------------------
 3 files changed, 23 insertions(+), 110 deletions(-)
---
diff --git a/embed/ephy-filters-manager.h b/embed/ephy-filters-manager.h
index 7fbb66b98..6bdc394d2 100644
--- a/embed/ephy-filters-manager.h
+++ b/embed/ephy-filters-manager.h
@@ -24,9 +24,6 @@
 
 G_BEGIN_DECLS
 
-#define ADBLOCK_DEFAULT_FILTER_URL "https://easylist.to/easylist/easylist.txt";
-#define ADBLOCK_PRIVACY_FILTER_URL "https://easylist.to/easylist/easyprivacy.txt";
-
 #define EPHY_TYPE_FILTERS_MANAGER (ephy_filters_manager_get_type ())
 
 G_DECLARE_FINAL_TYPE (EphyFiltersManager, ephy_filters_manager, EPHY, FILTERS_MANAGER, GObject)
diff --git a/lib/ephy-profile-utils.h b/lib/ephy-profile-utils.h
index f64f324f5..07926d226 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 32
+#define EPHY_PROFILE_MIGRATION_VERSION 33
 #define EPHY_INSECURE_PASSWORDS_MIGRATION_VERSION 11
 #define EPHY_FIREFOX_SYNC_PASSWORDS_MIGRATION_VERSION 19
 #define EPHY_TARGET_ORIGIN_MIGRATION_VERSION 21
diff --git a/src/profile-migrator/ephy-profile-migrator.c b/src/profile-migrator/ephy-profile-migrator.c
index 744a1b487..b7dcdaec8 100644
--- a/src/profile-migrator/ephy-profile-migrator.c
+++ b/src/profile-migrator/ephy-profile-migrator.c
@@ -85,12 +85,6 @@ legacy_profile_dir (void)
   return dir;
 }
 
-static gboolean
-legacy_dir_is_default (void)
-{
-  return !strcmp (legacy_profile_dir (), legacy_default_profile_dir ());
-}
-
 /*
  * What to do to add new migration steps:
  *  - Bump EPHY_PROFILE_MIGRATION_VERSION in lib/ephy-profile-utils.h
@@ -404,82 +398,6 @@ out:
   g_free (filename);
 }
 
-static void
-migrate_adblock_filters (void)
-{
-  char *adblock_dir;
-  g_autofree char *filters_filename = NULL;
-  char *contents;
-  gsize content_size;
-  GPtrArray *filters_array = NULL;
-  GError *error = NULL;
-
-  adblock_dir = g_build_filename (legacy_profile_dir (), "adblock", NULL);
-  if (!g_file_test (adblock_dir, G_FILE_TEST_IS_DIR)) {
-    g_free (adblock_dir);
-    return;
-  }
-
-  if (!legacy_dir_is_default ()) {
-    /* Adblock filters rules are now shared to save space */
-    ephy_file_delete_dir_recursively (adblock_dir, NULL);
-    g_free (adblock_dir);
-
-    adblock_dir = g_build_filename (legacy_default_profile_dir (), "adblock", NULL);
-  }
-
-  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_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' && strcmp (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_MAIN,
-                         EPHY_PREFS_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_initial_state (void)
 {
@@ -987,28 +905,6 @@ out:
   g_list_free_full (passwords, g_object_unref);
 }
 
-static void
-migrate_annoyance_list (void)
-{
-  GVariant *user_value;
-  const char **filters;
-  char **modified_filters;
-
-  /* Has the filters setting been modified? If not, we're done. */
-  user_value = g_settings_get_user_value (EPHY_SETTINGS_MAIN, EPHY_PREFS_ADBLOCK_FILTERS);
-  if (!user_value)
-    return;
-
-  /* The annoyance list was causing a bunch of problems. Forcibly remove it. */
-  filters = g_variant_get_strv (user_value, NULL);
-  modified_filters = ephy_strv_remove (filters, "https://easylist.to/easylist/fanboy-annoyance.txt";);
-  g_settings_set_strv (EPHY_SETTINGS_MAIN, EPHY_PREFS_ADBLOCK_FILTERS, (const char * const 
*)modified_filters);
-
-  g_variant_unref (user_value);
-  g_free (filters);
-  g_strfreev (modified_filters);
-}
-
 static void
 migrate_zoom_level (void)
 {
@@ -1352,6 +1248,25 @@ migrate_profile_directories (void)
   g_file_delete (gsb_journal_file, NULL, NULL);
 }
 
+static void
+migrate_adblock_to_content_filters (void)
+{
+  /*
+   * Switching from the ABP-format rule sets to the JSON ones is done by
+   * completely removing the adblock/ subdirectory. During startup Epiphany
+   * will read the new setting and download the new filtering rules itself.
+   */
+  g_autofree char *directory = g_build_filename (ephy_cache_dir (), "adblock", NULL);
+
+  g_autoptr (GError) error = NULL;
+  if (!ephy_file_delete_dir_recursively (directory, &error) &&
+      !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
+    g_warning ("Cannot delete adblock directory %s: %s", directory, error->message);
+
+  /* Remove the old key, to save a little space in the dconf store. */
+  g_settings_reset (EPHY_SETTINGS_MAIN, "adblock-filters");
+}
+
 static void
 migrate_nothing (void)
 {
@@ -1379,7 +1294,7 @@ const EphyProfileMigrator migrators[] = {
   /* 10 */ migrate_nothing,
   /* 11 */ migrate_insecure_passwords,
   /* 12 */ migrate_bookmarks,
-  /* 13 */ migrate_adblock_filters,
+  /* 13 */ migrate_nothing,
   /* 14 */ migrate_initial_state,
   /* 15 */ migrate_permissions,
   /* 16 */ migrate_nothing,
@@ -1394,11 +1309,12 @@ const EphyProfileMigrator migrators[] = {
   /* 25 */ migrate_passwords_timestamp,
   /* 26 */ migrate_nothing,
   /* 27 */ migrate_nothing,
-  /* 28 */ migrate_annoyance_list,
+  /* 28 */ migrate_nothing,
   /* 29 */ migrate_zoom_level,
   /* 30 */ migrate_profile_directories,
   /* 31 */ migrate_web_extension_config_dir,
   /* 32 */ migrate_webapps_harder,
+  /* 33 */ migrate_adblock_to_content_filters,
 };
 
 static gboolean


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