tor-browser

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

network-command.js (3527B)


      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 "use strict";
      6 
      7 // eslint-disable-next-line mozilla/reject-some-requires
      8 const DESCRIPTOR_TYPES = require("resource://devtools/client/fronts/descriptors/descriptor-types.js");
      9 
     10 class NetworkCommand {
     11  /**
     12   * This class helps listen, inspect and control network requests.
     13   *
     14   * @param {DescriptorFront} descriptorFront
     15   *        The context to inspect identified by this descriptor.
     16   * @param {WatcherFront} watcherFront
     17   *        If available, a reference to the related Watcher Front.
     18   * @param {object} commands
     19   *        The commands object with all interfaces defined from devtools/shared/commands/
     20   */
     21  constructor({ descriptorFront, watcherFront, commands }) {
     22    this.commands = commands;
     23    this.descriptorFront = descriptorFront;
     24    this.watcherFront = watcherFront;
     25  }
     26 
     27  /**
     28   * Send a HTTP request data payload
     29   *
     30   * @param {object} data data payload would like to sent to backend
     31   */
     32  async sendHTTPRequest(data) {
     33    // By default use the currently selected target.
     34    // So depending on the console and iframe selectors, the request may be sent
     35    // from different documents and be drastically different.
     36    const targetFront =
     37      this.descriptorFront.descriptorType == DESCRIPTOR_TYPES.EXTENSION
     38        ? this.commands.targetCommand.selectedTargetFront
     39        : this.commands.targetCommand.targetFront;
     40    const networkContentFront = await targetFront.getFront("networkContent");
     41    const { channelId } = await networkContentFront.sendHTTPRequest(data);
     42    return { channelId };
     43  }
     44 
     45  /**
     46   * Get the list of blocked URL filters.
     47   *
     48   * A URL filter is a RegExp string so that one filter can match many URLs.
     49   * It can be an absolute URL to match only one precise request:
     50   *   http://mozilla.org/index.html
     51   * Or just a string which would match all URL containing this string:
     52   *   mozilla
     53   * Or a RegExp to match various types of URLs:
     54   *   http://*mozilla.org/*.css
     55   *
     56   * @return {Array}
     57   *         List of all currently blocked URL filters.
     58   */
     59  async getBlockedUrls() {
     60    const networkParentFront = await this.watcherFront.getNetworkParentActor();
     61    return networkParentFront.getBlockedUrls();
     62  }
     63 
     64  /**
     65   * Updates the list of blocked URL filters.
     66   *
     67   * @param {Array} urls
     68   *        An array of URL filter strings.
     69   *        See getBlockedUrls for definition of URL filters.
     70   */
     71  async setBlockedUrls(urls) {
     72    const networkParentFront = await this.watcherFront.getNetworkParentActor();
     73    return networkParentFront.setBlockedUrls(urls);
     74  }
     75 
     76  /**
     77   * Block only one additional URL filter
     78   *
     79   * @param {string} url
     80   *        URL filter to block.
     81   *        See getBlockedUrls for definition of URL filters.
     82   */
     83  async blockRequestForUrl(url) {
     84    const networkParentFront = await this.watcherFront.getNetworkParentActor();
     85    return networkParentFront.blockRequest({ url });
     86  }
     87 
     88  /**
     89   * Stop blocking only one specific URL filter
     90   *
     91   * @param {string} url
     92   *        URL filter to unblock.
     93   *        See getBlockedUrls for definition of URL filters.
     94   */
     95  async unblockRequestForUrl(url) {
     96    const networkParentFront = await this.watcherFront.getNetworkParentActor();
     97    return networkParentFront.unblockRequest({ url });
     98  }
     99 
    100  destroy() {}
    101 }
    102 
    103 module.exports = NetworkCommand;