Skip to content

Commit

Permalink
Redirect vscode-resource requests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Nov 15, 2018
1 parent 2ff2c61 commit 02c20ee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ export class WebviewEditor extends BaseWebviewEditor {
allowSvgs: true,
enableWrappedPostMessage: true,
useSameOriginForRoot: false,
localResourceRoots: input.options.localResourceRoots || this.getDefaultLocalResourceRoots()
localResourceRoots: input.options.localResourceRoots || this.getDefaultLocalResourceRoots(),
extensionLocation: input.extensionLocation
}, input.options.retainContextWhenHidden);

if (this._webviewContent) {
Expand Down Expand Up @@ -233,7 +234,8 @@ export class WebviewEditor extends BaseWebviewEditor {
this._partService.getContainer(Parts.EDITOR_PART),
{
enableWrappedPostMessage: true,
useSameOriginForRoot: false
useSameOriginForRoot: false,
extensionLocation: input.extensionLocation
});
this._webview.mountTo(this._webviewContent);
input.webview = this._webview;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface WebviewOptions {
readonly enableWrappedPostMessage?: boolean;
readonly useSameOriginForRoot?: boolean;
readonly localResourceRoots?: ReadonlyArray<URI>;
readonly extensionLocation?: URI;
}

export class WebviewElement extends Disposable {
Expand Down Expand Up @@ -356,11 +357,11 @@ export class WebviewElement extends Disposable {

const appRootUri = URI.file(this._environmentService.appRoot);

registerFileProtocol(contents, WebviewProtocol.CoreResource, this._fileService, () => [
registerFileProtocol(contents, WebviewProtocol.CoreResource, this._fileService, null, () => [
appRootUri
]);

registerFileProtocol(contents, WebviewProtocol.VsCodeResource, this._fileService, () =>
registerFileProtocol(contents, WebviewProtocol.VsCodeResource, this._fileService, this._options.extensionLocation, () =>
(this._options.localResourceRoots || [])
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,52 @@ import { nativeSep } from 'vs/base/common/paths';
import { startsWith } from 'vs/base/common/strings';
import { URI } from 'vs/base/common/uri';
import { IFileService } from 'vs/platform/files/common/files';
import { REMOTE_HOST_SCHEME } from 'vs/platform/remote/common/remoteHosts';

export const enum WebviewProtocol {
CoreResource = 'vscode-core-resource',
VsCodeResource = 'vscode-resource'
}

function resolveContent(fileService: IFileService, resource: URI, mime: string, callback: any): void {
fileService.resolveContent(resource, { encoding: 'binary' }).then(contents => {
callback({
data: Buffer.from(contents.value, contents.encoding),
mimeType: mime
});
}, (err) => {
console.log(err);
callback({ error: -2 /* FAILED: https://cs.chromium.org/chromium/src/net/base/net_error_list.h */ });
});
}

export function registerFileProtocol(
contents: Electron.WebContents,
protocol: WebviewProtocol,
fileService: IFileService,
extensionLocation: URI | null | undefined,
getRoots: () => ReadonlyArray<URI>
) {
contents.session.protocol.registerBufferProtocol(protocol, (request, callback: any) => {
if (extensionLocation && extensionLocation.scheme === REMOTE_HOST_SCHEME) {
const requestUri = URI.parse(request.url);
const redirectedUri = URI.from({
scheme: REMOTE_HOST_SCHEME,
authority: extensionLocation.authority,
path: '/vscode-resource',
query: JSON.stringify({
requestResourcePath: requestUri.path
})
});
resolveContent(fileService, redirectedUri, getMimeType(requestUri), callback);
return;
}

const requestPath = URI.parse(request.url).path;
const normalizedPath = URI.file(requestPath);
for (const root of getRoots()) {
if (startsWith(normalizedPath.fsPath, root.fsPath + nativeSep)) {
fileService.resolveContent(normalizedPath, { encoding: 'binary' }).then(contents => {
const mime = getMimeType(normalizedPath);
callback({
data: Buffer.from(contents.value, contents.encoding),
mimeType: mime
});
}, () => {
callback({ error: -2 /* FAILED: https://cs.chromium.org/chromium/src/net/base/net_error_list.h */ });
});
resolveContent(fileService, normalizedPath, getMimeType(normalizedPath), callback);
return;
}
}
Expand Down

0 comments on commit 02c20ee

Please sign in to comment.