mainThreadUrls.ts 2.0 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { ExtHostContext, IExtHostContext, MainContext, MainThreadUrlsShape, ExtHostUrlsShape } from 'vs/workbench/api/node/extHost.protocol';
import { extHostNamedCustomer } from './extHostCustomers';
import { TPromise } from 'vs/base/common/winjs.base';
import { IURLService, IURLHandler } from 'vs/platform/url/common/url';
import URI from 'vs/base/common/uri';
import { IDisposable } from 'vs/base/common/lifecycle';

class ExtensionUrlHandler implements IURLHandler {

	constructor(
		private readonly proxy: ExtHostUrlsShape,
		private readonly handle: number,
		private readonly extensionId: string
	) { }

	handleURL(uri: URI): TPromise<boolean> {
		if (uri.authority !== this.extensionId) {
			return TPromise.as(false);
		}

		return this.proxy.$handleUrl(this.handle, uri).then(() => true);
	}
}

@extHostNamedCustomer(MainContext.MainThreadUrls)
export class MainThreadUrls implements MainThreadUrlsShape {

	private readonly proxy: ExtHostUrlsShape;

	private handlers = new Map<number, IDisposable>();

	constructor(
		context: IExtHostContext,
		@IURLService private urlService: IURLService
	) {
		this.proxy = context.getProxy(ExtHostContext.ExtHostUrls);
	}

	$registerUrlHandler(handle: number, extensionId: string): TPromise<void> {
		const handler = new ExtensionUrlHandler(this.proxy, handle, extensionId);
		const disposable = this.urlService.registerHandler(handler);
		this.handlers.set(handle, disposable);

		return TPromise.as(null);
	}

	$unregisterUrlHandler(handle: number): TPromise<void> {
		const disposable = this.handlers.get(handle);

		if (!disposable) {
			return TPromise.as(null);
		}

		disposable.dispose();
		this.handlers.delete(handle);

		return TPromise.as(null);
	}

	dispose(): void {

	}
}