[epiphany/pgriffis/web-extension-storage: 1/3] WebExtensions: Abstract event listeners into a class
- From: Patrick Griffis <pgriffis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/pgriffis/web-extension-storage: 1/3] WebExtensions: Abstract event listeners into a class
- Date: Tue, 24 May 2022 03:59:54 +0000 (UTC)
commit f13e7332b9d0edabf904302cc944a4d75edda303
Author: Patrick Griffis <pgriffis igalia com>
Date: Mon May 23 20:44:11 2022 -0500
WebExtensions: Abstract event listeners into a class
.../resources/js/webextensions-common.js | 47 +++++++++++++--------
.../resources/js/webextensions.js | 48 ++++------------------
src/webextension/api/runtime.c | 2 +-
src/webextension/api/tabs.c | 2 +-
src/webextension/ephy-web-extension-manager.c | 4 +-
5 files changed, 40 insertions(+), 63 deletions(-)
---
diff --git a/embed/web-process-extension/resources/js/webextensions-common.js
b/embed/web-process-extension/resources/js/webextensions-common.js
index d3f24cc98..3cab4497c 100644
--- a/embed/web-process-extension/resources/js/webextensions-common.js
+++ b/embed/web-process-extension/resources/js/webextensions-common.js
@@ -7,8 +7,28 @@ window.browser = {};
const promises = [];
let last_promise = 0;
-let runtime_onmessage_listeners = [];
-let runtime_onconnect_listeners = [];
+class EphyEventListener {
+ constructor () {
+ this._listeners = [];
+ }
+
+ addListener (cb) {
+ this._listeners.push({callback: cb});
+ }
+
+ removeListener (cb) {
+ this._listeners = this._listeners.filter(l => l.callback !== cb);
+ }
+
+ hasListener (cb) {
+ return !!this._listeners.find(l => l.callback === cb);
+ }
+
+ _emit (data) {
+ for (const listener of this._listeners)
+ listener.callback (data);
+ }
+}
const ephy_message = function (fn, args, cb) {
const promise = new Promise (function (resolve, reject) {
@@ -18,25 +38,16 @@ const ephy_message = function (fn, args, cb) {
return promise;
};
-const runtimeSendMessage = function(x) {
- for (const listener of runtime_onmessage_listeners)
- listener.callback(x);
-};
-
-const runtimeOnConnect = function(x) {
- for (const listener of runtime_onconnect_listeners)
- listener.callback(x);
-};
-
window.browser.runtime = {
getURL: function (args, cb) { return window.browser.extension.getURL(args, cb); },
getManifest: function (args, cb) { return '[]'; },
- onMessage: {
- addListener: function (cb) {
- runtime_onmessage_listeners.push({callback: cb});
- },
- removeListener: function (cb) {
- runtime_onmessage_listeners = runtime_onmessage_listeners.filter(l => l.callback !== cb);
+ onMessage: new EphyEventListener (),
+ onConnect: new EphyEventListener (),
+ sendMessage: function (args, cb) {
+ return ephy_message ('runtime.sendMessage', args, cb);
+ },
+};
+
},
hasListener: function (cb) {
return !!runtime_onmessage_listeners.find(l => l.callback === cb);
diff --git a/embed/web-process-extension/resources/js/webextensions.js
b/embed/web-process-extension/resources/js/webextensions.js
index 7fafff903..b68c8c189 100644
--- a/embed/web-process-extension/resources/js/webextensions.js
+++ b/embed/web-process-extension/resources/js/webextensions.js
@@ -1,29 +1,7 @@
'use strict';
/* exported pageActionOnClicked, browserActionClicked, browserActionClicked, tabsOnUpdated */
-/* global ephy_message */
-
-const tabs_listeners = [];
-const page_listeners = [];
-const browser_listeners = [];
-const runtime_listeners = [];
-const runtime_onmessageexternal_listeners = [];
-const windows_onremoved_listeners = [];
-
-const pageActionOnClicked = function(x) {
- for (const listener of page_listeners)
- listener.callback(x);
-};
-
-const browserActionClicked = function(x) {
- for (const listener of browser_listeners)
- listener.callback(x);
-};
-
-const tabsOnUpdated = function(x) {
- for (const listener of tabs_listeners)
- listener.callback(x);
-};
+/* global ephy_message EphyEventListener */
// Browser async API
window.browser.alarms = {
@@ -31,9 +9,7 @@ window.browser.alarms = {
};
window.browser.windows = {
- onRemoved: {
- addListener: function (cb) { windows_onremoved_listeners.push({callback: cb}); }
- }
+ onRemoved: new EphyEventListener (),
};
window.browser.tabs = {
@@ -43,9 +19,7 @@ window.browser.tabs = {
get: function (args, cb) { return ephy_message ('tabs.get', args, cb); },
insertCSS: function (...args) { return ephy_message ('tabs.insertCSS', args, null); },
removeCSS: function (...args) { return ephy_message ('tabs.removeCSS', args, null); },
- onUpdated: {
- addListener: function (cb) { tabs_listeners.push({callback: cb}); }
- },
+ onUpdated: new EphyEventListener (),
sendMessage: function (...args) { return ephy_message ('tabs.sendMessage', args, null); },
TAB_ID_NONE: -1,
};
@@ -59,12 +33,8 @@ window.browser.runtime.getBrowserInfo = function (args, cb) { return ephy_messag
window.browser.runtime.connectNative = function (args, cb) { return ephy_message ('runtime.connectNative',
args, cb); };
window.browser.runtime.openOptionsPage = function (args, cb) { return ephy_message
('runtime.openOptionsPage', args, cb); };
window.browser.runtime.setUninstallURL = function (args, cb) { return ephy_message
('runtime.setUninstallURL', args, cb); };
-window.browser.runtime.onInstalled = {
- addListener: function (cb) { runtime_listeners.push({callback: cb}); }
-};
-window.browser.runtime.onMessageExternal = {
- addListener: function (cb) { runtime_onmessageexternal_listeners.push({callback: cb}); }
-};
+window.browser.runtime.onInstalled = new EphyEventListener ();
+window.browser.runtime.onMessageExternal = new EphyEventListener ();
window.browser.pageAction = {
setIcon: function (args, cb) { return ephy_message ('pageAction.setIcon', args, cb); },
@@ -72,15 +42,11 @@ window.browser.pageAction = {
getTitle: function (args, cb) { return ephy_message ('pageAction.getTitle', args, cb); },
show: function (args, cb) { return ephy_message ('pageAction.show', args, cb); },
hide: function (args, cb) { return ephy_message ('pageAction.hide', args, cb); },
- onClicked: {
- addListener: function (cb) { page_listeners.push({callback: cb}); }
- }
+ onClicked: new EphyEventListener (),
};
window.browser.browserAction = {
- onClicked: {
- addListener: function (cb) { browser_listeners.push({callback: cb}); }
- }
+ onClicked: new EphyEventListener (),
};
window.browser.windows = {
diff --git a/src/webextension/api/runtime.c b/src/webextension/api/runtime.c
index 156390461..303edbade 100644
--- a/src/webextension/api/runtime.c
+++ b/src/webextension/api/runtime.c
@@ -55,7 +55,7 @@ runtime_handler_send_message (EphyWebExtension *self,
WebKitWebView *view = WEBKIT_WEB_VIEW (ephy_web_extension_manager_get_background_web_view (manager, self));
g_autofree char *script = NULL;
- script = g_strdup_printf ("runtimeSendMessage(%s);", jsc_value_to_json (args, 2));
+ script = g_strdup_printf ("window.browser.runtime.onMessage._emit(%s);", jsc_value_to_json (args, 2));
webkit_web_view_run_javascript (view, script, NULL, NULL, NULL);
return NULL;
diff --git a/src/webextension/api/tabs.c b/src/webextension/api/tabs.c
index aa69e41e8..47e0fb1c9 100644
--- a/src/webextension/api/tabs.c
+++ b/src/webextension/api/tabs.c
@@ -420,7 +420,7 @@ tabs_handler_send_message (EphyWebExtension *self,
return NULL;
serialized_message = jsc_value_to_json (message_value, 0);
- code = g_strdup_printf ("runtimeSendMessage(JSON.parse('%s'));", serialized_message);
+ code = g_strdup_printf ("window.browser.runtime.onMessage._emit(JSON.parse('%s'));", serialized_message);
target_web_view = get_web_view_for_tab_id (shell, jsc_value_to_int32 (tab_id_value), NULL);
diff --git a/src/webextension/ephy-web-extension-manager.c b/src/webextension/ephy-web-extension-manager.c
index d01cbe228..65f3a75bd 100644
--- a/src/webextension/ephy-web-extension-manager.c
+++ b/src/webextension/ephy-web-extension-manager.c
@@ -405,7 +405,7 @@ page_action_clicked (GtkWidget *event_box,
json = json_to_string (root, FALSE);
- script = g_strdup_printf ("pageActionOnClicked(%s);", json);
+ script = g_strdup_printf ("window.browser.pageAction.onClicked._emit(%s);", json);
webkit_web_view_run_javascript (web_view,
script,
NULL,
@@ -787,7 +787,7 @@ on_browser_action_clicked (GtkWidget *event_box,
g_autofree char *script = NULL;
WebKitWebView *web_view = WEBKIT_WEB_VIEW (ephy_web_extension_manager_get_background_web_view (self,
web_extension));
- script = g_strdup_printf ("browserActionClicked();");
+ script = g_strdup_printf ("window.browser.browserAction.onClicked._emit();");
webkit_web_view_run_javascript (web_view,
script,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]