microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler.ts
117lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { BaseCDPMessageHandler } from "./baseCDPMessageHandler"; |
| 5 | import { ProcessedCDPMessage } from "./ICDPMessageHandler"; |
| 6 | import { CDP_API_NAMES } from "./CDPAPINames"; |
| 7 | |
| 8 | interface ExecutionContext { |
| 9 | id: number; |
| 10 | origin: string; |
| 11 | name: string; |
| 12 | auxData?: { |
| 13 | isDefault: boolean; |
| 14 | type?: "default" | "page"; |
| 15 | frameId?: string; |
| 16 | }; |
| 17 | } |
| 18 | |
| 19 | export class IOSDirectCDPMessageHandler extends BaseCDPMessageHandler { |
| 20 | private isBackcompatConfigured: boolean; |
| 21 | private customMessageLastId: number; |
| 22 | |
| 23 | constructor() { |
| 24 | super(); |
| 25 | this.isBackcompatConfigured = false; |
| 26 | this.customMessageLastId = 0; |
| 27 | } |
| 28 | |
| 29 | public processDebuggerCDPMessage(event: any): ProcessedCDPMessage { |
| 30 | let sendBack = false; |
| 31 | if (!this.isBackcompatConfigured && event.method === CDP_API_NAMES.RUNTIME_ENABLE) { |
| 32 | this.configureTargetForIWDPCommunication(); |
| 33 | this.configureDebuggerForIWDPCommunication(); |
| 34 | this.isBackcompatConfigured = true; |
| 35 | } |
| 36 | return { |
| 37 | event, |
| 38 | sendBack, |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | public processApplicationCDPMessage(event: any): ProcessedCDPMessage { |
| 43 | if (event.method === CDP_API_NAMES.CONSOLE_MESSAGE_ADDED) { |
| 44 | event = this.processDeprecatedConsoleMessage(event); |
| 45 | } |
| 46 | if (event.result && event.result.properties) { |
| 47 | event.result = { result: event.result.properties }; |
| 48 | } |
| 49 | return { |
| 50 | event, |
| 51 | sendBack: false, |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | private processDeprecatedConsoleMessage(event: any) { |
| 56 | return { |
| 57 | method: CDP_API_NAMES.RUNTIME_CONSOLE_API_CALLED, |
| 58 | params: { |
| 59 | type: event.params.message.type, |
| 60 | timestamp: event.params.message.timestamp, |
| 61 | args: event.params.message.parameters || [ |
| 62 | { type: "string", value: event.params.message.text }, |
| 63 | ], |
| 64 | stackTrace: { |
| 65 | callFrames: event.params.message.stack || event.params.message.stackTrace, |
| 66 | }, |
| 67 | executionContextId: 1, |
| 68 | }, |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | private configureTargetForIWDPCommunication(): void { |
| 73 | try { |
| 74 | this.applicationTarget?.api.Console.enable({}); |
| 75 | this.applicationTarget?.api.Debugger.setBreakpointsActive({ active: true }); |
| 76 | } catch (err) { |
| 77 | // Specifically ignore a fail here since it's only for backcompat |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private configureDebuggerForIWDPCommunication(): void { |
| 82 | const context: ExecutionContext = { |
| 83 | id: this.customMessageLastId++, |
| 84 | origin: "", |
| 85 | name: "IOS Execution Context", |
| 86 | auxData: { |
| 87 | isDefault: true, |
| 88 | }, |
| 89 | }; |
| 90 | try { |
| 91 | this.sendCustomRequestToDebuggerTarget( |
| 92 | CDP_API_NAMES.EXECUTION_CONTEXT_CREATED, |
| 93 | { context }, |
| 94 | false, |
| 95 | ); |
| 96 | } catch (err) { |
| 97 | throw Error("Could not create Execution context"); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private sendCustomRequestToDebuggerTarget( |
| 102 | method: string, |
| 103 | params: any = {}, |
| 104 | addMessageId: boolean = true, |
| 105 | ): void { |
| 106 | let request: any = { |
| 107 | method, |
| 108 | params, |
| 109 | }; |
| 110 | |
| 111 | if (addMessageId) { |
| 112 | request.id = this.customMessageLastId++; |
| 113 | } |
| 114 | |
| 115 | this.debuggerTarget?.send(request); |
| 116 | } |
| 117 | } |