Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #647 from erikvold/796917
Browse files Browse the repository at this point in the history
fix bug 796917 using postMessage to an iframe from a content script does not serialize data r=@Gozala
  • Loading branch information
erikvold committed Nov 5, 2012
2 parents da06a63 + 60242f4 commit 2f4f95b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
20 changes: 20 additions & 0 deletions data/test-iframe-postmessage.html
@@ -0,0 +1,20 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<script>
window.addEventListener("message", function(msg) {
parent.postMessage(msg.data, "*");
});

parent.postMessage({
first: "a string",
second: ["an", "array"],
third: {an: 'object'}
}, '*');
</script>
</head>
<body>
<h1>Inner iframe</h1>
</body>
</html>
9 changes: 9 additions & 0 deletions data/test-iframe.html
@@ -0,0 +1,9 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<iframe id="inner" src="about:blank"></iframe>
</body>
</html>
11 changes: 11 additions & 0 deletions data/test-iframe.js
@@ -0,0 +1,11 @@

var count = 0

setTimeout(function() {
window.addEventListener("message", function(msg) {
if (++count > 1) self.postMessage(msg.data);
else msg.source.postMessage(msg.data, '*');
});

document.getElementById('inner').src = iframePath;
}, 0);
11 changes: 8 additions & 3 deletions lib/sdk/content/content-proxy.js
Expand Up @@ -593,13 +593,18 @@ const xRayWrappersMethodsFixes = {
catch(e) {
return null;
}

// Create a wrapper that is going to call `postMessage` through `eval`
let f = function postMessage(message, targetOrigin) {
message = message.toString().replace(/['\\]/g,"\\$&");
let jscode = "this.postMessage(";
if (typeof message != "string")
jscode += JSON.stringify(message);
else
jscode += "'" + message.toString().replace(/['\\]/g,"\\$&") + "'";

targetOrigin = targetOrigin.toString().replace(/['\\]/g,"\\$&");

let jscode = "this.postMessage('" + message + "', '" +
targetOrigin + "')";
jscode += ", '" + targetOrigin + "')";
return this.wrappedJSObject.eval(jscode);
};
return getProxyForFunction(f, NativeFunctionWrapper(f));
Expand Down
24 changes: 23 additions & 1 deletion test/test-page-mod.js
@@ -1,7 +1,6 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

var pageMod = require("sdk/page-mod");
Expand All @@ -13,6 +12,7 @@ const { Cc, Ci } = require("chrome");
const { open, getFrames, getMostRecentBrowserWindow } = require('sdk/window/utils');
const windowUtils = require('sdk/deprecated/window-utils');
const { getTabContentWindow, getActiveTab, openTab, closeTab } = require('sdk/tabs/utils');
const { data } = require('self');

/* XXX This can be used to delay closing the test Firefox instance for interactive
* testing or visual inspection. This test is registered first so that it runs
Expand Down Expand Up @@ -894,3 +894,25 @@ exports.testBug803529 = function(test) {

window.addEventListener("load", wait4Iframes, false);
};

exports.testIFramePostMessage = function(test) {
test.waitUntilDone();

tabs.open({
url: data.url("test-iframe.html"),
onReady: function(tab) {
var worker = tab.attach({
contentScriptFile: data.url('test-iframe.js'),
contentScript: ' var iframePath = \'' + data.url('test-iframe-postmessage.html') + '\'',
onMessage: function(msg) {
test.assertEqual(msg.first, 'a string');
test.assert(msg.second[1], "array");
test.assertEqual(typeof msg.third, 'object');

worker.destroy();
tab.close(function() test.done());
}
});
}
});
};

0 comments on commit 2f4f95b

Please sign in to comment.