Skip to content

Commit

Permalink
Add a PostMessageOptions API to workers and message_port
Browse files Browse the repository at this point in the history
The current pull request against the HTML spec is here:
whatwg/html#3799

Rename the RuntimeEnabledFeature from WindowPostMessageOptions to
PostMessageOptions

BUG=861735

Change-Id: Ia7980a85c10535f6d531c87f3790efcf6ed2d54d
Reviewed-on: https://chromium-review.googlesource.com/1169304
Commit-Queue: Dave Tapuska <[email protected]>
Reviewed-by: Marijn Kruisselbrink <[email protected]>
Reviewed-by: Kentaro Hara <[email protected]>
Cr-Commit-Position: refs/heads/master@{#582619}
  • Loading branch information
dtapuska authored and Chrome-bot committed Aug 13, 2018
1 parent 0419ed8 commit e2a45db
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
42 changes: 42 additions & 0 deletions service-workers/service-worker/postmessage.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,46 @@
});
}, 'postMessage a transferable ArrayBuffer between ServiceWorker and Client' +
' over MessagePort');

promise_test(t => {
var script = 'resources/postmessage-dictionary-transferables-worker.js';
var scope = 'resources/blank.html';
var sw = navigator.serviceWorker;

var message = 'Hello, world!';
var text_encoder = new TextEncoder;
var text_decoder = new TextDecoder;

return service_worker_unregister_and_register(t, script, scope)
.then(r => {
t.add_cleanup(() => r.unregister());

var ab = text_encoder.encode(message);
assert_equals(ab.byteLength, message.length);
r.installing.postMessage(ab, {transfer: [ab.buffer]});
assert_equals(text_decoder.decode(ab), '');
assert_equals(ab.byteLength, 0);

return new Promise(resolve => { sw.onmessage = resolve; });
})
.then(e => {
// Verify the integrity of the transferred array buffer.
assert_equals(e.data.content, message);
assert_equals(e.data.byteLength, message.length);
return new Promise(resolve => { sw.onmessage = resolve; });
})
.then(e => {
// Verify the integrity of the array buffer sent back from
// ServiceWorker via Client.postMessage.
assert_equals(text_decoder.decode(e.data), message);
assert_equals(e.data.byteLength, message.length);
return new Promise(resolve => { sw.onmessage = resolve; });
})
.then(e => {
// Verify that the array buffer on ServiceWorker is neutered.
assert_equals(e.data.content, '');
assert_equals(e.data.byteLength, 0);
});
}, 'postMessage with dictionary a transferable ArrayBuffer between ServiceWorker and Client');

</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var messageHandler = function(port, e) {
var text_decoder = new TextDecoder;
port.postMessage({
content: text_decoder.decode(e.data),
byteLength: e.data.byteLength
});

// Send back the array buffer via Client.postMessage.
port.postMessage(e.data, {transfer: [e.data.buffer]});

port.postMessage({
content: text_decoder.decode(e.data),
byteLength: e.data.byteLength
});
};

self.addEventListener('message', e => {
if (e.ports[0]) {
// Wait for messages sent via MessagePort.
e.ports[0].onmessage = messageHandler.bind(null, e.ports[0]);
return;
}
messageHandler(e.source, e);
});
18 changes: 18 additions & 0 deletions webmessaging/message-channels/dictionary-transferrable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<title>basic messagechannel with transfer</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
async_test(function(t) {
var channel = new MessageChannel();
var ab = new ArrayBuffer(1);
channel.port1.postMessage(ab, {transfer: [ab]});
channel.port2.onmessage = t.step_func(
function(e) {
assert_equals(e.data.byteLength, 1);
t.done();
});
channel.port2.start();
});
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
onmessage = (event) => {
try {
postMessage(event.data, {transfer: [event.data]});
} catch(e) {
postMessage(''+e);
}
}
/*
-->
<!doctype html>
<title>Using dictionary as postMessage's second argument</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
(async_test()).step(function() {
var worker = new Worker('#');
var ab = new ArrayBuffer(1);
worker.postMessage(ab, {transfer: [ab]});
worker.onmessage = this.step_func(function(e) {
assert_equals(e.data.byteLength, 1);
this.done();
});
});
</script>
<!--
*/
//-->
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(async_test()).step(function() {
var worker = new Worker('#');
worker.onmessage = this.step_func(function(e) {
assert_true(e.data);
assert_equals(1, e.data);
this.done();
});
});
Expand Down

0 comments on commit e2a45db

Please sign in to comment.