diff --git a/devtools/shared/resources/moz.build b/devtools/shared/resources/moz.build index 1a3ff8a72176..100f60f9cb5e 100644 --- a/devtools/shared/resources/moz.build +++ b/devtools/shared/resources/moz.build @@ -5,6 +5,7 @@ DIRS += [ 'legacy-listeners', 'legacy-target-watchers', + 'transformers', ] DevToolsModules( diff --git a/devtools/shared/resources/resource-watcher.js b/devtools/shared/resources/resource-watcher.js index 7046c38a1677..3d84b2808ae8 100644 --- a/devtools/shared/resources/resource-watcher.js +++ b/devtools/shared/resources/resource-watcher.js @@ -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"), +}; diff --git a/devtools/shared/resources/transformers/console-messages.js b/devtools/shared/resources/transformers/console-messages.js new file mode 100644 index 000000000000..536c3e2b6e9e --- /dev/null +++ b/devtools/shared/resources/transformers/console-messages.js @@ -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; +}; diff --git a/devtools/shared/resources/transformers/moz.build b/devtools/shared/resources/transformers/moz.build new file mode 100644 index 000000000000..7761df34b483 --- /dev/null +++ b/devtools/shared/resources/transformers/moz.build @@ -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' +)