summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/FormHandlerChild.sys.mjs
blob: 6b1af3dbc3fb76c02ada75316f2ee90bf02dcbae (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
/* 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/. */

/**
 * The FormHandlerChild is the place to implement logic that is shared
 * by child actors like FormAutofillChild, LoginManagerChild and FormHistoryChild
 * or in general components that deal with form data.
 */

export const FORM_SUBMISSION_REASON = {
  FORM_SUBMIT_EVENT: "form-submit-event",
  FORM_REMOVAL_AFTER_FETCH: "form-removal-after-fetch",
  IFRAME_PAGEHIDE: "iframe-pagehide",
  PAGE_NAVIGATION: "page-navigation",
};

export class FormHandlerChild extends JSWindowActorChild {
  handleEvent(event) {
    if (!event.isTrusted) {
      return;
    }
    switch (event.type) {
      case "DOMFormBeforeSubmit":
        this.processDOMFormBeforeSubmitEvent(event);
        break;
      default:
        throw new Error("Unexpected event type");
    }
  }

  /**
   * Process the DOMFormBeforeSubmitEvent that is dispatched
   * after a form submit event. Extract event data
   * that is relevant to the form submission listeners
   *
   * @param {Event} event DOMFormBeforeSubmit
   */
  processDOMFormBeforeSubmitEvent(event) {
    const form = event.target;
    const formSubmissionReason = FORM_SUBMISSION_REASON.FORM_SUBMIT_EVENT;

    this.#dispatchFormSubmissionEvent(form, formSubmissionReason);
  }

  // handle form-removal-after-fetch
  processFormRemovalAfterFetch(params) {}

  // handle iframe-pagehide
  processIframePagehide(params) {}

  // handle page-navigation
  processPageNavigation(params) {}

  /**
   * Dispatch the CustomEvent form-submission-detected also transfer
   * the information:
   *            detail.form   - the form that is being submitted
   *            detail.reason - the heuristic that detected the form submission
   *                            (see FORM_SUBMISSION_REASON)
   *
   * @param {HTMLFormElement} form
   * @param {string} reason
   */
  #dispatchFormSubmissionEvent(form, reason) {
    const formSubmissionEvent = new CustomEvent("form-submission-detected", {
      detail: { form, reason },
      bubbles: true,
    });
    this.document.dispatchEvent(formSubmissionEvent);
  }
}