tor-browser

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

head.js (4226B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 var gActiveListeners = {};
      7 
      8 // These event (un)registration handlers only work for one window, DONOT use
      9 // them with multiple windows.
     10 function registerPopupEventHandler(eventName, callback, win) {
     11  if (!win) {
     12    win = window;
     13  }
     14  gActiveListeners[eventName] = function (event) {
     15    if (event.target != win.PopupNotifications.panel) {
     16      return;
     17    }
     18    win.PopupNotifications.panel.removeEventListener(
     19      eventName,
     20      gActiveListeners[eventName]
     21    );
     22    delete gActiveListeners[eventName];
     23 
     24    callback.call(win.PopupNotifications.panel);
     25  };
     26  win.PopupNotifications.panel.addEventListener(
     27    eventName,
     28    gActiveListeners[eventName]
     29  );
     30 }
     31 
     32 function unregisterPopupEventHandler(eventName, win) {
     33  if (!win) {
     34    win = window;
     35  }
     36  win.PopupNotifications.panel.removeEventListener(
     37    eventName,
     38    gActiveListeners[eventName]
     39  );
     40  delete gActiveListeners[eventName];
     41 }
     42 
     43 function unregisterAllPopupEventHandlers(win) {
     44  if (!win) {
     45    win = window;
     46  }
     47  for (let eventName in gActiveListeners) {
     48    win.PopupNotifications.panel.removeEventListener(
     49      eventName,
     50      gActiveListeners[eventName]
     51    );
     52  }
     53  gActiveListeners = {};
     54 }
     55 
     56 function triggerMainCommand(popup) {
     57  info("triggering main command");
     58  let notifications = popup.childNodes;
     59  ok(notifications.length, "at least one notification displayed");
     60  let notification = notifications[0];
     61  info("triggering command: " + notification.getAttribute("buttonlabel"));
     62 
     63  EventUtils.synthesizeMouseAtCenter(notification.button, {});
     64 }
     65 
     66 function triggerSecondaryCommand(popup, win) {
     67  if (!win) {
     68    win = window;
     69  }
     70  info("triggering secondary command");
     71  let notifications = popup.childNodes;
     72  ok(notifications.length, "at least one notification displayed");
     73  let notification = notifications[0];
     74  EventUtils.synthesizeMouseAtCenter(notification.secondaryButton, {}, win);
     75 }
     76 
     77 function dismissNotification() {
     78  info("dismissing notification");
     79  executeSoon(function () {
     80    EventUtils.synthesizeKey("KEY_Escape");
     81  });
     82 }
     83 
     84 function waitForMessage(aMessage, browser) {
     85  // We cannot capture aMessage inside the checkFn, so we override the
     86  // checkFn.toSource to tunnel aMessage instead.
     87  let checkFn = function () {};
     88  checkFn.toSource = function () {
     89    return `function checkFn(event) {
     90      let message = ${aMessage.toSource()};
     91      if (event.data == message) {
     92        return true;
     93      }
     94      throw new Error(
     95       \`Unexpected result: \$\{event.data\}, expected \$\{message\}\`
     96      );
     97    }`;
     98  };
     99 
    100  return BrowserTestUtils.waitForContentEvent(
    101    browser.selectedBrowser,
    102    "message",
    103    /* capture */ true,
    104    checkFn,
    105    /* wantsUntrusted */ true
    106  ).then(() => {
    107    // An assertion in checkFn wouldn't be recorded as part of the test, so we
    108    // use this assertion to confirm that we've successfully received the
    109    // message (we'll only reach this point if that's the case).
    110    ok(true, "Received message: " + aMessage);
    111  });
    112 }
    113 
    114 function dispatchEvent(eventName) {
    115  info("dispatching event: " + eventName);
    116  let event = document.createEvent("Events");
    117  event.initEvent(eventName, false, false);
    118  gBrowser.selectedBrowser.contentWindow.dispatchEvent(event);
    119 }
    120 
    121 function setPermission(url, permission, originAttributes = {}) {
    122  let uri = Services.io.newURI(url);
    123  let principal = Services.scriptSecurityManager.createContentPrincipal(
    124    uri,
    125    originAttributes
    126  );
    127 
    128  Services.perms.addFromPrincipal(
    129    principal,
    130    permission,
    131    Ci.nsIPermissionManager.ALLOW_ACTION
    132  );
    133 }
    134 
    135 function removePermission(url, permission, originAttributes = {}) {
    136  let uri = Services.io.newURI(url);
    137  let principal = Services.scriptSecurityManager.createContentPrincipal(
    138    uri,
    139    originAttributes
    140  );
    141 
    142  Services.perms.removeFromPrincipal(principal, permission);
    143 }
    144 
    145 function getPermission(url, permission, originAttributes = {}) {
    146  let uri = Services.io.newURI(url);
    147  let principal = Services.scriptSecurityManager.createContentPrincipal(
    148    uri,
    149    originAttributes
    150  );
    151 
    152  return Services.perms.testPermissionFromPrincipal(principal, permission);
    153 }