summaryrefslogtreecommitdiffstats
path: root/mobile/android/modules/geckoview/GeckoViewAutofill.jsm
blob: b7d0aa39f9054d26da460d889fd1128d977caf93 (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
/* 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/. */
"use strict";

const { GeckoViewUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/GeckoViewUtils.sys.mjs"
);

var EXPORTED_SYMBOLS = ["gAutofillManager"];

class Autofill {
  constructor(sessionId, eventDispatcher) {
    this.eventDispatcher = eventDispatcher;
    this.sessionId = sessionId;
  }

  start() {
    this.eventDispatcher.sendRequest({
      type: "GeckoView:StartAutofill",
      sessionId: this.sessionId,
    });
  }

  add(node) {
    return this.eventDispatcher.sendRequestForResult({
      type: "GeckoView:AddAutofill",
      node,
    });
  }

  focus(node) {
    this.eventDispatcher.sendRequest({
      type: "GeckoView:OnAutofillFocus",
      node,
    });
  }

  update(node) {
    this.eventDispatcher.sendRequest({
      type: "GeckoView:UpdateAutofill",
      node,
    });
  }

  commit(node) {
    this.eventDispatcher.sendRequest({
      type: "GeckoView:CommitAutofill",
      node,
    });
  }

  clear() {
    this.eventDispatcher.sendRequest({
      type: "GeckoView:ClearAutofill",
    });
  }
}

class AutofillManager {
  sessions = new Set();
  autofill = null;

  ensure(sessionId, eventDispatcher) {
    if (!this.sessions.has(sessionId)) {
      this.autofill = new Autofill(sessionId, eventDispatcher);
      this.sessions.add(sessionId);
      this.autofill.start();
    }
    // This could be called for an outdated session, in which case we will just
    // ignore the autofill call.
    if (sessionId !== this.autofill.sessionId) {
      return null;
    }
    return this.autofill;
  }

  get(sessionId) {
    if (!this.autofill || sessionId !== this.autofill.sessionId) {
      warn`Disregarding old session ${sessionId}`;
      // We disregard old sessions
      return null;
    }
    return this.autofill;
  }

  delete(sessionId) {
    this.sessions.delete(sessionId);
    if (!this.autofill || sessionId !== this.autofill.sessionId) {
      // this delete call might happen *after* the next session already
      // started, in that case, we can safely ignore this call.
      return;
    }
    this.autofill.clear();
    this.autofill = null;
  }
}

var gAutofillManager = new AutofillManager();

const { debug, warn } = GeckoViewUtils.initLogging("Autofill");