tor-browser

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

browser_sendAsyncMessage.js (2364B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 declTest("asyncMessage testing", {
      6  async test(browser) {
      7    let parent = browser.browsingContext.currentWindowGlobal;
      8    let actorParent = parent.getActor("TestWindow");
      9    ok(actorParent, "JSWindowActorParent should have value.");
     10 
     11    await ContentTask.spawn(browser, {}, async function () {
     12      let child = content.windowGlobalChild;
     13      let actorChild = child.getActor("TestWindow");
     14      ok(actorChild, "JSWindowActorChild should have value.");
     15 
     16      let promise = new Promise(resolve => {
     17        actorChild.sendAsyncMessage("init", {});
     18        actorChild.done = data => resolve(data);
     19      }).then(data => {
     20        ok(data.initial, "Initial should be true.");
     21        ok(data.toParent, "ToParent should be true.");
     22        ok(data.toChild, "ToChild should be true.");
     23      });
     24 
     25      await promise;
     26    });
     27  },
     28 });
     29 
     30 declTest("asyncMessage without both sides", {
     31  async test(browser) {
     32    // If we don't create a parent actor, make sure the parent actor
     33    // gets created by having sent the message.
     34    await ContentTask.spawn(browser, {}, async function () {
     35      let child = content.windowGlobalChild;
     36      let actorChild = child.getActor("TestWindow");
     37      ok(actorChild, "JSWindowActorChild should have value.");
     38 
     39      let promise = new Promise(resolve => {
     40        actorChild.sendAsyncMessage("init", {});
     41        actorChild.done = data => resolve(data);
     42      }).then(data => {
     43        ok(data.initial, "Initial should be true.");
     44        ok(data.toParent, "ToParent should be true.");
     45        ok(data.toChild, "ToChild should be true.");
     46      });
     47 
     48      await promise;
     49    });
     50  },
     51 });
     52 
     53 declTest("asyncMessage can transfer MessagePorts", {
     54  async test(browser) {
     55    await ContentTask.spawn(browser, {}, async function () {
     56      let child = content.windowGlobalChild;
     57      let actorChild = child.getActor("TestWindow");
     58 
     59      let { port1, port2 } = new MessageChannel();
     60      actorChild.sendAsyncMessage("messagePort", { port: port2 }, [port2]);
     61      let reply = await new Promise(resolve => {
     62        port1.onmessage = message => {
     63          resolve(message.data);
     64          port1.close();
     65        };
     66      });
     67 
     68      is(reply, "Message sent from parent over a MessagePort.");
     69    });
     70  },
     71 });