[epiphany/pgriffis/web-extension/runtime-send-message: 1/4] WebExtensions: Assign a GUID to each extension view's context




commit 2fb06bce05a2426d50a90fbd9515f6c6aaf1509e
Author: Patrick Griffis <pgriffis igalia com>
Date:   Sat May 28 14:31:01 2022 -0500

    WebExtensions: Assign a GUID to each extension view's context

 src/webextension/api/alarms.c                 | 8 +++++++-
 src/webextension/api/alarms.h                 | 1 +
 src/webextension/api/notifications.c          | 4 +++-
 src/webextension/api/notifications.h          | 1 +
 src/webextension/api/pageaction.c             | 8 +++++++-
 src/webextension/api/pageaction.h             | 1 +
 src/webextension/api/runtime.c                | 6 +++++-
 src/webextension/api/runtime.h                | 1 +
 src/webextension/api/storage.c                | 7 ++++++-
 src/webextension/api/storage.h                | 1 +
 src/webextension/api/tabs.c                   | 9 ++++++++-
 src/webextension/api/tabs.h                   | 7 ++++---
 src/webextension/ephy-web-extension-manager.c | 7 ++++++-
 src/webextension/ephy-web-extension.h         | 2 ++
 14 files changed, 53 insertions(+), 10 deletions(-)
