Skip to content

Commit

Permalink
Fixes #2: Render remote images for contributed commands
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Jun 13, 2018
1 parent 50591e5 commit 2491c68
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 6 deletions.
63 changes: 60 additions & 3 deletions src/vs/code/electron-main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

import { app, ipcMain as ipc } from 'electron';
import { app, ipcMain as ipc, protocol } from 'electron';
import * as platform from 'vs/base/common/platform';
import { WindowsManager } from 'vs/code/electron-main/windows';
import { IWindowsService, OpenContext, ActiveWindowManager } from 'vs/platform/windows/common/windows';
Expand Down Expand Up @@ -58,9 +58,11 @@ import { IIssueService } from 'vs/platform/issue/common/issue';
import { IssueChannel } from 'vs/platform/issue/common/issueIpc';
import { IssueService } from 'vs/platform/issue/electron-main/issueService';
import { LogLevelSetterChannel } from 'vs/platform/log/common/logIpc';
import { setUnexpectedErrorHandler } from 'vs/base/common/errors';
import * as errors from 'vs/base/common/errors';
import { ElectronURLListener } from 'vs/platform/url/electron-main/electronUrlListener';
import { serve as serveDriver } from 'vs/platform/driver/electron-main/driver';
import { REMOTE_EXTENSIONS_FILE_SYSTEM_CHANNEL_NAME, RemoteExtensionsFileSystemChannelClient } from 'vs/platform/remote/node/remoteFileSystemIpc';
import { RunOnceScheduler } from 'vs/base/common/async';

