Bug 1644186 - Add ResourceTransformers. r=ochameau.

Differential Revision: https://phabricator.services.mozilla.com/D81831
This commit is contained in:
Nicolas Chevobbe 2020-07-03 12:21:31 +00:00
parent 52507b5218
commit d225d7ad21
4 changed files with 49 additions and 16 deletions

View File

@ -5,6 +5,7 @@
DIRS += [
'legacy-listeners',
'legacy-target-watchers',
'transformers',
]
DevToolsModules(

View File

@ -9,14 +9,6 @@ const EventEmitter = require("devtools/shared/event-emitter");
// eslint-disable-next-line mozilla/reject-some-requires
const { gDevTools } = require("devtools/client/framework/devtools");
// eslint-disable-next-line mozilla/reject-some-requires
loader.lazyRequireGetter(
this,
"getAdHocFrontOrPrimitiveGrip",
"devtools/client/fronts/object",
true
);
class ResourceWatcher {
/**
* This class helps retrieving existing and listening to resources.
@ -233,19 +225,20 @@ class ResourceWatcher {
* which describes the resource.
*/
_onResourceAvailable(targetFront, resources) {
for (const resource of resources) {
for (let resource of resources) {
// Put the targetFront on the resource for easy retrieval.
if (!resource.targetFront) {
resource.targetFront = targetFront;
}
const { resourceType } = resource;
if (resourceType == ResourceWatcher.TYPES.CONSOLE_MESSAGE) {
if (Array.isArray(resource.message.arguments)) {
// We might need to create fronts for each of the message arguments.
resource.message.arguments = resource.message.arguments.map(arg =>
getAdHocFrontOrPrimitiveGrip(arg, targetFront)
);
}
if (ResourceTransformers[resourceType]) {
resource = ResourceTransformers[resourceType]({
resource,
targetList: this.targetList,
targetFront,
isFissionEnabledOnContentToolbox: gDevTools.isFissionContentToolboxEnabled(),
});
}
this._availableListeners.emit(resourceType, {
@ -456,3 +449,12 @@ const LegacyListeners = {
[ResourceWatcher.TYPES
.ROOT_NODE]: require("devtools/shared/resources/legacy-listeners/root-node"),
};
// Optional transformers for each type of resource.
// Each module added here should be a function that will receive the resource, the target, …
// and perform some transformation on the resource before it will be emitted.
// This is a good place to handle backward compatibility and manual resource marshalling.
const ResourceTransformers = {
[ResourceWatcher.TYPES
.CONSOLE_MESSAGE]: require("devtools/shared/resources/transformers/console-messages"),
};

View File

@ -0,0 +1,23 @@
/* 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";
// eslint-disable-next-line mozilla/reject-some-requires
loader.lazyRequireGetter(
this,
"getAdHocFrontOrPrimitiveGrip",
"devtools/client/fronts/object",
true
);
module.exports = function({ resource, targetFront }) {
if (Array.isArray(resource.message.arguments)) {
// We might need to create fronts for each of the message arguments.
resource.message.arguments = resource.message.arguments.map(arg =>
getAdHocFrontOrPrimitiveGrip(arg, targetFront)
);
}
return resource;
};

View File

@ -0,0 +1,7 @@
# 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/.
DevToolsModules(
'console-messages.js'
)