[epiphany] Use EphyDownload to download adblock filters



commit 6e9137f7a96e316719b39b2ed9096a8a9c9eeeed
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Tue Nov 14 19:25:31 2017 -0600

    Use EphyDownload to download adblock filters
    
    Instead of using gvfs. This means that the adblocker now works even if
    gvfsd-http is not running. In particular, it should now (hopefully) work
    under Flatpak.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=776682

 embed/ephy-download.c                 |    3 +-
 embed/ephy-embed-shell.c              |    8 +-
 {lib => embed}/ephy-filters-manager.c |  101 ++++++++++++++++-----------------
 {lib => embed}/ephy-filters-manager.h |    0
 embed/meson.build                     |    1 +
 lib/meson.build                       |    1 -
 6 files changed, 57 insertions(+), 57 deletions(-)
---
diff --git a/embed/ephy-download.c b/embed/ephy-download.c
index 7140172..d274350 100644
--- a/embed/ephy-download.c
+++ b/embed/ephy-download.c
@@ -687,13 +687,14 @@ download_finished_cb (WebKitDownload *wk_download,
                       EphyDownload   *download)
 {
   download->finished = TRUE;
-  g_signal_emit (download, signals[COMPLETED], 0);
 
   if (g_settings_get_boolean (EPHY_SETTINGS_MAIN, EPHY_PREFS_AUTO_DOWNLOADS) &&
       download->action == EPHY_DOWNLOAD_ACTION_NONE)
     ephy_download_do_download_action (download, EPHY_DOWNLOAD_ACTION_OPEN, download->start_time);
   else
     ephy_download_do_download_action (download, download->action, download->start_time);
+
+  g_signal_emit (download, signals[COMPLETED], 0);
 }
 
 static void
diff --git a/embed/ephy-embed-shell.c b/embed/ephy-embed-shell.c
index 9f45841..443eb7a 100644
--- a/embed/ephy-embed-shell.c
+++ b/embed/ephy-embed-shell.c
@@ -985,10 +985,6 @@ ephy_embed_shell_startup (GApplication *application)
 
   G_APPLICATION_CLASS (ephy_embed_shell_parent_class)->startup (application);
 
-  filters_dir = adblock_filters_dir (shell);
-  priv->filters_manager = ephy_filters_manager_new (filters_dir);
-  g_free (filters_dir);
-
   ephy_embed_shell_create_web_context (shell);
 
   ephy_embed_shell_setup_web_extensions_server (shell);
@@ -1090,6 +1086,10 @@ ephy_embed_shell_startup (GApplication *application)
   ephy_embed_prefs_set_cookie_accept_policy (cookie_manager, cookie_policy);
   g_free (cookie_policy);
 
+  filters_dir = adblock_filters_dir (shell);
+  priv->filters_manager = ephy_filters_manager_new (filters_dir);
+  g_free (filters_dir);
+
 #if ENABLE_HTTPS_EVERYWHERE
     /* We might want to be smarter about this in the future. For now,
      * trigger an update of the rulesets once each time Epiphany is started.
diff --git a/lib/ephy-filters-manager.c b/embed/ephy-filters-manager.c
similarity index 81%
rename from lib/ephy-filters-manager.c
rename to embed/ephy-filters-manager.c
index fcbcdc1..20ca374 100644
--- a/lib/ephy-filters-manager.c
+++ b/embed/ephy-filters-manager.c
@@ -21,6 +21,7 @@
 #include "config.h"
 #include "ephy-filters-manager.h"
 
+#include "ephy-download.h"
 #include "ephy-prefs.h"
 #include "ephy-settings.h"
 #include "ephy-uri-tester-shared.h"
@@ -80,31 +81,20 @@ adblock_filter_file_is_valid (GFile *file)
 
 typedef struct {
   EphyFiltersManager *manager;
-
-  char *src_uri;
-  GFile *filter_file;
-  GFile *tmp_file;
+  EphyDownload *download;
+  char *source_uri;
 } AdblockFilterRetrieveData;
 
 static AdblockFilterRetrieveData *
 adblock_filter_retrieve_data_new (EphyFiltersManager *manager,
-                                  GFile              *src_file,
-                                  GFile              *filter_file)
+                                  EphyDownload       *download,
+                                  char               *source_uri)
 {
   AdblockFilterRetrieveData* data;
-  char *path, *tmp_path;
-
   data = g_slice_new (AdblockFilterRetrieveData);
   data->manager = g_object_ref (manager);
-  data->src_uri = g_file_get_uri (src_file);
-  data->filter_file = g_object_ref (filter_file);
-
-  path = g_file_get_path (filter_file);
-  tmp_path = g_strdup_printf ("%s.tmp", path);
-  g_free (path);
-  data->tmp_file = g_file_new_for_path (tmp_path);
-  g_free (tmp_path);
-
+  data->download = g_object_ref (download);
+  data->source_uri = g_strdup (source_uri);
   return data;
 }
 
@@ -112,57 +102,66 @@ static void
 adblock_filter_retrieve_data_free (AdblockFilterRetrieveData *data)
 {
   g_object_unref (data->manager);
-  g_object_unref (data->filter_file);
-  g_object_unref (data->tmp_file);
-
-  g_free (data->src_uri);
-
+  g_object_unref (data->download);
+  g_free (data->source_uri);
   g_slice_free (AdblockFilterRetrieveData, data);
 }
 
 static void
-retrieve_filter_file_finished (GFile                     *src,
-                               GAsyncResult              *result,
-                               AdblockFilterRetrieveData *data)
+download_completed_cb (EphyDownload              *download,
+                       AdblockFilterRetrieveData *data)
 {
-  GError *error = NULL;
+  g_signal_handlers_disconnect_by_data (download, data);
+  adblock_filter_retrieve_data_free (data);
+}
 
-  if (!g_file_copy_finish (src, result, &error) ||
-      !g_file_move (data->tmp_file, data->filter_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error)) {
-    GFileOutputStream *stream;
+static void
+download_error_cb (EphyDownload              *download,
+                   GError                    *error,
+                   AdblockFilterRetrieveData *data)
+{
+  GFileOutputStream *stream;
+  GFile *file;
 
-    /* If failed to retrieve, create an empty file if it doesn't exist to unblock extensions */
-    stream = g_file_create (data->filter_file, G_FILE_CREATE_NONE, NULL, NULL);
-    if (stream)
-      g_object_unref (stream);
+  /* Create an empty file if it doesn't exist to unblock extensions */
+  file = g_file_new_for_uri (ephy_download_get_destination_uri (download));
+  stream = g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL);
+  if (stream)
+    g_object_unref (stream);
+  g_object_unref (file);
 
