tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

helper.js (2829B)


      1 /**
      2 * Create a new promise that resolves when the window receives
      3 * the MessagePort and starts it.
      4 *
      5 * @param {Window} window - The window to wait for the MessagePort.
      6 * @returns {Promise<MessagePort>} A promise you should await to ensure the
      7 *     window
      8 * receives the MessagePort.
      9 */
     10 function expectMessagePortFromWindow(window) {
     11  return new Promise(resolve => {
     12    window.onmessage = e => {
     13      try {
     14        assert_true(e.ports[0] instanceof window.MessagePort);
     15        e.ports[0].start();
     16        resolve(e.ports[0]);
     17      } catch (e) {
     18        reject(e);
     19      }
     20    };
     21  });
     22 }
     23 
     24 /**
     25 * Create a new promise that resolves when the window receives
     26 * the MessagePort and does not start it.
     27 *
     28 * @param {Window} window - The window to wait for the MessagePort.
     29 * @returns {Promise<MessagePort>} A promise you should await to ensure the
     30 *     window
     31 * receives the MessagePort.
     32 */
     33 function expectMessagePortFromWindowWithoutStartingIt(window) {
     34  return new Promise(resolve => {
     35    window.onmessage = e => {
     36      try {
     37        assert_true(e.ports[0] instanceof window.MessagePort);
     38        resolve(e.ports[0]);
     39      } catch (e) {
     40        reject(e);
     41      }
     42    };
     43  });
     44 }
     45 
     46 /**
     47 * Create a new MessageChannel and transfers one of the ports to
     48 * the window which opened the window with a remote context provided
     49 * as an argument, and immediately closes the entangled port.
     50 *
     51 * @param {RemoteContextWrapper} remoteContextWrapper
     52 */
     53 async function createMessageChannelAndSendPortFollowedByClose(remoteContextWrapper) {
     54  await remoteContextWrapper.executeScript(() => {
     55    const {port1, port2} = new MessageChannel();
     56    port1.start();
     57    window.opener.postMessage({}, '*', [port2]);
     58    port1.close();
     59  });
     60 }
     61 
     62 /**
     63 * Create a new MessageChannel and transfers one of the ports to
     64 * the window which opened the window with a remote context provided
     65 * as an argument.
     66 *
     67 * @param {RemoteContextWrapper} remoteContextWrapper
     68 */
     69 async function createMessageChannelAndSendPort(remoteContextWrapper) {
     70  await remoteContextWrapper.executeScript(() => {
     71    const {port1, port2} = new MessageChannel();
     72    port1.start();
     73    window.opener.postMessage({}, '*', [port2]);
     74    window.closePort = () => {
     75      port1.close();
     76    }
     77  });
     78 }
     79 
     80 /**
     81 * Creates a window with a remote context.
     82 *
     83 * @returns {Promise<RemoteContextWrapper>}
     84 */
     85 async function addWindow() {
     86  const helper = new RemoteContextHelper();
     87  return helper.addWindow();
     88 }
     89 
     90 /**
     91 * Creates a new promise that resolves when the close event is fired.
     92 *
     93 * @param {MessagePort} port - MessagePort on which the close event will
     94 * be fired.
     95 * @returns {Promise} A promise you should await to ensure the close event
     96 * is dispatched.
     97 */
     98 function createCloseEventPromise(port) {
     99  return new Promise(resolve => port.onclose = resolve);
    100 }