dom/ipc/tests/test_child_docshell.html
author adam.kolodko <adam.kolodko@hotmail.com>
Thu, 13 Dec 2018 00:43:55 +0000
changeset 450383 ee53b81938266fffd38972636e7f992989281048
parent 429695 3b1489be7f57607ec39de27f4bc9b4770d569109
child 450384 3bb8efd6f02e7f24d6ee5a590a8bcaf336140675
permissions -rw-r--r--
Bug 1508990 - Enable ESLint for dom/ipc/ (automatic changes) r=Standard8,mrbkap Differential Revision: https://phabricator.services.mozilla.com/D13887

<!DOCTYPE HTML>
<html>
<!--
-->
<head>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

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

SimpleTest.waitForExplicitFinish();
SpecialPowers.addPermission("browser", true, document);
SpecialPowers.pushPrefEnv({"set": [
  ["dom.mozBrowserFramesEnabled", true],
  ["network.disable.ipc.security", true],
  ["dom.ipc.tabs.disabled", false],
]}, function() {

  var iframe =  document.createElementNS("http://www.w3.org/1999/xhtml", "iframe");
  iframe.setAttribute("remote", "true");
  SpecialPowers.wrap(iframe).mozbrowser = true;
  document.documentElement.appendChild(iframe);

  var mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
  mm.addMessageListener("chromeEventHandler", function(msg) {
    msg = SpecialPowers.wrap(msg);
    var result = msg.json;
    is(result.processType, SpecialPowers.Ci.nsIXULRuntime.PROCESS_TYPE_CONTENT,
       "The frame script is running in a real distinct child process");
    ok(result.hasCorrectInterface,
       "docshell.chromeEventHandler has EventTarget interface");
  });


  mm.addMessageListener("DOMWindowCreatedReceived", function(msg) {
    msg = SpecialPowers.wrap(msg);
    ok(true, "the chrome event handler looks functional");
    var result = msg.json;
    ok(result.stableChromeEventHandler, "docShell.chromeEventHandler is stable");
    ok(result.iframeHasNewDocShell, "iframe spawns a new docShell");
    ok(result.iframeHasSameChromeEventHandler, "but iframe has the same chrome event handler");
    SimpleTest.finish();
  });
  // Inject a frame script in the child process:
  mm.loadFrameScript("data:,new " + function ContentScriptScope() {
    var processType = Cc["@mozilla.org/xre/runtime;1"]
                        .getService(Ci.nsIXULRuntime)
                        .processType;
    var chromeEventHandler = docShell.chromeEventHandler;
    sendAsyncMessage("chromeEventHandler", {
      processType: Services.appinfo.processType,
      hasCorrectInterface: chromeEventHandler &&
                           EventTarget.isInstance(chromeEventHandler),
    });

    /*
      Ensure that this chromeEventHandler actually works,
      by creating a new window and listening for its DOMWindowCreated event
    */
    chromeEventHandler.addEventListener("DOMWindowCreated", function listener(evt) {
      if (evt.target == content.document) {
        return;
      }
      chromeEventHandler.removeEventListener("DOMWindowCreated", listener);
      let new_win = evt.target.defaultView;
      let new_docShell = new_win.docShell;
      sendAsyncMessage("DOMWindowCreatedReceived", {
        stableChromeEventHandler: chromeEventHandler === docShell.chromeEventHandler,
        iframeHasNewDocShell: new_docShell !== docShell,
        iframeHasSameChromeEventHandler: new_docShell.chromeEventHandler === chromeEventHandler,
      });
    });

    function go() {
      let i = content.document.createElement("iframe");
      i.setAttribute("src", "data:text/html,foo");
      content.document.documentElement.appendChild(i);
    }

    if (content.document.readyState == "complete") {
      go();
    } else {
      addEventListener("load", go, { once: true, capture: true });
    }
  }, false);
});

</script>
</body>
</html>