[epiphany/pgriffis/web-extension-gtask] WebExtensions: Improve compat of chrome API



commit b01f6d1c33ffbd481ec3c4672a0fd8a9542282a0
Author: Patrick Griffis <pgriffis igalia com>
Date:   Thu May 26 15:36:48 2022 -0500

    WebExtensions: Improve compat of chrome API
    
    We now use the last function as a callback.
    
    Closes #1788

 .../web-process-extension/ephy-webextension-api.c  |  6 +--
 .../resources/js/webextensions-common.js           | 35 ++++++++++------
 .../resources/js/webextensions.js                  | 46 +++++++++++-----------
 3 files changed, 47 insertions(+), 40 deletions(-)
---
diff --git a/embed/web-process-extension/ephy-webextension-api.c 
b/embed/web-process-extension/ephy-webextension-api.c
index 94b0ee46b..2448376f5 100644
--- a/embed/web-process-extension/ephy-webextension-api.c
+++ b/embed/web-process-extension/ephy-webextension-api.c
@@ -204,8 +204,6 @@ ephy_send_message (const char *function_name,
   WebKitUserMessage *message;
   char *args_json;
 
-  /* TODO: If function_args is list and last arg is callable, treat it as `chrome` API. */
-
   if (!jsc_value_is_function (reject_callback))
     return; /* Can't reject in this case. */
 
@@ -262,12 +260,12 @@ window_object_cleared_cb (WebKitScriptWorld         *world,
   g_bytes_unref (bytes);
   g_clear_object (&result);
 
+  ephy_webextension_install_common_apis (js_context, extension->guid, extension->translations);
+
   bytes = g_resources_lookup_data ("/org/gnome/epiphany-web-extension/js/webextensions.js", 
G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
   data = g_bytes_get_data (bytes, &data_size);
   result = jsc_context_evaluate_with_source_uri (js_context, data, data_size, 
"resource:///org/gnome/epiphany-web-extension/js/webextensions.js", 1);
   g_clear_object (&result);
-
-  ephy_webextension_install_common_apis (js_context, extension->guid, extension->translations);
 }
 
 static void
diff --git a/embed/web-process-extension/resources/js/webextensions-common.js 
b/embed/web-process-extension/resources/js/webextensions-common.js
index 5c2820d56..c8194fd26 100644
--- a/embed/web-process-extension/resources/js/webextensions-common.js
+++ b/embed/web-process-extension/resources/js/webextensions-common.js
@@ -28,33 +28,44 @@ class EphyEventListener {
     }
 }
 
-const ephy_message = function (fn, ...args) {
+const ephy_message = function (fn, args) {
+    let callback;
+
+    // This is a `chrome` callback based API.
+    if (args.length > 0 && typeof args[args.length - 1] === 'function')
+        callback = args.pop ();
+
     return new Promise (function (resolve, reject) {
-        ephy_send_message (fn, args, resolve, reject);
+        const resolve_wrapper = function (x) {
+            if (callback !== undefined)
+                callback (x);
+            resolve (x);
+        };
+        ephy_send_message (fn, args, resolve_wrapper, reject);
     });
 };
 
 window.browser.runtime = {
-    getURL: function (args, cb) { return window.browser.extension.getURL(args, cb); },
-    getManifest: function (args, cb) { return '[]'; },
+    getURL: function (args) { return window.browser.extension.getURL(args); },
+    getManifest: function () { return {}; },
     onMessage: new EphyEventListener (),
     onConnect: new EphyEventListener (),
-    sendMessage: function (args, cb) {
-        return ephy_message ('runtime.sendMessage', args, cb);
+    sendMessage: function (...args) {
+        return ephy_message ('runtime.sendMessage', args);
     },
 };
 
 
 window.browser.storage = {
     local: {
-        get: function (keys) {
-            return ephy_message ('storage.local.get', keys);
+        get: function (...args) {
+            return ephy_message ('storage.local.get', args);
         },
-        set: function (keys) {
-            return ephy_message ('storage.local.set', keys);
+        set: function (...args) {
+            return ephy_message ('storage.local.set', args);
         },
-        remove: function (keys) {
-            return ephy_message ('storage.local.remove', keys);
+        remove: function (...args) {
+            return ephy_message ('storage.local.remove', args);
         },
         clear: function () {
             return ephy_message ('storage.local.clear');
diff --git a/embed/web-process-extension/resources/js/webextensions.js 
b/embed/web-process-extension/resources/js/webextensions.js
index 33985f922..cc0dca1f8 100644
--- a/embed/web-process-extension/resources/js/webextensions.js
+++ b/embed/web-process-extension/resources/js/webextensions.js
@@ -5,7 +5,7 @@
 
 // Browser async API
 window.browser.alarms = {
-    clearAll: function (args, cb) { return ephy_message ('alarms.clearAll', args, cb); },
+    clearAll: function (...args) { return ephy_message ('alarms.clearAll', args); },
 };
 
 window.browser.windows = {
@@ -13,41 +13,42 @@ window.browser.windows = {
 };
 
 window.browser.tabs = {
-    create: function (args, cb) { return ephy_message ('tabs.create', args, cb); },
-    executeScript: function (...args) { return ephy_message ('tabs.executeScript', args, null); },
-    query: function (args, cb) { return ephy_message ('tabs.query', args, cb); },
-    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); },
+    create: function (...args) { return ephy_message ('tabs.create', args); },
+    executeScript: function (...args) { return ephy_message ('tabs.executeScript', args); },
+    query: function (...args) { return ephy_message ('tabs.query', args); },
+    get: function (...args) { return ephy_message ('tabs.get', args); },
+    insertCSS: function (...args) { return ephy_message ('tabs.insertCSS', args); },
+    removeCSS: function (...args) { return ephy_message ('tabs.removeCSS', args); },
     onUpdated: new EphyEventListener (),
-    sendMessage: function (...args) { return ephy_message ('tabs.sendMessage', args, null); },
+    sendMessage: function (...args) { return ephy_message ('tabs.sendMessage', args); },
     TAB_ID_NONE: -1,
 };
 
 window.browser.notifications = {
-    create: function (args, cb) { return ephy_message ('notifications.create', args, cb); },
+    create: function (...args) { return ephy_message ('notifications.create', args); },
 };
 
 // browser.extension is defined in ephy-webextension-common.c
 window.browser.extension.getViews = function (...args) { return []; };
+// Firefox returns null in private mode. So extensions sometimes handle this.
+window.browser.extension.getBackgroundPage = function () { return null; };
 
 // browser.runtime is defined in webextensions-common.js
-window.browser.runtime.getBrowserInfo = function (args, cb) { return ephy_message ('runtime.getBrowserInfo', 
args, cb); };
-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.getBrowserInfo = function (...args) { return ephy_message ('runtime.getBrowserInfo', 
args); };
+window.browser.runtime.connectNative = function (...args) { return ephy_message ('runtime.connectNative', 
args); };
+window.browser.runtime.openOptionsPage = function (...args) { return ephy_message 
('runtime.openOptionsPage', args); };
+window.browser.runtime.setUninstallURL = function (...args) { return ephy_message 
('runtime.setUninstallURL', args); };
 window.browser.runtime.onInstalled = new EphyEventListener ();
 window.browser.runtime.onMessageExternal = new EphyEventListener ();
-window.browser.runtime.sendNativeMessage = function (args) {
-  return new Promise ((resolve, reject) => { reject ('Unsupported API'); });
-};
+window.browser.runtime.sendNativeMessage = function (...args) { return ephy_message 
('runtime.sendNativeMessage', args); };
+
 
 window.browser.pageAction = {
-    setIcon: function (args, cb) { return ephy_message ('pageAction.setIcon', args, cb); },
-    setTitle: function (args, cb) { return ephy_message ('pageAction.setTitle', args, cb); },
-    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); },
+    setIcon: function (...args) { return ephy_message ('pageAction.setIcon', args); },
+    setTitle: function (...args) { return ephy_message ('pageAction.setTitle', args); },
+    getTitle: function (...args) { return ephy_message ('pageAction.getTitle', args); },
+    show: function (...args) { return ephy_message ('pageAction.show', args); },
+    hide: function (...args) { return ephy_message ('pageAction.hide', args); },
     onClicked: new EphyEventListener (),
 };
 
@@ -58,6 +59,3 @@ window.browser.browserAction = {
 window.browser.windows = {
   WINDOW_ID_CURRENT: -2, /* Matches Firefox, used in tabs.c. */
 };
-
-/* Firefox returns null in private mode. So extensions sometimes handle this. */
-window.browser.extension.getBackgroundPage = function () { return null; };


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