From 9bde0c09c312feebe5abc96240214f91aeca198a Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Sun, 23 Jun 2019 13:30:04 +0200 Subject: Split vAPI.net --- js/vapi-net.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 js/vapi-net.js (limited to 'js/vapi-net.js') diff --git a/js/vapi-net.js b/js/vapi-net.js new file mode 100644 index 0000000..5c6ccaf --- /dev/null +++ b/js/vapi-net.js @@ -0,0 +1,78 @@ +/******************************************************************************* + + η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.net = {}; + + vAPI.net.registerListeners = function () { + this.onBeforeRequest.types = this.onBeforeRequest.types + ? new Set(this.onBeforeRequest.types) + : null; + + this.onBeforeSendHeaders.types = this.onBeforeSendHeaders.types + ? new Set(this.onBeforeSendHeaders.types) + : null; + + let shouldLoadListenerMessageName = location.host + ':shouldLoad'; + let shouldLoadListener = function (e) { + let details = e.data; + let pendingReq = httpObserver.createPendingRequest(details.url); + pendingReq.rawType = details.rawType; + pendingReq.tabId = vAPI.tabs.manager.tabIdFromTarget(e.target); + }; + + // https://github.com/gorhill/uMatrix/issues/200 + // We need this only for Firefox 34 and less: the tab id is derived from + // the origin of the message. + if (!vAPI.modernFirefox) { + vAPI.messaging.globalMessageManager + .addMessageListener(shouldLoadListenerMessageName, + shouldLoadListener); + } + + httpObserver.register(); + + vAPI.addCleanUpTask(function () { + if (!vAPI.modernFirefox) { + vAPI.messaging.globalMessageManager + .removeMessageListener(shouldLoadListenerMessageName, + shouldLoadListener); + } + + httpObserver.unregister(); + }); + }; +})(); -- 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-net.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'js/vapi-net.js') diff --git a/js/vapi-net.js b/js/vapi-net.js index 5c6ccaf..c1a605d 100644 --- a/js/vapi-net.js +++ b/js/vapi-net.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 fbb154ffb1bc1be56004820bf62015c92d13c3c5 Mon Sep 17 00:00:00 2001 From: Alessio Vanni Date: Thu, 4 Jul 2019 17:17:32 +0200 Subject: Temporarily fix undefined reference Ideally HttpObserver should be its own module, but right now it depends on too much "context" to be independent from vapi-background. --- js/vapi-net.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'js/vapi-net.js') diff --git a/js/vapi-net.js b/js/vapi-net.js index c1a605d..3d5b27b 100644 --- a/js/vapi-net.js +++ b/js/vapi-net.js @@ -45,7 +45,7 @@ let shouldLoadListenerMessageName = location.host + ':shouldLoad'; let shouldLoadListener = function (e) { let details = e.data; - let pendingReq = httpObserver.createPendingRequest(details.url); + let pendingReq = vAPI.httpObserver.createPendingRequest(details.url); pendingReq.rawType = details.rawType; pendingReq.tabId = vAPI.tabs.manager.tabIdFromTarget(e.target); }; @@ -59,7 +59,7 @@ shouldLoadListener); } - httpObserver.register(); + vAPI.httpObserver.register(); vAPI.addCleanUpTask(function () { if (!vAPI.modernFirefox) { @@ -68,7 +68,7 @@ shouldLoadListener); } - httpObserver.unregister(); + vAPI.httpObserver.unregister(); }); }; })(); -- 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-net.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'js/vapi-net.js') diff --git a/js/vapi-net.js b/js/vapi-net.js index 3d5b27b..25de1cb 100644 --- a/js/vapi-net.js +++ b/js/vapi-net.js @@ -27,7 +27,7 @@ (function () { const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - const {Services} = Cu.import('resource://gre/modules/Services.jsm', null); + Cu.import('resource://gre/modules/Services.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-net.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'js/vapi-net.js') diff --git a/js/vapi-net.js b/js/vapi-net.js index 25de1cb..6f7e885 100644 --- a/js/vapi-net.js +++ b/js/vapi-net.js @@ -26,9 +26,6 @@ /******************************************************************************/ (function () { - const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - Cu.import('resource://gre/modules/Services.jsm'); - let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js vAPI.net = {}; -- 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-net.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'js/vapi-net.js') diff --git a/js/vapi-net.js b/js/vapi-net.js index 6f7e885..25de1cb 100644 --- a/js/vapi-net.js +++ b/js/vapi-net.js @@ -26,6 +26,9 @@ /******************************************************************************/ (function () { + const {classes: Cc, interfaces: Ci, utils: Cu} = Components; + Cu.import('resource://gre/modules/Services.jsm'); + let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js vAPI.net = {}; -- 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-net.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'js/vapi-net.js') diff --git a/js/vapi-net.js b/js/vapi-net.js index 25de1cb..ee52945 100644 --- a/js/vapi-net.js +++ b/js/vapi-net.js @@ -29,8 +29,6 @@ const {classes: Cc, interfaces: Ci, utils: Cu} = Components; Cu.import('resource://gre/modules/Services.jsm'); - let vAPI = self.vAPI; // Guaranteed to be initialized by vapi-background.js - vAPI.net = {}; vAPI.net.registerListeners = function () { -- 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-net.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'js/vapi-net.js') diff --git a/js/vapi-net.js b/js/vapi-net.js index ee52945..7accbc0 100644 --- a/js/vapi-net.js +++ b/js/vapi-net.js @@ -26,9 +26,6 @@ /******************************************************************************/ (function () { - const {classes: Cc, interfaces: Ci, utils: Cu} = Components; - Cu.import('resource://gre/modules/Services.jsm'); - vAPI.net = {}; vAPI.net.registerListeners = function () { -- cgit v1.2.3