tor-browser

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

browser_resources_console_messages_workers.js (8401B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test the ResourceCommand API around CONSOLE_MESSAGE in workers
      7 
      8 const FISSION_TEST_URL = URL_ROOT_SSL + "fission_document_workers.html";
      9 const WORKER_FILE = "test_worker.js";
     10 const IFRAME_FILE = `${URL_ROOT_ORG_SSL}fission_iframe_workers.html`;
     11 
     12 add_task(async function () {
     13  // Set the following pref to false as it's the one that enables direct connection
     14  // to the worker targets
     15  await pushPref("dom.worker.console.dispatch_events_to_main_thread", false);
     16 
     17  const tab = await addTab(FISSION_TEST_URL);
     18 
     19  const { client, resourceCommand, targetCommand } = await initResourceCommand(
     20    tab,
     21    { listenForWorkers: true }
     22  );
     23 
     24  info("Wait for the workers (from the main page and the iframe) to be ready");
     25  const targets = [];
     26  await new Promise(resolve => {
     27    const onAvailable = async ({ targetFront }) => {
     28      targets.push(targetFront);
     29      if (targets.length === 2) {
     30        resolve();
     31      }
     32    };
     33    targetCommand.watchTargets({
     34      types: [targetCommand.TYPES.WORKER],
     35      onAvailable,
     36    });
     37  });
     38 
     39  // The worker logs a message right when it starts, containing its location, so we can
     40  // assert that we get the logs from the worker spawned in the content page and from the
     41  // worker spawned in the iframe.
     42  info("Check that we receive the cached messages");
     43 
     44  const resources = [];
     45  const onAvailable = innerResources => {
     46    for (const resource of innerResources) {
     47      // Ignore resources from non worker targets
     48      if (!resource.targetFront.isWorkerTarget) {
     49        continue;
     50      }
     51 
     52      resources.push(resource);
     53    }
     54  };
     55 
     56  await resourceCommand.watchResources(
     57    [resourceCommand.TYPES.CONSOLE_MESSAGE],
     58    {
     59      onAvailable,
     60    }
     61  );
     62 
     63  is(resources.length, 2, "Got the expected number of existing messages");
     64  const startLogFromWorkerInMainPage = resources.find(
     65    message =>
     66      message.arguments[1] === `${URL_ROOT_SSL}${WORKER_FILE}#simple-worker`
     67  );
     68  const startLogFromWorkerInIframe = resources.find(
     69    message =>
     70      message.arguments[1] ===
     71      `${URL_ROOT_ORG_SSL}${WORKER_FILE}#simple-worker-in-iframe`
     72  );
     73 
     74  checkStartWorkerLogMessage(startLogFromWorkerInMainPage, {
     75    expectedUrl: `${URL_ROOT_SSL}${WORKER_FILE}#simple-worker`,
     76    isAlreadyExistingResource: true,
     77  });
     78  checkStartWorkerLogMessage(startLogFromWorkerInIframe, {
     79    expectedUrl: `${URL_ROOT_ORG_SSL}${WORKER_FILE}#simple-worker-in-iframe`,
     80    isAlreadyExistingResource: true,
     81  });
     82  let messageCount = resources.length;
     83 
     84  info(
     85    "Now log messages *after* the call to ResourceCommand.watchResources and after having received all existing messages"
     86  );
     87 
     88  await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
     89    content.wrappedJSObject.logMessageInWorker("live message from main page");
     90 
     91    const iframe = content.document.querySelector("iframe");
     92    SpecialPowers.spawn(iframe, [], () => {
     93      content.wrappedJSObject.logMessageInWorker("live message from iframe");
     94    });
     95  });
     96 
     97  // Wait until the 2 new logs are available
     98  await waitUntil(() => resources.length === messageCount + 2);
     99  const liveMessageFromWorkerInMainPage = resources.find(
    100    message => message.arguments[1] === "live message from main page"
    101  );
    102  const liveMessageFromWorkerInIframe = resources.find(
    103    message => message.arguments[1] === "live message from iframe"
    104  );
    105 
    106  checkLogInWorkerMessage(
    107    liveMessageFromWorkerInMainPage,
    108    "live message from main page"
    109  );
    110 
    111  checkLogInWorkerMessage(
    112    liveMessageFromWorkerInIframe,
    113    "live message from iframe"
    114  );
    115 
    116  // update the current number of resources received
    117  messageCount = resources.length;
    118 
    119  info("Now spawn new workers and log messages in main page and iframe");
    120  await SpecialPowers.spawn(
    121    tab.linkedBrowser,
    122    [WORKER_FILE],
    123    async workerUrl => {
    124      const spawnedWorker = new content.Worker(`${workerUrl}#spawned-worker`);
    125      spawnedWorker.postMessage({
    126        type: "log-in-worker",
    127        message: "live message in spawned worker from main page",
    128      });
    129 
    130      const iframe = content.document.querySelector("iframe");
    131      SpecialPowers.spawn(iframe, [workerUrl], async innerWorkerUrl => {
    132        const spawnedWorkerInIframe = new content.Worker(
    133          `${innerWorkerUrl}#spawned-worker-in-iframe`
    134        );
    135        spawnedWorkerInIframe.postMessage({
    136          type: "log-in-worker",
    137          message: "live message in spawned worker from iframe",
    138        });
    139      });
    140    }
    141  );
    142 
    143  info(
    144    "Wait until the 4 new logs are available (the ones logged at worker creation + the ones from postMessage"
    145  );
    146  await waitUntil(
    147    () => resources.length === messageCount + 4,
    148    `Couldn't get the expected number of resources (expected ${
    149      messageCount + 4
    150    }, got ${resources.length})`
    151  );
    152  const startLogFromSpawnedWorkerInMainPage = resources.find(
    153    message =>
    154      message.arguments[1] === `${URL_ROOT_SSL}${WORKER_FILE}#spawned-worker`
    155  );
    156  const startLogFromSpawnedWorkerInIframe = resources.find(
    157    message =>
    158      message.arguments[1] ===
    159      `${URL_ROOT_ORG_SSL}${WORKER_FILE}#spawned-worker-in-iframe`
    160  );
    161  const liveMessageFromSpawnedWorkerInMainPage = resources.find(
    162    message =>
    163      message.arguments[1] === "live message in spawned worker from main page"
    164  );
    165  const liveMessageFromSpawnedWorkerInIframe = resources.find(
    166    message =>
    167      message.arguments[1] === "live message in spawned worker from iframe"
    168  );
    169 
    170  checkStartWorkerLogMessage(startLogFromSpawnedWorkerInMainPage, {
    171    expectedUrl: `${URL_ROOT_SSL}${WORKER_FILE}#spawned-worker`,
    172  });
    173  checkStartWorkerLogMessage(startLogFromSpawnedWorkerInIframe, {
    174    expectedUrl: `${URL_ROOT_ORG_SSL}${WORKER_FILE}#spawned-worker-in-iframe`,
    175  });
    176  checkLogInWorkerMessage(
    177    liveMessageFromSpawnedWorkerInMainPage,
    178    "live message in spawned worker from main page"
    179  );
    180  checkLogInWorkerMessage(
    181    liveMessageFromSpawnedWorkerInIframe,
    182    "live message in spawned worker from iframe"
    183  );
    184  // update the current number of resources received
    185  messageCount = resources.length;
    186 
    187  info(
    188    "Add a remote iframe on the same origin we already have an iframe and check we get the messages"
    189  );
    190  await SpecialPowers.spawn(
    191    tab.linkedBrowser,
    192    [IFRAME_FILE],
    193    async iframeUrl => {
    194      const iframe = content.document.createElement("iframe");
    195      iframe.src = `${iframeUrl}?hashSuffix=in-second-iframe`;
    196      content.document.body.append(iframe);
    197    }
    198  );
    199 
    200  info("Wait until the new log is available");
    201  await waitUntil(
    202    () => resources.length === messageCount + 1,
    203    `Couldn't get the expected number of resources (expected ${
    204      messageCount + 1
    205    }, got ${resources.length})`
    206  );
    207  const startLogFromWorkerInSecondIframe = resources[resources.length - 1];
    208  checkStartWorkerLogMessage(startLogFromWorkerInSecondIframe, {
    209    expectedUrl: `${URL_ROOT_ORG_SSL}${WORKER_FILE}#simple-worker-in-second-iframe`,
    210  });
    211 
    212  targetCommand.destroy();
    213  await client.close();
    214 
    215  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    216    // registrationPromise is set by the test page.
    217    const registration = await content.wrappedJSObject.registrationPromise;
    218    registration.unregister();
    219  });
    220 });
    221 
    222 function checkStartWorkerLogMessage(
    223  resource,
    224  { expectedUrl, isAlreadyExistingResource = false }
    225 ) {
    226  const [firstArg, secondArg, thirdArg] = resource.arguments;
    227  is(firstArg, "[WORKER] started", "Got the expected first argument");
    228  is(secondArg, expectedUrl, "expected url was logged");
    229  is(
    230    thirdArg?._grip?.class,
    231    "DedicatedWorkerGlobalScope",
    232    "the global scope was logged as expected"
    233  );
    234  is(
    235    resource.isAlreadyExistingResource,
    236    isAlreadyExistingResource,
    237    "Resource has expected value for isAlreadyExistingResource"
    238  );
    239 }
    240 
    241 function checkLogInWorkerMessage(resource, expectedMessage) {
    242  const [firstArg, secondArg, thirdArg] = resource.arguments;
    243  is(firstArg, "[WORKER]", "Got the expected first argument");
    244  is(secondArg, expectedMessage, "expected message was logged");
    245  is(
    246    thirdArg?._grip?.class,
    247    "MessageEvent",
    248    "the message event object was logged as expected"
    249  );
    250  is(
    251    resource.isAlreadyExistingResource,
    252    false,
    253    "Resource has expected value for isAlreadyExistingResource"
    254  );
    255 }