tor-browser

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

legacy-processes-watcher.js (2300B)


      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 class LegacyProcessesWatcher {
      8  constructor(targetCommand, onTargetAvailable, onTargetDestroyed) {
      9    this.targetCommand = targetCommand;
     10    this.rootFront = targetCommand.rootFront;
     11 
     12    this.onTargetAvailable = onTargetAvailable;
     13    this.onTargetDestroyed = onTargetDestroyed;
     14 
     15    this.descriptors = new Set();
     16    this._processListChanged = this._processListChanged.bind(this);
     17  }
     18 
     19  async _processListChanged() {
     20    if (this.targetCommand.isDestroyed()) {
     21      return;
     22    }
     23 
     24    const processes = await this.rootFront.listProcesses();
     25    // Process the new list to detect the ones being destroyed
     26    // Force destroyed the descriptor as well as the target
     27    for (const descriptor of this.descriptors) {
     28      if (!processes.includes(descriptor)) {
     29        // Manually call onTargetDestroyed listeners in order to
     30        // ensure calling them *before* destroying the descriptor.
     31        // Otherwise the descriptor will automatically destroy the target
     32        // and may not fire the contentProcessTarget's destroy event.
     33        const target = descriptor.getCachedTarget();
     34        if (target) {
     35          this.onTargetDestroyed(target);
     36        }
     37 
     38        descriptor.destroy();
     39        this.descriptors.delete(descriptor);
     40      }
     41    }
     42 
     43    const promises = processes
     44      .filter(descriptor => !this.descriptors.has(descriptor))
     45      .map(async descriptor => {
     46        // Add the new process descriptors to the local list
     47        this.descriptors.add(descriptor);
     48        const target = await descriptor.getTarget();
     49        if (!target) {
     50          console.error(
     51            "Wasn't able to retrieve the target for",
     52            descriptor.actorID
     53          );
     54          return;
     55        }
     56        await this.onTargetAvailable(target);
     57      });
     58 
     59    await Promise.all(promises);
     60  }
     61 
     62  async listen() {
     63    this.rootFront.on("processListChanged", this._processListChanged);
     64    await this._processListChanged();
     65  }
     66 
     67  unlisten() {
     68    this.rootFront.off("processListChanged", this._processListChanged);
     69  }
     70 }
     71 
     72 module.exports = LegacyProcessesWatcher;