tor-browser

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

har-metadata-collector.js (2090B)


      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 const {
      8  TYPES,
      9 } = require("resource://devtools/shared/commands/resource/resource-command.js");
     10 
     11 /**
     12 * This collector class is dedicated to recording additional metadata necessary
     13 * to build HAR files. The actual request data will be provided by the
     14 * netmonitor which is already monitoring for requests.
     15 *
     16 * The only purpose of this class is to record additional document and network
     17 * events, which will help to assign requests to individual pages.
     18 *
     19 * It should be created and destroyed by the main netmonitor data collector.
     20 */
     21 class HarMetadataCollector {
     22  #commands;
     23  #initialURL;
     24  #navigationRequests;
     25 
     26  constructor(commands) {
     27    this.#commands = commands;
     28  }
     29 
     30  /**
     31   * Stop recording and clear the state.
     32   */
     33  destroy() {
     34    this.clear();
     35 
     36    this.#commands.resourceCommand.unwatchResources([TYPES.NETWORK_EVENT], {
     37      onAvailable: this.#onResourceAvailable,
     38    });
     39  }
     40 
     41  /**
     42   * Reset the current state.
     43   */
     44  clear() {
     45    // Keep the initial URL for requests captured before the first recorded
     46    // navigation.
     47    this.#initialURL = this.#commands.targetCommand.targetFront.url;
     48    this.#navigationRequests = [];
     49  }
     50 
     51  /**
     52   * Start recording additional events for HAR files building.
     53   */
     54  async connect() {
     55    this.clear();
     56 
     57    await this.#commands.resourceCommand.watchResources([TYPES.NETWORK_EVENT], {
     58      onAvailable: this.#onResourceAvailable,
     59    });
     60  }
     61 
     62  getHarData() {
     63    return {
     64      initialURL: this.#initialURL,
     65      navigationRequests: this.#navigationRequests,
     66    };
     67  }
     68 
     69  #onResourceAvailable = resources => {
     70    for (const resource of resources) {
     71      if (
     72        resource.resourceType === TYPES.NETWORK_EVENT &&
     73        resource.isNavigationRequest
     74      ) {
     75        this.#navigationRequests.push(resource);
     76      }
     77    }
     78  };
     79 }
     80 
     81 exports.HarMetadataCollector = HarMetadataCollector;