fune/toolkit/components/satchel/FormHandlerParent.sys.mjs
jneuberger 04bddea373 Bug 1884632 - P1. Handle heuristic for page navigations centrally in FormHandlerChild r=dimi,credential-management-reviewers,sgalich,geckoview-reviewers,owlish
FormHandler is a central place for heuristics that other components like the LoginManager and FormAutofill
rely on. This patch moves the heuristics that detect page navigations to the FormHandler.
It also applies two changes:
- Heuristic to capture on page navigation no longer relies on the process' active element in FormAutofill
- Capturing in cross-origin frames is introduced

Introduced page navigation heuristic:
When LoginManager/FormAutofill detect a form that they expect a submission for, a FormHandler actor pair is
created in the current window context, which registers the web progress listener that listens for "page navigations",
e.g. location changes of the observed window/document or history session changes.
- If the form is in a same-orign frame, we register the listener only at the top level.
- If the form is in a cross-origin frame, we additionally set up a listener with the root
  of the cross-origin process, so that we are aware of page navigations in both processes.
When a page navigation is observed, all existing (same-origin and cross-origin) FormHandler parents in the
browsing context subtree notify their children.
(Note: We don't create any new actors in this step, because they won't have any form to submit anyway).
When the corresponding FormHandlerChild(ren) are notified of the page navigation, they fire the "form-submission-detected" event.

On "form-submission-detected" event:
- The LoginManagerChild instance(s) kept track of all modified login forms and infers capturing them.
- The FormAutofillChild instance(s) kept track of all identified formautofill forms and infers capturing them.

Differential Revision: https://phabricator.services.mozilla.com/D204927
2024-05-13 21:31:38 +00:00

73 lines
2.8 KiB
JavaScript

/* 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/. */
export class FormHandlerParent extends JSWindowActorParent {
receiveMessage(message) {
switch (message.name) {
case "FormHandler:EnsureParentExists": {
// This dummy message is sent to make sure that the parent exists,
// because we use the existence of the parent to determine whether to
// notify the corresponding child when a page navigation occurs.
break;
}
case "FormHandler:NotifyNavigatedSubtree": {
this.onPageNavigated(message.data);
break;
}
case "FormHandler:RegisterProgressListenerAtTopLevel": {
this.registerProgressListenerAtTopLevel();
break;
}
}
}
/**
* Go through the subtree of the navigated browsing context and
* let the existing FormHandler parents (same-origin and cross-origin)
* notify their corresponding children of the detected page navigation
* If the current browsing context is the navigated one, skip it,
* because the page navigation was processed directly in the child.
*
* @param {BrowsingContext} navigatedBrowsingContext
*/
onPageNavigated(navigatedBrowsingContext) {
const browsingContexts =
navigatedBrowsingContext.getAllBrowsingContextsInSubtree();
if (this.browsingContext === navigatedBrowsingContext) {
// Don't notify the child of the navigated process root,
// since the page navigation was already processed in that child
browsingContexts.shift();
}
for (const context of browsingContexts) {
const windowGlobal = context.currentWindowGlobal;
if (!windowGlobal) {
continue;
}
// This next step doesn't create the FormHandler actor pair. We only
// check whether the FormHandlerParent already exists.
// If it exists, somebody in that window context registered an interest
// in form submissions, so we send a message.
// If it doesn't exist, then nobody in that window context is interested
// in the form submissions, so we don't need to send a message.
const formHandlerActor = windowGlobal.getExistingActor("FormHandler");
formHandlerActor?.sendAsyncMessage(
"FormHandler:FormSubmissionByNavigation"
);
}
}
/**
* Send a dummy message to the FormHandlerChild of the top.
* This is to make sure that the top child is being created and has
* registered the progress listener that listens for page navigations.
*/
registerProgressListenerAtTopLevel() {
const topLevelFormHandler =
this.browsingContext.top.currentWindowGlobal.getActor("FormHandler");
topLevelFormHandler.sendAsyncMessage("FormHandler:EnsureChildExists");
}
}