[epiphany/pgriffis/web-extension/runtime-send-message: 6/11] WebExtensions: Add MessageSender data to messages




commit 102ca37ffdebfeda8483e39f6dab63b6f201c9c7
Author: Patrick Griffis <pgriffis igalia com>
Date:   Thu Jun 2 10:09:00 2022 -0500

    WebExtensions: Add MessageSender data to messages

 src/webextension/api/runtime.c                | 22 +++++++++++++++++++++-
 src/webextension/ephy-web-extension-manager.c | 21 +++++++++++++++++++++
 src/webextension/ephy-web-extension-manager.h |  4 ++++
 3 files changed, 46 insertions(+), 1 deletion(-)
---
diff --git a/src/webextension/api/runtime.c b/src/webextension/api/runtime.c
index 19cb22afd..159d96864 100644
--- a/src/webextension/api/runtime.c
+++ b/src/webextension/api/runtime.c
@@ -58,6 +58,22 @@ is_empty_object (JSCValue *value)
   return FALSE;
 }
 
+static char *
+create_sender_object (EphyWebExtension *web_extension,
+                      gint64            page_id)
+{
+  g_autoptr (JsonNode) node = json_node_init_object (json_node_alloc (), json_object_new ());
+  JsonObject *obj = json_node_get_object (node);
+  EphyWebExtensionManager *manager = ephy_web_extension_manager_get_default ();
+  WebKitWebView *web_view = ephy_web_extension_manager_get_web_view_for_page_id (manager, web_extension, 
page_id);
+
+  json_object_set_string_member (obj, "id", ephy_web_extension_get_guid (web_extension));
+  if (web_view)
+    json_object_set_string_member (obj, "url", webkit_web_view_get_uri (web_view));
+
+  return json_to_string (node, FALSE);
+}
+
 static void
 runtime_handler_send_message (EphyWebExtension *self,
                               char             *name,
@@ -92,7 +108,11 @@ runtime_handler_send_message (EphyWebExtension *self,
 
   json = jsc_value_to_json (message, 0);
   g_message ("Sending message with %s", json);
-  ephy_web_extension_manager_emit_in_extension_views_with_reply (manager, self, "runtime.onMessage", json, 
extension_page_id, "{}", task);
+  ephy_web_extension_manager_emit_in_extension_views_with_reply (manager, self, "runtime.onMessage",
+                                                                 json,
+                                                                 extension_page_id,
+                                                                 create_sender_object (self, 
extension_page_id),
+                                                                 task);
 
   return;
 }
diff --git a/src/webextension/ephy-web-extension-manager.c b/src/webextension/ephy-web-extension-manager.c
index 71bd02d99..0796e1a6a 100644
--- a/src/webextension/ephy-web-extension-manager.c
+++ b/src/webextension/ephy-web-extension-manager.c
@@ -1349,4 +1349,25 @@ ephy_web_extension_manager_emit_in_extension_views_with_reply (EphyWebExtensionM
   ephy_web_extension_manager_emit_in_extension_views_internal (self, web_extension, name, json, 
extension_page_id, sender_json, reply_task);
 }
 
+WebKitWebView *
+ephy_web_extension_manager_get_web_view_for_page_id (EphyWebExtensionManager *self,
+                                                     EphyWebExtension        *web_extension,
+                                                     gint64                   page_id)
+{
+  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);
+
+  if (background_view && (gint64)webkit_web_view_get_page_id (background_view) == page_id)
+    return background_view;
+
+  if (popup_views) {
+    for (guint i = 0; i < popup_views->len; i++) {
+      WebKitWebView *popup_view = g_ptr_array_index (popup_views, i);
+      if ((gint64)webkit_web_view_get_page_id (popup_view) == page_id)
+        return popup_view;
+    }
+  }
+
+  g_warn_if_reached ();
+  return NULL;
 }
diff --git a/src/webextension/ephy-web-extension-manager.h b/src/webextension/ephy-web-extension-manager.h
index b202e38a0..aea6ec5a7 100644
--- a/src/webextension/ephy-web-extension-manager.h
+++ b/src/webextension/ephy-web-extension-manager.h
@@ -90,4 +90,8 @@ void                     ephy_web_extension_manager_emit_in_extension_views_with
                                                                                      const char              
*sender_json,
                                                                                      GTask                   
*reply_task);
 
+WebKitWebView           *ephy_web_extension_manager_get_web_view_for_page_id        (EphyWebExtensionManager 
*self,
+                                                                                     EphyWebExtension        
*web_extension,
+                                                                                     gint64                  
 page_id);
+
 G_END_DECLS


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