summaryrefslogtreecommitdiffstats
path: root/testing/talos/talos/talos-powers/chrome/talos-powers-content.js
blob: 4b7276e7a59ad5709ff590dec76d1db1fab601e3 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* 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/. */

/* eslint-env mozilla/frame-script */

addEventListener(
  "TalosContentProfilerCommand",
  e => {
    let name = e.detail.name;
    let data = e.detail.data;
    sendAsyncMessage("TalosContentProfiler:Command", { name, data });
  },
  { wantUntrusted: true } // since we're exposing to unprivileged
);

addMessageListener("TalosContentProfiler:Response", msg => {
  let name = msg.data.name;
  let data = msg.data.data;

  let event = Cu.cloneInto(
    {
      bubbles: true,
      detail: {
        name,
        data,
      },
    },
    content
  );
  content.dispatchEvent(
    new content.CustomEvent("TalosContentProfilerResponse", event)
  );
});

addEventListener(
  "TalosPowersContentForceCCAndGC",
  e => {
    Cu.forceGC();
    Cu.forceCC();
    Cu.forceShrinkingGC();
    sendSyncMessage("TalosPowersContent:ForceCCAndGC");
  },
  { wantUntrusted: true } // since we're exposing to unprivileged
);

addEventListener(
  "TalosPowersContentFocus",
  e => {
    if (
      content.location.protocol != "file:" &&
      content.location.hostname != "localhost" &&
      content.location.hostname != "127.0.0.1"
    ) {
      throw new Error(
        "TalosPowersContentFocus may only be used with local content"
      );
    }
    content.focus();
    let contentEvent = Cu.cloneInto(
      {
        bubbles: true,
      },
      content
    );
    content.dispatchEvent(
      new content.CustomEvent("TalosPowersContentFocused", contentEvent)
    );
  },
  { capture: true, wantUntrusted: true } // since we're exposing to unprivileged
);

addEventListener(
  "TalosPowersContentGetStartupInfo",
  e => {
    sendAsyncMessage("TalosPowersContent:GetStartupInfo");
    addMessageListener(
      "TalosPowersContent:GetStartupInfo:Result",
      function onResult(msg) {
        removeMessageListener(
          "TalosPowersContent:GetStartupInfo:Result",
          onResult
        );
        let event = Cu.cloneInto(
          {
            bubbles: true,
            detail: msg.data,
          },
          content
        );

        content.dispatchEvent(
          new content.CustomEvent(
            "TalosPowersContentGetStartupInfoResult",
            event
          )
        );
      }
    );
  },
  { wantUntrusted: true } // since we're exposing to unprivileged
);

addEventListener(
  "TalosPowersContentDumpConsole",
  e => {
    var messages;
    try {
      messages = Services.console.getMessageArray();
    } catch (ex) {
      dump(ex + "\n");
      messages = [];
    }

    for (var i = 0; i < messages.length; i++) {
      dump(messages[i].message + "\n");
    }
  },
  { wantUntrusted: true } // since we're exposing to unprivileged
);

/**
 * Content that wants to quit the whole session should
 * fire the TalosPowersGoQuitApplication custom event. This will
 * attempt to force-quit the browser.
 */
addEventListener(
  "TalosPowersGoQuitApplication",
  e => {
    // If we're loaded in a low-priority background process, like
    // the background page thumbnailer, then we shouldn't be allowed
    // to quit the whole application. This is a workaround until
    // bug 1164459 is fixed.
    let priority = docShell
      .QueryInterface(Ci.nsIDocumentLoader)
      .loadGroup.QueryInterface(Ci.nsISupportsPriority).priority;
    if (priority != Ci.nsISupportsPriority.PRIORITY_LOWEST) {
      sendAsyncMessage("Talos:ForceQuit", e.detail);
    }
  },
  { wantUntrusted: true } // since we're exposing to unprivileged
);

/**
 * Content that wants to trigger a WebRender capture should fire the
 * TalosPowersWebRenderCapture custom event.
 */
addEventListener(
  "TalosPowersWebRenderCapture",
  e => {
    if (content && content.windowUtils) {
      content.windowUtils.wrCapture();
    } else {
      dump("Unable to obtain DOMWindowUtils for TalosPowersWebRenderCapture\n");
    }
  },
  { wantUntrusted: true } // since we're exposing to unprivileged
);

/* *
 * Mediator for the generic ParentExec mechanism.
 * Listens for a query event from the content, forwards it as a query message
 * to the parent, listens to a parent reply message, and forwards it as a reply
 * event for the content to capture.
 * The consumer API for this mechanism is at content/TalosPowersContent.js
 * and the callees are at ParentExecServices at components/TalosPowersService.js
 */
addEventListener(
  "TalosPowers:ParentExec:QueryEvent",
  function (e) {
    if (
      content.location.protocol != "file:" &&
      content.location.hostname != "localhost" &&
      content.location.hostname != "127.0.0.1"
    ) {
      throw new Error(
        "TalosPowers:ParentExec may only be used with local content"
      );
    }
    let uniqueMessageId =
      "TalosPowers:ParentExec:" +
      content.document.documentURI +
      content.window.performance.now() +
      Math.random();

    // Listener for the reply from the parent process
    addMessageListener("TalosPowers:ParentExec:ReplyMsg", function done(reply) {
      if (reply.data.id != uniqueMessageId) {
        return;
      }

      removeMessageListener("TalosPowers:ParentExec:ReplyMsg", done);

      // reply to content via an event
      let contentEvent = Cu.cloneInto(
        {
          bubbles: true,
          detail: reply.data.result,
        },
        content
      );
      content.dispatchEvent(
        new content.CustomEvent(e.detail.listeningTo, contentEvent)
      );
    });

    // Send the query to the parent process
    sendAsyncMessage("TalosPowers:ParentExec:QueryMsg", {
      command: e.detail.command,
      id: uniqueMessageId,
    });
  },
  { wantUntrusted: true } // since we're exposing to unprivileged
);