#749: code review

This commit is contained in:
gorhill 2015-11-03 18:00:08 -05:00
parent 6bec4d795c
commit 45335855af
1 changed files with 20 additions and 7 deletions

View File

@ -62,7 +62,7 @@ function startup(data/*, reason*/) {
.getService(Ci.nsIAppShellService); .getService(Ci.nsIAppShellService);
let isReady = function() { let isReady = function() {
var hiddenDoc = null; var hiddenDoc;
try { try {
hiddenDoc = appShell.hiddenDOMWindow && hiddenDoc = appShell.hiddenDOMWindow &&
@ -70,7 +70,10 @@ function startup(data/*, reason*/) {
} catch (ex) { } catch (ex) {
} }
if ( hiddenDoc === null || hiddenDoc.readyState === 'loading' ) { // Do not test against `loading`: it does appear `readyState` could be
// undefined if looked up too early.
if ( !hiddenDoc ||
hiddenDoc.readyState !== 'interactive' && hiddenDoc.readyState !== 'complete' ) {
return false; return false;
} }
@ -82,6 +85,9 @@ function startup(data/*, reason*/) {
'chrome://' + hostName + '/content/background.html#' + version '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( restartListener.messageManager.addMessageListener(
hostName + '-restart', hostName + '-restart',
restartListener restartListener
@ -96,17 +102,24 @@ function startup(data/*, reason*/) {
// https://github.com/gorhill/uBlock/issues/749 // https://github.com/gorhill/uBlock/issues/749
// Poll until the proper environment is set up -- or give up eventually. // Poll until the proper environment is set up -- or give up eventually.
// We poll frequently early on but relax poll delay as time pass.
let tryCount = 300; let tryDelay = 5;
let trySum = 0;
let tryMax = 30000;
let timer = Cc['@mozilla.org/timer;1'] let timer = Cc['@mozilla.org/timer;1']
.createInstance(Ci.nsITimer); .createInstance(Ci.nsITimer);
let checkLater = function() { let checkLater = function() {
tryCount -= 1; trySum += tryDelay;
if ( tryCount > 0 ) { if ( trySum >= tryMax ) {
timer.init(timerObserver, 100, timer.TYPE_ONE_SHOT);
} else {
timer = null; timer = null;
return;
}
timer.init(timerObserver, tryDelay, timer.TYPE_ONE_SHOT);
tryDelay *= 2;
if ( tryDelay > 500 ) {
tryDelay = 500;
} }
}; };