Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Bug 666547 - No way to interact between content scripts and content js #193

Closed
wants to merge 2 commits into from

3 participants

Irakli Gozalishvili Myk Melez ochameau
Irakli Gozalishvili
Owner

Test case for this bug

Irakli Gozalishvili
Owner

Merged with latest master, so it can be merged in easily!

Myk Melez
Owner

@ochameau: can you review this change?

ochameau
Owner

We already have a simple test against postMessage use here:
https://github.com/mozilla/addon-sdk/blob/master/packages/api-utils/tests/test-content-proxy.js#L196-223

This example looks more like an example than a unit test.
If you think it makes sense to land such test, we will need to add comments or improve events name.
It is hard to follow all these: <, <~, <-, <=.

There is something wrong with the following line:
worker.on('<~', delivered);
it should be:
worker.port.on('<~', delivered);

Myk Melez
Owner

@ochameau: I don't know whether or not it makes sense to land this; that's why I asked you to review it! :-) If you think it makes sense, then please review; otherwise, just close the request! (I'm trying to make sure we don't leave requests like this hanging around forever. We should either review them and get them landed or close them so they don't clutter up the list of pending requests.)

Irakli Gozalishvili Gozala closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 23, 2011
  1. Irakli Gozalishvili
Commits on Jul 27, 2011
  1. Irakli Gozalishvili

    Merge remote-tracking branch 'upstream/master' into bug/postMessage.6…

    Gozala authored
    …66547
    
    Conflicts:
    	packages/api-utils/tests/test-content-worker.js
This page is out of date. Refresh to see the latest.
Showing with 104 additions and 26 deletions.
  1. +104 −26 packages/api-utils/tests/test-content-worker.js
130 packages/api-utils/tests/test-content-worker.js
View
@@ -23,20 +23,20 @@ const { Worker } = require('content/worker');
exports['test:sample'] = function(test) {
let window = makeWindow();
test.waitUntilDone();
-
- // As window has just being created, its document is still loading,
+
+ // As window has just being created, its document is still loading,
// and we have about:blank document before the expected one
- test.assertEqual(window.document.location.href, "about:blank",
+ test.assertEqual(window.document.location.href, "about:blank",
"window starts by loading about:blank");
-
+
// We need to wait for the load/unload of temporary about:blank
// or our worker is going to be automatically destroyed
window.addEventListener("load", function onload() {
window.removeEventListener("load", onload, true);
-
- test.assertNotEqual(window.document.location.href, "about:blank",
+
+ test.assertNotEqual(window.document.location.href, "about:blank",
"window is now on the right document");
-
+
let worker = Worker({
window: window,
contentScript: 'new ' + function WorkerScope() {
@@ -50,24 +50,24 @@ exports['test:sample'] = function(test) {
contentScriptWhen: 'ready',
onMessage: function(msg) {
test.assertEqual('bye!', msg);
- test.assertEqual(worker.url, window.document.location.href,
+ test.assertEqual(worker.url, window.document.location.href,
"worker.url still works");
test.done();
}
});
-
- test.assertEqual(worker.url, window.document.location.href,
+
+ test.assertEqual(worker.url, window.document.location.href,
"worker.url works");
worker.postMessage('hi!');
-
+
}, true);
-
+
}
exports['test:emit'] = function(test) {
let window = makeWindow();
test.waitUntilDone();
-
+
let worker = Worker({
window: window,
contentScript: 'new ' + function WorkerScope() {
@@ -75,7 +75,7 @@ exports['test:emit'] = function(test) {
self.port.on('addon-to-content', function (data) {
self.port.emit('content-to-addon', data);
});
-
+
// Check for global pollution
//if (typeof on != "undefined")
// self.postMessage("`on` is in globals");
@@ -83,26 +83,26 @@ exports['test:emit'] = function(test) {
self.postMessage("`once` is in globals");
if (typeof emit != "undefined")
self.postMessage("`emit` is in globals");
-
+
},
onMessage: function(msg) {
test.fail("Got an unexpected message : "+msg);
}
});
-
+
// Validate worker.port
worker.port.on('content-to-addon', function (data) {
test.assertEqual(data, "event data");
test.done();
});
worker.port.emit('addon-to-content', 'event data');
-
+
}
exports['test:emit hack message'] = function(test) {
let window = makeWindow();
test.waitUntilDone();
-
+
let worker = Worker({
window: window,
contentScript: 'new ' + function WorkerScope() {
@@ -119,7 +119,7 @@ exports['test:emit hack message'] = function(test) {
test.fail("Got exception: "+e);
}
});
-
+
worker.port.on('message', function (data) {
test.assertEqual(data, "event data");
test.done();
@@ -128,13 +128,13 @@ exports['test:emit hack message'] = function(test) {
test.fail("Got an unexpected message : "+msg);
});
worker.port.emit('message', 'event data');
-
+
}
exports['test:n-arguments emit'] = function(test) {
let window = makeWindow();
test.waitUntilDone();
-
+
let worker = Worker({
window: window,
contentScript: 'new ' + function WorkerScope() {
@@ -144,7 +144,7 @@ exports['test:n-arguments emit'] = function(test) {
});
}
});
-
+
// Validate worker.port
worker.port.on('content-to-addon', function (arg1, arg2, arg3) {
test.assertEqual(arg1, "first argument");
@@ -158,7 +158,7 @@ exports['test:n-arguments emit'] = function(test) {
exports['test:post-json-values-only'] = function(test) {
let window = makeWindow();
test.waitUntilDone();
-
+
let worker = Worker({
window: window,
contentScript: 'new ' + function WorkerScope() {
@@ -185,7 +185,7 @@ exports['test:post-json-values-only'] = function(test) {
exports['test:emit-json-values-only'] = function(test) {
let window = makeWindow();
test.waitUntilDone();
-
+
let worker = Worker({
window: window,
contentScript: 'new ' + function WorkerScope() {
@@ -202,7 +202,7 @@ exports['test:emit-json-values-only'] = function(test) {
});
}
});
-
+
// Validate worker.port
worker.port.on('content-to-addon', function (result) {
test.assertEqual(result[0], true, "functions become null");
@@ -331,4 +331,82 @@ exports['test:setTimeout are unregistered on content unload'] = function(test) {
}, 500);
}, true);
-}
+};
+
+exports['test:interaction with content js'] = function(test) {
+ test.waitUntilDone();
+ let window = makeWindow();
+
+ let worker = Worker({
+ window: window,
+ contentScript: 'new ' + function WorkerScope() {
+
+ function deliver(event) {
+ self.port.emit('<~', event.data);
+ }
+
+ function pipe(event) {
+
+ if (event.target === 'unsafeWindow')
+ unsafeWindow.postMessage(event.target, '*');
+ else if (event.target === 'defaultView')
+ document.defaultView.postMessage(event.target, '*');
+
+ // Notify that post was successful
+ self.port.emit('<-', event);
+ }
+
+ self.port.once('=>', function() {
+ window.removeEventListener('message', deliver, false);
+ self.removeListener('->', pipe);
+ self.port.emit('<=');
+ });
+
+ // Set up listeners
+ window.addEventListener('message', deliver, false);
+ self.port.on('->', pipe);
+ self.port.emit('<')
+ }
+ });
+
+ let unsafeWindowDelivered = false, defaultViewDelivered = false,
+ unsafeWindowPosted = false, defaultViewPosted = false,
+ deliverCount = 0, postCount = 0;
+
+ worker.port.on('<-', function (event) {
+ postCount ++;
+ if (event.target === 'unsafeWindow') {
+ unsafeWindowPosted = true;
+ worker.port.emit('->', { target: 'defaultView' });
+ }
+ if (event.target === 'defaultView') {
+ defaultViewPosted = true;
+ worker.port.emit('=>');
+ }
+ });
+
+ function delivered(target) {
+ deliverCount ++;
+ if (target === 'unsafeWindow')
+ unsafeWindowDelivered = true;
+ if (target === 'defaultView')
+ defaultViewDelivered = true;
+ }
+
+ worker.on('<~', delivered);
+
+ worker.port.once('<=', function stop() {
+ test.assert(unsafeWindowPosted, 'posted to use unsafeWindow');
+ test.assert(defaultViewPosted, 'posted to use defaultView');
+ test.assertEqual(postCount, 2, 'two messages were posted');
+ test.assertEqual(postCount, deliverCount, 'all messages were delivered');
+ test.assert(unsafeWindowDelivered, 'post via unsafeWindow was delivered');
+ test.assert(defaultViewDelivered, 'post via defaultView was delivered');
+ worker.port.removeListener('<~', delivered);
+ window.close();
+ test.done();
+ })
+ worker.port.once('<', function() {
+ worker.port.emit('->', { target: 'unsafeWindow' })
+ })
+};
Something went wrong with that request. Please try again.