-    if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
-      g_warning ("Error retrieving filter %s: %s\n", data->src_uri, error->message);
-    g_error_free (error);
-  }
+  if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+    g_warning ("Error retrieving filter %s: %s\n", data->source_uri, error->message);
 
+  g_signal_handlers_disconnect_by_data (download, data);
   adblock_filter_retrieve_data_free (data);
 }
 
 static void
-retrieve_filter_file (EphyFiltersManager *manager,
-                      const char         *filter_url,
-                      GFile              *file)
+start_retrieving_filter_file (EphyFiltersManager *manager,
+                              const char         *filter_url,
+                              GFile              *destination)
 {
-  GFile *src = g_file_new_for_uri (filter_url);
+  EphyDownload *download;
+  WebKitDownload *wk_download;
   AdblockFilterRetrieveData *data;
+  char *path;
+
+  download = ephy_download_new_for_uri (filter_url);
+  path = g_file_get_uri (destination);
+  ephy_download_set_destination_uri (download, path);
+  g_free (path);
 
-  data = adblock_filter_retrieve_data_new (manager, src, file);
+  wk_download = ephy_download_get_webkit_download (download);
+  webkit_download_set_allow_overwrite (wk_download, TRUE);
 
-  g_file_copy_async (src, data->tmp_file,
-                     G_FILE_COPY_OVERWRITE,
-                     G_PRIORITY_DEFAULT,
-                     manager->cancellable,
-                     NULL, NULL,
-                     (GAsyncReadyCallback)retrieve_filter_file_finished,
-                     data);
+  data = adblock_filter_retrieve_data_new (manager, download, g_strdup (filter_url));
 
-  g_object_unref (src);
+  g_signal_connect (download, "completed",
+                    G_CALLBACK (download_completed_cb), data);
+  g_signal_connect (download, "error",
+                    G_CALLBACK (download_error_cb), data);
+  g_object_unref (download);
 }
 
 static void
@@ -246,7 +245,7 @@ update_adblock_filter_files (EphyFiltersManager *manager)
 
     filter_file = ephy_uri_tester_get_adblock_filter_file (manager->filters_dir, filters[i]);
     if (!adblock_filter_file_is_valid (filter_file))
-      retrieve_filter_file (manager, filters[i], filter_file);
+      start_retrieving_filter_file (manager, filters[i], filter_file);
     files = g_list_prepend (files, filter_file);
   }
 
diff --git a/lib/ephy-filters-manager.h b/embed/ephy-filters-manager.h
similarity index 100%
rename from lib/ephy-filters-manager.h
rename to embed/ephy-filters-manager.h
diff --git a/embed/meson.build b/embed/meson.build
index c9d863c..99315d7 100644
--- a/embed/meson.build
+++ b/embed/meson.build
@@ -23,6 +23,7 @@ libephyembed_sources = [
   'ephy-encoding.c',
   'ephy-encodings.c',
   'ephy-file-monitor.c',
+  'ephy-filters-manager.c',
   'ephy-find-toolbar.c',
   'ephy-option-menu.c',
   'ephy-web-view.c',
diff --git a/lib/meson.build b/lib/meson.build
index 3b62fd0..b9f5d8e 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -17,7 +17,6 @@ libephymisc_sources = [
   'ephy-dnd.c',
   'ephy-favicon-helpers.c',
   'ephy-file-helpers.c',
-  'ephy-filters-manager.c',
   'ephy-flatpak-utils.c',
   'ephy-gui.c',
   'ephy-langs.c',


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