browser_broadcastChannel.js (2064B)
1 // BroadcastChannel is not considered part of CookieJar. It's not allowed to 2 // communicate with other windows with different cookie jar settings. 3 "use strict"; 4 5 CookiePolicyHelper.runTest("BroadcastChannel", { 6 cookieJarAccessAllowed: async w => { 7 new w.BroadcastChannel("hello"); 8 ok(true, "BroadcastChannel be used"); 9 }, 10 11 cookieJarAccessDenied: async w => { 12 try { 13 new w.BroadcastChannel("hello"); 14 ok(false, "BroadcastChannel cannot be used!"); 15 } catch (e) { 16 ok(true, "BroadcastChannel cannot be used!"); 17 is(e.name, "SecurityError", "We want a security error message."); 18 } 19 }, 20 }); 21 22 CookiePolicyHelper.runTest("BroadcastChannel in workers", { 23 cookieJarAccessAllowed: async w => { 24 function nonBlockingCode() { 25 new BroadcastChannel("hello"); 26 postMessage(true); 27 } 28 29 let blob = new w.Blob([ 30 nonBlockingCode.toString() + "; nonBlockingCode();", 31 ]); 32 ok(blob, "Blob has been created"); 33 34 let blobURL = w.URL.createObjectURL(blob); 35 ok(blobURL, "Blob URL has been created"); 36 37 let worker = new w.Worker(blobURL); 38 ok(worker, "Worker has been created"); 39 40 await new w.Promise((resolve, reject) => { 41 worker.onmessage = function (e) { 42 if (e) { 43 resolve(); 44 } else { 45 reject(); 46 } 47 }; 48 }); 49 }, 50 51 cookieJarAccessDenied: async w => { 52 function blockingCode() { 53 try { 54 new BroadcastChannel("hello"); 55 postMessage(false); 56 } catch (e) { 57 postMessage(e.name == "SecurityError"); 58 } 59 } 60 61 let blob = new w.Blob([blockingCode.toString() + "; blockingCode();"]); 62 ok(blob, "Blob has been created"); 63 64 let blobURL = w.URL.createObjectURL(blob); 65 ok(blobURL, "Blob URL has been created"); 66 67 let worker = new w.Worker(blobURL); 68 ok(worker, "Worker has been created"); 69 70 await new w.Promise((resolve, reject) => { 71 worker.onmessage = function (e) { 72 if (e) { 73 resolve(); 74 } else { 75 reject(); 76 } 77 }; 78 }); 79 }, 80 });