Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[electron] fix resolution of hostname placeholder #7823

Merged
merged 1 commit into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -189,4 +190,4 @@
]
}
]
}
}
2 changes: 1 addition & 1 deletion packages/core/src/browser/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,9 +33,15 @@ export class WebviewEnvironment {
@postConstruct()
protected async init(): Promise<void> {
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);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-ext/src/main/common/webview-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
9 changes: 8 additions & 1 deletion packages/plugin-ext/src/main/node/plugin-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/';

Expand All @@ -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}}', '.+');
}
Expand Down