From dc06d5fa0cbecfc10f7f1e96af04f945e016bc1c Mon Sep 17 00:00:00 2001 From: Gijs Kruitbosch Date: Fri, 31 Mar 2017 21:17:21 +0100 Subject: [PATCH] Fix #2493 by using the createWindowlessBrowser API when available --- platform/firefox/bootstrap.js | 78 +++++++++++++++++++++++++++-------- 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/platform/firefox/bootstrap.js b/platform/firefox/bootstrap.js index 04e43ebfc..3f4660547 100644 --- a/platform/firefox/bootstrap.js +++ b/platform/firefox/bootstrap.js @@ -61,6 +61,60 @@ function startup(data/*, reason*/) { let appShell = Cc['@mozilla.org/appshell/appShellService;1'] .getService(Ci.nsIAppShellService); + if ( appShell.createWindowlessBrowser ) { + getWindowlessBrowserFrame(appShell); + } else { + getHiddenWindowBrowserFrame(appShell); + } +} + +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 + ); + + // 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 + ); +} + +let windowlessBrowser; +let windowlessBrowserPL; + +function getWindowlessBrowserFrame(appShell) { + windowlessBrowser = appShell.createWindowlessBrowser(true); + windowlessBrowser.QueryInterface(Ci.nsIInterfaceRequestor); + let webProgress = windowlessBrowser.getInterface(Ci.nsIWebProgress); + let XPCOMUtils = Components.utils.import('resource://gre/modules/XPCOMUtils.jsm', null).XPCOMUtils; + windowlessBrowserPL = { + QueryInterface: XPCOMUtils.generateQI([ + Ci.nsIWebProgressListener, Ci.nsIWebProgressListener2, + Ci.nsISupportsWeakReference]), + onStateChange(wbp, request, stateFlags, status) { + 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,"; +} + + +function getHiddenWindowBrowserFrame(appShell) { let isReady = function() { var hiddenDoc; @@ -75,23 +129,7 @@ function startup(data/*, reason*/) { if ( !hiddenDoc || hiddenDoc.readyState !== 'complete' ) { return false; } - - bgProcess = hiddenDoc.documentElement.appendChild( - hiddenDoc.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 - ); - + createBgProcess(hiddenDoc); return true; }; @@ -150,6 +188,12 @@ function shutdown(data, reason) { bgProcess = null; } + if ( windowlessBrowser !== null ) { + windowlessBrowser.close(); + windowlessBrowser = null; + windowlessBrowserPL = null; + } + if ( data === undefined ) { return; }