diff options
author | Jesús <heckyel@hyperbola.info> | 2019-12-30 15:55:13 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2019-12-30 15:55:13 -0500 |
commit | 288df6a7bf8b933e2dc499e38f4915fcf974c14b (patch) | |
tree | 77bba994f260c064d3ee7f76c427ddfaa4f91710 /bootstrap.js | |
parent | a2c9deaa145b780722e93b3899600f287c8094a4 (diff) | |
download | ematrix-288df6a7bf8b933e2dc499e38f4915fcf974c14b.tar.lz ematrix-288df6a7bf8b933e2dc499e38f4915fcf974c14b.tar.xz ematrix-288df6a7bf8b933e2dc499e38f4915fcf974c14b.zip |
backport
- Flush caches on upgrade
- Properly handle FrameModule's unloading
- Use the new module and remove the old implementation
Diffstat (limited to 'bootstrap.js')
-rw-r--r-- | bootstrap.js | 127 |
1 files changed, 70 insertions, 57 deletions
diff --git a/bootstrap.js b/bootstrap.js index b0cdc91..3134dbe 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -21,13 +21,8 @@ uMatrix Home: https://github.com/gorhill/uMatrix */ -/* global ADDON_UNINSTALL, APP_SHUTDOWN */ -/* exported startup, shutdown, install, uninstall */ - 'use strict'; -/******************************************************************************/ - const {classes: Cc, interfaces: Ci, utils: Cu} = Components; // Accessing the context of the background page: @@ -38,10 +33,11 @@ let windowlessBrowserPL = null; let bgProcess = null; let version; const hostName = 'ematrix'; + const restartListener = { get messageManager() { return Cc['@mozilla.org/parentprocessmessagemanager;1'] - .getService(Ci.nsIMessageListenerManager); + .getService(Ci.nsIMessageListenerManager); }, receiveMessage: function() { @@ -50,19 +46,17 @@ const restartListener = { } }; -/******************************************************************************/ - // https://github.com/gorhill/uBlock/issues/2493 // Fix by https://github.com/gijsk // imported from https://github.com/gorhill/uBlock/pull/2497 -function startup(data/*, reason*/) { - if ( data !== undefined ) { +function startup(data, reason) { + if (data !== undefined) { version = data.version; } // Already started? - if ( bgProcess !== null ) { + if (bgProcess !== null) { return; } @@ -70,45 +64,58 @@ function startup(data/*, reason*/) { } function createBgProcess(parentDocument) { - bgProcess = parentDocument.documentElement.appendChild( - parentDocument.createElementNS('http://www.w3.org/1999/xhtml', 'iframe') - ); - bgProcess.setAttribute( - 'src', - 'chrome://' + hostName + '/content/background.html#' + version - ); + bgProcess = parentDocument + .documentElement + .appendChild(parentDocument + .createElementNS('http://www.w3.org/1999/xhtml', + 'iframe')); + + bgProcess.setAttribute('src', + 'chrome://' + hostName + + '/content/background.html#' + version); // https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIMessageListenerManager#addMessageListener%28%29 // "If the same listener registers twice for the same message, the // "second registration is ignored." - restartListener.messageManager.addMessageListener( - hostName + '-restart', - restartListener - ); + restartListener + .messageManager + .addMessageListener(hostName + '-restart', restartListener); } function getWindowlessBrowserFrame(appShell) { windowlessBrowser = appShell.createWindowlessBrowser(true); windowlessBrowser.QueryInterface(Ci.nsIInterfaceRequestor); + let webProgress = windowlessBrowser.getInterface(Ci.nsIWebProgress); - let XPCOMUtils = Cu.import('resource://gre/modules/XPCOMUtils.jsm', null).XPCOMUtils; + Cu.import('resource://gre/modules/XPCOMUtils.jsm'); + windowlessBrowserPL = { QueryInterface: XPCOMUtils.generateQI([ Ci.nsIWebProgressListener, Ci.nsIWebProgressListener2, Ci.nsISupportsWeakReference ]), + onStateChange: function(wbp, request, stateFlags, status) { - if ( !request ) { return; } - if ( stateFlags & Ci.nsIWebProgressListener.STATE_STOP ) { + if (!request) { + return; + } + + if (stateFlags & Ci.nsIWebProgressListener.STATE_STOP) { webProgress.removeProgressListener(windowlessBrowserPL); windowlessBrowserPL = null; createBgProcess(windowlessBrowser.document); } } }; - webProgress.addProgressListener(windowlessBrowserPL, Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT); - windowlessBrowser.document.location = "data:application/vnd.mozilla.xul+xml;charset=utf-8,<window%20id='" + hostName + "-win'/>"; + + webProgress.addProgressListener(windowlessBrowserPL, + Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT); + + windowlessBrowser.document.location = + "data:application/vnd.mozilla.xul+xml;charset=utf-8,<window%20id='" + + hostName + + "-win'/>"; } function waitForHiddenWindow() { @@ -120,13 +127,13 @@ function waitForHiddenWindow() { try { hiddenDoc = appShell.hiddenDOMWindow && - appShell.hiddenDOMWindow.document; + appShell.hiddenDOMWindow.document; } catch (ex) { } // Do not test against `loading`: it does appear `readyState` could be // undefined if looked up too early. - if ( !hiddenDoc || hiddenDoc.readyState !== 'complete' ) { + if (!hiddenDoc || hiddenDoc.readyState !== 'complete') { return false; } @@ -139,11 +146,11 @@ function waitForHiddenWindow() { // also what the webextension implementation in Firefox uses for // background pages). getWindowlessBrowserFrame(appShell); - + return true; }; - if ( isReady() ) { + if (isReady()) { return; } @@ -153,21 +160,24 @@ function waitForHiddenWindow() { let tryDelay = 5; let trySum = 0; + // https://trac.torproject.org/projects/tor/ticket/19438 // Try for a longer period. + // ηMatrix: I doubt this applies to us... let tryMax = 600011; + let timer = Cc['@mozilla.org/timer;1'] .createInstance(Ci.nsITimer); let checkLater = function() { trySum += tryDelay; - if ( trySum >= tryMax ) { + if (trySum >= tryMax) { timer = null; return; } timer.init(timerObserver, tryDelay, timer.TYPE_ONE_SHOT); tryDelay *= 2; - if ( tryDelay > 503 ) { + if (tryDelay > 503) { tryDelay = 503; } }; @@ -175,7 +185,8 @@ function waitForHiddenWindow() { var timerObserver = { observe: function() { timer.cancel(); - if ( isReady() ) { + + if (isReady()) { timer = null; } else { checkLater(); @@ -186,58 +197,60 @@ function waitForHiddenWindow() { checkLater(); } -/******************************************************************************/ - function shutdown(data, reason) { - if ( reason === APP_SHUTDOWN ) { + if (reason === APP_SHUTDOWN) { return; } - if ( bgProcess !== null ) { + if (bgProcess !== null) { bgProcess.parentNode.removeChild(bgProcess); bgProcess = null; } - if ( windowlessBrowser !== null ) { + if (windowlessBrowser !== null) { // close() does not exist for older versions of Firefox. - if ( typeof windowlessBrowser.close === 'function' ) { + // ηMatrix: how old? But keeping it doesn't really hurt that much. + if (typeof windowlessBrowser.close === 'function') { windowlessBrowser.close(); } windowlessBrowser = null; windowlessBrowserPL = null; } - if ( data === undefined ) { + if (data === undefined) { return; } // Remove the restartObserver only when the extension is being disabled - restartListener.messageManager.removeMessageListener( - hostName + '-restart', - restartListener - ); + restartListener + .messageManager + .removeMessageListener(hostName + '-restart', + restartListener); } -/******************************************************************************/ - -function install() { +function install(data, reason) { // https://bugzil.la/719376 Cc['@mozilla.org/intl/stringbundle;1'] - .getService(Ci.nsIStringBundleService) - .flushBundles(); -} + .getService(Ci.nsIStringBundleService) + .flushBundles(); + + if (reason === ADDON_UPGRADE) { + let Services = + Cu.import('resource://gre/modules/Services.jsm', null).Services -/******************************************************************************/ + Services.obs.notifyObservers(null, 'chrome-flush-caches', null); + Services.obs.notifyObservers(null, 'message-manager-flush-caches', null); + } +} function uninstall(data, aReason) { - if ( aReason !== ADDON_UNINSTALL ) { + if (aReason !== ADDON_UNINSTALL) { return; } + // To cleanup vAPI.localStorage in vapi-common.js, aka // "extensions.ematrix.*" in `about:config`. Cu.import('resource://gre/modules/Services.jsm', null) - .Services.prefs.getBranch('extensions.' + hostName + '.') - .deleteBranch(''); + .Services.prefs.getBranch('extensions.' + hostName + '.') + .deleteBranch(''); } - -/******************************************************************************/ |