summaryrefslogtreecommitdiffstats
path: root/mobile/android/chrome/geckoview/GeckoViewAutofillChild.js
blob: 9e668a8a99792779fefdc5949ff080d869344f93 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* 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/. */

const { GeckoViewChildModule } = ChromeUtils.import(
  "resource://gre/modules/GeckoViewChildModule.jsm"
);
var { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);
var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

XPCOMUtils.defineLazyModuleGetters(this, {
  FormLikeFactory: "resource://gre/modules/FormLikeFactory.jsm",
  GeckoViewAutofill: "resource://gre/modules/GeckoViewAutofill.jsm",
});

class GeckoViewAutofillChild extends GeckoViewChildModule {
  onInit() {
    debug`onInit`;

    // Listen to Gecko's autofill commit events.
    content.windowRoot.addEventListener(
      "PasswordManager:onFormSubmit",
      aEvent => {
        const formLike = aEvent.detail.form;
        this._autofill.commitAutofill(formLike);
      }
    );

    const options = {
      mozSystemGroup: true,
      capture: false,
    };

    addEventListener("DOMFormHasPassword", this, options);
    addEventListener("DOMInputPasswordAdded", this, options);
    addEventListener("pagehide", this, options);
    addEventListener("pageshow", this, options);
    addEventListener("focusin", this, options);
    addEventListener("focusout", this, options);

    XPCOMUtils.defineLazyGetter(
      this,
      "_autofill",
      () => new GeckoViewAutofill(this.eventDispatcher)
    );
  }

  onEnable() {
    debug`onEnable`;
  }

  onDisable() {
    debug`onDisable`;
  }

  // eslint-disable-next-line complexity
  handleEvent(aEvent) {
    debug`handleEvent: ${aEvent.type}`;

    switch (aEvent.type) {
      case "DOMFormHasPassword": {
        this._autofill.addElement(
          FormLikeFactory.createFromForm(aEvent.composedTarget)
        );
        break;
      }
      case "DOMInputPasswordAdded": {
        const input = aEvent.composedTarget;
        if (!input.form) {
          this._autofill.addElement(FormLikeFactory.createFromField(input));
        }
        break;
      }
      case "focusin": {
        if (aEvent.composedTarget instanceof content.HTMLInputElement) {
          this._autofill.onFocus(aEvent.composedTarget);
        }
        break;
      }
      case "focusout": {
        if (aEvent.composedTarget instanceof content.HTMLInputElement) {
          this._autofill.onFocus(null);
        }
        break;
      }
      case "pagehide": {
        if (aEvent.target === content.document) {
          this._autofill.clearElements();
        }
        break;
      }
      case "pageshow": {
        if (aEvent.target === content.document && aEvent.persisted) {
          this._autofill.scanDocument(aEvent.target);
        }
        break;
      }
    }
  }
}

const { debug, warn } = GeckoViewAutofillChild.initLogging("GeckoViewAutofill");
const module = GeckoViewAutofillChild.create(this);