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 { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
18 var { BrowserElementPromptService } = ChromeUtils.import(
19 "resource://gre/modules/BrowserElementPromptService.jsm"
22 function sendAsyncMsg(msg, data) {
23 // Ensure that we don't send any messages before BrowserElementChild.js
25 if (!BrowserElementIsReady) {
34 sendAsyncMessage("browser-element-api:call", data);
37 var LISTENED_EVENTS = [
38 // This listens to unload events from our message manager, but /not/ from
39 // the |content| window. That's because the window's unload event doesn't
40 // bubble, and we're not using a capturing listener. If we'd used
41 // useCapture == true, we /would/ hear unload events from the window, which
42 // is not what we want!
43 { type: "unload", useCapture: false, wantsUntrusted: false },
47 * The BrowserElementChild implements one half of <iframe mozbrowser>.
48 * (The other half is, unsurprisingly, BrowserElementParent.)
50 * This script is injected into an <iframe mozbrowser> via
51 * nsIMessageManager::LoadFrameScript().
53 * Our job here is to listen for events within this frame and bubble them up to
59 function BrowserElementChild() {
60 // Maps outer window id --> weak ref to window. Used by modal dialog code.
61 this._windowIDDict = {};
66 BrowserElementChild.prototype = {
68 debug("Starting up.");
70 BrowserElementPromptService.mapWindowToBrowserElementChild(content, this);
72 this._shuttingDown = false;
74 LISTENED_EVENTS.forEach(event => {
83 addMessageListener("browser-element-api:call", this);
87 * Shut down the frame's side of the browser API. This is called when:
88 * - our BrowserChildGlobal starts to die
89 * - the content is moved to frame without the browser API
90 * This is not called when the page inside |content| unloads.
94 this._shuttingDown = true;
96 BrowserElementPromptService.unmapWindowToBrowserElementChild(content);
98 LISTENED_EVENTS.forEach(event => {
107 removeMessageListener("browser-element-api:call", this);
111 switch (event.type) {
118 receiveMessage(message) {
122 "unblock-modal-prompt": this._recvStopWaiting,
125 if (message.data.msg_name in mmCalls) {
126 return mmCalls[message.data.msg_name].apply(self, arguments);
132 return content.document.defaultView.windowUtils;
135 _tryGetInnerWindowID(win) {
137 return win.windowGlobalChild.innerWindowId;
144 * Show a modal prompt. Called by BrowserElementPromptService.
146 showModalPrompt(win, args) {
148 outer: win.docShell.outerWindowID,
149 inner: this._tryGetInnerWindowID(win),
151 sendAsyncMsg("showmodalprompt", args);
153 let returnValue = this._waitForResult(win);
156 args.promptType == "prompt" ||
157 args.promptType == "confirm" ||
158 args.promptType == "custom-prompt"
166 * Spin in a nested event loop until we receive a unblock-modal-prompt message for
169 _waitForResult(win) {
170 debug("_waitForResult(" + win + ")");
171 let utils = win.windowUtils;
173 let outerWindowID = win.docShell.outerWindowID;
174 let innerWindowID = this._tryGetInnerWindowID(win);
175 if (innerWindowID === null) {
176 // I have no idea what waiting for a result means when there's no inner
177 // window, so let's just bail.
178 debug("_waitForResult: No inner window. Bailing.");
182 this._windowIDDict[outerWindowID] = Cu.getWeakReference(win);
185 "Entering modal state (outerWindowID=" +
193 utils.enterModalState();
195 // We'll decrement win.modalDepth when we receive a unblock-modal-prompt message
197 if (!win.modalDepth) {
201 let origModalDepth = win.modalDepth;
203 debug("Nested event loop - begin");
204 Services.tm.spinEventLoopUntil(
205 "BrowserElementChildPreload.js:_waitForResult",
207 // Bail out of the loop if the inner window changed; that means the
208 // window navigated. Bail out when we're shutting down because otherwise
209 // we'll leak our window.
210 if (this._tryGetInnerWindowID(win) !== innerWindowID) {
212 "_waitForResult: Inner window ID changed " +
213 "while in nested event loop."
218 return win.modalDepth !== origModalDepth || this._shuttingDown;
221 debug("Nested event loop - finish");
223 if (win.modalDepth == 0) {
224 delete this._windowIDDict[outerWindowID];
227 // If we exited the loop because the inner window changed, then bail on the
229 if (innerWindowID !== this._tryGetInnerWindowID(win)) {
230 throw Components.Exception(
231 "Modal state aborted by navigation",
232 Cr.NS_ERROR_NOT_AVAILABLE
236 let returnValue = win.modalReturnValue;
237 delete win.modalReturnValue;
239 if (!this._shuttingDown) {
240 utils.leaveModalState();
244 "Leaving modal state (outerID=" +
254 _recvStopWaiting(msg) {
255 let outerID = msg.json.windowID.outer;
256 let innerID = msg.json.windowID.inner;
257 let returnValue = msg.json.returnValue;
259 "recvStopWaiting(outer=" +
268 if (!this._windowIDDict[outerID]) {
269 debug("recvStopWaiting: No record of outer window ID " + outerID);
273 let win = this._windowIDDict[outerID].get();
276 debug("recvStopWaiting, but window is gone\n");
280 if (innerID !== this._tryGetInnerWindowID(win)) {
281 debug("recvStopWaiting, but inner ID has changed\n");
285 debug("recvStopWaiting " + win);
286 win.modalReturnValue = returnValue;
291 var api = new BrowserElementChild();