From bd8912080ba4b82558122f1f1a4e4eebd3ae6e96 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Wed, 1 Sep 2021 03:18:40 -0700 Subject: [PATCH] Kill IPC rendering / connection Summary: Remove IPC rendering / connection implementation. This has been behind GK for a couple of weeks, and no one complained / white listed. See also D30367413. flipper_js_client_emulator Reviewed By: passy Differential Revision: D30452442 fbshipit-source-id: fbe7b4914fecc023f3ec78a027106ea32c5a99bd --- .../src/chrome/JSEmulatorLauncherSheet.tsx | 108 ---------- desktop/app/src/chrome/SheetRenderer.tsx | 4 - desktop/app/src/reducers/application.tsx | 3 - .../appinspect/LaunchEmulator.tsx | 13 +- .../app/src/server/comms/ServerController.tsx | 13 -- .../app/src/server/comms/ServerWebSocket.tsx | 3 +- .../server/comms/ServerWebSocketBrowser.tsx | 2 +- desktop/app/src/server/devices/BaseDevice.tsx | 2 +- .../src/server/devices/webapp/JSDevice.tsx | 19 -- .../server/devices/webapp/jsServerUtils.tsx | 203 ------------------ desktop/static/SupportJSClientPreload.js | 94 -------- 11 files changed, 4 insertions(+), 460 deletions(-) delete mode 100644 desktop/app/src/chrome/JSEmulatorLauncherSheet.tsx delete mode 100644 desktop/app/src/server/devices/webapp/JSDevice.tsx delete mode 100644 desktop/app/src/server/devices/webapp/jsServerUtils.tsx delete mode 100644 desktop/static/SupportJSClientPreload.js diff --git a/desktop/app/src/chrome/JSEmulatorLauncherSheet.tsx b/desktop/app/src/chrome/JSEmulatorLauncherSheet.tsx deleted file mode 100644 index a2a64485e..000000000 --- a/desktop/app/src/chrome/JSEmulatorLauncherSheet.tsx +++ /dev/null @@ -1,108 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -import {Button} from '../ui'; -import React, {Component} from 'react'; -import {connect} from 'react-redux'; -import {State as Store} from '../reducers'; -import {launchJsEmulator} from '../server/devices/webapp/jsServerUtils'; -import {updateSettings, Action} from '../reducers/settings'; -import {Settings} from '../reducers/settings'; -import {Collapse, Form, Input as AntInput} from 'antd'; -import {Html5Outlined} from '@ant-design/icons'; - -type OwnProps = { - onHide: () => void; -}; - -type StateFromProps = { - settings: Settings; -}; - -type DispatchFromProps = { - updateSettings: (settings: Settings) => Action; -}; - -type State = { - url: string; - height: number; - width: number; -}; - -type Props = OwnProps & StateFromProps & DispatchFromProps; -class JSEmulatorLauncherSheet extends Component { - state: State = {...this.props.settings.jsApps.webAppLauncher}; - - applyChanges = async () => { - launchJsEmulator(this.state.url, this.state.height, this.state.width); - this.props.updateSettings({ - ...this.props.settings, - jsApps: { - webAppLauncher: {...this.state}, - }, - }); - this.props.onHide(); - }; - - onUrlChange = (e: React.ChangeEvent) => { - this.setState({url: e.target.value}); - }; - - onHeightChange = (e: React.ChangeEvent) => { - this.setState({height: Number(e.target.value)}); - }; - - onWidthChange = (e: React.ChangeEvent) => { - this.setState({width: Number(e.target.value)}); - }; - - render() { - const {url, height, width} = this.state; - return ( -
- - - - - - - - - - - - -
- ); - } -} - -const Launcher = connect( - ({settingsState}) => ({ - settings: settingsState, - }), - {updateSettings}, -)(JSEmulatorLauncherSheet); - -export default Launcher; - -export function JSEmulatorLauncherSheetSandy({onClose}: {onClose(): void}) { - return ( - - } - header="Launch JS Web App" - key="launch-js-web-app"> - - - - ); -} diff --git a/desktop/app/src/chrome/SheetRenderer.tsx b/desktop/app/src/chrome/SheetRenderer.tsx index aa0ead10a..55ef144c4 100644 --- a/desktop/app/src/chrome/SheetRenderer.tsx +++ b/desktop/app/src/chrome/SheetRenderer.tsx @@ -12,7 +12,6 @@ import ShareSheetExportUrl from './ShareSheetExportUrl'; import SignInSheet from './fb-stubs/SignInSheet'; import ExportDataPluginSheet from './ExportDataPluginSheet'; import ShareSheetExportFile from './ShareSheetExportFile'; -import JSEmulatorLauncherSheet from './JSEmulatorLauncherSheet'; import Sheet from './Sheet'; import { ACTIVE_SHEET_PLUGINS, @@ -23,7 +22,6 @@ import { ACTIVE_SHEET_SHARE_DATA_IN_FILE, ACTIVE_SHEET_SELECT_PLUGINS_TO_EXPORT, ACTIVE_SHEET_PLUGIN_SHEET, - ACTIVE_SHEET_JS_EMULATOR_LAUNCHER, ACTIVE_SHEET_CHANGELOG, ACTIVE_SHEET_CHANGELOG_RECENT_ONLY, } from '../reducers/application'; @@ -80,8 +78,6 @@ export function SheetRenderer({logger}: {logger: Logger}) { case ACTIVE_SHEET_PLUGIN_SHEET: // Currently unused. return null; - case ACTIVE_SHEET_JS_EMULATOR_LAUNCHER: - return ; default: return null; } diff --git a/desktop/app/src/reducers/application.tsx b/desktop/app/src/reducers/application.tsx index 0e818ee65..37c1973d0 100644 --- a/desktop/app/src/reducers/application.tsx +++ b/desktop/app/src/reducers/application.tsx @@ -26,8 +26,6 @@ export const ACTIVE_SHEET_SHARE_DATA_IN_FILE: 'SHARE_DATA_IN_FILE' = export const SET_EXPORT_STATUS_MESSAGE: 'SET_EXPORT_STATUS_MESSAGE' = 'SET_EXPORT_STATUS_MESSAGE'; export const UNSET_SHARE: 'UNSET_SHARE' = 'UNSET_SHARE'; -export const ACTIVE_SHEET_JS_EMULATOR_LAUNCHER: 'ACTIVE_SHEET_JS_EMULATOR_LAUNCHER' = - 'ACTIVE_SHEET_JS_EMULATOR_LAUNCHER'; export const ACTIVE_SHEET_CHANGELOG = 'ACTIVE_SHEET_CHANGELOG'; export const ACTIVE_SHEET_CHANGELOG_RECENT_ONLY = 'ACTIVE_SHEET_CHANGELOG_RECENT_ONLY'; @@ -41,7 +39,6 @@ export type ActiveSheet = | typeof ACTIVE_SHEET_DOCTOR | typeof ACTIVE_SHEET_SHARE_DATA_IN_FILE | typeof ACTIVE_SHEET_SELECT_PLUGINS_TO_EXPORT - | typeof ACTIVE_SHEET_JS_EMULATOR_LAUNCHER | typeof ACTIVE_SHEET_CHANGELOG | typeof ACTIVE_SHEET_CHANGELOG_RECENT_ONLY | null; diff --git a/desktop/app/src/sandy-chrome/appinspect/LaunchEmulator.tsx b/desktop/app/src/sandy-chrome/appinspect/LaunchEmulator.tsx index 82038f9f5..bc17c5e2e 100644 --- a/desktop/app/src/sandy-chrome/appinspect/LaunchEmulator.tsx +++ b/desktop/app/src/sandy-chrome/appinspect/LaunchEmulator.tsx @@ -24,8 +24,6 @@ import { launchSimulator, // TODO: move to iOSDeviceManager IOSDeviceParams, } from '../../server/devices/ios/iOSDeviceManager'; -import GK from '../../fb-stubs/GK'; -import {JSEmulatorLauncherSheetSandy} from '../../chrome/JSEmulatorLauncherSheet'; const COLD_BOOT = 'cold-boot'; @@ -143,7 +141,7 @@ export const LaunchEmulatorDialog = withTrackingScope( onClick={() => launchSimulator(device.udid) .catch((e) => { - console.error(e); + console.error('Failed to start simulator: ', e); message.error('Failed to start simulator: ' + e); }) .then(onClose) @@ -152,15 +150,6 @@ export const LaunchEmulatorDialog = withTrackingScope( )), ]; - // Launch JS emulator - if (GK.get('flipper_js_client_emulator')) { - items.push( - , - ); - } return ( ; diff --git a/desktop/app/src/server/devices/webapp/JSDevice.tsx b/desktop/app/src/server/devices/webapp/JSDevice.tsx deleted file mode 100644 index 2abc55cec..000000000 --- a/desktop/app/src/server/devices/webapp/JSDevice.tsx +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -import BaseDevice from '../BaseDevice'; - -export default class JSDevice extends BaseDevice { - webContentsId: number; - - constructor(serial: string, title: string, webContentsId: number) { - super(serial, 'emulator', title, 'JSWebApp'); - this.webContentsId = webContentsId; - } -} diff --git a/desktop/app/src/server/devices/webapp/jsServerUtils.tsx b/desktop/app/src/server/devices/webapp/jsServerUtils.tsx deleted file mode 100644 index 8d27d1278..000000000 --- a/desktop/app/src/server/devices/webapp/jsServerUtils.tsx +++ /dev/null @@ -1,203 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -import Client, {ClientQuery} from '../../../Client'; -import { - ClientConnection, - ConnectionStatus, - ConnectionStatusChange, - ResponseType, -} from '../../comms/ClientConnection'; -import {ipcRenderer, remote, IpcRendererEvent} from 'electron'; -import JSDevice from './JSDevice'; -import {buildClientId} from '../../../utils/clientUtils'; -import {FlipperServer} from '../../FlipperServer'; - -const connections: Map = new Map(); - -const availablePlugins: Map> = new Map(); - -function jsDeviceId(windowId: number): string { - return 'test_js_device' + windowId; -} - -export function initJsEmulatorIPC( - flipperServer: FlipperServer, - flipperConnections: Map< - string, - { - connection: ClientConnection | null | undefined; - client: Client; - } - >, -) { - ipcRenderer.on( - 'from-js-emulator-init-client', - (_event: IpcRendererEvent, message: any) => { - const {windowId} = message; - const {plugins, appName} = message.payload; - const device = new JSDevice(jsDeviceId(windowId), 'jsEmulator', windowId); - flipperServer.registerDevice(device); - - const connection = new JSClientFlipperConnection(windowId); - connections.set(windowId, connection); - availablePlugins.set(windowId, plugins); - - const query: ClientQuery = { - app: appName, - os: 'JSWebApp', - device: 'jsEmulator', - device_id: jsDeviceId(windowId), - sdk_version: 2, // hack to bybass callbacks in Client, will be fixed when JS Connection will be fully implemented - }; - const clientId = buildClientId(query); - - const client = new Client( - clientId, - query, - connection, - flipperServer.logger, - flipperServer.store, - plugins, - device, - ); - - flipperConnections.set(clientId, { - connection: connection, - client: client, - }); - - connection.subscribeToEvents((status) => { - if ( - status == ConnectionStatus.ERROR || - status == ConnectionStatus.CLOSED - ) { - console.debug(`Device disconnected ${client.id}`, 'server'); - flipperServer.server.removeConnection(client.id); - flipperServer.unregisterDevice(jsDeviceId(windowId)); - connections.delete(windowId); - availablePlugins.delete(windowId); - } - }); - - client - .init() - .then(() => { - console.log(client); - flipperServer.server.emit('new-client', client); - flipperServer.server.emit('clients-change'); - client.emit('plugins-change'); - - ipcRenderer.on( - 'from-js-emulator', - (_event: IpcRendererEvent, message: any) => { - const {command, payload} = message; - if (command === 'sendFlipperObject') { - client.onMessage( - JSON.stringify({ - params: { - api: payload.api, - method: payload.method, - params: JSON.parse(payload.params), - }, - method: 'execute', - }), - ); - } - }, - ); - }) - .catch((_) => {}); - }, - ); -} - -export function launchJsEmulator(url: string, height: number, width: number) { - const BrowserWindow = remote.BrowserWindow; - const win = new BrowserWindow({ - height: height, - width: width, - webPreferences: { - enableRemoteModule: true, - preload: require('path').join( - remote.app.getAppPath(), - 'SupportJSClientPreload.js', - ), - nodeIntegration: false, - contextIsolation: false, - allowRunningInsecureContent: true, - }, - }); - - win.webContents.on('preload-error', (_event, path, error) => { - console.log(path, error); - }); - - win.loadURL(url); - - win.webContents.on('did-finish-load', () => { - win.webContents.send('parent-window-id', remote.getCurrentWebContents().id); - - const childWindowId = win.webContents.id; - win.on('closed', () => { - connections.get(childWindowId)?.close(); - }); - }); -} - -export class JSClientFlipperConnection implements ClientConnection { - webContentsId: number; - connStatusSubscribers: Set = new Set(); - connStatus: ConnectionStatus; - - constructor(webContentsId: number) { - this.webContentsId = webContentsId; - this.connStatus = ConnectionStatus.CONNECTED; - } - subscribeToEvents(subscriber: ConnectionStatusChange): void { - this.connStatusSubscribers.add(subscriber); - } - send(data: any): void { - ipcRenderer.sendTo( - this.webContentsId, - 'message-to-plugin', - JSON.parse(data != null ? data : '{}'), - ); - } - // TODO: fully implement and return actual result - sendExpectResponse(data: any): Promise { - return new Promise((resolve, _) => { - const method = data != null ? JSON.parse(data).method : 'not-defined'; - - if (method !== 'getPlugins') { - this.send(data); - } - - if (method === 'getPlugins') { - resolve({ - success: { - plugins: availablePlugins.get(this.webContentsId), - }, - length: 0, - }); - } else { - resolve({ - success: undefined, - length: 0, - }); - } - }); - } - close(): void { - this.connStatus = ConnectionStatus.CLOSED; - this.connStatusSubscribers.forEach((subscriber) => { - subscriber(this.connStatus); - }); - } -} diff --git a/desktop/static/SupportJSClientPreload.js b/desktop/static/SupportJSClientPreload.js deleted file mode 100644 index 32c33a9bd..000000000 --- a/desktop/static/SupportJSClientPreload.js +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -// ============== -// Preload script -// ============== -const {remote, ipcRenderer} = require('electron'); - -const flipperState = { - mainWindowId: 0, - isClientInit: false, - plugins: null, - appName: 'JS App', -}; - -ipcRenderer.on('parent-window-id', (event, message) => { - flipperState.mainWindowId = message; -}); - -function cleanUpGWTArray(arr) { - const res = []; - for (let i = 0; i < arr.length; i++) { - res.push(arr[i]); - } - return res; -} - -function initClient(plugins, appName) { - if (flipperState.isClientInit) { - return; - } - if (plugins) { - flipperState.plugins = cleanUpGWTArray(plugins); - } - if (appName) { - flipperState.appName = appName; - } - if (flipperState.mainWindowId != 0) { - ipcRenderer.sendTo( - flipperState.mainWindowId, - 'from-js-emulator-init-client', - { - command: 'initClient', - windowId: remote.getCurrentWebContents().id, - payload: { - plugins: flipperState.plugins, - appName: flipperState.appName, - }, - }, - ); - flipperState.isClientInit = true; - } -} - -window.FlipperWebviewBridge = { - registerPlugins: function (plugins) { - flipperState.plugins = plugins; - }, - start: function (appName) { - flipperState.appName = appName; - initClient(); - }, - sendFlipperObject: function (plugin, method, data) { - initClient(); - if (flipperState.mainWindowId != 0) { - ipcRenderer.sendTo(flipperState.mainWindowId, 'from-js-emulator', { - command: 'sendFlipperObject', - payload: { - api: plugin, - method: method, - params: data, - }, - }); - } - }, - isFlipperSupported: true, - initClient: initClient, -}; - -ipcRenderer.on('message-to-plugin', (event, message) => { - const flipper = window.flipper; - if (!flipper) { - return; - } - const receiver = flipper.FlipperWebviewMessageReceiver.receive; - const {api, method, params} = message.params; - receiver(api, method, JSON.stringify(params)); -});