tor-browser

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

browser_resources_scope_flag.js (3666B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that the ResourceCommand clears its pending events for resources emitted from
      7 // target destroyed when devtools.browsertoolbox.scope is updated.
      8 
      9 const TEST_URL =
     10  "data:text/html;charset=utf-8," + encodeURIComponent(`<div id="test"></div>`);
     11 
     12 add_task(async function () {
     13  // Disable the preloaded process as it gets created lazily and may interfere
     14  // with process count assertions
     15  await pushPref("dom.ipc.processPrelaunch.enabled", false);
     16  // This preference helps destroying the content process when we close the tab
     17  await pushPref("dom.ipc.keepProcessesAlive.web", 1);
     18 
     19  // Start with multiprocess debugging enabled
     20  await pushPref("devtools.browsertoolbox.scope", "everything");
     21 
     22  const commands = await CommandsFactory.forMainProcess();
     23  const targetCommand = commands.targetCommand;
     24  await targetCommand.startListening();
     25  const { TYPES } = targetCommand;
     26 
     27  const targets = new Set();
     28  const onAvailable = async ({ targetFront }) => {
     29    targets.add(targetFront);
     30  };
     31  const onDestroyed = () => {};
     32  await targetCommand.watchTargets({
     33    types: [TYPES.PROCESS, TYPES.FRAME],
     34    onAvailable,
     35    onDestroyed,
     36  });
     37 
     38  info("Open a tab in a new content process");
     39  const firstTab = await BrowserTestUtils.openNewForegroundTab({
     40    gBrowser,
     41    url: TEST_URL,
     42    forceNewProcess: true,
     43  });
     44 
     45  const newTabInnerWindowId =
     46    firstTab.linkedBrowser.browsingContext.currentWindowGlobal.innerWindowId;
     47 
     48  info("Wait for the tab window global target");
     49  const windowGlobalTarget = await waitFor(() =>
     50    [...targets].find(
     51      target =>
     52        target.targetType == TYPES.FRAME &&
     53        target.innerWindowId == newTabInnerWindowId
     54    )
     55  );
     56 
     57  let gotTabResource = false;
     58  const onResourceAvailable = resources => {
     59    for (const resource of resources) {
     60      if (resource.targetFront == windowGlobalTarget) {
     61        gotTabResource = true;
     62 
     63        if (resource.targetFront.isDestroyed()) {
     64          ok(
     65            false,
     66            "we shouldn't get resources for the target that was destroyed when switching mode"
     67          );
     68        }
     69      }
     70    }
     71  };
     72 
     73  info("Start listening for resources");
     74  await commands.resourceCommand.watchResources(
     75    [commands.resourceCommand.TYPES.CONSOLE_MESSAGE],
     76    {
     77      onAvailable: onResourceAvailable,
     78      ignoreExistingResources: true,
     79    }
     80  );
     81 
     82  // Emit logs every ms to fill up the resourceCommand resource queue (pendingEvents)
     83  const intervalId = await SpecialPowers.spawn(
     84    firstTab.linkedBrowser,
     85    [],
     86    () => {
     87      let counter = 0;
     88      return content.wrappedJSObject.setInterval(() => {
     89        counter++;
     90        content.wrappedJSObject.console.log("STREAM_" + counter);
     91      }, 1);
     92    }
     93  );
     94 
     95  info("Wait until we get the first resource");
     96  await waitFor(() => gotTabResource);
     97 
     98  info("Disable multiprocess debugging");
     99  await pushPref("devtools.browsertoolbox.scope", "parent-process");
    100 
    101  info("Wait for the tab target to be destroyed");
    102  await waitFor(() => windowGlobalTarget.isDestroyed());
    103 
    104  info("Wait for a bit so any throttled action would have the time to occur");
    105  await wait(1000);
    106 
    107  // Stop listening for resources
    108  await commands.resourceCommand.unwatchResources(
    109    [commands.resourceCommand.TYPES.CONSOLE_MESSAGE],
    110    {
    111      onAvailable: onResourceAvailable,
    112    }
    113  );
    114  // And stop the interval
    115  await SpecialPowers.spawn(firstTab.linkedBrowser, [intervalId], id => {
    116    content.wrappedJSObject.clearInterval(id);
    117  });
    118 
    119  targetCommand.destroy();
    120  await commands.destroy();
    121 });