[epiphany/pgriffis/web-extension/runtime-send-message: 18/29] WebExtensions: Fix runtime.onMessage being emitted in wrong views




commit b7faa01fde82ee88a5336eda5c13902f36026491
Author: Patrick Griffis <pgriffis igalia com>
Date:   Sat May 28 14:44:52 2022 -0500

    WebExtensions: Fix runtime.onMessage being emitted in wrong views

 src/webextension/api/runtime.c                |  5 ++-
 src/webextension/ephy-web-extension-manager.c | 52 +++++++++++++++++++++------
 src/webextension/ephy-web-extension-manager.h |  7 ++++
 3 files changed, 51 insertions(+), 13 deletions(-)
---
diff --git a/src/webextension/api/runtime.c b/src/webextension/api/runtime.c
index 617ae59fb..833caae60 100644
--- a/src/webextension/api/runtime.c
+++ b/src/webextension/api/runtime.c
@@ -55,12 +55,11 @@ runtime_handler_send_message (EphyWebExtension  *self,
                               GError           **error)
 {
   EphyWebExtensionManager *manager = ephy_web_extension_manager_get_default ();
-  WebKitWebView *view = ephy_web_extension_manager_get_background_web_view (manager, self);
   g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
   g_autofree char *script = NULL;
+  g_autofree char *json = jsc_value_to_json (value, 0);
 
-  script = g_strdup_printf ("window.browser.runtime.onMessage._emit(%s);", jsc_value_to_json (value, 0));
-  webkit_web_view_run_javascript (view, script, NULL, NULL, NULL);
+  ephy_web_extension_manager_emit_in_extension_views_except_self (manager, self, "runtime.onMessage", json, 
context_guid);
 
   return NULL;
 }
diff --git a/src/webextension/ephy-web-extension-manager.c b/src/webextension/ephy-web-extension-manager.c
index 66192585c..61e7aca69 100644
--- a/src/webextension/ephy-web-extension-manager.c
+++ b/src/webextension/ephy-web-extension-manager.c
@@ -1146,27 +1146,39 @@ ephy_web_extension_manager_get_page_action (EphyWebExtensionManager *self,
   return ret;
 }
 
-void
-ephy_web_extension_manager_emit_in_extension_views (EphyWebExtensionManager *self,
-                                                    EphyWebExtension        *web_extension,
-                                                    const char              *name,
-                                                    const char              *json)
+static const char *
+get_guid_for_view (WebKitWebView *view)
+{
+  WebKitWebContext *context = webkit_web_view_get_context (view);
+  return g_object_get_data (G_OBJECT (context), "guid");
+}
+
+static void
+ephy_web_extension_manager_emit_in_extension_views_internal (EphyWebExtensionManager *self,
+                                                             EphyWebExtension        *web_extension,
+                                                             const char              *name,
+                                                             const char              *json,
+                                                             const char              *exception_guid)
 {
   WebKitWebView *background_view = ephy_web_extension_manager_get_background_web_view (self, web_extension);
   GPtrArray *popup_views = g_hash_table_lookup (self->popup_web_views, web_extension);
   g_autofree char *script = g_strdup_printf ("window.browser.%s._emit(%s);", name, json);
 
   if (background_view) {
-    webkit_web_view_run_javascript (background_view,
-                                    script,
-                                    NULL,
-                                    NULL,
-                                    NULL);
+    if (g_strcmp0 (get_guid_for_view (background_view), exception_guid) != 0)
+      webkit_web_view_run_javascript (background_view,
+                                      script,
+                                      NULL,
+                                      NULL,
+                                      NULL);
   }
 
   if (popup_views) {
     for (guint i = 0; i < popup_views->len; i++) {
       WebKitWebView *popup_view = g_ptr_array_index (popup_views, i);
+      if (g_strcmp0 (get_guid_for_view (popup_view), exception_guid) == 0)
+        continue;
+
       webkit_web_view_run_javascript (popup_view,
                                       script,
                                       NULL,
@@ -1175,3 +1187,23 @@ ephy_web_extension_manager_emit_in_extension_views (EphyWebExtensionManager *sel
     }
   }
 }
+
+void
+ephy_web_extension_manager_emit_in_extension_views (EphyWebExtensionManager *self,
+                                                    EphyWebExtension        *web_extension,
+                                                    const char              *name,
+                                                    const char              *json)
+{
+  ephy_web_extension_manager_emit_in_extension_views_internal (self, web_extension, name, json, NULL);
+}
+
+void
+ephy_web_extension_manager_emit_in_extension_views_except_self (EphyWebExtensionManager *self,
+                                                                EphyWebExtension        *web_extension,
+                                                                const char              *name,
+                                                                const char              *json,
+                                                                const char              *context_guid)
+{
+  g_assert (context_guid);
+  ephy_web_extension_manager_emit_in_extension_views_internal (self, web_extension, name, json, 
context_guid);
+}
diff --git a/src/webextension/ephy-web-extension-manager.h b/src/webextension/ephy-web-extension-manager.h
index 8cfe03e83..70bbf7e07 100644
--- a/src/webextension/ephy-web-extension-manager.h
+++ b/src/webextension/ephy-web-extension-manager.h
@@ -74,4 +74,11 @@ void                     ephy_web_extension_manager_emit_in_extension_views
                                                                                      const char              
*name,
                                                                                      const char              
*json);
 
+void                     ephy_web_extension_manager_emit_in_extension_views_except_self
+                                                                                    (EphyWebExtensionManager 
*self,
+                                                                                     EphyWebExtension        
*web_extension,
+                                                                                     const char              
*name,
+                                                                                     const char              
*json,
+                                                                                     const char              
*context_guid);
+
 G_END_DECLS


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