tor-browser

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

GeckoViewTestUtils.sys.mjs (1994B)


      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 lazy = {};
      6 
      7 ChromeUtils.defineESModuleGetters(lazy, {
      8  EventDispatcher: "resource://gre/modules/Messaging.sys.mjs",
      9 });
     10 
     11 export const GeckoViewTabUtil = {
     12  /**
     13   * Creates a new tab through service worker delegate.
     14   * Needs to be ran in a parent process.
     15   *
     16   * @param {string} url
     17   * @returns {Tab}
     18   * @throws {Error} Throws an error if the tab cannot be created.
     19   */
     20  async createNewTab(url = "about:blank") {
     21    let sessionId = undefined;
     22    const windowPromise = new Promise(resolve => {
     23      const openingObserver = subject => {
     24        if (sessionId !== undefined && subject.name === sessionId) {
     25          Services.obs.removeObserver(
     26            openingObserver,
     27            "browser-delayed-startup-finished"
     28          );
     29          resolve(subject);
     30        }
     31      };
     32      Services.obs.addObserver(
     33        openingObserver,
     34        "browser-delayed-startup-finished"
     35      );
     36    });
     37 
     38    try {
     39      sessionId = await lazy.EventDispatcher.instance.sendRequestForResult({
     40        type: "GeckoView:Test:NewTab",
     41        url,
     42      });
     43    } catch (errorMessage) {
     44      throw new Error(
     45        errorMessage + " GeckoView:Test:NewTab is not supported."
     46      );
     47    }
     48 
     49    if (!sessionId) {
     50      throw new Error("Could not open a session for the new tab.");
     51    }
     52 
     53    const window = await windowPromise;
     54 
     55    // Immediately load the URI in the browser after creating the new tab to
     56    // load into. This isn't done from the Java side to align with the
     57    // ServiceWorkerOpenWindow infrastructure which this is built on top of.
     58    window.browser.fixupAndLoadURIString(url, {
     59      loadFlags: Ci.nsIWebNavigation.LOAD_FLAGS_NONE,
     60      triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
     61    });
     62 
     63    return window.tab;
     64  },
     65 };