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
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test for the nsIProcessChecker part of Message Managers</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="runTests();">
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.8">
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = SpecialPowers.wrap(Components);
const APP_URL = "http://example.org";
const APP_MANIFEST = "http://example.org/manifest.webapp";
const CHILD_PROCESS_SHUTDOWN_MESSAGE = "child-process-shutdown";
let ppmm = Cc["@mozilla.org/parentprocessmessagemanager;1"]
.getService(Ci.nsIMessageBroadcaster);
let cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
.getService(Ci.nsISyncMessageSender);
let gAppsService = Cc["@mozilla.org/AppsService;1"]
.getService(Ci.nsIAppsService);
function setUp() {
SpecialPowers.setBoolPref("dom.mozBrowserFramesEnabled", true);
SpecialPowers.setBoolPref("dom.ipc.browser_frames.oop_by_default", true);
SpecialPowers.addPermission("browser", true, window.document);
SpecialPowers.addPermission("embed-apps", true, window.document);
let appId = gAppsService.getAppLocalIdByManifestURL(APP_MANIFEST);
SpecialPowers.addPermission("foobar", true, { url: APP_URL,
appId: appId,
isInBrowserElement: false });
runNextTest();
}
/**
* Load the example.org app in an <iframe mozbrowser mozapp>
*/
function loadApp(callback) {
let iframe = document.createElement("iframe");
iframe.setAttribute("mozapp", APP_MANIFEST);
iframe.mozbrowser = true;
iframe.src = APP_URL;
document.getElementById("content").appendChild(iframe);
iframe.addEventListener("mozbrowserloadend", function onloadend() {
iframe.removeEventListener("mozbrowserloadend", onloadend);
callback(iframe);
});
}
/**
* Prepare the child process for an intentional crash. This is to keep
* the leak automation tools happy.
*
* This also allows us to acquire the process message manaager that
* corresponds to the process by sending a message to a frame script
* in the content process and having it reply to us via the child
* process message manager.
*/
function prepareProcess(frameMM, callback) {
let frameScript = 'data:,\
privateNoteIntentionalCrash();\
var cpmm = Components.classes["@mozilla.org/childprocessmessagemanager;1"]\
.getService(Components.interfaces.nsISyncMessageSender);\
addMessageListener("TestChild:Ohai", function receiveMessage(msg) {\
cpmm.sendAsyncMessage("TestChild:Ohai");\
});';
frameMM.loadFrameScript(frameScript, false);
frameMM.sendAsyncMessage("TestChild:Ohai");
ppmm.addMessageListener("TestChild:Ohai", function receiveMessage(msg) {
ppmm.removeMessageListener("TestChild:Ohai", receiveMessage);
msg = SpecialPowers.wrap(msg);
callback(msg.target);
});
}
/**
* Expects an OOP frame's process to shut down and report three
* events/messages: an error event on the browser element, and a
* 'child-process-shutdown' message on both the frame and process
* message managers.
*/
function expectFrameProcessShutdown(iframe, frameMM, processMM, callback) {
let msgCount = 0;
function countMessage() {
msgCount += 1;
if (msgCount == 3) {
ok(true, "Observed all three expected events.");
callback();
}
};
iframe.addEventListener("mozbrowsererror", function onerror(event) {
iframe.removeEventListener("mozbrowsererror", onerror);
is(event.detail.type, "fatal", "Observed expected event.");
countMessage();
});
processMM.addMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, function receiveMessage() {
processMM.removeMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, receiveMessage);
ok(true, "Received 'child-process-shutdown' message from process message manager.");
countMessage();
});
frameMM.addMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, function receiveMessage() {
frameMM.removeMessageListener(CHILD_PROCESS_SHUTDOWN_MESSAGE, receiveMessage);
ok(true, "Received 'child-process-shutdown' message from frame message manager.");
countMessage();
});
}
function testSameProcess() {
// Assert permissions on the in-process child process message manager.
// It always has all permissions, including ones that were never
// assigned to anybody.
cpmm.sendAsyncMessage("TestPermission:InProcess");
ppmm.addMessageListener("TestPermission:InProcess", function receiveMessage(msg) {
ppmm.removeMessageListener("TestPermission:InProcess", receiveMessage);
msg = SpecialPowers.wrap(msg);
ok(msg.target.assertPermission("frobnaz"), "in-process cpmm always has all capabilities");
runNextTest();
});
}
function testFrameMessageManager() {
// Assert permissions on the frame message manager.
loadApp(function (iframe) {
let frameMM = SpecialPowers.getBrowserFrameMessageManager(iframe);
prepareProcess(frameMM, function (processMM) {
ok(frameMM.assertPermission("foobar"),
"Frame mm has assigned permission.");
ok(!frameMM.assertPermission("frobnaz"),
"Frame mm doesn't have non-existing permission.");
expectFrameProcessShutdown(iframe, frameMM, processMM, function () {
iframe.parentNode.removeChild(iframe);
runNextTest();
});
});
});
}
function testChildProcessMessageManager() {
// Assert permissions on the child process message manager.
loadApp(function (iframe) {
let frameMM = SpecialPowers.getBrowserFrameMessageManager(iframe);
prepareProcess(frameMM, function (processMM) {
ok(processMM.assertPermission("foobar"),
"Process mm has assigned permission.");
ok(!processMM.assertPermission("frobnaz"),
"Process mm doesn't have non-existing permission.");
expectFrameProcessShutdown(iframe, frameMM, processMM, function () {
iframe.parentNode.removeChild(iframe);
runNextTest();
});
});
});
}
function tearDown() {
SpecialPowers.clearUserPref("dom.mozBrowserFramesEnabled");
SpecialPowers.clearUserPref("dom.ipc.browser_frames.oop_by_default");
SimpleTest.finish();
}
let _tests = [
setUp,
testSameProcess,
testFrameMessageManager,
testChildProcessMessageManager,
tearDown
]
function runNextTest() {
SimpleTest.executeSoon(_tests.shift());
}
function runTests() {
SimpleTest.waitForExplicitFinish();
runNextTest();
}
</script>
</pre>
</body>
</html>
|