content/base/test/test_child_process_shutdown_message.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 <!DOCTYPE html>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <meta charset="utf-8">
michael@0 5 <title>Test that processes that are shutdown send a 'process-shutdown'
michael@0 6 message to their process message manager.</title>
michael@0 7 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 9 </head>
michael@0 10 <body onload="runTests();">
michael@0 11 <p id="display">
michael@0 12 </p>
michael@0 13 <div id="content" style="display: none">
michael@0 14
michael@0 15 </div>
michael@0 16 <pre id="test">
michael@0 17 <script class="testbody" type="application/javascript;version=1.8">
michael@0 18
michael@0 19 const APP_URL = "http://example.org";
michael@0 20 const APP_MANIFEST = "http://example.org/manifest.webapp";
michael@0 21 const CHILD_PROCESS_SHUTDOWN_MESSAGE = "child-process-shutdown";
michael@0 22
michael@0 23 let ppmm = SpecialPowers.Cc["@mozilla.org/parentprocessmessagemanager;1"]
michael@0 24 .getService(SpecialPowers.Ci.nsIMessageBroadcaster);
michael@0 25 let obs = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
michael@0 26 .getService(SpecialPowers.Ci.nsIObserverService);
michael@0 27
michael@0 28 /**
michael@0 29 * Load the example.org site in an <iframe mozbrowser>
michael@0 30 *
michael@0 31 * @param isApp
michael@0 32 * If true, the example.org site will be loaded as an app.
michael@0 33 */
michael@0 34 function loadBrowser(isApp, callback) {
michael@0 35 let iframe = document.createElement("iframe");
michael@0 36 if (isApp) {
michael@0 37 iframe.setAttribute("mozapp", APP_MANIFEST);
michael@0 38 }
michael@0 39 SpecialPowers.wrap(iframe).mozbrowser = true;
michael@0 40 iframe.src = APP_URL;
michael@0 41 document.getElementById("content").appendChild(iframe);
michael@0 42
michael@0 43 iframe.addEventListener("mozbrowserloadend", function onloadend() {
michael@0 44 iframe.removeEventListener("mozbrowserloadend", onloadend);
michael@0 45 callback(iframe);
michael@0 46 });
michael@0 47 }
michael@0 48
michael@0 49 /**
michael@0 50 * Prepare the child process for an intentional crash. This is to keep
michael@0 51 * the leak automation tools happy.
michael@0 52 *
michael@0 53 * This also allows us to acquire the process message manaager that
michael@0 54 * corresponds to the process by sending a message to a frame script
michael@0 55 * in the content process and having it reply to us via the child
michael@0 56 * process message manager.
michael@0 57 */
michael@0 58 function prepareProcess(frameMM, callback) {
michael@0 59 let frameScript = 'data:,\
michael@0 60 privateNoteIntentionalCrash();\
michael@0 61 var cpmm = Components.classes["@mozilla.org/childprocessmessagemanager;1"]\
michael@0 62 .getService(Components.interfaces.nsISyncMessageSender);\
michael@0 63 addMessageListener("TestChild:Ohai", function receiveMessage(msg) {\
michael@0 64 cpmm.sendAsyncMessage("TestChild:Ohai");\
michael@0 65 });';
michael@0 66 frameMM.loadFrameScript(frameScript, false);
michael@0 67 frameMM.sendAsyncMessage("TestChild:Ohai");
michael@0 68 ppmm.addMessageListener("TestChild:Ohai", function receiveMessage(msg) {
michael@0 69 ppmm.removeMessageListener("TestChild:Ohai", receiveMessage);
michael@0 70 msg = SpecialPowers.wrap(msg);
michael@0 71 callback(msg.target);
michael@0 72 });
michael@0 73 }
michael@0 74
michael@0 75 /**
michael@0 76 * Expects an OOP frame's process to shut down and report four
michael@0 77 * events/messages: an error event on the browser element, and a
michael@0 78 * 'child-process-shutdown' message on both the frame and process
michael@0 79 * message managers.
michael@0 80 */
michael@0 81 function expectFrameProcessShutdown(iframe, frameMM, processMM, callback) {
michael@0 82 let msgCount = 0;
michael@0 83 function countMessage() {
michael@0 84 msgCount += 1;
michael@0 85 if (msgCount == 4) {
michael@0 86 ok(true, "Observed all four expected events.");
michael@0 87 callback();
michael@0 88 }
michael@0 89 };
michael@0 90
michael@0 91 iframe.addEventListener("mozbrowsererror", function onerror(event) {
michael@0 92 iframe.removeEventListener("mozbrowsererror", onerror);
michael@0 93 is(event.detail.type, "fatal", "Observed expected event.");
michael@0 94 countMessage();
michael@0 95 });
michael@0 96
michael@0 97 processMM.addMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, function receiveMessage() {
michael@0 98 processMM.removeMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, receiveMessage);
michael@0 99 ok(true, "Received 'child-process-shutdown' message from process message manager.");
michael@0 100 countMessage();
michael@0 101 });
michael@0 102
michael@0 103 frameMM.addMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, function receiveMessage() {
michael@0 104 frameMM.removeMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, receiveMessage);
michael@0 105 ok(true, "Received 'child-process-shutdown' message from frame message manager.");
michael@0 106 countMessage();
michael@0 107 });
michael@0 108
michael@0 109 obs.addObserver(function observe(subject, type, data) {
michael@0 110 if (subject == SpecialPowers.unwrap(processMM)) {
michael@0 111 obs.removeObserver(observe, "message-manager-disconnect");
michael@0 112 ok(true, "Received 'message-manager-disconnect' notification with " +
michael@0 113 "frame message manager");
michael@0 114 countMessage();
michael@0 115 }
michael@0 116 }, "message-manager-disconnect", false);
michael@0 117 }
michael@0 118
michael@0 119 function setUp() {
michael@0 120 SpecialPowers.setBoolPref("dom.mozBrowserFramesEnabled", true);
michael@0 121 SpecialPowers.setBoolPref("dom.ipc.browser_frames.oop_by_default", true);
michael@0 122 SpecialPowers.addPermission("browser", true, window.document);
michael@0 123 SpecialPowers.addPermission("embed-apps", true, window.document);
michael@0 124
michael@0 125 // TODO: remove in bug 820712
michael@0 126 SpecialPowers.setBoolPref("network.disable.ipc.security", true);
michael@0 127
michael@0 128 runNextTest();
michael@0 129 }
michael@0 130
michael@0 131 function makeKillTest(isApp) function testKill() {
michael@0 132 loadBrowser(isApp, function (iframe) {
michael@0 133 // We want to make sure we get notified on both the frame and
michael@0 134 // process message managers.
michael@0 135 let frameMM = SpecialPowers.getBrowserFrameMessageManager(iframe);
michael@0 136 prepareProcess(frameMM, function (processMM) {
michael@0 137 // Let's kill the content process by asking for a permission
michael@0 138 // that it doesn't have.
michael@0 139 ok(!processMM.assertPermission("frobnaz"),
michael@0 140 "Content child should not have this permission");
michael@0 141 expectFrameProcessShutdown(iframe, frameMM, processMM, function () {
michael@0 142 iframe.parentNode.removeChild(iframe);
michael@0 143 runNextTest();
michael@0 144 });
michael@0 145 });
michael@0 146 });
michael@0 147 }
michael@0 148
michael@0 149 function tearDown() {
michael@0 150 SpecialPowers.clearUserPref("dom.mozBrowserFramesEnabled");
michael@0 151 SpecialPowers.clearUserPref("dom.ipc.browser_frames.oop_by_default");
michael@0 152
michael@0 153 // TODO: remove in bug 820712
michael@0 154 SpecialPowers.clearUserPref("network.disable.ipc.security");
michael@0 155
michael@0 156 SimpleTest.finish();
michael@0 157 }
michael@0 158
michael@0 159 let _tests = [
michael@0 160 setUp,
michael@0 161 makeKillTest(false),
michael@0 162 makeKillTest(true),
michael@0 163 tearDown
michael@0 164 ]
michael@0 165 function runNextTest() {
michael@0 166 SimpleTest.executeSoon(_tests.shift());
michael@0 167 }
michael@0 168
michael@0 169 function runTests() {
michael@0 170 SimpleTest.waitForExplicitFinish();
michael@0 171 runNextTest();
michael@0 172 }
michael@0 173
michael@0 174 </script>
michael@0 175 </pre>
michael@0 176 </body>
michael@0 177 </html>

mercurial