summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/mochitest/test_nukeContentWindow.html
blob: 0db8749b5928e3bd52be3f2ca563259c96d45956 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1322273
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1322273</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1322273">Mozilla Bug 1322273</a>

<iframe id="subframe"></iframe>

<script type="application/javascript">
"use strict";

function waitForWindowDestroyed(winID, callback) {
  let observer = {
    observe: function(subject, topic, data) {
      let id = subject.QueryInterface(SpecialPowers.Ci.nsISupportsPRUint64).data;
      if (id != winID) {
        return;
      }
      SpecialPowers.removeObserver(observer, "outer-window-nuked");
      SpecialPowers.executeSoon(callback);
    }
  };
  SpecialPowers.addObserver(observer, "outer-window-nuked");
}

add_task(async function() {
  let frame = $('subframe');
  frame.srcdoc = "foo";
  await new Promise(resolve => frame.addEventListener("load", resolve, {once: true}));

  let win = frame.contentWindow;
  let winID = SpecialPowers.wrap(win).docShell.outerWindowID;

  win.eval("obj = {}");
  win.obj.foo = {bar: "baz"};

  let obj = win.obj;

  let system = SpecialPowers.Services.scriptSecurityManager.getSystemPrincipal()
  let sandbox = SpecialPowers.Cu.Sandbox(system);

  sandbox.obj = obj;

  let isWrapperDead = SpecialPowers.Cu.evalInSandbox(`(${
      function isWrapperDead() {
        return Cu.isDeadWrapper(obj);
      }
    })`,
    sandbox);

  is(isWrapperDead(), false, "Sandbox wrapper for content window should not be dead");
  is(obj.foo.bar, "baz", "Content wrappers into and out of content window should be alive");

  // Remove the frame, which should nuke the content window.
  info("Remove the content frame");
  frame.remove();

  // Give the nuke wrappers task a chance to run.
  await new Promise(resolve => waitForWindowDestroyed(winID, resolve));

  is(isWrapperDead(), true, "Sandbox wrapper for content window should be dead");
  is(obj.foo.bar, "baz", "Content wrappers into and out of content window should be alive");
});
</script>

</body>
</html>