tor-browser

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

browser-console.js (3771B)


      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 WebConsole = require("resource://devtools/client/webconsole/webconsole.js");
      8 const { Utils } = require("resource://devtools/client/webconsole/utils.js");
      9 
     10 loader.lazyRequireGetter(
     11  this,
     12  "Telemetry",
     13  "resource://devtools/client/shared/telemetry.js"
     14 );
     15 loader.lazyRequireGetter(
     16  this,
     17  "BrowserConsoleManager",
     18  "resource://devtools/client/webconsole/browser-console-manager.js",
     19  true
     20 );
     21 
     22 /**
     23 * A BrowserConsole instance is an interactive console initialized *per commands*
     24 * that displays console log data as well as provides an interactive terminal to
     25 * manipulate all browser debuggable context and targeted by default at the current
     26 * top-level window's document.
     27 *
     28 * This object only wraps the iframe that holds the Browser Console UI. This is
     29 * meant to be an integration point between the Firefox UI and the Browser Console
     30 * UI and features.
     31 *
     32 * This object extends the WebConsole object located in webconsole.js
     33 */
     34 class BrowserConsole extends WebConsole {
     35  #bcInitializer = null;
     36  #bcDestroyer = null;
     37  #telemetry;
     38  /**
     39   * @class
     40   * @param object commands
     41   *        The commands object with all interfaces defined from devtools/shared/commands/
     42   * @param nsIDOMWindow iframeWindow
     43   *        The window where the browser console UI is already loaded.
     44   * @param nsIDOMWindow chromeWindow
     45   *        The window of the browser console owner.
     46   */
     47  constructor(commands, iframeWindow, chromeWindow) {
     48    super(null, commands, iframeWindow, chromeWindow, true);
     49 
     50    this.#telemetry = new Telemetry();
     51  }
     52 
     53  /**
     54   * Initialize the Browser Console instance.
     55   *
     56   * @return object
     57   *         A promise for the initialization.
     58   */
     59  init() {
     60    if (this.#bcInitializer) {
     61      return this.#bcInitializer;
     62    }
     63 
     64    this.#bcInitializer = (async () => {
     65      // Only add the shutdown observer if we've opened a Browser Console window.
     66      ShutdownObserver.init();
     67 
     68      this.#telemetry.toolOpened("browserconsole", this);
     69 
     70      await super.init(false);
     71 
     72      // Reports the console as created only after everything is done,
     73      // including TargetCommand.startListening.
     74      const id = Utils.supportsString(this.hudId);
     75      Services.obs.notifyObservers(id, "web-console-created");
     76    })();
     77    return this.#bcInitializer;
     78  }
     79 
     80  /**
     81   * Destroy the object.
     82   *
     83   * @return object
     84   *         A promise object that is resolved once the Browser Console is closed.
     85   */
     86  destroy() {
     87    if (this.#bcDestroyer) {
     88      return this.#bcDestroyer;
     89    }
     90 
     91    this.#bcDestroyer = (async () => {
     92      this.#telemetry.toolClosed("browserconsole", this);
     93 
     94      this.commands.targetCommand.destroy();
     95      await super.destroy();
     96      await this.currentTarget.destroy();
     97      this.chromeWindow.close();
     98    })();
     99 
    100    return this.#bcDestroyer;
    101  }
    102 
    103  updateWindowTitle() {
    104    BrowserConsoleManager.updateWindowTitle(this.chromeWindow);
    105  }
    106 }
    107 
    108 /**
    109 * The ShutdownObserver listens for app shutdown and saves the current state
    110 * of the Browser Console for session restore.
    111 */
    112 var ShutdownObserver = {
    113  _initialized: false,
    114 
    115  init() {
    116    if (this._initialized) {
    117      return;
    118    }
    119 
    120    Services.obs.addObserver(this, "quit-application-granted");
    121 
    122    this._initialized = true;
    123  },
    124 
    125  observe(message, topic) {
    126    if (topic == "quit-application-granted") {
    127      BrowserConsoleManager.storeBrowserConsoleSessionState();
    128      this.uninit();
    129    }
    130  },
    131 
    132  uninit() {
    133    Services.obs.removeObserver(this, "quit-application-granted");
    134  },
    135 };
    136 
    137 module.exports = BrowserConsole;