urlService.ts 2.6 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { IURLService, IURLHandler, IOpenURLOptions } from 'vs/platform/url/common/url';
7
import { URI, UriComponents } from 'vs/base/common/uri';
8
import { IMainProcessService } from 'vs/platform/ipc/electron-sandbox/mainProcessService';
9
import { URLHandlerChannel } from 'vs/platform/url/common/urlIpc';
10
import { IOpenerService, IOpener, matchesScheme } from 'vs/platform/opener/common/opener';
11
import product from 'vs/platform/product/common/product';
12
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
13
import { createChannelSender } from 'vs/base/parts/ipc/common/ipc';
14
import { IElectronService } from 'vs/platform/electron/electron-sandbox/electron';
B
Benjamin Pasero 已提交
15
import { NativeURLService } from 'vs/platform/url/common/urlService';
16

17 18 19 20 21
export interface IRelayOpenURLOptions extends IOpenURLOptions {
	openToSide?: boolean;
	openExternal?: boolean;
}

B
Benjamin Pasero 已提交
22
export class RelayURLService extends NativeURLService implements IURLHandler, IOpener {
J
Joao Moreno 已提交
23

24 25
	private urlService: IURLService;

26
	constructor(
27
		@IMainProcessService mainProcessService: IMainProcessService,
28
		@IOpenerService openerService: IOpenerService,
J
Joao Moreno 已提交
29
		@IElectronService private electronService: IElectronService
30
	) {
31 32
		super();

33
		this.urlService = createChannelSender(mainProcessService.getChannel('url'));
34 35

		mainProcessService.registerChannel('urlHandler', new URLHandlerChannel(this));
36
		openerService.registerOpener(this);
37 38
	}

39 40 41 42 43
	create(options?: Partial<UriComponents>): URI {
		const uri = super.create(options);

		let query = uri.query;
		if (!query) {
44
			query = `windowId=${encodeURIComponent(this.electronService.windowId)}`;
45
		} else {
46
			query += `&windowId=${encodeURIComponent(this.electronService.windowId)}`;
47 48 49 50 51
		}

		return uri.with({ query });
	}

52
	async open(resource: URI | string, options?: IRelayOpenURLOptions): Promise<boolean> {
J
Johannes Rieken 已提交
53

54
		if (!matchesScheme(resource, product.urlProtocol)) {
B
Benjamin Pasero 已提交
55 56 57
			return false;
		}

58 59 60
		if (typeof resource === 'string') {
			resource = URI.parse(resource);
		}
61
		return await this.urlService.open(resource, options);
62 63
	}

64 65
	async handleURL(uri: URI, options?: IOpenURLOptions): Promise<boolean> {
		const result = await super.open(uri, options);
J
Joao Moreno 已提交
66 67 68 69 70 71

		if (result) {
			await this.electronService.focusWindow();
		}

		return result;
72 73
	}
}
74 75

registerSingleton(IURLService, RelayURLService);