Skip to content

Commit

Permalink
eclipse-che/che#9990 set preferences and query parameters before plug…
Browse files Browse the repository at this point in the history
…in init

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
  • Loading branch information
evidolob authored and akurinnoy committed Oct 12, 2018
1 parent bfb763c commit f3cdf96
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 64 deletions.
11 changes: 6 additions & 5 deletions packages/plugin-ext/src/api/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import {

export interface PluginInitData {
plugins: PluginMetadata[];
preferences: { [key: string]: any };
env: EnvInit;
}

export interface Plugin {
Expand All @@ -52,6 +54,10 @@ export interface Plugin {
lifecycle: PluginLifecycle;
}

export interface EnvInit {
queryParams: QueryParameters;
}

export interface PluginAPI {

}
Expand Down Expand Up @@ -572,10 +578,6 @@ export interface EnvMain {
$getEnvVariable(envVarName: string): Promise<string | undefined>;
}

export interface EnvExt {
$setQueryParameters(queryParams: QueryParameters): void;
}

export interface PreferenceRegistryMain {
$updateConfigurationOption(
target: boolean | ConfigurationTarget | undefined,
Expand Down Expand Up @@ -703,7 +705,6 @@ export const MAIN_RPC_CONTEXT = {
TEXT_EDITORS_EXT: createProxyIdentifier<TextEditorsExt>('TextEditorsExt'),
EDITORS_AND_DOCUMENTS_EXT: createProxyIdentifier<EditorsAndDocumentsExt>('EditorsAndDocumentsExt'),
DOCUMENTS_EXT: createProxyIdentifier<DocumentsExt>('DocumentsExt'),
ENV_EXT: createProxyIdentifier<EnvExt>('EnvExt'),
TERMINAL_EXT: createProxyIdentifier<TerminalServiceExt>('TerminalServiceExt'),
PREFERENCE_REGISTRY_EXT: createProxyIdentifier<PreferenceRegistryExt>('PreferenceRegistryExt'),
LANGUAGES_EXT: createProxyIdentifier<LanguagesExt>('LanguagesExt'),
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin-ext/src/hosted/browser/hosted-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { RPCProtocol, RPCProtocolImpl } from '../../api/rpc-protocol';
import { ILogger } from '@theia/core';
import { PreferenceServiceImpl } from '@theia/core/lib/browser';
import { PluginContributionHandler } from '../../main/browser/plugin-contribution-handler';
import { getQueryParameters } from '../../main/browser/env-main';

@injectable()
export class HostedPluginSupport {
Expand Down Expand Up @@ -69,7 +70,7 @@ export class HostedPluginSupport {
if (frontend) {
const worker = new PluginWorker();
const hostedExtManager = worker.rpc.getProxy(MAIN_RPC_CONTEXT.HOSTED_PLUGIN_MANAGER_EXT);
hostedExtManager.$init({ plugins: pluginsMetadata });
hostedExtManager.$init({ plugins: pluginsMetadata, preferences: this.preferenceServiceImpl.getPreferences(), env: { queryParams: getQueryParameters() } });
setUpPluginApi(worker.rpc, container);
}

Expand All @@ -78,7 +79,7 @@ export class HostedPluginSupport {
pluginsMetadata.forEach(pluginMetadata => {
const rpc = this.createServerRpc(pluginMetadata);
const hostedExtManager = rpc.getProxy(MAIN_RPC_CONTEXT.HOSTED_PLUGIN_MANAGER_EXT);
hostedExtManager.$init({ plugins: [pluginMetadata] });
hostedExtManager.$init({ plugins: [pluginMetadata], preferences: this.preferenceServiceImpl.getPreferences(), env: { queryParams: getQueryParameters() } });
setUpPluginApi(rpc, container);
});
}
Expand Down
9 changes: 7 additions & 2 deletions packages/plugin-ext/src/hosted/browser/worker/worker-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { MAIN_RPC_CONTEXT, Plugin, emptyPlugin } from '../../../api/plugin-api';
import { createAPIFactory } from '../../../plugin/plugin-context';
import { getPluginId, PluginMetadata } from '../../../common/plugin-protocol';
import * as theia from '@theia/plugin';
import { EnvExtImpl } from '../../../plugin/env';
import { PreferenceRegistryExtImpl } from '../../../plugin/preference-registry';

// tslint:disable-next-line:no-any
const ctx = self as any;
Expand All @@ -42,6 +44,8 @@ addEventListener('message', (message: any) => {
function initialize(contextPath: string, pluginMetadata: PluginMetadata): void {
ctx.importScripts('/context/' + contextPath);
}
const envExt = new EnvExtImpl(rpc);
const preferenceRegistryExt = new PreferenceRegistryExtImpl(rpc);

const pluginManager = new PluginManagerExtImpl({
// tslint:disable-next-line:no-any
Expand Down Expand Up @@ -97,9 +101,9 @@ const pluginManager = new PluginManagerExtImpl({

return [result, foreign];
}
});
}, envExt, preferenceRegistryExt);

const apiFactory = createAPIFactory(rpc, pluginManager);
const apiFactory = createAPIFactory(rpc, pluginManager, envExt, preferenceRegistryExt);
let defaultApi: typeof theia;

const handler = {
Expand All @@ -121,6 +125,7 @@ const handler = {
ctx['theia'] = new Proxy(Object.create(null), handler);

rpc.set(MAIN_RPC_CONTEXT.HOSTED_PLUGIN_MANAGER_EXT, pluginManager);
rpc.set(MAIN_RPC_CONTEXT.PREFERENCE_REGISTRY_EXT, preferenceRegistryExt);

function isElectron() {
if (typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('Electron') >= 0) {
Expand Down
13 changes: 9 additions & 4 deletions packages/plugin-ext/src/hosted/node/plugin-host-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { PluginManagerExtImpl } from '../../plugin/plugin-manager';
import { MAIN_RPC_CONTEXT, Plugin, PluginAPIFactory } from '../../api/plugin-api';
import { PluginMetadata } from '../../common/plugin-protocol';
import { createAPIFactory } from '../../plugin/plugin-context';
import { EnvExtImpl } from '../../plugin/env';
import { PreferenceRegistryExtImpl } from '../../plugin/preference-registry';

/**
* Handle the RPC calls.
Expand All @@ -32,9 +34,12 @@ export class PluginHostRPC {
}

initialize() {
this.pluginManager = this.createPluginManager();
const envExt = new EnvExtImpl(this.rpc);
const preferenceRegistryExt = new PreferenceRegistryExtImpl(this.rpc);
this.pluginManager = this.createPluginManager(envExt, preferenceRegistryExt);
this.rpc.set(MAIN_RPC_CONTEXT.HOSTED_PLUGIN_MANAGER_EXT, this.pluginManager);
PluginHostRPC.apiFactory = createAPIFactory(this.rpc, this.pluginManager);
this.rpc.set(MAIN_RPC_CONTEXT.PREFERENCE_REGISTRY_EXT, preferenceRegistryExt);
PluginHostRPC.apiFactory = createAPIFactory(this.rpc, this.pluginManager, envExt, preferenceRegistryExt);
}

// tslint:disable-next-line:no-any
Expand All @@ -48,7 +53,7 @@ export class PluginHostRPC {
}
}

createPluginManager(): PluginManagerExtImpl {
createPluginManager(envExt: EnvExtImpl, preferencesManager: PreferenceRegistryExtImpl): PluginManagerExtImpl {
const pluginManager = new PluginManagerExtImpl({
loadPlugin(plugin: Plugin): void {
console.log('PLUGIN_HOST(' + process.pid + '): PluginManagerExtImpl/loadPlugin(' + plugin.pluginPath + ')');
Expand Down Expand Up @@ -96,7 +101,7 @@ export class PluginHostRPC {
}
return [result, foreign];
}
});
}, envExt, preferencesManager);
return pluginManager;
}
}
81 changes: 38 additions & 43 deletions packages/plugin-ext/src/main/browser/env-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,60 @@
import { interfaces } from 'inversify';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { RPCProtocol } from '../../api/rpc-protocol';
import { EnvMain, EnvExt, MAIN_RPC_CONTEXT } from '../../api/plugin-api';
import { EnvMain } from '../../api/plugin-api';
import { QueryParameters } from '../../common/env';

export class EnvMainImpl implements EnvMain {
private proxy: EnvExt;
private envVariableServer: EnvVariablesServer;

constructor(rpc: RPCProtocol, container: interfaces.Container) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.ENV_EXT);

this.envVariableServer = container.get(EnvVariablesServer);
}

this.proxy.$setQueryParameters(this.getQueryParameters());
$getEnvVariable(envVarName: string): Promise<string | undefined> {
return this.envVariableServer.getValue(envVarName).then(result => result ? result.value : undefined);
}

/**
* Returns query parameters from current page.
*/
private getQueryParameters(): QueryParameters {
const queryParameters: QueryParameters = {};
if (window.location.search !== '') {
const queryParametersString = window.location.search.substr(1); // remove question mark
const params = queryParametersString.split('&');
for (const pair of params) {
if (pair === '') {
continue;
}
}

/**
* Returns query parameters from current page.
*/
export function getQueryParameters(): QueryParameters {
const queryParameters: QueryParameters = {};
if (window.location.search !== '') {
const queryParametersString = window.location.search.substr(1); // remove question mark
const params = queryParametersString.split('&');
for (const pair of params) {
if (pair === '') {
continue;
}

const keyValue = pair.split('=');
let key: string = keyValue[0];
let value: string = keyValue[1] ? keyValue[1] : '';
try {
key = decodeURIComponent(key);
if (value !== '') {
value = decodeURIComponent(value);
}
} catch (error) {
// skip malformed URI sequence
continue;
const keyValue = pair.split('=');
let key: string = keyValue[0];
let value: string = keyValue[1] ? keyValue[1] : '';
try {
key = decodeURIComponent(key);
if (value !== '') {
value = decodeURIComponent(value);
}
} catch (error) {
// skip malformed URI sequence
continue;
}

const existedValue = queryParameters[key];
if (existedValue) {
if (existedValue instanceof Array) {
existedValue.push(value);
} else {
// existed value is string
queryParameters[key] = [existedValue, value];
}
const existedValue = queryParameters[key];
if (existedValue) {
if (existedValue instanceof Array) {
existedValue.push(value);
} else {
queryParameters[key] = value;
// existed value is string
queryParameters[key] = [existedValue, value];
}
} else {
queryParameters[key] = value;
}
}
return queryParameters;
}

$getEnvVariable(envVarName: string): Promise<string | undefined> {
return this.envVariableServer.getValue(envVarName).then(result => result ? result.value : undefined);
}

return queryParameters;
}
6 changes: 3 additions & 3 deletions packages/plugin-ext/src/plugin/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
********************************************************************************/

import { RPCProtocol } from '../api/rpc-protocol';
import { EnvMain, EnvExt, PLUGIN_RPC_CONTEXT } from '../api/plugin-api';
import { EnvMain, PLUGIN_RPC_CONTEXT } from '../api/plugin-api';
import { QueryParameters } from '../common/env';

export class EnvExtImpl implements EnvExt {
export class EnvExtImpl {
private proxy: EnvMain;
private queryParameters: QueryParameters;

Expand All @@ -43,7 +43,7 @@ export class EnvExtImpl implements EnvExt {
return this.queryParameters;
}

$setQueryParameters(queryParams: QueryParameters): void {
setQueryParameters(queryParams: QueryParameters): void {
this.queryParameters = queryParams;
}
}
9 changes: 6 additions & 3 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ import { fromDocumentSelector } from './type-converters';
import { DialogsExtImpl } from './dialogs';
import { MarkdownString } from './markdown-string';

export function createAPIFactory(rpc: RPCProtocol, pluginManager: PluginManager): PluginAPIFactory {
export function createAPIFactory(
rpc: RPCProtocol,
pluginManager: PluginManager,
envExt: EnvExtImpl,
preferenceRegistryExt: PreferenceRegistryExtImpl): PluginAPIFactory {

const commandRegistryExt = rpc.set(MAIN_RPC_CONTEXT.COMMAND_REGISTRY_EXT, new CommandRegistryImpl(rpc));
const quickOpenExt = rpc.set(MAIN_RPC_CONTEXT.QUICK_OPEN_EXT, new QuickOpenExtImpl(rpc));
const dialogsExt = new DialogsExtImpl(rpc);
Expand All @@ -90,8 +95,6 @@ export function createAPIFactory(rpc: RPCProtocol, pluginManager: PluginManager)
const workspaceExt = rpc.set(MAIN_RPC_CONTEXT.WORKSPACE_EXT, new WorkspaceExtImpl(rpc));
const statusBarMessageRegistryExt = new StatusBarMessageRegistryExt(rpc);
const terminalExt = rpc.set(MAIN_RPC_CONTEXT.TERMINAL_EXT, new TerminalServiceExtImpl(rpc));
const envExt = rpc.set(MAIN_RPC_CONTEXT.ENV_EXT, new EnvExtImpl(rpc));
const preferenceRegistryExt = rpc.set(MAIN_RPC_CONTEXT.PREFERENCE_REGISTRY_EXT, new PreferenceRegistryExtImpl(rpc));
const outputChannelRegistryExt = new OutputChannelRegistryExt(rpc);
const languagesExt = rpc.set(MAIN_RPC_CONTEXT.LANGUAGES_EXT, new LanguagesExtImpl(rpc, documents));

Expand Down
11 changes: 10 additions & 1 deletion packages/plugin-ext/src/plugin/plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import * as theia from '@theia/plugin';
import { join } from 'path';
import { dispose } from '../common/disposable-util';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { EnvExtImpl } from './env';
import { PreferenceRegistryExtImpl } from './preference-registry';

export interface PluginHost {

Expand All @@ -47,7 +49,9 @@ export class PluginManagerExtImpl implements PluginManagerExt, PluginManager {
private activatedPlugins = new Map<string, ActivatedPlugin>();
private pluginActivationPromises = new Map<string, Deferred<void>>();

constructor(private readonly host: PluginHost) {
constructor(private readonly host: PluginHost,
private readonly envExt: EnvExtImpl,
private readonly preferencesManager: PreferenceRegistryExtImpl) {
}

$stopPlugin(contextPath: string): PromiseLike<void> {
Expand All @@ -66,6 +70,11 @@ export class PluginManagerExtImpl implements PluginManagerExt, PluginManager {
}

$init(pluginInit: PluginInitData): PromiseLike<void> {
// init query parameters
this.envExt.setQueryParameters(pluginInit.env.queryParams);

this.preferencesManager.init(pluginInit.preferences);

const [plugins, foreignPlugins] = this.host.init(pluginInit.plugins);
// add foreign plugins
for (const plugin of foreignPlugins) {
Expand Down
10 changes: 9 additions & 1 deletion packages/plugin-ext/src/plugin/preference-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface ConfigurationInspect<T> {
workspaceFolderValue?: T;
}

// tslint:disable-next-line:no-any
function lookUp(tree: any, key: string): any {
if (!key) {
return;
Expand All @@ -51,6 +52,7 @@ function lookUp(tree: any, key: string): any {

export class PreferenceRegistryExtImpl implements PreferenceRegistryExt {
private proxy: PreferenceRegistryMain;
// tslint:disable-next-line:no-any
private _preferences: any;
private readonly _onDidChangeConfiguration = new Emitter<theia.ConfigurationChangeEvent>();

Expand All @@ -60,8 +62,14 @@ export class PreferenceRegistryExtImpl implements PreferenceRegistryExt {
this.proxy = rpc.getProxy(PLUGIN_RPC_CONTEXT.PREFERENCE_REGISTRY_MAIN);
}

$acceptConfigurationChanged(data: { [key: string]: any }, eventData: PreferenceChange): void {
// tslint:disable-next-line:no-any
init(data: { [key: string]: any }): void {
this._preferences = this.parse(data);
}

// tslint:disable-next-line:no-any
$acceptConfigurationChanged(data: { [key: string]: any }, eventData: PreferenceChange): void {
this.init(data);
this._onDidChangeConfiguration.fire(this.toConfigurationChangeEvent(eventData));
}

Expand Down

0 comments on commit f3cdf96

Please sign in to comment.