summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg-chrome-debugging.js
blob: 71393ae32ac1dbf97dcf67811e365738963cfa3e (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
/* 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/>. */

/**
 * Tests that chrome debugging works.
 */

add_task(async function() {
  const client = initDevToolsClient();

  const [type] = await client.connect();
  is(type, "browser", "Root actor should identify itself as a browser.");

  const descriptorFront = await client.mainRoot.getMainProcess();
  const front = await descriptorFront.getTarget();
  await front.attach();
  const threadFront = await front.attachThread();

  gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, "about:mozilla");

  // listen for a new source and global
  const onFooDotComNewSource = new Promise(resolve => {
    function onNewSource(packet) {
      if (packet.source.url == "http://foo.com/") {
        threadFront.off("newSource", onNewSource);
        resolve(packet);
      }
    }
    threadFront.on("newSource", onNewSource);
  });

  // Force the creation of a new privileged source
  const systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].createInstance(
    Ci.nsIPrincipal
  );
  const sandbox = Cu.Sandbox(systemPrincipal);
  Cu.evalInSandbox("function foo() {}", sandbox, null, "http://foo.com");

  const packet = await onFooDotComNewSource;
  ok(true, "Received the custom script source: " + packet.source.url);

  await client.close();
});

function initDevToolsClient() {
  const { DevToolsLoader } = ChromeUtils.import(
    "resource://devtools/shared/Loader.jsm"
  );
  const customLoader = new DevToolsLoader({
    invisibleToDebugger: true,
  });
  const { DevToolsServer } = customLoader.require(
    "devtools/server/devtools-server"
  );
  const { DevToolsClient } = require("devtools/client/devtools-client");

  DevToolsServer.init();
  DevToolsServer.registerAllActors();
  DevToolsServer.allowChromeProcess = true;

  const transport = DevToolsServer.connectPipe();
  return new DevToolsClient(transport);
}