diff options
author | Alessio Vanni <vannilla@firemail.cc> | 2019-06-22 00:34:29 +0200 |
---|---|---|
committer | Alessio Vanni <vannilla@firemail.cc> | 2019-06-22 00:34:29 +0200 |
commit | 50aff81e575d92fc3ac4a6194fffa7ea89344e2c (patch) | |
tree | 40af7cde497330a2fe7070797419aa3effbdd8f6 | |
parent | 3cc75d0a0da35336b9c12377ea1c63990f55be31 (diff) | |
download | ematrix-50aff81e575d92fc3ac4a6194fffa7ea89344e2c.tar.lz ematrix-50aff81e575d92fc3ac4a6194fffa7ea89344e2c.tar.xz ematrix-50aff81e575d92fc3ac4a6194fffa7ea89344e2c.zip |
Move some commonly used functions out of vapi-background
-rw-r--r-- | js/vapi-core.js | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/js/vapi-core.js b/js/vapi-core.js new file mode 100644 index 0000000..bc7bcb9 --- /dev/null +++ b/js/vapi-core.js @@ -0,0 +1,109 @@ +/******************************************************************************* + + η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 (important functions) + +'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 = self.vAPI || {}; + + // List of things that needs to be destroyed when disabling the extension + // Only functions should be added to it + // eMatrix: taken care by vAPI.addCleanUpTask --- use that function + let cleanupTasks = []; + + // This must be updated manually, every time a new task is added/removed + // eMatrix: do we? + let expectedNumberOfCleanups = 7; + + vAPI.addCleanUpTask = function (task) { + if (typeof task !== 'function') { + return; + } + + cleanupTasks.push(task); + }; + + vAPI.deferUntil = function (testFn, mainFn, details) { + let dtls = (typeof details !== 'object') ? {} : details; + let now = 0; + let next = dtls.next || 200; + let until = dtls.until || 2000; + + let check = function () { + if (testFn() === true || now >= until) { + mainFn(); + return; + } + now += next; + vAPI.setTimeout(check, next); + }; + + if ('sync' in dtls && dtls.sync === true) { + check(); + } else { + vAPI.setTimeout(check, 1); + } + }; + + window.addEventListener('unload', function () { + // if (typeof vAPI.app.onShutdown === 'function') { + // vAPI.app.onShutdown(); + // } + + // IMPORTANT: cleanup tasks must be executed using LIFO order. + for (let i=cleanupTasks.length-1; i>=0; --i) { + try { + cleanupTasks[i](); + } catch (e) { + // Just in case a clean up task ends up throwing for + // no reason + console.error(e); + } + } + + // eMatrix: temporarily disabled + // if (cleanupTasks.length < expectedNumberOfCleanups) { + // console.error + // ('eMatrix> Cleanup tasks performed: %s (out of %s)', + // cleanupTasks.length, + // expectedNumberOfCleanups); + // } + + // frameModule needs to be cleared too + let frameModuleURL = vAPI.getURL('frameModule.js'); + let frameModule = {}; + + Cu.import(frameModuleURL, frameModule); + frameModule.contentObserver.unregister(); + Cu.unload(frameModuleURL); + }); +})(); |