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
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 ChromeUtils.defineESModuleGetters(lazy, {
8 "resource://gre/modules/shared/AutofillFormFactory.sys.mjs",
10 "resource://gre/modules/shared/FormAutofillHandler.sys.mjs",
11 FormAutofillUtils: "resource://gre/modules/shared/FormAutofillUtils.sys.mjs",
14 export class FormStateManager {
15 #formHandlerByElement = new WeakMap();
16 #formHandlerByRootElement = new WeakMap();
17 #formHandlerByRootId = new Map();
19 constructor(onFilledModifiedCallback) {
21 * @type {WeakMap} mapping FormLike root HTML elements to FormAutofillHandler objects.
24 this.onFilledModifiedCallback = onFilledModifiedCallback;
27 getWeakIdentifiedForms() {
28 return ChromeUtils.nondeterministicGetWeakMapKeys(
29 this.#formHandlerByRootElement
34 * Get the form handler for the specified input element.
36 * @param {HTMLInputElement} element
37 * Focused input which triggered profile searching
38 * @returns {FormAutofillHandler | null}
39 * The form handler associated with the specified input element.
41 getFormHandler(element) {
46 let handler = this.#formHandlerByElement.get(element);
51 const rootElement = lazy.AutofillFormFactory.findRootForField(element);
52 return this.#formHandlerByRootElement.get(rootElement);
56 * Get the form handler for the specified root element.
58 * @param {string} rootElementId
59 * the id of the root element
60 * @returns {FormAutofillHandler | null}
61 * The form handler associated with the specified input element.
63 getFormHandlerByRootElementId(rootElementId) {
64 return this.#formHandlerByRootId.get(rootElementId);
68 * Get the form handler for the specified input element. If no handler exists
69 * in the cache, this function creates a new one.
71 * @param {HTMLInputElement} element
72 * Focused input which triggered profile searching
73 * @returns {FormAutofillHandler}
74 * The form handler associated with the specified input element.
76 getOrCreateFormHandler(element) {
77 let handler = this.getFormHandler(element);
79 handler = new lazy.FormAutofillHandler(
80 lazy.AutofillFormFactory.createFromField(element),
81 this.onFilledModifiedCallback
84 const root = handler.form.rootElement;
85 const rootElementId = lazy.FormAutofillUtils.getElementIdentifier(root);
86 this.#formHandlerByRootId.set(rootElementId, handler);
87 this.#formHandlerByRootElement.set(root, handler);
88 handler.form.elements.forEach(ele =>
89 this.#formHandlerByElement.set(ele, handler)
96 export default FormStateManager;