Bug 1555711 - Port RemotePrompt to a JS Window Actor to be Fission-compatible. r=NeilDeakin,layely

Differential Revision: https://phabricator.services.mozilla.com/D38189

--HG--
rename : browser/modules/RemotePrompt.jsm => browser/actors/PromptParent.jsm
extra : moz-landing-system : lando
This commit is contained in:
Mike Conley 2019-07-31 15:32:15 +00:00
Родитель f677505a74
Коммит b0452cabd0
8 изменённых файлов: 355 добавлений и 189 удалений

Просмотреть файл

@ -0,0 +1,283 @@
/* vim: set ts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var EXPORTED_SYMBOLS = ["PromptParent"];
ChromeUtils.defineModuleGetter(
this,
"PromptUtils",
"resource://gre/modules/SharedPromptUtils.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"Services",
"resource://gre/modules/Services.jsm"
);
/**
* @typedef {Object} Prompt
* @property {Function} resolver
* The resolve function to be called with the data from the Prompt
* after the user closes it.
* @property {Object} tabModalPrompt
* The TabModalPrompt being shown to the user.
*/
/**
* gBrowserPrompts weakly maps BrowsingContexts to a Map of their currently
* active Prompts.
*
* @type {WeakMap<BrowsingContext, Prompt>}
*/
let gBrowserPrompts = new WeakMap();
class PromptParent extends JSWindowActorParent {
didDestroy() {
// In the event that the subframe or tab crashed, make sure that
// we close any active Prompts.
this.forceClosePrompts(this.browsingContext);
}
/**
* Registers a new Prompt to be tracked for a particular BrowsingContext.
* We need to track a Prompt so that we can, for example, force-close the
* TabModalPrompt if the originating subframe or tab unloads or crashes.
*
* @param {BrowsingContext} browsingContext
* The BrowsingContext from which the request to open the Prompt came.
* @param {Object} tabModalPrompt
* The TabModalPrompt that will be shown to the user.
* @param {string} id
* A unique ID to differentiate multiple Prompts coming from the same
* BrowsingContext.
* @return {Promise}
* @resolves {Object}
* Resolves with the arguments returned from the TabModalPrompt when it
* is dismissed.
*/
registerPrompt(browsingContext, tabModalPrompt, id) {
let prompts = gBrowserPrompts.get(browsingContext);
if (!prompts) {
prompts = new Map();
gBrowserPrompts.set(browsingContext, prompts);
}
let promise = new Promise(resolve => {
prompts.set(id, {
tabModalPrompt,
resolver: resolve,
});
});
return promise;
}
/**
* Removes a Prompt for a BrowsingContext with a particular ID from the registry.
* This needs to be done to avoid leaking <xul:browser>'s.
*
* @param {BrowsingContext} browsingContext
* The BrowsingContext from which the request to open the Prompt came.
* @param {string} id
* A unique ID to differentiate multiple Prompts coming from the same
* BrowsingContext.
*/
unregisterPrompt(browsingContext, id) {
let prompts = gBrowserPrompts.get(browsingContext);
if (prompts) {
prompts.delete(id);
}
}
/**
* Programmatically closes a Prompt, without waiting for the TabModalPrompt to
* return with any arguments.
*
* @param {BrowsingContext} browsingContext
* The BrowsingContext from which the request to open the Prompt came.
* @param {string} id
* A unique ID to differentiate multiple Prompts coming from the same
* BrowsingContext.
*/
forceClosePrompt(browsingContext, id) {
let prompts = gBrowserPrompts.get(browsingContext);
let prompt = prompts.get(id);
if (prompt && prompt.tabModalPrompt) {
prompt.tabModalPrompt.abortPrompt();
}
}
/**
* Programmatically closes all Prompts for a BrowsingContext.
*
* @param {BrowsingContext} browsingContext
* The BrowsingContext from which the request to open the Prompts came.
*/
forceClosePrompts(browsingContext) {
let prompts = gBrowserPrompts.get(browsingContext) || [];
for (let prompt of prompts) {
if (prompt.tabModalPrompt) {
prompt.tabModalPrompt.abortPrompt();
}
}
}
receiveMessage(message) {
let browsingContext = this.browsingContext;
let args = message.data;
let id = args._remoteId;
switch (message.name) {
case "Prompt:Open": {
const COMMON_DIALOG = "chrome://global/content/commonDialog.xul";
const SELECT_DIALOG = "chrome://global/content/selectDialog.xul";
let topPrincipal =
browsingContext.top.currentWindowGlobal.documentPrincipal;
args.showAlertOrigin = topPrincipal.equals(args.promptPrincipal);
if (message.data.tabPrompt) {
return this.openTabPrompt(message.data, browsingContext, id);
}
let uri =
message.data.promptType == "select" ? SELECT_DIALOG : COMMON_DIALOG;
let browser = browsingContext.top.embedderElement;
return this.openModalWindow(uri, message.data, browser);
}
case "Prompt:ForceClose": {
this.forceClosePrompt(browsingContext, id);
break;
}
}
return undefined;
}
/**
* Opens a TabModalPrompt for a BrowsingContext, and puts the associated browser
* in the modal state until the TabModalPrompt is closed.
*
* @param {Object} args
* The arguments passed up from the BrowsingContext to be passed directly
* to the TabModalPrompt.
* @param {BrowsingContext} browsingContext
* The BrowsingContext from which the request to open the Prompts came.
* @param {string} id
* A unique ID to differentiate multiple Prompts coming from the same
* BrowsingContext.
* @return {Promise}
* @resolves {Object}
* Resolves with the arguments returned from the TabModalPrompt when it
* is dismissed.
*/
openTabPrompt(args, browsingContext, id) {
let browser = browsingContext.top.embedderElement;
let window = browser.ownerGlobal;
let tabPrompt = window.gBrowser.getTabModalPromptBox(browser);
let newPrompt;
let needRemove = false;
let onPromptClose = forceCleanup => {
let promptData = gBrowserPrompts.get(browsingContext);
if (!promptData || !promptData.has(id)) {
throw new Error(
"Failed to close a prompt since it wasn't registered for some reason."
);
}
let { resolver, tabModalPrompt } = promptData.get(id);
// It's possible that we removed the prompt during the
// appendPrompt call below. In that case, newPrompt will be
// undefined. We set the needRemove flag to remember to remove
// it right after we've finished adding it.
if (tabModalPrompt) {
tabPrompt.removePrompt(tabModalPrompt);
} else {
needRemove = true;
}
this.unregisterPrompt(browsingContext, id);
PromptUtils.fireDialogEvent(window, "DOMModalDialogClosed", browser);
resolver(args);
browser.leaveModalState();
};
try {
browser.enterModalState();
let eventDetail = {
tabPrompt: true,
promptPrincipal: args.promptPrincipal,
inPermitUnload: args.inPermitUnload,
};
PromptUtils.fireDialogEvent(
window,
"DOMWillOpenModalDialog",
browser,
eventDetail
);
args.promptActive = true;
newPrompt = tabPrompt.appendPrompt(args, onPromptClose);
let promise = this.registerPrompt(browsingContext, newPrompt, id);
if (needRemove) {
tabPrompt.removePrompt(newPrompt);
}
return promise;
} catch (ex) {
Cu.reportError(ex);
onPromptClose(true);
}
return null;
}
/**
* Opens a window-modal prompt for a BrowsingContext, and puts the associated
* browser in the modal state until the prompt is closed.
*
* @param {string} uri
* The URI to a XUL document to be loaded in a modal window.
* @param {Object} args
* The arguments passed up from the BrowsingContext to be passed
* directly to the modal window.
* @param {Element} browser
* The <xul:browser> from which the request to open the window-modal
* prompt came.
* @return {Promise}
* @resolves {Object}
* Resolves with the arguments returned from the window-modal
* prompt when it is dismissed.
*/
openModalWindow(uri, args, browser) {
let window = browser.ownerGlobal;
try {
browser.enterModalState();
PromptUtils.fireDialogEvent(window, "DOMWillOpenModalDialog", browser);
let bag = PromptUtils.objectToPropBag(args);
Services.ww.openWindow(
window,
uri,
"_blank",
"centerscreen,chrome,modal,titlebar",
bag
);
PromptUtils.propBagToObject(bag, args);
} finally {
browser.leaveModalState();
PromptUtils.fireDialogEvent(window, "DOMModalDialogClosed", browser);
}
return Promise.resolve(args);
}
}

