microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler.ts
62lines · modeblame
259c018fYuri Skorokhodov5 years ago | 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 | export class IOSDirectCDPMessageHandler extends BaseCDPMessageHandler { | |
| 9 | private isBackcompatConfigured: boolean; | |
| 10 | | |
| 11 | constructor() { | |
| 12 | super(); | |
| 13 | this.isBackcompatConfigured = false; | |
| 14 | } | |
| 15 | | |
| 16 | public processDebuggerCDPMessage(event: any): ProcessedCDPMessage { | |
| 17 | let sendBack = false; | |
| 18 | if (!this.isBackcompatConfigured && event.method === CDP_API_NAMES.RUNTIME_ENABLE) { | |
| 19 | this.configureTargetForIWDPCommunication(); | |
| 20 | } | |
| 21 | return { | |
| 22 | event, | |
| 23 | sendBack, | |
| 24 | }; | |
| 25 | } | |
| 26 | | |
| 27 | public processApplicationCDPMessage(event: any): ProcessedCDPMessage { | |
| 28 | if (event.method === CDP_API_NAMES.CONSOLE_MESSAGE_ADDED) { | |
| 29 | event = this.processDeprecatedConsoleMessage(event); | |
| 30 | } | |
| 31 | if (event.result && event.result.properties) { | |
| 32 | event.result = { result: event.result.properties }; | |
| 33 | } | |
| 34 | return { | |
| 35 | event, | |
| 36 | sendBack: false, | |
| 37 | }; | |
| 38 | } | |
| 39 | | |
| 40 | private processDeprecatedConsoleMessage(event: any) { | |
| 41 | return { | |
| 42 | method: CDP_API_NAMES.RUNTIME_CONSOLE_API_CALLED, | |
| 43 | params: { | |
| 44 | type: event.params.message.type, | |
| 45 | timestamp: event.params.message.timestamp, | |
| 46 | args: event.params.message.parameters || [{ type: "string", value: event.params.message.text }], | |
| 47 | stackTrace: { callFrames: event.params.message.stack || event.params.message.stackTrace }, | |
| 48 | executionContextId: 1, | |
| 49 | }, | |
| 50 | }; | |
| 51 | } | |
| 52 | | |
| 53 | private configureTargetForIWDPCommunication(): void { | |
| 54 | this.isBackcompatConfigured = true; | |
| 55 | try { | |
| 56 | this.applicationTarget?.api.Console.enable({}); | |
| 57 | this.applicationTarget?.api.Debugger.setBreakpointsActive({ active: true }); | |
| 58 | } catch (err) { | |
| 59 | // Specifically ignore a fail here since it's only for backcompat | |
| 60 | } | |
| 61 | } | |
| 62 | } |