summaryrefslogtreecommitdiffstats
path: root/devtools/server/connectors/js-process-actor/target-watchers/process.sys.mjs
blob: c2b6dd807c96ec663bc4dbb593cfc4a72e29b6b2 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* 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/. */

import { ContentProcessWatcherRegistry } from "resource://devtools/server/connectors/js-process-actor/ContentProcessWatcherRegistry.sys.mjs";

function watch() {
  // There is nothing to watch. This JS Process Actor will automatically be spawned
  // for each new DOM Process.
}
function unwatch() {}

function createTargetsForWatcher(watcherDataObject) {
  // Always ignore the parent process. A special WindowGlobal target actor will be spawned.
  if (ChromeUtils.domProcessChild.childID == 0) {
    return;
  }

  createContentProcessTargetActor(watcherDataObject);
}

/**
 * Instantiate a content process target actor for the current process
 * and for a given watcher actor.
 *
 * @param {Object} watcherDataObject
 */
function createContentProcessTargetActor(watcherDataObject) {
  logDOMProcess(
    ChromeUtils.domProcessChild,
    "Instantiate ContentProcessTarget"
  );

  const { connection, loader } =
    ContentProcessWatcherRegistry.getOrCreateConnectionForWatcher(
      watcherDataObject.watcherActorID
    );

  const { ContentProcessTargetActor } = loader.require(
    "devtools/server/actors/targets/content-process"
  );

  // Create the actual target actor.
  const targetActor = new ContentProcessTargetActor(connection, {
    sessionContext: watcherDataObject.sessionContext,
  });

  ContentProcessWatcherRegistry.onNewTargetActor(
    watcherDataObject,
    targetActor
  );
}

function destroyTargetsForWatcher(watcherDataObject, options) {
  // Unregister and destroy the existing target actors for this target type
  const actorsToDestroy = watcherDataObject.actors.filter(
    actor => actor.targetType == "process"
  );
  watcherDataObject.actors = watcherDataObject.actors.filter(
    actor => actor.targetType != "process"
  );

  for (const actor of actorsToDestroy) {
    ContentProcessWatcherRegistry.destroyTargetActor(
      watcherDataObject,
      actor,
      options
    );
  }
}

// If true, log info about DOMProcess's being created.
const DEBUG = false;

/**
 * Print information about operation being done against each content process.
 *
 * @param {nsIDOMProcessChild} domProcessChild
 *        The process for which we should log a message.
 * @param {String} message
 *        Message to log.
 */
function logDOMProcess(domProcessChild, message) {
  if (!DEBUG) {
    return;
  }
  dump(" [pid:" + domProcessChild + "] " + message + "\n");
}

export const ProcessTargetWatcher = {
  watch,
  unwatch,
  createTargetsForWatcher,
  destroyTargetsForWatcher,
};