tor-browser

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

ext-devtools-network.js (2646B)


      1 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* vim: set sts=2 sw=2 et tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 "use strict";
      8 
      9 var { SpreadArgs } = ExtensionCommon;
     10 
     11 var { ExtensionError } = ExtensionUtils;
     12 
     13 this.devtools_network = class extends ExtensionAPI {
     14  getAPI(context) {
     15    return {
     16      devtools: {
     17        network: {
     18          onNavigated: new EventManager({
     19            context,
     20            name: "devtools.onNavigated",
     21            register: fire => {
     22              const listener = url => {
     23                fire.async(url);
     24              };
     25 
     26              const promise = context.addOnNavigatedListener(listener);
     27              return () => {
     28                promise.then(() => {
     29                  context.removeOnNavigatedListener(listener);
     30                });
     31              };
     32            },
     33          }).api(),
     34 
     35          getHAR: function () {
     36            return context.devToolsToolbox.getHARFromNetMonitor();
     37          },
     38 
     39          onRequestFinished: new EventManager({
     40            context,
     41            name: "devtools.network.onRequestFinished",
     42            register: fire => {
     43              const listener = data => {
     44                fire.async(data);
     45              };
     46 
     47              const toolbox = context.devToolsToolbox;
     48              toolbox.addRequestFinishedListener(listener);
     49 
     50              return () => {
     51                toolbox.removeRequestFinishedListener(listener);
     52              };
     53            },
     54          }).api(),
     55 
     56          // The following method is used internally to allow the request API
     57          // piece that is running in the child process to ask the parent process
     58          // to fetch response content from the back-end.
     59          Request: {
     60            async getContent(requestId) {
     61              return context.devToolsToolbox
     62                .fetchResponseContent(requestId)
     63                .then(
     64                  ({ content }) =>
     65                    new SpreadArgs([content.text, content.mimeType])
     66                )
     67                .catch(err => {
     68                  const debugName = context.extension.policy.debugName;
     69                  const errorMsg =
     70                    "Unexpected error while fetching response content";
     71                  Cu.reportError(
     72                    `${debugName}: ${errorMsg} for ${requestId}: ${err}`
     73                  );
     74                  throw new ExtensionError(errorMsg);
     75                });
     76            },
     77          },
     78        },
     79      },
     80    };
     81  }
     82 };