summaryrefslogtreecommitdiffstats
path: root/testing/talos/talos/talos-powers/content/TalosPowersContent.js
blob: 93f9c735fccea9fd7e5563f7a68acba9149e7197 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* 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/. */

// This file should be executed by the [possibly unprivileged] consumer, e.g.:
// <script src="/scripts/TalosPowersContent.js"></script>
// and then e.g. TalosPowersParent.exec("sampleParentService", myArg, myCallback)
// It marely sends a query event and possibly listens to a reply event, and does not
// depend on any special privileges.

var TalosPowers;
var TalosPowersContent;
var TalosPowersParent;

(function() {
  // The talos powers chrome event/message listeners are set up
  // asynchronously during startup so attempts to use this code too early
  // may race against the extension initialization.  Code that has might
  // be vulnerable to that race can use `await TalosPowers.loadPromise;`
  // which waits until it can successfully exchange a simple "ping" message
  // with the extension.
  async function tryPing() {
    let pingPromise = new Promise(resolve => {
      TalosPowersParent.exec("ping", null, resolve);
    });
    let timeoutPromise = new Promise((resolve, reject) => {
      setTimeout(reject, 500);
    });

    try {
      await Promise.race([pingPromise, timeoutPromise]);
    } catch (e) {
      return tryPing();
    }

    return null;
  }

  TalosPowers = {};
  Object.defineProperty(TalosPowers, "loadPromise", {
    get: () => tryPing(),
    configurable: true,
    enumerable: true,
  });

  TalosPowersContent = {
    /**
     * Synchronously force CC and GC in this process, as well as in the
     * parent process.
     */
    forceCCAndGC() {
      var event = new CustomEvent("TalosPowersContentForceCCAndGC", {
        bubbles: true,
      });
      document.dispatchEvent(event);
    },

    focus(callback) {
      if (callback) {
        addEventListener("TalosPowersContentFocused", function focused() {
          removeEventListener("TalosPowersContentFocused", focused);
          callback();
        });
      }
      document.dispatchEvent(
        new CustomEvent("TalosPowersContentFocus", {
          bubbles: true,
        })
      );
    },

    getStartupInfo() {
      return new Promise(resolve => {
        var event = new CustomEvent("TalosPowersContentGetStartupInfo", {
          bubbles: true,
        });
        document.dispatchEvent(event);

        addEventListener(
          "TalosPowersContentGetStartupInfoResult",
          function onResult(e) {
            removeEventListener(
              "TalosPowersContentGetStartupInfoResult",
              onResult
            );
            resolve(e.detail);
          }
        );
      });
    },

    dumpConsole() {
      var event = new CustomEvent("TalosPowersContentDumpConsole", {
        bubbles: true,
      });
      document.dispatchEvent(event);
    },

    goQuitApplication(waitForStartupFinished) {
      var event = new CustomEvent("TalosPowersGoQuitApplication", {
        bubbles: true,
        detail: { waitForStartupFinished },
      });
      document.dispatchEvent(event);
    },

    wrCapture() {
      var event = new CustomEvent("TalosPowersWebRenderCapture", {
        bubbles: true,
      });
      document.dispatchEvent(event);
    },
  };

  /**
   * Generic interface to service functions which run at the parent process.
   */
  // If including this script proves too much touble, you may embed the following
  // code verbatim instead, and keep the copy up to date with its source here:
  TalosPowersParent = {
    replyId: 1,

    // dispatch an event to the framescript and register the result/callback event
    exec(commandName, arg, callback, opt_custom_window) {
      let win = opt_custom_window || window;
      let replyEvent = "TalosPowers:ParentExec:ReplyEvent:" + this.replyId++;
      if (callback) {
        win.addEventListener(
          replyEvent,
          function(e) {
            callback(e.detail);
          },
          { once: true }
        );
      }
      win.dispatchEvent(
        new win.CustomEvent("TalosPowers:ParentExec:QueryEvent", {
          bubbles: true,
          detail: {
            command: {
              name: commandName,
              data: arg,
            },
            listeningTo: replyEvent,
          },
        })
      );
    },
  };
  // End of possibly embedded code
})();