Bug 1813476 - [devtools] Convert RootActor to ES Class. r=devtools-reviewers,nchevobbe

We might followup and remove "echo" which is only used by test?

Differential Revision: https://phabricator.services.mozilla.com/D168256
This commit is contained in:
Alexandre Poirot 2023-01-31 16:50:08 +00:00
Родитель 6081889a65
Коммит 109b9327f7
1 изменённых файлов: 49 добавлений и 48 удалений

Просмотреть файл

@ -8,7 +8,9 @@
// error packets.
/* eslint-disable no-throw-literal */
const { Pool } = require("resource://devtools/shared/protocol.js");
const { Actor, Pool } = require("resource://devtools/shared/protocol.js");
const { rootSpec } = require("resource://devtools/shared/specs/root.js");
const {
LazyPool,
createExtraActors,
@ -16,8 +18,6 @@ const {
const {
DevToolsServer,
} = require("resource://devtools/server/devtools-server.js");
const protocol = require("resource://devtools/shared/protocol.js");
const { rootSpec } = require("resource://devtools/shared/specs/root.js");
const Resources = require("resource://devtools/server/actors/resources/index.js");
loader.lazyRequireGetter(
@ -101,9 +101,26 @@ loader.lazyRequireGetter(
* actually produce any actors until they are reached in the course of
* iteration: alliterative lazy live lists.
*/
exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
initialize(conn, parameters) {
protocol.Actor.prototype.initialize.call(this, conn);
class RootActor extends Actor {
constructor(conn, parameters) {
super(conn, rootSpec);
/**
* This `echo` request can't be easily specified via protocol.js types
* as it is a JSON value in the packet itself. Protocol.js only allows
* arbitrary json object in one property of the packet.
* In order to bypass protocol.js, declare the request method directly
* on the prototype/requestTypes, which is populated by Actor's constructor
*
* Note that this request is only used by tests.
*/
this.requestTypes.echo = function(request) {
/*
* Request packets are frozen. Copy request, so that
* DevToolsServerConnection.onPacket can attach a 'from' property.
*/
return Cu.cloneInto(request, {});
};
this._parameters = parameters;
this._onTabListChanged = this.onTabListChanged.bind(this);
@ -137,7 +154,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
)
: true,
};
},
}
/**
* Return a 'hello' packet as specified by the Remote Debugging Protocol.
@ -150,7 +167,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
testConnectionPrefix: this.conn.prefix,
traits: this.traits,
};
},
}
forwardingCancelled(prefix) {
return {
@ -158,7 +175,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
type: "forwardingCancelled",
prefix,
};
},
}
/**
* Destroys the actor from the browser window.
@ -166,7 +183,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
destroy() {
Resources.unwatchAllResources(this);
protocol.Actor.prototype.destroy.call(this);
super.destroy();
/* Tell the live lists we aren't watching any more. */
if (this._parameters.tabList) {
@ -211,11 +228,10 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
this._serviceWorkerRegistrationActorPool.destroy();
}
this._extraActors = null;
this.conn = null;
this._tabDescriptorActorPool = null;
this._globalActorPool = null;
this._parameters = null;
},
}
/**
* Gets the "root" form, which lists all the global actors that affect the entire
@ -233,7 +249,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
);
return actors;
},
}
/* The 'listTabs' request and the 'tabListChanged' notification. */
@ -273,7 +289,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
this._tabDescriptorActorPool = newActorPool;
return tabDescriptorActors;
},
}
/**
* Return the tab descriptor actor for the tab identified by one of the IDs
@ -316,13 +332,13 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
this._tabDescriptorActorPool.manage(descriptorActor);
return descriptorActor;
},
}
onTabListChanged() {
this.conn.send({ from: this.actorID, type: "tabListChanged" });
/* It's a one-shot notification; no need to watch any more. */
this._parameters.tabList.onListChanged = null;
},
}
/**
* This function can receive the following option from devtools client.
@ -362,12 +378,12 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
this._addonTargetActorPool = addonTargetActorPool;
return addonTargetActors;
},
}
onAddonListChanged() {
this.conn.send({ from: this.actorID, type: "addonListChanged" });
this._parameters.addonList.onListChanged = null;
},
}
listWorkers() {
const workerList = this._parameters.workerList;
@ -399,12 +415,12 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
workers: actors,
};
});
},
}
onWorkerListChanged() {
this.conn.send({ from: this.actorID, type: "workerListChanged" });
this._parameters.workerList.onListChanged = null;
},
}
listServiceWorkerRegistrations() {
const registrationList = this._parameters.serviceWorkerRegistrationList;
@ -433,7 +449,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
registrations: actors,
};
});
},
}
onServiceWorkerRegistrationListChanged() {
this.conn.send({
@ -441,7 +457,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
type: "serviceWorkerRegistrationListChanged",
});
this._parameters.serviceWorkerRegistrationList.onListChanged = null;
},
}
listProcesses() {
const { processList } = this._parameters;
@ -471,12 +487,12 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
}
this._processDescriptorActorPool = pool;
return [...this._processDescriptorActorPool.poolChildren()];
},
}
onProcessListChanged() {
this.conn.send({ from: this.actorID, type: "processListChanged" });
this._parameters.processList.onListChanged = null;
},
}
async getProcess(id) {
if (!DevToolsServer.allowChromeProcess) {
@ -506,7 +522,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
this._processDescriptorActorPool.manage(processDescriptor);
}
return processDescriptor;
},
}
_getKnownDescriptor(id, pool) {
// if there is no pool, then we do not have any descriptors
@ -519,7 +535,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
}
}
return null;
},
}
/**
* Remove the extra actor (added by ActorRegistry.addGlobalActor or
@ -540,7 +556,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
}
delete this._extraActors[name];
}
},
}
/**
* Start watching for a list of resource types.
@ -549,7 +565,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
*/
async watchResources(resourceTypes) {
await Resources.watchResources(this, resourceTypes);
},
}
/**
* Stop watching for a list of resource types.
@ -558,7 +574,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
*/
unwatchResources(resourceTypes) {
Resources.unwatchResources(this, resourceTypes);
},
}
/**
* Clear resources of a list of resource types.
@ -567,7 +583,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
*/
clearResources(resourceTypes) {
Resources.clearResources(this, resourceTypes);
},
}
/**
* Called by Resource Watchers, when new resources are available, updated or destroyed.
@ -599,22 +615,7 @@ exports.RootActor = protocol.ActorClassWithSpec(rootSpec, {
default:
throw new Error("Unsupported update type: " + updateType);
}
},
});
}
}
/**
* This `echo` request can't be easily specified via protocol.js types
* as it is a JSON value in the packet itself. Protocol.js only allows
* arbitrary json object in one property of the packet.
* In order to bypass protocol.js, declare the request method directly
* on the prototype/requestTypes, which is populated by ActorClassWithSpec.
*
* Note that this request is only used by tests.
*/
exports.RootActor.prototype.requestTypes.echo = function(request) {
/*
* Request packets are frozen. Copy request, so that
* DevToolsServerConnection.onPacket can attach a 'from' property.
*/
return Cu.cloneInto(request, {});
};
exports.RootActor = RootActor;