mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-01 17:57:36 +08:00
Bug: chromium:1213943 Change-Id: I309436e8608c3f89ac42a117a3f454b5e43eef16 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2933138 Commit-Queue: Sigurd Schneider <sigurds@chromium.org> Reviewed-by: Tim van der Lippe <tvanderlippe@chromium.org>
175 lines
6.7 KiB
TypeScript
175 lines
6.7 KiB
TypeScript
// Copyright 2018 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
/* eslint-disable rulesdir/no_underscored_properties */
|
|
|
|
import * as Common from '../../core/common/common.js';
|
|
import * as Host from '../../core/host/host.js';
|
|
import * as i18n from '../../core/i18n/i18n.js';
|
|
import * as Components from '../../ui/legacy/components/utils/utils.js';
|
|
import type * as ProtocolProxyApi from '../../generated/protocol-proxy-api.js';
|
|
import type * as Protocol from '../../generated/protocol.js';
|
|
|
|
import type * as ProtocolClient from '../../core/protocol_client/protocol_client.js';
|
|
import * as SDK from '../../core/sdk/sdk.js';
|
|
|
|
const UIStrings = {
|
|
/**
|
|
*@description Text that refers to the main target
|
|
*/
|
|
main: 'Main',
|
|
/**
|
|
*@description Text in Node Main of the Sources panel when debugging a Node.js app
|
|
*@example {example.com} PH1
|
|
*/
|
|
nodejsS: 'Node.js: {PH1}',
|
|
};
|
|
const str_ = i18n.i18n.registerUIStrings('entrypoints/node_main/NodeMain.ts', UIStrings);
|
|
const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
|
|
let nodeMainImplInstance: NodeMainImpl;
|
|
|
|
export class NodeMainImpl extends Common.ObjectWrapper.ObjectWrapper implements Common.Runnable.Runnable {
|
|
static instance(opts: {forceNew: boolean|null} = {forceNew: null}): NodeMainImpl {
|
|
const {forceNew} = opts;
|
|
if (!nodeMainImplInstance || forceNew) {
|
|
nodeMainImplInstance = new NodeMainImpl();
|
|
}
|
|
return nodeMainImplInstance;
|
|
}
|
|
async run(): Promise<void> {
|
|
Host.userMetrics.actionTaken(Host.UserMetrics.Action.ConnectToNodeJSFromFrontend);
|
|
SDK.Connections.initMainConnection(async () => {
|
|
const target = SDK.TargetManager.TargetManager.instance().createTarget(
|
|
'main', i18nString(UIStrings.main), SDK.Target.Type.Browser, null);
|
|
target.setInspectedURL('Node.js');
|
|
}, Components.TargetDetachedDialog.TargetDetachedDialog.webSocketConnectionLost);
|
|
}
|
|
}
|
|
|
|
Common.Runnable.registerEarlyInitializationRunnable(NodeMainImpl.instance);
|
|
|
|
export class NodeChildTargetManager extends SDK.SDKModel.SDKModel implements ProtocolProxyApi.TargetDispatcher {
|
|
_targetManager: SDK.TargetManager.TargetManager;
|
|
_parentTarget: SDK.Target.Target;
|
|
_targetAgent: ProtocolProxyApi.TargetApi;
|
|
_childTargets: Map<string, SDK.Target.Target>;
|
|
_childConnections: Map<string, NodeConnection>;
|
|
constructor(parentTarget: SDK.Target.Target) {
|
|
super(parentTarget);
|
|
this._targetManager = parentTarget.targetManager();
|
|
this._parentTarget = parentTarget;
|
|
this._targetAgent = parentTarget.targetAgent();
|
|
this._childTargets = new Map();
|
|
this._childConnections = new Map();
|
|
|
|
parentTarget.registerTargetDispatcher(this);
|
|
this._targetAgent.invoke_setDiscoverTargets({discover: true});
|
|
|
|
Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
|
|
Host.InspectorFrontendHostAPI.Events.DevicesDiscoveryConfigChanged, this._devicesDiscoveryConfigChanged, this);
|
|
Host.InspectorFrontendHost.InspectorFrontendHostInstance.setDevicesUpdatesEnabled(false);
|
|
Host.InspectorFrontendHost.InspectorFrontendHostInstance.setDevicesUpdatesEnabled(true);
|
|
}
|
|
|
|
_devicesDiscoveryConfigChanged(event: Common.EventTarget.EventTargetEvent): void {
|
|
const config = (event.data as Adb.Config);
|
|
const locations = [];
|
|
for (const address of config.networkDiscoveryConfig) {
|
|
const parts = address.split(':');
|
|
const port = parseInt(parts[1], 10);
|
|
if (parts[0] && port) {
|
|
locations.push({host: parts[0], port: port});
|
|
}
|
|
}
|
|
this._targetAgent.invoke_setRemoteLocations({locations});
|
|
}
|
|
|
|
dispose(): void {
|
|
Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(
|
|
Host.InspectorFrontendHostAPI.Events.DevicesDiscoveryConfigChanged, this._devicesDiscoveryConfigChanged, this);
|
|
|
|
for (const sessionId of this._childTargets.keys()) {
|
|
this.detachedFromTarget({sessionId});
|
|
}
|
|
}
|
|
|
|
targetCreated({targetInfo}: Protocol.Target.TargetCreatedEvent): void {
|
|
if (targetInfo.type === 'node' && !targetInfo.attached) {
|
|
this._targetAgent.invoke_attachToTarget({targetId: targetInfo.targetId, flatten: false});
|
|
}
|
|
}
|
|
|
|
targetInfoChanged(_event: Protocol.Target.TargetInfoChangedEvent): void {
|
|
}
|
|
|
|
targetDestroyed(_event: Protocol.Target.TargetDestroyedEvent): void {
|
|
}
|
|
|
|
attachedToTarget({sessionId, targetInfo}: Protocol.Target.AttachedToTargetEvent): void {
|
|
const name = i18nString(UIStrings.nodejsS, {PH1: targetInfo.url});
|
|
const connection = new NodeConnection(this._targetAgent, sessionId);
|
|
this._childConnections.set(sessionId, connection);
|
|
const target = this._targetManager.createTarget(
|
|
targetInfo.targetId, name, SDK.Target.Type.Node, this._parentTarget, undefined, undefined, connection);
|
|
this._childTargets.set(sessionId, target);
|
|
target.runtimeAgent().invoke_runIfWaitingForDebugger();
|
|
}
|
|
|
|
detachedFromTarget({sessionId}: Protocol.Target.DetachedFromTargetEvent): void {
|
|
const childTarget = this._childTargets.get(sessionId);
|
|
if (childTarget) {
|
|
childTarget.dispose('target terminated');
|
|
}
|
|
this._childTargets.delete(sessionId);
|
|
this._childConnections.delete(sessionId);
|
|
}
|
|
|
|
receivedMessageFromTarget({sessionId, message}: Protocol.Target.ReceivedMessageFromTargetEvent): void {
|
|
const connection = this._childConnections.get(sessionId);
|
|
const onMessage = connection ? connection._onMessage : null;
|
|
if (onMessage) {
|
|
onMessage.call(null, message);
|
|
}
|
|
}
|
|
|
|
targetCrashed(_event: Protocol.Target.TargetCrashedEvent): void {
|
|
}
|
|
}
|
|
|
|
export class NodeConnection implements ProtocolClient.InspectorBackend.Connection {
|
|
_targetAgent: ProtocolProxyApi.TargetApi;
|
|
_sessionId: string;
|
|
_onMessage: ((arg0: (Object|string)) => void)|null;
|
|
_onDisconnect: ((arg0: string) => void)|null;
|
|
constructor(targetAgent: ProtocolProxyApi.TargetApi, sessionId: string) {
|
|
this._targetAgent = targetAgent;
|
|
this._sessionId = sessionId;
|
|
this._onMessage = null;
|
|
this._onDisconnect = null;
|
|
}
|
|
|
|
setOnMessage(onMessage: (arg0: (Object|string)) => void): void {
|
|
this._onMessage = onMessage;
|
|
}
|
|
|
|
setOnDisconnect(onDisconnect: (arg0: string) => void): void {
|
|
this._onDisconnect = onDisconnect;
|
|
}
|
|
|
|
sendRawMessage(message: string): void {
|
|
this._targetAgent.invoke_sendMessageToTarget({message, sessionId: this._sessionId});
|
|
}
|
|
|
|
async disconnect(): Promise<void> {
|
|
if (this._onDisconnect) {
|
|
this._onDisconnect.call(null, 'force disconnect');
|
|
}
|
|
this._onDisconnect = null;
|
|
this._onMessage = null;
|
|
await this._targetAgent.invoke_detachFromTarget({sessionId: this._sessionId});
|
|
}
|
|
}
|
|
|
|
SDK.SDKModel.SDKModel.register(NodeChildTargetManager, {capabilities: SDK.Target.Capability.Target, autostart: true});
|