From bb43e9ea7508687932e386a344f2afc0e915e451 Mon Sep 17 00:00:00 2001 From: Anton Kosyakov Date: Fri, 15 May 2020 07:42:10 +0000 Subject: [PATCH] [electron] fix resolution of hostname placeholder Signed-off-by: Anton Kosyakov --- .vscode/launch.json | 5 +++-- packages/core/src/browser/endpoint.ts | 2 +- .../src/main/browser/webview/webview-environment.ts | 13 ++++++++++--- .../plugin-ext/src/main/common/webview-protocol.ts | 5 +++-- packages/plugin-ext/src/main/node/plugin-service.ts | 9 ++++++++- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 5a5a5af3b346e..3a88680b430c3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -33,7 +33,8 @@ "--no-cluster", "--app-project-path=${workspaceRoot}/examples/electron", "--remote-debugging-port=9222", - "--no-app-auto-install" + "--no-app-auto-install", + "--plugins=local-dir:../../plugins" ], "env": { "NODE_ENV": "development", @@ -189,4 +190,4 @@ ] } ] -} +} \ No newline at end of file diff --git a/packages/core/src/browser/endpoint.ts b/packages/core/src/browser/endpoint.ts index 8d2f95da51aef..21f3ee124a561 100644 --- a/packages/core/src/browser/endpoint.ts +++ b/packages/core/src/browser/endpoint.ts @@ -52,7 +52,7 @@ export class Endpoint { return this.location.pathname; } - protected get host(): string { + get host(): string { if (this.options.host) { return this.options.host; } diff --git a/packages/plugin-ext/src/main/browser/webview/webview-environment.ts b/packages/plugin-ext/src/main/browser/webview/webview-environment.ts index 520134bdf5f48..aa3657ee14196 100644 --- a/packages/plugin-ext/src/main/browser/webview/webview-environment.ts +++ b/packages/plugin-ext/src/main/browser/webview/webview-environment.ts @@ -20,6 +20,7 @@ import { Deferred } from '@theia/core/lib/common/promise-util'; import { EnvVariablesServer } from '@theia/core/lib/common/env-variables'; import URI from '@theia/core/lib/common/uri'; import { WebviewExternalEndpoint } from '../../common/webview-protocol'; +import { environment } from '@theia/application-package/lib/environment'; @injectable() export class WebviewEnvironment { @@ -32,9 +33,15 @@ export class WebviewEnvironment { @postConstruct() protected async init(): Promise { try { - const variable = await this.environments.getValue(WebviewExternalEndpoint.pattern); - const value = variable && variable.value || WebviewExternalEndpoint.defaultPattern; - this.externalEndpointHost.resolve(value.replace('{{hostname}}', window.location.host || 'localhost')); + let endpointPattern; + if (environment.electron.is()) { + endpointPattern = WebviewExternalEndpoint.defaultPattern; + } else { + const variable = await this.environments.getValue(WebviewExternalEndpoint.pattern); + endpointPattern = variable && variable.value || WebviewExternalEndpoint.defaultPattern; + } + const { host } = new Endpoint(); + this.externalEndpointHost.resolve(endpointPattern.replace('{{hostname}}', host)); } catch (e) { this.externalEndpointHost.reject(e); } diff --git a/packages/plugin-ext/src/main/common/webview-protocol.ts b/packages/plugin-ext/src/main/common/webview-protocol.ts index 58a06a59fccb7..51c62f9a8df76 100644 --- a/packages/plugin-ext/src/main/common/webview-protocol.ts +++ b/packages/plugin-ext/src/main/common/webview-protocol.ts @@ -18,8 +18,9 @@ * Each webview should be deployed on a unique origin (https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) * to ensure isolation from browser shared state as cookies, local storage and so on. * - * Use `THEIA_WEBVIEW_EXTERNAL_ENDPOINT` to customize the hostname pattern of a origin. - * By default is `{{uuid}}.webview.{{hostname}}`. Where `{{uuid}}` is a placeholder for a webview global id. + * Default hostname pattern of a origin is `{{uuid}}.webview.{{hostname}}`. Where `{{uuid}}` is a placeholder for a webview global id. + * For electron target the default pattern is always used. + * For the browser target use `THEIA_WEBVIEW_EXTERNAL_ENDPOINT` env variable to customize it. */ export namespace WebviewExternalEndpoint { export const pattern = 'THEIA_WEBVIEW_EXTERNAL_ENDPOINT'; diff --git a/packages/plugin-ext/src/main/node/plugin-service.ts b/packages/plugin-ext/src/main/node/plugin-service.ts index 1f2572f5672bb..f98aefde36300 100644 --- a/packages/plugin-ext/src/main/node/plugin-service.ts +++ b/packages/plugin-ext/src/main/node/plugin-service.ts @@ -22,6 +22,7 @@ import * as express from 'express'; import { BackendApplicationContribution } from '@theia/core/lib/node/backend-application'; import { injectable } from 'inversify'; import { WebviewExternalEndpoint } from '../common/webview-protocol'; +import { environment } from '@theia/application-package/lib/environment'; const pluginPath = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + './theia/plugins/'; @@ -42,7 +43,13 @@ export class PluginApiContribution implements BackendApplicationContribution { } protected webviewExternalEndpoint(): string { - return (process.env[WebviewExternalEndpoint.pattern] || WebviewExternalEndpoint.defaultPattern) + let endpointPattern; + if (environment.electron.is()) { + endpointPattern = WebviewExternalEndpoint.defaultPattern; + } else { + endpointPattern = process.env[WebviewExternalEndpoint.pattern] || WebviewExternalEndpoint.defaultPattern; + } + return endpointPattern .replace('{{uuid}}', '.+') .replace('{{hostname}}', '.+'); }