OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Accessing navigator.serviceWorker in sandboxed iframe.</title> |
| 3 <script src="../../resources/testharness.js"></script> |
| 4 <script src="../../resources/testharnessreport.js"></script> |
| 5 <script src="../resources/test-helpers.js"></script> |
| 6 <body> |
| 7 <script> |
| 8 |
| 9 function with_sandboxed_iframe(url, sandbox) { |
| 10 return new Promise(function(resolve) { |
| 11 var frame = document.createElement('iframe'); |
| 12 frame.sandbox = sandbox; |
| 13 frame.src = url; |
| 14 frame.onload = function() { resolve(frame); }; |
| 15 document.body.appendChild(frame); |
| 16 }); |
| 17 } |
| 18 |
| 19 var lastCallbackId = 0; |
| 20 var callbacks = {}; |
| 21 function postMassageAndWaitResult(frame) { |
| 22 return new Promise(function(resolve) { |
| 23 var id = ++lastCallbackId; |
| 24 callbacks[id] = resolve; |
| 25 frame.contentWindow.postMessage({id:id}, '*'); |
| 26 }); |
| 27 } |
| 28 |
| 29 window.onmessage = function (e) { |
| 30 message = e.data; |
| 31 var id = message['id']; |
| 32 var calback = callbacks[id]; |
| 33 delete callbacks[id]; |
| 34 calback(message['result']); |
| 35 }; |
| 36 |
| 37 promise_test(function(t) { |
| 38 var url = 'resources/sandboxed-iframe-navigator-serviceworker-iframe.html'; |
| 39 var frame; |
| 40 return with_iframe(url) |
| 41 .then(function(f) { |
| 42 frame = f; |
| 43 return postMassageAndWaitResult(f); |
| 44 }) |
| 45 .then(function(result) { |
| 46 frame.remove(); |
| 47 assert_equals(result, 'ok'); |
| 48 t.done(); |
| 49 }); |
| 50 }, 'Accessing navigator.serviceWorker in normal iframe should not throw.'); |
| 51 |
| 52 promise_test(function(t) { |
| 53 var url = 'resources/sandboxed-iframe-navigator-serviceworker-iframe.html'; |
| 54 var frame; |
| 55 return with_sandboxed_iframe(url, 'allow-scripts') |
| 56 .then(function(f) { |
| 57 frame = f; |
| 58 return postMassageAndWaitResult(f); |
| 59 }) |
| 60 .then(function(result) { |
| 61 frame.remove(); |
| 62 assert_equals( |
| 63 result, |
| 64 'SecurityError: Failed to read the \'serviceWorker\' property from
\'Navigator\': Service worker is disabled because the context is sandboxed and
lacks the \'allow-same-origin\' flag.'); |
| 65 t.done(); |
| 66 }); |
| 67 }, 'Accessing navigator.serviceWorker in sandboxed iframe should throw.'); |
| 68 |
| 69 promise_test(function(t) { |
| 70 var url = 'resources/sandboxed-iframe-navigator-serviceworker-iframe.html'; |
| 71 var frame; |
| 72 return with_sandboxed_iframe(url, 'allow-scripts allow-same-origin') |
| 73 .then(function(f) { |
| 74 frame = f; |
| 75 return postMassageAndWaitResult(f); |
| 76 }) |
| 77 .then(function(result) { |
| 78 frame.remove(); |
| 79 assert_equals(result, 'ok'); |
| 80 t.done(); |
| 81 }); |
| 82 }, |
| 83 'Accessing navigator.serviceWorker in sandboxed iframe with ' + |
| 84 'allow-same-origin flag should not throw.'); |
| 85 |
| 86 </script> |
| 87 </body> |
OLD | NEW |