|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { CancellationToken } from 'vs/base/common/cancellation'; |
| 7 | +import { toDisposable } from 'vs/base/common/lifecycle'; |
| 8 | +import { URI, UriComponents } from 'vs/base/common/uri'; |
| 9 | +import { IExtensionDescription } from 'vs/platform/extensions/common/extensions'; |
| 10 | +import { checkProposedApiEnabled } from 'vs/workbench/services/extensions/common/extensions'; |
| 11 | +import type * as vscode from 'vscode'; |
| 12 | +import { ExtHostProfileContentHandlersShape, IMainContext, MainContext, MainThreadProfileContentHandlersShape } from './extHost.protocol'; |
| 13 | + |
| 14 | + |
| 15 | +export class ExtHostProfileContentHandlers implements ExtHostProfileContentHandlersShape { |
| 16 | + |
| 17 | + private readonly proxy: MainThreadProfileContentHandlersShape; |
| 18 | + |
| 19 | + private readonly handlers = new Map<string, vscode.ProfileContentHandler>(); |
| 20 | + |
| 21 | + constructor( |
| 22 | + mainContext: IMainContext, |
| 23 | + ) { |
| 24 | + this.proxy = mainContext.getProxy(MainContext.MainThreadProfileContentHandlers); |
| 25 | + } |
| 26 | + |
| 27 | + registrProfileContentHandler( |
| 28 | + extension: IExtensionDescription, |
| 29 | + id: string, |
| 30 | + handler: vscode.ProfileContentHandler, |
| 31 | + ): vscode.Disposable { |
| 32 | + checkProposedApiEnabled(extension, 'profileContentHandlers'); |
| 33 | + if (this.handlers.has(id)) { |
| 34 | + throw new Error(`Handler with id '${id}' already registered`); |
| 35 | + } |
| 36 | + |
| 37 | + this.handlers.set(id, handler); |
| 38 | + this.proxy.$registerProfileContentHandler(id, handler.name, extension.identifier.value); |
| 39 | + |
| 40 | + return toDisposable(() => { |
| 41 | + this.handlers.delete(id); |
| 42 | + this.proxy.$unregisterProfileContentHandler(id); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + async $saveProfile(id: string, name: string, content: string, token: CancellationToken): Promise<UriComponents | null> { |
| 47 | + const handler = this.handlers.get(id); |
| 48 | + if (!handler) { |
| 49 | + throw new Error(`Unknown handler with id: ${id}`); |
| 50 | + } |
| 51 | + |
| 52 | + return handler.saveProfile(name, content, token); |
| 53 | + } |
| 54 | + |
| 55 | + async $readProfile(id: string, uri: UriComponents, token: CancellationToken): Promise<string | null> { |
| 56 | + const handler = this.handlers.get(id); |
| 57 | + if (!handler) { |
| 58 | + throw new Error(`Unknown handler with id: ${id}`); |
| 59 | + } |
| 60 | + |
| 61 | + return handler.readProfile(URI.revive(uri), token); |
| 62 | + } |
| 63 | +} |
0 commit comments