summaryrefslogtreecommitdiffstats
path: root/mobile/android/actors/SelectionActionDelegateParent.jsm
blob: 5791fdec41b7347169a04dcf8635deebe096d800 (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
/* 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";

var EXPORTED_SYMBOLS = ["SelectionActionDelegateParent"];

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

class SelectionActionDelegateParent extends GeckoViewActorParent {
  respondTo = null;
  actionId = null;

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

  receiveMessage(aMessage) {
    const { data, name } = aMessage;
    switch (name) {
      case "ShowSelectionAction": {
        this.rootActor.showSelectionAction(this, data);
        break;
      }

      case "HideSelectionAction": {
        this.rootActor.hideSelectionAction(this, data.reason);
        break;
      }

      default: {
        super.receiveMessage(aMessage);
      }
    }
  }

  hideSelectionAction(aRespondTo, reason) {
    // Mark previous actions as stale. Don't do this for "invisibleselection"
    // or "scroll" because previous actions should still be valid even after
    // these events occur.
    if (reason !== "invisibleselection" && reason !== "scroll") {
      this.actionId = null;
    }

    this.eventDispatcher?.sendRequest({
      type: "GeckoView:HideSelectionAction",
      reason,
    });
  }

  showSelectionAction(aRespondTo, aData) {
    this.actionId = Services.uuid.generateUUID().toString();
    this.respondTo = aRespondTo;

    this.eventDispatcher?.sendRequest({
      type: "GeckoView:ShowSelectionAction",
      actionId: this.actionId,
      ...aData,
    });
  }

  executeSelectionAction(aData) {
    if (this.actionId === null || aData.actionId != this.actionId) {
      warn`Stale response ${aData.id} ${aData.actionId}`;
      return;
    }
    this.respondTo.sendAsyncMessage("ExecuteSelectionAction", aData);
  }
}

const { debug, warn } = SelectionActionDelegateParent.initLogging(
  "SelectionActionDelegate"
);