fix timing issue at launch for Pale Moon by importing uBO's code

pull/2/head
gorhill 8 years ago
parent 12a6978748
commit 29f0171291

@ -113,25 +113,21 @@ function waitForHiddenWindow() {
let appShell = Cc['@mozilla.org/appshell/appShellService;1'] let appShell = Cc['@mozilla.org/appshell/appShellService;1']
.getService(Ci.nsIAppShellService); .getService(Ci.nsIAppShellService);
let onReady = function(e) { let isReady = function() {
if ( e ) { var hiddenDoc;
this.removeEventListener(e.type, onReady);
}
let hiddenDoc = appShell.hiddenDOMWindow.document; try {
hiddenDoc = appShell.hiddenDOMWindow &&
appShell.hiddenDOMWindow.document;
} catch (ex) {
}
// https://github.com/gorhill/uBlock/issues/10 // Do not test against `loading`: it does appear `readyState` could be
// Fixed by github.com/AlexVallat: // undefined if looked up too early.
// https://github.com/chrisaljoudi/uBlock/issues/1149 if ( !hiddenDoc || hiddenDoc.readyState !== 'complete' ) {
// https://github.com/AlexVallat/uBlock/commit/e762a29d308caa46578cdc34a9be92c4ad5ecdd0 return false;
if ( !hiddenDoc || hiddenDoc.readyState === 'loading' ) {
appShell.hiddenDOMWindow.addEventListener('DOMContentLoaded', onReady);
return;
} }
// Fix from https://github.com/gijsk, taken from:
// - https://github.com/gorhill/uBlock/commit/53a794d9b2a8c65406ee7a201cacbc91c297b2f8
//
// In theory, it should be possible to create a windowless browser // In theory, it should be possible to create a windowless browser
// immediately, without waiting for the hidden window to have loaded // immediately, without waiting for the hidden window to have loaded
// completely. However, in practice, on Windows this seems to lead // completely. However, in practice, on Windows this seems to lead
@ -146,36 +142,50 @@ function waitForHiddenWindow() {
} else { } else {
createBgProcess(hiddenDoc); createBgProcess(hiddenDoc);
} }
return true;
}; };
var ready = false; if ( isReady() ) {
try {
ready = appShell.hiddenDOMWindow &&
appShell.hiddenDOMWindow.document;
} catch (ex) {
}
if ( ready ) {
onReady();
return; return;
} }
let ww = Cc['@mozilla.org/embedcomp/window-watcher;1'] // https://github.com/gorhill/uBlock/issues/749
.getService(Ci.nsIWindowWatcher); // Poll until the proper environment is set up -- or give up eventually.
// We poll frequently early on but relax poll delay as time pass.
ww.registerNotification({ let tryDelay = 5;
observe: function(win, topic) { let trySum = 0;
if ( topic !== 'domwindowopened' ) { // https://trac.torproject.org/projects/tor/ticket/19438
// Try for a longer period.
let tryMax = 600011;
let timer = Cc['@mozilla.org/timer;1']
.createInstance(Ci.nsITimer);
let checkLater = function() {
trySum += tryDelay;
if ( trySum >= tryMax ) {
timer = null;
return; return;
} }
try { timer.init(timerObserver, tryDelay, timer.TYPE_ONE_SHOT);
void appShell.hiddenDOMWindow; tryDelay *= 2;
} catch (ex) { if ( tryDelay > 503 ) {
return; tryDelay = 503;
}
};
var timerObserver = {
observe: function() {
timer.cancel();
if ( isReady() ) {
timer = null;
} else {
checkLater();
} }
ww.unregisterNotification(this);
onReady();
} }
}); };
checkLater();
} }
/******************************************************************************/ /******************************************************************************/

Loading…
Cancel
Save