export class CodeApplication {

Expand Down Expand Up @@ -93,7 +95,7 @@ export class CodeApplication {
private registerListeners(): void {

// We handle uncaught exceptions here to prevent electron from opening a dialog to the user
setUnexpectedErrorHandler(err => this.onUnexpectedError(err));
errors.setUnexpectedErrorHandler(err => this.onUnexpectedError(err));
process.on('uncaughtException', err => this.onUnexpectedError(err));

app.on('will-quit', () => {
Expand Down Expand Up @@ -149,6 +151,61 @@ export class CodeApplication {
});
});

const connectionPool: Map<string, ActiveConnection> = new Map<string, ActiveConnection>();

class ActiveConnection {
private _authority: string;
private _client: TPromise<Client>;
private _disposeRunner: RunOnceScheduler;

constructor(authority: string) {
this._authority = authority;
const pieces = authority.split(':');
const host = pieces[0];
const port = parseInt(pieces[1], 10) + 1; // TODO@vs-remote
this._client = connect({ host, port }, `main`);
this._disposeRunner = new RunOnceScheduler(() => this._dispose(), 1000);
}

private _dispose(): void {
this._disposeRunner.dispose();
connectionPool.delete(this._authority);
this._client.then((connection) => {
connection.dispose();
});
}

public getClient(): TPromise<Client> {
this._disposeRunner.schedule();
return this._client;
}
}

protocol.registerBufferProtocol('vscode-remote', async (request, callback) => {
if (request.method !== 'GET') {
return callback(null);
}
const uri = URI.parse(request.url);
console.log(`REMOTE-FETCH: ${uri.toString()}`);

let activeConnection: ActiveConnection = null;
if (connectionPool.has(uri.authority)) {
activeConnection = connectionPool.get(uri.authority);
} else {
activeConnection = new ActiveConnection(uri.authority);
connectionPool.set(uri.authority, activeConnection);
}
try {
const rawClient = await activeConnection.getClient();
const client = new RemoteExtensionsFileSystemChannelClient(rawClient.getChannel(REMOTE_EXTENSIONS_FILE_SYSTEM_CHANNEL_NAME));
const fileContents = await client.getFile(uri.path);
callback(Buffer.from(fileContents, 'base64'));
} catch (err) {
errors.onUnexpectedError(err);
callback(null);
}
});

let macOpenFiles: string[] = [];
let runningTimeout: number = null;
app.on('open-file', (event: Event, path: string) => {
Expand Down
54 changes: 54 additions & 0 deletions src/vs/platform/remote/node/remoteFileSystemIpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

'use strict';

import { TPromise } from 'vs/base/common/winjs.base';
import { IChannel } from 'vs/base/parts/ipc/common/ipc';
import URI from 'vs/base/common/uri';
import * as pfs from 'vs/base/node/pfs';

export const REMOTE_EXTENSIONS_FILE_SYSTEM_CHANNEL_NAME = 'remoteextensionsfilesystem';

export interface IRemoteExtensionsFileSystem {
getFile(path: string): TPromise<string>;
}

export class RemoteExtensionsFileSystemImpl implements IRemoteExtensionsFileSystem {
async getFile(path: string): TPromise<string> {
// Ensure path has correct slashes
path = URI.file(path).fsPath;
const result = await pfs.readFile(path);
return result.toString('base64');
}
}

export interface IRemoteExtensionsFileSystemChannel extends IChannel {
call(command: 'getFile', arg: any): TPromise<string>;
}

export class RemoteExtensionsFileSystemChannel implements IRemoteExtensionsFileSystemChannel {

constructor(private service: IRemoteExtensionsFileSystem) { }

call(command: string, arg?: any): TPromise<any> {
switch (command) {
case 'getFile': return this.service.getFile(arg);
}
return undefined;
}
}

export class RemoteExtensionsFileSystemChannelClient implements IRemoteExtensionsFileSystem {

_serviceBrand: any;

constructor(private channel: IRemoteExtensionsFileSystemChannel) { }

getFile(path: string): TPromise<string> {
return this.channel.call('getFile', path);
}

}
2 changes: 1 addition & 1 deletion src/vs/workbench/electron-browser/bootstrap/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src 'self' https: data:; media-src 'none'; child-src 'self'; object-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' https:; font-src 'self' https:;">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src 'self' https: data: vscode-remote:; media-src 'none'; child-src 'self'; object-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' https:; font-src 'self' https:;">
</head>
<body class="monaco-shell vs-dark" aria-label="">
<script src="preload.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions src/vs/workbench/node/remoteExtensionsManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { InstantiationService } from 'vs/platform/instantiation/common/instantia
import { ExtensionManagementChannel } from 'vs/platform/extensionManagement/common/extensionManagementIpc';
import { RemoteExtensionsEnvironment } from 'vs/workbench/services/extensions/node/remoteExtensionsServiceImpl';
import { RemoteExtensionsEnvironmentChannel } from 'vs/workbench/services/extensions/node/remoteExtensionsIpc';
import { REMOTE_EXTENSIONS_FILE_SYSTEM_CHANNEL_NAME, RemoteExtensionsFileSystemImpl, RemoteExtensionsFileSystemChannel } from 'vs/platform/remote/node/remoteFileSystemIpc';

export interface IExtensionsManagementProcessInitData {
args: ParsedArgs;
Expand Down Expand Up @@ -79,6 +80,9 @@ export class RemoteExtensionManagementServer {
const remoteExtensionsEnvironemntChannel = new RemoteExtensionsEnvironmentChannel(new RemoteExtensionsEnvironment(this.environmentService));
server.registerChannel('remoteextensionsenvironment', remoteExtensionsEnvironemntChannel);

const remoteExtensionsFileSystemChannel = new RemoteExtensionsFileSystemChannel(new RemoteExtensionsFileSystemImpl());
server.registerChannel(REMOTE_EXTENSIONS_FILE_SYSTEM_CHANNEL_NAME, remoteExtensionsFileSystemChannel);

const extensionManagementService = accessor.get(IExtensionManagementService);
const channel = new ExtensionManagementChannel(extensionManagementService);
server.registerChannel('extensions', channel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { IChannel } from 'vs/base/parts/ipc/common/ipc';
import { IRemoteExtensionsEnvironmentData, IRemoteExtensionsEnvironment } from 'vs/workbench/services/extensions/common/remoteExtensionsService';

export interface IRemoteExtensionsEnvironmentChannel extends IChannel {
call(command: 'getData'): TPromise<IRemoteExtensionsEnvironmentData>;
call(command: string, arg?: any): TPromise<any>;
call(command: 'getRemoteExtensionInformation', arg: any): TPromise<IRemoteExtensionsEnvironmentData>;
}

export class RemoteExtensionsEnvironmentChannel implements IRemoteExtensionsEnvironmentChannel {
Expand Down

0 comments on commit 2491c68

Please sign in to comment.