Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / dom / browser-element / BrowserElementChildPreload.js
blob1bbcf9ff05de12b8ac9507a267116a3e9c53bfd3
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/. */
5 "use strict";
7 /* eslint-env mozilla/frame-script */
9 function debug(msg) {
10   // dump("BrowserElementChildPreload - " + msg + "\n");
13 debug("loaded");
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
23   // finishes loading.
24   if (!BrowserElementIsReady) {
25     return;
26   }
28   if (!data) {
29     data = {};
30   }
32   data.msg_name = msg;
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 },
45 /**
46  * The BrowserElementChild implements one half of <iframe mozbrowser>.
47  * (The other half is, unsurprisingly, BrowserElementParent.)
48  *
49  * This script is injected into an <iframe mozbrowser> via
50  * nsIMessageManager::LoadFrameScript().
51  *
52  * Our job here is to listen for events within this frame and bubble them up to
53  * the parent process.
54  */
56 var global = this;
58 function BrowserElementChild() {
59   // Maps outer window id --> weak ref to window.  Used by modal dialog code.
60   this._windowIDDict = {};
62   this._init();
65 BrowserElementChild.prototype = {
66   _init() {
67     debug("Starting up.");
69     BrowserElementPromptService.mapWindowToBrowserElementChild(content, this);
71     this._shuttingDown = false;
73     LISTENED_EVENTS.forEach(event => {
74       addEventListener(
75         event.type,
76         this,
77         event.useCapture,
78         event.wantsUntrusted
79       );
80     });
82     addMessageListener("browser-element-api:call", this);
83   },
85   /**
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.
90    */
91   destroy() {
92     debug("Destroying");
93     this._shuttingDown = true;
95     BrowserElementPromptService.unmapWindowToBrowserElementChild(content);
97     LISTENED_EVENTS.forEach(event => {
98       removeEventListener(
99         event.type,
100         this,
101         event.useCapture,
102         event.wantsUntrusted
103       );
104     });
106     removeMessageListener("browser-element-api:call", this);
107   },
109   handleEvent(event) {
110     switch (event.type) {
111       case "unload":
112         this.destroy(event);
113         break;
114     }
115   },
117   receiveMessage(message) {
118     let self = this;
120     let mmCalls = {
121       "unblock-modal-prompt": this._recvStopWaiting,
122     };
124     if (message.data.msg_name in mmCalls) {
125       return mmCalls[message.data.msg_name].apply(self, arguments);
126     }
127     return undefined;
128   },
130   get _windowUtils() {
131     return content.document.defaultView.windowUtils;
132   },
134   _tryGetInnerWindowID(win) {
135     try {
136       return win.windowGlobalChild.innerWindowId;
137     } catch (e) {
138       return null;
139     }
140   },
142   /**
143    * Show a modal prompt.  Called by BrowserElementPromptService.
144    */
145   showModalPrompt(win, args) {
146     args.windowID = {
147       outer: win.docShell.outerWindowID,
148       inner: this._tryGetInnerWindowID(win),
149     };
150     sendAsyncMsg("showmodalprompt", args);
152     let returnValue = this._waitForResult(win);
154     if (
155       args.promptType == "prompt" ||
156       args.promptType == "confirm" ||
157       args.promptType == "custom-prompt"
158     ) {
159       return returnValue;
160     }
161     return undefined;
162   },
164   /**
165    * Spin in a nested event loop until we receive a unblock-modal-prompt message for
166    * this window.
167    */
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.");
178       return undefined;
179     }
181     this._windowIDDict[outerWindowID] = Cu.getWeakReference(win);
183     debug(
184       "Entering modal state (outerWindowID=" +
185         outerWindowID +
186         ", " +
187         "innerWindowID=" +
188         innerWindowID +
189         ")"
190     );
192     utils.enterModalState();
194     // We'll decrement win.modalDepth when we receive a unblock-modal-prompt message
195     // for the window.
196     if (!win.modalDepth) {
197       win.modalDepth = 0;
198     }
199     win.modalDepth++;
200     let origModalDepth = win.modalDepth;
202     debug("Nested event loop - begin");
203     Services.tm.spinEventLoopUntil(
204       "BrowserElementChildPreload.js:_waitForResult",
205       () => {
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) {
210           debug(
211             "_waitForResult: Inner window ID changed " +
212               "while in nested event loop."
213           );
214           return true;
215         }
217         return win.modalDepth !== origModalDepth || this._shuttingDown;
218       }
219     );
220     debug("Nested event loop - finish");
222     if (win.modalDepth == 0) {
223       delete this._windowIDDict[outerWindowID];
224     }
226     // If we exited the loop because the inner window changed, then bail on the
227     // modal prompt.
228     if (innerWindowID !== this._tryGetInnerWindowID(win)) {
229       throw Components.Exception(
230         "Modal state aborted by navigation",
231         Cr.NS_ERROR_NOT_AVAILABLE
232       );
233     }
235     let returnValue = win.modalReturnValue;
236     delete win.modalReturnValue;
238     if (!this._shuttingDown) {
239       utils.leaveModalState();
240     }
242     debug(
243       "Leaving modal state (outerID=" +
244         outerWindowID +
245         ", " +
246         "innerID=" +
247         innerWindowID +
248         ")"
249     );
250     return returnValue;
251   },
253   _recvStopWaiting(msg) {
254     let outerID = msg.json.windowID.outer;
255     let innerID = msg.json.windowID.inner;
256     let returnValue = msg.json.returnValue;
257     debug(
258       "recvStopWaiting(outer=" +
259         outerID +
260         ", inner=" +
261         innerID +
262         ", returnValue=" +
263         returnValue +
264         ")"
265     );
267     if (!this._windowIDDict[outerID]) {
268       debug("recvStopWaiting: No record of outer window ID " + outerID);
269       return;
270     }
272     let win = this._windowIDDict[outerID].get();
274     if (!win) {
275       debug("recvStopWaiting, but window is gone\n");
276       return;
277     }
279     if (innerID !== this._tryGetInnerWindowID(win)) {
280       debug("recvStopWaiting, but inner ID has changed\n");
281       return;
282     }
284     debug("recvStopWaiting " + win);
285     win.modalReturnValue = returnValue;
286     win.modalDepth--;
287   },
290 var api = new BrowserElementChild();