tor-browser

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

spotlight.js (4124B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 const browser = window.docShell.chromeEventHandler;
      6 const { document: gDoc, XPCOMUtils } = browser.ownerGlobal;
      7 
      8 ChromeUtils.defineESModuleGetters(this, {
      9  AboutWelcomeParent: "resource:///actors/AboutWelcomeParent.sys.mjs",
     10  E10SUtils: "resource://gre/modules/E10SUtils.sys.mjs",
     11 });
     12 
     13 const CONFIG = window.arguments[0];
     14 
     15 function addStylesheet(href) {
     16  const link = document.head.appendChild(document.createElement("link"));
     17  link.rel = "stylesheet";
     18  link.href = href;
     19 }
     20 
     21 function disableEscClose() {
     22  addEventListener("keydown", event => {
     23    if (event.key === "Escape") {
     24      event.preventDefault();
     25      event.stopPropagation();
     26    }
     27  });
     28 }
     29 
     30 /**
     31 * Render content based on about:welcome multistage template.
     32 */
     33 function renderMultistage(ready) {
     34  const AWParent = new AboutWelcomeParent();
     35  const receive = name => data =>
     36    AWParent.onContentMessage(`AWPage:${name}`, data, browser);
     37 
     38  // Expose top level functions expected by the bundle.
     39  window.AWGetFeatureConfig = () => CONFIG;
     40  window.AWGetSelectedTheme = receive("GET_SELECTED_THEME");
     41  window.AWGetInstalledAddons = receive("GET_INSTALLED_ADDONS");
     42  window.AWSelectTheme = data => receive("SELECT_THEME")(data?.toUpperCase());
     43  // Do not send telemetry if message (e.g. spotlight in PBM) config sets metrics as 'block'.
     44  if (CONFIG?.metrics !== "block") {
     45    window.AWSendEventTelemetry = receive("TELEMETRY_EVENT");
     46  }
     47  window.AWSendToDeviceEmailsSupported = receive(
     48    "SEND_TO_DEVICE_EMAILS_SUPPORTED"
     49  );
     50  window.AWAddScreenImpression = receive("ADD_SCREEN_IMPRESSION");
     51  window.AWSendToParent = (name, data) => receive(name)(data);
     52  window.AWFinish = () => {
     53    window.close();
     54  };
     55  window.AWWaitForMigrationClose = receive("WAIT_FOR_MIGRATION_CLOSE");
     56  window.AWEvaluateScreenTargeting = receive("EVALUATE_SCREEN_TARGETING");
     57  window.AWEvaluateAttributeTargeting = receive("EVALUATE_ATTRIBUTE_TARGETING");
     58  window.AWPredictRemoteType = ({ browserEl, url }) => {
     59    const originAttributes = E10SUtils.predictOriginAttributes({
     60      browser: browserEl,
     61    });
     62    const loadContext = window.docShell.QueryInterface(Ci.nsILoadContext);
     63    const useRemoteTabs = loadContext.useRemoteTabs;
     64    const useRemoteSubframes = loadContext.useRemoteSubframes;
     65 
     66    return E10SUtils.getRemoteTypeForURI(
     67      url,
     68      useRemoteTabs,
     69      useRemoteSubframes,
     70      E10SUtils.DEFAULT_REMOTE_TYPE,
     71      null,
     72      originAttributes
     73    );
     74  };
     75 
     76  // Update styling to be compatible with about:welcome.
     77  addStylesheet("chrome://browser/content/aboutwelcome/aboutwelcome.css");
     78 
     79  document.body.classList.add("onboardingContainer");
     80  document.body.id = "multi-stage-message-root";
     81  // This value is reported as the "page" in telemetry
     82  document.body.dataset.page = "spotlight";
     83 
     84  // Prevent applying the default modal shadow and margins because the content
     85  // handles styling, including its own modal shadowing.
     86  const box = browser.closest(".dialogBox");
     87  const dialog = box.closest("dialog");
     88  box.classList.add("spotlightBox");
     89  dialog?.classList.add("spotlight");
     90  // Prevent SubDialog methods from manually setting dialog size.
     91  box.setAttribute("sizeto", "available");
     92  box.setAttribute("fixedsize", "false");
     93  addEventListener("pagehide", () => {
     94    box.classList.remove("spotlightBox");
     95    dialog?.classList.remove("spotlight");
     96    box.removeAttribute("sizeto");
     97  });
     98 
     99  if (CONFIG?.disableEscClose) {
    100    disableEscClose();
    101  }
    102 
    103  // Load the bundle to render the content as configured.
    104  document.head.appendChild(document.createElement("script")).src =
    105    "chrome://browser/content/aboutwelcome/aboutwelcome.bundle.js";
    106  ready();
    107 }
    108 
    109 // Indicate when we're ready to show and size (async localized) content.
    110 document.mozSubdialogReady = new Promise(resolve =>
    111  document.addEventListener(
    112    "DOMContentLoaded",
    113    () => renderMultistage(resolve),
    114    {
    115      once: true,
    116    }
    117  )
    118 );