summaryrefslogtreecommitdiffstats
path: root/toolkit/components/formautofill/shared/FormStateManager.sys.mjs
blob: 7481a5981c60c18373f852b12f245bddd88044bc (plain)
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
/* 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/. */

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  FormLikeFactory: "resource://gre/modules/FormLikeFactory.sys.mjs",
  FormAutofillHandler:
    "resource://gre/modules/shared/FormAutofillHandler.sys.mjs",
});

export class FormStateManager {
  constructor(onSubmit, onAutofillCallback) {
    /**
     * @type {WeakMap} mapping FormLike root HTML elements to FormAutofillHandler objects.
     */
    this._formsDetails = new WeakMap();
    /**
     * @type {object} The object where to store the active items, e.g. element,
     * handler, section, and field detail.
     */
    this._activeItems = {};

    this.onSubmit = onSubmit;

    this.onAutofillCallback = onAutofillCallback;
  }

  /**
   * Get the active input's information from cache which is created after page
   * identified.
   *
   * @returns {object | null}
   *          Return the active input's information that cloned from content cache
   *          (or return null if the information is not found in the cache).
   */
  get activeFieldDetail() {
    if (!this._activeItems.fieldDetail) {
      let formDetails = this.activeFormDetails;
      if (!formDetails) {
        return null;
      }
      for (let detail of formDetails) {
        let detailElement = detail.element;
        if (detailElement && this.activeInput == detailElement) {
          this._activeItems.fieldDetail = detail;
          break;
        }
      }
    }
    return this._activeItems.fieldDetail;
  }

  /**
   * Get the active form's information from cache which is created after page
   * identified.
   *
   * @returns {Array<object> | null}
   *          Return target form's information from content cache
   *          (or return null if the information is not found in the cache).
   *
   */
  get activeFormDetails() {
    let formHandler = this.activeHandler;
    return formHandler ? formHandler.fieldDetails : null;
  }

  get activeInput() {
    return this._activeItems.elementWeakRef?.deref();
  }

  get activeHandler() {
    const activeInput = this.activeInput;
    if (!activeInput) {
      return null;
    }

    // XXX: We are recomputing the activeHandler every time to avoid keeping a
    // reference on the active element. This might be called quite frequently
    // so if _getFormHandler/findRootForField become more costly, we should
    // look into caching this result (eg by adding a weakmap).
    let handler = this._getFormHandler(activeInput);
    if (handler) {
      handler.focusedInput = activeInput;
    }
    return handler;
  }

  get activeSection() {
    let formHandler = this.activeHandler;
    return formHandler ? formHandler.activeSection : null;
  }

  /**
   * Get the form's handler from cache which is created after page identified.
   *
   * @param {HTMLInputElement} element Focused input which triggered profile searching
   * @returns {Array<object> | null}
   *          Return target form's handler from content cache
   *          (or return null if the information is not found in the cache).
   *
   */
  _getFormHandler(element) {
    if (!element) {
      return null;
    }
    let rootElement = lazy.FormLikeFactory.findRootForField(element);
    return this._formsDetails.get(rootElement);
  }

  identifyAutofillFields(element) {
    let formHandler = this._getFormHandler(element);
    if (!formHandler) {
      let formLike = lazy.FormLikeFactory.createFromField(element);
      formHandler = new lazy.FormAutofillHandler(
        formLike,
        this.onSubmit,
        this.onAutofillCallback
      );
    } else if (!formHandler.updateFormIfNeeded(element)) {
      return formHandler.fieldDetails;
    }
    this._formsDetails.set(formHandler.form.rootElement, formHandler);
    return formHandler.collectFormFields();
  }

  updateActiveInput(element) {
    if (!element) {
      this._activeItems = {};
      return;
    }
    this._activeItems = {
      elementWeakRef: new WeakRef(element),
      fieldDetail: null,
    };
  }

  getRecords(formElement, handler) {
    handler = handler || this._formsDetails.get(formElement);
    const records = handler?.createRecords();

    if (
      !handler ||
      !records ||
      !Object.values(records).some(typeRecords => typeRecords.length)
    ) {
      return null;
    }
    return records;
  }

  didDestroy() {
    this._activeItems = {};
  }
}

export default FormStateManager;