-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[plugin] Cache command arguments to safely pass the command over JSON-RPC #5961
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,15 +13,20 @@ | |
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as theia from '@theia/plugin'; | ||
import { CommandRegistryExt, PLUGIN_RPC_CONTEXT as Ext, CommandRegistryMain } from '../common/plugin-api-rpc'; | ||
import { RPCProtocol } from '../common/rpc-protocol'; | ||
import { Disposable } from './types-impl'; | ||
import { KnownCommands } from './type-converters'; | ||
import { DisposableCollection } from '@theia/core'; | ||
|
||
// tslint:disable-next-line:no-any | ||
export type Handler = <T>(...args: any[]) => T | PromiseLike<T>; | ||
export type Handler = <T>(...args: any[]) => T | PromiseLike<T | undefined>; | ||
|
||
export interface ArgumentProcessor { | ||
// tslint:disable-next-line:no-any | ||
|
@@ -34,10 +39,16 @@ export class CommandRegistryImpl implements CommandRegistryExt { | |
private readonly commands = new Set<string>(); | ||
private readonly handlers = new Map<string, Handler>(); | ||
private readonly argumentProcessors: ArgumentProcessor[]; | ||
private readonly commandsConverter: CommandsConverter; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CommandRegistryImpl never does anything with the "commandsConverter". IMO, the converter should be owned by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be used by other APIs. |
||
|
||
constructor(rpc: RPCProtocol) { | ||
this.proxy = rpc.getProxy(Ext.COMMAND_REGISTRY_MAIN); | ||
this.argumentProcessors = []; | ||
this.commandsConverter = new CommandsConverter(this); | ||
} | ||
|
||
get converter(): CommandsConverter { | ||
return this.commandsConverter; | ||
} | ||
|
||
// tslint:disable-next-line:no-any | ||
|
@@ -78,7 +89,7 @@ export class CommandRegistryImpl implements CommandRegistryExt { | |
} | ||
|
||
// tslint:disable-next-line:no-any | ||
$executeCommand<T>(id: string, ...args: any[]): PromiseLike<T> { | ||
$executeCommand<T>(id: string, ...args: any[]): PromiseLike<T | undefined> { | ||
if (this.handlers.has(id)) { | ||
return this.executeLocalCommand(id, ...args); | ||
} else { | ||
|
@@ -102,7 +113,7 @@ export class CommandRegistryImpl implements CommandRegistryExt { | |
} | ||
|
||
// tslint:disable-next-line:no-any | ||
private async executeLocalCommand<T>(id: string, ...args: any[]): Promise<T> { | ||
private async executeLocalCommand<T>(id: string, ...args: any[]): Promise<T | undefined> { | ||
const handler = this.handlers.get(id); | ||
if (handler) { | ||
return handler<T>(...args.map(arg => this.argumentProcessors.reduce((r, p) => p.processArgument(r), arg))); | ||
|
@@ -123,3 +134,52 @@ export class CommandRegistryImpl implements CommandRegistryExt { | |
this.argumentProcessors.push(processor); | ||
} | ||
} | ||
|
||
// copied and modified from https://github.com/microsoft/vscode/blob/1.37.1/src/vs/workbench/api/common/extHostCommands.ts#L217-L259 | ||
export class CommandsConverter { | ||
akosyakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private readonly safeCommandId: string; | ||
private readonly commands: CommandRegistryImpl; | ||
private readonly commandsMap = new Map<number, theia.Command>(); | ||
private handle = 0; | ||
private isSafeCommandRegistered: boolean; | ||
|
||
constructor(commands: CommandRegistryImpl) { | ||
this.safeCommandId = `theia_safe_cmd_${Date.now().toString()}`; | ||
this.commands = commands; | ||
this.isSafeCommandRegistered = false; | ||
} | ||
|
||
/** | ||
* Convert to a command that can be safely passed over JSON-RPC. | ||
*/ | ||
toSafeCommand(command: theia.Command, disposables: DisposableCollection): theia.Command { | ||
if (!this.isSafeCommandRegistered) { | ||
this.commands.registerCommand({ id: this.safeCommandId }, this.executeSafeCommand, this); | ||
this.isSafeCommandRegistered = true; | ||
} | ||
|
||
const result: theia.Command = {}; | ||
Object.assign(result, command); | ||
|
||
if (command.command && command.arguments && command.arguments.length > 0) { | ||
tsmaeder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const id = this.handle++; | ||
this.commandsMap.set(id, command); | ||
disposables.push(new Disposable(() => this.commandsMap.delete(id))); | ||
result.command = this.safeCommandId; | ||
result.arguments = [id]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// tslint:disable-next-line:no-any | ||
private executeSafeCommand<R>(...args: any[]): PromiseLike<R | undefined> { | ||
const command = this.commandsMap.get(args[0]); | ||
if (!command || !command.command) { | ||
return Promise.reject('command NOT FOUND'); | ||
} | ||
return this.commands.executeCommand(command.command, ...(command.arguments || [])); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vinokurig command cannot be applied to composite elements? just checking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, according to previous logic that was lost, command is set to
fileNode
:https://github.com/theia-ide/theia/blob/93205109fa4017286ec0a441b0250b74b36c6495/packages/plugin-ext/src/main/browser/view/tree-views-main.tsx#L206-L218
bit is not set to
folderNode
:https://github.com/theia-ide/theia/blob/93205109fa4017286ec0a441b0250b74b36c6495/packages/plugin-ext/src/main/browser/view/tree-views-main.tsx#L189-L204
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant whether it should be set on the composite according to VS Code expectations? If so it would be nice to adjust it as well. It could be done separately, but needs an issue at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raised an issue with question #6025