summaryrefslogtreecommitdiffstats
path: root/toolkit/components/formautofill/FormAutofillContent.sys.mjs
blob: c07e0d67b3925b8d7107acd4912923619513cb5a (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
/* 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/. */

/**
 * Form Autofill content process module.
 */

/* eslint-disable no-use-before-define */

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  FormAutofill: "resource://autofill/FormAutofill.sys.mjs",
  ProfileAutocomplete:
    "resource://autofill/AutofillProfileAutoComplete.sys.mjs",
});

const formFillController = Cc[
  "@mozilla.org/satchel/form-fill-controller;1"
].getService(Ci.nsIFormFillController);

/**
 * Handles content's interactions for the process.
 *
 */
export var FormAutofillContent = {
  /**
   * @type {Set} Set of the fields with usable values in any saved profile.
   */
  get savedFieldNames() {
    return Services.cpmm.sharedData.get("FormAutofill:savedFieldNames");
  },

  /**
   * @type {boolean} Flag indicating whether a focus action requiring
   * the popup to be active is pending.
   */
  _popupPending: false,

  init() {
    this.log = lazy.FormAutofill.defineLogGetter(this, "FormAutofillContent");
    this.debug("init");

    // eslint-disable-next-line mozilla/balanced-listeners
    Services.cpmm.sharedData.addEventListener("change", this);

    const autofillEnabled = Services.cpmm.sharedData.get(
      "FormAutofill:enabled"
    );
    // If storage hasn't be initialized yet autofillEnabled is undefined but we need to ensure
    // autocomplete is registered before the focusin so register it in this case as long as the
    // pref is true.
    const shouldEnableAutofill =
      autofillEnabled === undefined &&
      (lazy.FormAutofill.isAutofillAddressesEnabled ||
        lazy.FormAutofill.isAutofillCreditCardsEnabled);
    if (autofillEnabled || shouldEnableAutofill) {
      lazy.ProfileAutocomplete.ensureRegistered();
    }

    this.activeAutofillChild = null;
  },

  get activeFieldDetail() {
    return this.activeAutofillChild?.activeFieldDetail;
  },

  get activeFormDetails() {
    return this.activeAutofillChild?.activeFormDetails;
  },

  get activeInput() {
    return this.activeAutofillChild?.activeInput;
  },

  get activeHandler() {
    return this.activeAutofillChild?.activeHandler;
  },

  get activeSection() {
    return this.activeAutofillChild?.activeSection;
  },

  set autofillPending(flag) {
    if (this.activeAutofillChild) {
      this.activeAutofillChild.autofillPending = flag;
    }
  },

  updateActiveAutofillChild(autofillChild) {
    this.activeAutofillChild = autofillChild;
  },

  showPopup() {
    if (Services.cpmm.sharedData.get("FormAutofill:enabled")) {
      this.debug("updateActiveElement: opening pop up");
      formFillController.showPopup();
    } else {
      this.debug(
        "updateActiveElement: Deferring pop-up until Autofill is ready"
      );
      this._popupPending = true;
    }
  },

  handleEvent(evt) {
    switch (evt.type) {
      case "change": {
        if (!evt.changedKeys.includes("FormAutofill:enabled")) {
          return;
        }
        if (Services.cpmm.sharedData.get("FormAutofill:enabled")) {
          lazy.ProfileAutocomplete.ensureRegistered();
          if (this._popupPending) {
            this._popupPending = false;
            this.debug("handleEvent: Opening deferred popup");
            formFillController.showPopup();
          }
        } else {
          lazy.ProfileAutocomplete.ensureUnregistered();
        }
        break;
      }
    }
  },
};

FormAutofillContent.init();