Commit bce2ece3 authored by Dmitry Gozman's avatar Dmitry Gozman Committed by Commit Bot

[DevTools] Fix NodeMain to not use flatten protocol

We cannot route flatten protocol to node targets, so we should
keep wrapping node subtragets with Target.sendMessageToTarget.

Bug: 941608
Change-Id: I0b04379e143c32b61c3ede6047d6c967cd96af06
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1538484Reviewed-by: default avatarPavel Feldman <pfeldman@chromium.org>
Commit-Queue: Dmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#644616}
parent fee73e22
...@@ -32,6 +32,8 @@ NodeMain.NodeChildTargetManager = class extends SDK.SDKModel { ...@@ -32,6 +32,8 @@ NodeMain.NodeChildTargetManager = class extends SDK.SDKModel {
this._targetAgent = parentTarget.targetAgent(); this._targetAgent = parentTarget.targetAgent();
/** @type {!Map<string, !SDK.Target>} */ /** @type {!Map<string, !SDK.Target>} */
this._childTargets = new Map(); this._childTargets = new Map();
/** @type {!Map<string, !NodeMain.NodeConnection>} */
this._childConnections = new Map();
parentTarget.registerTargetDispatcher(this); parentTarget.registerTargetDispatcher(this);
this._targetAgent.setDiscoverTargets(true); this._targetAgent.setDiscoverTargets(true);
...@@ -74,7 +76,7 @@ NodeMain.NodeChildTargetManager = class extends SDK.SDKModel { ...@@ -74,7 +76,7 @@ NodeMain.NodeChildTargetManager = class extends SDK.SDKModel {
*/ */
targetCreated(targetInfo) { targetCreated(targetInfo) {
if (targetInfo.type === 'node' && !targetInfo.attached) if (targetInfo.type === 'node' && !targetInfo.attached)
this._targetAgent.attachToTarget(targetInfo.targetId, true /* flatten */); this._targetAgent.attachToTarget(targetInfo.targetId, false /* flatten */);
} }
/** /**
...@@ -99,8 +101,10 @@ NodeMain.NodeChildTargetManager = class extends SDK.SDKModel { ...@@ -99,8 +101,10 @@ NodeMain.NodeChildTargetManager = class extends SDK.SDKModel {
*/ */
attachedToTarget(sessionId, targetInfo, waitingForDebugger) { attachedToTarget(sessionId, targetInfo, waitingForDebugger) {
const name = ls`Node.js: ${targetInfo.url}`; const name = ls`Node.js: ${targetInfo.url}`;
const connection = new NodeMain.NodeConnection(this._targetAgent, sessionId);
this._childConnections.set(sessionId, connection);
const target = this._targetManager.createTarget( const target = this._targetManager.createTarget(
targetInfo.targetId, name, SDK.Target.Type.Node, this._parentTarget, sessionId); targetInfo.targetId, name, SDK.Target.Type.Node, this._parentTarget, undefined, undefined, connection);
this._childTargets.set(sessionId, target); this._childTargets.set(sessionId, target);
target.runtimeAgent().runIfWaitingForDebugger(); target.runtimeAgent().runIfWaitingForDebugger();
} }
...@@ -113,6 +117,7 @@ NodeMain.NodeChildTargetManager = class extends SDK.SDKModel { ...@@ -113,6 +117,7 @@ NodeMain.NodeChildTargetManager = class extends SDK.SDKModel {
detachedFromTarget(sessionId, childTargetId) { detachedFromTarget(sessionId, childTargetId) {
this._childTargets.get(sessionId).dispose('target terminated'); this._childTargets.get(sessionId).dispose('target terminated');
this._childTargets.delete(sessionId); this._childTargets.delete(sessionId);
this._childConnections.delete(sessionId);
} }
/** /**
...@@ -122,7 +127,62 @@ NodeMain.NodeChildTargetManager = class extends SDK.SDKModel { ...@@ -122,7 +127,62 @@ NodeMain.NodeChildTargetManager = class extends SDK.SDKModel {
* @param {string=} childTargetId * @param {string=} childTargetId
*/ */
receivedMessageFromTarget(sessionId, message, childTargetId) { receivedMessageFromTarget(sessionId, message, childTargetId) {
// We use flatten protocol. const connection = this._childConnections.get(sessionId);
const onMessage = connection ? connection._onMessage : null;
if (onMessage)
onMessage.call(null, message);
}
};
/**
* @implements {Protocol.Connection}
*/
NodeMain.NodeConnection = class {
/**
* @param {!Protocol.TargetAgent} targetAgent
* @param {string} sessionId
*/
constructor(targetAgent, sessionId) {
this._targetAgent = targetAgent;
this._sessionId = sessionId;
this._onMessage = null;
this._onDisconnect = null;
}
/**
* @override
* @param {function((!Object|string))} onMessage
*/
setOnMessage(onMessage) {
this._onMessage = onMessage;
}
/**
* @override
* @param {function(string)} onDisconnect
*/
setOnDisconnect(onDisconnect) {
this._onDisconnect = onDisconnect;
}
/**
* @override
* @param {string} message
*/
sendRawMessage(message) {
this._targetAgent.sendMessageToTarget(message, this._sessionId);
}
/**
* @override
* @return {!Promise}
*/
disconnect() {
if (this._onDisconnect)
this._onDisconnect.call(null, 'force disconnect');
this._onDisconnect = null;
this._onMessage = null;
return this._targetAgent.detachFromTarget(this._sessionId);
} }
}; };
......
...@@ -268,8 +268,11 @@ Protocol._GenericError = -32000; ...@@ -268,8 +268,11 @@ Protocol._GenericError = -32000;
Protocol._ConnectionClosedErrorCode = -32001; Protocol._ConnectionClosedErrorCode = -32001;
Protocol.SessionRouter = class { Protocol.SessionRouter = class {
constructor() { /**
this._connection = Protocol.Connection._factory(); * @param {!Protocol.Connection} connection
*/
constructor(connection) {
this._connection = connection;
this._lastMessageId = 1; this._lastMessageId = 1;
this._pendingResponsesCount = 0; this._pendingResponsesCount = 0;
this._domainToLogger = new Map(); this._domainToLogger = new Map();
...@@ -458,12 +461,21 @@ Protocol.TargetBase = class { ...@@ -458,12 +461,21 @@ Protocol.TargetBase = class {
* @param {boolean} needsNodeJSPatching * @param {boolean} needsNodeJSPatching
* @param {?Protocol.TargetBase} parentTarget * @param {?Protocol.TargetBase} parentTarget
* @param {string} sessionId * @param {string} sessionId
* @param {?Protocol.Connection} connection
*/ */
constructor(needsNodeJSPatching, parentTarget, sessionId) { constructor(needsNodeJSPatching, parentTarget, sessionId, connection) {
this._needsNodeJSPatching = needsNodeJSPatching; this._needsNodeJSPatching = needsNodeJSPatching;
this._sessionId = sessionId; this._sessionId = sessionId;
this._router = parentTarget ? parentTarget._router : new Protocol.SessionRouter(); if ((!parentTarget && connection) || (!parentTarget && sessionId) || (connection && sessionId))
throw new Error('Either connection or sessionId (but not both) must be supplied for a child target');
if (sessionId)
this._router = parentTarget._router;
else if (connection)
this._router = new Protocol.SessionRouter(connection);
else
this._router = new Protocol.SessionRouter(Protocol.Connection._factory());
this._router.registerSession(this, this._sessionId); this._router.registerSession(this, this._sessionId);
this._agents = {}; this._agents = {};
......
...@@ -16,10 +16,11 @@ SDK.Target = class extends Protocol.TargetBase { ...@@ -16,10 +16,11 @@ SDK.Target = class extends Protocol.TargetBase {
* @param {?SDK.Target} parentTarget * @param {?SDK.Target} parentTarget
* @param {string} sessionId * @param {string} sessionId
* @param {boolean} suspended * @param {boolean} suspended
* @param {?Protocol.Connection} connection
*/ */
constructor(targetManager, id, name, type, parentTarget, sessionId, suspended) { constructor(targetManager, id, name, type, parentTarget, sessionId, suspended, connection) {
const needsNodeJSPatching = type === SDK.Target.Type.Node; const needsNodeJSPatching = type === SDK.Target.Type.Node;
super(needsNodeJSPatching, parentTarget, sessionId); super(needsNodeJSPatching, parentTarget, sessionId, connection);
this._targetManager = targetManager; this._targetManager = targetManager;
this._name = name; this._name = name;
this._inspectedURL = ''; this._inspectedURL = '';
......
...@@ -172,10 +172,12 @@ SDK.TargetManager = class extends Common.Object { ...@@ -172,10 +172,12 @@ SDK.TargetManager = class extends Common.Object {
* @param {?SDK.Target} parentTarget * @param {?SDK.Target} parentTarget
* @param {string=} sessionId * @param {string=} sessionId
* @param {boolean=} waitForDebuggerInPage * @param {boolean=} waitForDebuggerInPage
* @param {!Protocol.Connection=} connection
* @return {!SDK.Target} * @return {!SDK.Target}
*/ */
createTarget(id, name, type, parentTarget, sessionId, waitForDebuggerInPage) { createTarget(id, name, type, parentTarget, sessionId, waitForDebuggerInPage, connection) {
const target = new SDK.Target(this, id, name, type, parentTarget, sessionId || '', this._isSuspended); const target =
new SDK.Target(this, id, name, type, parentTarget, sessionId || '', this._isSuspended, connection || null);
if (waitForDebuggerInPage) if (waitForDebuggerInPage)
target.pageAgent().waitForDebugger(); target.pageAgent().waitForDebugger();
target.createModels(new Set(this._modelObservers.keysArray())); target.createModels(new Set(this._modelObservers.keysArray()));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment