[epiphany/pgriffis/web-extension/csp: 4/6] WebExtensions: Move downloads signal handling to downloads.c
- From: Patrick Griffis <pgriffis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/pgriffis/web-extension/csp: 4/6] WebExtensions: Move downloads signal handling to downloads.c
- Date: Tue, 21 Jun 2022 23:57:45 +0000 (UTC)
commit 44eeb202cbb9ceb26507201934ad8178fe0041ca
Author: Patrick Griffis <pgriffis igalia com>
Date: Tue Jun 21 18:26:49 2022 -0500
WebExtensions: Move downloads signal handling to downloads.c
This also properly disconnects the signals on dispose.
src/webextension/api/downloads.c | 71 ++++++++++++++++++++++++++-
src/webextension/api/downloads.h | 6 ++-
src/webextension/ephy-web-extension-manager.c | 66 +++++--------------------
src/webextension/ephy-web-extension-manager.h | 6 +++
4 files changed, 90 insertions(+), 59 deletions(-)
---
diff --git a/src/webextension/api/downloads.c b/src/webextension/api/downloads.c
index 60ad98635..d0cc791c0 100644
--- a/src/webextension/api/downloads.c
+++ b/src/webextension/api/downloads.c
@@ -21,6 +21,7 @@
#include "config.h"
#include "ephy-file-helpers.h"
+#include "ephy-web-extension-manager.h"
#include "api-utils.h"
#include "downloads.h"
@@ -603,8 +604,8 @@ add_download_to_json (JsonBuilder *builder,
json_builder_end_object (builder);
}
-char *
-ephy_web_extension_api_downloads_download_to_json (EphyDownload *download)
+static char *
+download_to_json (EphyDownload *download)
{
g_autoptr (JsonBuilder) builder = json_builder_new ();
g_autoptr (JsonNode) root = NULL;
@@ -807,3 +808,69 @@ ephy_web_extension_api_downloads_handler (EphyWebExtension *self,
g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_NOT_IMPLEMENTED, "downloads.%s():
Not Implemented", name);
}
+
+typedef struct {
+ const char *event_name;
+ char *json;
+} DownloadEventData;
+
+static void
+foreach_extension_cb (EphyWebExtension *web_extension,
+ gpointer user_data)
+{
+ EphyWebExtensionManager *manager = ephy_web_extension_manager_get_default ();
+ DownloadEventData *data = user_data;
+
+ if (!ephy_web_extension_has_permission (web_extension, "downloads"))
+ return;
+
+ ephy_web_extension_manager_emit_in_extension_views (manager, web_extension, data->event_name, data->json);
+}
+
+static void
+download_added_cb (EphyDownloadsManager *downloads_manager,
+ EphyDownload *download,
+ EphyWebExtensionManager *manager)
+{
+ g_autofree char *json = download_to_json (download);
+ DownloadEventData data = { "downloads.onCreated", json };
+ ephy_web_extension_manager_foreach_extension (manager, foreach_extension_cb, &data);
+}
+
+static void
+download_completed_cb (EphyDownloadsManager *downloads_manager,
+ EphyDownload *download,
+ EphyWebExtensionManager *manager)
+{
+ g_autofree char *json = download_to_json (download);
+ DownloadEventData data = { "downloads.onChanged", json };
+ ephy_web_extension_manager_foreach_extension (manager, foreach_extension_cb, &data);
+}
+
+static void
+download_removed_cb (EphyDownloadsManager *downloads_manager,
+ EphyDownload *download,
+ EphyWebExtensionManager *manager)
+{
+ g_autofree char *json = g_strdup_printf ("%" G_GUINT64_FORMAT, ephy_download_get_uid (download));
+ DownloadEventData data = { "downloads.onErased", json };
+ ephy_web_extension_manager_foreach_extension (manager, foreach_extension_cb, &data);
+}
+
+void
+ephy_web_extension_api_downloads_init (EphyWebExtensionManager *manager)
+{
+ EphyDownloadsManager *downloads_manager = get_downloads_manager ();
+
+ g_signal_connect (downloads_manager, "download-added", G_CALLBACK (download_added_cb), manager);
+ g_signal_connect (downloads_manager, "download-completed", G_CALLBACK (download_completed_cb), manager);
+ g_signal_connect (downloads_manager, "download-removed", G_CALLBACK (download_removed_cb), manager);
+}
+
+void
+ephy_web_extension_api_downloads_dispose (EphyWebExtensionManager *manager)
+{
+ EphyDownloadsManager *downloads_manager = get_downloads_manager ();
+
+ g_signal_handlers_disconnect_by_data (downloads_manager, manager);
+}
diff --git a/src/webextension/api/downloads.h b/src/webextension/api/downloads.h
index 85a0e6193..f91ad6e2e 100644
--- a/src/webextension/api/downloads.h
+++ b/src/webextension/api/downloads.h
@@ -28,12 +28,14 @@
G_BEGIN_DECLS
+void ephy_web_extension_api_downloads_init (EphyWebExtensionManager *manager);
+
+void ephy_web_extension_api_downloads_dispose (EphyWebExtensionManager *manager);
+
void ephy_web_extension_api_downloads_handler (EphyWebExtension *self,
char *name,
JSCValue *value,
WebKitWebView *web_view,
GTask *task);
-char *ephy_web_extension_api_downloads_download_to_json (EphyDownload *download);
-
G_END_DECLS
diff --git a/src/webextension/ephy-web-extension-manager.c b/src/webextension/ephy-web-extension-manager.c
index 34973f1bf..75fb718bd 100644
--- a/src/webextension/ephy-web-extension-manager.c
+++ b/src/webextension/ephy-web-extension-manager.c
@@ -210,57 +210,6 @@ destroy_widget_list (GSList *widget_list)
g_slist_free_full (widget_list, (GDestroyNotify)gtk_widget_destroy);
}
-static void
-download_added_cb (EphyDownloadsManager *downloads_manager,
- EphyDownload *download,
- EphyWebExtensionManager *manager)
-{
- for (GList *l = manager->web_extensions; l; l = g_list_next (l)) {
- EphyWebExtension *extension = l->data;
- g_autofree char *json = NULL;
-
- if (!ephy_web_extension_has_permission (extension, "downloads"))
- continue;
-
- json = ephy_web_extension_api_downloads_download_to_json (download);
- ephy_web_extension_manager_emit_in_extension_views (manager, extension, "downloads.onCreated", json);
- }
-}
-
-static void
-download_completed_cb (EphyDownloadsManager *downloads_manager,
- EphyDownload *download,
- EphyWebExtensionManager *manager)
-{
- for (GList *l = manager->web_extensions; l; l = g_list_next (l)) {
- EphyWebExtension *extension = l->data;
- g_autofree char *json = NULL;
-
- if (!ephy_web_extension_has_permission (extension, "downloads"))
- continue;
-
- json = ephy_web_extension_api_downloads_download_to_json (download);
- ephy_web_extension_manager_emit_in_extension_views (manager, extension, "downloads.onChanged", json);
- }
-}
-
-static void
-download_removed_cb (EphyDownloadsManager *downloads_manager,
- EphyDownload *download,
- EphyWebExtensionManager *manager)
-{
- for (GList *l = manager->web_extensions; l; l = g_list_next (l)) {
- EphyWebExtension *extension = l->data;
- g_autofree char *json = NULL;
-
- if (!ephy_web_extension_has_permission (extension, "downloads"))
- continue;
-
- json = g_strdup_printf ("%" G_GUINT64_FORMAT, ephy_download_get_uid (download));
- ephy_web_extension_manager_emit_in_extension_views (manager, extension, "downloads.onErased", json);
- }
-}
-
static void
ephy_web_extension_manager_constructed (GObject *object)
{
@@ -282,6 +231,8 @@ ephy_web_extension_manager_dispose (GObject *object)
{
EphyWebExtensionManager *self = EPHY_WEB_EXTENSION_MANAGER (object);
+ ephy_web_extension_api_downloads_dispose (self);
+
g_clear_pointer (&self->background_web_views, g_hash_table_destroy);
g_clear_pointer (&self->popup_web_views, g_hash_table_destroy);
g_clear_pointer (&self->page_action_map, g_hash_table_destroy);
@@ -308,7 +259,6 @@ ephy_web_extension_manager_class_init (EphyWebExtensionManagerClass *klass)
static void
ephy_web_extension_manager_init (EphyWebExtensionManager *self)
{
- EphyDownloadsManager *downloads_manager = ephy_embed_shell_get_downloads_manager
(ephy_embed_shell_get_default ());
WebKitWebContext *web_context;
web_context = ephy_embed_shell_get_web_context (ephy_embed_shell_get_default ());
@@ -316,9 +266,7 @@ ephy_web_extension_manager_init (EphyWebExtensionManager *self)
webkit_security_manager_register_uri_scheme_as_secure (webkit_web_context_get_security_manager
(web_context),
"ephy-webextension");
- g_signal_connect (downloads_manager, "download-added", G_CALLBACK (download_added_cb), self);
- g_signal_connect (downloads_manager, "download-completed", G_CALLBACK (download_completed_cb), self);
- g_signal_connect (downloads_manager, "download-removed", G_CALLBACK (download_removed_cb), self);
+ ephy_web_extension_api_downloads_init (self);
}
EphyWebExtensionManager *
@@ -1291,6 +1239,14 @@ application_window_added_timeout_cb (gpointer user_data)
return G_SOURCE_REMOVE;
}
+void
+ephy_web_extension_manager_foreach_extension (EphyWebExtensionManager *self,
+ EphyWebExtensionForeachFunc func,
+ gpointer user_data)
+{
+ g_list_foreach (self->web_extensions, (GFunc)func, user_data);
+}
+
static void
application_window_added_cb (EphyShell *shell,
EphyWindow *window,
diff --git a/src/webextension/ephy-web-extension-manager.h b/src/webextension/ephy-web-extension-manager.h
index decb1016c..ecf2b59fa 100644
--- a/src/webextension/ephy-web-extension-manager.h
+++ b/src/webextension/ephy-web-extension-manager.h
@@ -31,6 +31,8 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE (EphyWebExtensionManager, ephy_web_extension_manager, EPHY, WEB_EXTENSION_MANAGER,
GObject)
+typedef void (*EphyWebExtensionForeachFunc) (EphyWebExtension *extension, gpointer user_data);
+
EphyWebExtensionManager *ephy_web_extension_manager_get_default (void);
GList *ephy_web_extension_manager_get_web_extensions (EphyWebExtensionManager
*self);
@@ -102,4 +104,8 @@ void ephy_web_extension_manager_emit_in_tab_with_reply
GtkWidget *ephy_web_extensions_manager_create_web_extensions_webview (EphyWebExtension
*web_extension);
+void ephy_web_extension_manager_foreach_extension (EphyWebExtensionManager
*self,
+
EphyWebExtensionForeachFunc func,
+ gpointer
user_data);
+
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]