summaryrefslogtreecommitdiffstats
path: root/mobile/android/actors/GeckoViewAutoFillParent.sys.mjs
blob: d7248d61feb9c07fd39c550b0482e75150a87899 (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
/* 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 { GeckoViewActorParent } from "resource://gre/modules/GeckoViewActorParent.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  gAutofillManager: "resource://gre/modules/GeckoViewAutofill.sys.mjs",
});

export class GeckoViewAutoFillParent extends GeckoViewActorParent {
  constructor() {
    super();
    this.sessionId = Services.uuid.generateUUID().toString().slice(1, -1); // discard the surrounding curly braces
  }

  get rootActor() {
    return this.browsingContext.top.currentWindowGlobal.getActor(
      "GeckoViewAutoFill"
    );
  }

  get autofill() {
    return lazy.gAutofillManager.get(this.sessionId);
  }

  add(node) {
    // We will start a new session if the current one does not exist.
    const autofill = lazy.gAutofillManager.ensure(
      this.sessionId,
      this.eventDispatcher
    );
    return autofill?.add(node);
  }

  focus(node) {
    this.autofill?.focus(node);
  }

  commit(node) {
    this.autofill?.commit(node);
  }

  update(node) {
    this.autofill?.update(node);
  }

  clear() {
    lazy.gAutofillManager.delete(this.sessionId);
  }

  async receiveMessage(aMessage) {
    const { name } = aMessage;
    debug`receiveMessage ${name}`;

    // We need to re-route all messages through the root actor to ensure that we
    // have a consistent sessionId for the entire browsingContext tree.
    switch (name) {
      case "Add": {
        return this.rootActor.add(aMessage.data.node);
      }
      case "Focus": {
        this.rootActor.focus(aMessage.data.node);
        break;
      }
      case "Update": {
        this.rootActor.update(aMessage.data.node);
        break;
      }
      case "Commit": {
        this.rootActor.commit(aMessage.data.node);
        break;
      }
      case "Clear": {
        if (this.browsingContext === this.browsingContext.top) {
          this.clear();
        }
        break;
      }
    }

    return null;
  }
}

const { debug, warn } =
  GeckoViewAutoFillParent.initLogging("GeckoViewAutoFill");