tor-browser

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

browser_events_interception.js (3386B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { RootMessageHandlerRegistry } = ChromeUtils.importESModule(
      7  "chrome://remote/content/shared/messagehandler/RootMessageHandlerRegistry.sys.mjs"
      8 );
      9 const { RootMessageHandler } = ChromeUtils.importESModule(
     10  "chrome://remote/content/shared/messagehandler/RootMessageHandler.sys.mjs"
     11 );
     12 
     13 /**
     14 * Test that events can be intercepted in the windowglobal-in-root layer.
     15 */
     16 add_task(async function test_intercepted_event() {
     17  const tab = BrowserTestUtils.addTab(
     18    gBrowser,
     19    "https://example.com/document-builder.sjs?html=tab"
     20  );
     21  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
     22  const browsingContext = tab.linkedBrowser.browsingContext;
     23 
     24  const rootMessageHandler = createRootMessageHandler(
     25    "session-id-intercepted_event"
     26  );
     27 
     28  const onInterceptedEvent = rootMessageHandler.once(
     29    "event.testEventWithInterception"
     30  );
     31  rootMessageHandler.handleCommand({
     32    moduleName: "event",
     33    commandName: "testEmitEventWithInterception",
     34    destination: {
     35      type: WindowGlobalMessageHandler.type,
     36      id: browsingContext.id,
     37    },
     38  });
     39 
     40  const interceptedEvent = await onInterceptedEvent;
     41  is(
     42    interceptedEvent.additionalInformation,
     43    "information added through interception",
     44    "Intercepted event contained additional information"
     45  );
     46 
     47  rootMessageHandler.destroy();
     48  gBrowser.removeTab(tab);
     49 });
     50 
     51 /**
     52 * Test that events can be canceled in the windowglobal-in-root layer.
     53 */
     54 add_task(async function test_cancelable_event() {
     55  const tab = BrowserTestUtils.addTab(
     56    gBrowser,
     57    "https://example.com/document-builder.sjs?html=tab"
     58  );
     59  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
     60  const browsingContext = tab.linkedBrowser.browsingContext;
     61 
     62  const rootMessageHandler = createRootMessageHandler(
     63    "session-id-cancelable_event"
     64  );
     65 
     66  const cancelableEvents = [];
     67  const onCancelableEvent = (name, event) => cancelableEvents.push(event);
     68  rootMessageHandler.on(
     69    "event.testEventCancelableWithInterception",
     70    onCancelableEvent
     71  );
     72 
     73  // Emit an event that should be canceled in the windowglobal-in-root layer.
     74  // Note that `shouldCancel` is only something supported for this test event,
     75  // and not a general message handler mechanism to cancel events.
     76  await rootMessageHandler.handleCommand({
     77    moduleName: "event",
     78    commandName: "testEmitEventCancelableWithInterception",
     79    destination: {
     80      type: WindowGlobalMessageHandler.type,
     81      id: browsingContext.id,
     82    },
     83    params: {
     84      shouldCancel: true,
     85    },
     86  });
     87 
     88  is(cancelableEvents.length, 0, "No event was received");
     89 
     90  // Emit another event which should not be canceled (shouldCancel: false).
     91  await rootMessageHandler.handleCommand({
     92    moduleName: "event",
     93    commandName: "testEmitEventCancelableWithInterception",
     94    destination: {
     95      type: WindowGlobalMessageHandler.type,
     96      id: browsingContext.id,
     97    },
     98    params: {
     99      shouldCancel: false,
    100    },
    101  });
    102 
    103  await TestUtils.waitForCondition(() => cancelableEvents.length == 1);
    104  is(cancelableEvents[0].shouldCancel, false, "Expected event was received");
    105 
    106  rootMessageHandler.off(
    107    "event.testEventCancelableWithInterception",
    108    onCancelableEvent
    109  );
    110  rootMessageHandler.destroy();
    111  gBrowser.removeTab(tab);
    112 });