Bug 677638 - MessageChannel and MessagePort disabled by pref, r=smaug

This commit is contained in:
Andrea Marchesini 2013-09-03 14:39:03 +02:00
Родитель 99339eb328
Коммит 177b65d83a
14 изменённых файлов: 227 добавлений и 144 удалений

Просмотреть файл

@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "MessageChannel.h"
#include "mozilla/Preferences.h"
#include "mozilla/dom/MessageChannelBinding.h"
#include "mozilla/dom/MessagePort.h"
#include "nsContentUtils.h"
@ -21,6 +22,24 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MessageChannel)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
namespace {
bool gPrefInitialized = false;
bool gPrefEnabled = false;
}
/* static */ bool
MessageChannel::PrefEnabled()
{
if (!gPrefInitialized) {
Preferences::AddBoolVarCache(&gPrefEnabled, "dom.messageChannel.enabled");
gPrefInitialized = true;
}
return gPrefEnabled;
}
MessageChannel::MessageChannel(nsPIDOMWindow* aWindow)
: mWindow(aWindow)
{

Просмотреть файл

@ -28,6 +28,8 @@ public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MessageChannel)
static bool PrefEnabled();
public:
MessageChannel(nsPIDOMWindow* aWindow);

Просмотреть файл

@ -189,6 +189,7 @@
#include "prenv.h"
#include "prprf.h"
#include "mozilla/dom/MessageChannel.h"
#include "mozilla/dom/MessagePort.h"
#include "mozilla/dom/MessagePortBinding.h"
#include "mozilla/dom/indexedDB/IDBFactory.h"
@ -6782,7 +6783,7 @@ PostMessageReadStructuredClone(JSContext* cx,
}
}
if (tag == SCTAG_DOM_MESSAGEPORT) {
if (MessageChannel::PrefEnabled() && tag == SCTAG_DOM_MESSAGEPORT) {
NS_ASSERTION(!data, "Data should be empty");
MessagePort* port;
@ -6837,14 +6838,16 @@ PostMessageWriteStructuredClone(JSContext* cx,
scInfo->event->StoreISupports(supports);
}
MessagePort* port = nullptr;
nsresult rv = UNWRAP_OBJECT(MessagePort, cx, obj, port);
if (NS_SUCCEEDED(rv) && scInfo->subsumes) {
nsRefPtr<MessagePort> newPort = port->Clone(scInfo->window);
if (MessageChannel::PrefEnabled()) {
MessagePort* port = nullptr;
nsresult rv = UNWRAP_OBJECT(MessagePort, cx, obj, port);
if (NS_SUCCEEDED(rv) && scInfo->subsumes) {
nsRefPtr<MessagePort> newPort = port->Clone(scInfo->window);
return JS_WriteUint32Pair(writer, SCTAG_DOM_MESSAGEPORT, 0) &&
JS_WriteBytes(writer, &newPort, sizeof(newPort)) &&
scInfo->event->StoreISupports(newPort);
return JS_WriteUint32Pair(writer, SCTAG_DOM_MESSAGEPORT, 0) &&
JS_WriteBytes(writer, &newPort, sizeof(newPort)) &&
scInfo->event->StoreISupports(newPort);
}
}
const JSStructuredCloneCallbacks* runtimeCallbacks =

Просмотреть файл

@ -41,6 +41,7 @@ MOCHITEST_FILES = \
test_messageChannel_pingpong.html \
iframe_messageChannel_pingpong.html \
test_messageChannel_unshipped.html \
test_messageChannel_pref.html \
$(NULL)
MOCHITEST_CHROME_FILES = \

Просмотреть файл

@ -21,23 +21,29 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=677638
</pre>
<script type="application/javascript">
/** Test for Bug 677638 **/
var a = new MessageChannel();
ok(a, "MessageChannel created");
function runTest() {
/** Test for Bug 677638 **/
var a = new MessageChannel();
ok(a, "MessageChannel created");
var port1 = a.port1;
ok(port1, "MessageChannel.port1 exists");
is(port1, a.port1, "MessageChannel.port1 is port1");
var port1 = a.port1;
ok(port1, "MessageChannel.port1 exists");
is(port1, a.port1, "MessageChannel.port1 is port1");
var port2 = a.port2;
ok(port2, "MessageChannel.port1 exists");
is(port2, a.port2, "MessageChannel.port2 is port2");
var port2 = a.port2;
ok(port2, "MessageChannel.port1 exists");
is(port2, a.port2, "MessageChannel.port2 is port2");
[ 'postMessage', 'start', 'close' ].forEach(function(e) {
ok(e in port1, "MessagePort1." + e + " exists");
ok(e in port2, "MessagePort2." + e + " exists");
});
[ 'postMessage', 'start', 'close' ].forEach(function(e) {
ok(e in port1, "MessagePort1." + e + " exists");
ok(e in port2, "MessagePort2." + e + " exists");
});
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, runTest);
</script>
</body>
</html>

Просмотреть файл

@ -64,7 +64,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=677638
}
SimpleTest.waitForExplicitFinish();
runTest();
SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, runTest);
</script>
</body>
</html>

Просмотреть файл

@ -16,59 +16,62 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=677638
</pre>
<script type="application/javascript">
var MAX = 100;
function runTest() {
var MAX = 100;
var a = new MessageChannel();
ok(a, "MessageChannel created");
var a = new MessageChannel();
ok(a, "MessageChannel created");
// Populate the message queue of this port.
for (var i = 0; i < MAX; ++i) {
a.port1.postMessage(i);
}
// Populate the message queue of this port.
for (var i = 0; i < MAX; ++i) {
a.port1.postMessage(i);
}
window.addEventListener('message', receiveMessage, false);
function receiveMessage(evt) {
window.addEventListener('message', receiveMessage, false);
function receiveMessage(evt) {
// This test sends the port from this window to the iframe and viceversa.
if (evt.data.type == 'PORT') {
var port = evt.data.port;
var counter = 0;
port.onmessage = function(evt) {
// only 1 message should be received by this port.
if (counter++ == 0) {
ok(evt.data % 2, "The number " + evt.data + " has been received correctly by the main window");
// This test sends the port from this window to the iframe and viceversa.
if (evt.data.type == 'PORT') {
var port = evt.data.port;
var counter = 0;
port.onmessage = function(evt) {
// only 1 message should be received by this port.
if (counter++ == 0) {
ok(evt.data % 2, "The number " + evt.data + " has been received correctly by the main window");
if (evt.data < MAX - 1) {
ifr.contentWindow.postMessage({ type: 'PORT', port: port }, '*');
if (evt.data < MAX - 1) {
ifr.contentWindow.postMessage({ type: 'PORT', port: port }, '*');
} else {
SimpleTest.finish();
}
} else {
SimpleTest.finish();
ok(false, "Wrong message!");
}
} else {
ok(false, "Wrong message!");
}
} else if (evt.data.type == 'OK') {
ok(true, evt.data.msg);
} else if (evt.data.type == 'KO') {
ok(false, evt.data.msg);
} else {
ok(false, "Unknown message");
}
} else if (evt.data.type == 'OK') {
ok(true, evt.data.msg);
} else if (evt.data.type == 'KO') {
ok(false, evt.data.msg);
} else {
ok(false, "Unknown message");
}
var div = document.getElementById("content");
ok(div, "Parent exists");
var ifr = document.createElement("iframe");
ifr.addEventListener("load", iframeLoaded, false);
ifr.setAttribute('src', "iframe_messageChannel_pingpong.html");
div.appendChild(ifr);
function iframeLoaded() {
ifr.contentWindow.postMessage({ type: 'PORT', port: a.port2 }, '*');
}
}
var div = document.getElementById("content");
ok(div, "Parent exists");
var ifr = document.createElement("iframe");
ifr.addEventListener("load", iframeLoaded, false);
ifr.setAttribute('src', "iframe_messageChannel_pingpong.html");
div.appendChild(ifr);
function iframeLoaded() {
ifr.contentWindow.postMessage({ type: 'PORT', port: a.port2 }, '*');
}
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, runTest);
</script>
</body>
</html>

Просмотреть файл

@ -16,54 +16,57 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=677638
</pre>
<script type="application/javascript">
var a = new MessageChannel();
ok(a, "MessageChannel created");
function start() {
var a = new MessageChannel();
ok(a, "MessageChannel created");
window.addEventListener('message', receiveMessage, false);
function receiveMessage(evt) {
if (evt.data.status == 'READY') {
window.addEventListener('message', receiveMessage, false);
function receiveMessage(evt) {
if (evt.data.status == 'READY') {
runTest();
} else {
ok(false, "Unknown message");
}
}
var div = document.getElementById("content");
ok(div, "Parent exists");
var ifr = document.createElement("iframe");
ifr.addEventListener("load", iframeLoaded, false);
ifr.setAttribute('src', "iframe_messageChannel_post.html");
div.appendChild(ifr);
function iframeLoaded() {
ifr.contentWindow.postMessage({ port: a.port2 }, '*');
}
var tests = [ 42,
null,
undefined,
"hello world",
new Blob([]),
true ];
a.port1.onmessage = function(evt) {
ok(tests.length, "We are waiting for a message");
is(tests[0], evt.data, "Value ok: " + tests[0]);
tests.shift();
runTest();
} else {
ok(false, "Unknown message");
}
}
var div = document.getElementById("content");
ok(div, "Parent exists");
var ifr = document.createElement("iframe");
ifr.addEventListener("load", iframeLoaded, false);
ifr.setAttribute('src', "iframe_messageChannel_post.html");
div.appendChild(ifr);
function iframeLoaded() {
ifr.contentWindow.postMessage({ port: a.port2 }, '*');
}
var tests = [ 42,
null,
undefined,
"hello world",
new Blob([]),
true ];
a.port1.onmessage = function(evt) {
ok(tests.length, "We are waiting for a message");
is(tests[0], evt.data, "Value ok: " + tests[0]);
tests.shift();
runTest();
}
function runTest() {
if (!tests.length) {
SimpleTest.finish();
return;
}
a.port1.postMessage(tests[0]);
function runTest() {
if (!tests.length) {
SimpleTest.finish();
return;
}
a.port1.postMessage(tests[0]);
}
}
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, start);
</script>
</body>
</html>

Просмотреть файл

@ -0,0 +1,42 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=677638
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 677638 - pref</title>
<script type="application/javascript" 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=677638">Mozilla Bug 677638</a>
<div id="content"></div>
<pre id="test">
</pre>
<script type="application/javascript">
function runTest(what) {
var status;
try {
status = MessageChannel;
ok(what, "Should MessageChannel exist?");
} catch(e) {
ok(!what, "Should MessageChannel exist?");
}
try {
status = MessagePort;
ok(what, "Should MessagePort exist?");
} catch(e) {
ok(!what, "Should MessagePort exist?");
}
}
SimpleTest.waitForExplicitFinish();
runTest(false);
SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]},
function() { runTest(true); SimpleTest.finish(); });
</script>
</body>
</html>

Просмотреть файл

@ -228,7 +228,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=677638
];
SimpleTest.waitForExplicitFinish();
runTests();
SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, runTests);
</script>
</body>
</html>

Просмотреть файл

@ -16,49 +16,52 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=677638
</pre>
<script type="application/javascript">
var a = new MessageChannel();
ok(a, "MessageChannel created");
function start() {
var a = new MessageChannel();
ok(a, "MessageChannel created");
window.addEventListener('message', receiveMessage, false);
function receiveMessage(evt) {
if (evt.data.status == 'READY') {
runTest();
} else {
ok(false, "Unknown message");
window.addEventListener('message', receiveMessage, false);
function receiveMessage(evt) {
if (evt.data.status == 'READY') {
runTest();
} else {
ok(false, "Unknown message");
}
}
var div = document.getElementById("content");
ok(div, "Parent exists");
var ifr = document.createElement("iframe");
ifr.addEventListener("load", iframeLoaded, false);
ifr.setAttribute('src', "iframe_messageChannel_post.html");
div.appendChild(ifr);
function iframeLoaded() {
ifr.contentWindow.postMessage({ port: a.port2 }, '*');
}
a.port1.addEventListener('message', receivePortMessage, false);
function receivePortMessage(evt) {
is(evt.data.ab.byteLength, size, "The size is: " + size + " == " + ab.byteLength);
SimpleTest.finish();
}
// Start() is not implicity invoked when addEventListener is used.
a.port1.start();
var size = 1024 * 1024 * 32;
var ab = new ArrayBuffer(size);
is(ab.byteLength, size, "The size is: " + size + " == " + ab.byteLength);
function runTest() {
a.port1.postMessage({ab: ab, cb: ab}, [ab]);
ok(ab.byteLength == 0, "PostMessage - The size is: 0 == " + ab.byteLength)
}
}
var div = document.getElementById("content");
ok(div, "Parent exists");
var ifr = document.createElement("iframe");
ifr.addEventListener("load", iframeLoaded, false);
ifr.setAttribute('src', "iframe_messageChannel_post.html");
div.appendChild(ifr);
function iframeLoaded() {
ifr.contentWindow.postMessage({ port: a.port2 }, '*');
}
a.port1.addEventListener('message', receivePortMessage, false);
function receivePortMessage(evt) {
is(evt.data.ab.byteLength, size, "The size is: " + size + " == " + ab.byteLength);
SimpleTest.finish();
}
// Start() is not implicity invoked when addEventListener is used.
a.port1.start();
var size = 1024 * 1024 * 32;
var ab = new ArrayBuffer(size);
is(ab.byteLength, size, "The size is: " + size + " == " + ab.byteLength);
function runTest() {
a.port1.postMessage({ab: ab, cb: ab}, [ab]);
ok(ab.byteLength == 0, "PostMessage - The size is: 0 == " + ab.byteLength)
}
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, start);
</script>
</body>
</html>

Просмотреть файл

@ -117,7 +117,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=677638
}
SimpleTest.waitForExplicitFinish();
runTests();
SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, runTests);
</script>
</body>
</html>

Просмотреть файл

@ -7,7 +7,7 @@
* http://www.whatwg.org/specs/web-apps/current-work/#channel-messaging
*/
[Constructor]
[Constructor, Pref="dom.messageChannel.enabled"]
interface MessageChannel {
readonly attribute MessagePort port1;
readonly attribute MessagePort port2;

Просмотреть файл

@ -7,6 +7,7 @@
* http://www.whatwg.org/specs/web-apps/current-work/#channel-messaging
*/
[Pref="dom.messageChannel.enabled"]
interface MessagePort : EventTarget {
// TODO void postMessage(any message, optional sequence<Transferable> transfer);
[Throws]