tor-browser

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

browser_bfcache.js (3538B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { RootMessageHandler } = ChromeUtils.importESModule(
      7  "chrome://remote/content/shared/messagehandler/RootMessageHandler.sys.mjs"
      8 );
      9 
     10 const TEST_PREF = "remote.messagehandler.test.pref";
     11 
     12 // Check that pages in bfcache no longer have message handlers attached to them,
     13 // and that they will not emit unexpected events.
     14 add_task(async function test_bfcache_broadcast() {
     15  const tab = await addTab("https://example.com/document-builder.sjs?html=tab");
     16  const rootMessageHandler = createRootMessageHandler("session-id-bfcache");
     17 
     18  try {
     19    const browsingContext = tab.linkedBrowser.browsingContext;
     20    const contextDescriptor = {
     21      type: ContextDescriptorType.TopBrowsingContext,
     22      id: browsingContext.browserId,
     23    };
     24 
     25    // Whenever a "preference-changed" event from the eventonprefchange module
     26    // will be received on the root MessageHandler, increment a counter.
     27    let preferenceChangeEventCount = 0;
     28    const onEvent = (evtName, wrappedEvt) => {
     29      if (wrappedEvt.name === "preference-changed") {
     30        preferenceChangeEventCount++;
     31      }
     32    };
     33    rootMessageHandler.on("message-handler-event", onEvent);
     34 
     35    // Initialize the preference, no eventonprefchange module should be created
     36    // yet so preferenceChangeEventCount is not expected to be updated.
     37    Services.prefs.setIntPref(TEST_PREF, 0);
     38    await TestUtils.waitForCondition(() => preferenceChangeEventCount >= 0);
     39    is(preferenceChangeEventCount, 0);
     40 
     41    // Broadcast a "ping" command to force the creation of the eventonprefchange
     42    // module
     43    let values = await sendPingCommand(rootMessageHandler, contextDescriptor);
     44    is(values.length, 1, "Broadcast returned a single value");
     45 
     46    Services.prefs.setIntPref(TEST_PREF, 1);
     47    await TestUtils.waitForCondition(() => preferenceChangeEventCount >= 1);
     48    is(preferenceChangeEventCount, 1);
     49 
     50    info("Navigate to another page");
     51    await loadURL(
     52      tab.linkedBrowser,
     53      "https://example.com/document-builder.sjs?html=othertab"
     54    );
     55 
     56    values = await sendPingCommand(rootMessageHandler, contextDescriptor);
     57    is(values.length, 1, "Broadcast returned a single value after navigation");
     58 
     59    info("Update the preference and check we only receive 1 event");
     60    Services.prefs.setIntPref(TEST_PREF, 2);
     61    await TestUtils.waitForCondition(() => preferenceChangeEventCount >= 2);
     62    is(preferenceChangeEventCount, 2);
     63 
     64    info("Navigate to another origin");
     65    await loadURL(
     66      tab.linkedBrowser,
     67      "https://example.org/document-builder.sjs?html=otherorigin"
     68    );
     69 
     70    values = await sendPingCommand(rootMessageHandler, contextDescriptor);
     71    is(
     72      values.length,
     73      1,
     74      "Broadcast returned a single value after cross origin navigation"
     75    );
     76 
     77    info("Update the preference and check again that we only receive 1 event");
     78    Services.prefs.setIntPref(TEST_PREF, 3);
     79    await TestUtils.waitForCondition(() => preferenceChangeEventCount >= 3);
     80    is(preferenceChangeEventCount, 3);
     81  } finally {
     82    rootMessageHandler.destroy();
     83    gBrowser.removeTab(tab);
     84    Services.prefs.clearUserPref(TEST_PREF);
     85  }
     86 });
     87 
     88 function sendPingCommand(rootMessageHandler, contextDescriptor) {
     89  return rootMessageHandler.handleCommand({
     90    moduleName: "eventonprefchange",
     91    commandName: "ping",
     92    params: {},
     93    destination: {
     94      contextDescriptor,
     95      type: WindowGlobalMessageHandler.type,
     96    },
     97  });
     98 }