提交 8609a070 编写于 作者: J Joao Moreno

implement open-url in windows

related to #10340
上级 f6d0cc16
......@@ -217,12 +217,12 @@ export function once<T>(event: Event<T>): Event<T> {
};
}
export function any(...events: Event<any>[]): Event<void> {
export function any<T>(...events: Event<T>[]): Event<T> {
let listeners = [];
const emitter = new Emitter<void>({
const emitter = new Emitter<T>({
onFirstListenerAdd() {
listeners = events.map(e => e(() => emitter.fire(), null));
listeners = events.map(e => e(r => emitter.fire(r)));
},
onLastListenerRemove() {
listeners = dispose(listeners);
......
......@@ -121,6 +121,7 @@ export class EnvService implements IEnvService {
extensionDevelopmentPath: normalizePath(argv.extensionDevelopmentPath),
extensionTestsPath: normalizePath(argv.extensionTestsPath),
'disable-extensions': argv['disable-extensions'],
'open-url': argv['open-url'],
locale: argv.locale,
wait: argv.wait
});
......
......@@ -11,6 +11,7 @@ import { VSCodeWindow } from 'vs/code/electron-main/window';
import { TPromise } from 'vs/base/common/winjs.base';
import { IChannel } from 'vs/base/parts/ipc/common/ipc';
import { ILogService } from 'vs/code/electron-main/log';
import { IURLService } from 'vs/platform/url/common/url';
export interface IStartArguments {
args: ICommandLineArguments;
......@@ -52,12 +53,21 @@ export class LaunchService implements ILaunchService {
constructor(
@ILogService private logService: ILogService,
@IWindowsService private windowsService: IWindowsService
@IWindowsService private windowsService: IWindowsService,
@IURLService private urlService: IURLService
) {}
start(args: ICommandLineArguments, userEnv: IProcessEnvironment): TPromise<void> {
this.logService.log('Received data from other instance: ', args, userEnv);
const openUrlArg = args['open-url'] || [];
const openUrl = typeof openUrlArg === 'string' ? [openUrlArg] : openUrlArg;
if (openUrl.length > 0) {
openUrl.forEach(url => this.urlService.open(url));
return TPromise.as(null);
}
// Otherwise handle in windows service
let usedWindows: VSCodeWindow[];
if (!!args.extensionDevelopmentPath) {
......
......@@ -40,6 +40,7 @@ import { IRequestService } from 'vs/platform/request/common/request';
import { RequestService } from 'vs/platform/request/node/requestService';
import { generateUuid } from 'vs/base/common/uuid';
import { getPathLabel } from 'vs/base/common/labels';
import { IURLService } from 'vs/platform/url/common/url';
import { URLChannel } from 'vs/platform/url/common/urlIpc';
import { URLService } from 'vs/platform/url/electron-main/urlService';
......@@ -125,7 +126,7 @@ function main(accessor: ServicesAccessor, mainIpcServer: Server, userEnv: IProce
const electronIpcServer = new ElectronIPCServer(ipc);
// Register Electron IPC services
const urlService = instantiationService.createInstance(URLService);
const urlService = accessor.get(IURLService);
const urlChannel = instantiationService.createInstance(URLChannel, urlService);
electronIpcServer.registerChannel('url', urlChannel);
......@@ -455,6 +456,7 @@ function start(): void {
services.set(IConfigurationService, new SyncDescriptor(ConfigurationService));
services.set(IRequestService, new SyncDescriptor(RequestService));
services.set(IUpdateService, new SyncDescriptor(UpdateManager));
services.set(IURLService, new SyncDescriptor(URLService, args['open-url']));
const instantiationService = new InstantiationService(services);
......
......@@ -31,6 +31,7 @@ export interface ParsedArgs extends minimist.ParsedArgs {
'list-extensions'?: boolean;
'install-extension'?: string | string[];
'uninstall-extension'?: string | string[];
'open-url'?: string | string[];
}
const options: minimist.Opts = {
......@@ -43,7 +44,8 @@ const options: minimist.Opts = {
'install-extension',
'uninstall-extension',
'debugBrkPluginHost',
'debugPluginHost'
'debugPluginHost',
'open-url'
],
boolean: [
'help',
......
......@@ -14,5 +14,6 @@ export const IURLService = createDecorator<IURLService>(ID);
export interface IURLService {
_serviceBrand: any;
open(url: string): void;
onOpenURL: Event<URI>;
}
......@@ -55,4 +55,8 @@ export class URLChannelClient implements IURLService {
private _onOpenURL = eventFromCall<URI>(this.channel, 'event:onOpenURL', this.windowID, URIDeserializer);
get onOpenURL(): Event<URI> { return this._onOpenURL; }
open(url: string): void {
return; // not implemented
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@
'use strict';
import Event, {chain, mapEvent, buffer} from 'vs/base/common/event';
import Event, {mapEvent, chain, buffer, Emitter, any} from 'vs/base/common/event';
import {fromEventEmitter} from 'vs/base/node/event';
import {IURLService} from 'vs/platform/url/common/url';
import product from 'vs/platform/product';
......@@ -16,23 +16,30 @@ export class URLService implements IURLService {
_serviceBrand: any;
private openUrlEmitter: Emitter<string> = new Emitter<string>();
onOpenURL: Event<URI>;
constructor() {
app.setAsDefaultProtocolClient(product.urlProtocol);
constructor(initial: string | string[] = []) {
const globalBuffer = (global.getOpenUrls() || []) as string[];
const initialBuffer = [
...(typeof initial === 'string' ? [initial] : initial),
...globalBuffer
];
app.setAsDefaultProtocolClient(product.urlProtocol, process.execPath, ['--open-url']);
const rawOnOpenUrl = fromEventEmitter(app, 'open-url', (event: Electron.Event, url: string) => ({ event, url }));
// always prevent default and return the url as string
const onOpenUrl = mapEvent(rawOnOpenUrl, ({ event, url }) => {
const preventedOnOpenUrl = mapEvent(rawOnOpenUrl, ({ event, url }) => {
event.preventDefault();
return url;
});
// buffer all `onOpenUrl` events until someone starts listening
const bufferedOnOpenUrl = buffer(onOpenUrl, true, global.getOpenUrls());
const bufferedOnOpenUrl = buffer(preventedOnOpenUrl, true, initialBuffer);
this.onOpenURL = chain(bufferedOnOpenUrl)
this.onOpenURL = chain(any(bufferedOnOpenUrl, this.openUrlEmitter.event))
.map(url => {
try {
return URI.parse(url);
......@@ -43,4 +50,8 @@ export class URLService implements IURLService {
.filter(uri => !!uri)
.event;
}
open(url: string): void {
this.openUrlEmitter.fire(url);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册