summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/FormHistoryParent.sys.mjs
blob: 8fe943566e570ff8bafafa24e1698cd61e618c9d (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
/* 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/. */

import { FirefoxRelayTelemetry } from "resource://gre/modules/FirefoxRelayTelemetry.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  FirefoxRelay: "resource://gre/modules/FirefoxRelay.sys.mjs",
  FormHistory: "resource://gre/modules/FormHistory.sys.mjs",
  LoginHelper: "resource://gre/modules/LoginHelper.sys.mjs",
});

export class FormHistoryParent extends JSWindowActorParent {
  receiveMessage({ name, data }) {
    switch (name) {
      case "FormHistory:FormSubmitEntries":
        this.#onFormSubmitEntries(data);
        break;

      case "FormHistory:AutoCompleteSearchAsync":
        return this.#onAutoCompleteSearch(data);

      case "FormHistory:RemoveEntry":
        this.#onRemoveEntry(data);
        break;

      case "PasswordManager:offerRelayIntegration": {
        FirefoxRelayTelemetry.recordRelayOfferedEvent(
          "clicked",
          data.telemetry.flowId,
          data.telemetry.scenarioName
        );
        return this.#offerRelayIntegration();
      }

      case "PasswordManager:generateRelayUsername": {
        FirefoxRelayTelemetry.recordRelayUsernameFilledEvent(
          "clicked",
          data.telemetry.flowId
        );
        return this.#generateRelayUsername();
      }
    }

    return undefined;
  }

  #onFormSubmitEntries(entries) {
    const changes = entries.map(entry => ({
      op: "bump",
      fieldname: entry.name,
      value: entry.value,
    }));

    lazy.FormHistory.update(changes);
  }

  get formOrigin() {
    return lazy.LoginHelper.getLoginOrigin(
      this.manager.documentPrincipal?.originNoSuffix
    );
  }

  async #onAutoCompleteSearch({ searchString, params, scenarioName }) {
    const formHistoryPromise = lazy.FormHistory.getAutoCompleteResults(
      searchString,
      params
    );

    const relayPromise = lazy.FirefoxRelay.autocompleteItemsAsync({
      formOrigin: this.formOrigin,
      scenarioName,
      hasInput: !!searchString.length,
    });
    const [formHistoryEntries, externalEntries] = await Promise.all([
      formHistoryPromise,
      relayPromise,
    ]);

    return { formHistoryEntries, externalEntries };
  }

  #onRemoveEntry({ inputName, value, guid }) {
    lazy.FormHistory.update({
      op: "remove",
      fieldname: inputName,
      value,
      guid,
    });
  }

  getRootBrowser() {
    return this.browsingContext.topFrameElement;
  }

  async #offerRelayIntegration() {
    const browser = this.getRootBrowser();
    return lazy.FirefoxRelay.offerRelayIntegration(browser, this.formOrigin);
  }

  async #generateRelayUsername() {
    const browser = this.getRootBrowser();
    return lazy.FirefoxRelay.generateUsername(browser, this.formOrigin);
  }
}