tor-browser

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

process.js (2159B)


      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 loader.lazyGetter(this, "ppmm", () => {
      8  return Cc["@mozilla.org/parentprocessmessagemanager;1"].getService();
      9 });
     10 
     11 class ProcessActorList {
     12  constructor() {
     13    this._actors = new Map();
     14    this._onListChanged = null;
     15    this._mustNotify = false;
     16    this._hasObserver = false;
     17  }
     18 
     19  getList() {
     20    const processes = [];
     21    for (let i = 0; i < ppmm.childCount; i++) {
     22      const mm = ppmm.getChildAt(i);
     23      processes.push({
     24        // An ID of zero is always used for the parent. It would be nice to fix
     25        // this so that the pid is also used for the parent, see bug 1587443.
     26        id: mm.isInProcess ? 0 : mm.osPid,
     27        parent: mm.isInProcess,
     28        // TODO: exposes process message manager on frameloaders in order to compute this
     29        tabCount: undefined,
     30      });
     31    }
     32    this._mustNotify = true;
     33    this._checkListening();
     34 
     35    return processes;
     36  }
     37 
     38  get onListChanged() {
     39    return this._onListChanged;
     40  }
     41 
     42  set onListChanged(onListChanged) {
     43    if (typeof onListChanged !== "function" && onListChanged !== null) {
     44      throw new Error("onListChanged must be either a function or null.");
     45    }
     46    if (onListChanged === this._onListChanged) {
     47      return;
     48    }
     49 
     50    this._onListChanged = onListChanged;
     51    this._checkListening();
     52  }
     53 
     54  _checkListening() {
     55    if (this._onListChanged !== null && this._mustNotify) {
     56      if (!this._hasObserver) {
     57        Services.obs.addObserver(this, "ipc:content-created");
     58        Services.obs.addObserver(this, "ipc:content-shutdown");
     59        this._hasObserver = true;
     60      }
     61    } else if (this._hasObserver) {
     62      Services.obs.removeObserver(this, "ipc:content-created");
     63      Services.obs.removeObserver(this, "ipc:content-shutdown");
     64      this._hasObserver = false;
     65    }
     66  }
     67 
     68  observe() {
     69    if (this._mustNotify) {
     70      this._onListChanged();
     71      this._mustNotify = false;
     72    }
     73  }
     74 }
     75 
     76 exports.ProcessActorList = ProcessActorList;