[epiphany/pgriffis/web-extension/no-ucm: 8/8] WebExtensions: Generate background page if needed




commit 053ece5fa4847b1e6ac4a54c13445c19153db29f
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 ++++++++++++++------------
 src/webextension/ephy-web-extension.h         |  2 --
 3 files changed, 27 insertions(+), 76 deletions(-)
---
diff --git a/src/webextension/ephy-web-extension-manager.c b/src/webextension/ephy-web-extension-manager.c
index 43dc3853a..ad78d8827 100644
--- a/src/webextension/ephy-web-extension-manager.c
+++ b/src/webextension/ephy-web-extension-manager.c
@@ -881,7 +881,6 @@ 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;
   g_autoptr (WebKitSettings) settings = NULL;
   WebKitWebContext *web_context;
   GtkWidget *web_view;
@@ -899,9 +898,6 @@ ephy_web_extensions_manager_create_web_extensions_webview (EphyWebExtension *web
   else
     webkit_settings_set_user_agent_with_application_details (settings, "Epiphany", EPHY_VERSION);
 
-  /* 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);
@@ -911,7 +907,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", settings,
                            "related-view", ephy_web_extension_manager_get_background_web_view (manager, 
web_extension),
                            NULL);
@@ -923,22 +918,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,
@@ -1175,11 +1154,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;
@@ -1195,33 +1171,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..ce35b2cf1 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,29 @@ 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)
-{
-  EphyWebExtension *self = EPHY_WEB_EXTENSION (user_data);
+static char *
+generate_background_page (EphyWebExtension *self,
+                          JsonArray        *scripts)
+{
+  /* The entry point is always an HTML file, if they list scripts we just generate one.
+   * This behavior exactly matches Firefox. */
+  g_autoptr (GString) background_page = g_string_new ("<html><head><meta charset=\"utf-8\"></head><body>");
+  char *path = g_strdup ("_generated_background_page.html");
+
+  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, 
G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, FALSE);
+
+    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>");
 
-  g_ptr_array_add (self->background->scripts, g_strdup (json_node_get_string (element_node)));
+  web_extension_add_resource (self, path, background_page->str, background_page->len);
+
+  return path;
 }
 
 static void
@@ -533,7 +547,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 +557,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 +1010,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 +1127,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)
 {
diff --git a/src/webextension/ephy-web-extension.h b/src/webextension/ephy-web-extension.h
index 2b332b7a8..d3d8577e9 100644
--- a/src/webextension/ephy-web-extension.h
+++ b/src/webextension/ephy-web-extension.h
@@ -115,8 +115,6 @@ const char            *ephy_web_extension_browser_action_get_tooltip      (EphyW
 
 const char            *ephy_web_extension_get_browser_popup               (EphyWebExtension *self);
 
-GPtrArray             *ephy_web_extension_background_web_view_get_scripts (EphyWebExtension *self);
-
 GList                 *ephy_web_extension_get_content_scripts             (EphyWebExtension *self);
 
 GList                 *ephy_web_extension_get_content_script_js           (EphyWebExtension *self,


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