head.js (2072B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 "use strict"; 5 6 /** 7 * Add a tab with given `url`. Returns a promise 8 * that will be resolved when the tab finished loading. 9 */ 10 function addTab(url) { 11 return BrowserTestUtils.openNewForegroundTab(gBrowser, TAB_URL); 12 } 13 14 /** 15 * Remove the given `tab`. 16 */ 17 function removeTab(tab) { 18 gBrowser.removeTab(tab); 19 } 20 21 /** 22 * Create a worker with the given `url` in the given `tab`. 23 */ 24 function createWorkerInTab(tab, url) { 25 info("Creating worker with url '" + url + "'\n"); 26 return SpecialPowers.spawn(tab.linkedBrowser, [url], urlChild => { 27 if (!content._workers) { 28 content._workers = {}; 29 } 30 content._workers[urlChild] = new content.Worker(urlChild); 31 }); 32 } 33 34 /** 35 * Terminate the worker with the given `url` in the given `tab`. 36 */ 37 function terminateWorkerInTab(tab, url) { 38 info("Terminating worker with url '" + url + "'\n"); 39 return SpecialPowers.spawn(tab.linkedBrowser, [url], urlChild => { 40 content._workers[urlChild].terminate(); 41 delete content._workers[urlChild]; 42 }); 43 } 44 45 /** 46 * Post the given `message` to the worker with the given `url` in the given 47 * `tab`. 48 */ 49 function postMessageToWorkerInTab(tab, url, message) { 50 info("Posting message to worker with url '" + url + "'\n"); 51 return SpecialPowers.spawn( 52 tab.linkedBrowser, 53 [url, message], 54 (urlChild, messageChild) => { 55 let worker = content._workers[urlChild]; 56 worker.postMessage(messageChild); 57 return new Promise(function (resolve) { 58 worker.onmessage = function (event) { 59 worker.onmessage = null; 60 resolve(event.data); 61 }; 62 }); 63 } 64 ); 65 } 66 67 /** 68 * Disable the cache in the given `tab`. 69 */ 70 function disableCacheInTab(tab) { 71 return SpecialPowers.spawn(tab.linkedBrowser, [], () => { 72 content.docShell.defaultLoadFlags = 73 Ci.nsIRequest.LOAD_BYPASS_CACHE | Ci.nsIRequest.INHIBIT_CACHING; 74 }); 75 }