1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* eslint-env mozilla/frame-script */
10 // dump("BrowserElementChildPreload - " + msg + "\n");
15 var BrowserElementIsReady;
17 var { BrowserElementPromptService } = ChromeUtils.import(
18 "resource://gre/modules/BrowserElementPromptService.jsm"
21 function sendAsyncMsg(msg, data) {
22 // Ensure that we don't send any messages before BrowserElementChild.js
24 if (!BrowserElementIsReady) {
33 sendAsyncMessage("browser-element-api:call", data);
36 var LISTENED_EVENTS = [
37 // This listens to unload events from our message manager, but /not/ from
38 // the |content| window. That's because the window's unload event doesn't
39 // bubble, and we're not using a capturing listener. If we'd used
40 // useCapture == true, we /would/ hear unload events from the window, which
41 // is not what we want!
42 { type: "unload", useCapture: false, wantsUntrusted: false },
46 * The BrowserElementChild implements one half of <iframe mozbrowser>.
47 * (The other half is, unsurprisingly, BrowserElementParent.)
49 * This script is injected into an <iframe mozbrowser> via
50 * nsIMessageManager::LoadFrameScript().
52 * Our job here is to listen for events within this frame and bubble them up to
58 function BrowserElementChild() {
59 // Maps outer window id --> weak ref to window. Used by modal dialog code.
60 this._windowIDDict = {};
65 BrowserElementChild.prototype = {
67 debug("Starting up.");
69 BrowserElementPromptService.mapWindowToBrowserElementChild(content, this);
71 this._shuttingDown = false;
73 LISTENED_EVENTS.forEach(event => {
82 addMessageListener("browser-element-api:call", this);
86 * Shut down the frame's side of the browser API. This is called when:
87 * - our BrowserChildGlobal starts to die
88 * - the content is moved to frame without the browser API
89 * This is not called when the page inside |content| unloads.
93 this._shuttingDown = true;
95 BrowserElementPromptService.unmapWindowToBrowserElementChild(content);
97 LISTENED_EVENTS.forEach(event => {
106 removeMessageListener("browser-element-api:call", this);
110 switch (event.type) {
117 receiveMessage(message) {
121 "unblock-modal-prompt": this._recvStopWaiting,
124 if (message.data.msg_name in mmCalls) {
125 return mmCalls[message.data.msg_name].apply(self, arguments);
131 return content.document.defaultView.windowUtils;
134 _tryGetInnerWindowID(win) {
136 return win.windowGlobalChild.innerWindowId;
143 * Show a modal prompt. Called by BrowserElementPromptService.
145 showModalPrompt(win, args) {
147 outer: win.docShell.outerWindowID,
148 inner: this._tryGetInnerWindowID(win),
150 sendAsyncMsg("showmodalprompt", args);
152 let returnValue = this._waitForResult(win);
155 args.promptType == "prompt" ||
156 args.promptType == "confirm" ||
157 args.promptType == "custom-prompt"
165 * Spin in a nested event loop until we receive a unblock-modal-prompt message for
168 _waitForResult(win) {
169 debug("_waitForResult(" + win + ")");
170 let utils = win.windowUtils;
172 let outerWindowID = win.docShell.outerWindowID;
173 let innerWindowID = this._tryGetInnerWindowID(win);
174 if (innerWindowID === null) {
175 // I have no idea what waiting for a result means when there's no inner
176 // window, so let's just bail.
177 debug("_waitForResult: No inner window. Bailing.");
181 this._windowIDDict[outerWindowID] = Cu.getWeakReference(win);
184 "Entering modal state (outerWindowID=" +
192 utils.enterModalState();
194 // We'll decrement win.modalDepth when we receive a unblock-modal-prompt message
196 if (!win.modalDepth) {
200 let origModalDepth = win.modalDepth;
202 debug("Nested event loop - begin");
203 Services.tm.spinEventLoopUntil(
204 "BrowserElementChildPreload.js:_waitForResult",
206 // Bail out of the loop if the inner window changed; that means the
207 // window navigated. Bail out when we're shutting down because otherwise
208 // we'll leak our window.
209 if (this._tryGetInnerWindowID(win) !== innerWindowID) {
211 "_waitForResult: Inner window ID changed " +
212 "while in nested event loop."
217 return win.modalDepth !== origModalDepth || this._shuttingDown;
220 debug("Nested event loop - finish");
222 if (win.modalDepth == 0) {
223 delete this._windowIDDict[outerWindowID];
226 // If we exited the loop because the inner window changed, then bail on the
228 if (innerWindowID !== this._tryGetInnerWindowID(win)) {
229 throw Components.Exception(
230 "Modal state aborted by navigation",
231 Cr.NS_ERROR_NOT_AVAILABLE
235 let returnValue = win.modalReturnValue;
236 delete win.modalReturnValue;
238 if (!this._shuttingDown) {
239 utils.leaveModalState();
243 "Leaving modal state (outerID=" +
253 _recvStopWaiting(msg) {
254 let outerID = msg.json.windowID.outer;
255 let innerID = msg.json.windowID.inner;
256 let returnValue = msg.json.returnValue;
258 "recvStopWaiting(outer=" +
267 if (!this._windowIDDict[outerID]) {
268 debug("recvStopWaiting: No record of outer window ID " + outerID);
272 let win = this._windowIDDict[outerID].get();
275 debug("recvStopWaiting, but window is gone\n");
279 if (innerID !== this._tryGetInnerWindowID(win)) {
280 debug("recvStopWaiting, but inner ID has changed\n");
284 debug("recvStopWaiting " + win);
285 win.modalReturnValue = returnValue;
290 var api = new BrowserElementChild();