Skip to content

Commit 21524f4

Browse files
committed
Bug 1998953 - [remote] Improve logging when prompts are opened and closed. r=jdescottes
Differential Revision: https://phabricator.services.mozilla.com/D271801
1 parent cfa8b20 commit 21524f4

File tree

3 files changed

+72
-27
lines changed

3 files changed

+72
-27
lines changed

remote/marionette/driver.sys.mjs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,14 @@ export function GeckoDriver(server) {
334334
this._actionsHelper = new ActionsHelper(this);
335335
}
336336

337+
GeckoDriver.prototype._trace = function (message, browsingContext = null) {
338+
if (browsingContext !== null) {
339+
lazy.logger.trace(`[${browsingContext.id}] ${message}`);
340+
} else {
341+
lazy.logger.trace(message);
342+
}
343+
};
344+
337345
/**
338346
* The current context decides if commands are executed in chrome- or
339347
* content space.
@@ -410,20 +418,39 @@ GeckoDriver.prototype.QueryInterface = ChromeUtils.generateQI([
410418
* Callback used to observe the closing of modal dialogs
411419
* during the session's lifetime.
412420
*/
413-
GeckoDriver.prototype.handleClosedModalDialog = function () {
421+
GeckoDriver.prototype.handleClosedModalDialog = function (_eventName, data) {
422+
const { contentBrowser, detail } = data;
423+
424+
this._trace(
425+
`Prompt closed (type: "${detail.promptType}", accepted: "${detail.accepted}")`,
426+
contentBrowser.browsingContext
427+
);
428+
414429
this.dialog = null;
415430
};
416431

417432
/**
418433
* Callback used to observe the creation of new modal dialogs
419434
* during the session's lifetime.
420435
*/
421-
GeckoDriver.prototype.handleOpenModalDialog = function (eventName, data) {
422-
this.dialog = data.prompt;
436+
GeckoDriver.prototype.handleOpenModalDialog = function (_eventName, data) {
437+
const { contentBrowser, prompt } = data;
438+
439+
prompt.getText().then(text => {
440+
// We need the text to identify a user prompt when it gets
441+
// randomly opened. Because on Android the text is asynchronously
442+
// retrieved lets delay the logging without making the handler async.
443+
this._trace(
444+
`Prompt opened (type: "${prompt.promptType}", text: "${text}")`,
445+
contentBrowser.browsingContext
446+
);
447+
});
448+
449+
this.dialog = prompt;
423450

424451
if (this.dialog.promptType === "beforeunload" && !this.currentSession?.bidi) {
425452
// Only implicitly accept the prompt when its not a BiDi session.
426-
lazy.logger.trace(`Implicitly accepted "beforeunload" prompt`);
453+
this._trace(`Implicitly accepted "beforeunload" prompt`);
427454
this.dialog.accept();
428455
return;
429456
}

remote/shared/listeners/PromptListener.sys.mjs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ export class PromptListener {
8585
* Handles `DOMModalDialogClosed` events.
8686
*/
8787
handleEvent(event) {
88-
lazy.logger.trace(`Received event ${event.type}`);
89-
9088
const chromeWin = event.target.opener
9189
? event.target.opener.ownerGlobal
9290
: event.target.ownerGlobal;
@@ -136,12 +134,10 @@ export class PromptListener {
136134
* `domwindowopened` - when a new chrome window opened,
137135
* `geckoview-prompt-show` - when a modal dialog opened on Android.
138136
*/
139-
observe(subject, topic) {
140-
lazy.logger.trace(`Received observer notification ${topic}`);
141-
137+
async observe(subject, topic) {
142138
let curBrowser = this.#curBrowserFn && this.#curBrowserFn();
143139
switch (topic) {
144-
case "common-dialog-loaded":
140+
case "common-dialog-loaded": {
145141
if (curBrowser) {
146142
if (
147143
!this.#hasCommonDialog(
@@ -178,12 +174,14 @@ export class PromptListener {
178174
});
179175

180176
break;
177+
}
181178

182-
case "domwindowopened":
179+
case "domwindowopened": {
183180
subject.addEventListener("DOMModalDialogClosed", this);
184181
break;
182+
}
185183

186-
case "geckoview-prompt-show":
184+
case "geckoview-prompt-show": {
187185
for (let win of Services.wm.getEnumerator(null)) {
188186
const subjectObject = subject.wrappedJSObject;
189187
const prompt = win
@@ -210,6 +208,7 @@ export class PromptListener {
210208
}
211209
}
212210
break;
211+
}
213212
}
214213
}
215214

@@ -248,9 +247,13 @@ export class PromptListener {
248247
}
249248

250249
#register() {
251-
Services.obs.addObserver(this, "common-dialog-loaded");
252-
Services.obs.addObserver(this, "domwindowopened");
253-
Services.obs.addObserver(this, "geckoview-prompt-show");
250+
for (const observerName of [
251+
"common-dialog-loaded",
252+
"domwindowopened",
253+
"geckoview-prompt-show",
254+
]) {
255+
Services.obs.addObserver(this, observerName);
256+
}
254257

255258
// Register event listener and save already open prompts for all already open windows.
256259
for (const win of Services.wm.getEnumerator(null)) {
@@ -259,21 +262,19 @@ export class PromptListener {
259262
}
260263

261264
#unregister() {
262-
const removeObserver = observerName => {
265+
[
266+
"common-dialog-loaded",
267+
"domwindowopened",
268+
"geckoview-prompt-show",
269+
].forEach(observerName => {
263270
try {
264271
Services.obs.removeObserver(this, observerName);
265272
} catch (e) {
266-
lazy.logger.debug(`Failed to remove observer "${observerName}"`);
273+
lazy.logger.debug(
274+
`${this.constructor.name}: Failed to remove observer "${observerName}"`
275+
);
267276
}
268-
};
269-
270-
for (const observerName of [
271-
"common-dialog-loaded",
272-
"domwindowopened",
273-
"geckoview-prompt-show",
274-
]) {
275-
removeObserver(observerName);
276-
}
277+
});
277278

278279
// Unregister event listener for all open windows
279280
for (const win of Services.wm.getEnumerator(null)) {

remote/webdriver-bidi/modules/root/browsingContext.sys.mjs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2127,7 +2127,7 @@ class BrowsingContextModule extends RootBiDiModule {
21272127
}
21282128
};
21292129

2130-
#onPromptClosed = async (eventName, data) => {
2130+
#onPromptClosed = (eventName, data) => {
21312131
if (this.#subscribedEvents.has("browsingContext.userPromptClosed")) {
21322132
const { contentBrowser, detail } = data;
21332133
const navigableId = lazy.NavigableManager.getIdForBrowser(contentBrowser);
@@ -2136,6 +2136,12 @@ class BrowsingContextModule extends RootBiDiModule {
21362136
return;
21372137
}
21382138

2139+
lazy.logger.trace(
2140+
`[${contentBrowser.browsingContext.id}] Prompt closed (type: "${
2141+
detail.promptType
2142+
}", accepted: "${detail.accepted}")`
2143+
);
2144+
21392145
const params = {
21402146
context: navigableId,
21412147
accepted: detail.accepted,
@@ -2156,6 +2162,17 @@ class BrowsingContextModule extends RootBiDiModule {
21562162
const { contentBrowser, prompt } = data;
21572163
const type = prompt.promptType;
21582164

2165+
prompt.getText().then(text => {
2166+
// We need the text to identify a user prompt when it gets
2167+
// randomly opened. Because on Android the text is asynchronously
2168+
// retrieved lets delay the logging without making the handler async.
2169+
lazy.logger.trace(
2170+
`[${contentBrowser.browsingContext.id}] Prompt opened (type: "${
2171+
prompt.promptType
2172+
}", text: "${text}")`
2173+
);
2174+
});
2175+
21592176
// Do not send opened event for unsupported prompt types.
21602177
if (!(type in UserPromptType)) {
21612178
lazy.logger.trace(`Prompt type "${type}" not supported`);

0 commit comments

Comments
 (0)