summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/xpcshell/testactors.js
blob: bbcd8abe6e42d4b43d2981e1c122693bf24d51ae (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const {
  LazyPool,
  createExtraActors,
} = require("resource://devtools/shared/protocol/lazy-pool.js");
const { RootActor } = require("resource://devtools/server/actors/root.js");
const { ThreadActor } = require("resource://devtools/server/actors/thread.js");
const {
  DevToolsServer,
} = require("resource://devtools/server/devtools-server.js");
const {
  ActorRegistry,
} = require("resource://devtools/server/actors/utils/actor-registry.js");
const {
  SourcesManager,
} = require("resource://devtools/server/actors/utils/sources-manager.js");
const makeDebugger = require("resource://devtools/server/actors/utils/make-debugger.js");
const protocol = require("resource://devtools/shared/protocol.js");
const {
  windowGlobalTargetSpec,
} = require("resource://devtools/shared/specs/targets/window-global.js");
const {
  tabDescriptorSpec,
} = require("resource://devtools/shared/specs/descriptors/tab.js");
const Targets = require("resource://devtools/server/actors/targets/index.js");
const {
  createContentProcessSessionContext,
} = require("resource://devtools/server/actors/watcher/session-context.js");

var gTestGlobals = new Set();
DevToolsServer.addTestGlobal = function (global) {
  gTestGlobals.add(global);
};
DevToolsServer.removeTestGlobal = function (global) {
  gTestGlobals.delete(global);
};

DevToolsServer.getTestGlobal = function (name) {
  for (const g of gTestGlobals) {
    if (g.__name == name) {
      return g;
    }
  }

  return null;
};

var gAllowNewThreadGlobals = false;
DevToolsServer.allowNewThreadGlobals = function () {
  gAllowNewThreadGlobals = true;
};
DevToolsServer.disallowNewThreadGlobals = function () {
  gAllowNewThreadGlobals = false;
};

// A mock tab list, for use by tests. This simply presents each global in
// gTestGlobals as a tab, and the list is fixed: it never calls its
// onListChanged handler.
//
// As implemented now, we consult gTestGlobals when we're constructed, not
// when we're iterated over, so tests have to add their globals before the
// root actor is created.
function TestTabList(connection) {
  this.conn = connection;

  // An array of actors for each global added with
  // DevToolsServer.addTestGlobal.
  this._descriptorActors = [];

  // A pool mapping those actors' names to the actors.
  this._descriptorActorPool = new LazyPool(connection);

  for (const global of gTestGlobals) {
    const actor = new TestTargetActor(connection, global);
    this._descriptorActorPool.manage(actor);

    const descriptorActor = new TestDescriptorActor(connection, actor);
    this._descriptorActorPool.manage(descriptorActor);

    this._descriptorActors.push(descriptorActor);
  }
}

TestTabList.prototype = {
  constructor: TestTabList,
  destroy() {},
  getList() {
    return Promise.resolve([...this._descriptorActors]);
  },
  // Helper method only available for the xpcshell implementation of tablist.
  getTargetActorForTab(title) {
    const descriptorActor = this._descriptorActors.find(d => d.title === title);
    if (!descriptorActor) {
      return null;
    }
    return descriptorActor._targetActor;
  },
};

exports.createRootActor = function createRootActor(connection) {
  ActorRegistry.registerModule("devtools/server/actors/webconsole", {
    prefix: "console",
    constructor: "WebConsoleActor",
    type: { target: true },
  });
  const root = new RootActor(connection, {
    tabList: new TestTabList(connection),
    globalActorFactories: ActorRegistry.globalActorFactories,
  });

  root.applicationType = "xpcshell-tests";
  return root;
};

class TestDescriptorActor extends protocol.Actor {
  constructor(conn, targetActor) {
    super(conn, tabDescriptorSpec);
    this._targetActor = targetActor;
  }

  // We don't exercise the selected tab in xpcshell tests.
  get selected() {
    return false;
  }

  get title() {
    return this._targetActor.title;
  }

  form() {
    const form = {
      actor: this.actorID,
      traits: {},
      selected: this.selected,
      title: this._targetActor.title,
      url: this._targetActor.url,
    };

    return form;
  }

  getFavicon() {
    return "";
  }

  getTarget() {
    return this._targetActor.form();
  }
}

class TestTargetActor extends protocol.Actor {
  constructor(conn, global) {
    super(conn, windowGlobalTargetSpec);

    this.sessionContext = createContentProcessSessionContext();
    this._global = global;
    this._global.wrappedJSObject = global;
    this.threadActor = new ThreadActor(this, this._global);
    this.conn.addActor(this.threadActor);
    this._extraActors = {};
    // This is a hack in order to enable threadActor to be accessed from getFront
    this._extraActors.threadActor = this.threadActor;
    this.makeDebugger = makeDebugger.bind(null, {
      findDebuggees: () => [this._global],
      shouldAddNewGlobalAsDebuggee: () => gAllowNewThreadGlobals,
    });
    this.dbg = this.makeDebugger();
    this.notifyResources = this.notifyResources.bind(this);
  }

  targetType = Targets.TYPES.FRAME;

  get window() {
    return this._global;
  }

  // Both title and url point to this._global.__name
  get title() {
    return this._global.__name;
  }

  get url() {
    return this._global.__name;
  }

  get sourcesManager() {
    if (!this._sourcesManager) {
      this._sourcesManager = new SourcesManager(this.threadActor);
    }
    return this._sourcesManager;
  }

  form() {
    const response = {
      actor: this.actorID,
      title: this.title,
      threadActor: this.threadActor.actorID,
    };

    // Walk over target-scoped actors and add them to a new LazyPool.
    const actorPool = new LazyPool(this.conn);
    const actors = createExtraActors(
      ActorRegistry.targetScopedActorFactories,
      actorPool,
      this
    );
    if (actorPool?._poolMap.size > 0) {
      this._descriptorActorPool = actorPool;
      this.conn.addActorPool(this._descriptorActorPool);
    }

    return { ...response, ...actors };
  }

  detach() {
    this.threadActor.destroy();
    return { type: "detached" };
  }

  reload() {
    this.sourcesManager.reset();
    this.threadActor.clearDebuggees();
    this.threadActor.dbg.addDebuggees();
    return {};
  }

  removeActorByName(name) {
    const actor = this._extraActors[name];
    if (this._descriptorActorPool) {
      this._descriptorActorPool.removeActor(actor);
    }
    delete this._extraActors[name];
  }

  notifyResources(updateType, resources) {
    this.emit(`resource-${updateType}-form`, resources);
  }
}