browser_privilegedmozilla_remoteworker.js (3584B)
1 add_setup(async function () { 2 await SpecialPowers.pushPrefEnv({ 3 set: [ 4 ["browser.tabs.remote.separatePrivilegedMozillaWebContentProcess", true], 5 ["browser.tabs.remote.separatedMozillaDomains", "example.org"], 6 ["dom.ipc.processCount.web", 1], 7 ["dom.ipc.processCount.privilegedmozilla", 1], 8 ["dom.serviceWorkers.enabled", true], 9 ["dom.serviceWorkers.testing.enabled", true], 10 ], 11 }); 12 }); 13 14 // This test attempts to verify proper placement of spawned remoteworkers 15 // by spawning them and then verifying that they were spawned in the expected 16 // process by way of nsIWorkerDebuggerManager enumeration. 17 // 18 // Unfortunately, there's no other way to introspect where a worker global was 19 // spawned at this time. (devtools just ends up enumerating all workers in all 20 // processes and we don't want to depend on devtools in this test). 21 // 22 // As a result, this test currently only tests situations where it's known that 23 // a remote worker will be spawned in the same process that is initiating its 24 // spawning. 25 // 26 // This should be enhanced in the future. 27 add_task(async function test_serviceworker() { 28 const basePath = "browser/dom/workers/test"; 29 const pagePath = `${basePath}/file_service_worker_container.html`; 30 const scriptPath = `${basePath}/file_service_worker.js`; 31 32 Services.ppmm.releaseCachedProcesses(); 33 34 async function runWorkerInProcess() { 35 function getActiveWorkerURLs() { 36 const wdm = Cc[ 37 "@mozilla.org/dom/workers/workerdebuggermanager;1" 38 ].getService(Ci.nsIWorkerDebuggerManager); 39 40 const workerDebuggerUrls = Array.from( 41 wdm.getWorkerDebuggerEnumerator() 42 ).map(wd => { 43 return wd.url; 44 }); 45 46 return workerDebuggerUrls; 47 } 48 49 return new Promise(resolve => { 50 content.navigator.serviceWorker.ready.then(({ active }) => { 51 const { port1, port2 } = new content.MessageChannel(); 52 active.postMessage("webpage->serviceworker", [port2]); 53 port1.onmessage = evt => { 54 resolve({ 55 msg: evt.data, 56 workerUrls: getActiveWorkerURLs(), 57 }); 58 }; 59 }); 60 }).then(async res => { 61 // Unregister the service worker used in this test. 62 const registration = await content.navigator.serviceWorker.ready; 63 await registration.unregister(); 64 return res; 65 }); 66 } 67 68 const testCaseList = [ 69 // TODO: find a reasonable way to test the non-privileged scenario 70 // (because more than 1 process is usually available and the worker 71 // can be launched in a different one from the one where the tab 72 // is running). 73 /*{ 74 remoteType: "web", 75 hostname: "example.com", 76 },*/ 77 { 78 remoteType: "privilegedmozilla", 79 hostname: `example.org`, 80 }, 81 ]; 82 83 for (const testCase of testCaseList) { 84 const { remoteType, hostname } = testCase; 85 86 info(`Test remote serviceworkers launch selects a ${remoteType} process`); 87 88 const tab = await BrowserTestUtils.openNewForegroundTab({ 89 gBrowser, 90 url: `https://${hostname}/${pagePath}`, 91 }); 92 93 is( 94 tab.linkedBrowser.remoteType, 95 remoteType, 96 `Got the expected remoteType for ${hostname} tab` 97 ); 98 99 const results = await SpecialPowers.spawn( 100 tab.linkedBrowser, 101 [], 102 runWorkerInProcess 103 ); 104 105 Assert.deepEqual( 106 results, 107 { 108 msg: "serviceworker-reply", 109 workerUrls: [`https://${hostname}/${scriptPath}`], 110 }, 111 `Got the expected results for ${hostname} tab` 112 ); 113 114 BrowserTestUtils.removeTab(tab); 115 } 116 });