Просмотреть файл

@ -42,6 +42,7 @@ FINAL_TARGET_FILES.actors += [
'PageStyleChild.jsm',
'PluginChild.jsm',
'PluginParent.jsm',
'PromptParent.jsm',
'RFPHelperChild.jsm',
'SearchTelemetryChild.jsm',
'SubframeCrashChild.jsm',

Просмотреть файл

@ -95,6 +95,14 @@ let ACTORS = {
allFrames: true,
},
Prompt: {
parent: {
moduleURI: "resource:///actors/PromptParent.jsm",
},
allFrames: true,
},
SwitchDocumentDirection: {
child: {
moduleURI: "resource:///actors/SwitchDocumentDirectionChild.jsm",
@ -540,7 +548,6 @@ XPCOMUtils.defineLazyModuleGetters(this, {
PluginManager: "resource:///actors/PluginParent.jsm",
PictureInPicture: "resource://gre/modules/PictureInPicture.jsm",
ReaderParent: "resource:///modules/ReaderParent.jsm",
RemotePrompt: "resource:///modules/RemotePrompt.jsm",
});
/* global ContentPrefServiceParent:false, ContentSearch:false,
@ -645,7 +652,6 @@ const listeners = {
"PictureInPicture:Close": ["PictureInPicture"],
"PictureInPicture:Playing": ["PictureInPicture"],
"PictureInPicture:Paused": ["PictureInPicture"],
"Prompt:Open": ["RemotePrompt"],
"Reader:FaviconRequest": ["ReaderParent"],
"Reader:UpdateReaderButton": ["ReaderParent"],
// PLEASE KEEP THIS LIST IN SYNC WITH THE MOBILE LISTENERS IN BrowserCLH.js

Просмотреть файл

@ -1,131 +0,0 @@
/* vim: set ts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var EXPORTED_SYMBOLS = ["RemotePrompt"];
ChromeUtils.defineModuleGetter(
this,
"PromptUtils",
"resource://gre/modules/SharedPromptUtils.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"Services",
"resource://gre/modules/Services.jsm"
);
var RemotePrompt = {
// Listeners are added in BrowserGlue.jsm
receiveMessage(message) {
switch (message.name) {
case "Prompt:Open":
const COMMON_DIALOG = "chrome://global/content/commonDialog.xul";
const SELECT_DIALOG = "chrome://global/content/selectDialog.xul";
if (message.data.tabPrompt) {
this.openTabPrompt(message.data, message.target);
} else {
let uri =
message.data.promptType == "select" ? SELECT_DIALOG : COMMON_DIALOG;
this.openModalWindow(uri, message.data, message.target);
}
break;
}
},
openTabPrompt(args, browser) {
let window = browser.ownerGlobal;
let tabPrompt = window.gBrowser.getTabModalPromptBox(browser);
let newPrompt;
let needRemove = false;
let promptId = args._remoteId;
function onPromptClose(forceCleanup) {
// It's possible that we removed the prompt during the
// appendPrompt call below. In that case, newPrompt will be
// undefined. We set the needRemove flag to remember to remove
// it right after we've finished adding it.
if (newPrompt) {
tabPrompt.removePrompt(newPrompt);
} else {
needRemove = true;
}
PromptUtils.fireDialogEvent(window, "DOMModalDialogClosed", browser);
browser.messageManager.sendAsyncMessage("Prompt:Close", args);
}
browser.messageManager.addMessageListener(
"Prompt:ForceClose",
function listener(message) {
// If this was for another prompt in the same tab, ignore it.
if (message.data._remoteId !== promptId) {
return;
}
browser.messageManager.removeMessageListener(
"Prompt:ForceClose",
listener
);
if (newPrompt) {
newPrompt.abortPrompt();
}
}
);
try {
let eventDetail = {
tabPrompt: true,
promptPrincipal: args.promptPrincipal,
inPermitUnload: args.inPermitUnload,
};
PromptUtils.fireDialogEvent(
window,
"DOMWillOpenModalDialog",
browser,
eventDetail
);
args.promptActive = true;
newPrompt = tabPrompt.appendPrompt(args, onPromptClose);
if (needRemove) {
tabPrompt.removePrompt(newPrompt);
}
// TODO since we don't actually open a window, need to check if
// there's other stuff in nsWindowWatcher::OpenWindowInternal
// that we might need to do here as well.
} catch (ex) {
Cu.reportError(ex);
onPromptClose(true);
}
},
openModalWindow(uri, args, browser) {
let window = browser.ownerGlobal;
try {
PromptUtils.fireDialogEvent(window, "DOMWillOpenModalDialog", browser);
let bag = PromptUtils.objectToPropBag(args);
Services.ww.openWindow(
window,
uri,
"_blank",
"centerscreen,chrome,modal,titlebar",
bag
);
PromptUtils.propBagToObject(bag, args);
} finally {
PromptUtils.fireDialogEvent(window, "DOMModalDialogClosed", browser);
browser.messageManager.sendAsyncMessage("Prompt:Close", args);
}
},
};

Просмотреть файл

@ -157,7 +157,6 @@ EXTRA_JS_MODULES += [
'PingCentre.jsm',
'ProcessHangMonitor.jsm',
'ReaderParent.jsm',
'RemotePrompt.jsm',
'Sanitizer.jsm',
'SelectionChangedMenulist.jsm',
'SiteDataManager.jsm',

Просмотреть файл

@ -34,6 +34,16 @@ class BrowserElementChild extends JSWindowActorChild {
this.sendAsyncMessage("Done", { permitUnload });
break;
}
case "EnterModalState": {
this.contentWindow.windowUtils.enterModalState();
break;
}
case "LeaveModalState": {
this.contentWindow.windowUtils.leaveModalState();
break;
}
}
}
}

Просмотреть файл

@ -545,11 +545,10 @@ function openTabPrompt(domWin, tabPrompt, args) {
}
function openRemotePrompt(domWin, args) {
let docShell = domWin.docShell;
let messageManager = docShell
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIBrowserChild).messageManager;
let windowGlobal = domWin.getWindowGlobalChild();
let actor = windowGlobal.getActor("Prompt");
let docShell = domWin.docShell;
let inPermitUnload =
docShell.contentViewer && docShell.contentViewer.inPermitUnload;
let eventDetail = Cu.cloneInto(
@ -563,19 +562,12 @@ function openRemotePrompt(domWin, args) {
eventDetail
);
// If domWin is reloaded while we're showing a remote modal
// dialog, it is possible to detach domWin from its tree, and make
// it impossible to reach its scriptable top,
// a.k.a. window.top. To prevent this, make sure to enter/exit
// modal state beginning from top.
let winUtils = domWin.top.windowUtils;
winUtils.enterModalState();
let closed = false;
let windowUtils = domWin.windowUtils;
windowUtils.enterModalState();
let frameMM = docShell.messageManager;
// It should be hard or impossible to cause a window to create multiple
// prompts, but just in case, give our prompt an ID.
// It is technically possible for multiple prompts to be sent from a single
// BrowsingContext. See bug 1266353. We use a randomly generated UUID to
// differentiate between the different prompts.
let id =
"id" +
Cc["@mozilla.org/uuid-generator;1"]
@ -583,32 +575,10 @@ function openRemotePrompt(domWin, args) {
.generateUUID()
.toString();
messageManager.addMessageListener("Prompt:Close", function listener(message) {
if (message.data._remoteId !== id) {
return;
}
let frameMM = docShell.messageManager;
let closed = false;
messageManager.removeMessageListener("Prompt:Close", listener);
frameMM.removeEventListener("pagehide", pagehide, true);
winUtils.leaveModalState();
PromptUtils.fireDialogEvent(domWin, "DOMModalDialogClosed");
// Copy the response from the closed prompt into our args, it will be
// read by our caller.
if (message.data) {
for (let key in message.data) {
args[key] = message.data[key];
}
}
// Exit our nested event loop when we unwind.
closed = true;
});
frameMM.addEventListener("pagehide", pagehide, true);
function pagehide(e) {
// Check whether the event relates to our window or its ancestors
let onPageHide = e => {
let window = domWin;
let eventWindow = e.target.defaultView;
while (window != eventWindow && window.parent != window) {
@ -617,21 +587,41 @@ function openRemotePrompt(domWin, args) {
if (window != eventWindow) {
return;
}
frameMM.removeEventListener("pagehide", pagehide, true);
messageManager.sendAsyncMessage("Prompt:ForceClose", { _remoteId: id });
actor.sendAsyncMessage("Prompt:ForceClose", { _remoteId: id });
closed = true;
};
frameMM.addEventListener("pagehide", onPageHide, true);
try {
let promptPrincipal = domWin.document.nodePrincipal;
args.promptPrincipal = promptPrincipal;
args.inPermitUnload = inPermitUnload;
args._remoteId = id;
let promise = actor.sendQuery("Prompt:Open", args);
promise.then(returnedArgs => {
// Copy the response from the closed prompt into our args, it will be
// read by our caller.
if (returnedArgs) {
if (returnedArgs._remoteId !== id) {
return;
}
for (let key in returnedArgs) {
args[key] = returnedArgs[key];
}
}
closed = true;
});
Services.tm.spinEventLoopUntilOrShutdown(() => closed);
} finally {
frameMM.removeEventListener("pagehide", onPageHide, true);
windowUtils.leaveModalState();
PromptUtils.fireDialogEvent(domWin, "DOMModalDialogClosed");
}
let topPrincipal = domWin.top.document.nodePrincipal;
let promptPrincipal = domWin.document.nodePrincipal;
args.promptPrincipal = promptPrincipal;
args.showAlertOrigin = topPrincipal.equals(promptPrincipal);
args.inPermitUnload = inPermitUnload;
args._remoteId = id;
messageManager.sendAsyncMessage("Prompt:Open", args, {});
Services.tm.spinEventLoopUntilOrShutdown(() => closed);
}
function ModalPrompter(domWin) {

Просмотреть файл

@ -2122,6 +2122,14 @@
sendToChildren(this.browsingContext, false);
}
enterModalState() {
this.sendMessageToActor("EnterModalState", {}, "BrowserElement", true);
}
leaveModalState() {
this.sendMessageToActor("LeaveModalState", {}, "BrowserElement", true);
}
}
MozXULElement.implementCustomInterface(MozBrowser, [Ci.nsIBrowser]);