[epiphany/pgriffis/web-extension/content-script-send-message] WebExtensions: Implement message sending from Content Scripts
- From: Patrick Griffis <pgriffis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/pgriffis/web-extension/content-script-send-message] WebExtensions: Implement message sending from Content Scripts
- Date: Mon, 6 Jun 2022 01:54:54 +0000 (UTC)
commit f8aea8250fe667e272bf4e03612ac7c082186885
Author: Patrick Griffis <pgriffis igalia com>
Date: Sun Jun 5 19:24:00 2022 -0500
WebExtensions: Implement message sending from Content Scripts
This does a lot of refactoring but the gist is that our WebKit extensions
now send messages to the WebView instead of the WebContext which simplified
message handling and lets us easily handle content-script user messages.
This fixes runtime.sendMessage() as well as the entire storage API.
.../ephy-web-process-extension.c | 3 +-
.../web-process-extension/ephy-webextension-api.c | 3 +-
.../ephy-webextension-common.c | 38 +++---
.../ephy-webextension-common.h | 9 +-
src/webextension/api/alarms.c | 14 +--
src/webextension/api/alarms.h | 2 +-
src/webextension/api/notifications.c | 6 +-
src/webextension/api/notifications.h | 2 +-
src/webextension/api/pageaction.c | 14 +--
src/webextension/api/pageaction.h | 2 +-
src/webextension/api/runtime.c | 20 ++--
src/webextension/api/runtime.h | 2 +-
src/webextension/api/storage.c | 12 +-
src/webextension/api/storage.h | 2 +-
src/webextension/api/tabs.c | 76 ++++++------
src/webextension/api/tabs.h | 2 +-
src/webextension/ephy-web-extension-manager.c | 133 ++++++++++++++-------
src/webextension/ephy-web-extension-manager.h | 8 +-
src/webextension/ephy-web-extension.h | 4 +-
19 files changed, 191 insertions(+), 161 deletions(-)
---
diff --git a/embed/web-process-extension/ephy-web-process-extension.c
b/embed/web-process-extension/ephy-web-process-extension.c
index 99a388c1f..d0983555a 100644
--- a/embed/web-process-extension/ephy-web-process-extension.c
+++ b/embed/web-process-extension/ephy-web-process-extension.c
@@ -249,10 +249,9 @@ content_script_window_object_cleared_cb (WebKitScriptWorld *world,
result = jsc_context_evaluate_with_source_uri (js_context, data, data_size,
"resource:///org/gnome/epiphany-web-extension/js/webextensions-common.js", 1);
g_clear_object (&result);
- ephy_webextension_install_common_apis (extension->extension,
+ ephy_webextension_install_common_apis (page,
js_context,
guid,
- webkit_web_page_get_id (page),
translations);
}
diff --git a/embed/web-process-extension/ephy-webextension-api.c
b/embed/web-process-extension/ephy-webextension-api.c
index 2aa5e989a..6f097048b 100644
--- a/embed/web-process-extension/ephy-webextension-api.c
+++ b/embed/web-process-extension/ephy-webextension-api.c
@@ -201,10 +201,9 @@ window_object_cleared_cb (WebKitScriptWorld *world,
g_bytes_unref (bytes);
g_clear_object (&result);
- ephy_webextension_install_common_apis (extension->extension,
+ ephy_webextension_install_common_apis (page,
js_context,
extension->guid,
- webkit_web_page_get_id (page),
extension->translations);
js_browser = jsc_context_get_value (js_context, "browser");
diff --git a/embed/web-process-extension/ephy-webextension-common.c
b/embed/web-process-extension/ephy-webextension-common.c
index 9fd984cb5..6726d1dbe 100644
--- a/embed/web-process-extension/ephy-webextension-common.c
+++ b/embed/web-process-extension/ephy-webextension-common.c
@@ -23,8 +23,8 @@
#include <locale.h>
typedef struct {
- WebKitWebExtension *extension;
- guint64 page_id;
+ WebKitWebPage *page;
+ const char *guid;
} EphySendMessageData;
typedef struct {
@@ -42,7 +42,7 @@ ephy_callback_data_free (EphyCallbackData *data)
static EphyCallbackData *
ephy_callback_data_new (JSCValue *resolve_callback,
- JSCValue *reject_callback)
+ JSCValue *reject_callback)
{
EphyCallbackData *data = g_new (EphyCallbackData, 1);
data->resolve_callback = g_object_ref (resolve_callback);
@@ -51,16 +51,16 @@ ephy_callback_data_new (JSCValue *resolve_callback,
}
static void
-on_send_message_finish (WebKitWebExtension *extension,
- GAsyncResult *result,
- gpointer user_data)
+on_send_message_finish (WebKitWebPage *page,
+ GAsyncResult *result,
+ gpointer user_data)
{
EphyCallbackData *callback_data = user_data;
g_autoptr (GError) error = NULL;
g_autoptr (WebKitUserMessage) response = NULL;
g_autoptr (JSCValue) ret = NULL;
- response = webkit_web_extension_send_message_to_context_finish (extension, result, &error);
+ response = webkit_web_page_send_message_to_view_finish (page, result, &error);
if (error) {
ret = jsc_value_function_call (callback_data->reject_callback, G_TYPE_STRING, error->message,
G_TYPE_NONE);
@@ -92,8 +92,6 @@ ephy_send_message (const char *function_name,
gpointer user_data)
{
EphySendMessageData *send_message_data = user_data;
- WebKitWebExtension *extension = send_message_data->extension;
- guint64 page_id = send_message_data->page_id;
WebKitUserMessage *message;
g_autofree char *args_json = NULL;
@@ -107,11 +105,11 @@ ephy_send_message (const char *function_name,
args_json = jsc_value_to_json (function_args, 0);
message = webkit_user_message_new (function_name,
- g_variant_new ("(ts)", page_id, args_json));
+ g_variant_new ("(ss)", send_message_data->guid, args_json));
- webkit_web_extension_send_message_to_context (extension, message, NULL,
- (GAsyncReadyCallback)on_send_message_finish,
- ephy_callback_data_new (resolve_callback, reject_callback));
+ webkit_web_page_send_message_to_view (send_message_data->page, message, NULL,
+ (GAsyncReadyCallback)on_send_message_finish,
+ ephy_callback_data_new (resolve_callback, reject_callback));
}
static char *
@@ -175,11 +173,10 @@ js_exception_handler (JSCContext *context,
}
void
-ephy_webextension_install_common_apis (WebKitWebExtension *extension,
- JSCContext *js_context,
- const char *guid,
- guint64 page_id,
- JsonObject *translations)
+ephy_webextension_install_common_apis (WebKitWebPage *page,
+ JSCContext *js_context,
+ const char *guid,
+ JsonObject *translations)
{
g_autoptr (JSCValue) result = NULL;
g_autoptr (JSCValue) js_browser = NULL;
@@ -233,8 +230,8 @@ ephy_webextension_install_common_apis (WebKitWebExtension *extension,
/* global functions */
send_message_data = g_new (EphySendMessageData, 1);
- send_message_data->extension = extension;
- send_message_data->page_id = page_id;
+ send_message_data->page = page;
+ send_message_data->guid = guid;
js_function = jsc_value_new_function (js_context,
NULL,
G_CALLBACK (ephy_send_message),
@@ -247,5 +244,4 @@ ephy_webextension_install_common_apis (WebKitWebExtension *extension,
JSC_TYPE_VALUE);
jsc_context_set_value (js_context, "ephy_send_message", js_function);
g_clear_object (&js_function);
-
}
diff --git a/embed/web-process-extension/ephy-webextension-common.h
b/embed/web-process-extension/ephy-webextension-common.h
index 586bc43d2..220838356 100644
--- a/embed/web-process-extension/ephy-webextension-common.h
+++ b/embed/web-process-extension/ephy-webextension-common.h
@@ -27,10 +27,9 @@
G_BEGIN_DECLS
-void ephy_webextension_install_common_apis (WebKitWebExtension *extension,
- JSCContext *js_context,
- const char *guid,
- guint64 page_id,
- JsonObject *translations);
+void ephy_webextension_install_common_apis (WebKitWebPage *page,
+ JSCContext *js_context,
+ const char *guid,
+ JsonObject *translations);
G_END_DECLS
diff --git a/src/webextension/api/alarms.c b/src/webextension/api/alarms.c
index 8924c1154..b1722a334 100644
--- a/src/webextension/api/alarms.c
+++ b/src/webextension/api/alarms.c
@@ -166,7 +166,7 @@ static char *
alarms_handler_create (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
g_autoptr (JSCValue) alarm_name = NULL;
@@ -224,7 +224,7 @@ static char *
alarms_handler_clear (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
GHashTable *alarms = get_alarms (self);
@@ -246,7 +246,7 @@ static char *
alarms_handler_clear_all (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
GHashTable *alarms = get_alarms (self);
@@ -262,7 +262,7 @@ static char *
alarms_handler_get (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
GHashTable *alarms = get_alarms (self);
@@ -283,7 +283,7 @@ static char *
alarms_handler_get_all (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
GHashTable *alarms = get_alarms (self);
@@ -311,7 +311,7 @@ void
ephy_web_extension_api_alarms_handler (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task)
{
g_autoptr (GError) error = NULL;
@@ -328,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, extension_page_id, &error);
+ ret = handler.execute (self, name, args, web_view, &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 7409aa43c..2114c95a3 100644
--- a/src/webextension/api/alarms.h
+++ b/src/webextension/api/alarms.h
@@ -30,7 +30,7 @@ G_BEGIN_DECLS
void ephy_web_extension_api_alarms_handler (EphyWebExtension *self,
char *name,
JSCValue *value,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task);
G_END_DECLS
diff --git a/src/webextension/api/notifications.c b/src/webextension/api/notifications.c
index 00b55bfb0..09f4f2332 100644
--- a/src/webextension/api/notifications.c
+++ b/src/webextension/api/notifications.c
@@ -29,7 +29,7 @@ static char *
notifications_handler_create (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
@@ -64,7 +64,7 @@ void
ephy_web_extension_api_notifications_handler (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task)
{
g_autoptr (GError) error = NULL;
@@ -75,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, extension_page_id, &error);
+ ret = handler.execute (self, name, args, web_view, &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 0f456810e..c05e9a856 100644
--- a/src/webextension/api/notifications.h
+++ b/src/webextension/api/notifications.h
@@ -28,7 +28,7 @@ G_BEGIN_DECLS
void ephy_web_extension_api_notifications_handler (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task);
G_END_DECLS
diff --git a/src/webextension/api/pageaction.c b/src/webextension/api/pageaction.c
index 2fd5b335e..dc5d01e46 100644
--- a/src/webextension/api/pageaction.c
+++ b/src/webextension/api/pageaction.c
@@ -57,7 +57,7 @@ static char *
pageaction_handler_seticon (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
GtkWidget *action;
@@ -83,7 +83,7 @@ static char *
pageaction_handler_settitle (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
GtkWidget *action;
@@ -106,7 +106,7 @@ static char *
pageaction_handler_gettitle (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
@@ -128,7 +128,7 @@ static char *
pageaction_handler_show (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
@@ -149,7 +149,7 @@ static char *
pageaction_handler_hide (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
@@ -178,7 +178,7 @@ void
ephy_web_extension_api_pageaction_handler (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task)
{
g_autoptr (GError) error = NULL;
@@ -189,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, extension_page_id, &error);
+ ret = handler.execute (self, name, args, web_view, &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 1830339da..9dffb1d1c 100644
--- a/src/webextension/api/pageaction.h
+++ b/src/webextension/api/pageaction.h
@@ -28,7 +28,7 @@ G_BEGIN_DECLS
void ephy_web_extension_api_pageaction_handler (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task);
G_END_DECLS
diff --git a/src/webextension/api/runtime.c b/src/webextension/api/runtime.c
index 159d96864..19438c034 100644
--- a/src/webextension/api/runtime.c
+++ b/src/webextension/api/runtime.c
@@ -31,7 +31,7 @@ static char *
runtime_handler_get_browser_info (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
g_autoptr (JsonBuilder) builder = json_builder_new ();
@@ -60,12 +60,10 @@ is_empty_object (JSCValue *value)
static char *
create_sender_object (EphyWebExtension *web_extension,
- gint64 page_id)
+ WebKitWebView *web_view)
{
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)
@@ -78,7 +76,7 @@ static void
runtime_handler_send_message (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task)
{
EphyWebExtensionManager *manager = ephy_web_extension_manager_get_default ();
@@ -110,8 +108,8 @@ runtime_handler_send_message (EphyWebExtension *self,
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,
- create_sender_object (self,
extension_page_id),
+ web_view,
+ create_sender_object (self, web_view),
task);
return;
@@ -121,7 +119,7 @@ static char *
runtime_handler_open_options_page (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
const char *data = ephy_web_extension_get_option_ui_page (self);
@@ -157,7 +155,7 @@ void
ephy_web_extension_api_runtime_handler (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task)
{
g_autoptr (GError) error = NULL;
@@ -168,7 +166,7 @@ ephy_web_extension_api_runtime_handler (EphyWebExtension *self,
char *ret;
if (g_strcmp0 (handler.name, name) == 0) {
- ret = handler.execute (self, name, args, extension_page_id, &error);
+ ret = handler.execute (self, name, args, web_view, &error);
if (error)
g_task_return_error (task, g_steal_pointer (&error));
@@ -183,7 +181,7 @@ ephy_web_extension_api_runtime_handler (EphyWebExtension *self,
EphyWebExtensionAsyncApiHandler handler = runtime_async_handlers[idx];
if (g_strcmp0 (handler.name, name) == 0) {
- handler.execute (self, name, args, extension_page_id, task);
+ handler.execute (self, name, args, web_view, task);
return;
}
}
diff --git a/src/webextension/api/runtime.h b/src/webextension/api/runtime.h
index 7ab12e61b..ffce31286 100644
--- a/src/webextension/api/runtime.h
+++ b/src/webextension/api/runtime.h
@@ -28,7 +28,7 @@ G_BEGIN_DECLS
void ephy_web_extension_api_runtime_handler (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task);
G_END_DECLS
diff --git a/src/webextension/api/storage.c b/src/webextension/api/storage.c
index 3f43e87f1..04ead21fd 100644
--- a/src/webextension/api/storage.c
+++ b/src/webextension/api/storage.c
@@ -61,7 +61,7 @@ static char *
storage_handler_local_set (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
JsonNode *local_storage = ephy_web_extension_get_local_storage (self);
@@ -90,7 +90,7 @@ static char *
storage_handler_local_get (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
JsonNode *local_storage = ephy_web_extension_get_local_storage (self);
@@ -150,7 +150,7 @@ static char *
storage_handler_local_remove (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
JsonNode *local_storage = ephy_web_extension_get_local_storage (self);
@@ -180,7 +180,7 @@ static char *
storage_handler_local_clear (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
ephy_web_extension_clear_local_storage (self);
@@ -199,7 +199,7 @@ void
ephy_web_extension_api_storage_handler (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task)
{
g_autoptr (GError) error = NULL;
@@ -217,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, extension_page_id, &error);
+ ret = handler.execute (self, name, args, web_view, &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 3c8e10e8a..f905613c3 100644
--- a/src/webextension/api/storage.h
+++ b/src/webextension/api/storage.h
@@ -31,7 +31,7 @@ G_BEGIN_DECLS
void ephy_web_extension_api_storage_handler (EphyWebExtension *self,
char *name,
JSCValue *value,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task);
G_END_DECLS
diff --git a/src/webextension/api/tabs.c b/src/webextension/api/tabs.c
index 7d857e607..60438323a 100644
--- a/src/webextension/api/tabs.c
+++ b/src/webextension/api/tabs.c
@@ -196,7 +196,7 @@ static char *
tabs_handler_query (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
g_autoptr (JsonBuilder) builder = json_builder_new ();
@@ -274,7 +274,7 @@ static char *
tabs_handler_insert_css (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
EphyShell *shell = ephy_shell_get_default ();
@@ -329,7 +329,7 @@ static char *
tabs_handler_remove_css (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
EphyShell *shell = ephy_shell_get_default ();
@@ -383,7 +383,7 @@ static char *
tabs_handler_get (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
EphyShell *shell = ephy_shell_get_default ();
@@ -437,7 +437,7 @@ static void
tabs_handler_execute_script (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task)
{
g_autoptr (JSCValue) code_value = NULL;
@@ -500,7 +500,7 @@ static char *
tabs_handler_send_message (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
g_autoptr (JSCValue) tab_id_value = NULL;
@@ -585,13 +585,13 @@ static char *
tabs_handler_create (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
EphyShell *shell = ephy_shell_get_default ();
EphyEmbed *embed;
EphyWindow *parent_window;
- EphyWebView *web_view;
+ EphyWebView *new_web_view;
g_autoptr (JSCValue) create_properties = NULL;
g_autofree char *url = NULL;
EphyNewTabFlags new_tab_flags = 0;
@@ -615,7 +615,7 @@ tabs_handler_create (EphyWebExtension *self,
parent_window = get_window_by_id (shell, get_number_property (create_properties, "windowId"));
embed = ephy_shell_new_tab (shell, parent_window, NULL, new_tab_flags);
- web_view = ephy_embed_get_web_view (embed);
+ new_web_view = ephy_embed_get_web_view (embed);
if (url && get_boolean_property (create_properties, "openInReaderMode", FALSE)) {
char *reader_url = g_strconcat (EPHY_READER_SCHEME, ":", url, NULL);
@@ -624,13 +624,13 @@ tabs_handler_create (EphyWebExtension *self,
}
if (url)
- ephy_web_view_load_url (web_view, url);
+ ephy_web_view_load_url (new_web_view, url);
else
- ephy_web_view_load_new_tab_page (web_view);
+ ephy_web_view_load_new_tab_page (new_web_view);
builder = json_builder_new ();
- add_web_view_to_json (builder, parent_window, web_view,
- ephy_web_extension_has_tab_or_host_permission (self, web_view, TRUE));
+ add_web_view_to_json (builder, parent_window, new_web_view,
+ ephy_web_extension_has_tab_or_host_permission (self, new_web_view, TRUE));
root = json_builder_get_root (builder);
return json_to_string (root, FALSE);
}
@@ -639,7 +639,7 @@ static char *
tabs_handler_update (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
EphyShell *shell = ephy_shell_get_default ();
@@ -648,7 +648,7 @@ tabs_handler_update (EphyWebExtension *self,
g_autofree char *new_url = NULL;
g_autoptr (JsonBuilder) builder = NULL;
g_autoptr (JsonNode) root = NULL;
- WebKitWebView *web_view;
+ WebKitWebView *target_web_view;
EphyWindow *parent_window;
int tab_id = -1;
int muted;
@@ -669,13 +669,13 @@ tabs_handler_update (EphyWebExtension *self,
}
if (tab_id >= 0)
- web_view = get_web_view_for_tab_id (shell, tab_id, &parent_window);
+ target_web_view = get_web_view_for_tab_id (shell, tab_id, &parent_window);
else {
- web_view = WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell));
+ target_web_view = WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell));
parent_window = EPHY_WINDOW (gtk_application_get_active_window (GTK_APPLICATION (shell)));
}
- if (!web_view) {
+ if (!target_web_view) {
g_set_error (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "tabs.update(): Failed to
find tabId %d.", tab_id);
return NULL;
}
@@ -688,14 +688,14 @@ tabs_handler_update (EphyWebExtension *self,
muted = get_boolean_property (update_properties, "muted", VALUE_UNSET);
if (muted != VALUE_UNSET)
- webkit_web_view_set_is_muted (web_view, muted);
+ webkit_web_view_set_is_muted (target_web_view, muted);
if (new_url)
- webkit_web_view_load_uri (web_view, new_url);
+ webkit_web_view_load_uri (target_web_view, new_url);
builder = json_builder_new ();
- add_web_view_to_json (builder, parent_window, EPHY_WEB_VIEW (web_view),
- ephy_web_extension_has_tab_or_host_permission (self, EPHY_WEB_VIEW (web_view),
TRUE));
+ add_web_view_to_json (builder, parent_window, EPHY_WEB_VIEW (target_web_view),
+ ephy_web_extension_has_tab_or_host_permission (self, EPHY_WEB_VIEW
(target_web_view), TRUE));
root = json_builder_get_root (builder);
return json_to_string (root, FALSE);
}
@@ -720,7 +720,7 @@ static char *
tabs_handler_remove (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
EphyShell *shell = ephy_shell_get_default ();
@@ -756,13 +756,13 @@ static char *
tabs_handler_set_zoom (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
EphyShell *shell = ephy_shell_get_default ();
g_autoptr (JSCValue) zoom_level_value = NULL;
g_autoptr (JSCValue) tab_id_value = NULL;
- WebKitWebView *web_view;
+ WebKitWebView *target_web_view;
int tab_id = -1;
double zoom_level;
@@ -788,16 +788,16 @@ tabs_handler_set_zoom (EphyWebExtension *self,
}
if (tab_id >= 0)
- web_view = get_web_view_for_tab_id (shell, tab_id, NULL);
+ target_web_view = get_web_view_for_tab_id (shell, tab_id, NULL);
else
- web_view = WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell));
+ target_web_view = WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell));
- if (!web_view) {
+ if (!target_web_view) {
g_set_error (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "tabs.setZoom(): Failed
to find tabId %d.", tab_id);
return NULL;
}
- webkit_web_view_set_zoom_level (web_view, jsc_value_to_double (zoom_level_value));
+ webkit_web_view_set_zoom_level (target_web_view, jsc_value_to_double (zoom_level_value));
return NULL;
}
@@ -805,12 +805,12 @@ static char *
tabs_handler_get_zoom (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error)
{
EphyShell *shell = ephy_shell_get_default ();
g_autoptr (JSCValue) tab_id_value = NULL;
- WebKitWebView *web_view;
+ WebKitWebView *target_web_view;
int tab_id = -1;
tab_id_value = jsc_value_object_get_property_at_index (args, 0);
@@ -818,16 +818,16 @@ tabs_handler_get_zoom (EphyWebExtension *self,
tab_id = jsc_value_to_int32 (tab_id_value);
if (tab_id >= 0)
- web_view = get_web_view_for_tab_id (shell, tab_id, NULL);
+ target_web_view = get_web_view_for_tab_id (shell, tab_id, NULL);
else
- web_view = WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell));
+ target_web_view = WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell));
- if (!web_view) {
+ if (!target_web_view) {
g_set_error (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "tabs.getZoom(): Failed
to find tabId %d.", tab_id);
return NULL;
}
- return g_strdup_printf ("%f", webkit_web_view_get_zoom_level (web_view));
+ return g_strdup_printf ("%f", webkit_web_view_get_zoom_level (target_web_view));
}
static EphyWebExtensionSyncApiHandler tabs_sync_handlers[] = {
@@ -851,7 +851,7 @@ void
ephy_web_extension_api_tabs_handler (EphyWebExtension *self,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task)
{
g_autoptr (GError) error = NULL;
@@ -861,7 +861,7 @@ ephy_web_extension_api_tabs_handler (EphyWebExtension *self,
char *ret;
if (g_strcmp0 (handler.name, name) == 0) {
- ret = handler.execute (self, name, args, extension_page_id, &error);
+ ret = handler.execute (self, name, args, web_view, &error);
if (error)
g_task_return_error (task, g_steal_pointer (&error));
@@ -876,7 +876,7 @@ ephy_web_extension_api_tabs_handler (EphyWebExtension *self,
EphyWebExtensionAsyncApiHandler handler = tab_async_handlers[idx];
if (g_strcmp0 (handler.name, name) == 0) {
- handler.execute (self, name, args, extension_page_id, task);
+ handler.execute (self, name, args, web_view, task);
return;
}
}
diff --git a/src/webextension/api/tabs.h b/src/webextension/api/tabs.h
index 367857ace..da7ce1e62 100644
--- a/src/webextension/api/tabs.h
+++ b/src/webextension/api/tabs.h
@@ -30,7 +30,7 @@ G_BEGIN_DECLS
void ephy_web_extension_api_tabs_handler (EphyWebExtension *self,
char *name,
JSCValue *value,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task);
G_END_DECLS
diff --git a/src/webextension/ephy-web-extension-manager.c b/src/webextension/ephy-web-extension-manager.c
index 37d896e70..47571e784 100644
--- a/src/webextension/ephy-web-extension-manager.c
+++ b/src/webextension/ephy-web-extension-manager.c
@@ -509,19 +509,19 @@ on_web_extension_api_handler_finish (EphyWebExtension *web_extension,
}
static gboolean
-ephy_web_extension_handle_user_message (WebKitWebContext *context,
- WebKitUserMessage *message,
- gpointer user_data)
+extension_view_handle_user_message (WebKitWebView *web_view,
+ WebKitUserMessage *message,
+ gpointer user_data)
{
EphyWebExtension *web_extension = user_data;
g_autoptr (JSCContext) js_context = NULL;
g_autoptr (JSCValue) args = NULL;
const char *name = webkit_user_message_get_name (message);
g_auto (GStrv) split = NULL;
- guint64 page_id;
+ const char *guid;
const char *json_args;
- g_variant_get (webkit_user_message_get_parameters (message), "(t&s)", &page_id, &json_args);
+ g_variant_get (webkit_user_message_get_parameters (message), "(&s&s)", &guid, &json_args);
js_context = jsc_context_new ();
args = jsc_value_new_from_json (js_context, json_args);
@@ -550,7 +550,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, page_id, task);
+ handler.execute (web_extension, split[1], args, web_view, task);
return TRUE;
}
}
@@ -560,6 +560,66 @@ ephy_web_extension_handle_user_message (WebKitWebContext *context,
return TRUE;
}
+static gboolean
+content_scripts_handle_user_message (WebKitWebView *web_view,
+ WebKitUserMessage *message,
+ gpointer user_data)
+{
+ EphyWebExtension *web_extension = user_data;
+ g_autoptr (JSCContext) js_context = NULL;
+ g_autoptr (JSCValue) args = NULL;
+ const char *name = webkit_user_message_get_name (message);
+ g_auto (GStrv) split = NULL;
+ const char *json_args;
+ const char *extension_guid;
+ GTask *task;
+
+ g_variant_get (webkit_user_message_get_parameters (message), "(&s&s)", &extension_guid, &json_args);
+
+ /* Multiple extensions can send user-messages from the same web-view, so only the target one handles this.
*/
+ if (strcmp (extension_guid, ephy_web_extension_get_guid (web_extension)) != 0)
+ return FALSE;
+
+ js_context = jsc_context_new ();
+ args = jsc_value_new_from_json (js_context, json_args);
+
+ LOG ("%s(): Called for %s, function %s (%s)\n", __FUNCTION__, ephy_web_extension_get_name (web_extension),
name, json_args);
+
+ /* Private API for message replies handled by the manager. */
+ if (strcmp (name, "runtime._sendMessageReply") == 0) {
+ WebKitUserMessage *reply = webkit_user_message_new ("", g_variant_new_string (""));
+ handle_message_reply (web_extension, args);
+ webkit_user_message_send_reply (message, reply);
+ return TRUE;
+ }
+
+ split = g_strsplit (name, ".", 2);
+ if (g_strv_length (split) != 2) {
+ respond_with_error (message, "Invalid function name");
+ return TRUE;
+ }
+
+ /* Content Scripts are very limited in their API access compared to extension views so we handle them
individually. */
+ if (strcmp (split[0], "storage") == 0) {
+ 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);
+
+ ephy_web_extension_api_storage_handler (web_extension, split[1], args, web_view, task);
+ return TRUE;
+ }
+
+ if (strcmp (name, "runtime.sendMessage") == 0) {
+ 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);
+
+ ephy_web_extension_api_runtime_handler (web_extension, split[1], args, web_view, task);
+ return TRUE;
+ }
+
+ respond_with_error (message, "Permission Denied");
+ return TRUE;
+}
+
static void
add_content_scripts (EphyWebExtension *web_extension,
EphyWebView *web_view)
@@ -665,6 +725,10 @@ ephy_web_extension_manager_add_web_extension_to_webview (EphyWebExtensionManager
}
}
+ g_signal_connect (web_view, "user-message-received",
+ G_CALLBACK (content_scripts_handle_user_message),
+ web_extension);
+
webkit_web_view_send_message_to_page (WEBKIT_WEB_VIEW (web_view),
webkit_user_message_new ("WebExtension.Initialize",
g_variant_new_string (ephy_web_extension_get_guid (web_extension))),
NULL, NULL, NULL);
@@ -803,9 +867,7 @@ create_web_extensions_webview (EphyWebExtension *web_extension)
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");
-
g_signal_connect_object (web_context, "initialize-web-extensions", G_CALLBACK (init_web_extension_api),
web_extension, 0);
- g_signal_connect (web_context, "user-message-received", G_CALLBACK
(ephy_web_extension_handle_user_message), web_extension);
web_view = g_object_new (WEBKIT_TYPE_WEB_VIEW,
"web-context", web_context,
@@ -815,6 +877,7 @@ create_web_extensions_webview (EphyWebExtension *web_extension)
NULL);
webkit_web_view_set_cors_allowlist (WEBKIT_WEB_VIEW (web_view), ephy_web_extension_get_host_permissions
(web_extension));
+ g_signal_connect (web_view, "user-message-received", G_CALLBACK (extension_view_handle_user_message),
web_extension);
settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (web_view));
webkit_settings_set_enable_write_console_messages_to_stdout (settings, TRUE);
@@ -1034,6 +1097,8 @@ ephy_web_extension_manager_remove_web_extension_from_webview (EphyWebExtensionMa
if (lentry)
ephy_location_entry_page_action_clear (lentry);
+ g_signal_handlers_disconnect_by_func (web_view, content_scripts_handle_user_message, web_extension);
+
remove_content_scripts (web_extension, web_view);
remove_custom_css (web_extension, web_view);
}
@@ -1274,11 +1339,13 @@ on_extension_emit_ready (GObject *source,
pending_messages = g_hash_table_lookup (manager->pending_messages, tracker->web_extension);
pending_task = g_hash_table_lookup (pending_messages, tracker->message_guid);
- g_assert (pending_task);
- g_assert (g_hash_table_steal (pending_messages, tracker->message_guid));
- g_clear_pointer (&tracker->message_guid, g_free);
+ /* It is possible another view responded and removed the pending_task already. */
+ if (pending_task) {
+ g_assert (g_hash_table_steal (pending_messages, tracker->message_guid));
+ g_clear_pointer (&tracker->message_guid, g_free);
- g_task_return_pointer (pending_task, NULL, NULL);
+ g_task_return_pointer (pending_task, NULL, NULL);
+ }
}
g_free (tracker);
}
@@ -1289,7 +1356,7 @@ ephy_web_extension_manager_emit_in_extension_views_internal (EphyWebExtensionMan
EphyWebExtension *web_extension,
const char *name,
const char *message_json,
- gint64 page_id_exception,
+ WebKitWebView *web_view_exception,
const char *sender_json,
GTask *reply_task)
{
@@ -1318,7 +1385,7 @@ ephy_web_extension_manager_emit_in_extension_views_internal (EphyWebExtensionMan
script = g_strdup_printf ("window.browser.%s._emit(%s);", name, message_json);
if (background_view) {
- if ((gint64)webkit_web_view_get_page_id (background_view) != page_id_exception) {
+ if (web_view_exception != background_view) {
webkit_web_view_run_javascript (background_view,
script,
NULL,
@@ -1331,7 +1398,7 @@ ephy_web_extension_manager_emit_in_extension_views_internal (EphyWebExtensionMan
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_exception)
+ if (web_view_exception == popup_view)
continue;
webkit_web_view_run_javascript (popup_view,
@@ -1372,7 +1439,7 @@ ephy_web_extension_manager_emit_in_extension_views (EphyWebExtensionManager *sel
const char *name,
const char *json)
{
- ephy_web_extension_manager_emit_in_extension_views_internal (self, web_extension, name, json, -1, NULL,
NULL);
+ ephy_web_extension_manager_emit_in_extension_views_internal (self, web_extension, name, json, NULL, NULL,
NULL);
}
void
@@ -1380,10 +1447,9 @@ ephy_web_extension_manager_emit_in_extension_views_except_self (EphyWebExtension
EphyWebExtension *web_extension,
const char *name,
const char *json,
- gint64 extension_page_id)
+ WebKitWebView *own_webview)
{
- g_assert (extension_page_id > 0);
- ephy_web_extension_manager_emit_in_extension_views_internal (self, web_extension, name, json,
extension_page_id, NULL, NULL);
+ ephy_web_extension_manager_emit_in_extension_views_internal (self, web_extension, name, json, own_webview,
NULL, NULL);
}
void
@@ -1391,35 +1457,12 @@ ephy_web_extension_manager_emit_in_extension_views_with_reply (EphyWebExtensionM
EphyWebExtension *web_extension,
const char *name,
const char *json,
- gint64 extension_page_id,
+ WebKitWebView *own_web_view,
const char *sender_json,
GTask *reply_task)
{
g_assert (sender_json);
g_assert (reply_task);
- g_assert (extension_page_id > 0);
- 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;
+ g_assert (own_web_view);
+ ephy_web_extension_manager_emit_in_extension_views_internal (self, web_extension, name, json,
own_web_view, sender_json, reply_task);
}
diff --git a/src/webextension/ephy-web-extension-manager.h b/src/webextension/ephy-web-extension-manager.h
index aea6ec5a7..5593da6b1 100644
--- a/src/webextension/ephy-web-extension-manager.h
+++ b/src/webextension/ephy-web-extension-manager.h
@@ -79,19 +79,15 @@ void ephy_web_extension_manager_emit_in_extension_views_exce
EphyWebExtension
*web_extension,
const char
*name,
const char
*json,
- gint64
extension_page_id);
+ WebKitWebView
*own_web_view);
void ephy_web_extension_manager_emit_in_extension_views_with_reply
(EphyWebExtensionManager
*self,
EphyWebExtension
*web_extension,
const char
*name,
const char
*json,
- gint64
extension_page_id,
+ WebKitWebView
*own_web_view,
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
diff --git a/src/webextension/ephy-web-extension.h b/src/webextension/ephy-web-extension.h
index 40d2ba11f..59b9ec839 100644
--- a/src/webextension/ephy-web-extension.h
+++ b/src/webextension/ephy-web-extension.h
@@ -38,13 +38,13 @@ G_DECLARE_FINAL_TYPE (EphyWebExtension, ephy_web_extension, EPHY, WEB_EXTENSION,
typedef void (*executeTaskHandler)(EphyWebExtension *web_extension,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GTask *task);
typedef char *(*executeHandler)(EphyWebExtension *web_extension,
char *name,
JSCValue *args,
- gint64 extension_page_id,
+ WebKitWebView *web_view,
GError **error);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]