mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-03 09:35:40 -05:00
Merge from vscode a348d103d1256a06a2c9b3f9b406298a9fef6898 (#15681)
* Merge from vscode a348d103d1256a06a2c9b3f9b406298a9fef6898 * Fixes and cleanup * Distro * Fix hygiene yarn * delete no yarn lock changes file * Fix hygiene * Fix layer check * Fix CI * Skip lib checks * Remove tests deleted in vs code * Fix tests * Distro * Fix tests and add removed extension point * Skip failing notebook tests for now * Disable broken tests and cleanup build folder * Update yarn.lock and fix smoke tests * Bump sqlite * fix contributed actions and file spacing * Fix user data path * Update yarn.locks Co-authored-by: ADS Merger <karlb@microsoft.com>
This commit is contained in:
@@ -1,218 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { VSBufferReadableStream } from 'vs/base/common/buffer';
|
||||
import { CancellationToken } from 'vs/base/common/cancellation';
|
||||
import { isUNC } from 'vs/base/common/extpath';
|
||||
import { Schemas } from 'vs/base/common/network';
|
||||
import { sep } from 'vs/base/common/path';
|
||||
import { URI } from 'vs/base/common/uri';
|
||||
import { IHeaders } from 'vs/base/parts/request/common/request';
|
||||
import { FileOperationError, FileOperationResult, IFileService } from 'vs/platform/files/common/files';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { IRemoteConnectionData } from 'vs/platform/remote/common/remoteAuthorityResolver';
|
||||
import { IRequestService } from 'vs/platform/request/common/request';
|
||||
import { getWebviewContentMimeType } from 'vs/platform/webview/common/mimeTypes';
|
||||
|
||||
|
||||
export const webviewPartitionId = 'webview';
|
||||
|
||||
export namespace WebviewResourceResponse {
|
||||
export enum Type { Success, Failed, AccessDenied, NotModified }
|
||||
|
||||
export class StreamSuccess {
|
||||
readonly type = Type.Success;
|
||||
|
||||
constructor(
|
||||
public readonly stream: VSBufferReadableStream,
|
||||
public readonly etag: string | undefined,
|
||||
public readonly mimeType: string,
|
||||
) { }
|
||||
}
|
||||
|
||||
export const Failed = { type: Type.Failed } as const;
|
||||
export const AccessDenied = { type: Type.AccessDenied } as const;
|
||||
|
||||
export class NotModified {
|
||||
readonly type = Type.NotModified;
|
||||
|
||||
constructor(
|
||||
public readonly mimeType: string,
|
||||
) { }
|
||||
}
|
||||
|
||||
export type StreamResponse = StreamSuccess | typeof Failed | typeof AccessDenied | NotModified;
|
||||
}
|
||||
|
||||
export namespace WebviewFileReadResponse {
|
||||
export enum Type { Success, NotModified }
|
||||
|
||||
export class StreamSuccess {
|
||||
readonly type = Type.Success;
|
||||
|
||||
constructor(
|
||||
public readonly stream: VSBufferReadableStream,
|
||||
public readonly etag: string | undefined
|
||||
) { }
|
||||
}
|
||||
|
||||
export const NotModified = { type: Type.NotModified } as const;
|
||||
|
||||
export type Response = StreamSuccess | typeof NotModified;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a call to `IFileService.readFileStream` and converts the result to a `WebviewFileReadResponse.Response`
|
||||
*/
|
||||
export async function readFileStream(
|
||||
fileService: IFileService,
|
||||
resource: URI,
|
||||
etag: string | undefined,
|
||||
): Promise<WebviewFileReadResponse.Response> {
|
||||
try {
|
||||
const result = await fileService.readFileStream(resource, { etag });
|
||||
return new WebviewFileReadResponse.StreamSuccess(result.value, result.etag);
|
||||
} catch (e) {
|
||||
if (e instanceof FileOperationError) {
|
||||
const result = e.fileOperationResult;
|
||||
|
||||
// NotModified status is expected and can be handled gracefully
|
||||
if (result === FileOperationResult.FILE_NOT_MODIFIED_SINCE) {
|
||||
return WebviewFileReadResponse.NotModified;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise the error is unexpected. Re-throw and let caller handle it
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
export interface WebviewResourceFileReader {
|
||||
readFileStream(resource: URI, etag: string | undefined): Promise<WebviewFileReadResponse.Response>;
|
||||
}
|
||||
|
||||
export async function loadLocalResource(
|
||||
requestUri: URI,
|
||||
ifNoneMatch: string | undefined,
|
||||
options: {
|
||||
extensionLocation: URI | undefined;
|
||||
roots: ReadonlyArray<URI>;
|
||||
remoteConnectionData?: IRemoteConnectionData | null;
|
||||
rewriteUri?: (uri: URI) => URI,
|
||||
},
|
||||
fileReader: WebviewResourceFileReader,
|
||||
requestService: IRequestService,
|
||||
logService: ILogService,
|
||||
token: CancellationToken,
|
||||
): Promise<WebviewResourceResponse.StreamResponse> {
|
||||
logService.debug(`loadLocalResource - being. requestUri=${requestUri}`);
|
||||
|
||||
let resourceToLoad = getResourceToLoad(requestUri, options.roots);
|
||||
|
||||
logService.debug(`loadLocalResource - found resource to load. requestUri=${requestUri}, resourceToLoad=${resourceToLoad}`);
|
||||
|
||||
if (!resourceToLoad) {
|
||||
return WebviewResourceResponse.AccessDenied;
|
||||
}
|
||||
|
||||
const mime = getWebviewContentMimeType(requestUri); // Use the original path for the mime
|
||||
|
||||
// Perform extra normalization if needed
|
||||
if (options.rewriteUri) {
|
||||
resourceToLoad = options.rewriteUri(resourceToLoad);
|
||||
}
|
||||
|
||||
if (resourceToLoad.scheme === Schemas.http || resourceToLoad.scheme === Schemas.https) {
|
||||
const headers: IHeaders = {};
|
||||
if (ifNoneMatch) {
|
||||
headers['If-None-Match'] = ifNoneMatch;
|
||||
}
|
||||
|
||||
const response = await requestService.request({
|
||||
url: resourceToLoad.toString(true),
|
||||
headers: headers
|
||||
}, token);
|
||||
|
||||
logService.debug(`loadLocalResource - Loaded over http(s). requestUri=${requestUri}, response=${response.res.statusCode}`);
|
||||
|
||||
if (response.res.statusCode === 200) {
|
||||
return new WebviewResourceResponse.StreamSuccess(response.stream, response.res.headers['etag'], mime);
|
||||
}
|
||||
return WebviewResourceResponse.Failed;
|
||||
}
|
||||
|
||||
try {
|
||||
const contents = await fileReader.readFileStream(resourceToLoad, ifNoneMatch);
|
||||
logService.debug(`loadLocalResource - Loaded using fileReader. requestUri=${requestUri}`);
|
||||
|
||||
switch (contents.type) {
|
||||
case WebviewFileReadResponse.Type.Success:
|
||||
return new WebviewResourceResponse.StreamSuccess(contents.stream, contents.etag, mime);
|
||||
|
||||
case WebviewFileReadResponse.Type.NotModified:
|
||||
return new WebviewResourceResponse.NotModified(mime);
|
||||
|
||||
default:
|
||||
logService.error(`loadLocalResource - Unknown file read response`);
|
||||
return WebviewResourceResponse.Failed;
|
||||
}
|
||||
} catch (err) {
|
||||
logService.debug(`loadLocalResource - Error using fileReader. requestUri=${requestUri}`);
|
||||
console.log(err);
|
||||
|
||||
return WebviewResourceResponse.Failed;
|
||||
}
|
||||
}
|
||||
|
||||
function getResourceToLoad(
|
||||
requestUri: URI,
|
||||
roots: ReadonlyArray<URI>
|
||||
): URI | undefined {
|
||||
const normalizedPath = normalizeRequestPath(requestUri);
|
||||
|
||||
for (const root of roots) {
|
||||
if (containsResource(root, normalizedPath)) {
|
||||
return normalizedPath;
|
||||
}
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function normalizeRequestPath(requestUri: URI) {
|
||||
if (requestUri.scheme === Schemas.vscodeWebviewResource) {
|
||||
// The `vscode-webview-resource` scheme has the following format:
|
||||
//
|
||||
// vscode-webview-resource://id/scheme//authority?/path
|
||||
//
|
||||
|
||||
// Encode requestUri.path so that URI.parse can properly parse special characters like '#', '?', etc.
|
||||
const resourceUri = URI.parse(encodeURIComponent(requestUri.path).replace(/%2F/gi, '/').replace(/^\/([a-z0-9\-]+)(\/{1,2})/i, (_: string, scheme: string, sep: string) => {
|
||||
if (sep.length === 1) {
|
||||
return `${scheme}:///`; // Add empty authority.
|
||||
} else {
|
||||
return `${scheme}://`; // Url has own authority.
|
||||
}
|
||||
}));
|
||||
return resourceUri.with({
|
||||
query: requestUri.query,
|
||||
fragment: requestUri.fragment
|
||||
});
|
||||
} else {
|
||||
return requestUri;
|
||||
}
|
||||
}
|
||||
|
||||
function containsResource(root: URI, resource: URI): boolean {
|
||||
let rootPath = root.fsPath + (root.fsPath.endsWith(sep) ? '' : sep);
|
||||
let resourceFsPath = resource.fsPath;
|
||||
|
||||
if (isUNC(root.fsPath) && isUNC(resource.fsPath)) {
|
||||
rootPath = rootPath.toLowerCase();
|
||||
resourceFsPath = resourceFsPath.toLowerCase();
|
||||
}
|
||||
|
||||
return resourceFsPath.startsWith(rootPath);
|
||||
}
|
||||
@@ -3,14 +3,12 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { VSBuffer } from 'vs/base/common/buffer';
|
||||
import { UriComponents } from 'vs/base/common/uri';
|
||||
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { IRemoteConnectionData } from 'vs/platform/remote/common/remoteAuthorityResolver';
|
||||
import { IWebviewPortMapping } from 'vs/platform/webview/common/webviewPortMapping';
|
||||
|
||||
export const IWebviewManagerService = createDecorator<IWebviewManagerService>('webviewManagerService');
|
||||
|
||||
export const webviewPartitionId = 'webview';
|
||||
|
||||
export interface WebviewWebContentsId {
|
||||
readonly webContentsId: number;
|
||||
}
|
||||
@@ -19,27 +17,8 @@ export interface WebviewWindowId {
|
||||
readonly windowId: number;
|
||||
}
|
||||
|
||||
export type WebviewManagerDidLoadResourceResponse =
|
||||
{ buffer: VSBuffer, etag: string | undefined }
|
||||
| 'not-modified'
|
||||
| 'access-denied'
|
||||
| 'not-found';
|
||||
|
||||
export interface IWebviewManagerService {
|
||||
_serviceBrand: unknown;
|
||||
|
||||
registerWebview(id: string, windowId: number, metadata: RegisterWebviewMetadata): Promise<void>;
|
||||
unregisterWebview(id: string): Promise<void>;
|
||||
updateWebviewMetadata(id: string, metadataDelta: Partial<RegisterWebviewMetadata>): Promise<void>;
|
||||
|
||||
didLoadResource(requestId: number, response: WebviewManagerDidLoadResourceResponse): void;
|
||||
|
||||
setIgnoreMenuShortcuts(id: WebviewWebContentsId | WebviewWindowId, enabled: boolean): Promise<void>;
|
||||
}
|
||||
|
||||
export interface RegisterWebviewMetadata {
|
||||
readonly extensionLocation: UriComponents | undefined;
|
||||
readonly localResourceRoots: readonly UriComponents[];
|
||||
readonly remoteConnectionData: IRemoteConnectionData | null;
|
||||
readonly portMappings: readonly IWebviewPortMapping[];
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ import { IAddress } from 'vs/platform/remote/common/remoteAgentConnection';
|
||||
import { extractLocalHostUriMetaDataForPortMapping, ITunnelService, RemoteTunnel } from 'vs/platform/remote/common/tunnel';
|
||||
|
||||
export interface IWebviewPortMapping {
|
||||
webviewPort: number;
|
||||
extensionHostPort: number;
|
||||
readonly webviewPort: number;
|
||||
readonly extensionHostPort: number;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,14 +5,8 @@
|
||||
|
||||
import { session, WebContents, webContents } from 'electron';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { URI } from 'vs/base/common/uri';
|
||||
import { IFileService } from 'vs/platform/files/common/files';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { ITunnelService } from 'vs/platform/remote/common/tunnel';
|
||||
import { IRequestService } from 'vs/platform/request/common/request';
|
||||
import { webviewPartitionId } from 'vs/platform/webview/common/resourceLoader';
|
||||
import { IWebviewManagerService, RegisterWebviewMetadata, WebviewManagerDidLoadResourceResponse, WebviewWebContentsId, WebviewWindowId } from 'vs/platform/webview/common/webviewManagerService';
|
||||
import { WebviewPortMappingProvider } from 'vs/platform/webview/electron-main/webviewPortMappingProvider';
|
||||
import { IWebviewManagerService, webviewPartitionId, WebviewWebContentsId, WebviewWindowId } from 'vs/platform/webview/common/webviewManagerService';
|
||||
import { WebviewProtocolProvider } from 'vs/platform/webview/electron-main/webviewProtocolProvider';
|
||||
import { IWindowsMainService } from 'vs/platform/windows/electron-main/windows';
|
||||
|
||||
@@ -20,64 +14,24 @@ export class WebviewMainService extends Disposable implements IWebviewManagerSer
|
||||
|
||||
declare readonly _serviceBrand: undefined;
|
||||
|
||||
private readonly protocolProvider: WebviewProtocolProvider;
|
||||
private readonly portMappingProvider: WebviewPortMappingProvider;
|
||||
|
||||
constructor(
|
||||
@IFileService fileService: IFileService,
|
||||
@ILogService logService: ILogService,
|
||||
@IRequestService requestService: IRequestService,
|
||||
@ITunnelService tunnelService: ITunnelService,
|
||||
@IWindowsMainService private readonly windowsMainService: IWindowsMainService,
|
||||
) {
|
||||
super();
|
||||
this.protocolProvider = this._register(new WebviewProtocolProvider(fileService, logService, requestService, windowsMainService));
|
||||
this.portMappingProvider = this._register(new WebviewPortMappingProvider(tunnelService));
|
||||
this._register(new WebviewProtocolProvider());
|
||||
|
||||
const sess = session.fromPartition(webviewPartitionId);
|
||||
sess.setPermissionRequestHandler((webContents, permission /* 'media' | 'geolocation' | 'notifications' | 'midiSysex' | 'pointerLock' | 'fullscreen' | 'openExternal' */, callback) => {
|
||||
sess.setPermissionRequestHandler((_webContents, permission, callback) => {
|
||||
if (permission === 'clipboard-read') {
|
||||
return callback(true);
|
||||
}
|
||||
|
||||
return callback(false);
|
||||
});
|
||||
|
||||
sess.setPermissionCheckHandler((webContents, permission /* 'media' */) => {
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
public async registerWebview(id: string, windowId: number, metadata: RegisterWebviewMetadata): Promise<void> {
|
||||
const extensionLocation = metadata.extensionLocation ? URI.from(metadata.extensionLocation) : undefined;
|
||||
|
||||
this.protocolProvider.registerWebview(id, {
|
||||
...metadata,
|
||||
windowId: windowId,
|
||||
extensionLocation,
|
||||
localResourceRoots: metadata.localResourceRoots.map(x => URI.from(x))
|
||||
});
|
||||
|
||||
this.portMappingProvider.registerWebview(id, {
|
||||
extensionLocation,
|
||||
mappings: metadata.portMappings,
|
||||
resolvedAuthority: metadata.remoteConnectionData,
|
||||
});
|
||||
}
|
||||
|
||||
public async unregisterWebview(id: string): Promise<void> {
|
||||
this.protocolProvider.unregisterWebview(id);
|
||||
this.portMappingProvider.unregisterWebview(id);
|
||||
}
|
||||
|
||||
public async updateWebviewMetadata(id: string, metaDataDelta: Partial<RegisterWebviewMetadata>): Promise<void> {
|
||||
const extensionLocation = metaDataDelta.extensionLocation ? URI.from(metaDataDelta.extensionLocation) : undefined;
|
||||
|
||||
this.protocolProvider.updateWebviewMetadata(id, {
|
||||
...metaDataDelta,
|
||||
extensionLocation,
|
||||
localResourceRoots: metaDataDelta.localResourceRoots?.map(x => URI.from(x)),
|
||||
});
|
||||
|
||||
this.portMappingProvider.updateWebviewMetadata(id, {
|
||||
...metaDataDelta,
|
||||
extensionLocation,
|
||||
sess.setPermissionCheckHandler((_webContents, permission /* 'media' */) => {
|
||||
return permission === 'clipboard-read';
|
||||
});
|
||||
}
|
||||
|
||||
@@ -87,7 +41,7 @@ export class WebviewMainService extends Disposable implements IWebviewManagerSer
|
||||
if (typeof (id as WebviewWindowId).windowId === 'number') {
|
||||
const { windowId } = (id as WebviewWindowId);
|
||||
const window = this.windowsMainService.getWindowById(windowId);
|
||||
if (!window) {
|
||||
if (!window?.win) {
|
||||
throw new Error(`Invalid windowId: ${windowId}`);
|
||||
}
|
||||
contents = window.win.webContents;
|
||||
@@ -103,8 +57,4 @@ export class WebviewMainService extends Disposable implements IWebviewManagerSer
|
||||
contents.setIgnoreMenuShortcuts(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
public async didLoadResource(requestId: number, response: WebviewManagerDidLoadResourceResponse): Promise<void> {
|
||||
this.protocolProvider.didLoadResource(requestId, response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { OnBeforeRequestListenerDetails, session } from 'electron';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { URI } from 'vs/base/common/uri';
|
||||
import { IAddress } from 'vs/platform/remote/common/remoteAgentConnection';
|
||||
import { ITunnelService } from 'vs/platform/remote/common/tunnel';
|
||||
import { webviewPartitionId } from 'vs/platform/webview/common/resourceLoader';
|
||||
import { IWebviewPortMapping, WebviewPortMappingManager } from 'vs/platform/webview/common/webviewPortMapping';
|
||||
|
||||
interface OnBeforeRequestListenerDetails_Extended extends OnBeforeRequestListenerDetails {
|
||||
readonly lastCommittedOrigin?: string;
|
||||
}
|
||||
|
||||
interface PortMappingData {
|
||||
readonly extensionLocation: URI | undefined;
|
||||
readonly mappings: readonly IWebviewPortMapping[];
|
||||
readonly resolvedAuthority: IAddress | null | undefined;
|
||||
}
|
||||
|
||||
export class WebviewPortMappingProvider extends Disposable {
|
||||
|
||||
private readonly _webviewData = new Map<string, {
|
||||
readonly manager: WebviewPortMappingManager;
|
||||
metadata: PortMappingData;
|
||||
}>();
|
||||
|
||||
constructor(
|
||||
@ITunnelService private readonly _tunnelService: ITunnelService,
|
||||
) {
|
||||
super();
|
||||
|
||||
const sess = session.fromPartition(webviewPartitionId);
|
||||
|
||||
sess.webRequest.onBeforeRequest({
|
||||
urls: [
|
||||
'*://localhost:*/*',
|
||||
'*://127.0.0.1:*/*',
|
||||
'*://0.0.0.0:*/*',
|
||||
]
|
||||
}, async (details: OnBeforeRequestListenerDetails_Extended, callback) => {
|
||||
let origin: URI;
|
||||
try {
|
||||
origin = URI.parse(details.lastCommittedOrigin!);
|
||||
} catch {
|
||||
return callback({});
|
||||
}
|
||||
|
||||
const webviewId = origin.authority;
|
||||
const entry = this._webviewData.get(webviewId);
|
||||
if (!entry) {
|
||||
return callback({});
|
||||
}
|
||||
|
||||
const redirect = await entry.manager.getRedirect(entry.metadata.resolvedAuthority, details.url);
|
||||
return callback(redirect ? { redirectURL: redirect } : {});
|
||||
});
|
||||
}
|
||||
|
||||
public async registerWebview(id: string, metadata: PortMappingData): Promise<void> {
|
||||
const manager = new WebviewPortMappingManager(
|
||||
() => this._webviewData.get(id)?.metadata.extensionLocation,
|
||||
() => this._webviewData.get(id)?.metadata.mappings || [],
|
||||
this._tunnelService);
|
||||
|
||||
this._webviewData.set(id, { metadata, manager });
|
||||
}
|
||||
|
||||
public unregisterWebview(id: string): void {
|
||||
const existing = this._webviewData.get(id);
|
||||
if (existing) {
|
||||
existing.manager.dispose();
|
||||
this._webviewData.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
public async updateWebviewMetadata(id: string, metadataDelta: Partial<PortMappingData>): Promise<void> {
|
||||
const entry = this._webviewData.get(id);
|
||||
if (entry) {
|
||||
this._webviewData.set(id, {
|
||||
...entry,
|
||||
...metadataDelta,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,47 +4,24 @@
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { protocol, session } from 'electron';
|
||||
import { Readable } from 'stream';
|
||||
import { bufferToStream, VSBufferReadableStream } from 'vs/base/common/buffer';
|
||||
import { CancellationToken } from 'vs/base/common/cancellation';
|
||||
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { FileAccess, Schemas } from 'vs/base/common/network';
|
||||
import { URI } from 'vs/base/common/uri';
|
||||
import { FileOperationError, FileOperationResult, IFileService } from 'vs/platform/files/common/files';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { IRemoteConnectionData } from 'vs/platform/remote/common/remoteAuthorityResolver';
|
||||
import { IRequestService } from 'vs/platform/request/common/request';
|
||||
import { loadLocalResource, readFileStream, WebviewFileReadResponse, webviewPartitionId, WebviewResourceFileReader, WebviewResourceResponse } from 'vs/platform/webview/common/resourceLoader';
|
||||
import { WebviewManagerDidLoadResourceResponse } from 'vs/platform/webview/common/webviewManagerService';
|
||||
import { IWindowsMainService } from 'vs/platform/windows/electron-main/windows';
|
||||
import { webviewPartitionId } from 'vs/platform/webview/common/webviewManagerService';
|
||||
|
||||
interface WebviewMetadata {
|
||||
readonly windowId: number;
|
||||
readonly extensionLocation: URI | undefined;
|
||||
readonly localResourceRoots: readonly URI[];
|
||||
readonly remoteConnectionData: IRemoteConnectionData | null;
|
||||
}
|
||||
|
||||
export class WebviewProtocolProvider extends Disposable {
|
||||
|
||||
private static validWebviewFilePaths = new Map([
|
||||
['/index.html', 'index.html'],
|
||||
['/electron-browser/index.html', 'index.html'],
|
||||
['/fake.html', 'fake.html'],
|
||||
['/electron-browser-index.html', 'index.html'],
|
||||
['/main.js', 'main.js'],
|
||||
['/host.js', 'host.js'],
|
||||
['/service-worker.js', 'service-worker.js'],
|
||||
]);
|
||||
|
||||
private readonly webviewMetadata = new Map<string, WebviewMetadata>();
|
||||
|
||||
private requestIdPool = 1;
|
||||
private readonly pendingResourceReads = new Map<number, { resolve: (content: WebviewManagerDidLoadResourceResponse) => void }>();
|
||||
|
||||
constructor(
|
||||
@IFileService private readonly fileService: IFileService,
|
||||
@ILogService private readonly logService: ILogService,
|
||||
@IRequestService private readonly requestService: IRequestService,
|
||||
@IWindowsMainService private readonly windowsMainService: IWindowsMainService,
|
||||
) {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
const sess = session.fromPartition(webviewPartitionId);
|
||||
@@ -53,80 +30,6 @@ export class WebviewProtocolProvider extends Disposable {
|
||||
const webviewHandler = this.handleWebviewRequest.bind(this);
|
||||
protocol.registerFileProtocol(Schemas.vscodeWebview, webviewHandler);
|
||||
sess.protocol.registerFileProtocol(Schemas.vscodeWebview, webviewHandler);
|
||||
|
||||
// Register the protocol loading webview resources both inside the webview and at the top level
|
||||
const webviewResourceHandler = this.handleWebviewResourceRequest.bind(this);
|
||||
protocol.registerStreamProtocol(Schemas.vscodeWebviewResource, webviewResourceHandler);
|
||||
sess.protocol.registerStreamProtocol(Schemas.vscodeWebviewResource, webviewResourceHandler);
|
||||
|
||||
this._register(toDisposable(() => {
|
||||
protocol.unregisterProtocol(Schemas.vscodeWebviewResource);
|
||||
sess.protocol.unregisterProtocol(Schemas.vscodeWebviewResource);
|
||||
protocol.unregisterProtocol(Schemas.vscodeWebview);
|
||||
sess.protocol.unregisterProtocol(Schemas.vscodeWebview);
|
||||
}));
|
||||
}
|
||||
|
||||
private streamToNodeReadable(stream: VSBufferReadableStream): Readable {
|
||||
return new class extends Readable {
|
||||
private listening = false;
|
||||
|
||||
_read(size?: number): void {
|
||||
if (!this.listening) {
|
||||
this.listening = true;
|
||||
|
||||
// Data
|
||||
stream.on('data', data => {
|
||||
try {
|
||||
if (!this.push(data.buffer)) {
|
||||
stream.pause(); // pause the stream if we should not push anymore
|
||||
}
|
||||
} catch (error) {
|
||||
this.emit(error);
|
||||
}
|
||||
});
|
||||
|
||||
// End
|
||||
stream.on('end', () => {
|
||||
try {
|
||||
this.push(null); // signal EOS
|
||||
} catch (error) {
|
||||
this.emit(error);
|
||||
}
|
||||
});
|
||||
|
||||
// Error
|
||||
stream.on('error', error => this.emit('error', error));
|
||||
}
|
||||
|
||||
// ensure the stream is flowing
|
||||
stream.resume();
|
||||
}
|
||||
|
||||
_destroy(error: Error | null, callback: (error: Error | null) => void): void {
|
||||
stream.destroy();
|
||||
|
||||
callback(null);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public async registerWebview(id: string, metadata: WebviewMetadata): Promise<void> {
|
||||
this.webviewMetadata.set(id, metadata);
|
||||
}
|
||||
|
||||
public unregisterWebview(id: string): void {
|
||||
this.webviewMetadata.delete(id);
|
||||
}
|
||||
|
||||
public async updateWebviewMetadata(id: string, metadataDelta: Partial<WebviewMetadata>): Promise<void> {
|
||||
const entry = this.webviewMetadata.get(id);
|
||||
if (entry) {
|
||||
this.webviewMetadata.set(id, {
|
||||
...entry,
|
||||
...metadataDelta,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private async handleWebviewRequest(
|
||||
@@ -149,156 +52,4 @@ export class WebviewProtocolProvider extends Disposable {
|
||||
}
|
||||
callback({ error: -10 /* ACCESS_DENIED - https://cs.chromium.org/chromium/src/net/base/net_error_list.h?l=32 */ });
|
||||
}
|
||||
|
||||
private async handleWebviewResourceRequest(
|
||||
request: Electron.ProtocolRequest,
|
||||
callback: (stream: NodeJS.ReadableStream | Electron.ProtocolResponse) => void
|
||||
) {
|
||||
try {
|
||||
const uri = URI.parse(request.url);
|
||||
const ifNoneMatch = request.headers['If-None-Match'];
|
||||
|
||||
const id = uri.authority;
|
||||
const metadata = this.webviewMetadata.get(id);
|
||||
if (metadata) {
|
||||
|
||||
// Try to further rewrite remote uris so that they go to the resolved server on the main thread
|
||||
let rewriteUri: undefined | ((uri: URI) => URI);
|
||||
if (metadata.remoteConnectionData) {
|
||||
rewriteUri = (uri) => {
|
||||
if (metadata.remoteConnectionData) {
|
||||
if (uri.scheme === Schemas.vscodeRemote || (metadata.extensionLocation?.scheme === Schemas.vscodeRemote)) {
|
||||
return URI.parse(`http://${metadata.remoteConnectionData.host}:${metadata.remoteConnectionData.port}`).with({
|
||||
path: '/vscode-remote-resource',
|
||||
query: `tkn=${metadata.remoteConnectionData.connectionToken}&path=${encodeURIComponent(uri.path)}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
return uri;
|
||||
};
|
||||
}
|
||||
|
||||
const fileReader: WebviewResourceFileReader = {
|
||||
readFileStream: async (resource: URI, etag: string | undefined): Promise<WebviewFileReadResponse.Response> => {
|
||||
if (resource.scheme === Schemas.file) {
|
||||
return readFileStream(this.fileService, resource, etag);
|
||||
}
|
||||
|
||||
// Unknown uri scheme. Try delegating the file read back to the renderer
|
||||
// process which should have a file system provider registered for the uri.
|
||||
|
||||
const window = this.windowsMainService.getWindowById(metadata.windowId);
|
||||
if (!window) {
|
||||
throw new FileOperationError('Could not find window for resource', FileOperationResult.FILE_NOT_FOUND);
|
||||
}
|
||||
|
||||
const requestId = this.requestIdPool++;
|
||||
const p = new Promise<WebviewManagerDidLoadResourceResponse>(resolve => {
|
||||
this.pendingResourceReads.set(requestId, { resolve });
|
||||
});
|
||||
|
||||
window.send(`vscode:loadWebviewResource-${id}`, requestId, uri);
|
||||
|
||||
const result = await p;
|
||||
switch (result) {
|
||||
case 'access-denied':
|
||||
throw new FileOperationError('Could not read file', FileOperationResult.FILE_PERMISSION_DENIED);
|
||||
|
||||
case 'not-found':
|
||||
throw new FileOperationError('Could not read file', FileOperationResult.FILE_NOT_FOUND);
|
||||
|
||||
case 'not-modified':
|
||||
return WebviewFileReadResponse.NotModified;
|
||||
|
||||
default:
|
||||
return new WebviewFileReadResponse.StreamSuccess(bufferToStream(result.buffer), result.etag);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const result = await loadLocalResource(uri, ifNoneMatch, {
|
||||
extensionLocation: metadata.extensionLocation,
|
||||
roots: metadata.localResourceRoots,
|
||||
remoteConnectionData: metadata.remoteConnectionData,
|
||||
rewriteUri,
|
||||
}, fileReader, this.requestService, this.logService, CancellationToken.None);
|
||||
|
||||
switch (result.type) {
|
||||
case WebviewFileReadResponse.Type.Success:
|
||||
{
|
||||
const cacheHeaders: Record<string, string> = result.etag ? {
|
||||
'ETag': result.etag,
|
||||
'Cache-Control': 'no-cache'
|
||||
} : {};
|
||||
|
||||
const ifNoneMatch = request.headers['If-None-Match'];
|
||||
if (ifNoneMatch && result.etag === ifNoneMatch) {
|
||||
/*
|
||||
* Note that the server generating a 304 response MUST
|
||||
* generate any of the following header fields that would
|
||||
* have been sent in a 200 (OK) response to the same request:
|
||||
* Cache-Control, Content-Location, Date, ETag, Expires, and Vary.
|
||||
* (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match)
|
||||
*/
|
||||
return callback({
|
||||
statusCode: 304, // not modified
|
||||
data: undefined, // The request fails if `data` is not set
|
||||
headers: {
|
||||
'Content-Type': result.mimeType,
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
...cacheHeaders
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return callback({
|
||||
statusCode: 200,
|
||||
data: this.streamToNodeReadable(result.stream),
|
||||
headers: {
|
||||
'Content-Type': result.mimeType,
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
...cacheHeaders
|
||||
}
|
||||
});
|
||||
}
|
||||
case WebviewResourceResponse.Type.NotModified:
|
||||
{
|
||||
/*
|
||||
* Note that the server generating a 304 response MUST
|
||||
* generate any of the following header fields that would
|
||||
* have been sent in a 200 (OK) response to the same request:
|
||||
* Cache-Control, Content-Location, Date, ETag, Expires, and Vary.
|
||||
* (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match)
|
||||
*/
|
||||
return callback({
|
||||
statusCode: 304, // not modified
|
||||
data: undefined, // The request fails if `data` is not set
|
||||
headers: {
|
||||
'Content-Type': result.mimeType,
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
case WebviewResourceResponse.Type.AccessDenied:
|
||||
{
|
||||
console.error('Webview: Cannot load resource outside of protocol root');
|
||||
return callback({ data: undefined, statusCode: 401 });
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// noop
|
||||
}
|
||||
|
||||
return callback({ data: undefined, statusCode: 404 });
|
||||
}
|
||||
|
||||
public didLoadResource(requestId: number, response: WebviewManagerDidLoadResourceResponse) {
|
||||
const pendingRead = this.pendingResourceReads.get(requestId);
|
||||
if (!pendingRead) {
|
||||
throw new Error('Unknown request');
|
||||
}
|
||||
this.pendingResourceReads.delete(requestId);
|
||||
pendingRead.resolve(response);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user