From d9858f22244f58eb2a305aa2e42439c77acdb861 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sun, 23 Jun 2019 02:34:09 +0200 Subject: Make messaging its own file Also reorganize the Makefile a bit. --- js/vapi-messaging.js | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 js/vapi-messaging.js (limited to 'js/vapi-messaging.js') diff --git a/js/vapi-messaging.js b/js/vapi-messaging.js new file mode 100644 index 0000000..abed913 --- /dev/null +++ b/js/vapi-messaging.js @@ -0,0 +1,144 @@ +/******************************************************************************* + + ηMatrix - a browser extension to black/white list requests. + Copyright (C) 2014-2019 The uMatrix/uBlock Origin authors + Copyright (C) 2019 Alessio Vanni + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see {http://www.gnu.org/licenses/}. + + Home: https://gitlab.com/vannilla/ematrix + uMatrix Home: https://github.com/gorhill/uMatrix +*/ + +/* global self, Components */ + +// For background page (tabs management) + +'use strict'; + +/******************************************************************************/ + +(function () { + const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); + + let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js + + vAPI.messaging = { + get globalMessageManager() { + return Cc['@mozilla.org/globalmessagemanager;1'] + .getService(Ci.nsIMessageListenerManager); + }, + frameScript: vAPI.getURL('frameScript.js'), + listeners: {}, + defaultHandler: null, + NOOPFUNC: function(){}, + UNHANDLED: 'vAPI.messaging.notHandled' + }; + + vAPI.messaging.listen = function (listenerName, callback) { + this.listeners[listenerName] = callback; + }; + + vAPI.messaging.onMessage = function ({target, data}) { + let messageManager = target.messageManager; + + if (!messageManager) { + // Message came from a popup, and its message manager is + // not usable. So instead we broadcast to the parent + // window. + messageManager = + vAPI.browser. + getOwnerWindow(target.webNavigation + .QueryInterface(Ci.nsIDocShell) + .chromeEventHandler).messageManager; + } + + let channelNameRaw = data.channelName; + let pos = channelNameRaw.indexOf('|'); + let channelName = channelNameRaw.slice(pos + 1); + + let callback = vAPI.messaging.NOOPFUNC; + if (data.requestId !== undefined) { + callback = CallbackWrapper.factory(messageManager, + channelName, + channelNameRaw.slice(0, pos), + data.requestId).callback; + } + + let sender = { + tab: { + id: vAPI.tabs.manager.tabIdFromTarget(target) + } + }; + + // Specific handler + let r = vAPI.messaging.UNHANDLED; + let listener = vAPI.messaging.listeners[channelName]; + + if (typeof listener === 'function') { + r = listener(data.msg, sender, callback); + } + if (r !== vAPI.messaging.UNHANDLED) { + return; + } + + // Default handler + r = vAPI.messaging.defaultHandler(data.msg, sender, callback); + if (r !== vAPI.messaging.UNHANDLED) { + return; + } + + console.error('eMatrix> messaging > unknown request: %o', data); + + // Unhandled: Need to callback anyways in case caller expected + // an answer, or else there is a memory leak on caller's side + callback(); + }; + + vAPI.messaging.setup = function (defaultHandler) { + // Already setup? + if (this.defaultHandler !== null) { + return; + } + + if (typeof defaultHandler !== 'function') { + defaultHandler = function () { + return vAPI.messaging.UNHANDLED; + }; + } + + this.defaultHandler = defaultHandler; + this.globalMessageManager.addMessageListener(location.host + + ':background', + this.onMessage); + this.globalMessageManager.loadFrameScript(this.frameScript, true); + + vAPI.addCleanUpTask(function () { + let gmm = vAPI.messaging.globalMessageManager; + + gmm.removeDelayedFrameScript(vAPI.messaging.frameScript); + gmm.removeMessageListener(location.host + ':background', + vAPI.messaging.onMessage); + }); + }; + + vAPI.messaging.broadcast = function (message) { + this.globalMessageManager + .broadcastAsyncMessage(location.host + ':broadcast', + JSON.stringify({ + broadcast: true, + msg: message})); + }; +})(); -- cgit v1.2.3 From 6458ec333511fe07101dd3afffbefed7b4fead07 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sun, 23 Jun 2019 02:43:40 +0200 Subject: Make CallbackWrapper a module In theory this way it can be used anywhere else if needed, but right now it's simply because it has to be placed somewhere and a module seems the best place for an object definition (constructor, etc.) --- js/vapi-messaging.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'js/vapi-messaging.js') diff --git a/js/vapi-messaging.js b/js/vapi-messaging.js index abed913..b00c054 100644 --- a/js/vapi-messaging.js +++ b/js/vapi-messaging.js @@ -32,6 +32,8 @@ (function () { const {classes: Cc, interfaces: Ci, utils: Cu} = Components; const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); + const {CallbackWrapper} = + Cu.import('chrome://ematrix/content/CallbackWrapper.jsm', null); let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js -- cgit v1.2.3 From 194b9f768b7e8ea57217fa6cf7b501727e65b662 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sun, 23 Jun 2019 13:36:44 +0200 Subject: Remove some comments While they are technically informative, the splitting makes things easier to follow already (somewhat) and there's not really a need to list each global variable (there aren't many anyway.) --- js/vapi-messaging.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'js/vapi-messaging.js') diff --git a/js/vapi-messaging.js b/js/vapi-messaging.js index b00c054..e7d1985 100644 --- a/js/vapi-messaging.js +++ b/js/vapi-messaging.js @@ -21,10 +21,6 @@ uMatrix Home: https://github.com/gorhill/uMatrix */ -/* global self, Components */ - -// For background page (tabs management) - 'use strict'; /******************************************************************************/ -- cgit v1.2.3 From 9d87f8f864b28d182af9659a3095433adb4b0126 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 17:33:06 +0200 Subject: Change how modules are imported I can't really find a reason why the returned value is preferred over the normal importing process. Additionally, there's a good chance importing Services.jsm can be done only once at the start of everything, instead of binding each object to a separate closure. --- js/vapi-messaging.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'js/vapi-messaging.js') diff --git a/js/vapi-messaging.js b/js/vapi-messaging.js index e7d1985..170e85d 100644 --- a/js/vapi-messaging.js +++ b/js/vapi-messaging.js @@ -27,9 +27,8 @@ (function () { const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); - const {CallbackWrapper} = - Cu.import('chrome://ematrix/content/CallbackWrapper.jsm', null); + Cu.import('resource://gre/modules/Services.jsm'); + Cu.import('chrome://ematrix/content/CallbackWrapper.jsm'); let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js -- cgit v1.2.3 From 51f5e899fff9e804d9c91e4fefdd57ea5a85e99c Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 18:06:54 +0200 Subject: Make components and services global Given that they are used a lot, at least in vAPI, let's just define/import them only once. --- js/vapi-messaging.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'js/vapi-messaging.js') diff --git a/js/vapi-messaging.js b/js/vapi-messaging.js index 170e85d..0df2362 100644 --- a/js/vapi-messaging.js +++ b/js/vapi-messaging.js @@ -26,8 +26,6 @@ /******************************************************************************/ (function () { - const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - Cu.import('resource://gre/modules/Services.jsm'); Cu.import('chrome://ematrix/content/CallbackWrapper.jsm'); let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js -- cgit v1.2.3 From f1f66637b814155c95a2bfddfdd9cd2973b86659 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 18:20:08 +0200 Subject: Revert "Make components and services global" This reverts commit 51f5e899fff9e804d9c91e4fefdd57ea5a85e99c. It seems to cause issues with the popup menu. --- js/vapi-messaging.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'js/vapi-messaging.js') diff --git a/js/vapi-messaging.js b/js/vapi-messaging.js index 0df2362..170e85d 100644 --- a/js/vapi-messaging.js +++ b/js/vapi-messaging.js @@ -26,6 +26,8 @@ /******************************************************************************/ (function () { + const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + Cu.import('resource://gre/modules/Services.jsm'); Cu.import('chrome://ematrix/content/CallbackWrapper.jsm'); let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js -- cgit v1.2.3 From 6908a6c89f9f44380faa1831ec13cff4042b671a Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 18:41:18 +0200 Subject: Make vAPI definitely global At least for background.html, it can be defined once at the start of vapi-core and then populated. --- js/vapi-messaging.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'js/vapi-messaging.js') diff --git a/js/vapi-messaging.js b/js/vapi-messaging.js index 170e85d..b320a27 100644 --- a/js/vapi-messaging.js +++ b/js/vapi-messaging.js @@ -29,8 +29,6 @@ const {classes: Cc, interfaces: Ci, utils: Cu} = Components; Cu.import('resource://gre/modules/Services.jsm'); Cu.import('chrome://ematrix/content/CallbackWrapper.jsm'); - - let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js vAPI.messaging = { get globalMessageManager() { -- cgit v1.2.3 From acd097e4733c106a15815c90e21c00d0c545e042 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Fri, 19 Jul 2019 16:00:08 +0200 Subject: Make components and Services.jsm global Once again, but this time it works. --- js/vapi-messaging.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'js/vapi-messaging.js') diff --git a/js/vapi-messaging.js b/js/vapi-messaging.js index b320a27..d0a3333 100644 --- a/js/vapi-messaging.js +++ b/js/vapi-messaging.js @@ -26,8 +26,6 @@ /******************************************************************************/ (function () { - const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - Cu.import('resource://gre/modules/Services.jsm'); Cu.import('chrome://ematrix/content/CallbackWrapper.jsm'); vAPI.messaging = { -- cgit v1.2.3