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 ChromeUtils.defineESModuleGetters(lazy, {
8 AppInfo: "chrome://remote/content/shared/AppInfo.sys.mjs",
9 Log: "chrome://remote/content/shared/Log.sys.mjs",
12 ChromeUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get());
14 const COMMON_DIALOG = "chrome://global/content/commonDialog.xhtml";
17 export const modal = {
18 ACTION_CLOSED: "closed",
19 ACTION_OPENED: "opened",
23 * Check for already existing modal or tab modal dialogs and
24 * return the first one.
26 * @param {browser.Context} context
27 * Reference to the browser context to check for existent dialogs.
29 * @returns {modal.Dialog}
30 * Returns instance of the Dialog class, or `null` if no modal dialog
33 modal.findPrompt = function (context) {
34 // First check if there is a modal dialog already present for the
35 // current browser window.
36 for (let win of Services.wm.getEnumerator(null)) {
37 // TODO: Use BrowserWindowTracker.getTopWindow for modal dialogs without
40 win.document.documentURI === COMMON_DIALOG &&
42 win.opener === context.window
44 lazy.logger.trace("Found open window modal prompt");
45 return new modal.Dialog(win);
49 if (lazy.AppInfo.isAndroid) {
50 const geckoViewPrompts = context.window.prompts();
51 if (geckoViewPrompts.length) {
52 lazy.logger.trace("Found open GeckoView prompt");
53 const prompt = geckoViewPrompts[0];
54 return new modal.Dialog(prompt);
58 const contentBrowser = context.contentBrowser;
60 // If no modal dialog has been found yet, also check for tab and content modal
61 // dialogs for the current tab.
63 // TODO: Find an adequate implementation for Firefox on Android (bug 1708105)
64 if (contentBrowser?.tabDialogBox) {
65 let dialogs = contentBrowser.tabDialogBox.getTabDialogManager().dialogs;
67 lazy.logger.trace("Found open tab modal prompt");
68 return new modal.Dialog(dialogs[0].frameContentWindow);
71 dialogs = contentBrowser.tabDialogBox.getContentDialogManager().dialogs;
73 // Even with the dialog manager handing back a dialog, the `Dialog` property
74 // gets lazily added. If it's not set yet, ignore the dialog for now.
75 if (dialogs.length && dialogs[0].frameContentWindow.Dialog) {
76 lazy.logger.trace("Found open content prompt");
77 return new modal.Dialog(dialogs[0].frameContentWindow);
84 * Represents a modal dialog.
86 * @param {DOMWindow} dialog
87 * DOMWindow of the dialog.
89 modal.Dialog = class {
93 this.#win = Cu.getWeakReference(dialog);
97 if (lazy.AppInfo.isAndroid) {
98 return this.window.args;
100 let tm = this.tabModal;
101 return tm ? tm.args : null;
105 if (lazy.AppInfo.isAndroid) {
106 return this.window !== null;
114 get isWindowModal() {
116 Services.prompt.MODAL_TYPE_WINDOW,
117 Services.prompt.MODAL_TYPE_INTERNAL_WINDOW,
118 ].includes(this.args.modalType);
122 return this.window?.Dialog;
126 return this.args.inPermitUnload ? "beforeunload" : this.args.promptType;
130 let tm = this.tabModal;
131 return tm ? tm.ui : null;
135 * For Android, this returns a GeckoViewPrompter, which can be used to control prompts.
136 * Otherwise, this returns the ChromeWindow associated with an open dialog window if
137 * it is currently attached to the DOM.
141 let win = this.#win.get();
142 if (win && (lazy.AppInfo.isAndroid || win.parent)) {
149 set text(inputText) {
150 if (lazy.AppInfo.isAndroid) {
151 this.window.setInputText(inputText);
153 // see toolkit/components/prompts/content/commonDialog.js
154 let { loginTextbox } = this.ui;
155 loginTextbox.value = inputText;
160 if (lazy.AppInfo.isAndroid) {
161 // GeckoView does not have a UI, so the methods are called directly
162 this.window.acceptPrompt();
164 const { button0 } = this.ui;
170 if (lazy.AppInfo.isAndroid) {
171 // GeckoView does not have a UI, so the methods are called directly
172 this.window.dismissPrompt();
174 const { button0, button1 } = this.ui;
175 (button1 ? button1 : button0).click();
180 * Returns text of the prompt.
182 * @returns {string | Promise}
183 * Returns string on desktop and Promise on Android.
186 if (lazy.AppInfo.isAndroid) {
187 const textPromise = await this.window.getPromptText();
190 return this.ui.infoBody.textContent;
194 * Returns text of the prompt input.
197 * Returns string on desktop and Promise on Android.
199 async getInputText() {
200 if (lazy.AppInfo.isAndroid) {
201 const textPromise = await this.window.getInputText();
204 return this.ui.loginTextbox.value;