/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var {
findPlaceholders,
getPath,
} = require("resource://devtools/shared/protocol/utils.js"); var {
types,
BULK_REQUEST,
} = require("resource://devtools/shared/protocol/types.js");
/** *Managesarequesttemplate.
*/ class Request { /** *@param{string}type *Thetypedefinedinthespecificationforthisrequest. *Formethods,itwillbetheattributenamein"methods"dictionary. *Forevents,itwillbetheattributenamein"events"dictionary. *@param{object}template *Therequesttemplate.
*/
constructor(type, template = {}) { // The EventEmitter event name (this.type, attribute name in the event specification file) emitted on the Actor/Front, // may be different from the RDP JSON packet event name (ret[type], type attribute value in the event specification file) // In the specification: // "my-event": { // <= EventEmitter name // type: "myEvent", // <= RDP packet type attribute // ... // } this.type = template.type || type;
/** *Writearequest. * *@param{Array}fnArgs *Thefunctionargumentstoplaceintherequest. *@param{object}ctx *Theobjectmakingtherequest. *@returnsarequestpacket.
*/
write(fnArgs, ctx) { // Bulk request can't send custom attributes/custom JSON packet. // Only communicate "type" and "length" attributes to the transport layer, // which will emit a JSON RDP packet with an additional "actor attribute. if (this.template === BULK_REQUEST) { // The Front's method is expected to be called with a unique object argument // with a "length" attribute, which refers to the total size of bytes to be // sent via a the bulk StreamCopier. if (typeof fnArgs[0].length != "number") { thrownew Error( "This front's method is expected to send a bulk request and should be called with an object argument with a length attribute."
);
} return { type: this.type, length: fnArgs[0].length };
}
const ret = {
type: this.type,
}; for (const key in this.template) { const value = this.template[key]; if (value instanceof Arg || value instanceof Option) {
ret[key] = value.write(
value.index in fnArgs ? fnArgs[value.index] : undefined,
ctx,
key
);
} elseif (key == "type") { // Ignore the type attribute which have already been considered in the constructor. continue;
} else { thrownew Error( "Request can only an object with `Arg` or `Option` properties"
);
}
} return ret;
}
/** *Readarequest. * *@param{object}packet *Therequestpacket. *@param{object}ctx *Theobjectmakingtherequest. *@returnsanargumentsarray
*/
read(packet, ctx) { if (this.template === BULK_REQUEST) { // The transport layer will convey a custom packet object with length, copyTo and copyToBuffer, // which we transfer to the Actor's method via a unique object argument. // This help know about the incoming data size and read the binary buffer via `copyTo` // or `copyToBuffer`. return [
{
length: packet.length,
copyTo: packet.copyTo,
copyToBuffer: packet.copyToBuffer,
},
];
}
/** *Placeholderforsimplearguments.
*/ class Arg { /** *@param{number}index *Theargumentindextoplaceatthisposition. *@paramtypetype *Theargumentshouldbemarshalledasthistype.
*/
constructor(index, type) { this.index = index; // Prevent force loading all Arg types by accessing it only when needed
loader.lazyGetter(this, "type", function () { return types.getType(type);
});
}
// Outside of protocol.js, Arg is called as factory method, without the new keyword.
exports.Arg = function (index, type) { returnnew Arg(index, type);
};
write(arg, ctx, name) { // Ignore if arg is undefined or null; allow other falsy values if (arg == undefined || arg[name] == undefined) { return undefined;
} const v = arg[name]; returnthis.type.write(v, ctx);
}
read(v, ctx, outArgs, name) { if (outArgs[this.index] === undefined) {
outArgs[this.index] = {};
} if (v === undefined) { return;
}
outArgs[this.index][name] = this.type.read(v, ctx);
}
}
// Outside of protocol.js, Option is called as factory method, without the new keyword.
exports.Option = function (index, type) { returnnew Option(index, type);
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.