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

import { sendFillRequestToParent } from "resource://gre/modules/FillHelpers.sys.mjs";

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

const lazy = {};

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

const autocompleteController = Cc[
  "@mozilla.org/autocomplete/controller;1"
].getService(Ci.nsIAutoCompleteController);

function getActorFromWindow(contentWindow, name = "FormAutofill") {
  // In unit tests, contentWindow isn't a real window.
  if (!contentWindow) {
    return null;
  }

  return contentWindow.windowGlobalChild
    ? contentWindow.windowGlobalChild.getActor(name)
    : null;
}

export const ProfileAutocomplete = {
  QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),

  _registered: false,

  ensureRegistered() {
    if (this._registered) {
      return;
    }

    this.log = lazy.FormAutofill.defineLogGetter(this, "ProfileAutocomplete");
    this.debug("ensureRegistered");
    this._registered = true;

    Services.obs.addObserver(this, "autocomplete-will-enter-text");

    this.debug(
      "ensureRegistered. Finished with _registered:",
      this._registered
    );
  },

  ensureUnregistered() {
    if (!this._registered) {
      return;
    }

    this.debug("ensureUnregistered");
    this._registered = false;

    Services.obs.removeObserver(this, "autocomplete-will-enter-text");
  },

  async observe(_subject, topic, _data) {
    switch (topic) {
      case "autocomplete-will-enter-text": {
        if (!lazy.FormAutofillContent.activeInput) {
          // The observer notification is for autocomplete in a different process.
          break;
        }
        await this._fillFromAutocompleteRow(
          lazy.FormAutofillContent.activeInput
        );
        break;
      }
    }
  },

  async _fillFromAutocompleteRow(focusedInput) {
    this.debug("_fillFromAutocompleteRow:", focusedInput);
    let formDetails = lazy.FormAutofillContent.activeFormDetails;
    if (!formDetails) {
      // The observer notification is for a different frame.
      return;
    }
    const actor = getActorFromWindow(focusedInput.ownerGlobal, "AutoComplete");
    if (!actor) {
      throw new Error("Invalid autocomplete selectedIndex");
    }

    const selectedIndex = actor.selectedIndex;
    const lastProfileAutoCompleteResult = actor.lastProfileAutoCompleteResult;
    const validIndex =
      selectedIndex >= 0 &&
      selectedIndex < lastProfileAutoCompleteResult?.matchCount;
    const comment = validIndex
      ? lastProfileAutoCompleteResult.getCommentAt(selectedIndex)
      : null;

    let profile = JSON.parse(comment);
    if (
      selectedIndex == -1 ||
      lastProfileAutoCompleteResult?.getStyleAt(selectedIndex) != "autofill"
    ) {
      if (
        focusedInput &&
        focusedInput == autocompleteController?.input.focusedInput
      ) {
        if (profile?.fillMessageName == "FormAutofill:ClearForm") {
          // The child can do this directly.
          getActorFromWindow(focusedInput.ownerGlobal)?.clearForm();
        } else {
          // Pass focusedInput as both input arguments.
          await sendFillRequestToParent(
            "FormAutofill",
            autocompleteController.input,
            comment
          );
        }
      }
    }
  },
};