tor-browser

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

RootBiDiModule.sys.mjs (4580B)


      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 https://mozilla.org/MPL/2.0/. */
      4 
      5 import { Module } from "chrome://remote/content/shared/messagehandler/Module.sys.mjs";
      6 
      7 const lazy = {};
      8 
      9 ChromeUtils.defineESModuleGetters(lazy, {
     10  assert: "chrome://remote/content/shared/webdriver/Assert.sys.mjs",
     11  error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
     12  NavigableManager: "chrome://remote/content/shared/NavigableManager.sys.mjs",
     13  WindowGlobalMessageHandler:
     14    "chrome://remote/content/shared/messagehandler/WindowGlobalMessageHandler.sys.mjs",
     15 });
     16 
     17 export class RootBiDiModule extends Module {
     18  /**
     19   * Emits an event for a specific browsing context.
     20   *
     21   * @param {string} browsingContextId
     22   *     The ID of the browsing context to which the event should be emitted.
     23   * @param {string} eventName
     24   *     The name of the event to be emitted.
     25   * @param {object} eventPayload
     26   *     The payload to be sent with the event.
     27   * @returns {boolean}
     28   *     Returns `true` if the event was successfully emitted, otherwise `false`.
     29   */
     30  _emitEventForBrowsingContext(browsingContextId, eventName, eventPayload) {
     31    // This event is emitted from the parent process but for a given browsing
     32    // context. Set the event's contextInfo to the message handler corresponding
     33    // to this browsing context.
     34    const contextInfo = {
     35      contextId: browsingContextId,
     36      type: lazy.WindowGlobalMessageHandler.type,
     37    };
     38    return this.emitEvent(eventName, eventPayload, contextInfo);
     39  }
     40 
     41  /**
     42   * Retrieves a browsing context based on its navigable id.
     43   *
     44   * @see https://w3c.github.io/webdriver-bidi/#get-a-navigable
     45   *
     46   * @param {string} navigableId
     47   *     Unique id of the browsing context.
     48   * @param {object=} options
     49   * @param {boolean=} options.supportsChromeScope
     50   *     If set to `true` chrome browsing contexts are supported
     51   *     for the BiDi command. Defaults to `false`.
     52   *
     53   * @returns {BrowsingContext|null}
     54   *     The browsing context, or null if `navigableId` is null.
     55   *
     56   * @throws {NoSuchFrameError}
     57   *     If the browsing context cannot be found.
     58   */
     59  _getNavigable(navigableId, options = {}) {
     60    const { supportsChromeScope = false } = options;
     61 
     62    if (navigableId === null) {
     63      // The WebDriver BiDi specification expects `null` to be
     64      // returned if navigable id is `null`.
     65      return null;
     66    }
     67 
     68    const context = lazy.NavigableManager.getBrowsingContextById(navigableId);
     69 
     70    if (context && !context.isContent) {
     71      lazy.assert.hasSystemAccess();
     72 
     73      if (!supportsChromeScope) {
     74        throw new lazy.error.UnsupportedOperationError(
     75          "The command does not support browsing contexts in privileged (chrome) scope"
     76        );
     77      }
     78    }
     79 
     80    if (context === null) {
     81      throw new lazy.error.NoSuchFrameError(
     82        `Browsing Context with id ${navigableId} not found`
     83      );
     84    }
     85 
     86    return context;
     87  }
     88 
     89  /**
     90   * Checks if there is a listener for a specific event and context information.
     91   *
     92   * @param {string} eventName
     93   *     The name of the event to check for listeners.
     94   * @param {ContextInfo} contextInfo
     95   *     The context information to check for listeners within.
     96   * @returns {boolean}
     97   *     Returns `true` if there is at least one listener for the specified event and context, otherwise `false`.
     98   */
     99  _hasListener(eventName, contextInfo) {
    100    return this.messageHandler.eventsDispatcher.hasListener(
    101      eventName,
    102      contextInfo
    103    );
    104  }
    105 
    106  /**
    107   * Forwards a command to the windowglobal module corresponding to the provided
    108   * browsing context id, using the same module name as the current one.
    109   *
    110   * @param {string} commandName
    111   *     The name of the command to execute.
    112   * @param {number} browsingContextID
    113   *     The debuggable context ID.
    114   * @param {object} params
    115   *    Any command parameters to pass.
    116   * @param {object=} args
    117   *     Any additional command arguments to pass.
    118   * @returns {Promise}
    119   *     A Promise that will resolve with the return value of the
    120   *     command once it has been executed.
    121   */
    122  _forwardToWindowGlobal(commandName, browsingContextID, params, args = {}) {
    123    return this.messageHandler.forwardCommand({
    124      moduleName: this.moduleName,
    125      commandName,
    126      destination: {
    127        type: lazy.WindowGlobalMessageHandler.type,
    128        id: browsingContextID,
    129      },
    130      ...args,
    131      params,
    132    });
    133  }
    134 }