aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile11
-rw-r--r--background.html1
-rw-r--r--js/vapi-background.js108
-rw-r--r--js/vapi-messaging.js144
4 files changed, 152 insertions, 112 deletions
diff --git a/Makefile b/Makefile
index f9c7b77..754f0b3 100644
--- a/Makefile
+++ b/Makefile
@@ -119,11 +119,14 @@ JS2 := js/httpsb.js js/i18n.js js/liquid-dict.js js/logger.js \
# vapi-popup.js is apparently not needed on UXP
# TODO: investigate wether or not it can be removed
JS3 := js/tab.js js/traffic.js js/udom.js js/uritools.js js/user-rules.js \
- js/usersettings.js js/utils.js js/vapi-background.js js/vapi-client.js \
- js/vapi-common.js js/vapi-popup.js js/vapi-tabs.js js/vapi-window.js \
- js/xal.js js/vapi-core.js js/vapi-browser.js js/vapi-storage.js
+ js/usersettings.js js/utils.js js/xal.js
-JS := $(JS1) $(JS2) $(JS3)
+JS4 := js/vapi-client.js js/vapi-common.js js/vapi-background.js \
+ js/vapi-popup.js js/vapi-tabs.js js/vapi-window.js \
+ js/vapi-core.js js/vapi-browser.js js/vapi-storage.js \
+ js/vapi-messaging.js
+
+JS := $(JS1) $(JS2) $(JS3) $(JS4)
all: eMatrix.xpi
diff --git a/background.html b/background.html
index 9aac3a5..bab0678 100644
--- a/background.html
+++ b/background.html
@@ -34,6 +34,7 @@
<script src="js/vapi-core.js"></script>
<script src="js/vapi-common.js"></script>
<script src="js/vapi-window.js"></script>
+<script src="js/vapi-messaging.js"></script>
<script src="js/vapi-background.js"></script>
<script src="js/vapi-tabs.js"></script>
<script src="js/vapi-browser.js"></script>
diff --git a/js/vapi-background.js b/js/vapi-background.js
index 14544cb..6c874f3 100644
--- a/js/vapi-background.js
+++ b/js/vapi-background.js
@@ -69,114 +69,6 @@
}
};
- // Internal message passing mechanism
- 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}));
- };
-
let CallbackWrapper = function (messageManager, channelName,
listenerId, requestId) {
// This allows to avoid creating a closure for every single
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}));
+ };
+})();