1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/* 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/. */
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
AppInfo: "chrome://remote/content/shared/AppInfo.sys.mjs",
Log: "chrome://remote/content/shared/Log.sys.mjs",
});
XPCOMUtils.defineLazyGetter(lazy, "logger", () =>
lazy.Log.get(lazy.Log.TYPES.MARIONETTE)
);
const COMMON_DIALOG = "chrome://global/content/commonDialog.xhtml";
/** @namespace */
export const modal = {
ACTION_CLOSED: "closed",
ACTION_OPENED: "opened",
};
/**
* Check for already existing modal or tab modal dialogs
*
* @param {browser.Context} context
* Reference to the browser context to check for existent dialogs.
*
* @returns {modal.Dialog}
* Returns instance of the Dialog class, or `null` if no modal dialog
* is present.
*/
modal.findModalDialogs = function (context) {
// First check if there is a modal dialog already present for the
// current browser window.
for (let win of Services.wm.getEnumerator(null)) {
// TODO: Use BrowserWindowTracker.getTopWindow for modal dialogs without
// an opener.
if (
win.document.documentURI === COMMON_DIALOG &&
win.opener &&
win.opener === context.window
) {
lazy.logger.trace("Found open window modal prompt");
return new modal.Dialog(() => context, win);
}
}
if (lazy.AppInfo.isAndroid) {
const geckoViewPrompts = context.window.prompts();
if (geckoViewPrompts.length) {
lazy.logger.trace("Found open GeckoView prompt");
const prompt = geckoViewPrompts[0];
return new modal.Dialog(() => context, prompt);
}
}
const contentBrowser = context.contentBrowser;
// If no modal dialog has been found yet, also check for tab and content modal
// dialogs for the current tab.
//
// TODO: Find an adequate implementation for Firefox on Android (bug 1708105)
if (contentBrowser?.tabDialogBox) {
let dialogs = contentBrowser.tabDialogBox.getTabDialogManager().dialogs;
if (dialogs.length) {
lazy.logger.trace("Found open tab modal prompt");
return new modal.Dialog(() => context, dialogs[0].frameContentWindow);
}
dialogs = contentBrowser.tabDialogBox.getContentDialogManager().dialogs;
// Even with the dialog manager handing back a dialog, the `Dialog` property
// gets lazily added. If it's not set yet, ignore the dialog for now.
if (dialogs.length && dialogs[0].frameContentWindow.Dialog) {
lazy.logger.trace("Found open content prompt");
return new modal.Dialog(() => context, dialogs[0].frameContentWindow);
}
}
// If no modal dialog has been found yet, check for old non SubDialog based
// content modal dialogs. Even with those deprecated in Firefox 89 we should
// keep supporting applications that don't have them implemented yet.
if (contentBrowser?.tabModalPromptBox) {
const prompts = contentBrowser.tabModalPromptBox.listPrompts();
if (prompts.length) {
lazy.logger.trace("Found open old-style content prompt");
return new modal.Dialog(() => context, null);
}
}
return null;
};
/**
* Observer for modal and tab modal dialogs.
*
* @param {function(): browser.Context} curBrowserFn
* Function that returns the current |browser.Context|.
*
* @returns {modal.DialogObserver}
* Returns instance of the DialogObserver class.
*/
modal.DialogObserver = class {
constructor(curBrowserFn) {
this._curBrowserFn = curBrowserFn;
this.callbacks = new Set();
this.register();
}
register() {
Services.obs.addObserver(this, "common-dialog-loaded");
Services.obs.addObserver(this, "domwindowopened");
Services.obs.addObserver(this, "geckoview-prompt-show");
Services.obs.addObserver(this, "tabmodal-dialog-loaded");
// Register event listener for all already open windows
for (let win of Services.wm.getEnumerator(null)) {
win.addEventListener("DOMModalDialogClosed", this);
}
}
unregister() {
Services.obs.removeObserver(this, "common-dialog-loaded");
Services.obs.removeObserver(this, "domwindowopened");
Services.obs.removeObserver(this, "geckoview-prompt-show");
Services.obs.removeObserver(this, "tabmodal-dialog-loaded");
// Unregister event listener for all open windows
for (let win of Services.wm.getEnumerator(null)) {
win.removeEventListener("DOMModalDialogClosed", this);
}
}
cleanup() {
this.callbacks.clear();
this.unregister();
}
handleEvent(event) {
lazy.logger.trace(`Received event ${event.type}`);
const chromeWin = event.target.opener
? event.target.opener.ownerGlobal
: event.target.ownerGlobal;
if (chromeWin != this._curBrowserFn().window) {
return;
}
this.callbacks.forEach(callback => {
callback(modal.ACTION_CLOSED, event.target);
});
}
observe(subject, topic) {
lazy.logger.trace(`Received observer notification ${topic}`);
const curBrowser = this._curBrowserFn();
switch (topic) {
// This topic is only used by the old-style content modal dialogs like
// alert, confirm, and prompt. It can be removed when only the new
// subdialog based content modals remain. Those will be made default in
// Firefox 89, and this case is deprecated.
case "tabmodal-dialog-loaded":
const container = curBrowser.contentBrowser.closest(
".browserSidebarContainer"
);
if (!container.contains(subject)) {
return;
}
this.callbacks.forEach(callback =>
callback(modal.ACTION_OPENED, subject)
);
break;
case "common-dialog-loaded":
const modalType = subject.Dialog.args.modalType;
if (
modalType === Services.prompt.MODAL_TYPE_TAB ||
modalType === Services.prompt.MODAL_TYPE_CONTENT
) {
// Find the container of the dialog in the parent document, and ensure
// it is a descendant of the same container as the current browser.
const container = curBrowser.contentBrowser.closest(
".browserSidebarContainer"
);
if (!container.contains(subject.docShell.chromeEventHandler)) {
return;
}
} else if (
subject.ownerGlobal != curBrowser.window &&
subject.opener?.ownerGlobal != curBrowser.window
) {
return;
}
this.callbacks.forEach(callback =>
callback(modal.ACTION_OPENED, subject)
);
break;
case "domwindowopened":
subject.addEventListener("DOMModalDialogClosed", this);
break;
case "geckoview-prompt-show":
for (let win of Services.wm.getEnumerator(null)) {
const prompt = win.prompts().find(item => item.id == subject.id);
if (prompt) {
this.callbacks.forEach(callback =>
callback(modal.ACTION_OPENED, prompt)
);
return;
}
}
break;
}
}
/**
* Add dialog handler by function reference.
*
* @param {Function} callback
* The handler to be added.
*/
add(callback) {
if (this.callbacks.has(callback)) {
return;
}
this.callbacks.add(callback);
}
/**
* Remove dialog handler by function reference.
*
* @param {Function} callback
* The handler to be removed.
*/
remove(callback) {
if (!this.callbacks.has(callback)) {
return;
}
this.callbacks.delete(callback);
}
/**
* Returns a promise that waits for the dialog to be closed.
*/
async dialogClosed() {
return new Promise(resolve => {
const dialogClosed = (action, dialog) => {
if (action == modal.ACTION_CLOSED) {
this.remove(dialogClosed);
resolve();
}
};
this.add(dialogClosed);
});
}
};
/**
* Represents a modal dialog.
*
* @param {function(): browser.Context} curBrowserFn
* Function that returns the current |browser.Context|.
* @param {DOMWindow} dialog
* DOMWindow of the dialog.
*/
modal.Dialog = class {
constructor(curBrowserFn, dialog) {
this.curBrowserFn_ = curBrowserFn;
this.win_ = Cu.getWeakReference(dialog);
}
get args() {
if (lazy.AppInfo.isAndroid) {
return this.window.args;
}
let tm = this.tabModal;
return tm ? tm.args : null;
}
get curBrowser_() {
return this.curBrowserFn_();
}
get isOpen() {
if (lazy.AppInfo.isAndroid) {
return this.window !== null;
}
if (!this.ui) {
return false;
}
return true;
}
get isWindowModal() {
return [
Services.prompt.MODAL_TYPE_WINDOW,
Services.prompt.MODAL_TYPE_INTERNAL_WINDOW,
].includes(this.args.modalType);
}
get tabModal() {
let win = this.window;
if (win) {
return win.Dialog;
}
return this.curBrowser_.getTabModal();
}
get text() {
if (lazy.AppInfo.isAndroid) {
return this.window.getPromptText();
}
return this.ui.infoBody.textContent;
}
get ui() {
let tm = this.tabModal;
return tm ? tm.ui : null;
}
/**
* For Android, this returns a GeckoViewPrompter, which can be used to control prompts.
* Otherwise, this returns the ChromeWindow associated with an open dialog window if
* it is currently attached to the DOM.
*/
get window() {
if (this.win_) {
let win = this.win_.get();
if (win && (lazy.AppInfo.isAndroid || win.parent)) {
return win;
}
}
return null;
}
set text(inputText) {
if (lazy.AppInfo.isAndroid) {
this.window.setInputText(inputText);
} else {
// see toolkit/components/prompts/content/commonDialog.js
let { loginTextbox } = this.ui;
loginTextbox.value = inputText;
}
}
accept() {
if (lazy.AppInfo.isAndroid) {
// GeckoView does not have a UI, so the methods are called directly
this.window.acceptPrompt();
} else {
const { button0 } = this.ui;
button0.click();
}
}
dismiss() {
if (lazy.AppInfo.isAndroid) {
// GeckoView does not have a UI, so the methods are called directly
this.window.dismissPrompt();
} else {
const { button0, button1 } = this.ui;
(button1 ? button1 : button0).click();
}
}
};
|