[epiphany/pgriffis/web-extension/no-ucm] WebExtensions: Generate background page if needed
- From: Patrick Griffis <pgriffis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/pgriffis/web-extension/no-ucm] WebExtensions: Generate background page if needed
- Date: Thu, 23 Jun 2022 02:32:35 +0000 (UTC)
commit a736509c97bed18c8909e5351d4bbe9a004c6d1d
Author: Patrick Griffis <pgriffis igalia com>
Date: Wed Jun 22 21:29:33 2022 -0500
WebExtensions: Generate background page if needed
If only scripts are listed we simply generate a background page including those scripts.
This exactly matches Firefox's behavior.
This simplifies the web view creation and usage since it only has a single codepath.
src/webextension/ephy-web-extension-manager.c | 51 ---------------------------
src/webextension/ephy-web-extension.c | 50 ++++++++++++++------------
2 files changed, 28 insertions(+), 73 deletions(-)
---
diff --git a/src/webextension/ephy-web-extension-manager.c b/src/webextension/ephy-web-extension-manager.c
index b02b14695..4bdaca0ae 100644
--- a/src/webextension/ephy-web-extension-manager.c
+++ b/src/webextension/ephy-web-extension-manager.c
@@ -861,14 +861,10 @@ GtkWidget *
ephy_web_extensions_manager_create_web_extensions_webview (EphyWebExtension *web_extension)
{
EphyWebExtensionManager *manager = ephy_web_extension_manager_get_default ();
- g_autoptr (WebKitUserContentManager) ucm = NULL;
WebKitSettings *settings;
WebKitWebContext *web_context;
GtkWidget *web_view;
- /* Create an own ucm so new scripts/css are only applied to this web_view */
- ucm = webkit_user_content_manager_new ();
-
web_context = webkit_web_context_new ();
webkit_web_context_register_uri_scheme (web_context, "ephy-webextension", web_extension_cb, web_extension,
NULL);
@@ -878,7 +874,6 @@ ephy_web_extensions_manager_create_web_extensions_webview (EphyWebExtension *web
web_view = g_object_new (WEBKIT_TYPE_WEB_VIEW,
"web-context", web_context,
- "user-content-manager", ucm,
"settings", ephy_embed_prefs_get_settings (),
"related-view", ephy_web_extension_manager_get_background_web_view (manager,
web_extension),
NULL);
@@ -894,22 +889,6 @@ ephy_web_extensions_manager_create_web_extensions_webview (EphyWebExtension *web
return web_view;
}
-static char *
-create_base_uri_for_resource_path (EphyWebExtension *web_extension,
- const char *resource_path)
-{
- g_autofree char *dir_name = NULL;
-
- if (resource_path) {
- dir_name = g_path_get_dirname (resource_path);
-
- if (g_strcmp0 (dir_name, ".") != 0)
- return g_strdup_printf ("ephy-webextension://%s/%s/", ephy_web_extension_get_guid (web_extension),
dir_name);
- }
-
- return g_strdup_printf ("ephy-webextension://%s/", ephy_web_extension_get_guid (web_extension));
-}
-
static void
on_popup_load_changed (WebKitWebView *web_view,
WebKitLoadEvent load_event,
@@ -1146,11 +1125,8 @@ static void
run_background_script (EphyWebExtensionManager *self,
EphyWebExtension *web_extension)
{
- WebKitUserContentManager *ucm;
GtkWidget *background;
- GPtrArray *scripts;
const char *page;
- g_autofree char *base_uri = NULL;
if (!ephy_web_extension_has_background_web_view (web_extension) ||
ephy_web_extension_manager_get_background_web_view (self, web_extension))
return;
@@ -1166,33 +1142,6 @@ run_background_script (EphyWebExtensionManager *self,
webkit_web_view_load_uri (WEBKIT_WEB_VIEW (background), page_uri);
return;
}
-
- scripts = ephy_web_extension_background_web_view_get_scripts (web_extension);
- base_uri = create_base_uri_for_resource_path (web_extension, page);
-
- ucm = webkit_web_view_get_user_content_manager (WEBKIT_WEB_VIEW (background));
-
- for (unsigned int i = 0; i < scripts->len; i++) {
- const char *script_file = g_ptr_array_index (scripts, i);
- g_autofree char *data = NULL;
- WebKitUserScript *user_script;
-
- if (!script_file)
- continue;
-
- data = ephy_web_extension_get_resource_as_string (web_extension, script_file);
- if (data) {
- user_script = webkit_user_script_new (data,
- WEBKIT_USER_CONTENT_INJECT_TOP_FRAME,
- WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END,
- NULL,
- NULL);
-
- webkit_user_content_manager_add_script (ucm, user_script);
- }
- }
-
- webkit_web_view_load_html (WEBKIT_WEB_VIEW (background), "<body></body>", base_uri);
}
static GPtrArray *
diff --git a/src/webextension/ephy-web-extension.c b/src/webextension/ephy-web-extension.c
index 46169ee98..a170fc1b1 100644
--- a/src/webextension/ephy-web-extension.c
+++ b/src/webextension/ephy-web-extension.c
@@ -67,7 +67,6 @@ typedef struct {
} WebExtensionBrowserAction;
typedef struct {
- GPtrArray *scripts;
char *page;
} WebExtensionBackground;
@@ -120,6 +119,10 @@ G_DEFINE_QUARK (web - extension - error - quark, web_extension_error)
G_DEFINE_TYPE (EphyWebExtension, ephy_web_extension, G_TYPE_OBJECT)
static gboolean is_supported_scheme (const char *scheme);
+static void web_extension_add_resource (EphyWebExtension *self,
+ const char *name,
+ gpointer data,
+ guint len);
gboolean
ephy_web_extension_has_resource (EphyWebExtension *self,
@@ -262,15 +265,12 @@ web_extension_background_new (void)
{
WebExtensionBackground *background = g_malloc0 (sizeof (WebExtensionBackground));
- background->scripts = g_ptr_array_new_full (1, g_free);
-
return background;
}
static void
web_extension_background_free (WebExtensionBackground *background)
{
- g_clear_pointer (&background->scripts, g_ptr_array_unref);
g_clear_pointer (&background->page, g_free);
g_free (background);
}
@@ -511,15 +511,31 @@ web_extension_add_content_script (JsonArray *array,
self->content_scripts = g_list_append (self->content_scripts, content_script);
}
-static void
-web_extension_add_scripts (JsonArray *array,
- guint index_,
- JsonNode *element_node,
- gpointer user_data)
+static char *
+generate_background_page (EphyWebExtension *self,
+ JsonArray *scripts)
{
- EphyWebExtension *self = EPHY_WEB_EXTENSION (user_data);
+ /* The entry point is always an HTML file, if they list scripts we just generate one.
+ * This behavior exactly matches Firefox. */
+ GString *background_page = g_string_new ("<html><head><meta charset=\"utf-8\"></head><body>");
+ char *path = g_strdup ("_generated_background_page.html");
+ gsize len;
+
+ for (unsigned int i = 0; i < json_array_get_length (scripts); i++) {
+ const char *script_file = json_array_get_string_element (scripts, i);
+ g_autofree char *escaped_script_file = g_uri_escape_string (script_file, NULL, FALSE);
- g_ptr_array_add (self->background->scripts, g_strdup (json_node_get_string (element_node)));
+ g_string_append_printf (background_page,
+ "<script type=\"text/javascript\" src=\"ephy-webextension://%s/%s\"></script>",
+ ephy_web_extension_get_guid (self),
+ escaped_script_file);
+ }
+ g_string_append (background_page, "</body>");
+
+ len = background_page->len;
+ web_extension_add_resource (self, path, g_string_free (background_page, FALSE), len);
+
+ return path;
}
static void
@@ -533,7 +549,6 @@ web_extension_add_background (JsonObject *object,
* - persistent with false is not supported yet.
*/
EphyWebExtension *self = EPHY_WEB_EXTENSION (user_data);
- JsonArray *child_array;
if (!json_object_has_member (object, "scripts") && !json_object_has_member (object, "page") &&
!json_object_has_member (object, "persistent")) {
g_warning ("Invalid background section, it must be either scripts, page or persistent entry.");
@@ -544,8 +559,7 @@ web_extension_add_background (JsonObject *object,
self->background = web_extension_background_new ();
if (json_object_has_member (object, "scripts")) {
- child_array = json_object_get_array_member (object, "scripts");
- json_array_foreach_element (child_array, web_extension_add_scripts, self);
+ self->background->page = generate_background_page (self, json_object_get_array_member (object,
"scripts"));
} else if (!self->background->page && json_object_has_member (object, "page")) {
self->background->page = g_strdup (json_object_get_string_member (object, "page"));
} else if (json_object_has_member (object, "persistent")) {
@@ -998,8 +1012,6 @@ ephy_web_extension_load (GFile *target)
json_object_foreach_member (background_object, web_extension_add_background, self);
}
- if (self->background)
- g_ptr_array_add (self->background->scripts, NULL);
if (json_object_has_member (root_object, "page_action")) {
g_autoptr (JsonObject) page_action_object = json_object_get_object_member (root_object, "page_action");
@@ -1117,12 +1129,6 @@ ephy_web_extension_background_web_view_get_page (EphyWebExtension *self)
return self->background->page;
}
-GPtrArray *
-ephy_web_extension_background_web_view_get_scripts (EphyWebExtension *self)
-{
- return self->background->scripts;
-}
-
GList *
ephy_web_extension_get_content_scripts (EphyWebExtension *self)
{
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]