summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/process.js
blob: 2ed2da64c0249c24b80005c4a9b6b52647556c23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

loader.lazyGetter(this, "ppmm", () => {
  return Cc["@mozilla.org/parentprocessmessagemanager;1"].getService();
});

class ProcessActorList {
  constructor() {
    this._actors = new Map();
    this._onListChanged = null;
    this._mustNotify = false;
    this._hasObserver = false;
  }

  getList() {
    const processes = [];
    for (let i = 0; i < ppmm.childCount; i++) {
      const mm = ppmm.getChildAt(i);
      processes.push({
        // An ID of zero is always used for the parent. It would be nice to fix
        // this so that the pid is also used for the parent, see bug 1587443.
        id: mm.isInProcess ? 0 : mm.osPid,
        parent: mm.isInProcess,
        // TODO: exposes process message manager on frameloaders in order to compute this
        tabCount: undefined,
      });
    }
    this._mustNotify = true;
    this._checkListening();

    return processes;
  }

  get onListChanged() {
    return this._onListChanged;
  }

  set onListChanged(onListChanged) {
    if (typeof onListChanged !== "function" && onListChanged !== null) {
      throw new Error("onListChanged must be either a function or null.");
    }
    if (onListChanged === this._onListChanged) {
      return;
    }

    this._onListChanged = onListChanged;
    this._checkListening();
  }

  _checkListening() {
    if (this._onListChanged !== null && this._mustNotify) {
      if (!this._hasObserver) {
        Services.obs.addObserver(this, "ipc:content-created");
        Services.obs.addObserver(this, "ipc:content-shutdown");
        this._hasObserver = true;
      }
    } else if (this._hasObserver) {
      Services.obs.removeObserver(this, "ipc:content-created");
      Services.obs.removeObserver(this, "ipc:content-shutdown");
      this._hasObserver = false;
    }
  }

  observe() {
    if (this._mustNotify) {
      this._onListChanged();
      this._mustNotify = false;
    }
  }
}

exports.ProcessActorList = ProcessActorList;