2013-06-06 23:29:27 +04:00
|
|
|
/* 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";
|
|
|
|
|
2017-08-21 21:21:48 +03:00
|
|
|
const { extend } = require("devtools/shared/extend");
|
2017-08-16 16:17:24 +03:00
|
|
|
var EventEmitter = require("devtools/shared/event-emitter");
|
2016-08-05 22:17:17 +03:00
|
|
|
var {getStack, callFunctionWithAsyncStack} = require("devtools/shared/platform/stack");
|
2016-09-30 00:51:15 +03:00
|
|
|
var {settleAll} = require("devtools/shared/DevToolsUtils");
|
2017-09-13 01:55:00 +03:00
|
|
|
var {lazyLoadSpec, lazyLoadFront} = require("devtools/shared/specs/index");
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2018-04-14 00:34:12 +03:00
|
|
|
// Bug 1454373: devtools/shared/defer still uses Promise.jsm which is slower
|
|
|
|
// than DOM Promises. So implement our own copy of `defer` based on DOM Promises.
|
|
|
|
function defer() {
|
|
|
|
let resolve, reject;
|
2018-06-01 13:36:09 +03:00
|
|
|
const promise = new Promise(function() {
|
2018-04-14 00:34:12 +03:00
|
|
|
resolve = arguments[0];
|
|
|
|
reject = arguments[1];
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
resolve: resolve,
|
|
|
|
reject: reject,
|
2018-10-19 15:55:39 +03:00
|
|
|
promise: promise,
|
2018-04-14 00:34:12 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* Types: named marshallers/demarshallers.
|
|
|
|
*
|
|
|
|
* Types provide a 'write' function that takes a js representation and
|
|
|
|
* returns a protocol representation, and a "read" function that
|
|
|
|
* takes a protocol representation and returns a js representation.
|
|
|
|
*
|
|
|
|
* The read and write methods are also passed a context object that
|
|
|
|
* represent the actor or front requesting the translation.
|
|
|
|
*
|
|
|
|
* Types are referred to with a typestring. Basic types are
|
|
|
|
* registered by name using addType, and more complex types can
|
|
|
|
* be generated by adding detail to the type name.
|
|
|
|
*/
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var types = Object.create(null);
|
2013-06-06 23:29:27 +04:00
|
|
|
exports.types = types;
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var registeredTypes = types.registeredTypes = new Map();
|
|
|
|
var registeredLifetimes = types.registeredLifetimes = new Map();
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the type object associated with a given typestring.
|
|
|
|
* If passed a type object, it will be returned unchanged.
|
|
|
|
*
|
|
|
|
* Types can be registered with addType, or can be created on
|
|
|
|
* the fly with typestrings. Examples:
|
|
|
|
*
|
|
|
|
* boolean
|
|
|
|
* threadActor
|
|
|
|
* threadActor#detail
|
|
|
|
* array:threadActor
|
|
|
|
* array:array:threadActor#detail
|
|
|
|
*
|
|
|
|
* @param [typestring|type] type
|
|
|
|
* Either a typestring naming a type or a type object.
|
|
|
|
*
|
|
|
|
* @returns a type object.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
types.getType = function(type) {
|
2013-06-06 23:29:27 +04:00
|
|
|
if (!type) {
|
|
|
|
return types.Primitive;
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:25:54 +03:00
|
|
|
if (typeof (type) !== "string") {
|
2013-06-06 23:29:27 +04:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If already registered, we're done here.
|
|
|
|
let reg = registeredTypes.get(type);
|
2016-12-02 20:32:04 +03:00
|
|
|
if (reg) {
|
|
|
|
return reg;
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2017-09-13 01:55:00 +03:00
|
|
|
// Try to lazy load the spec, if not already loaded.
|
|
|
|
if (lazyLoadSpec(type)) {
|
|
|
|
// If a spec module was lazy loaded, it will synchronously call
|
|
|
|
// generateActorSpec, and set the type in `registeredTypes`.
|
|
|
|
reg = registeredTypes.get(type);
|
|
|
|
if (reg) {
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
// New type, see if it's a collection/lifetime type:
|
2018-06-01 13:36:09 +03:00
|
|
|
const sep = type.indexOf(":");
|
2013-06-06 23:29:27 +04:00
|
|
|
if (sep >= 0) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const collection = type.substring(0, sep);
|
|
|
|
const subtype = types.getType(type.substring(sep + 1));
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
if (collection === "array") {
|
|
|
|
return types.addArrayType(subtype);
|
2013-07-21 21:15:59 +04:00
|
|
|
} else if (collection === "nullable") {
|
|
|
|
return types.addNullableType(subtype);
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (registeredLifetimes.has(collection)) {
|
|
|
|
return types.addLifetimeType(collection, subtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Error("Unknown collection type: " + collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a collection, might be actor detail
|
2018-06-01 13:36:09 +03:00
|
|
|
const pieces = type.split("#", 2);
|
2013-06-06 23:29:27 +04:00
|
|
|
if (pieces.length > 1) {
|
2019-01-28 21:42:46 +03:00
|
|
|
if (pieces[1] != "actorid") {
|
|
|
|
throw new Error("Unsupported detail, only support 'actorid', got: " + pieces[1]);
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
return types.addActorDetail(type, pieces[0], pieces[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Error("Unknown type: " + type);
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2013-07-21 21:38:40 +04:00
|
|
|
/**
|
|
|
|
* Don't allow undefined when writing primitive types to packets. If
|
|
|
|
* you want to allow undefined, use a nullable type.
|
|
|
|
*/
|
|
|
|
function identityWrite(v) {
|
|
|
|
if (v === undefined) {
|
|
|
|
throw Error("undefined passed where a value is required");
|
|
|
|
}
|
2015-06-15 12:51:00 +03:00
|
|
|
// This has to handle iterator->array conversion because arrays of
|
|
|
|
// primitive types pass through here.
|
2018-04-19 01:31:30 +03:00
|
|
|
if (v && typeof v.next === "function") {
|
2015-06-15 12:51:00 +03:00
|
|
|
return [...v];
|
|
|
|
}
|
2013-07-21 21:38:40 +04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* Add a type to the type system.
|
|
|
|
*
|
|
|
|
* When registering a type, you can provide `read` and `write` methods.
|
|
|
|
*
|
|
|
|
* The `read` method will be passed a JS object value from the JSON
|
|
|
|
* packet and must return a native representation. The `write` method will
|
|
|
|
* be passed a native representation and should provide a JSONable value.
|
|
|
|
*
|
|
|
|
* These methods will both be passed a context. The context is the object
|
|
|
|
* performing or servicing the request - on the server side it will be
|
|
|
|
* an Actor, on the client side it will be a Front.
|
|
|
|
*
|
|
|
|
* @param typestring name
|
|
|
|
* Name to register
|
|
|
|
* @param object typeObject
|
|
|
|
* An object whose properties will be stored in the type, including
|
|
|
|
* the `read` and `write` methods.
|
|
|
|
* @param object options
|
|
|
|
* Can specify `thawed` to prevent the type from being frozen.
|
|
|
|
*
|
|
|
|
* @returns a type object that can be used in protocol definitions.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
types.addType = function(name, typeObject = {}, options = {}) {
|
2013-06-06 23:29:27 +04:00
|
|
|
if (registeredTypes.has(name)) {
|
|
|
|
throw Error("Type '" + name + "' already exists.");
|
|
|
|
}
|
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const type = Object.assign({
|
2016-12-02 20:32:04 +03:00
|
|
|
toString() {
|
|
|
|
return "[protocol type:" + name + "]";
|
|
|
|
},
|
2017-01-12 01:21:56 +03:00
|
|
|
name: name,
|
2013-06-06 23:29:27 +04:00
|
|
|
primitive: !(typeObject.read || typeObject.write),
|
2013-07-21 21:38:40 +04:00
|
|
|
read: identityWrite,
|
2018-10-19 15:55:39 +03:00
|
|
|
write: identityWrite,
|
2013-06-06 23:29:27 +04:00
|
|
|
}, typeObject);
|
|
|
|
|
|
|
|
registeredTypes.set(name, type);
|
|
|
|
|
|
|
|
return type;
|
|
|
|
};
|
|
|
|
|
2014-10-12 10:18:00 +04:00
|
|
|
/**
|
|
|
|
* Remove a type previously registered with the system.
|
|
|
|
* Primarily useful for types registered by addons.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
types.removeType = function(name) {
|
2014-10-12 10:18:00 +04:00
|
|
|
// This type may still be referenced by other types, make sure
|
|
|
|
// those references don't work.
|
2018-06-01 13:36:09 +03:00
|
|
|
const type = registeredTypes.get(name);
|
2014-10-12 10:18:00 +04:00
|
|
|
|
|
|
|
type.name = "DEFUNCT:" + name;
|
|
|
|
type.category = "defunct";
|
|
|
|
type.primitive = false;
|
2018-03-12 21:24:38 +03:00
|
|
|
type.read = type.write = function() {
|
2016-12-02 20:32:04 +03:00
|
|
|
throw new Error("Using defunct type: " + name);
|
|
|
|
};
|
2014-10-12 10:18:00 +04:00
|
|
|
|
|
|
|
registeredTypes.delete(name);
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2014-10-12 10:18:00 +04:00
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* Add an array type to the type system.
|
|
|
|
*
|
|
|
|
* getType() will call this function if provided an "array:<type>"
|
|
|
|
* typestring.
|
|
|
|
*
|
|
|
|
* @param type subtype
|
|
|
|
* The subtype to be held by the array.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
types.addArrayType = function(subtype) {
|
2013-06-06 23:29:27 +04:00
|
|
|
subtype = types.getType(subtype);
|
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const name = "array:" + subtype.name;
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
// Arrays of primitive types are primitive types themselves.
|
|
|
|
if (subtype.primitive) {
|
|
|
|
return types.addType(name);
|
|
|
|
}
|
|
|
|
return types.addType(name, {
|
2014-04-18 03:25:07 +04:00
|
|
|
category: "array",
|
2018-04-05 19:23:09 +03:00
|
|
|
read: (v, ctx) => {
|
2018-04-19 01:31:30 +03:00
|
|
|
if (v && typeof v.next === "function") {
|
2018-04-05 19:23:09 +03:00
|
|
|
v = [...v];
|
|
|
|
}
|
|
|
|
return v.map(i => subtype.read(i, ctx));
|
|
|
|
},
|
|
|
|
write: (v, ctx) => {
|
2018-04-19 01:31:30 +03:00
|
|
|
if (v && typeof v.next === "function") {
|
2018-04-05 19:23:09 +03:00
|
|
|
v = [...v];
|
|
|
|
}
|
|
|
|
return v.map(i => subtype.write(i, ctx));
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2013-06-06 23:29:27 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a dict type to the type system. This allows you to serialize
|
|
|
|
* a JS object that contains non-primitive subtypes.
|
|
|
|
*
|
|
|
|
* Properties of the value that aren't included in the specializations
|
|
|
|
* will be serialized as primitive values.
|
|
|
|
*
|
|
|
|
* @param object specializations
|
|
|
|
* A dict of property names => type
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
types.addDictType = function(name, specializations) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const specTypes = {};
|
|
|
|
for (const prop in specializations) {
|
2018-05-03 22:47:27 +03:00
|
|
|
try {
|
|
|
|
specTypes[prop] = types.getType(specializations[prop]);
|
|
|
|
} catch (e) {
|
|
|
|
// Types may not be defined yet. Sometimes, we define the type *after* using it, but
|
|
|
|
// also, we have cyclic definitions on types. So lazily load them when they are not
|
|
|
|
// immediately available.
|
|
|
|
loader.lazyGetter(specTypes, prop, () => {
|
|
|
|
return types.getType(specializations[prop]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
return types.addType(name, {
|
2014-04-18 03:25:07 +04:00
|
|
|
category: "dict",
|
2018-05-03 22:47:27 +03:00
|
|
|
specializations,
|
2013-06-06 23:29:27 +04:00
|
|
|
read: (v, ctx) => {
|
2018-06-01 13:36:09 +03:00
|
|
|
const ret = {};
|
|
|
|
for (const prop in v) {
|
2018-05-03 22:47:27 +03:00
|
|
|
if (prop in specTypes) {
|
|
|
|
ret[prop] = specTypes[prop].read(v[prop], ctx);
|
2013-06-06 23:29:27 +04:00
|
|
|
} else {
|
|
|
|
ret[prop] = v[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
|
|
|
|
write: (v, ctx) => {
|
2018-06-01 13:36:09 +03:00
|
|
|
const ret = {};
|
|
|
|
for (const prop in v) {
|
2018-05-03 22:47:27 +03:00
|
|
|
if (prop in specTypes) {
|
|
|
|
ret[prop] = specTypes[prop].write(v[prop], ctx);
|
2013-06-06 23:29:27 +04:00
|
|
|
} else {
|
|
|
|
ret[prop] = v[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2016-05-17 21:25:54 +03:00
|
|
|
});
|
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register an actor type with the type system.
|
|
|
|
*
|
|
|
|
* Types are marshalled differently when communicating server->client
|
|
|
|
* than they are when communicating client->server. The server needs
|
|
|
|
* to provide useful information to the client, so uses the actor's
|
|
|
|
* `form` method to get a json representation of the actor. When
|
|
|
|
* making a request from the client we only need the actor ID string.
|
|
|
|
*
|
|
|
|
* This function can be called before the associated actor has been
|
|
|
|
* constructed, but the read and write methods won't work until
|
|
|
|
* the associated addActorImpl or addActorFront methods have been
|
|
|
|
* called during actor/front construction.
|
|
|
|
*
|
|
|
|
* @param string name
|
|
|
|
* The typestring to register.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
types.addActorType = function(name) {
|
2017-09-13 01:55:00 +03:00
|
|
|
// We call addActorType from:
|
|
|
|
// FrontClassWithSpec when registering front synchronously,
|
|
|
|
// generateActorSpec when defining specs,
|
|
|
|
// specs modules to register actor type early to use them in other types
|
|
|
|
if (registeredTypes.has(name)) {
|
|
|
|
return registeredTypes.get(name);
|
|
|
|
}
|
2018-06-01 13:36:09 +03:00
|
|
|
const type = types.addType(name, {
|
2013-06-06 23:29:27 +04:00
|
|
|
_actor: true,
|
2014-04-18 03:25:07 +04:00
|
|
|
category: "actor",
|
2013-06-06 23:29:27 +04:00
|
|
|
read: (v, ctx, detail) => {
|
|
|
|
// If we're reading a request on the server side, just
|
|
|
|
// find the actor registered with this actorID.
|
|
|
|
if (ctx instanceof Actor) {
|
|
|
|
return ctx.conn.getActor(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reading a response on the client side, check for an
|
|
|
|
// existing front on the connection, and create the front
|
|
|
|
// if it isn't found.
|
2018-06-01 13:36:09 +03:00
|
|
|
const actorID = typeof (v) === "string" ? v : v.actor;
|
2013-07-21 22:02:30 +04:00
|
|
|
let front = ctx.conn.getActor(actorID);
|
2014-10-12 00:08:07 +04:00
|
|
|
if (!front) {
|
2018-05-26 02:20:28 +03:00
|
|
|
// If front isn't instantiated yet, create one.
|
2017-09-13 01:55:00 +03:00
|
|
|
|
|
|
|
// Try lazy loading front if not already loaded.
|
|
|
|
// The front module will synchronously call `FrontClassWithSpec` and
|
|
|
|
// augment `type` with the `frontClass` attribute.
|
|
|
|
if (!type.frontClass) {
|
|
|
|
lazyLoadFront(name);
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:20:35 +03:00
|
|
|
// Use intermediate Class variable to please eslint requiring
|
|
|
|
// a capital letter for all constructors.
|
|
|
|
const Class = type.frontClass;
|
|
|
|
front = new Class(ctx.conn);
|
2013-07-21 22:02:30 +04:00
|
|
|
front.actorID = actorID;
|
2013-06-06 23:29:27 +04:00
|
|
|
ctx.marshallPool().manage(front);
|
|
|
|
}
|
2014-10-12 00:08:07 +04:00
|
|
|
|
2019-01-28 21:42:46 +03:00
|
|
|
// When the type `${name}#actorid` is used, `v` is a string refering to the
|
|
|
|
// actor ID. We only set the actorID just before and so do not need anything else.
|
|
|
|
if (detail != "actorid") {
|
|
|
|
v = identityWrite(v);
|
|
|
|
front.form(v, ctx);
|
|
|
|
}
|
2014-10-12 00:08:07 +04:00
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
return front;
|
|
|
|
},
|
|
|
|
write: (v, ctx, detail) => {
|
|
|
|
// If returning a response from the server side, make sure
|
|
|
|
// the actor is added to a parent object and return its form.
|
|
|
|
if (v instanceof Actor) {
|
|
|
|
if (!v.actorID) {
|
|
|
|
ctx.marshallPool().manage(v);
|
|
|
|
}
|
2019-01-28 21:42:46 +03:00
|
|
|
if (detail == "actorid") {
|
|
|
|
return v.actorID;
|
|
|
|
}
|
2019-01-28 21:42:33 +03:00
|
|
|
return identityWrite(v.form(detail));
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Writing a request from the client side, just send the actor id.
|
|
|
|
return v.actorID;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return type;
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
types.addNullableType = function(subtype) {
|
2013-07-21 21:15:59 +04:00
|
|
|
subtype = types.getType(subtype);
|
|
|
|
return types.addType("nullable:" + subtype.name, {
|
2014-04-18 03:25:07 +04:00
|
|
|
category: "nullable",
|
2013-07-21 21:15:59 +04:00
|
|
|
read: (value, ctx) => {
|
|
|
|
if (value == null) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return subtype.read(value, ctx);
|
|
|
|
},
|
|
|
|
write: (value, ctx) => {
|
|
|
|
if (value == null) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return subtype.write(value, ctx);
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2013-07-21 21:15:59 +04:00
|
|
|
});
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2013-07-21 21:15:59 +04:00
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* Register an actor detail type. This is just like an actor type, but
|
|
|
|
* will pass a detail hint to the actor's form method during serialization/
|
|
|
|
* deserialization.
|
|
|
|
*
|
|
|
|
* This is called by getType() when passed an 'actorType#detail' string.
|
|
|
|
*
|
|
|
|
* @param string name
|
|
|
|
* The typestring to register this type as.
|
|
|
|
* @param type actorType
|
|
|
|
* The actor type you'll be detailing.
|
|
|
|
* @param string detail
|
|
|
|
* The detail to pass.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
types.addActorDetail = function(name, actorType, detail) {
|
2013-06-06 23:29:27 +04:00
|
|
|
actorType = types.getType(actorType);
|
|
|
|
if (!actorType._actor) {
|
2016-12-02 20:32:04 +03:00
|
|
|
throw Error(`Details only apply to actor types, tried to add detail '${detail}' ` +
|
|
|
|
`to ${actorType.name}`);
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
return types.addType(name, {
|
|
|
|
_actor: true,
|
2014-04-18 03:25:07 +04:00
|
|
|
category: "detail",
|
2013-06-06 23:29:27 +04:00
|
|
|
read: (v, ctx) => actorType.read(v, ctx, detail),
|
2018-10-19 15:55:39 +03:00
|
|
|
write: (v, ctx) => actorType.write(v, ctx, detail),
|
2013-06-06 23:29:27 +04:00
|
|
|
});
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register an actor lifetime. This lets the type system find a parent
|
|
|
|
* actor that differs from the actor fulfilling the request.
|
|
|
|
*
|
|
|
|
* @param string name
|
|
|
|
* The lifetime name to use in typestrings.
|
|
|
|
* @param string prop
|
|
|
|
* The property of the actor that holds the parent that should be used.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
types.addLifetime = function(name, prop) {
|
2013-06-06 23:29:27 +04:00
|
|
|
if (registeredLifetimes.has(name)) {
|
|
|
|
throw Error("Lifetime '" + name + "' already registered.");
|
|
|
|
}
|
|
|
|
registeredLifetimes.set(name, prop);
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2014-10-12 10:18:00 +04:00
|
|
|
/**
|
|
|
|
* Remove a previously-registered lifetime. Useful for lifetimes registered
|
|
|
|
* in addons.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
types.removeLifetime = function(name) {
|
2014-10-12 10:18:00 +04:00
|
|
|
registeredLifetimes.delete(name);
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2014-10-12 10:18:00 +04:00
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* Register a lifetime type. This creates an actor type tied to the given
|
|
|
|
* lifetime.
|
|
|
|
*
|
|
|
|
* This is called by getType() when passed a '<lifetimeType>:<actorType>'
|
|
|
|
* typestring.
|
|
|
|
*
|
|
|
|
* @param string lifetime
|
|
|
|
* A lifetime string previously regisered with addLifetime()
|
|
|
|
* @param type subtype
|
|
|
|
* An actor type
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
types.addLifetimeType = function(lifetime, subtype) {
|
2013-06-06 23:29:27 +04:00
|
|
|
subtype = types.getType(subtype);
|
|
|
|
if (!subtype._actor) {
|
2016-12-02 20:32:04 +03:00
|
|
|
throw Error(`Lifetimes only apply to actor types, tried to apply ` +
|
|
|
|
`lifetime '${lifetime}' to ${subtype.name}`);
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
2018-06-01 13:36:09 +03:00
|
|
|
const prop = registeredLifetimes.get(lifetime);
|
2013-06-06 23:29:27 +04:00
|
|
|
return types.addType(lifetime + ":" + subtype.name, {
|
2014-04-18 03:25:07 +04:00
|
|
|
category: "lifetime",
|
2013-06-06 23:29:27 +04:00
|
|
|
read: (value, ctx) => subtype.read(value, ctx[prop]),
|
2018-10-19 15:55:39 +03:00
|
|
|
write: (value, ctx) => subtype.write(value, ctx[prop]),
|
2016-05-17 21:25:54 +03:00
|
|
|
});
|
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
// Add a few named primitive types.
|
|
|
|
types.Primitive = types.addType("primitive");
|
|
|
|
types.String = types.addType("string");
|
|
|
|
types.Number = types.addType("number");
|
|
|
|
types.Boolean = types.addType("boolean");
|
|
|
|
types.JSON = types.addType("json");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request/Response templates and generation
|
|
|
|
*
|
|
|
|
* Request packets are specified as json templates with
|
|
|
|
* Arg and Option placeholders where arguments should be
|
|
|
|
* placed.
|
|
|
|
*
|
|
|
|
* Reponse packets are also specified as json templates,
|
|
|
|
* with a RetVal placeholder where the return value should be
|
|
|
|
* placed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Placeholder for simple arguments.
|
|
|
|
*
|
|
|
|
* @param number index
|
|
|
|
* The argument index to place at this position.
|
|
|
|
* @param type type
|
|
|
|
* The argument should be marshalled as this type.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
var Arg = function(index, type) {
|
2017-08-21 21:21:48 +03:00
|
|
|
this.index = index;
|
2017-09-13 01:55:00 +03:00
|
|
|
// Prevent force loading all Arg types by accessing it only when needed
|
2018-03-12 21:24:38 +03:00
|
|
|
loader.lazyGetter(this, "type", function() {
|
2017-09-13 01:55:00 +03:00
|
|
|
return types.getType(type);
|
|
|
|
});
|
2017-08-21 21:21:48 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2017-08-21 21:21:48 +03:00
|
|
|
Arg.prototype = {
|
2018-03-12 21:24:38 +03:00
|
|
|
write: function(arg, ctx) {
|
2013-06-06 23:29:27 +04:00
|
|
|
return this.type.write(arg, ctx);
|
|
|
|
},
|
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
read: function(v, ctx, outArgs) {
|
2013-06-06 23:29:27 +04:00
|
|
|
outArgs[this.index] = this.type.read(v, ctx);
|
2014-04-18 03:25:07 +04:00
|
|
|
},
|
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
describe: function() {
|
2014-04-18 03:25:07 +04:00
|
|
|
return {
|
|
|
|
_arg: this.index,
|
|
|
|
type: this.type.name,
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2017-08-21 21:21:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Outside of protocol.js, Arg is called as factory method, without the new keyword.
|
2018-03-12 21:24:38 +03:00
|
|
|
exports.Arg = function(index, type) {
|
2017-08-21 21:21:48 +03:00
|
|
|
return new Arg(index, type);
|
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Placeholder for an options argument value that should be hoisted
|
|
|
|
* into the packet.
|
|
|
|
*
|
|
|
|
* If provided in a method specification:
|
|
|
|
*
|
|
|
|
* { optionArg: Option(1)}
|
|
|
|
*
|
|
|
|
* Then arguments[1].optionArg will be placed in the packet in this
|
|
|
|
* value's place.
|
|
|
|
*
|
|
|
|
* @param number index
|
|
|
|
* The argument index of the options value.
|
|
|
|
* @param type type
|
|
|
|
* The argument should be marshalled as this type.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
var Option = function(index, type) {
|
2017-08-21 21:21:48 +03:00
|
|
|
Arg.call(this, index, type);
|
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2017-08-21 21:21:48 +03:00
|
|
|
Option.prototype = extend(Arg.prototype, {
|
2018-03-12 21:24:38 +03:00
|
|
|
write: function(arg, ctx, name) {
|
2014-08-12 07:59:00 +04:00
|
|
|
// Ignore if arg is undefined or null; allow other falsy values
|
|
|
|
if (arg == undefined || arg[name] == undefined) {
|
2013-06-06 23:29:27 +04:00
|
|
|
return undefined;
|
|
|
|
}
|
2018-06-01 13:36:09 +03:00
|
|
|
const v = arg[name];
|
2013-06-06 23:29:27 +04:00
|
|
|
return this.type.write(v, ctx);
|
|
|
|
},
|
2018-03-12 21:24:38 +03:00
|
|
|
read: function(v, ctx, outArgs, name) {
|
2013-06-06 23:29:27 +04:00
|
|
|
if (outArgs[this.index] === undefined) {
|
|
|
|
outArgs[this.index] = {};
|
|
|
|
}
|
|
|
|
if (v === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
outArgs[this.index][name] = this.type.read(v, ctx);
|
2014-04-18 03:25:07 +04:00
|
|
|
},
|
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
describe: function() {
|
2014-04-18 03:25:07 +04:00
|
|
|
return {
|
|
|
|
_option: this.index,
|
|
|
|
type: this.type.name,
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2013-06-06 23:29:27 +04:00
|
|
|
});
|
|
|
|
|
2017-08-21 21:21:48 +03:00
|
|
|
// Outside of protocol.js, Option is called as factory method, without the new keyword.
|
2018-03-12 21:24:38 +03:00
|
|
|
exports.Option = function(index, type) {
|
2017-08-21 21:21:48 +03:00
|
|
|
return new Option(index, type);
|
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Placeholder for return values in a response template.
|
|
|
|
*
|
|
|
|
* @param type type
|
|
|
|
* The return value should be marshalled as this type.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
var RetVal = function(type) {
|
2017-09-13 01:55:00 +03:00
|
|
|
// Prevent force loading all RetVal types by accessing it only when needed
|
2018-03-12 21:24:38 +03:00
|
|
|
loader.lazyGetter(this, "type", function() {
|
2017-09-13 01:55:00 +03:00
|
|
|
return types.getType(type);
|
|
|
|
});
|
2017-08-21 21:21:48 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2017-08-21 21:21:48 +03:00
|
|
|
RetVal.prototype = {
|
2018-03-12 21:24:38 +03:00
|
|
|
write: function(v, ctx) {
|
2013-07-21 21:38:40 +04:00
|
|
|
return this.type.write(v, ctx);
|
2013-06-06 23:29:27 +04:00
|
|
|
},
|
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
read: function(v, ctx) {
|
2013-07-21 21:38:40 +04:00
|
|
|
return this.type.read(v, ctx);
|
2014-04-18 03:25:07 +04:00
|
|
|
},
|
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
describe: function() {
|
2014-04-18 03:25:07 +04:00
|
|
|
return {
|
2018-10-19 15:55:39 +03:00
|
|
|
_retval: this.type.name,
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2017-08-21 21:21:48 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2017-08-21 21:21:48 +03:00
|
|
|
// Outside of protocol.js, RetVal is called as factory method, without the new keyword.
|
2018-03-12 21:24:38 +03:00
|
|
|
exports.RetVal = function(type) {
|
2017-08-21 21:21:48 +03:00
|
|
|
return new RetVal(type);
|
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/* Template handling functions */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the value at a given path, or undefined if not found.
|
|
|
|
*/
|
|
|
|
function getPath(obj, path) {
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const name of path) {
|
2013-06-06 23:29:27 +04:00
|
|
|
if (!(name in obj)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
obj = obj[name];
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find Placeholders in the template and save them along with their
|
|
|
|
* paths.
|
|
|
|
*/
|
2016-05-17 21:25:54 +03:00
|
|
|
function findPlaceholders(template, constructor, path = [], placeholders = []) {
|
|
|
|
if (!template || typeof (template) != "object") {
|
2013-06-06 23:29:27 +04:00
|
|
|
return placeholders;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (template instanceof constructor) {
|
2015-05-28 18:54:00 +03:00
|
|
|
placeholders.push({ placeholder: template, path: [...path] });
|
2013-06-06 23:29:27 +04:00
|
|
|
return placeholders;
|
|
|
|
}
|
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const name in template) {
|
2013-06-06 23:29:27 +04:00
|
|
|
path.push(name);
|
|
|
|
findPlaceholders(template[name], constructor, path, placeholders);
|
|
|
|
path.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
return placeholders;
|
|
|
|
}
|
|
|
|
|
2014-04-18 03:25:07 +04:00
|
|
|
function describeTemplate(template) {
|
|
|
|
return JSON.parse(JSON.stringify(template, (key, value) => {
|
|
|
|
if (value.describe) {
|
|
|
|
return value.describe();
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* Manages a request template.
|
|
|
|
*
|
|
|
|
* @param object template
|
|
|
|
* The request template.
|
|
|
|
* @construcor
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
var Request = function(template = {}) {
|
2017-08-21 21:21:48 +03:00
|
|
|
this.type = template.type;
|
|
|
|
this.template = template;
|
|
|
|
this.args = findPlaceholders(template, Arg);
|
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2017-08-21 21:21:48 +03:00
|
|
|
Request.prototype = {
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* Write a request.
|
|
|
|
*
|
|
|
|
* @param array fnArgs
|
|
|
|
* The function arguments to place in the request.
|
|
|
|
* @param object ctx
|
|
|
|
* The object making the request.
|
|
|
|
* @returns a request packet.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
write: function(fnArgs, ctx) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const ret = {};
|
|
|
|
for (const key in this.template) {
|
|
|
|
const value = this.template[key];
|
2013-06-06 23:29:27 +04:00
|
|
|
if (value instanceof Arg) {
|
2018-04-05 20:00:02 +03:00
|
|
|
ret[key] = value.write(value.index in fnArgs ? fnArgs[value.index] : undefined,
|
|
|
|
ctx, key);
|
|
|
|
} else if (key == "type") {
|
|
|
|
ret[key] = value;
|
|
|
|
} else {
|
|
|
|
throw new Error("Request can only an object with `Arg` or `Option` properties");
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
2018-04-05 20:00:02 +03:00
|
|
|
}
|
|
|
|
return ret;
|
2013-06-06 23:29:27 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a request.
|
|
|
|
*
|
|
|
|
* @param object packet
|
|
|
|
* The request packet.
|
|
|
|
* @param object ctx
|
|
|
|
* The object making the request.
|
|
|
|
* @returns an arguments array
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
read: function(packet, ctx) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const fnArgs = [];
|
|
|
|
for (const templateArg of this.args) {
|
|
|
|
const arg = templateArg.placeholder;
|
|
|
|
const path = templateArg.path;
|
|
|
|
const name = path[path.length - 1];
|
2013-06-06 23:29:27 +04:00
|
|
|
arg.read(getPath(packet, path), ctx, fnArgs, name);
|
|
|
|
}
|
|
|
|
return fnArgs;
|
|
|
|
},
|
2014-04-18 03:25:07 +04:00
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
describe: function() {
|
2016-12-02 20:32:04 +03:00
|
|
|
return describeTemplate(this.template);
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2017-08-21 21:21:48 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages a response template.
|
|
|
|
*
|
|
|
|
* @param object template
|
|
|
|
* The response template.
|
|
|
|
* @construcor
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
var Response = function(template = {}) {
|
2017-08-21 21:21:48 +03:00
|
|
|
this.template = template;
|
2018-06-01 13:36:09 +03:00
|
|
|
const placeholders = findPlaceholders(template, RetVal);
|
2017-08-21 21:21:48 +03:00
|
|
|
if (placeholders.length > 1) {
|
|
|
|
throw Error("More than one RetVal specified in response");
|
|
|
|
}
|
2018-06-01 13:36:09 +03:00
|
|
|
const placeholder = placeholders.shift();
|
2017-08-21 21:21:48 +03:00
|
|
|
if (placeholder) {
|
|
|
|
this.retVal = placeholder.placeholder;
|
|
|
|
this.path = placeholder.path;
|
|
|
|
}
|
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2017-08-21 21:21:48 +03:00
|
|
|
Response.prototype = {
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* Write a response for the given return value.
|
|
|
|
*
|
|
|
|
* @param val ret
|
|
|
|
* The return value.
|
|
|
|
* @param object ctx
|
|
|
|
* The object writing the response.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
write: function(ret, ctx) {
|
2018-04-09 22:45:17 +03:00
|
|
|
// Consider that `template` is either directly a `RetVal`,
|
|
|
|
// or a dictionary with may be one `RetVal`.
|
|
|
|
if (this.template instanceof RetVal) {
|
|
|
|
return this.template.write(ret, ctx);
|
|
|
|
}
|
2018-06-01 13:36:09 +03:00
|
|
|
const result = {};
|
|
|
|
for (const key in this.template) {
|
|
|
|
const value = this.template[key];
|
2013-06-06 23:29:27 +04:00
|
|
|
if (value instanceof RetVal) {
|
2018-04-09 22:45:17 +03:00
|
|
|
result[key] = value.write(ret, ctx);
|
|
|
|
} else {
|
2018-04-13 02:06:10 +03:00
|
|
|
throw new Error("Response can only be a `RetVal` instance or an object " +
|
|
|
|
"with one property being a `RetVal` instance.");
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
2018-04-09 22:45:17 +03:00
|
|
|
}
|
|
|
|
return result;
|
2013-06-06 23:29:27 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a return value from the given response.
|
|
|
|
*
|
|
|
|
* @param object packet
|
|
|
|
* The response packet.
|
|
|
|
* @param object ctx
|
|
|
|
* The object reading the response.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
read: function(packet, ctx) {
|
2013-06-06 23:29:27 +04:00
|
|
|
if (!this.retVal) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2018-06-01 13:36:09 +03:00
|
|
|
const v = getPath(packet, this.path);
|
2013-06-06 23:29:27 +04:00
|
|
|
return this.retVal.read(v, ctx);
|
2014-04-18 03:25:07 +04:00
|
|
|
},
|
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
describe: function() {
|
2016-12-02 20:32:04 +03:00
|
|
|
return describeTemplate(this.template);
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2017-08-21 21:21:48 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Actor and Front implementations
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A protocol object that can manage the lifetime of other protocol
|
2017-08-21 21:21:48 +03:00
|
|
|
* objects. Pools are used on both sides of the connection to help coordinate lifetimes.
|
|
|
|
*
|
|
|
|
* @param optional conn
|
|
|
|
* Either a DebuggerServerConnection or a DebuggerClient. Must have
|
|
|
|
* addActorPool, removeActorPool, and poolFor.
|
|
|
|
* conn can be null if the subclass provides a conn property.
|
|
|
|
* @constructor
|
2013-06-06 23:29:27 +04:00
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
class Pool extends EventEmitter {
|
|
|
|
constructor(conn) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
if (conn) {
|
|
|
|
this.conn = conn;
|
|
|
|
}
|
|
|
|
this.__poolMap = null;
|
2017-08-21 21:21:48 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the parent pool for this client.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
parent() {
|
2016-12-02 20:32:04 +03:00
|
|
|
return this.conn.poolFor(this.actorID);
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
poolFor(actorID) {
|
2018-09-26 11:03:33 +03:00
|
|
|
return this.conn.poolFor(actorID);
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2018-09-26 11:03:33 +03:00
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* Override this if you want actors returned by this actor
|
|
|
|
* to belong to a different actor by default.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
marshallPool() {
|
2016-12-02 20:32:04 +03:00
|
|
|
return this;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pool is the base class for all actors, even leaf nodes.
|
|
|
|
* If the child map is actually referenced, go ahead and create
|
|
|
|
* the stuff needed by the pool.
|
|
|
|
*/
|
|
|
|
get _poolMap() {
|
2016-12-02 20:32:04 +03:00
|
|
|
if (this.__poolMap) {
|
|
|
|
return this.__poolMap;
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
this.__poolMap = new Map();
|
|
|
|
this.conn.addActorPool(this);
|
|
|
|
return this.__poolMap;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an actor as a child of this pool.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
manage(actor) {
|
2013-06-06 23:29:27 +04:00
|
|
|
if (!actor.actorID) {
|
|
|
|
actor.actorID = this.conn.allocID(actor.actorPrefix || actor.typeName);
|
2018-09-26 11:08:10 +03:00
|
|
|
} else {
|
|
|
|
// If the actor is already registerd in a pool, remove it without destroying it.
|
|
|
|
// This happens for example when an addon is reloaded. To see this behavior, take a
|
|
|
|
// look at devtools/server/tests/unit/test_addon_reload.js
|
|
|
|
|
|
|
|
// TODO: not all actors have been moved to protocol.js, so they do not all have
|
|
|
|
// a parent field. Remove the check for the parent once the conversion is finished
|
|
|
|
const parent = this.poolFor(actor.actorID);
|
|
|
|
if (parent) {
|
|
|
|
parent.unmanage(actor);
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
this._poolMap.set(actor.actorID, actor);
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an actor as a child of this pool.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
unmanage(actor) {
|
2014-11-27 13:51:00 +03:00
|
|
|
this.__poolMap && this.__poolMap.delete(actor.actorID);
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
// true if the given actor ID exists in the pool.
|
2019-01-02 16:38:54 +03:00
|
|
|
has(actorID) {
|
2015-05-28 18:54:00 +03:00
|
|
|
return this.__poolMap && this._poolMap.has(actorID);
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
// The actor for a given actor id stored in this pool
|
2019-01-02 16:38:54 +03:00
|
|
|
actor(actorID) {
|
2018-09-26 11:03:33 +03:00
|
|
|
if (this.__poolMap) {
|
|
|
|
return this._poolMap.get(actorID);
|
|
|
|
}
|
|
|
|
return null;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
// Same as actor, should update debugger connection to use 'actor'
|
|
|
|
// and then remove this.
|
2019-01-02 16:38:54 +03:00
|
|
|
get(actorID) {
|
2018-09-26 11:03:33 +03:00
|
|
|
if (this.__poolMap) {
|
|
|
|
return this._poolMap.get(actorID);
|
|
|
|
}
|
|
|
|
return null;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
// True if this pool has no children.
|
2019-01-02 16:38:54 +03:00
|
|
|
isEmpty() {
|
2015-05-28 18:54:00 +03:00
|
|
|
return !this.__poolMap || this._poolMap.size == 0;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2016-09-30 00:51:15 +03:00
|
|
|
// Generator that yields each non-self child of the pool.
|
2019-01-02 16:38:54 +03:00
|
|
|
* poolChildren() {
|
2016-09-30 00:51:15 +03:00
|
|
|
if (!this.__poolMap) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const actor of this.__poolMap.values()) {
|
2016-09-30 00:51:15 +03:00
|
|
|
// Self-owned actors are ok, but don't need visiting twice.
|
|
|
|
if (actor === this) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
yield actor;
|
|
|
|
}
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2016-09-30 00:51:15 +03:00
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* Destroy this item, removing it from a parent if it has one,
|
|
|
|
* and destroying all children if necessary.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
destroy() {
|
2018-06-01 13:36:09 +03:00
|
|
|
const parent = this.parent();
|
2013-06-06 23:29:27 +04:00
|
|
|
if (parent) {
|
|
|
|
parent.unmanage(this);
|
|
|
|
}
|
|
|
|
if (!this.__poolMap) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const actor of this.__poolMap.values()) {
|
2013-06-06 23:29:27 +04:00
|
|
|
// Self-owned actors are ok, but don't need destroying twice.
|
|
|
|
if (actor === this) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-01 13:36:09 +03:00
|
|
|
const destroy = actor.destroy;
|
2013-06-06 23:29:27 +04:00
|
|
|
if (destroy) {
|
|
|
|
// Disconnect destroy while we're destroying in case of (misbehaving)
|
|
|
|
// circular ownership.
|
|
|
|
actor.destroy = null;
|
|
|
|
destroy.call(actor);
|
|
|
|
actor.destroy = destroy;
|
|
|
|
}
|
2016-05-17 21:25:54 +03:00
|
|
|
}
|
2014-04-08 11:23:00 +04:00
|
|
|
this.conn.removeActorPool(this, true);
|
2013-06-06 23:29:27 +04:00
|
|
|
this.__poolMap.clear();
|
|
|
|
this.__poolMap = null;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For getting along with the debugger server pools, should be removable
|
|
|
|
* eventually.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
cleanup() {
|
2013-06-06 23:29:27 +04:00
|
|
|
this.destroy();
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
exports.Pool = Pool;
|
|
|
|
|
2018-04-25 15:46:44 +03:00
|
|
|
/**
|
|
|
|
* Keep track of which actorSpecs have been created. If a replica of a spec
|
|
|
|
* is created, it can be caught, and specs which inherit from other specs will
|
|
|
|
* not overwrite eachother.
|
|
|
|
*/
|
|
|
|
var actorSpecs = new WeakMap();
|
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
/**
|
|
|
|
* An actor in the actor tree.
|
2017-08-21 21:21:48 +03:00
|
|
|
*
|
|
|
|
* @param optional conn
|
|
|
|
* Either a DebuggerServerConnection or a DebuggerClient. Must have
|
|
|
|
* addActorPool, removeActorPool, and poolFor.
|
|
|
|
* conn can be null if the subclass provides a conn property.
|
|
|
|
* @constructor
|
2013-06-06 23:29:27 +04:00
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
class Actor extends Pool {
|
|
|
|
// Existing Actors extending this class expect initialize to contain constructor logic.
|
|
|
|
initialize(conn) {
|
|
|
|
// Repeat Pool.constructor here as we can't call it from initialize
|
|
|
|
// This is to be removed once actors switch to es classes and are able to call
|
|
|
|
// Actor's contructor.
|
|
|
|
if (conn) {
|
|
|
|
this.conn = conn;
|
2017-08-21 21:21:48 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
// Will contain the actor's ID
|
|
|
|
this.actorID = null;
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
this._actorSpec = actorSpecs.get(Object.getPrototypeOf(this));
|
|
|
|
// Forward events to the connection.
|
|
|
|
if (this._actorSpec && this._actorSpec.events) {
|
|
|
|
for (const [name, request] of this._actorSpec.events.entries()) {
|
|
|
|
this.on(name, (...args) => {
|
|
|
|
this._sendEvent(name, request, ...args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
toString() {
|
2016-12-02 20:32:04 +03:00
|
|
|
return "[Actor " + this.typeName + "/" + this.actorID + "]";
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2014-10-12 00:08:07 +04:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
_sendEvent(name, request, ...args) {
|
2018-08-27 15:31:48 +03:00
|
|
|
if (!this.actorID) {
|
|
|
|
console.error(`Tried to send a '${name}' event on an already destroyed actor` +
|
|
|
|
` '${this.typeName}'`);
|
|
|
|
return;
|
|
|
|
}
|
2014-09-25 23:22:41 +04:00
|
|
|
let packet;
|
|
|
|
try {
|
|
|
|
packet = request.write(args, this);
|
2016-05-17 21:25:54 +03:00
|
|
|
} catch (ex) {
|
2014-09-25 23:22:41 +04:00
|
|
|
console.error("Error sending event: " + name);
|
|
|
|
throw ex;
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
packet.from = packet.from || this.actorID;
|
|
|
|
this.conn.send(packet);
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
destroy() {
|
|
|
|
super.destroy();
|
2013-06-06 23:29:27 +04:00
|
|
|
this.actorID = null;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Override this method in subclasses to serialize the actor.
|
|
|
|
* @param [optional] string hint
|
|
|
|
* Optional string to customize the form.
|
|
|
|
* @returns A jsonable object.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
form(hint) {
|
2016-05-17 21:25:54 +03:00
|
|
|
return { actor: this.actorID };
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
writeError(error, typeName, method) {
|
2018-05-09 13:44:54 +03:00
|
|
|
console.error(`Error while calling actor '${typeName}'s method '${method}'`,
|
|
|
|
error.message);
|
2016-04-01 10:17:50 +03:00
|
|
|
if (error.stack) {
|
2018-05-09 13:44:54 +03:00
|
|
|
console.error(error.stack);
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
this.conn.send({
|
|
|
|
from: this.actorID,
|
2016-04-01 10:17:50 +03:00
|
|
|
error: error.error || "unknownError",
|
2018-10-19 15:55:39 +03:00
|
|
|
message: error.message,
|
2013-06-06 23:29:27 +04:00
|
|
|
});
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-08-09 09:28:41 +04:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
_queueResponse(create) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const pending = this._pendingResponse || Promise.resolve(null);
|
|
|
|
const response = create(pending);
|
2013-08-09 09:28:41 +04:00
|
|
|
this._pendingResponse = response;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2018-05-17 16:04:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw an error with the passed message and attach an `error` property to the Error
|
|
|
|
* object so it can be consumed by the writeError function.
|
|
|
|
* @param {String} error: A string (usually a single word serving as an id) that will
|
|
|
|
* be assign to error.error.
|
|
|
|
* @param {String} message: The string that will be passed to the Error constructor.
|
|
|
|
* @throws This always throw.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
throwError(error, message) {
|
2018-05-17 16:04:24 +03:00
|
|
|
const err = new Error(message);
|
|
|
|
err.error = error;
|
|
|
|
throw err;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
exports.Actor = Actor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tags a prtotype method as an actor method implementation.
|
|
|
|
*
|
|
|
|
* @param function fn
|
|
|
|
* The implementation function, will be returned.
|
|
|
|
* @param spec
|
|
|
|
* The method specification, with the following (optional) properties:
|
|
|
|
* request (object): a request template.
|
|
|
|
* response (object): a response template.
|
|
|
|
* oneway (bool): 'true' if no response should be sent.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
exports.method = function(fn, spec = {}) {
|
2013-06-06 23:29:27 +04:00
|
|
|
fn._methodSpec = Object.freeze(spec);
|
2016-12-02 20:32:04 +03:00
|
|
|
if (spec.request) {
|
|
|
|
Object.freeze(spec.request);
|
|
|
|
}
|
|
|
|
if (spec.response) {
|
|
|
|
Object.freeze(spec.response);
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
return fn;
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
2016-04-28 13:10:40 +03:00
|
|
|
* Generates an actor specification from an actor description.
|
2013-06-06 23:29:27 +04:00
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
var generateActorSpec = function(actorDesc) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const actorSpec = {
|
2016-04-28 13:10:40 +03:00
|
|
|
typeName: actorDesc.typeName,
|
2018-10-19 15:55:39 +03:00
|
|
|
methods: [],
|
2013-06-06 23:29:27 +04:00
|
|
|
};
|
|
|
|
|
2016-04-28 13:10:40 +03:00
|
|
|
// Find method and form specifications attached to properties.
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const name of Object.getOwnPropertyNames(actorDesc)) {
|
|
|
|
const desc = Object.getOwnPropertyDescriptor(actorDesc, name);
|
2013-06-06 23:29:27 +04:00
|
|
|
if (!desc.value) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.value._methodSpec) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const methodSpec = desc.value._methodSpec;
|
|
|
|
const spec = {};
|
2016-04-28 13:10:40 +03:00
|
|
|
spec.name = methodSpec.name || name;
|
2017-08-21 21:21:48 +03:00
|
|
|
spec.request = new Request(Object.assign({type: spec.name},
|
2016-12-02 20:32:04 +03:00
|
|
|
methodSpec.request || undefined));
|
2017-08-21 21:21:48 +03:00
|
|
|
spec.response = new Response(methodSpec.response || undefined);
|
2016-04-28 13:10:40 +03:00
|
|
|
spec.release = methodSpec.release;
|
|
|
|
spec.oneway = methodSpec.oneway;
|
|
|
|
|
|
|
|
actorSpec.methods.push(spec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find additional method specifications
|
|
|
|
if (actorDesc.methods) {
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const name in actorDesc.methods) {
|
|
|
|
const methodSpec = actorDesc.methods[name];
|
|
|
|
const spec = {};
|
2016-04-28 13:10:40 +03:00
|
|
|
|
|
|
|
spec.name = methodSpec.name || name;
|
2017-08-21 21:21:48 +03:00
|
|
|
spec.request = new Request(Object.assign({type: spec.name},
|
2016-12-02 20:32:04 +03:00
|
|
|
methodSpec.request || undefined));
|
2017-08-21 21:21:48 +03:00
|
|
|
spec.response = new Response(methodSpec.response || undefined);
|
2016-04-28 13:10:40 +03:00
|
|
|
spec.release = methodSpec.release;
|
|
|
|
spec.oneway = methodSpec.oneway;
|
|
|
|
|
|
|
|
actorSpec.methods.push(spec);
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find event specifications
|
2016-04-28 13:10:40 +03:00
|
|
|
if (actorDesc.events) {
|
|
|
|
actorSpec.events = new Map();
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const name in actorDesc.events) {
|
|
|
|
const eventRequest = actorDesc.events[name];
|
2013-06-06 23:29:27 +04:00
|
|
|
Object.freeze(eventRequest);
|
2017-08-21 21:21:48 +03:00
|
|
|
actorSpec.events.set(name, new Request(Object.assign({type: name}, eventRequest)));
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 13:10:40 +03:00
|
|
|
if (!registeredTypes.has(actorSpec.typeName)) {
|
|
|
|
types.addActorType(actorSpec.typeName);
|
|
|
|
}
|
|
|
|
registeredTypes.get(actorSpec.typeName).actorSpec = actorSpec;
|
|
|
|
|
|
|
|
return actorSpec;
|
|
|
|
};
|
|
|
|
exports.generateActorSpec = generateActorSpec;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates request handlers as described by the given actor specification on
|
|
|
|
* the given actor prototype. Returns the actor prototype.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
var generateRequestHandlers = function(actorSpec, actorProto) {
|
2016-04-28 13:10:40 +03:00
|
|
|
actorProto.typeName = actorSpec.typeName;
|
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
// Generate request handlers for each method definition
|
|
|
|
actorProto.requestTypes = Object.create(null);
|
2016-04-28 13:10:40 +03:00
|
|
|
actorSpec.methods.forEach(spec => {
|
2018-06-01 13:36:09 +03:00
|
|
|
const handler = function(packet, conn) {
|
2013-06-06 23:29:27 +04:00
|
|
|
try {
|
2014-09-25 23:22:41 +04:00
|
|
|
let args;
|
|
|
|
try {
|
|
|
|
args = spec.request.read(packet, this);
|
2016-05-17 21:25:54 +03:00
|
|
|
} catch (ex) {
|
2015-01-02 23:37:51 +03:00
|
|
|
console.error("Error reading request: " + packet.type);
|
2014-09-25 23:22:41 +04:00
|
|
|
throw ex;
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2018-03-28 11:08:06 +03:00
|
|
|
if (!this[spec.name]) {
|
|
|
|
throw new Error(`Spec for '${actorProto.typeName}' specifies a '${spec.name}'` +
|
|
|
|
` method that isn't implemented by the actor`);
|
|
|
|
}
|
2018-06-01 13:36:09 +03:00
|
|
|
const ret = this[spec.name].apply(this, args);
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const sendReturn = (retToSend) => {
|
2014-09-25 23:22:41 +04:00
|
|
|
if (spec.oneway) {
|
|
|
|
// No need to send a response.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let response;
|
|
|
|
try {
|
2016-12-02 20:32:04 +03:00
|
|
|
response = spec.response.write(retToSend, this);
|
2016-05-17 21:25:54 +03:00
|
|
|
} catch (ex) {
|
2014-09-25 23:22:41 +04:00
|
|
|
console.error("Error writing response to: " + spec.name);
|
|
|
|
throw ex;
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
response.from = this.actorID;
|
|
|
|
// If spec.release has been specified, destroy the object.
|
|
|
|
if (spec.release) {
|
2013-10-31 05:29:06 +04:00
|
|
|
try {
|
|
|
|
this.destroy();
|
2016-05-17 21:25:54 +03:00
|
|
|
} catch (e) {
|
2018-05-09 13:44:54 +03:00
|
|
|
this.writeError(e, actorProto.typeName, spec.name);
|
2013-10-31 05:29:06 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
conn.send(response);
|
|
|
|
};
|
|
|
|
|
2013-08-09 09:28:41 +04:00
|
|
|
this._queueResponse(p => {
|
|
|
|
return p
|
|
|
|
.then(() => ret)
|
|
|
|
.then(sendReturn)
|
2018-05-09 13:44:54 +03:00
|
|
|
.catch(e => this.writeError(e, actorProto.typeName, spec.name));
|
2016-05-17 21:25:54 +03:00
|
|
|
});
|
|
|
|
} catch (e) {
|
2013-08-09 09:28:41 +04:00
|
|
|
this._queueResponse(p => {
|
2018-05-09 13:44:54 +03:00
|
|
|
return p.then(() => this.writeError(e, actorProto.typeName, spec.name));
|
2013-08-09 09:28:41 +04:00
|
|
|
});
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
actorProto.requestTypes[spec.request.type] = handler;
|
|
|
|
});
|
|
|
|
|
|
|
|
return actorProto;
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2016-04-28 13:10:40 +03:00
|
|
|
/**
|
|
|
|
* Create an actor class for the given actor specification and prototype.
|
|
|
|
*
|
|
|
|
* @param object actorSpec
|
|
|
|
* The actor specification. Must have a 'typeName' property.
|
|
|
|
* @param object actorProto
|
|
|
|
* The actor prototype. Should have method definitions, can have event
|
|
|
|
* definitions.
|
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
var ActorClassWithSpec = function(actorSpec, actorProto) {
|
2016-04-28 13:10:40 +03:00
|
|
|
if (!actorSpec.typeName) {
|
|
|
|
throw Error("Actor specification must have a typeName member.");
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
2014-04-18 03:25:07 +04:00
|
|
|
|
2017-08-21 21:21:48 +03:00
|
|
|
// Existing Actors are relying on the initialize instead of constructor methods.
|
2018-06-01 13:36:09 +03:00
|
|
|
const cls = function() {
|
|
|
|
const instance = Object.create(cls.prototype);
|
2017-08-21 21:21:48 +03:00
|
|
|
instance.initialize.apply(instance, arguments);
|
|
|
|
return instance;
|
|
|
|
};
|
|
|
|
cls.prototype = extend(Actor.prototype, generateRequestHandlers(actorSpec, actorProto));
|
2016-04-28 13:10:40 +03:00
|
|
|
|
2018-04-25 15:46:44 +03:00
|
|
|
actorSpecs.set(cls.prototype, actorSpec);
|
|
|
|
|
2014-04-18 03:25:07 +04:00
|
|
|
return cls;
|
2013-06-06 23:29:27 +04:00
|
|
|
};
|
2016-08-22 17:25:57 +03:00
|
|
|
exports.ActorClassWithSpec = ActorClassWithSpec;
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for client-side actor fronts.
|
2017-08-21 21:21:48 +03:00
|
|
|
*
|
|
|
|
* @param optional conn
|
|
|
|
* Either a DebuggerServerConnection or a DebuggerClient. Must have
|
|
|
|
* addActorPool, removeActorPool, and poolFor.
|
|
|
|
* conn can be null if the subclass provides a conn property.
|
|
|
|
* @param optional form
|
|
|
|
* The json form provided by the server.
|
|
|
|
* @constructor
|
2013-06-06 23:29:27 +04:00
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
class Front extends Pool {
|
2019-01-28 21:42:39 +03:00
|
|
|
constructor(conn = null, form = null) {
|
2019-01-02 16:38:54 +03:00
|
|
|
super(conn);
|
|
|
|
this.actorID = null;
|
|
|
|
this._requests = [];
|
|
|
|
|
|
|
|
// Front listener functions registered via `onFront` get notified
|
|
|
|
// of new fronts via this dedicated EventEmitter object.
|
|
|
|
this._frontListeners = new EventEmitter();
|
|
|
|
|
|
|
|
// List of optional listener for each event, that is processed immediatly on packet
|
|
|
|
// receival, before emitting event via EventEmitter on the Front.
|
|
|
|
// These listeners are register via Front.before function.
|
|
|
|
// Map(Event Name[string] => Event Listener[function])
|
|
|
|
this._beforeListeners = new Map();
|
|
|
|
|
|
|
|
// protocol.js no longer uses this data in the constructor, only external
|
|
|
|
// uses do. External usage of manually-constructed fronts will be
|
|
|
|
// drastically reduced if we convert the root and target actors to
|
|
|
|
// protocol.js, in which case this can probably go away.
|
|
|
|
if (form) {
|
|
|
|
this.actorID = form.actor;
|
|
|
|
}
|
2017-08-21 21:21:48 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
destroy() {
|
2013-10-31 05:29:06 +04:00
|
|
|
// Reject all outstanding requests, they won't make sense after
|
|
|
|
// the front is destroyed.
|
|
|
|
while (this._requests && this._requests.length > 0) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const { deferred, to, type, stack } = this._requests.shift();
|
|
|
|
const msg = "Connection closed, pending request to " + to +
|
2015-07-09 08:29:35 +03:00
|
|
|
", type " + type + " failed" +
|
|
|
|
"\n\nRequest stack:\n" + stack.formattedStack;
|
|
|
|
deferred.reject(new Error(msg));
|
2013-10-31 05:29:06 +04:00
|
|
|
}
|
2019-01-02 16:38:54 +03:00
|
|
|
super.destroy();
|
2018-12-14 20:34:53 +03:00
|
|
|
this.clearEvents();
|
2013-06-06 23:29:27 +04:00
|
|
|
this.actorID = null;
|
2018-11-15 13:23:00 +03:00
|
|
|
this._frontListeners = null;
|
2018-12-11 19:32:41 +03:00
|
|
|
this._beforeListeners = null;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
manage(front) {
|
2014-09-26 11:35:00 +04:00
|
|
|
if (!front.actorID) {
|
|
|
|
throw new Error("Can't manage front without an actor ID.\n" +
|
|
|
|
"Ensure server supports " + front.typeName + ".");
|
|
|
|
}
|
2019-01-02 16:38:54 +03:00
|
|
|
super.manage(front);
|
2018-11-15 13:23:00 +03:00
|
|
|
|
|
|
|
// Call listeners registered via `onFront` method
|
|
|
|
this._frontListeners.emit(front.typeName, front);
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2018-11-15 13:23:00 +03:00
|
|
|
|
|
|
|
// Run callback on every front of this type that currently exists, and on every
|
|
|
|
// instantiation of front type in the future.
|
|
|
|
onFront(typeName, callback) {
|
|
|
|
// First fire the callback on already instantiated fronts
|
|
|
|
for (const front of this.poolChildren()) {
|
|
|
|
if (front.typeName == typeName) {
|
|
|
|
callback(front);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Then register the callback for fronts instantiated in the future
|
|
|
|
this._frontListeners.on(typeName, callback);
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2014-09-26 11:35:00 +04:00
|
|
|
|
2018-12-11 19:32:41 +03:00
|
|
|
/**
|
|
|
|
* Register an event listener that will be called immediately on packer receival.
|
|
|
|
* The given callback is going to be called before emitting the event via EventEmitter
|
|
|
|
* API on the Front. Event emitting will be delayed if the callback is async.
|
|
|
|
* Only one such listener can be registered per type of event.
|
|
|
|
*
|
|
|
|
* @param String type
|
|
|
|
* Event emitted by the actor to intercept.
|
|
|
|
* @param Function callback
|
|
|
|
* Function that will process the event.
|
|
|
|
*/
|
|
|
|
before(type, callback) {
|
|
|
|
if (this._beforeListeners.has(type)) {
|
|
|
|
throw new Error(`Can't register multiple before listeners for "${type}".`);
|
|
|
|
}
|
|
|
|
this._beforeListeners.set(type, callback);
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2018-12-11 19:32:41 +03:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
toString() {
|
2016-12-02 20:32:04 +03:00
|
|
|
return "[Front for " + this.typeName + "/" + this.actorID + "]";
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the actor from its representation.
|
|
|
|
* Subclasses should override this.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
form(form) {}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a packet on the connection.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
send(packet) {
|
2018-11-28 20:58:15 +03:00
|
|
|
if (packet.to) {
|
2018-11-28 20:36:25 +03:00
|
|
|
this.conn._transport.send(packet);
|
2018-11-28 20:58:15 +03:00
|
|
|
} else {
|
|
|
|
packet.to = this.actorID;
|
|
|
|
// The connection might be closed during the promise resolution
|
|
|
|
if (this.conn._transport) {
|
|
|
|
this.conn._transport.send(packet);
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a two-way request on the connection.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
request(packet) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const deferred = defer();
|
2015-02-26 04:23:06 +03:00
|
|
|
// Save packet basics for debugging
|
2018-06-01 13:36:09 +03:00
|
|
|
const { to, type } = packet;
|
2015-02-26 04:23:06 +03:00
|
|
|
this._requests.push({
|
|
|
|
deferred,
|
|
|
|
to: to || this.actorID,
|
2015-06-26 10:55:24 +03:00
|
|
|
type,
|
2016-08-05 22:17:17 +03:00
|
|
|
stack: getStack(),
|
2015-02-26 04:23:06 +03:00
|
|
|
});
|
2013-06-06 23:29:27 +04:00
|
|
|
this.send(packet);
|
|
|
|
return deferred.promise;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for incoming packets from the client's actor.
|
|
|
|
*/
|
2019-01-02 16:38:54 +03:00
|
|
|
onPacket(packet) {
|
2013-06-06 23:29:27 +04:00
|
|
|
// Pick off event packets
|
2018-06-01 13:36:09 +03:00
|
|
|
const type = packet.type || undefined;
|
2014-05-15 18:36:03 +04:00
|
|
|
if (this._clientSpec.events && this._clientSpec.events.has(type)) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const event = this._clientSpec.events.get(packet.type);
|
2014-09-25 23:22:41 +04:00
|
|
|
let args;
|
|
|
|
try {
|
|
|
|
args = event.request.read(packet, this);
|
2016-05-17 21:25:54 +03:00
|
|
|
} catch (ex) {
|
2014-09-25 23:22:41 +04:00
|
|
|
console.error("Error reading event: " + packet.type);
|
2014-10-12 00:08:07 +04:00
|
|
|
console.exception(ex);
|
2014-09-25 23:22:41 +04:00
|
|
|
throw ex;
|
|
|
|
}
|
2018-12-11 19:32:41 +03:00
|
|
|
// Check for "pre event" callback to be processed before emitting events on fronts
|
|
|
|
// Use event.name instead of packet.type to use specific event name instead of RDP
|
|
|
|
// packet's type.
|
|
|
|
const beforeEvent = this._beforeListeners.get(event.name);
|
|
|
|
if (beforeEvent) {
|
|
|
|
const result = beforeEvent.apply(this, args);
|
|
|
|
// Check to see if the beforeEvent returned a promise -- if so,
|
2015-09-15 02:04:54 +03:00
|
|
|
// wait for their resolution before emitting. Otherwise, emit synchronously.
|
2018-12-11 19:32:41 +03:00
|
|
|
if (result && typeof result.then == "function") {
|
|
|
|
result.then(() => {
|
2019-01-02 16:38:54 +03:00
|
|
|
super.emit(event.name, ...args);
|
2016-12-02 20:32:04 +03:00
|
|
|
});
|
2015-09-15 02:04:54 +03:00
|
|
|
return;
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
2015-09-15 02:04:54 +03:00
|
|
|
|
2019-01-02 16:38:54 +03:00
|
|
|
super.emit(event.name, ...args);
|
2013-06-06 23:29:27 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remaining packets must be responses.
|
|
|
|
if (this._requests.length === 0) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const msg = "Unexpected packet " + this.actorID + ", " + JSON.stringify(packet);
|
|
|
|
const err = Error(msg);
|
2013-09-14 04:09:52 +04:00
|
|
|
console.error(err);
|
|
|
|
throw err;
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const { deferred, stack } = this._requests.shift();
|
2016-08-05 22:17:17 +03:00
|
|
|
callFunctionWithAsyncStack(() => {
|
2015-06-26 10:55:24 +03:00
|
|
|
if (packet.error) {
|
|
|
|
// "Protocol error" is here to avoid TBPL heuristics. See also
|
2016-08-16 03:28:05 +03:00
|
|
|
// https://dxr.mozilla.org/webtools-central/source/tbpl/php/inc/GeneralErrorFilter.php
|
2015-06-26 10:55:24 +03:00
|
|
|
let message;
|
|
|
|
if (packet.error && packet.message) {
|
|
|
|
message = "Protocol error (" + packet.error + "): " + packet.message;
|
|
|
|
} else {
|
|
|
|
message = packet.error;
|
|
|
|
}
|
|
|
|
deferred.reject(message);
|
2014-09-08 06:39:00 +04:00
|
|
|
} else {
|
2015-06-26 10:55:24 +03:00
|
|
|
deferred.resolve(packet);
|
2014-09-08 06:39:00 +04:00
|
|
|
}
|
2015-06-26 10:55:24 +03:00
|
|
|
}, stack, "DevTools RDP");
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2016-09-30 00:51:15 +03:00
|
|
|
|
|
|
|
hasRequests() {
|
|
|
|
return !!this._requests.length;
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
2016-09-30 00:51:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for all current requests from this front to settle. This is especially useful
|
|
|
|
* for tests and other utility environments that may not have events or mechanisms to
|
|
|
|
* await the completion of requests without this utility.
|
|
|
|
*
|
|
|
|
* @return Promise
|
|
|
|
* Resolved when all requests have settled.
|
|
|
|
*/
|
|
|
|
waitForRequestsToSettle() {
|
|
|
|
return settleAll(this._requests.map(({ deferred }) => deferred.promise));
|
2019-01-02 16:38:54 +03:00
|
|
|
}
|
|
|
|
}
|
2017-08-21 21:21:48 +03:00
|
|
|
|
2013-06-06 23:29:27 +04:00
|
|
|
exports.Front = Front;
|
|
|
|
|
|
|
|
/**
|
2016-04-28 13:10:40 +03:00
|
|
|
* Generates request methods as described by the given actor specification on
|
|
|
|
* the given front prototype. Returns the front prototype.
|
2013-06-06 23:29:27 +04:00
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
var generateRequestMethods = function(actorSpec, frontProto) {
|
2016-04-28 13:10:40 +03:00
|
|
|
if (frontProto._actorSpec) {
|
2013-06-06 23:29:27 +04:00
|
|
|
throw new Error("frontProto called twice on the same front prototype!");
|
|
|
|
}
|
2016-04-28 13:10:40 +03:00
|
|
|
|
|
|
|
frontProto.typeName = actorSpec.typeName;
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
// Generate request methods.
|
2018-06-01 13:36:09 +03:00
|
|
|
const methods = actorSpec.methods;
|
2013-06-06 23:29:27 +04:00
|
|
|
methods.forEach(spec => {
|
2018-12-11 19:32:41 +03:00
|
|
|
const name = spec.name;
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
frontProto[name] = function(...args) {
|
2018-11-29 13:00:06 +03:00
|
|
|
// If this.actorID are not available, the request will not be able to complete.
|
|
|
|
// The front was probably destroyed earlier.
|
|
|
|
if (!this.actorID) {
|
|
|
|
throw new Error(
|
|
|
|
`Can not send request because front '${this.typeName}' is already destroyed.`);
|
|
|
|
}
|
|
|
|
|
2014-09-25 23:22:41 +04:00
|
|
|
let packet;
|
|
|
|
try {
|
|
|
|
packet = spec.request.write(args, this);
|
2016-05-17 21:25:54 +03:00
|
|
|
} catch (ex) {
|
2014-09-25 23:22:41 +04:00
|
|
|
console.error("Error writing request: " + name);
|
|
|
|
throw ex;
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
if (spec.oneway) {
|
|
|
|
// Fire-and-forget oneway packets.
|
|
|
|
this.send(packet);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.request(packet).then(response => {
|
2014-09-25 23:22:41 +04:00
|
|
|
let ret;
|
|
|
|
try {
|
|
|
|
ret = spec.response.read(response, this);
|
2016-05-17 21:25:54 +03:00
|
|
|
} catch (ex) {
|
2014-09-25 23:22:41 +04:00
|
|
|
console.error("Error reading response to: " + name);
|
|
|
|
throw ex;
|
|
|
|
}
|
2013-06-06 23:29:27 +04:00
|
|
|
return ret;
|
2015-06-03 00:52:53 +03:00
|
|
|
});
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
|
|
|
// Release methods should call the destroy function on return.
|
|
|
|
if (spec.release) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const fn = frontProto[name];
|
2018-03-12 21:24:38 +03:00
|
|
|
frontProto[name] = function(...args) {
|
2013-06-06 23:29:27 +04:00
|
|
|
return fn.apply(this, args).then(result => {
|
|
|
|
this.destroy();
|
|
|
|
return result;
|
2016-05-17 21:25:54 +03:00
|
|
|
});
|
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Process event specifications
|
2016-04-28 13:10:40 +03:00
|
|
|
frontProto._clientSpec = {};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const actorEvents = actorSpec.events;
|
2016-12-02 20:32:04 +03:00
|
|
|
if (actorEvents) {
|
2016-04-28 13:10:40 +03:00
|
|
|
frontProto._clientSpec.events = new Map();
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const [name, request] of actorEvents) {
|
2016-04-28 13:10:40 +03:00
|
|
|
frontProto._clientSpec.events.set(request.type, {
|
2018-12-11 19:32:41 +03:00
|
|
|
name,
|
|
|
|
request,
|
2013-06-06 23:29:27 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 13:10:40 +03:00
|
|
|
|
|
|
|
frontProto._actorSpec = actorSpec;
|
|
|
|
|
|
|
|
return frontProto;
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2013-06-06 23:29:27 +04:00
|
|
|
|
2016-04-28 13:10:40 +03:00
|
|
|
/**
|
|
|
|
* Create a front class for the given actor specification and front prototype.
|
|
|
|
*
|
|
|
|
* @param object actorSpec
|
|
|
|
* The actor specification you're creating a front for.
|
2013-06-06 23:29:27 +04:00
|
|
|
* @param object proto
|
|
|
|
* The object prototype. Must have a 'typeName' property,
|
|
|
|
* should have method definitions, can have event definitions.
|
|
|
|
*/
|
2018-12-11 19:32:41 +03:00
|
|
|
var FrontClassWithSpec = function(actorSpec) {
|
|
|
|
class OneFront extends Front {
|
2016-04-28 13:10:40 +03:00
|
|
|
}
|
2018-12-11 19:32:41 +03:00
|
|
|
generateRequestMethods(actorSpec, OneFront.prototype);
|
|
|
|
return OneFront;
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2016-08-22 17:25:57 +03:00
|
|
|
exports.FrontClassWithSpec = FrontClassWithSpec;
|
2014-04-18 03:25:07 +04:00
|
|
|
|
2018-12-11 19:32:41 +03:00
|
|
|
exports.registerFront = function(cls) {
|
|
|
|
const { typeName } = cls.prototype;
|
|
|
|
if (!registeredTypes.has(typeName)) {
|
|
|
|
types.addActorType(typeName);
|
|
|
|
}
|
|
|
|
registeredTypes.get(typeName).frontClass = cls;
|
|
|
|
};
|
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
exports.dumpActorSpec = function(type) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const actorSpec = type.actorSpec;
|
|
|
|
const ret = {
|
2014-04-18 03:25:07 +04:00
|
|
|
category: "actor",
|
|
|
|
typeName: type.name,
|
|
|
|
methods: [],
|
2018-10-19 15:55:39 +03:00
|
|
|
events: {},
|
2014-04-18 03:25:07 +04:00
|
|
|
};
|
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const method of actorSpec.methods) {
|
2014-04-18 03:25:07 +04:00
|
|
|
ret.methods.push({
|
|
|
|
name: method.name,
|
|
|
|
release: method.release || undefined,
|
|
|
|
oneway: method.oneway || undefined,
|
|
|
|
request: method.request.describe(),
|
2018-10-19 15:55:39 +03:00
|
|
|
response: method.response.describe(),
|
2014-04-18 03:25:07 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actorSpec.events) {
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const [name, request] of actorSpec.events) {
|
2014-04-18 03:25:07 +04:00
|
|
|
ret.events[name] = request.describe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON.stringify(ret);
|
|
|
|
|
|
|
|
return ret;
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2014-04-18 03:25:07 +04:00
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
exports.dumpProtocolSpec = function() {
|
2018-06-01 13:36:09 +03:00
|
|
|
const ret = {
|
2014-04-18 03:25:07 +04:00
|
|
|
types: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let [name, type] of registeredTypes) {
|
|
|
|
// Force lazy instantiation if needed.
|
|
|
|
type = types.getType(name);
|
2018-06-01 13:36:09 +03:00
|
|
|
const category = type.category || undefined;
|
2014-05-15 18:36:03 +04:00
|
|
|
if (category === "dict") {
|
2014-04-18 03:25:07 +04:00
|
|
|
ret.types[name] = {
|
|
|
|
category: "dict",
|
|
|
|
typeName: name,
|
2018-10-19 15:55:39 +03:00
|
|
|
specializations: type.specializations,
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2014-05-15 18:36:03 +04:00
|
|
|
} else if (category === "actor") {
|
2014-04-18 03:25:07 +04:00
|
|
|
ret.types[name] = exports.dumpActorSpec(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-05-17 21:25:54 +03:00
|
|
|
};
|
2018-08-21 14:46:46 +03:00
|
|
|
|
2019-01-28 21:42:50 +03:00
|
|
|
/**
|
|
|
|
* Instantiate a global (preference, device) or target-scoped (webconsole, inspector)
|
|
|
|
* front of the given type by picking its actor ID out of either the target or root
|
|
|
|
* front's form.
|
|
|
|
*
|
|
|
|
* @param DebuggerClient client
|
|
|
|
* The DebuggerClient instance to use.
|
|
|
|
* @param string typeName
|
|
|
|
* The type name of the front to instantiate. This is defined in its specifiation.
|
|
|
|
* @param json form
|
|
|
|
* If we want to instantiate a global actor's front, this is the root front's form,
|
|
|
|
* otherwise we are instantiating a target-scoped front from the target front's form.
|
|
|
|
*/
|
2018-08-21 14:46:46 +03:00
|
|
|
function getFront(client, typeName, form) {
|
|
|
|
const type = types.getType(typeName);
|
|
|
|
if (!type) {
|
|
|
|
throw new Error(`No spec for front type '${typeName}'.`);
|
|
|
|
}
|
|
|
|
if (!type.frontClass) {
|
|
|
|
lazyLoadFront(typeName);
|
|
|
|
}
|
2018-12-11 19:32:41 +03:00
|
|
|
// Use intermediate Class variable to please eslint requiring
|
|
|
|
// a capital letter for all constructors.
|
|
|
|
const Class = type.frontClass;
|
2019-01-28 21:42:50 +03:00
|
|
|
const instance = new Class(client);
|
|
|
|
const { formAttributeName } = instance;
|
|
|
|
if (!formAttributeName) {
|
|
|
|
throw new Error(`Can't find the form attribute name for ${typeName}`);
|
|
|
|
}
|
|
|
|
// Retrive the actor ID from root or target actor's form
|
|
|
|
instance.actorID = form[formAttributeName];
|
|
|
|
if (!instance.actorID) {
|
|
|
|
throw new Error(`Can't find the actor ID for ${typeName} from root or target` +
|
|
|
|
` actor's form.`);
|
|
|
|
}
|
|
|
|
// Historically, all global and target scoped front were the first protocol.js in the
|
|
|
|
// hierarchy of fronts. So that they have to self-own themself. But now, Root and Target
|
|
|
|
// are fronts and should own them. The only issue here is that we should manage the
|
|
|
|
// front *before* calling initialize which is going to try managing child fronts.
|
|
|
|
instance.manage(instance);
|
|
|
|
|
2018-12-11 19:32:41 +03:00
|
|
|
if (typeof (instance.initialize) == "function") {
|
2019-01-28 21:42:50 +03:00
|
|
|
return instance.initialize().then(() => instance);
|
2018-12-11 19:32:41 +03:00
|
|
|
}
|
|
|
|
return instance;
|
2018-08-21 14:46:46 +03:00
|
|
|
}
|
|
|
|
exports.getFront = getFront;
|