---
diff --git a/src/webextension/api/alarms.c b/src/webextension/api/alarms.c
index a0f6e54f7..6ae9a0948 100644
--- a/src/webextension/api/alarms.c
+++ b/src/webextension/api/alarms.c
@@ -166,6 +166,7 @@ static char *
 alarms_handler_create (EphyWebExtension  *self,
                        char              *name,
                        JSCValue          *args,
+                       const char        *context_guid,
                        GError           **error)
 {
   g_autoptr (JSCValue) alarm_name = NULL;
@@ -223,6 +224,7 @@ static char *
 alarms_handler_clear (EphyWebExtension  *self,
                       char              *name,
                       JSCValue          *args,
+                      const char        *context_guid,
                       GError           **error)
 {
   GHashTable *alarms = get_alarms (self);
@@ -244,6 +246,7 @@ static char *
 alarms_handler_clear_all (EphyWebExtension  *self,
                           char              *name,
                           JSCValue          *args,
+                          const char        *context_guid,
                           GError           **error)
 {
   GHashTable *alarms = get_alarms (self);
@@ -259,6 +262,7 @@ static char *
 alarms_handler_get (EphyWebExtension  *self,
                     char              *name,
                     JSCValue          *args,
+                    const char        *context_guid,
                     GError           **error)
 {
   GHashTable *alarms = get_alarms (self);
@@ -279,6 +283,7 @@ static char *
 alarms_handler_get_all (EphyWebExtension  *self,
                         char              *name,
                         JSCValue          *args,
+                        const char        *context_guid,
                         GError           **error)
 {
   GHashTable *alarms = get_alarms (self);
@@ -306,6 +311,7 @@ void
 ephy_web_extension_api_alarms_handler (EphyWebExtension *self,
                                        char             *name,
                                        JSCValue         *args,
+                                       const char       *context_guid,
                                        GTask            *task)
 {
   g_autoptr (GError) error = NULL;
@@ -322,7 +328,7 @@ ephy_web_extension_api_alarms_handler (EphyWebExtension *self,
     char *ret;
 
     if (g_strcmp0 (handler.name, name) == 0) {
-      ret = handler.execute (self, name, args, &error);
+      ret = handler.execute (self, name, args, context_guid, &error);
 
       if (error)
         g_task_return_error (task, g_steal_pointer (&error));
diff --git a/src/webextension/api/alarms.h b/src/webextension/api/alarms.h
index 9c8d03c19..32936ebb1 100644
--- a/src/webextension/api/alarms.h
+++ b/src/webextension/api/alarms.h
@@ -30,6 +30,7 @@ G_BEGIN_DECLS
 void ephy_web_extension_api_alarms_handler (EphyWebExtension *self,
                                             char            *name,
                                             JSCValue        *value,
+                                            const char      *context_guid,
                                             GTask           *task);
 
 G_END_DECLS
diff --git a/src/webextension/api/notifications.c b/src/webextension/api/notifications.c
index 43b9e196b..0cf5b7ac7 100644
--- a/src/webextension/api/notifications.c
+++ b/src/webextension/api/notifications.c
@@ -29,6 +29,7 @@ static char *
 notifications_handler_create (EphyWebExtension  *self,
                               char              *name,
                               JSCValue          *args,
+                              const char        *context_guid,
                               GError           **error)
 {
   g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
@@ -63,6 +64,7 @@ void
 ephy_web_extension_api_notifications_handler (EphyWebExtension *self,
                                               char             *name,
                                               JSCValue         *args,
+                                              const char       *context_guid,
                                               GTask            *task)
 {
   g_autoptr (GError) error = NULL;
@@ -73,7 +75,7 @@ ephy_web_extension_api_notifications_handler (EphyWebExtension *self,
     char *ret;
 
     if (g_strcmp0 (handler.name, name) == 0) {
-      ret = handler.execute (self, name, args, &error);
+      ret = handler.execute (self, name, args, context_guid, &error);
 
       if (error)
         g_task_return_error (task, g_steal_pointer (&error));
diff --git a/src/webextension/api/notifications.h b/src/webextension/api/notifications.h
index eb3a455a6..80a31382a 100644
--- a/src/webextension/api/notifications.h
+++ b/src/webextension/api/notifications.h
@@ -28,6 +28,7 @@ G_BEGIN_DECLS
 void ephy_web_extension_api_notifications_handler (EphyWebExtension *self,
                                                     char            *name,
                                                     JSCValue        *args,
+                                                    const char      *context_guid,
                                                     GTask           *task);
 
 G_END_DECLS
diff --git a/src/webextension/api/pageaction.c b/src/webextension/api/pageaction.c
index 4840a77b5..f93d54a6c 100644
--- a/src/webextension/api/pageaction.c
+++ b/src/webextension/api/pageaction.c
@@ -57,6 +57,7 @@ static char *
 pageaction_handler_seticon (EphyWebExtension  *self,
                             char              *name,
                             JSCValue          *args,
+                            const char        *context_guid,
                             GError           **error)
 {
   GtkWidget *action;
@@ -82,6 +83,7 @@ static char *
 pageaction_handler_settitle (EphyWebExtension  *self,
                              char              *name,
                              JSCValue          *args,
+                             const char        *context_guid,
                              GError           **error)
 {
   GtkWidget *action;
@@ -104,6 +106,7 @@ static char *
 pageaction_handler_gettitle (EphyWebExtension  *self,
                              char              *name,
                              JSCValue          *args,
+                             const char        *context_guid,
                              GError           **error)
 {
   g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
@@ -125,6 +128,7 @@ static char *
 pageaction_handler_show (EphyWebExtension  *self,
                          char              *name,
                          JSCValue          *args,
+                         const char        *context_guid,
                          GError           **error)
 {
   g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
@@ -145,6 +149,7 @@ static char *
 pageaction_handler_hide (EphyWebExtension  *self,
                          char              *name,
                          JSCValue          *args,
+                         const char        *context_guid,
                          GError           **error)
 {
   g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
@@ -173,6 +178,7 @@ void
 ephy_web_extension_api_pageaction_handler (EphyWebExtension *self,
                                            char             *name,
                                            JSCValue         *args,
+                                           const char       *context_guid,
                                            GTask            *task)
 {
   g_autoptr (GError) error = NULL;
@@ -183,7 +189,7 @@ ephy_web_extension_api_pageaction_handler (EphyWebExtension *self,
     char *ret;
 
     if (g_strcmp0 (handler.name, name) == 0) {
-      ret = handler.execute (self, name, args, &error);
+      ret = handler.execute (self, name, args, context_guid, &error);
 
       if (error)
         g_task_return_error (task, g_steal_pointer (&error));
diff --git a/src/webextension/api/pageaction.h b/src/webextension/api/pageaction.h
index 293bf7825..e1531db28 100644
--- a/src/webextension/api/pageaction.h
+++ b/src/webextension/api/pageaction.h
@@ -28,6 +28,7 @@ G_BEGIN_DECLS
 void ephy_web_extension_api_pageaction_handler (EphyWebExtension *self,
                                                  char            *name,
                                                  JSCValue        *args,
+                                                 const char      *context_guid,
                                                  GTask           *task);
 
 G_END_DECLS
diff --git a/src/webextension/api/runtime.c b/src/webextension/api/runtime.c
index 85e2898d9..617ae59fb 100644
--- a/src/webextension/api/runtime.c
+++ b/src/webextension/api/runtime.c
@@ -31,6 +31,7 @@ static char *
 runtime_handler_get_browser_info (EphyWebExtension  *self,
                                   char              *name,
                                   JSCValue          *args,
+                                  const char        *context_guid,
                                   GError           **error)
 {
   g_autoptr (JsonBuilder) builder = json_builder_new ();
@@ -50,6 +51,7 @@ static char *
 runtime_handler_send_message (EphyWebExtension  *self,
                               char              *name,
                               JSCValue          *args,
+                              const char        *context_guid,
                               GError           **error)
 {
   EphyWebExtensionManager *manager = ephy_web_extension_manager_get_default ();
@@ -67,6 +69,7 @@ static char *
 runtime_handler_open_options_page (EphyWebExtension  *self,
                                    char              *name,
                                    JSCValue          *args,
+                                   const char        *context_guid,
                                    GError           **error)
 {
   const char *data = ephy_web_extension_get_option_ui_page (self);
@@ -99,6 +102,7 @@ void
 ephy_web_extension_api_runtime_handler (EphyWebExtension *self,
                                         char             *name,
                                         JSCValue         *args,
+                                        const char       *context_guid,
                                         GTask            *task)
 {
   g_autoptr (GError) error = NULL;
@@ -109,7 +113,7 @@ ephy_web_extension_api_runtime_handler (EphyWebExtension *self,
     char *ret;
 
     if (g_strcmp0 (handler.name, name) == 0) {
-      ret = handler.execute (self, name, args, &error);
+      ret = handler.execute (self, name, args, context_guid, &error);
 
       if (error)
         g_task_return_error (task, g_steal_pointer (&error));
diff --git a/src/webextension/api/runtime.h b/src/webextension/api/runtime.h
index ed90dfb77..3ee3aefa6 100644
--- a/src/webextension/api/runtime.h
+++ b/src/webextension/api/runtime.h
@@ -28,6 +28,7 @@ G_BEGIN_DECLS
 void ephy_web_extension_api_runtime_handler (EphyWebExtension *self,
                                               char            *name,
                                               JSCValue        *args,
+                                              const char      *context_guid,
                                               GTask           *task);
 
 G_END_DECLS
diff --git a/src/webextension/api/storage.c b/src/webextension/api/storage.c
index 54b4a8bb1..a70da76d1 100644
--- a/src/webextension/api/storage.c
+++ b/src/webextension/api/storage.c
@@ -61,6 +61,7 @@ static char *
 storage_handler_local_set (EphyWebExtension  *self,
                            char              *name,
                            JSCValue          *args,
+                           const char        *context_guid,
                            GError           **error)
 {
   JsonNode *local_storage = ephy_web_extension_get_local_storage (self);
@@ -89,6 +90,7 @@ static char *
 storage_handler_local_get (EphyWebExtension  *self,
                            char              *name,
                            JSCValue          *args,
+                           const char        *context_guid,
                            GError           **error)
 {
   JsonNode *local_storage = ephy_web_extension_get_local_storage (self);
@@ -148,6 +150,7 @@ static char *
 storage_handler_local_remove (EphyWebExtension  *self,
                               char              *name,
                               JSCValue          *args,
+                              const char        *context_guid,
                               GError           **error)
 {
   JsonNode *local_storage = ephy_web_extension_get_local_storage (self);
@@ -177,6 +180,7 @@ static char *
 storage_handler_local_clear (EphyWebExtension  *self,
                              char              *name,
                              JSCValue          *args,
+                             const char        *context_guid,
                              GError           **error)
 {
   ephy_web_extension_clear_local_storage (self);
@@ -195,6 +199,7 @@ void
 ephy_web_extension_api_storage_handler (EphyWebExtension *self,
                                         char             *name,
                                         JSCValue         *args,
+                                        const char       *context_guid,
                                         GTask            *task)
 {
   g_autoptr (GError) error = NULL;
@@ -212,7 +217,7 @@ ephy_web_extension_api_storage_handler (EphyWebExtension *self,
     char *ret;
 
     if (g_strcmp0 (handler.name, name) == 0) {
-      ret = handler.execute (self, name, args, &error);
+      ret = handler.execute (self, name, args, context_guid, &error);
 
       if (error)
         g_task_return_error (task, g_steal_pointer (&error));
diff --git a/src/webextension/api/storage.h b/src/webextension/api/storage.h
index a7bcbab0e..20bdfb64f 100644
--- a/src/webextension/api/storage.h
+++ b/src/webextension/api/storage.h
@@ -31,6 +31,7 @@ G_BEGIN_DECLS
 void ephy_web_extension_api_storage_handler (EphyWebExtension *self,
                                               char            *name,
                                               JSCValue        *value,
+                                              const char      *context_guid,
                                               GTask           *task);
 
 G_END_DECLS
diff --git a/src/webextension/api/tabs.c b/src/webextension/api/tabs.c
index be94623ef..8bcb46fe8 100644
--- a/src/webextension/api/tabs.c
+++ b/src/webextension/api/tabs.c
@@ -150,6 +150,7 @@ static char *
 tabs_handler_query (EphyWebExtension  *self,
                     char              *name,
                     JSCValue          *args,
+                    const char        *context_guid,
                     GError           **error)
 {
   g_autoptr (JsonBuilder) builder = json_builder_new ();
@@ -227,6 +228,7 @@ static char *
 tabs_handler_insert_css (EphyWebExtension  *self,
                          char              *name,
                          JSCValue          *args,
+                         const char        *context_guid,
                          GError           **error)
 {
   EphyShell *shell = ephy_shell_get_default ();
@@ -281,6 +283,7 @@ static char *
 tabs_handler_remove_css (EphyWebExtension  *self,
                          char              *name,
                          JSCValue          *args,
+                         const char        *context_guid,
                          GError           **error)
 {
   EphyShell *shell = ephy_shell_get_default ();
@@ -334,6 +337,7 @@ static char *
 tabs_handler_get (EphyWebExtension  *self,
                   char              *name,
                   JSCValue          *args,
+                  const char        *context_guid,
                   GError           **error)
 {
   EphyShell *shell = ephy_shell_get_default ();
@@ -366,6 +370,7 @@ static char *
 tabs_handler_execute_script (EphyWebExtension  *self,
                              char              *name,
                              JSCValue          *args,
+                             const char        *context_guid,
                              GError           **error)
 {
   g_autoptr (JSCValue) code_value = NULL;
@@ -429,6 +434,7 @@ static char *
 tabs_handler_send_message (EphyWebExtension  *self,
                            char              *name,
                            JSCValue          *args,
+                           const char        *context_guid,
                            GError           **error)
 {
   g_autoptr (JSCValue) tab_id_value = NULL;
@@ -486,6 +492,7 @@ void
 ephy_web_extension_api_tabs_handler (EphyWebExtension *self,
                                      char             *name,
                                      JSCValue         *args,
+                                     const char       *context_guid,
                                      GTask            *task)
 {
   g_autoptr (GError) error = NULL;
@@ -496,7 +503,7 @@ ephy_web_extension_api_tabs_handler (EphyWebExtension *self,
     char *ret;
 
     if (g_strcmp0 (handler.name, name) == 0) {
-      ret = handler.execute (self, name, args, &error);
+      ret = handler.execute (self, name, args, context_guid, &error);
 
       if (error)
         g_task_return_error (task, g_steal_pointer (&error));
diff --git a/src/webextension/api/tabs.h b/src/webextension/api/tabs.h
index d9a85049f..50f9c2e46 100644
--- a/src/webextension/api/tabs.h
+++ b/src/webextension/api/tabs.h
@@ -28,8 +28,9 @@
 G_BEGIN_DECLS
 
 void ephy_web_extension_api_tabs_handler (EphyWebExtension *self,
-                                           char            *name,
-                                           JSCValue        *value,
-                                           GTask           *task);
+                                          char             *name,
+                                          JSCValue         *value,
+                                          const char       *context_guid,
+                                          GTask            *task);
 
 G_END_DECLS
diff --git a/src/webextension/ephy-web-extension-manager.c b/src/webextension/ephy-web-extension-manager.c
index 1dde9d88f..831f67d83 100644
--- a/src/webextension/ephy-web-extension-manager.c
+++ b/src/webextension/ephy-web-extension-manager.c
@@ -511,6 +511,7 @@ ephy_web_extension_handle_user_message (WebKitWebContext  *context,
   g_autoptr (JSCValue) args = NULL;
   const char *name = webkit_user_message_get_name (message);
   g_auto (GStrv) split = NULL;
+  const char *context_guid = g_object_get_data (G_OBJECT (context), "guid");
 
   js_context = jsc_context_new ();
   args = jsc_value_new_from_json (js_context, g_variant_get_string (webkit_user_message_get_parameters 
(message), NULL));
@@ -531,7 +532,7 @@ ephy_web_extension_handle_user_message (WebKitWebContext  *context,
       GTask *task = g_task_new (web_extension, NULL, 
(GAsyncReadyCallback)on_web_extension_api_handler_finish, NULL);
       g_task_set_task_data (task, api_handler_data_new (message, args), 
(GDestroyNotify)api_handler_data_free);
 
-      handler.execute (web_extension, split[1], args, task);
+      handler.execute (web_extension, split[1], args, context_guid, task);
       return TRUE;
     }
   }
@@ -734,6 +735,10 @@ create_web_extensions_webview (EphyWebExtension *web_extension)
   ucm = webkit_user_content_manager_new ();
 
   web_context = webkit_web_context_new ();
+
+  /* Assign a temporary GUID that is used to distinguish WebContexts on received messages. */
+  g_object_set_data_full (G_OBJECT (web_context), "guid", g_dbus_generate_guid (), g_free);
+
   webkit_web_context_register_uri_scheme (web_context, "ephy-webextension", web_extension_cb, web_extension, 
NULL);
   webkit_security_manager_register_uri_scheme_as_secure (webkit_web_context_get_security_manager 
(web_context),
                                                          "ephy-webextension");
diff --git a/src/webextension/ephy-web-extension.h b/src/webextension/ephy-web-extension.h
index 8d57900bc..3fa446d9a 100644
--- a/src/webextension/ephy-web-extension.h
+++ b/src/webextension/ephy-web-extension.h
@@ -38,11 +38,13 @@ G_DECLARE_FINAL_TYPE (EphyWebExtension, ephy_web_extension, EPHY, WEB_EXTENSION,
 typedef void (*executeTaskHandler)(EphyWebExtension *web_extension,
                                    char             *name,
                                    JSCValue         *args,
+                                   const char       *context_guid,
                                    GTask            *task);
 
 typedef char *(*executeHandler)(EphyWebExtension  *web_extension,
                                 char              *name,
                                 JSCValue          *args,
+                                const char        *context_guid,
                                 GError           **error);
 
 


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