| <!doctype html> |
| <html> |
| <head> |
| <meta charset=utf-8> |
| <title></title> |
| <script src=/resources/testharness.js></script> |
| <script src=/resources/testharnessreport.js></script> |
| </head> |
| <body> |
| <script> |
| <!-- |
| promise_test(t => { |
| return new Promise((resolve) => { |
| let ifr = document.createElement("iframe"); |
| ifr.src = |
| "data:text/html,<script> let bc = new BroadcastChannel(\"test\");" + |
| "bc.onmessage = (e) => {" + |
| " if (e.data == \"ping\") bc.postMessage('pong');"+ |
| " else parent.postMessage({workerMessageOrigin: e.data, messageOrigin: e.origin}, \"*\"); };" + |
| "new Worker(URL.createObjectURL(new Blob([\"" + |
| "let bc2 = new BroadcastChannel('test'); bc2.postMessage('ping'); " + |
| "bc2.onmessage = e => bc2.postMessage(e.origin);" + |
| "\"], {type: 'text/javascript'}))); </script>"; |
| window.addEventListener("message", t.step_func(e => { |
| assert_equals(e.data.workerMessageOrigin, "null"); |
| assert_equals(e.data.messageOrigin, "null"); |
| resolve(); |
| }), {once: true}); |
| t.add_cleanup(() => { document.body.removeChild(ifr) }); |
| document.body.appendChild(ifr); |
| }); |
| }, "Opaque origin should be serialized to \"null\""); |
| |
| |
| const iframe_src = (channel_name, iframe_name) => `data:text/html,<script> |
| let bc2 = new BroadcastChannel("${channel_name}"); |
| bc2.onmessage = (e) => { |
| if (e.data == "from-${iframe_name}") { |
| parent.postMessage("${iframe_name}-done", "*"); |
| } else { |
| parent.postMessage("fail", "*"); |
| } |
| }; |
| let bc3 = new BroadcastChannel("${channel_name}"); |
| bc3.postMessage("from-${iframe_name}"); |
| </script>`; |
| |
| promise_test(t => { |
| return new Promise((resolve, reject) => { |
| const channel_name = "opaque-origin-test-2"; |
| const bc1 = new BroadcastChannel(channel_name); |
| bc1.onmessage = t.unreached_func("Received message from an opaque origin"); |
| |
| // We'll create an iframe and have it send a BroadcastChannel message |
| // between two instances. Once the message is received, it will postMessage |
| // back and we'll repeat this with another iframe. If the first |
| // BroadcastChannel message is received by `bc1`, or if the second |
| // BroadcastChannel message is received by `bc1` or `bc2` in the first |
| // iframe, then the test should fail. |
| |
| window.addEventListener("message", e => { |
| if(e.data == "iframe1-done") { |
| let iframe2 = document.createElement("iframe"); |
| iframe2.src = iframe_src(channel_name, "iframe2"); |
| t.add_cleanup(() => { document.body.removeChild(iframe2) }); |
| document.body.appendChild(iframe2); |
| } else if(e.data == "iframe2-done") { |
| resolve(); |
| } else if(e.data == "fail") { |
| reject("One opaque origin received a message from the other"); |
| } else { |
| reject("An unexpected error occurred"); |
| } |
| }); |
| |
| let iframe1 = document.createElement("iframe"); |
| iframe1.src = iframe_src(channel_name, "iframe1"); |
| t.add_cleanup(() => { document.body.removeChild(iframe1) }); |
| document.body.appendChild(iframe1); |
| }); |
| }, "BroadcastChannel messages from opaque origins should be self-contained"); |
| //--> |
| </script> |
| </body> |
| </html> |