|
| 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 { ipcMain as ipc, IpcMainEvent, MimeTypedBuffer, protocol } from 'electron'; |
| 7 | +import { Disposable, toDisposable } from 'vs/base/common/lifecycle'; |
| 8 | +import { Schemas } from 'vs/base/common/network'; |
| 9 | +import { URI, UriComponents } from 'vs/base/common/uri'; |
| 10 | +import { IFileService } from 'vs/platform/files/common/files'; |
| 11 | +import { loadLocalResource, WebviewResourceResponse } from 'vs/platform/webview/common/resourceLoader'; |
| 12 | + |
| 13 | +export interface RegisterWebviewMetadata { |
| 14 | + readonly extensionLocation: URI | undefined; |
| 15 | + readonly localResourceRoots: readonly URI[]; |
| 16 | +} |
| 17 | + |
| 18 | +type ErrorCallback = (response: MimeTypedBuffer | { error: number }) => void; |
| 19 | + |
| 20 | + |
| 21 | +export class WebviewProtocolProvider extends Disposable { |
| 22 | + |
| 23 | + private readonly webviewMetadata = new Map<string, { |
| 24 | + readonly extensionLocation: URI | undefined; |
| 25 | + readonly localResourceRoots: URI[]; |
| 26 | + }>(); |
| 27 | + |
| 28 | + constructor( |
| 29 | + @IFileService private readonly fileService: IFileService, |
| 30 | + ) { |
| 31 | + super(); |
| 32 | + |
| 33 | + ipc.on('vscode:registerWebview', (event: IpcMainEvent, id: string, data: RegisterWebviewMetadata) => { |
| 34 | + this.webviewMetadata.set(id, { |
| 35 | + extensionLocation: data.extensionLocation ? URI.from(data.extensionLocation) : undefined, |
| 36 | + localResourceRoots: data.localResourceRoots.map((x: UriComponents) => URI.from(x)), |
| 37 | + }); |
| 38 | + |
| 39 | + event.sender.send(`vscode:didRegisterWebview-${id}`); |
| 40 | + }); |
| 41 | + |
| 42 | + ipc.on('vscode:unregisterWebview', (_event: IpcMainEvent, id: string) => { |
| 43 | + this.webviewMetadata.delete(id); |
| 44 | + }); |
| 45 | + |
| 46 | + |
| 47 | + protocol.registerBufferProtocol(Schemas.vscodeWebviewResource, async (request, callback): Promise<void> => { |
| 48 | + try { |
| 49 | + const uri = URI.parse(request.url); |
| 50 | + const resource = URI.parse(uri.path.replace(/^\/(\w+)/, '$1:')); |
| 51 | + |
| 52 | + const id = uri.authority; |
| 53 | + const metadata = this.webviewMetadata.get(id); |
| 54 | + if (metadata) { |
| 55 | + const result = await loadLocalResource(resource, this.fileService, metadata.extensionLocation, () => metadata.localResourceRoots); |
| 56 | + if (result.type === WebviewResourceResponse.Type.Success) { |
| 57 | + return callback({ |
| 58 | + data: Buffer.from(result.data.buffer), |
| 59 | + mimeType: result.mimeType |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + if (result.type === WebviewResourceResponse.Type.AccessDenied) { |
| 64 | + console.error('Webview: Cannot load resource outside of protocol root'); |
| 65 | + return (callback as ErrorCallback)({ error: -10 /* ACCESS_DENIED: https://cs.chromium.org/chromium/src/net/base/net_error_list.h */ }); |
| 66 | + } |
| 67 | + } |
| 68 | + } catch { |
| 69 | + // noop |
| 70 | + } |
| 71 | + |
| 72 | + return (callback as ErrorCallback)({ error: -2 }); |
| 73 | + }); |
| 74 | + |
| 75 | + this._register(toDisposable(() => protocol.unregisterProtocol(Schemas.vscodeWebviewResource))); |
| 76 | + } |
| 77 | +} |
0 commit comments